Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: add tests for backspace handling

Abdullah Attaabf1f5c61c89
diff --git a/packages/editor/src/extensions/key-map/key-map.ts b/packages/editor/src/extensions/key-map/key-map.ts
index 334afa83f..a7382ced7 100644
--- a/packages/editor/src/extensions/key-map/key-map.ts
+++ b/packages/editor/src/extensions/key-map/key-map.ts
@@ -48,7 +48,7 @@ export const KeyMap = Extension.create({
* This function corrects this behavior and moves the cursor to the
* end of the list.
*/
-function joinUpWithLastListItem(editor: Editor) {
+export function joinUpWithLastListItem(editor: Editor) {
const { selection, doc } = editor.state;
const { $from, empty } = selection;
diff --git a/packages/editor/src/extensions/key-map/tests/key-map.test.ts b/packages/editor/src/extensions/key-map/tests/key-map.test.ts
new file mode 100644
index 000000000..7f8021754
--- /dev/null
+++ b/packages/editor/src/extensions/key-map/tests/key-map.test.ts
@@ -0,0 +1,106 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { createEditor, h } from "@/test-utils";
+import tap from "tap";
+import expect from "expect";
+import BulletList from "../../bullet-list";
+import { ListItem } from "../../list-item";
+import { joinUpWithLastListItem } from "../key-map";
+
+tap.test(`join up with last list item in a flat list`, async (t) => {
+ const { editor } = createEditor({
+ initialContent: h("div", [
+ h("ul", [h("li", ["Hello"]), h("li", ["World"])]),
+ h("p")
+ ]).innerHTML,
+ extensions: {
+ bulletList: BulletList,
+ listItem: ListItem
+ }
+ });
+
+ editor.commands.setTextSelection(editor.state.doc.nodeSize - 2);
+ joinUpWithLastListItem(editor);
+
+ expect(editor.state.selection.$from.parent.textContent).toBe("World");
+ t.matchSnapshot(editor.state.doc.content.toJSON());
+});
+
+tap.test(`join up with last list item in a nested list`, async (t) => {
+ const { editor } = createEditor({
+ initialContent: h("div", [
+ h("ul", [
+ h("li", ["Hello"]),
+ h("li", [
+ "World",
+ h("ul", [
+ h("li", ["Hello"]),
+ h("li", ["World", h("ul", [h("li", ["Hello"])])]),
+ h("li", ["A very nested world"])
+ ])
+ ])
+ ]),
+ h("p")
+ ]).innerHTML,
+ extensions: {
+ bulletList: BulletList,
+ listItem: ListItem
+ }
+ });
+
+ editor.commands.setTextSelection(editor.state.doc.nodeSize - 2);
+ joinUpWithLastListItem(editor);
+
+ expect(editor.state.selection.$from.parent.textContent).toBe(
+ "A very nested world"
+ );
+ t.matchSnapshot(editor.state.doc.content.toJSON());
+});
+
+tap.test(`join up with last list item in a nested list`, async (t) => {
+ const { editor } = createEditor({
+ initialContent: h("div", [
+ h("ul", [
+ h("li", ["Hello"]),
+ h("li", [
+ "World",
+ h("ul", [
+ h("li", ["Hello"]),
+ h("li", ["World", h("ul", [h("li", ["Hello"])])]),
+ h("li", ["A very nested world"])
+ ])
+ ])
+ ]),
+ h("p")
+ ]).innerHTML,
+ extensions: {
+ bulletList: BulletList,
+ listItem: ListItem
+ }
+ });
+
+ editor.commands.setTextSelection(editor.state.doc.nodeSize - 2);
+ joinUpWithLastListItem(editor);
+
+ expect(editor.state.selection.$from.parent.textContent).toBe(
+ "A very nested world"
+ );
+ t.matchSnapshot(editor.state.doc.content.toJSON());
+});
diff --git a/packages/editor/src/extensions/task-list/tests/task-list.test.ts b/packages/editor/src/extensions/task-list/tests/task-list.test.ts
index 1b3de30e2..ddc19207b 100644
--- a/packages/editor/src/extensions/task-list/tests/task-list.test.ts
+++ b/packages/editor/src/extensions/task-list/tests/task-list.test.ts
@@ -17,51 +17,61 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import "@/tests.setup";
+import { createEditor, h, ul, li } from "@/test-utils";
import tap from "tap";
import expect from "expect";
-import { Editor, AnyExtension, Extensions } from "@tiptap/core";
import { TaskListNode } from "../index";
import { TaskItemNode } from "../../task-item";
-import StarterKit from "@tiptap/starter-kit";
-import { builders, doc, NodeBuilder, p, eq } from "prosemirror-test-builder";
-import { Node, Schema } from "@tiptap/pm/model";
+import { p, eq } from "prosemirror-test-builder";
import { countCheckedItems, deleteCheckedItems, sortList } from "../utils";
-import { EditorState } from "@tiptap/pm/state";
-type Builder<TNodes extends string> = {
- scheme: Schema;
-} & Record<TNodes, NodeBuilder>;
+function taskList(...children: HTMLLIElement[]) {
+ return ul(children, { class: "checklist" });
+}
-type EditorOptions<TNodes extends string> = {
- extensions: Record<TNodes, AnyExtension>;
- initialDoc?: (builder: Builder<TNodes>) => Node;
-};
+function taskItem(
+ text: string,
+ attr: { checked?: boolean } = {},
+ subList?: HTMLUListElement
+) {
+ const children: HTMLElement[] = [h("p", [text])];
+ if (subList) children.push(subList);
-function createEditor<TNodes extends string>(options: EditorOptions<TNodes>) {
- const { extensions, initialDoc } = options;
- const editor = new Editor({
- extensions: [StarterKit, ...(Object.values(extensions) as Extensions)]
+ return li(children, {
+ class: "checklist--item " + (attr.checked ? "checked" : "")
});
-
- const builder = builders(editor.schema) as unknown as Builder<TNodes>;
-
- if (initialDoc) {
- const doc = initialDoc(builder);
- editor.view.updateState(
- EditorState.create({
- schema: editor.view.state.schema,
- doc,
- plugins: editor.state.plugins
- })
- );
-
- return { editor, builder, initialDoc: doc };
- }
-
- return { editor, builder, initialDoc: editor.state.doc };
}
+const NESTED_TASK_LIST = taskList(
+ taskItem("Task item 1", { checked: true }),
+ taskItem("Task item 2"),
+ taskItem(
+ "Task item 3",
+ { checked: false },
+ taskList(
+ taskItem("Task item 4", { checked: true }),
+ taskItem("Task item 5"),
+ taskItem(
+ "Task item 6",
+ { checked: false },
+ taskList(
+ taskItem("Task item 7", { checked: true }),
+ taskItem("Task item 8", { checked: true }),
+ taskItem(
+ "Task item 9",
+ { checked: false },
+ taskList(
+ taskItem("Task item 10", { checked: true }),
+ taskItem("Task item 11", { checked: true }),
+ taskItem("Task item 12")
+ )
+ )
+ )
+ )
+ )
+ )
+).outerHTML;
+
tap.test(`count items in a task list`, async () => {
const {
builder: { taskItem, taskList }
@@ -89,54 +99,24 @@ tap.test(`count items in a task list`, async () => {
tap.test(`delete checked items in a task list`, async (t) => {
const { editor } = createEditor({
- initialDoc: ({ taskItem, taskList }) =>
- doc(
- taskList(
- taskItem({ checked: true }, p("Task item 1")),
- taskItem({ checked: false }, p("Task item 2"))
- )
- ),
+ initialContent: taskList(
+ taskItem("Task item 1", { checked: true }),
+ taskItem("Task item 2")
+ ).outerHTML,
extensions: {
taskItem: TaskItemNode,
taskList: TaskListNode
}
});
- let { tr } = editor.state;
- tr = deleteCheckedItems(tr, 0) || tr;
- editor.view.dispatch(tr);
+ editor.commands.command(({ tr }) => !!deleteCheckedItems(tr, 0));
t.matchSnapshot(editor.state.doc.content.toJSON());
});
tap.test(`delete checked items in a nested task list`, async (t) => {
const { editor } = createEditor({
- initialDoc: ({ taskItem, taskList }) =>
- doc(
- taskList(
- taskItem({ checked: true }, p("Task item 1")),
- taskItem({ checked: false }, p("Task item 2")),
- taskItem(
- { checked: false },
- p("Task item 3"),
- taskList(
- taskItem({ checked: true }, p("Task item 4")),
- taskItem({ checked: false }, p("Task item 5")),
- taskItem({ checked: false }, p("Task item 6")),
- taskItem(
- { checked: true },
- p("Task item 7"),
- taskList(
- taskItem({ checked: true }, p("Task item 8")),
- taskItem({ checked: true }, p("Task item 9")),
- taskItem({ checked: false }, p("Task item 10")),
- taskItem({ checked: true }, p("Task item 11"))
- )
- )
- )
- )
- )
- ),
+ initialContent: NESTED_TASK_LIST,
extensions: {
taskItem: TaskItemNode,
taskList: TaskListNode
@@ -153,66 +133,36 @@ tap.test(`delete checked items in a nested task list`, async (t) => {
tap.test(
`delete checked items in a task list with no checked items should do nothing`,
async () => {
- const { editor, initialDoc } = createEditor({
- initialDoc: ({ taskItem, taskList }) =>
- doc(
+ const { editor } = createEditor({
+ initialContent: taskList(
+ taskItem("Task item 1", { checked: false }),
+ taskItem("Task item 2"),
+ taskItem(
+ "Task item 3",
+ { checked: false },
taskList(
- taskItem({ checked: false }, p("Task item 2")),
- taskItem(
- { checked: false },
- p("Task item 3"),
- taskList(
- taskItem({ checked: false }, p("Task item 5")),
- taskItem({ checked: false }, p("Task item 6")),
- taskItem(
- { checked: false },
- p("Task item 7"),
- taskList(taskItem({ checked: false }, p("Task item 10")))
- )
- )
- )
+ taskItem("Task item 4", { checked: false }),
+ taskItem("Task item 5"),
+ taskItem("Task item 6", { checked: false })
)
- ),
+ )
+ ).outerHTML,
extensions: {
taskItem: TaskItemNode,
taskList: TaskListNode
}
});
+ const beforeDoc = editor.state.doc.copy(editor.state.doc.content);
editor.commands.command(({ tr }) => !!deleteCheckedItems(tr, 0));
- expect(eq(editor.state.doc, initialDoc)).toBe(true);
+ expect(eq(editor.state.doc, beforeDoc)).toBe(true);
}
);
tap.test(`sort checked items to the bottom of the task list`, async (t) => {
const { editor } = createEditor({
- initialDoc: ({ taskItem, taskList }) =>
- doc(
- taskList(
- taskItem({ checked: true }, p("Task item 1")),
- taskItem({ checked: false }, p("Task item 2")),
- taskItem(
- { checked: false },
- p("Task item 3"),
- taskList(
- taskItem({ checked: true }, p("Task item 4")),
- taskItem({ checked: true }, p("Task item 5")),
- taskItem({ checked: false }, p("Task item 6")),
- taskItem(
- { checked: false },
- p("Task item 7"),
- taskList(
- taskItem({ checked: true }, p("Task item 8")),
- taskItem({ checked: true }, p("Task item 9")),
- taskItem({ checked: false }, p("Task item 10")),
- taskItem({ checked: true }, p("Task item 11"))
- )
- )
- )
- )
- )
- ),
+ initialContent: NESTED_TASK_LIST,
extensions: {
taskItem: TaskItemNode,
taskList: TaskListNode
@@ -227,21 +177,18 @@ tap.test(`sort checked items to the bottom of the task list`, async (t) => {
tap.test(
`sorting a task list with no checked items should do nothing`,
async () => {
- const { editor, initialDoc } = createEditor({
- initialDoc: ({ taskItem, taskList }) =>
- doc(
- taskList(
- taskItem({ checked: false }, p("Task item 1")),
- taskItem({ checked: false }, p("Task item 2"))
- )
- ),
+ const { editor } = createEditor({
+ initialContent: taskList(taskItem("Task item 1"), taskItem("Task item 2"))
+ .outerHTML,
extensions: {
taskItem: TaskItemNode,
taskList: TaskListNode
}
});
+ const beforeDoc = editor.state.doc.copy(editor.state.doc.content);
editor.commands.command(({ tr }) => !!sortList(tr, 0));
- expect(eq(editor.state.doc, initialDoc)).toBe(true);
+
+ expect(eq(editor.state.doc, beforeDoc)).toBe(true);
}
);
diff --git a/packages/editor/tap-snapshots/src/extensions/key-map/tests/key-map.test.ts.test.cjs b/packages/editor/tap-snapshots/src/extensions/key-map/tests/key-map.test.ts.test.cjs
new file mode 100644
index 000000000..bdaa1e976
--- /dev/null
+++ b/packages/editor/tap-snapshots/src/extensions/key-map/tests/key-map.test.ts.test.cjs
@@ -0,0 +1,273 @@
+/* IMPORTANT
+ * This snapshot file is auto-generated, but designed for humans.
+ * It should be checked into source control and tracked carefully.
+ * Re-generate by setting TAP_SNAPSHOT=1 and running tests.
+ * Make sure to inspect the output below. Do not ignore changes!
+ */
+'use strict'
+exports[`src/extensions/key-map/tests/key-map.test.ts TAP join up with last list item in a flat list > must match snapshot 1`] = `
+Array [
+ Object {
+ "attrs": Null Object {
+ "listType": "",
+ },
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "Hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "World",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ ],
+ "type": "bulletList",
+ },
+]
+`
+
+exports[`src/extensions/key-map/tests/key-map.test.ts TAP join up with last list item in a nested list > must match snapshot 1`] = `
+Array [
+ Object {
+ "attrs": Null Object {
+ "listType": "",
+ },
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "Hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "World",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "attrs": Null Object {
+ "listType": "",
+ },
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "Hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "World",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "attrs": Null Object {
+ "listType": "",
+ },
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "Hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ ],
+ "type": "bulletList",
+ },
+ ],
+ "type": "listItem",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "A very nested world",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ ],
+ "type": "bulletList",
+ },
+ ],
+ "type": "listItem",
+ },
+ ],
+ "type": "bulletList",
+ },
+]
+`
+
+exports[`src/extensions/key-map/tests/key-map.test.ts TAP join up with last list item in a nested list > must match snapshot 2`] = `
+Array [
+ Object {
+ "attrs": Null Object {
+ "listType": "",
+ },
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "Hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "World",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "attrs": Null Object {
+ "listType": "",
+ },
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "Hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "World",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "attrs": Null Object {
+ "listType": "",
+ },
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "Hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ ],
+ "type": "bulletList",
+ },
+ ],
+ "type": "listItem",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "content": Array [
+ Object {
+ "text": "A very nested world",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "listItem",
+ },
+ ],
+ "type": "bulletList",
+ },
+ ],
+ "type": "listItem",
+ },
+ ],
+ "type": "bulletList",
+ },
+]
+`
diff --git a/packages/editor/tap-snapshots/src/extensions/task-list/tests/task-list.test.ts.test.cjs b/packages/editor/tap-snapshots/src/extensions/task-list/tests/task-list.test.ts.test.cjs
index 9c7cde23c..823f2eb67 100644
--- a/packages/editor/tap-snapshots/src/extensions/task-list/tests/task-list.test.ts.test.cjs
+++ b/packages/editor/tap-snapshots/src/extensions/task-list/tests/task-list.test.ts.test.cjs
@@ -44,46 +44,85 @@ Array [
"type": "paragraph",
},
Object {
- "attrs": Null Object {
- "title": null,
- },
"content": Array [
Object {
- "attrs": Null Object {
- "checked": false,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 5",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
+ "text": "Task item 4",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 5",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 6",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 7",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 8",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 9",
+ "type": "text",
},
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 10",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 11",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
Object {
- "attrs": Null Object {
- "checked": false,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 6",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
+ "text": "Task item 12",
+ "type": "text",
},
],
- "type": "taskList",
+ "type": "paragraph",
},
],
"type": "taskItem",
@@ -163,156 +202,85 @@ Array [
"type": "paragraph",
},
Object {
- "attrs": Null Object {
- "title": null,
- },
"content": Array [
Object {
- "attrs": Null Object {
- "checked": true,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 4",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
+ "text": "Task item 4",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 5",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 6",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 7",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 8",
+ "type": "text",
},
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
Object {
- "attrs": Null Object {
- "checked": true,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 5",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
+ "text": "Task item 9",
+ "type": "text",
},
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
Object {
- "attrs": Null Object {
- "checked": false,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 6",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
+ "text": "Task item 10",
+ "type": "text",
},
+ ],
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
Object {
- "attrs": Null Object {
- "checked": false,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 7",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- Object {
- "attrs": Null Object {
- "title": null,
- },
- "content": Array [
- Object {
- "attrs": Null Object {
- "checked": true,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 8",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
- },
- Object {
- "attrs": Null Object {
- "checked": true,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 9",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
- },
- Object {
- "attrs": Null Object {
- "checked": false,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 10",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
- },
- Object {
- "attrs": Null Object {
- "checked": true,
- },
- "content": Array [
- Object {
- "content": Array [
- Object {
- "text": "Task item 11",
- "type": "text",
- },
- ],
- "type": "paragraph",
- },
- ],
- "type": "taskItem",
- },
- ],
- "type": "taskList",
- },
- ],
- "type": "taskItem",
+ "text": "Task item 11",
+ "type": "text",
},
],
- "type": "taskList",
+ "type": "paragraph",
+ },
+ Object {
+ "content": Array [
+ Object {
+ "text": "Task item 12",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
},
],
"type": "taskItem",
diff --git a/packages/editor/test-utils/index.ts b/packages/editor/test-utils/index.ts
new file mode 100644
index 000000000..01a326891
--- /dev/null
+++ b/packages/editor/test-utils/index.ts
@@ -0,0 +1,95 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import "@/tests.setup";
+import { Editor, AnyExtension, Extensions } from "@tiptap/core";
+import StarterKit from "@tiptap/starter-kit";
+import { builders, NodeBuilder } from "prosemirror-test-builder";
+import { Schema } from "@tiptap/pm/model";
+
+type Builder<TNodes extends string> = {
+ scheme: Schema;
+} & Record<TNodes, NodeBuilder>;
+
+type EditorOptions<TNodes extends string> = {
+ extensions: Record<TNodes, AnyExtension>;
+ initialContent?: string;
+};
+
+export function createEditor<TNodes extends string>(
+ options: EditorOptions<TNodes>
+) {
+ const { extensions, initialContent } = options;
+ const editor = new Editor({
+ extensions: [
+ StarterKit.configure({
+ bulletList: false,
+ listItem: false,
+ orderedList: false
+ }),
+ ...(Object.values(extensions) as Extensions)
+ ]
+ });
+
+ const builder = builders(editor.schema) as unknown as Builder<TNodes>;
+
+ if (initialContent) {
+ editor.commands.setContent(initialContent, true, {
+ preserveWhitespace: "full"
+ });
+
+ return { editor, builder };
+ }
+
+ return { editor, builder };
+}
+
+export function h<K extends keyof HTMLElementTagNameMap>(
+ tag: K,
+ children: (HTMLElement | string)[] = [],
+ attr: Record<string, string | undefined> = {}
+): HTMLElementTagNameMap[K] {
+ const element = document.createElement(tag);
+ element.append(
+ ...children.map((v) =>
+ typeof v === "string" ? document.createTextNode(v) : v
+ )
+ );
+ for (const key in attr) {
+ const value = attr[key];
+ if (value) element.setAttribute(key, value);
+ }
+ return element;
+}
+
+function elem<K extends keyof HTMLElementTagNameMap>(tag: K) {
+ return function (
+ children: (HTMLElement | string)[] = [],
+ attr: Record<string, string | undefined> = {}
+ ): HTMLElementTagNameMap[K] {
+ return h(tag, children, attr);
+ };
+}
+
+export const ul = elem("ul");
+export const li = elem("li");
+
+export function text(text: string) {
+ return document.createTextNode(text);
+}
diff --git a/packages/editor/tsconfig.json b/packages/editor/tsconfig.json
index aaa528f38..b736cc96a 100644
--- a/packages/editor/tsconfig.json
+++ b/packages/editor/tsconfig.json
@@ -3,7 +3,11 @@
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"jsx": "react-jsx",
- "outDir": "./dist/"
+ "outDir": "./dist/",
+ "baseUrl": "./",
+ "paths": {
+ "@/*": ["*"]
+ }
},
"include": ["src/"]
}