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: {
@@ -13,15 +15,18 @@ const props = defineProps({
1315})
1416
1517const document = defineModel <DatabasePageItem >()
18+ const { mediaTree, host } = useStudio ()
1619
1720const editor = shallowRef <Editor .IStandaloneCodeEditor | null >(null )
1821const editorRef = ref ()
1922const content = ref <string >(' ' )
2023const currentDocumentId = ref <string | null >(null )
24+ const localStatus = ref <DraftStatus >(props .draftItem .status )
2125
2226// Trigger on action events
23- watch (() => props .draftItem .status , () => {
24- if (editor .value ) {
27+ watch (() => props .draftItem .status , (newStatus ) => {
28+ if (editor .value && newStatus !== localStatus .value ) {
29+ localStatus .value = newStatus
2530 setContent (props .draftItem .modified as DatabasePageItem )
2631 }
2732})
@@ -35,6 +40,7 @@ watch(() => document.value?.id, async () => {
3540
3641onMounted (async () => {
3742 const monaco = await setupMonaco ()
43+ setupSuggestion (monaco .monaco , host .meta .components (), mediaTree .root .value )
3844
3945 // create a Monaco editor instance
4046 editor .value = monaco .createEditor (editorRef .value )
@@ -52,6 +58,10 @@ onMounted(async () => {
5258 content .value = newContent
5359
5460 generateDocumentFromContent (document .value ! .id , content .value ).then ((doc ) => {
61+ // Update local status
62+ localStatus .value = DraftStatus .Updated
63+
64+ // Update document
5565 document .value = {
5666 ... pickReservedKeysFromDocument (props .draftItem .original as DatabasePageItem || document .value ! ),
5767 ... doc ,
@@ -68,7 +78,11 @@ function setContent(document: DatabasePageItem) {
6878 content .value = md || ' '
6979
7080 if (editor .value && editor .value .getModel ()?.getValue () !== md ) {
81+ // Keep the cursor position
82+ const position = editor .value .getPosition ()
7183 editor .value .getModel ()?.setValue (md || ' ' )
84+ // Restore the cursor position
85+ position && editor .value .setPosition (position )
7286 }
7387
7488 currentDocumentId .value = document .id
0 commit comments