@open-elements/ui 0.8.0 → 0.10.0
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/README.md +153 -18
- package/dist/components/capability-status.d.ts +10 -0
- package/dist/components/capability-status.d.ts.map +1 -0
- package/dist/components/capability-status.js +12 -0
- package/dist/components/capability-status.js.map +1 -0
- package/dist/components/markdown-editor.d.ts +1 -1
- package/dist/components/markdown-editor.d.ts.map +1 -1
- package/dist/components/markdown-editor.js +131 -44
- package/dist/components/markdown-editor.js.map +1 -1
- package/dist/components/markdown-view.d.ts +1 -1
- package/dist/components/markdown-view.d.ts.map +1 -1
- package/dist/components/markdown-view.js +110 -27
- package/dist/components/markdown-view.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/markdown-extensions.d.ts +48 -0
- package/dist/lib/markdown-extensions.d.ts.map +1 -0
- package/dist/lib/markdown-extensions.js +73 -0
- package/dist/lib/markdown-extensions.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/index.d.ts +17 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +41 -24
- package/src/components/__tests__/capability-status.test.tsx +82 -0
- package/src/components/__tests__/markdown-editor.test.tsx +226 -22
- package/src/components/__tests__/markdown-view.test.tsx +274 -21
- package/src/components/capability-status.tsx +63 -0
- package/src/components/markdown-editor.tsx +185 -79
- package/src/components/markdown-view.tsx +133 -28
- package/src/index.ts +3 -0
- package/src/lib/__tests__/markdown-extensions.test.ts +290 -0
- package/src/lib/markdown-extensions.ts +107 -0
- package/src/test/setup.ts +29 -0
- package/src/types/__tests__/markdown-toolbar-action.test.ts +34 -0
- package/src/types/index.ts +32 -0
|
@@ -1,34 +1,111 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { useEffect } from "react";
|
|
4
|
-
import { useEditor, EditorContent } from "@tiptap/react";
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { useEditor, EditorContent, type Editor } from "@tiptap/react";
|
|
5
|
+
import type { NodeWithPos } from "@tiptap/core";
|
|
6
|
+
import { cn } from "../lib/utils.ts";
|
|
7
|
+
import { createMarkdownExtensions } from "../lib/markdown-extensions.ts";
|
|
8
8
|
import type { MarkdownViewProps } from "../types/index.ts";
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
/** The ProseMirror document node type, without a direct `@tiptap/pm` dependency. */
|
|
11
|
+
type ProseMirrorNode = NodeWithPos["node"];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Resolve the document position of the task item whose checkbox was just
|
|
15
|
+
* toggled to `checked`.
|
|
16
|
+
*
|
|
17
|
+
* The `node` handed to `onReadOnlyChecked` cannot be trusted for this: the
|
|
18
|
+
* extension-list node view is reused across updates, so its closure keeps the
|
|
19
|
+
* node from creation and goes stale after the first toggle. Instead we locate
|
|
20
|
+
* the clicked item by DOM: at the moment of the click exactly one `<li>` has a
|
|
21
|
+
* live checkbox that disagrees with its `data-checked` attribute (the document
|
|
22
|
+
* has not been updated yet). DOM order matches document order, so its index
|
|
23
|
+
* among the task-item `<li>`s maps directly to the nth `taskItem` node.
|
|
24
|
+
*/
|
|
25
|
+
function positionOfToggled(editor: Editor, checked: boolean): number {
|
|
26
|
+
const items = Array.from(editor.view.dom.querySelectorAll<HTMLElement>("li[data-checked]"));
|
|
27
|
+
const clickedIndex = items.findIndex((li) => {
|
|
28
|
+
const input = li.querySelector<HTMLInputElement>("input[type=checkbox]");
|
|
29
|
+
return input != null && input.checked === checked && li.dataset.checked !== String(checked);
|
|
30
|
+
});
|
|
31
|
+
if (clickedIndex === -1) return -1;
|
|
32
|
+
|
|
33
|
+
let seen = 0;
|
|
34
|
+
let position = -1;
|
|
35
|
+
editor.state.doc.descendants((node, pos) => {
|
|
36
|
+
if (position !== -1) return false; // already found — stop searching
|
|
37
|
+
if (node.type.name === "taskItem") {
|
|
38
|
+
if (seen === clickedIndex) {
|
|
39
|
+
position = pos;
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
seen += 1;
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
});
|
|
46
|
+
return position;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function MarkdownView({ content, onChange }: MarkdownViewProps) {
|
|
50
|
+
const [busy, setBusy] = useState(false);
|
|
51
|
+
|
|
52
|
+
// Refs let the stable onReadOnlyChecked callback read the latest values
|
|
53
|
+
// without being recreated (which would require rebuilding the editor).
|
|
54
|
+
const busyRef = useRef(false);
|
|
55
|
+
const lastConfirmedRef = useRef(content);
|
|
56
|
+
const editorRef = useRef<Editor | null>(null);
|
|
57
|
+
const onChangeRef = useRef(onChange);
|
|
58
|
+
onChangeRef.current = onChange;
|
|
59
|
+
|
|
60
|
+
const setBusy2 = useCallback((value: boolean) => {
|
|
61
|
+
busyRef.current = value;
|
|
62
|
+
setBusy(value);
|
|
63
|
+
}, []);
|
|
64
|
+
|
|
65
|
+
const handleReadOnlyChecked = useCallback(
|
|
66
|
+
(_node: ProseMirrorNode, checked: boolean): boolean => {
|
|
67
|
+
const editor = editorRef.current;
|
|
68
|
+
// Swallow the click (node view reverts it) when there is no consumer to
|
|
69
|
+
// report to, or while a save is already in flight.
|
|
70
|
+
if (!editor || !onChangeRef.current || busyRef.current) return false;
|
|
71
|
+
|
|
72
|
+
const position = positionOfToggled(editor, checked);
|
|
73
|
+
if (position === -1) return false;
|
|
74
|
+
|
|
75
|
+
// Apply the toggle to the document ourselves — the read-only node view
|
|
76
|
+
// does not touch the document, only the DOM checkbox.
|
|
77
|
+
const current = editor.state.doc.nodeAt(position);
|
|
78
|
+
editor.view.dispatch(
|
|
79
|
+
editor.state.tr.setNodeMarkup(position, undefined, { ...current?.attrs, checked }),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const markdown = editor.storage.markdown.getMarkdown();
|
|
83
|
+
const result = onChangeRef.current(markdown);
|
|
84
|
+
|
|
85
|
+
if (result instanceof Promise) {
|
|
86
|
+
setBusy2(true);
|
|
87
|
+
result
|
|
88
|
+
.then(() => {
|
|
89
|
+
lastConfirmedRef.current = markdown;
|
|
90
|
+
setBusy2(false);
|
|
91
|
+
})
|
|
92
|
+
.catch(() => {
|
|
93
|
+
editor.commands.setContent(lastConfirmedRef.current);
|
|
94
|
+
setBusy2(false);
|
|
95
|
+
});
|
|
96
|
+
} else {
|
|
97
|
+
lastConfirmedRef.current = markdown;
|
|
98
|
+
}
|
|
99
|
+
return true;
|
|
100
|
+
},
|
|
101
|
+
[setBusy2],
|
|
102
|
+
);
|
|
103
|
+
|
|
11
104
|
const editor = useEditor({
|
|
12
|
-
extensions:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
blockquote: false,
|
|
17
|
-
horizontalRule: false,
|
|
18
|
-
bulletList: false,
|
|
19
|
-
orderedList: false,
|
|
20
|
-
listItem: false,
|
|
21
|
-
}),
|
|
22
|
-
Link.configure({
|
|
23
|
-
openOnClick: true,
|
|
24
|
-
HTMLAttributes: {
|
|
25
|
-
class: "text-blue-600 underline",
|
|
26
|
-
target: "_blank",
|
|
27
|
-
rel: "noopener noreferrer",
|
|
28
|
-
},
|
|
29
|
-
}),
|
|
30
|
-
Markdown,
|
|
31
|
-
],
|
|
105
|
+
extensions: createMarkdownExtensions({
|
|
106
|
+
openLinksOnClick: true,
|
|
107
|
+
onReadOnlyChecked: handleReadOnlyChecked,
|
|
108
|
+
}),
|
|
32
109
|
content,
|
|
33
110
|
immediatelyRender: false,
|
|
34
111
|
editable: false,
|
|
@@ -38,12 +115,40 @@ export function MarkdownView({ content }: MarkdownViewProps) {
|
|
|
38
115
|
},
|
|
39
116
|
},
|
|
40
117
|
});
|
|
118
|
+
editorRef.current = editor;
|
|
41
119
|
|
|
120
|
+
// Guarded sync: only rebuild when `content` genuinely differs from what the
|
|
121
|
+
// document already holds, so an echo of what we just reported is a no-op. A
|
|
122
|
+
// genuinely new value also becomes the confirmed baseline, so a later
|
|
123
|
+
// rejection does not roll back to a stale document.
|
|
42
124
|
useEffect(() => {
|
|
43
|
-
if (editor)
|
|
125
|
+
if (!editor) return;
|
|
126
|
+
const current = editor.storage.markdown.getMarkdown();
|
|
127
|
+
if (content !== current) {
|
|
44
128
|
editor.commands.setContent(content);
|
|
129
|
+
lastConfirmedRef.current = content;
|
|
45
130
|
}
|
|
46
131
|
}, [content, editor]);
|
|
47
132
|
|
|
48
|
-
|
|
133
|
+
// Reflect the busy state on the actual checkboxes. Re-applied whenever the
|
|
134
|
+
// document may have been rebuilt (busy toggled, content changed), because
|
|
135
|
+
// node views recreate the input elements on every transaction.
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (!editor) return;
|
|
138
|
+
const inputs = editor.view.dom.querySelectorAll<HTMLInputElement>("input[type=checkbox]");
|
|
139
|
+
inputs.forEach((input) => {
|
|
140
|
+
input.disabled = busy;
|
|
141
|
+
});
|
|
142
|
+
}, [busy, editor, content]);
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<div
|
|
146
|
+
aria-busy={busy}
|
|
147
|
+
className={cn(
|
|
148
|
+
busy && "[&_input[type=checkbox]]:cursor-not-allowed [&_input[type=checkbox]]:opacity-50",
|
|
149
|
+
)}
|
|
150
|
+
>
|
|
151
|
+
<EditorContent editor={editor} />
|
|
152
|
+
</div>
|
|
153
|
+
);
|
|
49
154
|
}
|
package/src/index.ts
CHANGED
|
@@ -139,6 +139,8 @@ export type { DetailFieldProps, DetailFieldTranslations } from "./components/det
|
|
|
139
139
|
export { LanguageSwitch } from "./components/language-switch.tsx";
|
|
140
140
|
export { HealthStatus } from "./components/health-status.tsx";
|
|
141
141
|
export type { HealthStatusProps, HealthStatusTranslations } from "./components/health-status.tsx";
|
|
142
|
+
export { CapabilityStatus } from "./components/capability-status.tsx";
|
|
143
|
+
export type { CapabilityStatusProps } from "./components/capability-status.tsx";
|
|
142
144
|
export { TagChips } from "./components/tag-chips.tsx";
|
|
143
145
|
export type { TagChipsProps } from "./components/tag-chips.tsx";
|
|
144
146
|
export { TagForm } from "./components/tag-form.tsx";
|
|
@@ -188,6 +190,7 @@ export type {
|
|
|
188
190
|
UserMultiSelectTranslations,
|
|
189
191
|
MarkdownEditorProps,
|
|
190
192
|
MarkdownViewProps,
|
|
193
|
+
MarkdownToolbarAction,
|
|
191
194
|
} from "./types/index.ts";
|
|
192
195
|
|
|
193
196
|
// Utilities
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from "vitest";
|
|
2
|
+
import { Editor } from "@tiptap/core";
|
|
3
|
+
import StarterKit from "@tiptap/starter-kit";
|
|
4
|
+
import { TaskList, TaskItem } from "@tiptap/extension-list";
|
|
5
|
+
import { Markdown } from "tiptap-markdown";
|
|
6
|
+
import { createMarkdownExtensions } from "../markdown-extensions.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Serialize `input` through a real editor built from the shared factory and
|
|
10
|
+
* return the Markdown the document produces. This is exactly the pipeline the
|
|
11
|
+
* components use, so what these tests verify is what ships.
|
|
12
|
+
*/
|
|
13
|
+
function roundTrip(input: string): string {
|
|
14
|
+
const editor = new Editor({ extensions: createMarkdownExtensions(), content: input });
|
|
15
|
+
const markdown = editor.storage.markdown.getMarkdown();
|
|
16
|
+
editor.destroy();
|
|
17
|
+
return markdown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let editors: Editor[] = [];
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
editors.forEach((e) => e.destroy());
|
|
24
|
+
editors = [];
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("createMarkdownExtensions — round-trip", () => {
|
|
28
|
+
it("keeps a task list unchanged, including the [x] and [ ] markers", () => {
|
|
29
|
+
const input = "- [x] Write the press release\n- [ ] Send invitations";
|
|
30
|
+
expect(roundTrip(input)).toBe(input);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("keeps a bullet list unchanged", () => {
|
|
34
|
+
const input = "- Milk\n- Bread";
|
|
35
|
+
expect(roundTrip(input)).toBe(input);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("keeps an ordered list unchanged", () => {
|
|
39
|
+
const input = "1. First\n2. Second";
|
|
40
|
+
expect(roundTrip(input)).toBe(input);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("keeps headings at every level H1–H6", () => {
|
|
44
|
+
const input = "# H1\n\n## H2\n\n### H3\n\n#### H4\n\n##### H5\n\n###### H6";
|
|
45
|
+
expect(roundTrip(input)).toBe(input);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("keeps a blockquote, a fenced code block with a language tag, and a rule", () => {
|
|
49
|
+
const input = "> A wise quote\n\n```js\nconst x = 1;\n```\n\n---";
|
|
50
|
+
const out = roundTrip(input);
|
|
51
|
+
expect(out).toBe(input);
|
|
52
|
+
expect(out).toContain("> A wise quote");
|
|
53
|
+
expect(out).toContain("```js\nconst x = 1;\n```");
|
|
54
|
+
expect(out).toContain("---");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("keeps a nested task list, preserving indentation and nesting level", () => {
|
|
58
|
+
const input = "- [ ] parent\n - [ ] child";
|
|
59
|
+
expect(roundTrip(input)).toBe(input);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("keeps marks inside blocks — bold and links survive", () => {
|
|
63
|
+
const input = "- [x] Call **Anna** about [the offer](https://example.com)";
|
|
64
|
+
expect(roundTrip(input)).toBe(input);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("keeps both kinds of items in a mixed list", () => {
|
|
68
|
+
const out = roundTrip("- [x] Done\n- Just an item");
|
|
69
|
+
// A checklist item and a plain item cannot share one node, so the serializer
|
|
70
|
+
// splits them; byte-identity is not promised, but nothing may be dropped.
|
|
71
|
+
expect(out).toContain("- [x] Done");
|
|
72
|
+
expect(out).toContain("- Just an item");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("does not grow a trailing paragraph after a code block", () => {
|
|
76
|
+
const input = "intro\n\n```js\nconst y = 2;\n```";
|
|
77
|
+
expect(roundTrip(input)).toBe(input);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("does not grow a trailing paragraph after a horizontal rule", () => {
|
|
81
|
+
const input = "intro\n\n---";
|
|
82
|
+
expect(roundTrip(input)).toBe(input);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("produces empty Markdown for empty content", () => {
|
|
86
|
+
expect(roundTrip("")).toBe("");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("keeps an unchecked-only task list and does not normalise the marker", () => {
|
|
90
|
+
const input = "- [ ] Open item";
|
|
91
|
+
const out = roundTrip(input);
|
|
92
|
+
expect(out).toBe(input);
|
|
93
|
+
expect(out).not.toContain("[x]");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("leaves plain text byte-identical", () => {
|
|
97
|
+
const input = "Just a plain paragraph with no markdown.";
|
|
98
|
+
expect(roundTrip(input)).toBe(input);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("shows the placeholder when one is configured and the document is empty", () => {
|
|
102
|
+
const editor = new Editor({
|
|
103
|
+
extensions: createMarkdownExtensions({ placeholder: "Type here..." }),
|
|
104
|
+
content: "",
|
|
105
|
+
});
|
|
106
|
+
editors.push(editor);
|
|
107
|
+
const placeholderExt = editor.extensionManager.extensions.find((e) => e.name === "placeholder");
|
|
108
|
+
expect(placeholderExt?.options.placeholder).toBe("Type here...");
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("createMarkdownExtensions — marks Markdown cannot express", () => {
|
|
113
|
+
it("does not register underline, so the command is unavailable", () => {
|
|
114
|
+
const editor = new Editor({ extensions: createMarkdownExtensions(), content: "hello" });
|
|
115
|
+
editors.push(editor);
|
|
116
|
+
const hasUnderline = editor.extensionManager.extensions.some((e) => e.name === "underline");
|
|
117
|
+
expect(hasUnderline).toBe(false);
|
|
118
|
+
// The mark is absent from the schema, so no command can apply it —
|
|
119
|
+
// attempting to resolve the mark type throws rather than mutating the doc.
|
|
120
|
+
expect(editor.schema.marks.underline).toBeUndefined();
|
|
121
|
+
editor.commands.selectAll();
|
|
122
|
+
expect(() => editor.chain().setMark("underline").run()).toThrow();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
/** Mount a real editor attached to the DOM so keyboard handling is active. */
|
|
127
|
+
function mountEditor(content: string, extensions = createMarkdownExtensions()): Editor {
|
|
128
|
+
const element = document.createElement("div");
|
|
129
|
+
document.body.appendChild(element);
|
|
130
|
+
const editor = new Editor({ element, extensions, content });
|
|
131
|
+
editors.push(editor);
|
|
132
|
+
return editor;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function pressKey(editor: Editor, init: KeyboardEventInit): void {
|
|
136
|
+
editor.view.dom.dispatchEvent(
|
|
137
|
+
new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ...init }),
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Simulate typing the trailing space of "[ ] " so a task-item input rule would fire. */
|
|
142
|
+
function typeSpaceAfterBracket(editor: Editor): void {
|
|
143
|
+
editor.commands.setContent("<p>[ ]</p>");
|
|
144
|
+
editor.commands.focus("end");
|
|
145
|
+
const { from } = editor.state.selection;
|
|
146
|
+
editor.view.someProp("handleTextInput", (handler) =>
|
|
147
|
+
handler(editor.view, from, from, " ", () => editor.state.tr),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function hasTaskList(editor: Editor): boolean {
|
|
152
|
+
return (editor.getJSON().content ?? []).some((node) => node.type === "taskList");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Dispatch the task-list creation shortcut (`Mod-Shift-9`). jsdom is treated as a
|
|
157
|
+
* non-Mac platform by prosemirror-keymap, so `Mod` resolves to Ctrl — sending
|
|
158
|
+
* both metaKey and ctrlKey would fail to match the binding.
|
|
159
|
+
*/
|
|
160
|
+
function pressTaskListShortcut(editor: Editor): void {
|
|
161
|
+
editor.commands.focus();
|
|
162
|
+
pressKey(editor, { key: "9", code: "Digit9", ctrlKey: true, shiftKey: true });
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
describe("createMarkdownExtensions — task list creation stays closed", () => {
|
|
166
|
+
it("does nothing when Mod-Shift-9 is pressed", () => {
|
|
167
|
+
const editor = mountEditor("<p></p>");
|
|
168
|
+
pressTaskListShortcut(editor);
|
|
169
|
+
expect(hasTaskList(editor)).toBe(false);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("does nothing when the user types '[ ] ' — the literal text remains", () => {
|
|
173
|
+
const editor = mountEditor("<p></p>");
|
|
174
|
+
typeSpaceAfterBracket(editor);
|
|
175
|
+
expect(hasTaskList(editor)).toBe(false);
|
|
176
|
+
expect(editor.getText()).toContain("[ ]");
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("guard: the same '[ ] ' input DOES create a task list with the default TaskItem", () => {
|
|
180
|
+
// Proves the simulated input rule genuinely fires, so the stripped-rule
|
|
181
|
+
// assertion above cannot silently pass if the trigger stops working.
|
|
182
|
+
const editor = mountEditor("<p></p>", [
|
|
183
|
+
StarterKit.configure({ underline: false }),
|
|
184
|
+
TaskList,
|
|
185
|
+
TaskItem.configure({ nested: true }),
|
|
186
|
+
Markdown,
|
|
187
|
+
]);
|
|
188
|
+
typeSpaceAfterBracket(editor);
|
|
189
|
+
expect(hasTaskList(editor)).toBe(true);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("createMarkdownExtensions — editing an existing task list", () => {
|
|
194
|
+
it("splits a task item into a new unchecked item on Enter", () => {
|
|
195
|
+
const editor = mountEditor("- [x] Done");
|
|
196
|
+
editor.commands.focus("end");
|
|
197
|
+
pressKey(editor, { key: "Enter", code: "Enter" });
|
|
198
|
+
const md = editor.storage.markdown.getMarkdown();
|
|
199
|
+
expect(md).toContain("- [x] Done");
|
|
200
|
+
expect(md.split("\n").some((line) => line.startsWith("- [ ]"))).toBe(true);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("lifts a nested item one level on Shift-Tab", () => {
|
|
204
|
+
const editor = mountEditor("- [ ] parent\n - [ ] child");
|
|
205
|
+
editor.commands.focus("end");
|
|
206
|
+
pressKey(editor, { key: "Tab", code: "Tab", shiftKey: true });
|
|
207
|
+
expect(editor.storage.markdown.getMarkdown()).toBe("- [ ] parent\n- [ ] child");
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
describe("createMarkdownExtensions — task list creation gate follows allowedActions", () => {
|
|
212
|
+
it("keeps creation closed when taskList is not among the allowed actions", () => {
|
|
213
|
+
const shortcutEditor = mountEditor(
|
|
214
|
+
"<p></p>",
|
|
215
|
+
createMarkdownExtensions({ allowedActions: ["bold", "italic"] }),
|
|
216
|
+
);
|
|
217
|
+
pressTaskListShortcut(shortcutEditor);
|
|
218
|
+
expect(hasTaskList(shortcutEditor)).toBe(false);
|
|
219
|
+
|
|
220
|
+
const ruleEditor = mountEditor(
|
|
221
|
+
"<p></p>",
|
|
222
|
+
createMarkdownExtensions({ allowedActions: ["bold", "italic"] }),
|
|
223
|
+
);
|
|
224
|
+
typeSpaceAfterBracket(ruleEditor);
|
|
225
|
+
expect(hasTaskList(ruleEditor)).toBe(false);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("opens the keyboard shortcut when taskList is allowed", () => {
|
|
229
|
+
const editor = mountEditor(
|
|
230
|
+
"<p></p>",
|
|
231
|
+
createMarkdownExtensions({ allowedActions: ["taskList"] }),
|
|
232
|
+
);
|
|
233
|
+
pressTaskListShortcut(editor);
|
|
234
|
+
expect(hasTaskList(editor)).toBe(true);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("opens the '[ ] ' input rule when taskList is allowed", () => {
|
|
238
|
+
const editor = mountEditor(
|
|
239
|
+
"<p></p>",
|
|
240
|
+
createMarkdownExtensions({ allowedActions: ["taskList"] }),
|
|
241
|
+
);
|
|
242
|
+
typeSpaceAfterBracket(editor);
|
|
243
|
+
expect(hasTaskList(editor)).toBe(true);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("keeps a stored task list editable even when creation is gated", () => {
|
|
247
|
+
const editor = mountEditor("- [x] Done", createMarkdownExtensions({ allowedActions: [] }));
|
|
248
|
+
editor.commands.focus("end");
|
|
249
|
+
pressKey(editor, { key: "Enter", code: "Enter" });
|
|
250
|
+
const md = editor.storage.markdown.getMarkdown();
|
|
251
|
+
expect(md).toContain("- [x] Done");
|
|
252
|
+
expect(md.split("\n").some((line) => line.startsWith("- [ ]"))).toBe(true);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("round-trips a stored task list unchanged when creation is gated", () => {
|
|
256
|
+
const editor = new Editor({
|
|
257
|
+
extensions: createMarkdownExtensions({ allowedActions: [] }),
|
|
258
|
+
content: "- [x] Done",
|
|
259
|
+
});
|
|
260
|
+
editors.push(editor);
|
|
261
|
+
expect(editor.storage.markdown.getMarkdown()).toBe("- [x] Done");
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe("createMarkdownExtensions — marks stay reachable regardless of allowedActions", () => {
|
|
266
|
+
it("applies bold via Mod-b even when only italic is offered", () => {
|
|
267
|
+
const editor = mountEditor(
|
|
268
|
+
"<p>hello</p>",
|
|
269
|
+
createMarkdownExtensions({ allowedActions: ["italic"] }),
|
|
270
|
+
);
|
|
271
|
+
editor.commands.selectAll();
|
|
272
|
+
pressKey(editor, { key: "b", code: "KeyB", ctrlKey: true });
|
|
273
|
+
expect(editor.storage.markdown.getMarkdown()).toBe("**hello**");
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("completes the **bold** input rule even when only italic is offered", () => {
|
|
277
|
+
// `<p>...</p>` is parsed as literal HTML text, so the incomplete `**bold*`
|
|
278
|
+
// survives to the document and the closing `*` triggers the bold input rule.
|
|
279
|
+
const editor = mountEditor(
|
|
280
|
+
"<p>**bold*</p>",
|
|
281
|
+
createMarkdownExtensions({ allowedActions: ["italic"] }),
|
|
282
|
+
);
|
|
283
|
+
editor.commands.focus("end");
|
|
284
|
+
const { from } = editor.state.selection;
|
|
285
|
+
editor.view.someProp("handleTextInput", (handler) =>
|
|
286
|
+
handler(editor.view, from, from, "*", () => editor.state.tr),
|
|
287
|
+
);
|
|
288
|
+
expect(editor.storage.markdown.getMarkdown()).toBe("**bold**");
|
|
289
|
+
});
|
|
290
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Extensions, NodeWithPos } from "@tiptap/core";
|
|
2
|
+
import StarterKit from "@tiptap/starter-kit";
|
|
3
|
+
import { TaskList, TaskItem } from "@tiptap/extension-list";
|
|
4
|
+
import Placeholder from "@tiptap/extension-placeholder";
|
|
5
|
+
import { Markdown } from "tiptap-markdown";
|
|
6
|
+
import type { MarkdownToolbarAction } from "../types/index.ts";
|
|
7
|
+
|
|
8
|
+
/** The ProseMirror document node type, re-derived without a direct `@tiptap/pm` dependency. */
|
|
9
|
+
type ProseMirrorNode = NodeWithPos["node"];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Options for {@link createMarkdownExtensions}.
|
|
13
|
+
*/
|
|
14
|
+
export interface MarkdownExtensionsOptions {
|
|
15
|
+
/** Placeholder text shown while the document is empty. Editor only. */
|
|
16
|
+
readonly placeholder?: string;
|
|
17
|
+
/** Whether clicking a link opens it. `true` in the view, `false` in the editor. */
|
|
18
|
+
readonly openLinksOnClick?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Actions the user may *create*. Only `"taskList"` is honoured here — its
|
|
21
|
+
* keyboard shortcut and input rule are opened when it is present and stripped
|
|
22
|
+
* when it is not. All other actions are gated at the toolbar button only, so
|
|
23
|
+
* they need no schema-level handling. Omitted or empty → task-list creation
|
|
24
|
+
* stays closed. This never affects what the schema can parse or serialize.
|
|
25
|
+
*/
|
|
26
|
+
readonly allowedActions?: readonly MarkdownToolbarAction[];
|
|
27
|
+
/**
|
|
28
|
+
* Invoked when a task-list checkbox is toggled while the editor is read-only.
|
|
29
|
+
* Return `true` to accept the toggle (the caller is responsible for applying
|
|
30
|
+
* it to the document) or `false` to revert the checkbox. Used by
|
|
31
|
+
* `MarkdownView` to make checklists interactive; omit for a read-only view.
|
|
32
|
+
*/
|
|
33
|
+
readonly onReadOnlyChecked?: (node: ProseMirrorNode, checked: boolean) => boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Builds the TipTap extension set shared by `MarkdownEditor`, `MarkdownView`
|
|
38
|
+
* and the round-trip tests.
|
|
39
|
+
*
|
|
40
|
+
* The schema intentionally covers everything Markdown can express and TipTap
|
|
41
|
+
* can model with the StarterKit plus task lists, so stored content round-trips
|
|
42
|
+
* untouched. Two marks are deliberately excluded:
|
|
43
|
+
*
|
|
44
|
+
* - `underline` — Markdown has no representation for it, so enabling it would
|
|
45
|
+
* reintroduce the data-loss bug this configuration exists to prevent.
|
|
46
|
+
* - a separate `Link` extension — StarterKit already ships `link`, so it is
|
|
47
|
+
* configured through StarterKit rather than registered twice.
|
|
48
|
+
*
|
|
49
|
+
* Task lists render but cannot be created: the `Mod-Shift-9` shortcut and the
|
|
50
|
+
* `[ ] ` input rule are stripped. Editing shortcuts inside an existing task
|
|
51
|
+
* list (Enter, Tab, Shift-Tab) are kept.
|
|
52
|
+
*/
|
|
53
|
+
export function createMarkdownExtensions(options?: MarkdownExtensionsOptions): Extensions {
|
|
54
|
+
const openLinksOnClick = options?.openLinksOnClick ?? false;
|
|
55
|
+
const taskListAllowed = (options?.allowedActions ?? []).includes("taskList");
|
|
56
|
+
|
|
57
|
+
// The `tight` attribute makes the Markdown serializer render task lists
|
|
58
|
+
// tightly (no blank line between items), matching how tiptap-markdown already
|
|
59
|
+
// treats bullet and ordered lists. Without it, prosemirror-markdown falls back
|
|
60
|
+
// to a loose list and a multi-item checklist no longer round-trips byte-for-byte.
|
|
61
|
+
// `rendered: false` keeps the attribute out of the DOM so it is not persisted.
|
|
62
|
+
// It is applied whether or not task-list *creation* is allowed, because a
|
|
63
|
+
// stored task list must always round-trip.
|
|
64
|
+
const TaskListWithTight = TaskList.extend({
|
|
65
|
+
addAttributes() {
|
|
66
|
+
return {
|
|
67
|
+
...this.parent?.(),
|
|
68
|
+
tight: { default: true, rendered: false },
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Gate task-list *creation* on the allowlist: when it is not offered, strip the
|
|
74
|
+
// `Mod-Shift-9` shortcut and the `[ ] ` input rule so a checklist cannot be
|
|
75
|
+
// created by any means. In-list editing shortcuts (Enter/Tab/Shift-Tab) are
|
|
76
|
+
// always kept so a stored list stays editable.
|
|
77
|
+
const TaskListNode = taskListAllowed
|
|
78
|
+
? TaskListWithTight
|
|
79
|
+
: TaskListWithTight.extend({ addKeyboardShortcuts: () => ({}) });
|
|
80
|
+
const TaskItemNode = taskListAllowed ? TaskItem : TaskItem.extend({ addInputRules: () => [] });
|
|
81
|
+
|
|
82
|
+
return [
|
|
83
|
+
StarterKit.configure({
|
|
84
|
+
underline: false,
|
|
85
|
+
link: {
|
|
86
|
+
openOnClick: openLinksOnClick,
|
|
87
|
+
HTMLAttributes: openLinksOnClick
|
|
88
|
+
? {
|
|
89
|
+
class: "text-blue-600 underline",
|
|
90
|
+
target: "_blank",
|
|
91
|
+
rel: "noopener noreferrer",
|
|
92
|
+
}
|
|
93
|
+
: { class: "text-blue-600 underline" },
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
// `list-none pl-0` suppresses the `prose` bullet so a checkbox does not also
|
|
97
|
+
// get a list marker; `flex items-start gap-2` aligns the box with its label.
|
|
98
|
+
TaskListNode.configure({ HTMLAttributes: { class: "list-none pl-0" } }),
|
|
99
|
+
TaskItemNode.configure({
|
|
100
|
+
nested: true,
|
|
101
|
+
HTMLAttributes: { class: "flex items-start gap-2" },
|
|
102
|
+
onReadOnlyChecked: options?.onReadOnlyChecked,
|
|
103
|
+
}),
|
|
104
|
+
Placeholder.configure({ placeholder: options?.placeholder ?? "" }),
|
|
105
|
+
Markdown,
|
|
106
|
+
];
|
|
107
|
+
}
|
package/src/test/setup.ts
CHANGED
|
@@ -1 +1,30 @@
|
|
|
1
1
|
import "@testing-library/jest-dom/vitest";
|
|
2
|
+
|
|
3
|
+
// jsdom does not implement layout. ProseMirror's scrollIntoView (triggered by
|
|
4
|
+
// focus/selection commands) reads client rects from DOM nodes — including Text
|
|
5
|
+
// nodes, which jsdom does not give getClientRects/getBoundingClientRect. Provide
|
|
6
|
+
// empty stubs so editor commands do not crash under jsdom. Purely additive: only
|
|
7
|
+
// methods that are missing are defined.
|
|
8
|
+
const emptyRect = (): DOMRect => ({
|
|
9
|
+
top: 0,
|
|
10
|
+
bottom: 0,
|
|
11
|
+
left: 0,
|
|
12
|
+
right: 0,
|
|
13
|
+
width: 0,
|
|
14
|
+
height: 0,
|
|
15
|
+
x: 0,
|
|
16
|
+
y: 0,
|
|
17
|
+
toJSON: () => ({}),
|
|
18
|
+
});
|
|
19
|
+
const emptyRectList = (): DOMRectList =>
|
|
20
|
+
Object.assign([] as DOMRect[], { item: () => null }) as unknown as DOMRectList;
|
|
21
|
+
|
|
22
|
+
type Measurable = {
|
|
23
|
+
getClientRects?: () => DOMRectList;
|
|
24
|
+
getBoundingClientRect?: () => DOMRect;
|
|
25
|
+
};
|
|
26
|
+
for (const proto of [Node.prototype, Range.prototype]) {
|
|
27
|
+
const measurable = proto as unknown as Measurable;
|
|
28
|
+
if (!measurable.getClientRects) measurable.getClientRects = emptyRectList;
|
|
29
|
+
if (!measurable.getBoundingClientRect) measurable.getBoundingClientRect = emptyRect;
|
|
30
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import type { MarkdownToolbarAction, MarkdownEditorProps, MarkdownViewProps } from "../index.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* These assertions are enforced by `tsc` (the runtime bodies are trivial). A
|
|
6
|
+
* regression in the types fails `pnpm typecheck`, not just this test run.
|
|
7
|
+
*/
|
|
8
|
+
describe("MarkdownToolbarAction", () => {
|
|
9
|
+
it("does not accept 'unlink' — Unlink is rendered as part of 'link'", () => {
|
|
10
|
+
const props: MarkdownEditorProps = {
|
|
11
|
+
value: "",
|
|
12
|
+
onChange: () => {},
|
|
13
|
+
// @ts-expect-error "unlink" is not a MarkdownToolbarAction.
|
|
14
|
+
toolbar: ["unlink"],
|
|
15
|
+
};
|
|
16
|
+
void props;
|
|
17
|
+
expect(true).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("accepts the declared actions", () => {
|
|
21
|
+
const actions: readonly MarkdownToolbarAction[] = ["bold", "taskList", "horizontalRule"];
|
|
22
|
+
expect(actions).toHaveLength(3);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("MarkdownViewProps", () => {
|
|
27
|
+
it("is still exactly a readonly content string", () => {
|
|
28
|
+
type Expected = { readonly content: string };
|
|
29
|
+
type Equal<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
|
|
30
|
+
// Fails to compile if MarkdownViewProps gains, loses or changes a field.
|
|
31
|
+
const unchanged: Equal<MarkdownViewProps, Expected> = true;
|
|
32
|
+
expect(unchanged).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
});
|