Download this file
| 1 | // adapter.mjs — the saver's view of the editor (saver.mjs): the text |
| 2 | // now, the text to send (the structure check, §6.4 step 5), and a body |
| 3 | // applied from the server. What is applied is its own undo step, so |
| 4 | // undo after a merge brings back the person's own text (§6.5), and is |
| 5 | // marked remote, so it is not taken for typing. |
| 6 | |
| 7 | import { closeHistory } from 'prosemirror-history' |
| 8 | import { serialise, applyBody, prepareSave } from './fidelity.mjs' |
| 9 | |
| 10 | export function editorAdapter (holder, fidelity) { |
| 11 | const editor = { remote: false } |
| 12 | const view = () => holder.view |
| 13 | const remote = run => { |
| 14 | const was = editor.remote |
| 15 | editor.remote = true |
| 16 | try { return run() } finally { editor.remote = was } |
| 17 | } |
| 18 | editor.current = () => serialise(view().state.doc, fidelity) |
| 19 | editor.prepare = () => remote(() => prepareSave(view(), fidelity).body) |
| 20 | editor.apply = body => remote(() => { |
| 21 | view().dispatch(closeHistory(view().state.tr)) |
| 22 | const result = applyBody(view(), fidelity, body) |
| 23 | view().dispatch(closeHistory(view().state.tr)) |
| 24 | return result |
| 25 | }) |
| 26 | return editor |
| 27 | } |
| 28 | |