@uxf/wysiwyg 15.1.0 → 15.2.1
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/LICENSE +21 -0
- package/README.md +2 -0
- package/package.json +9 -9
- package/plugins/list/create-list-plugin.js +93 -4
- package/plugins/list/list-item-outside-list.test.d.ts +1 -0
- package/plugins/list/list-item-outside-list.test.js +167 -0
- package/plugins/list/list-item-with-several-contents.test.d.ts +1 -0
- package/plugins/list/list-item-with-several-contents.test.js +96 -0
- package/wysiwyg-editor.js +64 -52
- package/wysiwyg-editor.test.js +54 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022–2026 UX Fans s.r.o.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# @uxf/wysiwyg
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@uxf/wysiwyg)
|
|
4
|
+
|
|
3
5
|
Rich-text / WYSIWYG editor for React, built on [Plate](https://platejs.org) / [Slate](https://docs.slatejs.org). It ships a ready-made `WysiwygEditor` component, a toolbar, and a set of UXF-styled plugins (headings, marks, lists, links, images, videos, buttons, blockquote, highlight).
|
|
4
6
|
|
|
5
7
|
## When to use
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uxf/wysiwyg",
|
|
3
|
-
"version": "15.1
|
|
3
|
+
"version": "15.2.1",
|
|
4
4
|
"description": "UXF Wysiwyg editor",
|
|
5
|
-
"author": "
|
|
6
|
-
"homepage": "https://
|
|
5
|
+
"author": "UX Fans s.r.o. <dev@uxf.cz>",
|
|
6
|
+
"homepage": "https://fe.uxf.dev/docs/wysiwyg",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"@platejs/link": "53.3.5",
|
|
19
19
|
"@platejs/list-classic": "53.0.0",
|
|
20
20
|
"@platejs/media": "53.1.4",
|
|
21
|
-
"@uxf/core": "15.
|
|
22
|
-
"@uxf/core-react": "15.
|
|
23
|
-
"@uxf/ui": "15.1
|
|
21
|
+
"@uxf/core": "15.2.1",
|
|
22
|
+
"@uxf/core-react": "15.2.1",
|
|
23
|
+
"@uxf/ui": "15.2.1",
|
|
24
24
|
"platejs": "53.3.15",
|
|
25
25
|
"react": "^19.3.0",
|
|
26
26
|
"react-dom": "^19.3.0"
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"@platejs/media": "53.1.4",
|
|
34
34
|
"@types/react": "19.3.0",
|
|
35
35
|
"@types/react-dom": "19.3.0",
|
|
36
|
-
"@uxf/core": "15.
|
|
37
|
-
"@uxf/core-react": "15.
|
|
38
|
-
"@uxf/ui": "15.1
|
|
36
|
+
"@uxf/core": "15.2.1",
|
|
37
|
+
"@uxf/core-react": "15.2.1",
|
|
38
|
+
"@uxf/ui": "15.2.1",
|
|
39
39
|
"platejs": "53.3.15",
|
|
40
40
|
"react": "19.3.1",
|
|
41
41
|
"react-dom": "19.3.1"
|
|
@@ -1,10 +1,91 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createListPlugin = exports.toggleList = exports.getListItemEntry = void 0;
|
|
4
|
+
const list_classic_1 = require("@platejs/list-classic");
|
|
4
5
|
const react_1 = require("@platejs/list-classic/react");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "
|
|
6
|
+
const platejs_1 = require("platejs");
|
|
7
|
+
var list_classic_2 = require("@platejs/list-classic");
|
|
8
|
+
Object.defineProperty(exports, "getListItemEntry", { enumerable: true, get: function () { return list_classic_2.getListItemEntry; } });
|
|
9
|
+
Object.defineProperty(exports, "toggleList", { enumerable: true, get: function () { return list_classic_2.toggleList; } });
|
|
10
|
+
/** The type of `node` when list-classic takes it for a list (`ul`, `ol`, `taskList`, as configured). */
|
|
11
|
+
function getListType(editor, node) {
|
|
12
|
+
return platejs_1.ElementApi.isElement(node) && (0, list_classic_1.isListRoot)(editor, node) ? node.type : undefined;
|
|
13
|
+
}
|
|
14
|
+
function isListItem(editor, node) {
|
|
15
|
+
return platejs_1.ElementApi.isElement(node) && node.type === editor.getType(platejs_1.KEYS.li);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* list-classic takes an `li`'s parent for its list without checking that it is one. For an `li` at the root
|
|
19
|
+
* that parent is the editor itself, so Enter or Backspace in an empty item moves it "out of" the root and Slate
|
|
20
|
+
* throws "Cannot get the parent path of the root path []". Its normalizer never repairs such an item, and
|
|
21
|
+
* Slate's `insertFragment` makes one whenever a pasted paragraph is followed by a list of two or more items: the
|
|
22
|
+
* items before the last one are lifted out of their list. Stored documents may hold them too.
|
|
23
|
+
*
|
|
24
|
+
* - An `li` in another element (an `li`, a paragraph) is lifted out of it, Slate's `liftNodes` keeps the order
|
|
25
|
+
* and splits the element around it. It is not wrapped there: a list as the first child of an `li` makes
|
|
26
|
+
* list-classic's normalizer remove the first item of the surrounding list, and a list in a paragraph is
|
|
27
|
+
* not valid HTML. Lifted to the root, it is wrapped there.
|
|
28
|
+
* - A run of `li`s at the root is wrapped into one list: of the type of the list after the run, which
|
|
29
|
+
* list-classic's normalizer then merges it into, else of the list before it, else a bulleted one, which is
|
|
30
|
+
* how a browser renders a bare `<li>` anyway. After the paste above the neighbours are the two halves of the
|
|
31
|
+
* pasted-into paragraph, so the lifted items of a pasted numbered list become a bulleted list; the original
|
|
32
|
+
* type is no longer in the document.
|
|
33
|
+
*
|
|
34
|
+
* @returns whether it changed the document
|
|
35
|
+
*/
|
|
36
|
+
function repairListItemOutsideList(editor, [node, path]) {
|
|
37
|
+
var _a, _b;
|
|
38
|
+
if (!isListItem(editor, node)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (path.length > 1) {
|
|
42
|
+
if (getListType(editor, platejs_1.NodeApi.get(editor, platejs_1.PathApi.parent(path)))) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
editor.tf.liftNodes({ at: path });
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
const siblings = editor.children;
|
|
49
|
+
const [index] = path;
|
|
50
|
+
let first = index;
|
|
51
|
+
let last = index;
|
|
52
|
+
while (first > 0 && isListItem(editor, siblings[first - 1])) {
|
|
53
|
+
first--;
|
|
54
|
+
}
|
|
55
|
+
while (last < siblings.length - 1 && isListItem(editor, siblings[last + 1])) {
|
|
56
|
+
last++;
|
|
57
|
+
}
|
|
58
|
+
const type = (_b = (_a = getListType(editor, siblings[last + 1])) !== null && _a !== void 0 ? _a : getListType(editor, siblings[first - 1])) !== null && _b !== void 0 ? _b : editor.getType(platejs_1.KEYS.ulClassic);
|
|
59
|
+
// one wrapper for the whole run: wrapNodes wraps every match between the two ends together
|
|
60
|
+
editor.tf.wrapNodes({ children: [], type }, { at: editor.api.range([first], [last]), match: (n, p) => p.length === 1 && isListItem(editor, n) });
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* An `li` holds one `lic`, its text, followed by at most its sublist. list-classic's normalizer accepts a `lic`
|
|
65
|
+
* at any position though, and Slate's own Enter makes a second one: with a selection from a paragraph to the
|
|
66
|
+
* start of an item, list-classic finds no single `lic` above the selection and leaves the break to Slate, which
|
|
67
|
+
* deletes the range and splits the `lic` inside its `li`. Enter later in that second `lic` then crashed with
|
|
68
|
+
* "Invalid attempt to destructure non-iterable instance": list-classic's `insertListItem` takes any second child
|
|
69
|
+
* for the sublist and moves the node after the split-off text, which is not there.
|
|
70
|
+
*
|
|
71
|
+
* The `li` is split before every `lic` after its first child, so each one becomes an item of its own, in order,
|
|
72
|
+
* with whatever follows it (its sublist) staying with it.
|
|
73
|
+
*
|
|
74
|
+
* @returns whether it changed the document
|
|
75
|
+
*/
|
|
76
|
+
function splitListItemWithSeveralContents(editor, [node, path]) {
|
|
77
|
+
if (!platejs_1.ElementApi.isElement(node) || !isListItem(editor, node)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const contentType = editor.getType(platejs_1.KEYS.lic);
|
|
81
|
+
const index = node.children.findIndex((child, childIndex) => childIndex > 0 && platejs_1.ElementApi.isElement(child) && child.type === contentType);
|
|
82
|
+
if (index === -1) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
// at a path, splitNodes splits its parent right before it and copies the parent's properties to the new half
|
|
86
|
+
editor.tf.splitNodes({ at: [...path, index] });
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
8
89
|
/**
|
|
9
90
|
* list-classic, not the newer indent-based `@platejs/list`: it yields exactly `ul` / `ol` / `li` /
|
|
10
91
|
* `lic`, the shape every stored document already has.
|
|
@@ -13,5 +94,13 @@ Object.defineProperty(exports, "toggleList", { enumerable: true, get: function (
|
|
|
13
94
|
* stored format has never contained. Disabled through the parent, which carries `withList`'s editor
|
|
14
95
|
* overrides; registering the four children individually would lose them.
|
|
15
96
|
*/
|
|
16
|
-
const createListPlugin = () => react_1.ListPlugin.configure({ override: { enabled: { taskList: false } } })
|
|
97
|
+
const createListPlugin = () => react_1.ListPlugin.configure({ override: { enabled: { taskList: false } } }).overrideEditor(({ editor, tf: { normalizeNode } }) => ({
|
|
98
|
+
transforms: {
|
|
99
|
+
normalizeNode(entry, options) {
|
|
100
|
+
if (!repairListItemOutsideList(editor, entry) && !splitListItemWithSeveralContents(editor, entry)) {
|
|
101
|
+
normalizeNode(entry, options);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
}));
|
|
17
106
|
exports.createListPlugin = createListPlugin;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const platejs_1 = require("platejs");
|
|
4
|
+
const react_1 = require("platejs/react");
|
|
5
|
+
const create_all_plugins_with_ui_1 = require("../../create-all-plugins-with-ui");
|
|
6
|
+
const FILE = { extension: ".png", id: 1, name: "a.png", namespace: "wysiwyg", uuid: "u" };
|
|
7
|
+
/** The editor exactly as `WysiwygEditor` builds it from the full plugin set. */
|
|
8
|
+
function createEditor(value) {
|
|
9
|
+
return (0, react_1.createPlateEditor)({
|
|
10
|
+
plugins: (0, create_all_plugins_with_ui_1.createAllPluginsWithUi)({
|
|
11
|
+
image: { getImageUrl: (file) => file.name, uploadImage: async () => FILE },
|
|
12
|
+
}),
|
|
13
|
+
shouldNormalizeEditor: true,
|
|
14
|
+
value,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const listItem = (text) => ({ children: [{ children: [{ text }], type: "lic" }], type: "li" });
|
|
18
|
+
const paragraph = (text) => ({ children: [{ text }], type: "paragraph" });
|
|
19
|
+
/** Paths of the `li` nodes whose parent is not a list – the editor root included. */
|
|
20
|
+
function listItemsOutsideList(editor) {
|
|
21
|
+
return Array.from(platejs_1.NodeApi.nodes(editor))
|
|
22
|
+
.filter(([node]) => node.type === "li")
|
|
23
|
+
.filter(([, path]) => {
|
|
24
|
+
var _a;
|
|
25
|
+
const parent = path.length === 1 ? undefined : platejs_1.NodeApi.get(editor, platejs_1.PathApi.parent(path));
|
|
26
|
+
return !["ul", "ol", "taskList"].includes((_a = parent === null || parent === void 0 ? void 0 : parent.type) !== null && _a !== void 0 ? _a : "");
|
|
27
|
+
})
|
|
28
|
+
.map(([, path]) => path);
|
|
29
|
+
}
|
|
30
|
+
describe("an li outside a list", () => {
|
|
31
|
+
// Slate's insertFragment keeps the last block of a fragment and lifts the blocks before it out of their
|
|
32
|
+
// wrappers, so the list's earlier items land at the root as bare `li`s. list-classic leaves them there.
|
|
33
|
+
it("is not left behind by pasting a paragraph followed by a list into a paragraph", () => {
|
|
34
|
+
const editor = createEditor([paragraph("ab")]);
|
|
35
|
+
editor.tf.select({ offset: 1, path: [0, 0] });
|
|
36
|
+
editor.tf.insertFragment([
|
|
37
|
+
paragraph("x"),
|
|
38
|
+
{ children: [listItem("first"), listItem("second")], type: "ul" },
|
|
39
|
+
]);
|
|
40
|
+
expect(listItemsOutsideList(editor)).toEqual([]);
|
|
41
|
+
});
|
|
42
|
+
it("is wrapped into a list when a stored document has one", () => {
|
|
43
|
+
var _a;
|
|
44
|
+
const editor = createEditor([listItem("a")]);
|
|
45
|
+
expect(listItemsOutsideList(editor)).toEqual([]);
|
|
46
|
+
expect((_a = editor.children.at(0)) === null || _a === void 0 ? void 0 : _a.type).toBe("ul");
|
|
47
|
+
});
|
|
48
|
+
// list-classic takes the editor root for the item's list, and moving the item out of it inserts a node at
|
|
49
|
+
// the root path: "Cannot get the parent path of the root path []".
|
|
50
|
+
it("survives Enter and Backspace in an empty item", () => {
|
|
51
|
+
const editor = createEditor([listItem("a")]);
|
|
52
|
+
editor.tf.select(editor.api.end([0]));
|
|
53
|
+
expect(() => {
|
|
54
|
+
editor.tf.insertBreak();
|
|
55
|
+
editor.tf.deleteBackward("character");
|
|
56
|
+
}).not.toThrow();
|
|
57
|
+
});
|
|
58
|
+
it("survives Enter in an empty item that is the whole document", () => {
|
|
59
|
+
const editor = createEditor([listItem("")]);
|
|
60
|
+
editor.tf.select(editor.api.end([0]));
|
|
61
|
+
expect(() => editor.tf.insertBreak()).not.toThrow();
|
|
62
|
+
});
|
|
63
|
+
it("merges into the list next to it instead of starting a new one", () => {
|
|
64
|
+
const editor = createEditor([{ children: [listItem("a")], type: "ol" }, listItem("b")]);
|
|
65
|
+
expect(editor.children.at(0)).toMatchObject({
|
|
66
|
+
children: [{ children: [{ children: [{ text: "a" }] }] }, { children: [{ children: [{ text: "b" }] }] }],
|
|
67
|
+
type: "ol",
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
it("merges into the list after it first, the rest of the list a paste lifted it from", () => {
|
|
71
|
+
const editor = createEditor([
|
|
72
|
+
{ children: [listItem("o")], type: "ol" },
|
|
73
|
+
listItem("b"),
|
|
74
|
+
{ children: [listItem("u")], type: "ul" },
|
|
75
|
+
]);
|
|
76
|
+
expect(editor.children.slice(0, 2)).toMatchObject([
|
|
77
|
+
{ children: [{ children: [{ children: [{ text: "o" }] }] }], type: "ol" },
|
|
78
|
+
{
|
|
79
|
+
children: [
|
|
80
|
+
{ children: [{ children: [{ text: "b" }] }] },
|
|
81
|
+
{ children: [{ children: [{ text: "u" }] }] },
|
|
82
|
+
],
|
|
83
|
+
type: "ul",
|
|
84
|
+
},
|
|
85
|
+
]);
|
|
86
|
+
});
|
|
87
|
+
// list-classic's own normalizer takes a task list for a list too: the item must stay in it, not loop
|
|
88
|
+
// between the two normalizers ("Could not completely normalize the editor").
|
|
89
|
+
it("leaves an item of a task list alone", () => {
|
|
90
|
+
const editor = createEditor([{ children: [listItem("a")], type: "taskList" }]);
|
|
91
|
+
expect(editor.children.at(0)).toMatchObject({
|
|
92
|
+
children: [{ children: [{ children: [{ text: "a" }] }], type: "li" }],
|
|
93
|
+
type: "taskList",
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
// list-classic's normalizer keeps only the text of the last li of such a group
|
|
97
|
+
it("keeps the text of an li directly in an li", () => {
|
|
98
|
+
const editor = createEditor([
|
|
99
|
+
{ children: [{ children: [listItem("a"), listItem("b")], type: "li" }], type: "ul" },
|
|
100
|
+
{
|
|
101
|
+
children: [{ children: [{ children: [{ text: "x" }], type: "lic" }, listItem("c")], type: "li" }],
|
|
102
|
+
type: "ul",
|
|
103
|
+
},
|
|
104
|
+
]);
|
|
105
|
+
expect(listItemsOutsideList(editor)).toEqual([]);
|
|
106
|
+
expect(platejs_1.NodeApi.string(editor)).toBe("abxc");
|
|
107
|
+
});
|
|
108
|
+
describe("keeps every item's text and order", () => {
|
|
109
|
+
/** Each root block as its type, a list as its type and its items' texts. */
|
|
110
|
+
const outline = (editor) => editor.children.map((block) => ["ul", "ol"].includes(block.type)
|
|
111
|
+
? [block.type, block.children.map((item) => platejs_1.NodeApi.string(item))]
|
|
112
|
+
: [block.type, platejs_1.NodeApi.string(block)]);
|
|
113
|
+
// list-classic removes the first item of the surrounding list when an li starts with a list
|
|
114
|
+
it("for an li directly in an li that is not the first item", () => {
|
|
115
|
+
const editor = createEditor([
|
|
116
|
+
{
|
|
117
|
+
children: [listItem("p"), { children: [listItem("a"), listItem("b")], type: "li" }],
|
|
118
|
+
type: "ul",
|
|
119
|
+
},
|
|
120
|
+
]);
|
|
121
|
+
expect(listItemsOutsideList(editor)).toEqual([]);
|
|
122
|
+
expect(outline(editor).at(0)).toEqual(["ul", ["p", "a", "b"]]);
|
|
123
|
+
});
|
|
124
|
+
it("for an li directly in an li followed by the item's own content", () => {
|
|
125
|
+
const editor = createEditor([
|
|
126
|
+
{
|
|
127
|
+
children: [
|
|
128
|
+
listItem("p"),
|
|
129
|
+
{ children: [listItem("a"), { children: [{ text: "x" }], type: "lic" }], type: "li" },
|
|
130
|
+
],
|
|
131
|
+
type: "ul",
|
|
132
|
+
},
|
|
133
|
+
]);
|
|
134
|
+
expect(listItemsOutsideList(editor)).toEqual([]);
|
|
135
|
+
expect(outline(editor).at(0)).toEqual(["ul", ["p", "a", "x"]]);
|
|
136
|
+
});
|
|
137
|
+
it("for an li in a paragraph, without a list inside the paragraph", () => {
|
|
138
|
+
const editor = createEditor([
|
|
139
|
+
{ children: [listItem("q")], type: "paragraph" },
|
|
140
|
+
]);
|
|
141
|
+
expect(listItemsOutsideList(editor)).toEqual([]);
|
|
142
|
+
expect(platejs_1.NodeApi.string(editor)).toBe("q");
|
|
143
|
+
expect(Array.from(platejs_1.NodeApi.nodes(editor)).some(([node, path]) => node.type === "ul" && path.length > 1)).toBe(false);
|
|
144
|
+
});
|
|
145
|
+
it("for a run of items after a list, which all join it", () => {
|
|
146
|
+
const editor = createEditor([
|
|
147
|
+
{ children: [listItem("a")], type: "ol" },
|
|
148
|
+
listItem("b"),
|
|
149
|
+
listItem("c"),
|
|
150
|
+
]);
|
|
151
|
+
expect(outline(editor).at(0)).toEqual(["ol", ["a", "b", "c"]]);
|
|
152
|
+
});
|
|
153
|
+
// The lifted items sit between the two halves of the pasted-into paragraph, so their list type is not
|
|
154
|
+
// in the document any more: they become one bulleted list.
|
|
155
|
+
it("for the items a paste lifts out of a numbered list", () => {
|
|
156
|
+
const editor = createEditor([paragraph("ab")]);
|
|
157
|
+
editor.tf.select({ offset: 1, path: [0, 0] });
|
|
158
|
+
editor.tf.insertFragment([
|
|
159
|
+
paragraph("x"),
|
|
160
|
+
{ children: [listItem("1"), listItem("2"), listItem("3")], type: "ol" },
|
|
161
|
+
]);
|
|
162
|
+
expect(listItemsOutsideList(editor)).toEqual([]);
|
|
163
|
+
expect(platejs_1.NodeApi.string(editor)).toBe("ax123b");
|
|
164
|
+
expect(outline(editor).filter(([type]) => type === "ul" || type === "ol")).toEqual([["ul", ["1", "2"]]]);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const platejs_1 = require("platejs");
|
|
4
|
+
const react_1 = require("platejs/react");
|
|
5
|
+
const create_all_plugins_with_ui_1 = require("../../create-all-plugins-with-ui");
|
|
6
|
+
const FILE = { extension: ".png", id: 1, name: "a.png", namespace: "wysiwyg", uuid: "u" };
|
|
7
|
+
/** The editor exactly as `WysiwygEditor` builds it from the full plugin set. */
|
|
8
|
+
function createEditor(value) {
|
|
9
|
+
return (0, react_1.createPlateEditor)({
|
|
10
|
+
plugins: (0, create_all_plugins_with_ui_1.createAllPluginsWithUi)({
|
|
11
|
+
image: { getImageUrl: (file) => file.name, uploadImage: async () => FILE },
|
|
12
|
+
}),
|
|
13
|
+
shouldNormalizeEditor: true,
|
|
14
|
+
value,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const content = (text) => ({ children: [{ text }], type: "lic" });
|
|
18
|
+
const listItem = (text) => ({ children: [content(text)], type: "li" });
|
|
19
|
+
const paragraph = (text) => ({ children: [{ text }], type: "paragraph" });
|
|
20
|
+
/** Each `li` in the document as the types of its children. */
|
|
21
|
+
const listItemShapes = (editor) => Array.from(platejs_1.NodeApi.nodes(editor))
|
|
22
|
+
.filter(([node]) => node.type === "li")
|
|
23
|
+
.map(([node]) => node.children.map((child) => child.type));
|
|
24
|
+
/** The point at `offset` in the text node holding `text`, wherever normalization has put it. */
|
|
25
|
+
function pointIn(editor, text, offset) {
|
|
26
|
+
var _a;
|
|
27
|
+
const [, path] = (_a = Array.from(platejs_1.NodeApi.texts(editor)).find(([node]) => node.text === text)) !== null && _a !== void 0 ? _a : [];
|
|
28
|
+
if (!path) {
|
|
29
|
+
throw new Error(`no text "${text}"`);
|
|
30
|
+
}
|
|
31
|
+
return { offset, path };
|
|
32
|
+
}
|
|
33
|
+
/** Each root block as its type, a list as its type and its items' texts. */
|
|
34
|
+
const outline = (editor) => editor.children.map((block) => ["ul", "ol"].includes(block.type)
|
|
35
|
+
? [block.type, block.children.map((item) => platejs_1.NodeApi.string(item))]
|
|
36
|
+
: [block.type, platejs_1.NodeApi.string(block)]);
|
|
37
|
+
describe("an li with more than one lic", () => {
|
|
38
|
+
// list-classic finds no single lic above the selection and leaves Enter to Slate, which deletes the range and
|
|
39
|
+
// splits the lic inside its li. Enter in the split-off lic then crashed in list-classic's insertListItem with
|
|
40
|
+
// "Invalid attempt to destructure non-iterable instance".
|
|
41
|
+
it("is not left behind by Enter with a selection from a paragraph to the start of an item", () => {
|
|
42
|
+
const editor = createEditor([paragraph("pq"), { children: [listItem("ab")], type: "ul" }]);
|
|
43
|
+
editor.tf.select({ anchor: { offset: 0, path: [0, 0] }, focus: { offset: 0, path: [1, 0, 0, 0] } });
|
|
44
|
+
editor.tf.insertBreak();
|
|
45
|
+
expect(listItemShapes(editor)).toEqual([["lic"], ["lic"]]);
|
|
46
|
+
editor.tf.select(editor.api.end([1]));
|
|
47
|
+
expect(() => editor.tf.insertBreak()).not.toThrow();
|
|
48
|
+
expect(outline(editor).at(1)).toEqual(["ul", ["", "ab", ""]]);
|
|
49
|
+
});
|
|
50
|
+
it("is split into one item per lic when a stored document has one", () => {
|
|
51
|
+
const editor = createEditor([
|
|
52
|
+
{ children: [{ children: [content("a"), content("b"), content("c")], type: "li" }], type: "ol" },
|
|
53
|
+
]);
|
|
54
|
+
expect(listItemShapes(editor)).toEqual([["lic"], ["lic"], ["lic"]]);
|
|
55
|
+
expect(outline(editor).at(0)).toEqual(["ol", ["a", "b", "c"]]);
|
|
56
|
+
});
|
|
57
|
+
it("keeps the sublist with the lic it follows", () => {
|
|
58
|
+
const editor = createEditor([
|
|
59
|
+
{
|
|
60
|
+
children: [
|
|
61
|
+
{
|
|
62
|
+
children: [content("a"), { children: [listItem("a1")], type: "ul" }, content("b")],
|
|
63
|
+
type: "li",
|
|
64
|
+
},
|
|
65
|
+
listItem("c"),
|
|
66
|
+
],
|
|
67
|
+
type: "ul",
|
|
68
|
+
},
|
|
69
|
+
]);
|
|
70
|
+
expect(listItemShapes(editor)).toEqual([["lic", "ul"], ["lic"], ["lic"], ["lic"]]);
|
|
71
|
+
expect(outline(editor).at(0)).toEqual(["ul", ["aa1", "b", "c"]]);
|
|
72
|
+
});
|
|
73
|
+
it("gives the split-off item an id of its own", () => {
|
|
74
|
+
var _a;
|
|
75
|
+
const editor = createEditor([
|
|
76
|
+
{ children: [{ children: [content("a"), content("b")], type: "li" }], type: "ul" },
|
|
77
|
+
]);
|
|
78
|
+
const ids = ((_a = editor.children.at(0)) === null || _a === void 0 ? void 0 : _a.children).map((item) => item.id);
|
|
79
|
+
expect(ids).toHaveLength(2);
|
|
80
|
+
expect(new Set(ids).size).toBe(2);
|
|
81
|
+
});
|
|
82
|
+
// Enter in any position of either lic, the second one crashed when not at its start
|
|
83
|
+
["ab", "cd"].forEach((text) => {
|
|
84
|
+
[0, 1, 2].forEach((offset) => {
|
|
85
|
+
it(`survives Enter at offset ${offset} of "${text}" in an li with two lic`, () => {
|
|
86
|
+
const editor = createEditor([
|
|
87
|
+
{ children: [{ children: [content("ab"), content("cd")], type: "li" }], type: "ul" },
|
|
88
|
+
]);
|
|
89
|
+
editor.tf.select(pointIn(editor, text, offset));
|
|
90
|
+
expect(() => editor.tf.insertBreak()).not.toThrow();
|
|
91
|
+
expect(platejs_1.NodeApi.string(editor)).toBe("abcd");
|
|
92
|
+
expect(listItemShapes(editor).every((shape) => shape.length === 1)).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
package/wysiwyg-editor.js
CHANGED
|
@@ -103,92 +103,104 @@ const Editor = props => {
|
|
|
103
103
|
t1 = $[5];
|
|
104
104
|
}
|
|
105
105
|
const editor = (0, react_1.usePlateEditor)(t0, t1);
|
|
106
|
+
const onChangeRef = (0, react_2.useRef)(props.onChange);
|
|
106
107
|
let t2;
|
|
107
|
-
if ($[6] !== props) {
|
|
108
|
-
t2 =
|
|
109
|
-
|
|
110
|
-
return props.onChange(value);
|
|
108
|
+
if ($[6] !== props.onChange) {
|
|
109
|
+
t2 = () => {
|
|
110
|
+
onChangeRef.current = props.onChange;
|
|
111
111
|
};
|
|
112
|
-
$[6] = props;
|
|
112
|
+
$[6] = props.onChange;
|
|
113
113
|
$[7] = t2;
|
|
114
114
|
}
|
|
115
115
|
else {
|
|
116
116
|
t2 = $[7];
|
|
117
117
|
}
|
|
118
|
+
(0, react_2.useLayoutEffect)(t2);
|
|
118
119
|
let t3;
|
|
119
|
-
if ($[8]
|
|
120
|
-
t3 =
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
$[
|
|
125
|
-
$[12] = t3;
|
|
120
|
+
if ($[8] === Symbol.for("react.memo_cache_sentinel")) {
|
|
121
|
+
t3 = t4 => {
|
|
122
|
+
const { value } = t4;
|
|
123
|
+
return onChangeRef.current(value);
|
|
124
|
+
};
|
|
125
|
+
$[8] = t3;
|
|
126
126
|
}
|
|
127
127
|
else {
|
|
128
|
-
t3 = $[
|
|
128
|
+
t3 = $[8];
|
|
129
129
|
}
|
|
130
|
+
const handleChange = t3;
|
|
130
131
|
let t4;
|
|
131
|
-
if ($[
|
|
132
|
-
t4 =
|
|
133
|
-
$[
|
|
134
|
-
$[
|
|
132
|
+
if ($[9] !== props.customPluginsToolbarButtons || $[10] !== props.id || $[11] !== props.toolbarLeftElement || $[12] !== props.toolbarRightElement) {
|
|
133
|
+
t4 = react_2.default.createElement(toolbar_1.Toolbar, { customPluginsButtons: props.customPluginsToolbarButtons, leftElement: props.toolbarLeftElement, plateId: props.id, rightElement: props.toolbarRightElement });
|
|
134
|
+
$[9] = props.customPluginsToolbarButtons;
|
|
135
|
+
$[10] = props.id;
|
|
136
|
+
$[11] = props.toolbarLeftElement;
|
|
137
|
+
$[12] = props.toolbarRightElement;
|
|
138
|
+
$[13] = t4;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
t4 = $[13];
|
|
142
|
+
}
|
|
143
|
+
let t5;
|
|
144
|
+
if ($[14] !== t) {
|
|
145
|
+
t5 = t("uxf-wysiwyg:common.placeholder");
|
|
146
|
+
$[14] = t;
|
|
147
|
+
$[15] = t5;
|
|
135
148
|
}
|
|
136
149
|
else {
|
|
137
|
-
|
|
150
|
+
t5 = $[15];
|
|
138
151
|
}
|
|
139
|
-
const
|
|
140
|
-
let
|
|
141
|
-
if ($[
|
|
142
|
-
|
|
143
|
-
placeholder:
|
|
152
|
+
const t6 = `${CLASS_NAME}__editable ${(_b = (_a = props.editableProps) === null || _a === void 0 ? void 0 : _a.className) !== null && _b !== void 0 ? _b : ""}`;
|
|
153
|
+
let t7;
|
|
154
|
+
if ($[16] !== props.editableProps || $[17] !== t5 || $[18] !== t6) {
|
|
155
|
+
t7 = react_2.default.createElement(react_1.PlateContent, { autoFocus: false,
|
|
156
|
+
placeholder: t5,
|
|
144
157
|
readOnly: false,
|
|
145
158
|
spellCheck: false,
|
|
146
159
|
...props.editableProps,
|
|
147
|
-
className:
|
|
148
|
-
$[
|
|
149
|
-
$[16] = t4;
|
|
160
|
+
className: t6 });
|
|
161
|
+
$[16] = props.editableProps;
|
|
150
162
|
$[17] = t5;
|
|
151
163
|
$[18] = t6;
|
|
164
|
+
$[19] = t7;
|
|
152
165
|
}
|
|
153
166
|
else {
|
|
154
|
-
|
|
167
|
+
t7 = $[19];
|
|
155
168
|
}
|
|
156
|
-
let
|
|
157
|
-
if ($[
|
|
158
|
-
|
|
159
|
-
$[
|
|
160
|
-
$[
|
|
169
|
+
let t8;
|
|
170
|
+
if ($[20] !== ref) {
|
|
171
|
+
t8 = ref && react_2.default.createElement(EditorRefHandler, { ref: ref });
|
|
172
|
+
$[20] = ref;
|
|
173
|
+
$[21] = t8;
|
|
161
174
|
}
|
|
162
175
|
else {
|
|
163
|
-
|
|
176
|
+
t8 = $[21];
|
|
164
177
|
}
|
|
165
|
-
let
|
|
166
|
-
if ($[
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
$[21] = t6;
|
|
178
|
+
let t9;
|
|
179
|
+
if ($[22] !== t7 || $[23] !== t8) {
|
|
180
|
+
t9 = react_2.default.createElement("div", { className: `${CLASS_NAME}__content` },
|
|
181
|
+
t7,
|
|
182
|
+
t8);
|
|
171
183
|
$[22] = t7;
|
|
172
184
|
$[23] = t8;
|
|
185
|
+
$[24] = t9;
|
|
173
186
|
}
|
|
174
187
|
else {
|
|
175
|
-
|
|
188
|
+
t9 = $[24];
|
|
176
189
|
}
|
|
177
|
-
let
|
|
178
|
-
if ($[
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
$[
|
|
183
|
-
$[
|
|
184
|
-
$[
|
|
185
|
-
$[
|
|
186
|
-
$[28] = t9;
|
|
190
|
+
let t10;
|
|
191
|
+
if ($[25] !== editor || $[26] !== t4 || $[27] !== t9) {
|
|
192
|
+
t10 = react_2.default.createElement(react_1.Plate, { editor: editor, onChange: handleChange },
|
|
193
|
+
t4,
|
|
194
|
+
t9);
|
|
195
|
+
$[25] = editor;
|
|
196
|
+
$[26] = t4;
|
|
197
|
+
$[27] = t9;
|
|
198
|
+
$[28] = t10;
|
|
187
199
|
}
|
|
188
200
|
else {
|
|
189
|
-
|
|
201
|
+
t10 = $[28];
|
|
190
202
|
}
|
|
191
|
-
return
|
|
203
|
+
return t10;
|
|
192
204
|
};
|
|
193
205
|
const WysiwygEditor = props => {
|
|
194
206
|
var _a, _b;
|
package/wysiwyg-editor.test.js
CHANGED
|
@@ -35,7 +35,8 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
const react_1 = require("@testing-library/react");
|
|
37
37
|
const context_1 = require("@uxf/ui/context");
|
|
38
|
-
const react_2 =
|
|
38
|
+
const react_2 = require("platejs/react");
|
|
39
|
+
const react_3 = __importStar(require("react"));
|
|
39
40
|
const create_all_plugins_with_ui_1 = require("./create-all-plugins-with-ui");
|
|
40
41
|
const create_plugins_with_ui_1 = require("./create-plugins-with-ui");
|
|
41
42
|
const wysiwyg_editor_1 = require("./wysiwyg-editor");
|
|
@@ -64,8 +65,8 @@ async function settle() {
|
|
|
64
65
|
await (0, react_1.act)(async () => undefined);
|
|
65
66
|
}
|
|
66
67
|
async function renderEditor(editorRef) {
|
|
67
|
-
const result = (0, react_1.render)(
|
|
68
|
-
|
|
68
|
+
const result = (0, react_1.render)(react_3.default.createElement(context_1.UiContextProvider, { value: UI_CONTEXT },
|
|
69
|
+
react_3.default.createElement(wysiwyg_editor_1.WysiwygEditor, { id: "test-editor", initialValue: INITIAL_VALUE, onChange: () => undefined, plugins: (0, create_plugins_with_ui_1.createPluginsWithUi)([(0, create_all_plugins_with_ui_1.createBoldPluginWithUi)()]), ref: editorRef })));
|
|
69
70
|
await settle();
|
|
70
71
|
return result;
|
|
71
72
|
}
|
|
@@ -107,8 +108,8 @@ const EVERY_ELEMENT = [
|
|
|
107
108
|
{ buttonText: "klik", buttonUrl: "https://uxf.cz", children: [{ text: "" }], type: "button" },
|
|
108
109
|
];
|
|
109
110
|
async function renderEditorWithAllPlugins() {
|
|
110
|
-
const result = (0, react_1.render)(
|
|
111
|
-
|
|
111
|
+
const result = (0, react_1.render)(react_3.default.createElement(context_1.UiContextProvider, { value: UI_CONTEXT },
|
|
112
|
+
react_3.default.createElement(wysiwyg_editor_1.WysiwygEditor, { id: "all-plugins-editor", initialValue: EVERY_ELEMENT, onChange: () => undefined, plugins: (0, create_all_plugins_with_ui_1.createAllPluginsWithUi)({
|
|
112
113
|
image: { getImageUrl: (file) => file.name, uploadImage: async () => FILE },
|
|
113
114
|
}) })));
|
|
114
115
|
await settle();
|
|
@@ -136,7 +137,7 @@ describe("WysiwygEditor", () => {
|
|
|
136
137
|
});
|
|
137
138
|
it("exposes focus() through its ref", async () => {
|
|
138
139
|
var _a;
|
|
139
|
-
const editorRef = (0,
|
|
140
|
+
const editorRef = (0, react_3.createRef)();
|
|
140
141
|
await renderEditor(editorRef);
|
|
141
142
|
expect(typeof ((_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.focus)).toBe("function");
|
|
142
143
|
});
|
|
@@ -179,3 +180,50 @@ describe("WysiwygEditor", () => {
|
|
|
179
180
|
expect(container.querySelector(".uxf-wysiwyg__element-paragraph")).not.toBeNull();
|
|
180
181
|
});
|
|
181
182
|
});
|
|
183
|
+
describe("WysiwygEditor onChange", () => {
|
|
184
|
+
const PLUGINS = (0, create_plugins_with_ui_1.createPluginsWithUi)([(0, create_all_plugins_with_ui_1.createBoldPluginWithUi)()]);
|
|
185
|
+
/** Rendered inside `<Plate>` through the toolbar slot, it records what Plate's store holds as `onChange`. */
|
|
186
|
+
function StoreProbe(props) {
|
|
187
|
+
props.probe.onChanges.add((0, react_2.usePlateValue)("onChange"));
|
|
188
|
+
props.probe.editor = (0, react_2.useEditorRef)();
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
function renderWithProbe(probe, onChange) {
|
|
192
|
+
return (react_3.default.createElement(context_1.UiContextProvider, { value: UI_CONTEXT },
|
|
193
|
+
react_3.default.createElement(wysiwyg_editor_1.WysiwygEditor, { customPluginsToolbarButtons: react_3.default.createElement(StoreProbe, { probe: probe }), id: "on-change-editor", initialValue: INITIAL_VALUE, onChange: onChange, plugins: PLUGINS })));
|
|
194
|
+
}
|
|
195
|
+
// Every new function written to Plate's store re-renders its subscribers. Keyed on the whole props object,
|
|
196
|
+
// that happened on every parent render, and fed a "Maximum update depth exceeded" loop in a consumer.
|
|
197
|
+
it("keeps the function in Plate's store when the parent re-renders", async () => {
|
|
198
|
+
const probe = { onChanges: new Set() };
|
|
199
|
+
const onChange = jest.fn();
|
|
200
|
+
const { rerender } = (0, react_1.render)(renderWithProbe(probe, onChange));
|
|
201
|
+
await settle();
|
|
202
|
+
rerender(renderWithProbe(probe, onChange));
|
|
203
|
+
rerender(renderWithProbe(probe, onChange));
|
|
204
|
+
await settle();
|
|
205
|
+
expect(probe.onChanges.size).toBe(1);
|
|
206
|
+
});
|
|
207
|
+
// Consumers pass inline callbacks too (`@uxf/page-builder-web`), so a new callback must neither reach the
|
|
208
|
+
// store nor be missed: the editor calls whichever one the last render passed.
|
|
209
|
+
it("calls the latest callback without replacing the function in the store", async () => {
|
|
210
|
+
const probe = { onChanges: new Set() };
|
|
211
|
+
const first = jest.fn();
|
|
212
|
+
const latest = jest.fn();
|
|
213
|
+
const { rerender } = (0, react_1.render)(renderWithProbe(probe, first));
|
|
214
|
+
await settle();
|
|
215
|
+
first.mockClear();
|
|
216
|
+
rerender(renderWithProbe(probe, latest));
|
|
217
|
+
await settle();
|
|
218
|
+
(0, react_1.act)(() => {
|
|
219
|
+
var _a;
|
|
220
|
+
(_a = probe.editor) === null || _a === void 0 ? void 0 : _a.tf.insertText("!", { at: { offset: 5, path: [0, 0] } });
|
|
221
|
+
});
|
|
222
|
+
await settle();
|
|
223
|
+
expect(probe.onChanges.size).toBe(1);
|
|
224
|
+
expect(first).not.toHaveBeenCalled();
|
|
225
|
+
expect(latest).toHaveBeenLastCalledWith([
|
|
226
|
+
expect.objectContaining({ children: [{ text: "hello!" }], type: "paragraph" }),
|
|
227
|
+
]);
|
|
228
|
+
});
|
|
229
|
+
});
|