-
-
Notifications
You must be signed in to change notification settings - Fork 686
/
utils.js
246 lines (217 loc) · 6.23 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
import { Editor, Transforms, Node } from 'slate';
/**
* @description Creates or updates an existing $elementType. It also takes care
* of the saved selection and uses PathRef.
*
* @param {Editor} editor The Slate editor for the context
* @param {object} data Relevant data for this element
*
* @returns {boolean} true if an element was possibly inserted, false otherwise
* (currently we do not check here if the element was already applied to the
* editor)
*/
export const _insertElement = (elementType) => (editor, data) => {
if (editor.getSavedSelection()) {
const selection = editor.selection || editor.getSavedSelection();
const rangeRef = Editor.rangeRef(editor, selection);
const res = Array.from(
Editor.nodes(editor, {
match: (n) => n.type === elementType,
mode: 'highest',
at: selection,
}),
);
if (res.length) {
const [, path] = res[0];
Transforms.setNodes(
editor,
{ data },
{
at: path ? path : null,
match: path ? (n) => n.type === elementType : null,
},
);
} else {
Transforms.wrapNodes(
editor,
{ type: elementType, data },
{
split: true,
at: selection,
match: (node) => {
return Node.string(node).length !== 0;
},
},
);
}
const sel = JSON.parse(JSON.stringify(rangeRef.current));
setTimeout(() => {
Transforms.select(editor, sel);
editor.setSavedSelection(sel);
});
return true;
}
return false;
};
/**
* Will unwrap a node that has as type the one received or one from an array.
* It identifies the current target element and expands the selection to it, in
* case the selection was just partial. This allows a "clear and reassign"
* operation, for example for the Link plugin.
*
* @param {string|Object[]} elementType - this can be a string or an array of strings
* @returns {Object|null} - current node
*/
export const _unwrapElement = (elementType) => (editor) => {
const selection = editor.selection || editor.getSavedSelection();
let [link] = Editor.nodes(editor, {
at: selection,
match: (node) => node?.type === elementType,
});
const isAtStart =
selection.anchor.offset === 0 && selection.focus.offset === 0;
if (!link && !isAtStart) return false;
if (!link) {
try {
link = Editor.previous(editor, {
at: selection.anchor.path,
});
} catch (ex) {
link = [];
}
}
const [, path] = link;
const [start, end] = Editor.edges(editor, path);
const range = { anchor: start, focus: end };
const ref = Editor.rangeRef(editor, range);
Transforms.select(editor, range);
Transforms.unwrapNodes(editor, {
match: (n) =>
Array.isArray(elementType)
? elementType.includes(n.type)
: n.type === elementType,
at: range,
});
const current = ref.current;
ref.unref();
return current;
};
export const _isActiveElement = (elementType) => (editor) => {
const selection = editor.selection || editor.getSavedSelection();
let found;
try {
found = Array.from(
Editor.nodes(editor, {
match: (n) => n.type === elementType,
at: selection,
}) || [],
);
} catch (e) {
// eslint-disable-next-line
// console.warn('Error in finding active element', e);
return false;
}
if (found.length) return true;
if (selection) {
const { path } = selection.anchor;
const isAtStart =
selection.anchor.offset === 0 && selection.focus.offset === 0;
if (isAtStart) {
try {
found = Editor.previous(editor, {
at: path,
// match: (n) => n.type === MENTION,
});
} catch (ex) {
found = [];
}
if (found && found[0] && found[0].type === elementType) {
return true;
}
}
}
return false;
};
/**
* Will look for a node that has as type the one received or one from an array
* @param {string|Object[]} elementType - this can be a string or an array of strings
* @returns {Object|null} - found node
*/
export const _getActiveElement =
(elementType) =>
(editor, direction = 'any') => {
const selection = editor.selection || editor.getSavedSelection();
let found = [];
try {
found = Array.from(
Editor.nodes(editor, {
match: (n) =>
Array.isArray(elementType)
? elementType.includes(n.type)
: n.type === elementType,
at: selection,
}),
);
} catch (e) {
return null;
}
if (found.length) return found[0];
if (!selection) return null;
if (direction === 'any' || direction === 'backward') {
const { path } = selection.anchor;
const isAtStart =
selection.anchor.offset === 0 && selection.focus.offset === 0;
if (isAtStart) {
let found;
try {
found = Editor.previous(editor, {
at: path,
});
} catch (ex) {
// eslint-disable-next-line no-console
console.warn('Unable to find previous node', editor, path);
return;
}
if (found && found[0] && found[0].type === elementType) {
if (
(Array.isArray(elementType) &&
elementType.includes(found[0].type)) ||
found[0].type === elementType
) {
return found;
}
} else {
return null;
}
}
}
if (direction === 'any' || direction === 'forward') {
const { path } = selection.anchor;
const isAtStart =
selection.anchor.offset === 0 && selection.focus.offset === 0;
if (isAtStart) {
let found;
try {
found = Editor.next(editor, {
at: path,
});
} catch (e) {
// eslint-disable-next-line
console.warn('Unable to find next node', editor, path);
return;
}
if (found && found[0] && found[0].type === elementType) {
if (
(Array.isArray(elementType) &&
elementType.includes(found[0].type)) ||
found[0].type === elementType
) {
return found;
}
} else {
return null;
}
}
}
return null;
};