-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.ts
40 lines (35 loc) · 1.24 KB
/
editor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { derived, get, writable } from 'svelte/store'
import { getActiveMarks } from './getActiveMarks'
import type { Editor, EditorProps, ExtObject } from '@my-org/core'
import type { EditorState } from 'prosemirror-state'
const STATE_STORAGE_KEY = 'editor-state'
export let editor: Editor | undefined
export const props = writable<EditorProps>()
export const state = writable<EditorState | undefined>()
export const extObj = writable<ExtObject>({} as ExtObject)
export const activeMarks = derived(state, s => (s ? getActiveMarks(s) : []))
export const editorActions = {
setEditor(instance: Editor) {
editor = instance
try {
const existing = localStorage.getItem(STATE_STORAGE_KEY)
const yjsExt = instance.getExtension('yjs')
if (existing && !yjsExt) {
instance.setState(JSON.parse(existing))
}
} catch (err) {}
extObj.set(instance.data.extObj)
props.set(instance.data.props)
state.set(instance.data.state)
instance.on('update', (k, v) => {
if (k === 'extObj') {
extObj.set(v)
} else if (k === 'props') {
props.set(v)
} else if (k === 'state') {
state.set(v)
localStorage.setItem(STATE_STORAGE_KEY, JSON.stringify(v.toJSON()))
}
})
}
}