Skip to content

Instantly share code, notes, and snippets.

@paulmr
Last active August 30, 2017 10:29
Show Gist options
  • Save paulmr/969f5b6669d887940ccacbab88c1e119 to your computer and use it in GitHub Desktop.
Save paulmr/969f5b6669d887940ccacbab88c1e119 to your computer and use it in GitHub Desktop.
Testing prosemirror position mapping across steps of a transform
/* testing the prose mirror bits */
import jsdom from 'node-jsdom'
import { Document } from 'xmldom'
import { Node, DOMSerializer, DOMParser } from 'prosemirror-model'
import { EditorState } from 'prosemirror-state'
import * as basic from 'prosemirror-schema-basic'
const parser = DOMParser.fromSchema(basic.schema)
function createDom(str) {
let doc = jsdom.jsdom(str)
return doc.body.firstChild
}
const ops = [
(tr) => {
let paraDom = createDom('<p>hello world</p>')
let para = parser.parse(paraDom)
tr.insert(0, para.content)
},
(tr) => {
tr.deleteRange(tr.mapping.map(0), tr.mapping.map(5))
}
]
function applyAllOneTr(ops, z) {
let tr = z.tr
ops.forEach((op) => op(tr))
return z.apply(tr)
}
function applyAllOneTrPerOp(ops, z) {
let res = z
ops.forEach((op) => {
let tr = res.tr
op(tr)
res = res.apply(tr)
})
return res
}
function printNode(n, indent) {
indent = indent || ""
let str = indent + (n.isText ? `text("${n.text}")` : n.type.name)
let nodeStrings = [ str ]
n.content.forEach((c) => nodeStrings.push(printNode(c, indent + " ")))
return nodeStrings.join("\n")
}
[ applyAllOneTr, applyAllOneTrPerOp ].forEach((applyAll) => {
jsdom.env('<p id="data">Document here</p>', (errors, window) => {
let s = `${applyAll.name}\n`
const document = window.document
const e = document.getElementById('data')
const doc = parser.parse(e)
const startState = EditorState.create({ doc: doc })
const endState = applyAll(ops, startState)
console.log(s + printNode(endState.doc, "> "))
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment