-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorate.ts
174 lines (162 loc) · 5.3 KB
/
decorate.ts
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
import { Decoration, DecorationSet } from 'prosemirror-view';
import { DOMSerializer, Fragment } from 'prosemirror-model';
import { getTypeFromRegistry } from './registry';
import {
DecoratorFn,
ProsemirrorNode,
ProsemirrorDomSerializer,
ProsemirrorSchema,
ProsemirrorDoc,
ResolveAdditions,
ResolveRemovals,
Registry,
} from './types';
const additionDecoration = (from: number, to: number, isBlock: boolean) => {
if (isBlock) {
return Decoration.node(from, to, {
class: 'prosemirror-diff-add',
});
} else {
return Decoration.inline(from, to, {
class: 'prosemirror-diff-add',
});
}
};
const removalDecoration = (
position: number,
element: string | ProsemirrorNode,
serializer: ProsemirrorDomSerializer,
schema: ProsemirrorSchema
) => {
let getDomNode =
typeof element === 'string'
? () => {
const span = document.createElement('span');
span.classList.add('prosemirror-diff-remove');
span.innerHTML = element;
return span;
}
: () => {
const node = schema.nodeFromJSON(element);
const wrapper = document.createElement(
node.isBlock ? 'div' : 'span'
);
wrapper.classList.add('prosemirror-diff-remove');
const domNode = serializer.serializeFragment(
Fragment.from(node)
);
wrapper.appendChild(domNode);
return wrapper;
};
return Decoration.widget(position, getDomNode, {
class: 'prosemirror-diff-remove',
removal: element,
});
};
export const decorateText: DecoratorFn = (textElement, context) => {
const { schema, serializer, offset, additions, removals } = context;
const removalsMap = removals && removals.text && removals.text.map;
const additionsMap = additions && additions.text.map;
const decorations = [];
if (removals) {
for (let [index, removalsHere] of removalsMap) {
removalsHere.forEach(removal => {
decorations.push(
removalDecoration(
-1 +
offset +
(index === 'end' ? textElement.text.length : index),
removal,
serializer,
schema
)
);
});
}
}
if (additions) {
for (let [index, additionHere] of additionsMap) {
decorations.push(
additionDecoration(
offset + index - 1,
offset + index + additionHere.length - 1,
false
)
);
}
}
return decorations;
};
export const decorateNodeWithContent: DecoratorFn = (node, context) => {
const {
schema,
serializer,
offset,
decorate,
additions,
removals,
} = context;
const removalsMap = removals && removals.content.map;
const childRemovalsMap = removals && removals.content.children;
const additionsMap = additions && additions.content.map;
const childAdditionsMap = additions && additions.content.children;
let decorations = [];
const addRemovalDecorations = (removedItems, pos) =>
removedItems.forEach(removal => {
decorations.push(
removalDecoration(pos, removal, serializer, schema)
);
});
node.forEach((child, pos, index) => {
const docPosition = offset + pos;
const removalsHere = removalsMap && removalsMap.get(index);
const additionHere = additionsMap && additionsMap.get(index);
if (removalsHere && removalsHere.length > 0) {
addRemovalDecorations(removalsHere, docPosition);
}
if (additionHere) {
decorations.push(
additionDecoration(
docPosition + 0,
docPosition + child.nodeSize + 0,
child.isBlock
)
);
}
decorations = [
...decorations,
...decorate(
child,
docPosition + 1,
childAdditionsMap && childAdditionsMap.get(index),
childRemovalsMap && childRemovalsMap.get(index)
),
];
});
const removedAtEnd = removalsMap && removalsMap.get('end');
if (removedAtEnd) {
addRemovalDecorations(removedAtEnd, offset + node.nodeSize - 2);
}
return decorations;
};
export const decorate = (
doc: ProsemirrorDoc,
schema: ProsemirrorSchema,
additions: ResolveAdditions,
removals: ResolveRemovals,
registry: Registry
) => {
const serializer = DOMSerializer.fromSchema(schema);
const decorate = (node, offset, additions, removals) => {
return getTypeFromRegistry(registry, node.type).render.decorate(node, {
schema,
serializer,
offset,
decorate,
additions,
removals,
});
};
const decorations = decorate(doc, 0, additions, removals);
return DecorationSet.create(doc, decorations);
};