Skip to content

Commit

Permalink
fix: ignore external state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl committed Dec 12, 2023
1 parent 5bc5c1b commit 9b80b01
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
9 changes: 5 additions & 4 deletions packages/apps/client/src/ScriptEditor/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { useEffect, useRef } from 'react'
import { undo, redo, history } from 'prosemirror-history'
import { keymap } from 'prosemirror-keymap'
import { EditorState, SelectionBookmark, TextSelection } from 'prosemirror-state'
import { EditorState, SelectionBookmark } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view'
import { Fragment, Node, Slice } from 'prosemirror-model'
import { baseKeymap } from 'prosemirror-commands'
import { ReplaceStep, replaceStep } from 'prosemirror-transform'
import { replaceStep } from 'prosemirror-transform'
import { schema } from './scriptSchema'
import 'prosemirror-view/style/prosemirror.css'
import { updateModel } from './plugins/updateModel'
import { EXTERNAL_STATE_CHANGE, updateModel } from './plugins/updateModel'
import { readOnlyNodeFilter } from './plugins/readOnlyNodeFilter'
import { randomId } from '../lib/lib'
import { formatingKeymap } from './keymaps'
Expand Down Expand Up @@ -101,7 +101,7 @@ export function Editor({
keymap(formatingKeymap),
keymap(baseKeymap),
readOnlyNodeFilter(),
updateModel(() => {}),
updateModel((lineId, change) => console.log(lineId, change)),
],
doc,
})
Expand Down Expand Up @@ -169,6 +169,7 @@ export function Editor({
if (!step) return

const tr = editorState.tr.step(step)
tr.setMeta(EXTERNAL_STATE_CHANGE, true)
let newState = editorState.apply(tr)

if (selectionBookmark) newState = restoreSelection(newState, selectionBookmark)
Expand Down
3 changes: 3 additions & 0 deletions packages/apps/client/src/ScriptEditor/plugins/updateModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { UILineId } from '../../model/UILine'
import { Node } from 'prosemirror-model'
import { schema } from '../scriptSchema'

export const EXTERNAL_STATE_CHANGE = 'externalStateChange'

export function updateModel(onChange?: (lineId: UILineId, contents: SomeContents) => void) {
return new Plugin({
appendTransaction: (trs, oldState, newState) => {
const anyChanges = trs.reduce<boolean>((memo, tr) => memo || tr.docChanged, false)
if (!anyChanges) return null

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) => {
oldState.doc.nodesBetween(oldStart, oldEnd, (_node, _pos, parent) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/apps/client/src/lib/mdParser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export default function createParser(): Parser {

performance.mark('astFromMarkdownishEnd')

console.log(performance.measure('astFromMarkdownish', 'astFromMarkdownishBegin', 'astFromMarkdownishEnd'))
// console.log(performance.measure('astFromMarkdownish', 'astFromMarkdownishBegin', 'astFromMarkdownishEnd'))

console.log(document)
// console.log(document)

return document
}
Expand Down

0 comments on commit 9b80b01

Please sign in to comment.