Last active
August 30, 2017 10:29
-
-
Save paulmr/969f5b6669d887940ccacbab88c1e119 to your computer and use it in GitHub Desktop.
Testing prosemirror position mapping across steps of a transform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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