-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.js
338 lines (284 loc) · 10.3 KB
/
utils.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
const uuidRegex = /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/;
// regexes that matches the mention placeholder
// this one matches the whole placeholder
const mentionRegexMatchWhole = /(@{.+?_(?:groupMention|person)_(?:all|moderators|here|[\w-]{36})})/;
// this one matches the individual values
const mentionRegexMatchValues = /@{(.+?)_(groupMention|person)_(all|moderators|here|[\w-]{36})}/g;
// returns the mention placeholder string
// WARNING: this string should match the regex right above this
function getMentionPlaceholder(name, type, id) {
return `@{${name}_${type}_${id}}`;
}
export function getFirstName(name) {
const index = name.indexOf(' ');
return index > 0 ? name.substring(0, index) : name;
}
// converts a string of text into operation deltas
// used for drafts, where the mention object is our own placeholder string
export function buildContents(text, mentions) {
try {
// seperate out our mention placeholder so we can parse them
const split = text.split(mentionRegexMatchWhole);
// goes through the lines looking for ones that match the mention placeholder
const contents = split.map((line) => {
const matches = mentionRegexMatchValues.exec(line);
// if found, convert our placeholder mention into a mention delta
if (matches && matches.length === 4) {
const name = matches[1];
const type = matches[2];
const id = matches[3];
// make sure all the fields are valid before inserting the mention delta
if (type === 'groupMention' || type === 'person') {
if (id === 'all' || uuidRegex.test(id)) {
// if the mention list wasn't provided then go ahead and insert
if (!mentions || mentions.some((mention) => mention.id === id)) {
return {
insert: {
mention: {
index: 0,
denotationChar: '@',
id,
objectType: type,
value: name,
},
},
};
}
}
}
}
// otherwise just insert the text
return {insert: line};
});
return contents;
} catch (e) {
e.func = 'buildContents';
throw e;
}
}
// gets the text inside the composer
export function getQuillText(quill) {
try {
const contents = quill.getContents();
let text = '';
contents.forEach((op) => {
if (typeof op.insert === 'string') {
// if its just a string then we can insert right away
text += op.insert;
} else if (typeof op.insert === 'object') {
if (op.insert.mention) {
// if it's a mention object, then we insert a placeholder for later
const {mention} = op.insert;
text += getMentionPlaceholder(mention.value, mention.objectType, mention.id);
}
}
});
return text;
} catch (e) {
e.func = 'getQuillText';
throw e;
}
}
// convert placeholder mentions to <spark-mention> elements
export function replaceMentions(text, mentions) {
try {
return text.replace(mentionRegexMatchValues, (match, name, type, id) => {
let sb = '';
if (type === 'groupMention') {
// check if an all mention was inserted to the composer
if (id === 'moderators' || id === 'here') {
mentions.people.forEach((mention, i) => {
sb += `<spark-mention data-object-type='${mention.objectType}' data-object-id='${mention.id}'>${mention.name}</spark-mention>`;
if (i < mentions.people.length - 1) {
sb += ', ';
}
});
} else if (id === 'all' && mentions.group.some((mention) => mention.groupType === id)) {
sb = `<spark-mention data-object-type='${type}' data-group-type='${id}'>${name}</spark-mention>`;
}
} else if (type === 'person') {
if (uuidRegex.test(id)) {
// only convert the ids that are in the list of mentions
if (mentions.people.some((mention) => mention.id === id)) {
sb = `<spark-mention data-object-type='${type}' data-object-id='${id}'>${name}</spark-mention>`;
}
}
}
return sb || match;
});
} catch (e) {
e.func = 'replaceMentions';
throw e;
}
}
// get the mention objects currently in the editor
export function getMentions(quill) {
try {
const contents = quill.getContents();
const mentions = {
group: [],
people: [],
};
contents.forEach((op) => {
if (typeof op.insert === 'object' && op.insert.mention) {
const {mention} = op.insert;
if (mention.objectType === 'person') {
mentions.people.push({
id: mention.id,
objectType: mention.objectType,
});
mentions.mentionType = 'person';
} else if (mention.objectType === 'groupMention' && mention.id === 'all') {
mentions.group.push({
groupType: mention.id,
objectType: mention.objectType,
});
mentions.mentionType = mention.id;
} else if (
mention.objectType === 'groupMention' &&
mention.items &&
(mention.id === 'moderators' || mention.id === 'here')
) {
mentions.mentionType = mention.id;
const list = JSON.parse(mention.items);
list.forEach((person) => {
mentions.people.push({
id: person.id,
objectType: person.objectType,
name: person.value,
});
});
}
}
});
return mentions;
} catch (e) {
e.func = 'getMentions';
throw e;
}
}
// builds up the avatar for a mention item
export function buildMentionAvatar(item) {
const {id, src, displayName} = item;
let classes = 'ql-mention-avatar';
let avatar;
if (src) {
// if we have a picture then use that
avatar = `<img class='${classes}' alt='Avatar for ${displayName}' src='${src}'>`;
} else {
// otherwise we build it ourself
let initials;
if (id === 'all' || id === 'moderators' || id === 'here') {
// avatar is a circle @ for all
classes += ' group-mention';
initials = '@';
} else {
// use the initials of the name as the avatar
let chars = displayName.charAt(0);
const space = displayName.indexOf(' ');
if (space >= 0) {
chars += displayName.charAt(space + 1);
}
initials = chars.toUpperCase();
}
avatar = `<div class='${classes}'>${initials}</div>`;
}
return avatar;
}
// build the text element for mention item
export function buildMentionText(item) {
const {displayName, secondary} = item;
let text = '';
text += "<div class='ql-mention-item-text'>";
text += "<div class='ql-mention-item-text-primary'>";
text += displayName;
text += '</div>';
if (secondary) {
text += "<div class='ql-mention-item-text-secondary'>";
text += secondary;
text += '</div>';
}
text += '</div>';
return text;
}
// converts <spark-mention> elements to our placeholder mention string
export function keepReplacement(content, node) {
// should always be spark-mention but just in case
if (node.tagName === 'SPARK-MENTION') {
const type = node.getAttribute('data-object-type');
let id;
if (type === 'groupMention') {
id = node.getAttribute('data-group-type');
} else if (type === 'person') {
id = node.getAttribute('data-object-id');
}
if (id) {
return getMentionPlaceholder(content, type, id);
}
}
return content;
}
// quill's empty state is not really empty, needs custom matcher
const emptyQuillContentsMatcher = '{"ops":[{"insert":"\\n"}]}';
export function isQuillEmpty(quill) {
if (JSON.stringify(quill.getContents()) === emptyQuillContentsMatcher) {
return true;
}
return false;
}
// takes keyBindings passed as props, returns decorated keybindings
export function addEmptyCheckToHandlerParams(keyBindings) {
const keyBindingKeys = Object.keys(keyBindings);
return keyBindingKeys.reduce((decoratedKeyBindings, keyBindingKey) => {
const keyBinding = keyBindings[keyBindingKey];
// store original handler fnc
const keyBindingHandler = keyBinding.handler;
// monkey patch for handler function that passes reference to quill editor without losing this binding
keyBinding.handler = function handler(range, context) {
// stores this binding for closing over in util function
const that = this;
function boundIsQuillEmpty() {
return isQuillEmpty(that.quill);
}
// calls handler with util passed through
return keyBindingHandler(range, context, boundIsQuillEmpty);
};
// eslint-disable-next-line no-param-reassign
decoratedKeyBindings[keyBindingKey] = keyBinding;
return decoratedKeyBindings;
}, {});
}
// compares key binding uniqueIds to find bindings that need replacing
export function getKeyBindingDelta(prevKeyBindings, newKeyBindings) {
const newKeyBindingsKeys = Object.keys(newKeyBindings);
const keysToUpdate = {};
newKeyBindingsKeys.forEach((currentKey) => {
const oldId = prevKeyBindings[currentKey]?.uniqueId;
const newId = newKeyBindings[currentKey]?.uniqueId;
// new ID exists and doesnt match old ID, flag for update
if (typeof newId !== 'undefined' && oldId !== newId) {
keysToUpdate[currentKey] = oldId;
}
});
return keysToUpdate;
}
// replaces quill instance's outdated key bindings with fresh data and callbacks
export function updateKeyBindings(quill, bindingsToReplace, newKeyBindings) {
const quillBindings = quill.keyboard.bindings;
// loop over keys and values of outdated bindings
for (const [bindingName, idToReplace] of Object.entries(bindingsToReplace)) {
const currentNewBinding = newKeyBindings[bindingName];
const currentKeyCode = currentNewBinding.key;
// all bindings registered to quill instance
const currentQuillBindings = quillBindings[currentKeyCode];
// compares binding's id to id that needs to be replaced with fresh data
const quillBindingToReplace = currentQuillBindings.findIndex(
(currentBinding) => typeof currentBinding.uniqueId !== 'undefined' && currentBinding.uniqueId === idToReplace
);
// remove the old binding, if it exists
if (quillBindingToReplace > -1) {
currentQuillBindings.splice(quillBindingToReplace, 1);
}
quill.keyboard.addBinding(currentNewBinding);
}
}