diff --git a/packages/apps/client/src/components/ScriptEditor/plugins/updateModel.ts b/packages/apps/client/src/components/ScriptEditor/plugins/updateModel.ts index 2681c89..ebe828f 100644 --- a/packages/apps/client/src/components/ScriptEditor/plugins/updateModel.ts +++ b/packages/apps/client/src/components/ScriptEditor/plugins/updateModel.ts @@ -1,11 +1,12 @@ import { Plugin } from 'prosemirror-state' import { UILineId } from '../../model/UILine' import { Node } from 'prosemirror-model' +import { AddMarkStep, RemoveMarkStep, StepMap } from 'prosemirror-transform' import { schema } from '../scriptSchema' export const EXTERNAL_STATE_CHANGE = 'externalStateChange' -export function updateModel(onChange?: (lineId: UILineId, contents: SomeContents) => void) { +export function updateModel(onChange?: (lineId: UILineId, lineNodes: Node[]) => void) { return new Plugin({ appendTransaction: (trs, oldState, newState) => { const anyChanges = trs.reduce((memo, tr) => memo || tr.docChanged, false) @@ -14,7 +15,13 @@ export function updateModel(onChange?: (lineId: UILineId, contents: SomeContents for (const tr of trs) { if (tr.getMeta(EXTERNAL_STATE_CHANGE)) return for (const step of tr.steps) { - step.getMap().forEach((oldStart, oldEnd, newStart, newEnd) => { + let stepMap = step.getMap() + // For some reason AddMarkStep/RemoveMarkStep don't provide a map. Generate it manually + if (step instanceof AddMarkStep || step instanceof RemoveMarkStep) { + stepMap = new StepMap([step.from, step.to - step.from, step.to - step.from]) + } + + stepMap.forEach((oldStart, oldEnd, newStart, newEnd) => { oldState.doc.nodesBetween(oldStart, oldEnd, (_node, _pos, parent) => { if (!parent) return if (parent.type.name !== 'line') return @@ -42,5 +49,3 @@ export function updateModel(onChange?: (lineId: UILineId, contents: SomeContents }, }) } - -type SomeContents = unknown