enso/app/gui2/stories/MockProjectStoreWrapper.vue
somebody1234 9b7e3d0f16
E2E tests (#8239)
- Closes #8179

# Important Notes
- ⚠️ These tests are currently *not run* on any CI workflow.
- There is some unused code for mocking the PM. This has been intentionally kept, as this may be useful in the future.
Note that this may be useful for testing the dashboard, however the dashboard is currently only tested in cloud mode
- that is, without the backend switcher, and with only the remote backend available. As such, currently it uses HTTP API mocks, and no PM mock.
2023-11-27 15:48:37 +00:00

58 lines
1.4 KiB
Vue

<script setup lang="ts">
import { useProjectStore } from '@/stores/project'
import { useObserveYjs } from '@/util/crdt'
import diff from 'fast-diff'
import { computed, watchEffect } from 'vue'
const props = defineProps<{ modelValue: string }>()
const emit = defineEmits<{ 'update:modelValue': [modelValue: string] }>()
const projectStore = useProjectStore()
const mod = projectStore.projectModel.createNewModule('Main.enso')
mod.doc.ydoc.emit('load', [])
function applyEdits(module: NonNullable<typeof projectStore.module>, newText: string) {
const contents = projectStore.module?.doc.contents
if (!contents) return
const edits = diff(contents.toString(), newText)
if (edits.length === 0) return
module.transact(() => {
let i = 0
for (const [type, string] of edits) {
switch (type) {
case -1: {
contents.delete(i, string.length)
break
}
case 0: {
i += string.length
break
}
case 1: {
contents.insert(i, string)
break
}
}
}
})
}
watchEffect(() => projectStore.module && applyEdits(projectStore.module, props.modelValue))
const text = computed(() => projectStore.module?.doc.contents)
useObserveYjs(text, () => {
if (text.value) {
const newValue = text.value?.toString()
if (newValue !== props.modelValue) {
emit('update:modelValue', newValue)
}
}
})
</script>
<template>
<slot></slot>
</template>