-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcldiffutils.js
488 lines (459 loc) · 15.5 KB
/
cldiffutils.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
;(function (root, factory) {
if (typeof exports === 'object') {
require('clunderscore')
module.exports = factory(require('googlediff'))
} else {
root.clDiffUtils = factory(root.diff_match_patch)
}
})(this, function (DiffMatchPatch) {
var clDiffUtils = {
cloneObject: cloneObject,
offsetToPatch: offsetToPatch,
patchToOffset: patchToOffset,
serializeObject: serializeObject,
flattenContent: flattenContent,
makePatchableText: makePatchableText,
restoreDiscussionOffsets: restoreDiscussionOffsets,
makeContentChange: makeContentChange,
applyContentChanges: applyContentChanges,
getTextPatches: getTextPatches,
getObjectPatches: getObjectPatches,
quickPatch: quickPatch,
mergeObjects: mergeObjects,
mergeFlattenContent: mergeFlattenContent
}
var marker = '\uF111\uF222\uF333\uF444'
var DIFF_DELETE = -1
var DIFF_INSERT = 1
var DIFF_EQUAL = 0
var diffMatchPatch = new DiffMatchPatch() // eslint-disable-line new-cap
var diffMatchPatchStrict = new DiffMatchPatch() // eslint-disable-line new-cap
diffMatchPatchStrict.Match_Threshold = 0
diffMatchPatchStrict.Patch_DeleteThreshold = 0
var diffMatchPatchPermissive = new DiffMatchPatch() // eslint-disable-line new-cap
diffMatchPatchPermissive.Match_Distance = 999999999
function cloneObject (obj) {
return JSON.parse(JSON.stringify(obj))
}
function offsetToPatch (text, offset) {
var patch = diffMatchPatchPermissive.patch_make(text, [
[0, text.slice(0, offset)],
[1, marker],
[0, text.slice(offset)]
])[0]
var diffs = patch.diffs.cl_map(function (diff) {
if (!diff[0]) {
return diff[1]
} else if (diff[1] === marker) {
return ''
}
})
return {
diffs: diffs,
length: patch.length1,
start: patch.start1
}
}
function patchToOffset (text, patch) {
var markersLength = 0
var diffs = patch.diffs.cl_map(function (diff) {
if (!diff) {
markersLength += marker.length
return [1, marker]
} else {
return [0, diff]
}
})
return diffMatchPatchPermissive.patch_apply([{
diffs: diffs,
length1: patch.length,
length2: patch.length + markersLength,
start1: patch.start,
start2: patch.start
}], text)[0].indexOf(marker)
}
function flattenObject (obj) {
return obj.cl_reduce(function (result, value, key) {
result[key] = value[1]
return result
}, {})
}
function flattenContent (content) {
var result = ({}).cl_extend(content)
result.properties = flattenObject(content.properties)
result.discussions = flattenObject(content.discussions)
result.comments = flattenObject(content.comments)
result.text = content.text.cl_reduce(function (text, item) {
switch (item.type) {
case 'discussion':
if (result.discussions[item.id]) {
result.discussions[item.id][item.name] = text.length
}
return text
default:
return text + item[1]
}
}, '')
return result
}
function getTextPatches (oldText, newText) {
var diffs = diffMatchPatch.diff_main(oldText, newText)
diffMatchPatch.diff_cleanupEfficiency(diffs)
var patches = []
var startOffset = 0
diffs.cl_each(function (change) {
var changeType = change[0]
var changeText = change[1]
switch (changeType) {
case DIFF_EQUAL:
startOffset += changeText.length
break
case DIFF_DELETE:
changeText && patches.push({
o: startOffset,
d: changeText
})
break
case DIFF_INSERT:
changeText && patches.push({
o: startOffset,
a: changeText
})
startOffset += changeText.length
break
}
})
return patches.length ? patches : undefined
}
function getObjectPatches (oldObject, newObjects) {
var valueHash = Object.create(null)
var valueArray = []
oldObject = hashObject(oldObject, valueHash, valueArray)
newObjects = hashObject(newObjects, valueHash, valueArray)
var diffs = diffMatchPatch.diff_main(oldObject, newObjects)
var patches = []
diffs.cl_each(function (change) {
var changeType = change[0]
var changeHash = change[1]
if (changeType === DIFF_EQUAL) {
return
}
changeHash.split('').cl_each(function (objHash) {
var obj = valueArray[objHash.charCodeAt(0)]
var patch = {
k: obj[0]
}
patch[changeType === DIFF_DELETE ? 'd' : 'a'] = obj[1]
patches.push(patch)
})
})
return patches.length ? patches : undefined
}
function makePatchableText (content, markerKeys, markerIdxMap) {
var markers = []
// Sort keys to have predictable marker positions, in case of same offset
var discussionKeys = Object.keys(content.discussions).sort()
discussionKeys.cl_each(function (discussionId) {
function addMarker (offsetName) {
var markerKey = discussionId + offsetName
if (discussion[offsetName] !== undefined) {
var idx = markerIdxMap[markerKey]
if (idx === undefined) {
idx = markerKeys.length
markerIdxMap[markerKey] = idx
markerKeys.push({
id: discussionId,
offsetName: offsetName
})
}
markers.push({
idx: idx,
offset: discussion[offsetName]
})
}
}
var discussion = content.discussions[discussionId]
if (discussion.offset0 === discussion.offset1) {
// Remove discussion offsets if markers are at the same position
discussion.offset0 = discussion.offset1 = undefined
} else {
addMarker('offset0')
addMarker('offset1')
}
})
var lastOffset = 0
var result = ''
markers
.sort(function (marker1, marker2) {
return marker1.offset - marker2.offset
})
.cl_each(function (marker) {
result +=
content.text.slice(lastOffset, marker.offset) +
String.fromCharCode(0xe000 + marker.idx) // Use a character from the private use area
lastOffset = marker.offset
})
return result + content.text.slice(lastOffset)
}
function stripDiscussionOffsets (objectMap) {
return objectMap.cl_reduce(function (result, object, id) {
result[id] = {
text: object.text
}
return result
}, {})
}
function restoreDiscussionOffsets (content, markerKeys) {
var len = content.text.length
var maxIdx = markerKeys.length
for (var i = 0; i < len; i++) {
var idx = content.text.charCodeAt(i) - 0xe000
if (idx >= 0 && idx < maxIdx) {
var markerKey = markerKeys[idx]
content.text = content.text.slice(0, i) + content.text.slice(i + 1)
var discussion = content.discussions[markerKey.id]
if (discussion) {
discussion[markerKey.offsetName] = i
}
i-- // We just removed the current character, we may have multiple markers with same offset
}
}
}
function makeContentChange (oldContent, newContent) {
var markerKeys = []
var markerIdxMap = Object.create(null)
var oldText = makePatchableText(oldContent, markerKeys, markerIdxMap)
var newText = makePatchableText(newContent, markerKeys, markerIdxMap)
var textPatches = getTextPatches(oldText, newText)
textPatches && textPatches.cl_each(function (patch) {
// If markers are present, replace changeText with an array of text and markers
var changeText = patch.a || patch.d
var textItems = []
var lastItem = ''
var len = changeText.length
var maxIdx = markerKeys.length
for (var i = 0; i < len; i++) {
var idx = changeText.charCodeAt(i) - 0xe000
if (idx >= 0 && idx < maxIdx) {
var markerKey = markerKeys[idx]
lastItem.length && textItems.push(lastItem)
textItems.push({
type: 'discussion',
name: markerKey.offsetName,
id: markerKey.id
})
lastItem = ''
} else {
lastItem += changeText[i]
}
}
if (textItems.length) {
lastItem.length && textItems.push(lastItem)
if (patch.a) {
patch.a = textItems
} else {
patch.d = textItems
}
}
})
var propertiesPatches = getObjectPatches(oldContent.properties, newContent.properties)
var discussionsPatches = getObjectPatches(
stripDiscussionOffsets(oldContent.discussions),
stripDiscussionOffsets(newContent.discussions)
)
var commentsPatches = getObjectPatches(oldContent.comments, newContent.comments)
if (textPatches || propertiesPatches || discussionsPatches || commentsPatches) {
return {
text: textPatches,
properties: propertiesPatches,
discussions: discussionsPatches,
comments: commentsPatches
}
}
}
function applyContentChanges (content, contentChanges, isBackward) {
function applyObjectPatches (obj, patches) {
if (patches) {
patches.cl_each(function (patch) {
if (!patch.a ^ !isBackward) {
obj[patch.k] = patch.a || patch.d
} else {
delete obj[patch.k]
}
})
}
}
var markerKeys = []
var markerIdxMap = Object.create(null)
var result = {
text: makePatchableText(content, markerKeys, markerIdxMap),
properties: cloneObject(content.properties),
discussions: stripDiscussionOffsets(content.discussions),
comments: cloneObject(content.comments)
}
contentChanges.cl_each(function (contentChange) {
var textPatches = contentChange.text || []
if (isBackward) {
textPatches = textPatches.slice().reverse()
}
result.text = textPatches.cl_reduce(function (text, patch) {
var isAdd = !patch.a ^ !isBackward
var textChanges = patch.a || patch.d || ''
// When no marker is present, textChanges is a string
if (typeof textChanges === 'string') {
textChanges = [textChanges]
}
var textChange = textChanges.cl_map(function (textChange) {
if (!textChange.type) {
// textChange is a string
return textChange
}
// textChange is a marker
var markerKey = textChange.id + textChange.name
var idx = markerIdxMap[markerKey]
if (idx === undefined) {
idx = markerKeys.length
markerIdxMap[markerKey] = idx
markerKeys.push({
id: textChange.id,
offsetName: textChange.name
})
}
return String.fromCharCode(0xe000 + idx)
}).join('')
if (!textChange) {
return text
} else if (isAdd) {
return text.slice(0, patch.o).concat(textChange).concat(text.slice(patch.o))
} else {
return text.slice(0, patch.o).concat(text.slice(patch.o + textChange.length))
}
}, result.text)
applyObjectPatches(result.properties, contentChange.properties)
applyObjectPatches(result.discussions, contentChange.discussions)
applyObjectPatches(result.comments, contentChange.comments)
})
restoreDiscussionOffsets(result, markerKeys)
return result
}
function serializeObject (obj) {
return JSON.stringify(obj, function (key, value) {
return Object.prototype.toString.call(value) === '[object Object]'
? Object.keys(value).sort().cl_reduce(function (sorted, key) {
sorted[key] = value[key]
return sorted
}, {})
: value
})
}
function hashArray (arr, valueHash, valueArray) {
var hash = []
arr.cl_each(function (obj) {
var serializedObj = serializeObject(obj)
var objHash = valueHash[serializedObj]
if (objHash === undefined) {
objHash = valueArray.length
valueArray.push(obj)
valueHash[serializedObj] = objHash
}
hash.push(objHash)
})
return String.fromCharCode.apply(null, hash)
}
function hashObject (obj, valueHash, valueArray) {
return hashArray(Object.keys(obj || {}).sort().cl_map(function (key) {
return [key, obj[key]]
}), valueHash, valueArray)
}
function mergeText (oldText, newText, serverText) {
var diffs = diffMatchPatch.diff_main(oldText, newText)
diffMatchPatch.diff_cleanupSemantic(diffs)
var patches = diffMatchPatch.patch_make(oldText, diffs)
var patchResult = diffMatchPatch.patch_apply(patches, serverText)
if (!patchResult[1]
.cl_some(function (changeApplied) {
return !changeApplied
})) {
return patchResult[0]
}
diffs = diffMatchPatchStrict.diff_main(patchResult[0], newText)
diffMatchPatch.diff_cleanupSemantic(diffs)
return diffs.cl_map(function (diff) {
return diff[1]
}).join('')
}
function quickPatch (oldStr, newStr, destStr, strict) {
var dmp = strict ? diffMatchPatchStrict : diffMatchPatch
var diffs = dmp.diff_main(oldStr, newStr)
var patches = dmp.patch_make(oldStr, diffs)
var patchResult = dmp.patch_apply(patches, destStr)
return patchResult[0]
}
function mergeObjects (oldObject, newObject, serverObject) {
var mergedObject = ({}).cl_extend(newObject).cl_extend(serverObject)
mergedObject.cl_each(function (value, key) {
if (!oldObject[key]) {
return // There might be conflict, keep the server value
}
var newValue = newObject[key] && serializeObject(newObject[key])
var serverValue = serverObject[key] && serializeObject(serverObject[key])
if (newValue === serverValue) {
return // no conflict
}
var oldValue = serializeObject(oldObject[key])
if (oldValue !== newValue && !serverValue) {
return // Removed on server but changed on client
}
if (oldValue !== serverValue && !newValue) {
return // Removed on client but changed on server
}
if (oldValue !== newValue && oldValue === serverValue) {
// Take the client value
if (!newValue) {
delete mergedObject[key]
} else {
mergedObject[key] = newObject[key]
}
} else if (oldValue !== serverValue && oldValue === newValue) {
// Take the server value
if (!serverValue) {
delete mergedObject[key]
}
}
// Take the server value otherwise
})
return cloneObject(mergedObject)
}
function mergeFlattenContent (oldContent, newContent, serverContent) {
var markerKeys = []
var markerIdxMap = Object.create(null)
var oldText = makePatchableText(oldContent, markerKeys, markerIdxMap)
var serverText = makePatchableText(serverContent, markerKeys, markerIdxMap)
var localText = makePatchableText(newContent, markerKeys, markerIdxMap)
var isServerTextChanges = oldText !== serverText
var isTextSynchronized = serverText === localText
var result = {
text: isTextSynchronized || !isServerTextChanges
? localText
: mergeText(oldText, serverText, localText),
properties: mergeObjects(
oldContent.properties,
newContent.properties,
serverContent.properties
),
discussions: mergeObjects(
stripDiscussionOffsets(oldContent.discussions),
stripDiscussionOffsets(newContent.discussions),
stripDiscussionOffsets(serverContent.discussions)
),
comments: mergeObjects(
oldContent.comments,
newContent.comments,
serverContent.comments
)
}
restoreDiscussionOffsets(result, markerKeys)
return result
}
return clDiffUtils
})