2023-10-29 22:02:07 +03:00
|
|
|
<script setup lang="ts">
|
2024-03-14 20:05:26 +03:00
|
|
|
/**
|
|
|
|
* Mock project store, used in some histoire stories.
|
|
|
|
*/
|
|
|
|
|
2023-10-29 22:02:07 +03:00
|
|
|
import { useProjectStore } from '@/stores/project'
|
2024-02-02 12:22:18 +03:00
|
|
|
import { Ast } from '@/util/ast'
|
2024-02-06 19:44:24 +03:00
|
|
|
import { reactive, watch } from 'vue'
|
2024-08-08 15:12:05 +03:00
|
|
|
import { SourceDocument } from 'ydoc-shared/ast/sourceDocument'
|
2023-10-29 22:02:07 +03:00
|
|
|
|
|
|
|
const props = defineProps<{ modelValue: string }>()
|
|
|
|
const emit = defineEmits<{ 'update:modelValue': [modelValue: string] }>()
|
|
|
|
|
|
|
|
const projectStore = useProjectStore()
|
|
|
|
const mod = projectStore.projectModel.createNewModule('Main.enso')
|
2024-01-18 16:45:18 +03:00
|
|
|
projectStore.setObservedFileName('Main.enso')
|
2024-07-05 14:13:04 +03:00
|
|
|
mod.doc.ydoc.emit('load', [mod.doc.ydoc])
|
2024-02-02 12:22:18 +03:00
|
|
|
let syncedCode: string | undefined
|
|
|
|
watch(
|
|
|
|
() => projectStore.module,
|
|
|
|
(mod) => {
|
|
|
|
if (!mod) return
|
|
|
|
const syncModule = new Ast.MutableModule(mod.doc.ydoc)
|
|
|
|
applyEdits(syncModule, props.modelValue)
|
2024-02-06 19:44:24 +03:00
|
|
|
const moduleSource = reactive(SourceDocument.Empty())
|
|
|
|
syncModule.observe((update) => moduleSource.applyUpdate(syncModule, update))
|
|
|
|
watch(
|
|
|
|
() => moduleSource.text,
|
|
|
|
(text) => {
|
|
|
|
if (text !== props.modelValue) {
|
|
|
|
syncedCode = text
|
|
|
|
emit('update:modelValue', text)
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
},
|
2024-02-06 19:44:24 +03:00
|
|
|
)
|
2024-02-02 12:22:18 +03:00
|
|
|
watch(
|
|
|
|
() => props.modelValue,
|
|
|
|
(modelValue) => applyEdits(syncModule, modelValue),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2023-10-29 22:02:07 +03:00
|
|
|
|
2024-02-02 12:22:18 +03:00
|
|
|
function applyEdits(syncModule: Ast.MutableModule, newText: string) {
|
|
|
|
if (newText !== syncedCode) {
|
2024-03-14 20:05:26 +03:00
|
|
|
syncModule.transact(() => {
|
2024-02-02 12:22:18 +03:00
|
|
|
syncModule.syncRoot(Ast.parseBlock(newText, syncModule))
|
2024-03-14 20:05:26 +03:00
|
|
|
})
|
2023-10-29 22:02:07 +03:00
|
|
|
}
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
2023-10-29 22:02:07 +03:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<slot></slot>
|
|
|
|
</template>
|