This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
forked from nytimes/prosemirror-change-tracking-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
44 lines (39 loc) · 1.65 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const {ProseMirror} = require("prosemirror/src/edit")
const {schema} = require("prosemirror/src/schema-basic")
const {changeTracking} = require("./index")
let pm = window.pm = new ProseMirror({
place: document.body,
doc: schema.parseDOM(document.querySelector("#content")),
plugins: [changeTracking.config({author: null})]
})
let tracking = window.tracking = changeTracking.get(window.pm)
const controls = document.body.appendChild(document.createElement("div"))
function updateControls() {
controls.textContent = ""
tracking.changes.forEach(change => {
let div = controls.appendChild(document.createElement("div"))
div.className = "change"
div.appendChild(document.createElement("strong")).appendChild(document.createTextNode(change.author))
let deleted = change.deletedText(), added = pm.doc.textBetween(change.from, change.to, " ")
let desc = deleted ? " deleted " + JSON.stringify(deleted) : ""
if (added) desc += (desc ? " and" : "") + " added " + JSON.stringify(added)
div.appendChild(document.createTextNode(desc))
div.appendChild(document.createElement("br"))
let commit = div.appendChild(document.createElement("button"))
commit.textContent = "Accept"
commit.addEventListener("click", e => {
e.preventDefault()
tracking.acceptChange(change)
updateControls()
})
div.appendChild(document.createTextNode(" "))
let revert = div.appendChild(document.createElement("button"))
revert.textContent = "Revert"
revert.addEventListener("click", e => {
e.preventDefault()
tracking.revertChange(change)
})
})
}
updateControls()
pm.on.change.add(() => setTimeout(updateControls, 50))