@use-kona/editor 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/createEditable.js +1 -0
- package/dist/core/queries.d.ts +5 -2
- package/dist/core/queries.js +5 -2
- package/dist/plugins/ListsPlugin/ListsPlugin.d.ts +1 -0
- package/dist/plugins/ListsPlugin/ListsPlugin.js +42 -5
- package/package.json +1 -1
- package/src/core/createEditable.tsx +4 -0
- package/src/core/queries.ts +8 -2
- package/src/plugins/ListsPlugin/ListsPlugin.tsx +64 -9
|
@@ -39,6 +39,7 @@ const createEditable = (editor, plugins)=>({ readOnly })=>{
|
|
|
39
39
|
return result;
|
|
40
40
|
};
|
|
41
41
|
const handleHotkey = (event)=>{
|
|
42
|
+
if ('Tab' === event.key) event.preventDefault();
|
|
42
43
|
for (const plugin of plugins)if (plugin.hotkeys) {
|
|
43
44
|
for (const hotkey of plugin.hotkeys)if (is_hotkey(hotkey[0], event) && hotkey[1]) {
|
|
44
45
|
event.preventDefault();
|
package/dist/core/queries.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
import { Editor, NodeEntry
|
|
2
|
-
|
|
1
|
+
import { Editor, type NodeEntry } from 'slate';
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated
|
|
4
|
+
*/
|
|
5
|
+
export declare const getPrev: (editor: Editor, node: NodeEntry) => NodeEntry<import("slate").Ancestor> | null | undefined;
|
package/dist/core/queries.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Editor, Path } from "slate";
|
|
2
2
|
const getPrev = (editor, node)=>{
|
|
3
3
|
try {
|
|
4
4
|
const [, path] = node;
|
|
5
|
-
return
|
|
5
|
+
return Editor.above(editor, {
|
|
6
|
+
at: Path.previous(path),
|
|
7
|
+
mode: 'lowest'
|
|
8
|
+
});
|
|
6
9
|
} catch (e) {
|
|
7
10
|
return null;
|
|
8
11
|
}
|
|
@@ -30,6 +30,7 @@ export declare class ListsPlugin implements IPlugin {
|
|
|
30
30
|
private isListItem;
|
|
31
31
|
static toggleList: (editor: Editor, listType: string, listItemType?: string) => void;
|
|
32
32
|
private getListItemDepth;
|
|
33
|
+
private getListDepth;
|
|
33
34
|
private getListItem;
|
|
34
35
|
}
|
|
35
36
|
export {};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { Editor, Element, Node, Transforms } from "slate";
|
|
3
3
|
import { jsx as external_slate_hyperscript_jsx } from "slate-hyperscript";
|
|
4
|
-
import { getPrev } from "../../core/queries.js";
|
|
5
4
|
import styles_module from "./styles.module.js";
|
|
6
5
|
class ListsPlugin {
|
|
7
6
|
options;
|
|
@@ -48,6 +47,24 @@ class ListsPlugin {
|
|
|
48
47
|
split: true
|
|
49
48
|
});
|
|
50
49
|
}
|
|
50
|
+
if (this.isList(editor, node)) {
|
|
51
|
+
const prevListItemPath = Editor.before(editor, path, {
|
|
52
|
+
unit: 'block'
|
|
53
|
+
});
|
|
54
|
+
const prevList = Editor.above(editor, {
|
|
55
|
+
at: prevListItemPath,
|
|
56
|
+
match: (n)=>this.isList(editor, n)
|
|
57
|
+
});
|
|
58
|
+
if (prevList) {
|
|
59
|
+
const currentDepth = this.getListDepth(editor, path);
|
|
60
|
+
const prevDepth = this.getListDepth(editor, prevList[1]);
|
|
61
|
+
if (this.isList(editor, prevList[0]) && currentDepth === prevDepth) try {
|
|
62
|
+
Transforms.mergeNodes(editor, {
|
|
63
|
+
match: (n)=>this.isList(editor, n)
|
|
64
|
+
});
|
|
65
|
+
} catch (e) {}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
51
68
|
normalizeNode(entry);
|
|
52
69
|
};
|
|
53
70
|
editor.insertBreak = ()=>{
|
|
@@ -152,9 +169,15 @@ class ListsPlugin {
|
|
|
152
169
|
else if (!event.shiftKey) {
|
|
153
170
|
const currentListItem = this.getListItem(editor);
|
|
154
171
|
if (!currentListItem) return;
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
172
|
+
const prevListItemPath = Editor.before(editor, currentListItem[1], {
|
|
173
|
+
unit: 'block'
|
|
174
|
+
});
|
|
175
|
+
if (currentListItem && prevListItemPath) {
|
|
176
|
+
const prevListItem = Editor.above(editor, {
|
|
177
|
+
at: prevListItemPath,
|
|
178
|
+
match: (n)=>this.isListItem(editor, n)
|
|
179
|
+
});
|
|
180
|
+
if (!prevListItem) return;
|
|
158
181
|
const currentDepth = this.getListItemDepth(editor, currentListItem[1]);
|
|
159
182
|
const prevDepth = this.getListItemDepth(editor, prevListItem[1]);
|
|
160
183
|
if (prevListItem && prevDepth >= currentDepth) Transforms.wrapNodes(editor, {
|
|
@@ -220,9 +243,23 @@ class ListsPlugin {
|
|
|
220
243
|
if (item) {
|
|
221
244
|
let count = 0;
|
|
222
245
|
let parent = Editor.parent(editor, item[1]);
|
|
223
|
-
|
|
246
|
+
do {
|
|
224
247
|
parent = Editor.parent(editor, parent[1]);
|
|
225
248
|
count++;
|
|
249
|
+
}while (null !== parent && this.isList(editor, parent[0]));
|
|
250
|
+
return count;
|
|
251
|
+
}
|
|
252
|
+
return 0;
|
|
253
|
+
};
|
|
254
|
+
getListDepth = (editor, path)=>{
|
|
255
|
+
if (path) {
|
|
256
|
+
const node = Node.get(editor, path);
|
|
257
|
+
if (!this.isList(editor, node)) return 0;
|
|
258
|
+
let count = 0;
|
|
259
|
+
let parent = Editor.parent(editor, path);
|
|
260
|
+
while(parent && this.isList(editor, parent[0])){
|
|
261
|
+
count++;
|
|
262
|
+
parent = Editor.parent(editor, parent[1]);
|
|
226
263
|
}
|
|
227
264
|
return count;
|
|
228
265
|
}
|
package/package.json
CHANGED
|
@@ -80,6 +80,10 @@ export const createEditable =
|
|
|
80
80
|
};
|
|
81
81
|
|
|
82
82
|
const handleHotkey = (event: KeyboardEvent) => {
|
|
83
|
+
if (event.key === 'Tab') {
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
}
|
|
86
|
+
|
|
83
87
|
for (const plugin of plugins) {
|
|
84
88
|
if (plugin.hotkeys) {
|
|
85
89
|
for (const hotkey of plugin.hotkeys) {
|
package/src/core/queries.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import { Editor, NodeEntry,
|
|
1
|
+
import { Editor, type NodeEntry, Path } from 'slate';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated
|
|
5
|
+
*/
|
|
3
6
|
export const getPrev = (editor: Editor, node: NodeEntry) => {
|
|
4
7
|
try {
|
|
5
8
|
const [, path] = node;
|
|
6
|
-
return
|
|
9
|
+
return Editor.above(editor, {
|
|
10
|
+
at: Path.previous(path),
|
|
11
|
+
mode: 'lowest',
|
|
12
|
+
});
|
|
7
13
|
} catch (e) {
|
|
8
14
|
return null;
|
|
9
15
|
}
|
|
@@ -88,6 +88,33 @@ export class ListsPlugin implements IPlugin {
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
if (this.isList(editor, node as CustomElement)) {
|
|
92
|
+
const prevListItemPath = Editor.before(editor, path, {
|
|
93
|
+
unit: 'block',
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const prevList = Editor.above(editor, {
|
|
97
|
+
at: prevListItemPath,
|
|
98
|
+
match: (n) => this.isList(editor, n as CustomElement),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (prevList) {
|
|
102
|
+
const currentDepth = this.getListDepth(editor, path);
|
|
103
|
+
const prevDepth = this.getListDepth(editor, prevList[1]);
|
|
104
|
+
|
|
105
|
+
if (
|
|
106
|
+
this.isList(editor, prevList[0] as CustomElement) &&
|
|
107
|
+
currentDepth === prevDepth
|
|
108
|
+
) {
|
|
109
|
+
try {
|
|
110
|
+
Transforms.mergeNodes(editor, {
|
|
111
|
+
match: (n) => this.isList(editor, n as CustomElement),
|
|
112
|
+
});
|
|
113
|
+
} catch (e) {}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
91
118
|
normalizeNode(entry);
|
|
92
119
|
};
|
|
93
120
|
|
|
@@ -249,17 +276,25 @@ export class ListsPlugin implements IPlugin {
|
|
|
249
276
|
return;
|
|
250
277
|
}
|
|
251
278
|
|
|
252
|
-
const
|
|
279
|
+
const prevListItemPath = Editor.before(editor, currentListItem[1], {
|
|
280
|
+
unit: 'block',
|
|
281
|
+
});
|
|
253
282
|
|
|
254
|
-
if (
|
|
255
|
-
|
|
256
|
-
|
|
283
|
+
if (currentListItem && prevListItemPath) {
|
|
284
|
+
const prevListItem = Editor.above(editor, {
|
|
285
|
+
at: prevListItemPath,
|
|
286
|
+
match: (n) => this.isListItem(editor, n as CustomElement),
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
if (!prevListItem) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
257
292
|
|
|
258
|
-
if (currentListItem && prevListItem) {
|
|
259
293
|
const currentDepth = this.getListItemDepth(
|
|
260
294
|
editor,
|
|
261
295
|
currentListItem[1],
|
|
262
296
|
);
|
|
297
|
+
|
|
263
298
|
const prevDepth = this.getListItemDepth(editor, prevListItem[1]);
|
|
264
299
|
|
|
265
300
|
if (prevListItem && prevDepth >= currentDepth) {
|
|
@@ -370,17 +405,37 @@ export class ListsPlugin implements IPlugin {
|
|
|
370
405
|
if (item) {
|
|
371
406
|
let count = 0;
|
|
372
407
|
let parent = Editor.parent(editor, item[1]);
|
|
373
|
-
|
|
408
|
+
do {
|
|
409
|
+
parent = Editor.parent(editor, parent[1]);
|
|
410
|
+
count++;
|
|
411
|
+
} while (
|
|
374
412
|
parent !== null &&
|
|
375
413
|
this.isList(editor, parent[0] as CustomElement)
|
|
376
|
-
)
|
|
377
|
-
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
return count;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return 0;
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
private getListDepth = (editor: Editor, path?: Path) => {
|
|
423
|
+
if (path) {
|
|
424
|
+
const node = Node.get(editor, path) as CustomElement;
|
|
425
|
+
if (!this.isList(editor, node)) {
|
|
426
|
+
return 0;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
let count = 0;
|
|
430
|
+
let parent = Editor.parent(editor, path);
|
|
431
|
+
|
|
432
|
+
while (parent && this.isList(editor, parent[0] as CustomElement)) {
|
|
378
433
|
count++;
|
|
434
|
+
parent = Editor.parent(editor, parent[1]);
|
|
379
435
|
}
|
|
380
436
|
|
|
381
437
|
return count;
|
|
382
438
|
}
|
|
383
|
-
|
|
384
439
|
return 0;
|
|
385
440
|
};
|
|
386
441
|
|