Skip to content

Commit

Permalink
fix(Editor): changing bold/italic didn't generate an update
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jan 25, 2024
1 parent cb2d5df commit 17b0fdb
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<boolean>((memo, tr) => memo || tr.docChanged, false)
Expand All @@ -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
Expand Down Expand Up @@ -42,5 +49,3 @@ export function updateModel(onChange?: (lineId: UILineId, contents: SomeContents
},
})
}

type SomeContents = unknown

0 comments on commit 17b0fdb

Please sign in to comment.