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
/
index.js
195 lines (172 loc) · 6.46 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const {Plugin} = require("prosemirror/src/edit")
const {Transform} = require("prosemirror/src/transform")
class TrackedChange {
constructor(from, to, deleted, author) {
this.from = from
this.to = to
this.deleted = deleted
this.author = author
}
map(mapping, inclusive) {
let from = mapping.map(this.from, inclusive ? -1 : 1)
let to = mapping.map(this.to, inclusive ? 1 : -1)
if (from > to || from == to && !this.deleted.size) return null
return new TrackedChange(from, to, this.deleted, this.author)
}
deletedText() {
return this.deleted.content.textBetween(0, this.deleted.content.size, " ")
}
}
exports.TrackedChange = TrackedChange
function applyAndSlice(doc, changes, from, to) {
let tr = new Transform(doc)
for (let i = changes.length - 1; i >= 0; i--) {
let change = changes[i]
tr.replace(change.from, change.to, change.deleted)
}
return tr.doc.slice(from, tr.map(to))
}
function minimizeChange(change, doc, side) {
let tr = new Transform(doc).replace(change.from, change.to, change.deleted)
let changedDoc = tr.doc
let $from = doc.resolve(change.from), sameDepth = $from.depth
let $changedFrom = changedDoc.resolve(change.from), changeEnd = change.from + change.deleted.size
while (change.to > $from.end(sameDepth) || changeEnd > $changedFrom.end(sameDepth)) --sameDepth
let node = $from.node(sameDepth), changedNode = $changedFrom.node(sameDepth)
if (side == -1) {
let diffStart = node.content.findDiffStart(changedNode.content, $from.start(sameDepth))
if (!diffStart) return null
if (diffStart == change.from || diffStart >= change.to) return change
return new TrackedChange(diffStart, change.to, changedDoc.slice(diffStart, tr.map(change.to)), change.author)
} else {
let diffEnd = node.content.findDiffEnd(changedNode.content, $from.end(sameDepth), $changedFrom.end(sameDepth))
if (!diffEnd) return null
if (diffEnd.a == change.to || diffEnd.a <= change.from || diffEnd.b <= change.from) return change
return new TrackedChange(change.from, diffEnd.a, changedDoc.slice(change.from, diffEnd.b), change.author)
}
}
function mapChanges(changes, map, author, updated, docAfter) {
let result = []
for (let i = 0; i < changes.length; i++) {
let change = changes[i], mapped = change.map(map, author == change.author), idx
if (mapped) {
if (updated && (idx = updated.indexOf(change)) > -1)
mapped = minimizeChange(mapped, docAfter, updated[idx + 1])
if (mapped) result.push(mapped)
}
}
return result
}
exports.changeTracking = new Plugin(class ChangeTracking {
constructor(pm, options) {
this.pm = pm
this.changes = options.changes.slice()
this.annotations = []
this.author = options.author
pm.on.transform.add(this.onTransform = this.onTransform.bind(this))
}
detach() {
this.pm.on.transform.remove(this.onTransform)
}
onTransform(transform, _, options) {
if (!this.author || options.reverting) // FIXME split changes when typing inside them?
this.changes = mapChanges(this.changes, transform)
else
this.record(transform, this.author)
this.updateAnnotations()
}
record(transform, author) {
let updated = []
for (let i = 0; i < transform.steps.length; i++) {
let map = transform.maps[i]
for (let r = 0; r < map.ranges.length; r += 3)
this.recordRange(transform.docs[i], map.ranges[r], map.ranges[r] + map.ranges[r + 1], author, updated)
this.changes = mapChanges(this.changes, map, author, updated, transform.docs[i + 1] || transform.doc)
updated.length = 0
}
}
recordRange(doc, from, to, author, updatedChanges) {
let i = 0
for (; i < this.changes.length; i++) {
let change = this.changes[i]
if (change.author != author || change.to < from) continue
if (change.from > to) break
let changes = [change], newContent = from < change.from || to > change.to
for (let j = i + 1; j < this.changes.length; j++) {
let next = this.changes[j]
if (next.author != author) continue
if (next.from > to) break
changes.push(next)
newContent = true
this.changes.splice(j--, 1)
}
let newFrom = Math.min(change.from, from), newTo = Math.max(changes[changes.length - 1].to, to)
let slice = newContent ? applyAndSlice(doc, changes, newFrom, newTo) : change.deleted
updatedChanges.push(this.changes[i] = new TrackedChange(newFrom, newTo, slice, change.author),
from <= changes[0].from ? -1 : 1)
return
}
this.changes.splice(i, 0, new TrackedChange(from, to, doc.slice(from, to), author))
}
updateAnnotations() {
// See if our document annotations still match the set of changes,
// and update them if they don't.
let iA = 0
for (let iC = 0; iC < this.changes.length; iC++) {
let change = this.changes[iC], matched = false
let deletedText = change.deletedText()
while (iA < this.annotations.length) {
let ann = this.annotations[iA]
if (ann.from > change.to) break
if (ann.from == change.from && ann.to == change.to && ann.options.deletedText == deletedText) {
iA++
matched = true
} else {
this.pm.removeRange(ann)
this.annotations.splice(iA, 1)
}
}
if (!matched) {
let ann = this.pm.markRange(change.from, change.to, rangeOptionsFor(change, deletedText))
this.annotations.splice(iA++, 0, ann)
}
}
for (let i = iA; i < this.annotations.length; i++) {
this.pm.removeRange(this.annotations[iA])
}
this.annotations.length = iA
}
forgetChange(change) {
let found = this.changes.indexOf(change)
if (found == -1) return false
this.changes.splice(found, 1)
return true
}
acceptChange(change) {
if (this.forgetChange(change))
this.updateAnnotations()
}
revertChange(change) {
if (this.forgetChange(change))
this.pm.tr.replace(change.from, change.to, change.deleted).apply({
scrollIntoView: true,
reverting: true,
addToHistory: false
})
}
}, {
changes: [],
author: null
})
function rangeOptionsFor(change, deletedText) {
let options = {}
if (change.from == change.to) options.removeWhenEmpty = false
else options.className = "inserted"
if (deletedText) {
options.deletedText = deletedText
let elt = options.elementBefore = document.createElement("span")
elt.textContent = deletedText
elt.className = "deleted"
}
return options
}