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: {
1012 type: Object as PropType <DraftItem >,
1113 required: true ,
1214 },
15+ readOnly: {
16+ type: Boolean ,
17+ required: false ,
18+ default: false ,
19+ },
1320})
1421
1522const document = defineModel <DatabasePageItem >()
23+ const { mediaTree, host } = useStudio ()
1624
1725const editor = shallowRef <Editor .IStandaloneCodeEditor | null >(null )
1826const editorRef = ref ()
1927const content = ref <string >(' ' )
2028const currentDocumentId = ref <string | null >(null )
29+ const localStatus = ref <DraftStatus >(props .draftItem .status )
2130
2231// Trigger on action events
23- watch (() => props .draftItem .status , () => {
24- if (editor .value ) {
32+ watch (() => props .draftItem .status , (newStatus ) => {
33+ if (editor .value && newStatus !== localStatus .value ) {
34+ localStatus .value = newStatus
2535 setContent (props .draftItem .modified as DatabasePageItem )
2636 }
2737})
@@ -35,10 +45,24 @@ watch(() => document.value?.id, async () => {
3545
3646onMounted (async () => {
3747 const monaco = await setupMonaco ()
48+ setupSuggestion (monaco .monaco , host .meta .components (), mediaTree .root .value )
3849
3950 // create a Monaco editor instance
40- editor .value = monaco .createEditor (editorRef .value )
51+ editor .value = monaco .createEditor (editorRef .value , {
52+ readOnly: props .readOnly ,
53+ scrollbar: props .readOnly
54+ ? {
55+ vertical: ' hidden' ,
56+ horizontal: ' hidden' ,
57+ handleMouseWheel: false ,
58+ }
59+ : undefined ,
60+ })
4161 editor .value .onDidChangeModelContent (() => {
62+ if (props .readOnly ) {
63+ return
64+ }
65+
4266 // Do not trigger model updates if the document id has changed
4367 if (currentDocumentId .value !== document .value ?.id ) {
4468 return
@@ -52,6 +76,10 @@ onMounted(async () => {
5276 content .value = newContent
5377
5478 generateDocumentFromContent (document .value ! .id , content .value ).then ((doc ) => {
79+ // Update local status
80+ localStatus .value = DraftStatus .Updated
81+
82+ // Update document
5583 document .value = {
5684 ... pickReservedKeysFromDocument (props .draftItem .original as DatabasePageItem || document .value ! ),
5785 ... doc ,
@@ -68,7 +96,11 @@ function setContent(document: DatabasePageItem) {
6896 content .value = md || ' '
6997
7098 if (editor .value && editor .value .getModel ()?.getValue () !== md ) {
99+ // Keep the cursor position
100+ const position = editor .value .getPosition ()
71101 editor .value .getModel ()?.setValue (md || ' ' )
102+ // Restore the cursor position
103+ position && editor .value .setPosition (position )
72104 }
73105
74106 currentDocumentId .value = document .id
0 commit comments