enso/app/gui2/mock/MockProjectStoreWrapper.vue
Paweł Grabarz d9972d547a
Migrate to pnpm (#10422)
Fixes #10197

# Important Notes
From now on, package installation will be using `pnpm install`. Installing it globally is fine for convenience, but it can also be used as `corepack pnpm install` without having to install anything other than node. For now, all other scripts are still invoked using `npm`, so we can still invoke them with usual `--workspace` setting. As far as I can tell that doesn't really have any other side effects and is identical as running the script through `pnpm run` in respective workspace project subdirectory.
2024-07-05 11:13:04 +00:00

55 lines
1.5 KiB
Vue

<script setup lang="ts">
/**
* Mock project store, used in some histoire stories.
*/
import { useProjectStore } from '@/stores/project'
import { Ast } from '@/util/ast'
import { SourceDocument } from 'shared/ast/sourceDocument'
import { reactive, watch } from 'vue'
const props = defineProps<{ modelValue: string }>()
const emit = defineEmits<{ 'update:modelValue': [modelValue: string] }>()
const projectStore = useProjectStore()
const mod = projectStore.projectModel.createNewModule('Main.enso')
projectStore.setObservedFileName('Main.enso')
mod.doc.ydoc.emit('load', [mod.doc.ydoc])
let syncedCode: string | undefined
watch(
() => projectStore.module,
(mod) => {
if (!mod) return
const syncModule = new Ast.MutableModule(mod.doc.ydoc)
applyEdits(syncModule, props.modelValue)
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)
}
},
)
watch(
() => props.modelValue,
(modelValue) => applyEdits(syncModule, modelValue),
)
},
)
function applyEdits(syncModule: Ast.MutableModule, newText: string) {
if (newText !== syncedCode) {
syncModule.transact(() => {
syncModule.syncRoot(Ast.parseBlock(newText, syncModule))
})
}
}
</script>
<template>
<slot></slot>
</template>