-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace-highlighted-text.js
More file actions
136 lines (116 loc) · 4.4 KB
/
replace-highlighted-text.js
File metadata and controls
136 lines (116 loc) · 4.4 KB
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
function replaceRange(range, html, selectInserted = false) {
range.deleteContents();
// Create a DocumentFragment to insert and populate it with HTML
// Need to test for the existence of range.createContextualFragment
// because it's non-standard and IE 9 does not support it
let fragment;
if (range.createContextualFragment) {
fragment = range.createContextualFragment(html);
} else {
// In IE 9 we need to use innerHTML of a temporary element
var div = document.createElement("div"), child;
div.innerHTML = html;
fragment = document.createDocumentFragment();
while ( (child = div.firstChild) ) {
fragment.appendChild(child);
}
}
var firstInsertedNode = fragment.firstChild;
var lastInsertedNode = fragment.lastChild;
range.insertNode(fragment);
if (selectInserted) {
if (firstInsertedNode) {
range.setStartBefore(firstInsertedNode);
range.setEndAfter(lastInsertedNode);
}
sel.removeAllRanges();
sel.addRange(range);
}
}
function wrapDeleted(fragment){
const wrapper = document.createElement('span');
wrapper.appendChild(fragment);
wrapper.classList.add('text-deleted');
return wrapper;
}
function wrapAdded(fragment){
const wrapper = document.createElement('span');
wrapper.appendChild(fragment);
wrapper.classList.add('text-added');
return wrapper;
}
function revertButtonClick(button, e) {
const diffContainer = button.parentNode.parentNode;
const textDeleted = diffContainer.getElementsByClassName('text-deleted')[0].innerHTML;
diffContainer.outerHTML = textDeleted;
}
export function diffRange(range, html) {
const deletedFragment = range.extractContents();
// Create a DocumentFragment to insert and populate it with HTML
// Need to test for the existence of range.createContextualFragment
// because it's non-standard and IE 9 does not support it
let newFragment;
if (range.createContextualFragment) {
newFragment = range.createContextualFragment(html);
} else {
// In IE 9 we need to use innerHTML of a temporary element
var div = document.createElement("div"), child;
div.innerHTML = html;
newFragment = document.createDocumentFragment();
while ( (child = div.firstChild) ) {
fragment.appendChild(child);
}
}
Array.prototype.forEach.call(deletedFragment.children[0].children, elem => {
if(elem.classList.contains('text-added')) {
elem.outerHTML = '';
} else {
elem.outerHTML = elem.innerHTML;
}
});
const fragment = document.createDocumentFragment();
const container = document.createElement('span');
container.classList.add('diff-container');
const revertButton = document.createElement('button');
revertButton.classList.add('revert-btn');
revertButton.innerHTML = 'x';
revertButton.addEventListener('click', revertButtonClick.bind(null, revertButton));
const revertButtonContainer = document.createElement('span');
revertButtonContainer.classList.add('revert-btn-container');
revertButtonContainer.appendChild(revertButton);
container.appendChild(revertButtonContainer);
container.appendChild(wrapDeleted(deletedFragment));
container.appendChild(wrapAdded(newFragment));
fragment.appendChild(container);
range.insertNode(fragment);
}
export function wrapRange(range, wrapperElementType) {
const clone = range.cloneContents();
const elem = document.createElement(wrapperElementType);
elem.classList.add("text-highlighted");
range.surroundContents(elem);
return clone;
}
export function styleRange(range) {
const deletedFragment = range.extractContents();
// Create a DocumentFragment to insert and populate it with HTML
// Need to test for the existence of range.createContextualFragment
// because it's non-standard and IE 9 does not support it
let newFragment;
if (range.createContextualFragment) {
newFragment = range.createContextualFragment(html);
} else {
// In IE 9 we need to use innerHTML of a temporary element
var div = document.createElement("div"), child;
div.innerHTML = html;
newFragment = document.createDocumentFragment();
while ( (child = div.firstChild) ) {
fragment.appendChild(child);
}
}
const fragment = document.createDocumentFragment();
fragment.appendChild(wrapDeleted(deletedFragment));
fragment.appendChild(wrapAdded(newFragment));
range.insertNode(fragment);
}
export default replaceRange;