11<script setup lang="ts">
22import { onMounted , ref , shallowRef , watch } from ' vue'
33import type { DatabasePageItem , DraftItem } from ' ../../../../types'
4+ import { DraftStatus } from ' ../../../../types/draft'
45import type { PropType } from ' vue'
5- import { setupMonaco , type Editor } from ' ../../../../utils/monaco'
6+ import { setupMonaco , setupSuggestion , type Editor } from ' ../../../../utils/monaco/index '
67import { generateContentFromDocument , generateDocumentFromContent , pickReservedKeysFromDocument } from ' ../../../../utils/content'
8+ import { useStudio } from ' ../../../../composables/useStudio'
79
810const props = defineProps ({
911 draftItem: {
@@ -18,15 +20,18 @@ const props = defineProps({
1820})
1921
2022const document = defineModel <DatabasePageItem >()
23+ const { mediaTree, host } = useStudio ()
2124
2225const editor = shallowRef <Editor .IStandaloneCodeEditor | null >(null )
2326const editorRef = ref ()
2427const content = ref <string >(' ' )
2528const currentDocumentId = ref <string | null >(null )
29+ const localStatus = ref <DraftStatus >(props .draftItem .status )
2630
2731// Trigger on action events
28- watch (() => props .draftItem .status , () => {
29- if (editor .value ) {
32+ watch (() => props .draftItem .status , (newStatus ) => {
33+ if (editor .value && newStatus !== localStatus .value ) {
34+ localStatus .value = newStatus
3035 setContent (props .draftItem .modified as DatabasePageItem )
3136 }
3237})
@@ -40,6 +45,7 @@ watch(() => document.value?.id, async () => {
4045
4146onMounted (async () => {
4247 const monaco = await setupMonaco ()
48+ setupSuggestion (monaco .monaco , host .meta .components (), mediaTree .root .value )
4349
4450 // create a Monaco editor instance
4551 editor .value = monaco .createEditor (editorRef .value , {
@@ -70,6 +76,10 @@ onMounted(async () => {
7076 content .value = newContent
7177
7278 generateDocumentFromContent (document .value ! .id , content .value ).then ((doc ) => {
79+ // Update local status
80+ localStatus .value = DraftStatus .Updated
81+
82+ // Update document
7383 document .value = {
7484 ... pickReservedKeysFromDocument (props .draftItem .original as DatabasePageItem || document .value ! ),
7585 ... doc ,
@@ -86,7 +96,11 @@ function setContent(document: DatabasePageItem) {
8696 content .value = md || ' '
8797
8898 if (editor .value && editor .value .getModel ()?.getValue () !== md ) {
99+ // Keep the cursor position
100+ const position = editor .value .getPosition ()
89101 editor .value .getModel ()?.setValue (md || ' ' )
102+ // Restore the cursor position
103+ position && editor .value .setPosition (position )
90104 }
91105
92106 currentDocumentId .value = document .id
0 commit comments