Skip to content

Instantly share code, notes, and snippets.

@marijnh

marijnh/test.ts Secret

Created May 31, 2022 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marijnh/b0193255515e9e061209be93ef419458 to your computer and use it in GitHub Desktop.
Save marijnh/b0193255515e9e061209be93ef419458 to your computer and use it in GitHub Desktop.
import { Schema } from "prosemirror-model"
import {EditorState} from "prosemirror-state"
import {history, undo} from "prosemirror-history"
import {Step} from "prosemirror-transform"
const schema: Schema = new Schema({
nodes: {
doc: {
name: 'doc',
content: 'page',
toDOM: () => ['section', {class: 'document'}, 0],
parseDom: [{tag: 'section.document'}]
},
text: {
name: 'text',
group: 'inline'
},
paragraph: {
name: 'paragraph',
group: 'block',
content: 'inline*',
attrs: {id: {default: null}},
toDOM: () => ['p', {}, 0],
parseDOM: [{tag: 'p'}]
},
heading: {
name: 'heading',
content: 'inline*',
attrs: {id: {default: null}},
defining: true,
toDOM: () => [`h2`, {}, 0],
parseDOM: [{tag: 'h2'}]
},
page: {
name: 'page',
content: 'heading block*',
group: 'block',
attrs: {id: {default: null}},
defining: true,
toDOM: () => ['section', {class: 'page'}, 0],
parseDom: [{tag: 'section.page'}],
},
blockquote: {
name: 'blockquote',
content: 'block*',
group: 'block',
attrs: {id: {default: null}},
defining: true,
parseDOM: [{tag: 'blockquote'}],
toDOM: () => ['blockquote', 0]
}
}
})
let doc0 = schema.node("doc", null, [
schema.node("page", null, [
schema.node("heading", null, [schema.text("Hello ProseMirror")]),
schema.node("paragraph", null, [schema.text(">")])
])
])
let state = EditorState.create({
doc: doc0,
plugins: [history()]
})
let state1 = state.apply(state.tr.step(Step.fromJSON(schema, {
"stepType": "replace", "from": 21, "to": 22
})).step(Step.fromJSON(schema, {
"stepType": "replaceAround",
"from": 20, "to": 22, "gapFrom": 20, "gapTo": 22, "insert": 1,
"slice": { "content": [ { "type": "blockquote", "attrs": { "id": null } } ] },
"structure": true
})))
let steps = [
{
"stepType": "replaceAround",
"from": 1, "to": 20, "gapFrom": 2, "gapTo": 19,
"insert": 1,
"slice": { "content": [ { "type": "heading", "attrs": { "id": "slto" } } ] },
"structure": true
},
{
"stepType": "replaceAround",
"from": 21, "to": 23, "gapFrom": 22, "gapTo": 22, "insert": 1,
"slice": { "content": [ { "type": "paragraph", "attrs": { "id": "mmnbqpamb" } } ] },
"structure": true
},
{
"stepType": "replaceAround",
"from": 20, "to": 24, "gapFrom": 21, "gapTo": 23, "insert": 1,
"slice": { "content": [ { "type": "blockquote", "attrs": { "id": "fnrjfhzbg" } } ] },
"structure": true
},
{
"stepType": "replaceAround",
"from": 0, "to": 25, "gapFrom": 1, "gapTo": 24, "insert": 1,
"slice": { "content": [ { "type": "page", "attrs": { "id": "mrnkje" } } ] },
"structure": true
}
]
let tr = state1.tr.setMeta("addToHistory", false)
for (let step of steps) tr.step(Step.fromJSON(schema, step))
let state2 = state1.apply(tr)
console.log(state2.doc + "")
undo(state2, tr => {
console.log(state2.apply(tr).doc +"")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment