Fix the table cell arrow race and remove the TipTap Writer
Phase 4a. The ProseMirror focus handler starts a 20 ms timer that writes the ProseMirror selection back to the DOM. Under load, the timer ran after a native arrow move and before ProseMirror read the selectionchange event, so one move was lost. The cell editor now moves the DOM selection with Selection.modify and reads the result in the same key event. This covers plain and Shift arrows and word moves. Other shortcuts stay with the browser. - Workbench.svelte no longer filters access events. Rust already drops them. - Delete the unused TipTap Writer: WriterHost.svelte, writerMenu.ts, writerMarks.ts, headingMarks.ts, and their two unit tests. - Remove the 8 TipTap packages that only the Writer used, and its entry in docs/menu-surfaces.json. With a 4x CPU slowdown, 11 of 60 runs failed before the fix and 0 of 120 after it. pnpm check, pnpm test, and the smoke suite pass.
This commit is contained in:
@@ -80,13 +80,6 @@
|
|||||||
"design": "Shared action menu",
|
"design": "Shared action menu",
|
||||||
"check": "tests/smoke/menus/consistency.smoke.mjs; tests/smoke/links/linkEditing.smoke.mjs; tests/smoke/editor/surfaces.smoke.mjs"
|
"check": "tests/smoke/menus/consistency.smoke.mjs; tests/smoke/links/linkEditing.smoke.mjs; tests/smoke/editor/surfaces.smoke.mjs"
|
||||||
},
|
},
|
||||||
"RuneStone/src/lib/editor/WriterHost.svelte": {
|
|
||||||
"counts": {
|
|
||||||
"svelteMenu": 1
|
|
||||||
},
|
|
||||||
"design": "Shared action menu",
|
|
||||||
"check": "tests/smoke/menus/consistency.smoke.mjs; tests/smoke/links/linkEditing.smoke.mjs; tests/smoke/editor/surfaces.smoke.mjs"
|
|
||||||
},
|
|
||||||
"RuneStone/src/lib/layout/ContextMenu.svelte": {
|
"RuneStone/src/lib/layout/ContextMenu.svelte": {
|
||||||
"counts": {
|
"counts": {
|
||||||
"action": 1
|
"action": 1
|
||||||
|
|||||||
@@ -38,14 +38,6 @@
|
|||||||
"@tiptap/extension-highlight": "^3.31.3",
|
"@tiptap/extension-highlight": "^3.31.3",
|
||||||
"@tiptap/extension-image": "^3.31.3",
|
"@tiptap/extension-image": "^3.31.3",
|
||||||
"@tiptap/extension-link": "^3.31.3",
|
"@tiptap/extension-link": "^3.31.3",
|
||||||
"@tiptap/extension-subscript": "^3.31.3",
|
|
||||||
"@tiptap/extension-superscript": "^3.31.3",
|
|
||||||
"@tiptap/extension-table": "^3.31.3",
|
|
||||||
"@tiptap/extension-table-cell": "^3.31.3",
|
|
||||||
"@tiptap/extension-table-header": "^3.31.3",
|
|
||||||
"@tiptap/extension-table-row": "^3.31.3",
|
|
||||||
"@tiptap/extension-task-item": "^3.31.3",
|
|
||||||
"@tiptap/extension-task-list": "^3.31.3",
|
|
||||||
"@tiptap/pm": "^3.31.3",
|
"@tiptap/pm": "^3.31.3",
|
||||||
"@tiptap/starter-kit": "^3.31.3",
|
"@tiptap/starter-kit": "^3.31.3",
|
||||||
"markdown-it": "^15.0.2",
|
"markdown-it": "^15.0.2",
|
||||||
|
|||||||
Generated
-102
@@ -47,30 +47,6 @@ importers:
|
|||||||
'@tiptap/extension-link':
|
'@tiptap/extension-link':
|
||||||
specifier: ^3.31.3
|
specifier: ^3.31.3
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
||||||
'@tiptap/extension-subscript':
|
|
||||||
specifier: ^3.31.3
|
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
|
||||||
'@tiptap/extension-superscript':
|
|
||||||
specifier: ^3.31.3
|
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
|
||||||
'@tiptap/extension-table':
|
|
||||||
specifier: ^3.31.3
|
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
|
||||||
'@tiptap/extension-table-cell':
|
|
||||||
specifier: ^3.31.3
|
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
|
|
||||||
'@tiptap/extension-table-header':
|
|
||||||
specifier: ^3.31.3
|
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
|
|
||||||
'@tiptap/extension-table-row':
|
|
||||||
specifier: ^3.31.3
|
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
|
|
||||||
'@tiptap/extension-task-item':
|
|
||||||
specifier: ^3.31.3
|
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
|
|
||||||
'@tiptap/extension-task-list':
|
|
||||||
specifier: ^3.31.3
|
|
||||||
version: 3.31.3(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
|
|
||||||
'@tiptap/pm':
|
'@tiptap/pm':
|
||||||
specifier: ^3.31.3
|
specifier: ^3.31.3
|
||||||
version: 3.31.3
|
version: 3.31.3
|
||||||
@@ -569,49 +545,6 @@ packages:
|
|||||||
peerDependencies:
|
peerDependencies:
|
||||||
'@tiptap/core': 3.31.3
|
'@tiptap/core': 3.31.3
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
|
||||||
resolution: {integrity: sha512-jUMMtg4QF7wIXIvsgBmgJjdQMETOX4KMF+Nl/0nuCKs59lEi1StOJzkiEdm+UbFhxvvY9B30uLVgzogGIlHHyg==}
|
|
||||||
peerDependencies:
|
|
||||||
'@tiptap/core': 3.31.3
|
|
||||||
'@tiptap/pm': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
|
||||||
resolution: {integrity: sha512-WzM84fb8akg1Ni2b1DcE1pE2kkWLbkEVxN2DKElzCVk/kuSNp+VKqOjunHGTnO+kJhXdcmXTVqAO4Vp2kiDxMQ==}
|
|
||||||
peerDependencies:
|
|
||||||
'@tiptap/core': 3.31.3
|
|
||||||
'@tiptap/pm': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
|
||||||
resolution: {integrity: sha512-5nueKR/p/IX6B4etWqHjyRsjNfDf6dJZaRgfw1/1l90CJe2h3tTZk4JbUKWN3Mo4FzZuYKzmU3fxcZ8pSVOeJQ==}
|
|
||||||
peerDependencies:
|
|
||||||
'@tiptap/extension-table': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
|
||||||
resolution: {integrity: sha512-sstVtNQiBYX4P16vlhwBYPsthxDnodHWQfGq0EUQM40Z7H796Lgh9t1qYtu8yARQuHKzvINRwJCSy6DZ8YTDtQ==}
|
|
||||||
peerDependencies:
|
|
||||||
'@tiptap/extension-table': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
|
||||||
resolution: {integrity: sha512-up6tDK+hYVTFDeJ3XqKO0WJqBzyXNfGxMoimo5h83jrq9dcNh0oPCOzglZ7rdo8GH9I3VJ2fNKxxU2Eg2HcMoQ==}
|
|
||||||
peerDependencies:
|
|
||||||
'@tiptap/extension-table': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
|
||||||
resolution: {integrity: sha512-7cnVPHhdiGGeauYqca6JVyPLTqZbqFEEk9nn2e2E8+fBo6zVtV07AktJBqth/XEzjZxcUmGxjoeuWYAisWjUHg==}
|
|
||||||
peerDependencies:
|
|
||||||
'@tiptap/core': 3.31.3
|
|
||||||
'@tiptap/pm': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
|
||||||
resolution: {integrity: sha512-gCWvvXsCzi9tVFXPqKlkbU81XxtIdfjGy7FM9QFRKiWAvq5uv+Q5nvwrCyMVyP4bA7zRGWFaPzwjL68djgzoBw==}
|
|
||||||
peerDependencies:
|
|
||||||
'@tiptap/extension-list': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
|
||||||
resolution: {integrity: sha512-3WgVzmfEnDbmxbjUBlvTiKpT53KSBnnYhXUVQxM0nz1vpoWG8QifhsnCVsNI6TZmhgR0x/jY+UW9Hc/WLF8DkQ==}
|
|
||||||
peerDependencies:
|
|
||||||
'@tiptap/extension-list': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected]':
|
'@tiptap/[email protected]':
|
||||||
resolution: {integrity: sha512-gdsWtF+taeaCu6V+5Ct10fGo0ACUy1GnYtbb+mcathBt8OqbT+Ws60p/yEmKesBDz2Hn+B5IcWy6+2BBZl5ZTg==}
|
resolution: {integrity: sha512-gdsWtF+taeaCu6V+5Ct10fGo0ACUy1GnYtbb+mcathBt8OqbT+Ws60p/yEmKesBDz2Hn+B5IcWy6+2BBZl5ZTg==}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
@@ -1825,41 +1758,6 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
'@tiptap/core': 3.31.3(@tiptap/[email protected])
|
'@tiptap/core': 3.31.3(@tiptap/[email protected])
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])':
|
|
||||||
dependencies:
|
|
||||||
'@tiptap/core': 3.31.3(@tiptap/[email protected])
|
|
||||||
'@tiptap/pm': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])':
|
|
||||||
dependencies:
|
|
||||||
'@tiptap/core': 3.31.3(@tiptap/[email protected])
|
|
||||||
'@tiptap/pm': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))':
|
|
||||||
dependencies:
|
|
||||||
'@tiptap/extension-table': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))':
|
|
||||||
dependencies:
|
|
||||||
'@tiptap/extension-table': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))':
|
|
||||||
dependencies:
|
|
||||||
'@tiptap/extension-table': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])':
|
|
||||||
dependencies:
|
|
||||||
'@tiptap/core': 3.31.3(@tiptap/[email protected])
|
|
||||||
'@tiptap/pm': 3.31.3
|
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))':
|
|
||||||
dependencies:
|
|
||||||
'@tiptap/extension-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))':
|
|
||||||
dependencies:
|
|
||||||
'@tiptap/extension-list': 3.31.3(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
|
|
||||||
|
|
||||||
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))':
|
'@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@tiptap/core': 3.31.3(@tiptap/[email protected])
|
'@tiptap/core': 3.31.3(@tiptap/[email protected])
|
||||||
|
|||||||
@@ -1,286 +0,0 @@
|
|||||||
<script lang="ts">
|
|
||||||
import { t } from "$lib/i18n";
|
|
||||||
import { untrack } from "svelte";
|
|
||||||
import type { OpenBuffer } from "$lib/workspace/types";
|
|
||||||
import { workspaceReadDataUrl } from "$lib/workspace/api";
|
|
||||||
import { markdownBlocks } from "$lib/workspace/markdownBlocks";
|
|
||||||
import { findMarkdownImages, isRemoteImage, resolveImageRel } from "$lib/workspace/media";
|
|
||||||
import { renderMarkdownHtml } from "$lib/workspace/noteHtml";
|
|
||||||
import { htmlToMarkdown } from "$lib/workspace/htmlToMarkdown";
|
|
||||||
import ContextMenu from "$lib/layout/ContextMenu.svelte";
|
|
||||||
import type { MenuItem } from "$lib/layout/menuTypes";
|
|
||||||
import { writerContextItems } from "./writerMenu";
|
|
||||||
import { useSpellingMenu } from "./spellingMenu";
|
|
||||||
|
|
||||||
type Props = {
|
|
||||||
buffer: OpenBuffer;
|
|
||||||
onChange: (value: string) => void;
|
|
||||||
onInsertImage?: (file: File) => Promise<string | null>;
|
|
||||||
onOpenLink?: (kind: "md" | "wiki", target: string) => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
type OpenMenu = { x: number; y: number; items: MenuItem[] } | null;
|
|
||||||
|
|
||||||
let { buffer, onChange, onInsertImage, onOpenLink }: Props = $props();
|
|
||||||
|
|
||||||
let host: HTMLDivElement | undefined = $state();
|
|
||||||
let editor: import("@tiptap/core").Editor | undefined;
|
|
||||||
let ready = $state(0);
|
|
||||||
let applying = false;
|
|
||||||
let skipUpdate = true;
|
|
||||||
let menu = $state<OpenMenu>(null);
|
|
||||||
|
|
||||||
function imageFiles(data: DataTransfer | null): File[] {
|
|
||||||
if (!data) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
return [...data.files].filter((file) => file.type.startsWith("image/"));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadUrls(noteRel: string, contents: string): Promise<Map<string, string>> {
|
|
||||||
const urls = new Map<string, string>();
|
|
||||||
for (const block of markdownBlocks(contents)) {
|
|
||||||
if (block.type !== "text") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (const image of findMarkdownImages(block.line)) {
|
|
||||||
if (urls.has(image.src)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (isRemoteImage(image.src)) {
|
|
||||||
urls.set(image.src, image.src.trim());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const primary = resolveImageRel(noteRel, image.src);
|
|
||||||
const base = image.src.replaceAll("\\", "/").split("/").pop() ?? image.src;
|
|
||||||
try {
|
|
||||||
urls.set(image.src, await workspaceReadDataUrl(primary));
|
|
||||||
} catch {
|
|
||||||
try {
|
|
||||||
urls.set(image.src, await workspaceReadDataUrl(base));
|
|
||||||
} catch {
|
|
||||||
urls.set(image.src, "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return urls;
|
|
||||||
}
|
|
||||||
|
|
||||||
$effect(() => {
|
|
||||||
const el = host;
|
|
||||||
if (!el) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let cancelled = false;
|
|
||||||
void Promise.all([
|
|
||||||
import("@tiptap/core"),
|
|
||||||
import("@tiptap/starter-kit"),
|
|
||||||
import("@tiptap/extension-image"),
|
|
||||||
import("@tiptap/extension-link"),
|
|
||||||
import("@tiptap/extension-table"),
|
|
||||||
import("@tiptap/extension-superscript"),
|
|
||||||
import("@tiptap/extension-subscript"),
|
|
||||||
import("@tiptap/extension-task-list"),
|
|
||||||
import("@tiptap/extension-task-item"),
|
|
||||||
import("@tiptap/extension-highlight"),
|
|
||||||
import("./headingMarks"),
|
|
||||||
import("./writerMarks"),
|
|
||||||
]).then(async ([core, starter, imageMod, linkMod, tableMod, supMod, subMod, taskListMod, taskItemMod, highlightMod, marks, extra]) => {
|
|
||||||
if (cancelled || !el.isConnected) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const path = untrack(() => buffer.relPath);
|
|
||||||
const value = untrack(() => buffer.contents ?? "");
|
|
||||||
const urls = await loadUrls(path, value);
|
|
||||||
if (cancelled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
applying = true;
|
|
||||||
const html = renderMarkdownHtml(value, urls) || "<p></p>";
|
|
||||||
const Image = imageMod.default.extend({
|
|
||||||
addAttributes() {
|
|
||||||
return {
|
|
||||||
...this.parent?.(),
|
|
||||||
"data-md-src": { default: null },
|
|
||||||
"data-md-kind": { default: "md" },
|
|
||||||
};
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const Link = linkMod.default.extend({
|
|
||||||
addAttributes() {
|
|
||||||
return {
|
|
||||||
...this.parent?.(),
|
|
||||||
"data-kind": { default: "md" },
|
|
||||||
class: { default: null },
|
|
||||||
};
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const view = new core.Editor({
|
|
||||||
element: el,
|
|
||||||
extensions: [
|
|
||||||
starter.default.configure({ heading: { levels: [1, 2, 3, 4, 5, 6] } }),
|
|
||||||
Image.configure({ inline: false, allowBase64: true }),
|
|
||||||
Link.configure({ openOnClick: false, autolink: false }),
|
|
||||||
tableMod.TableKit.configure({ table: { resizable: false } }),
|
|
||||||
supMod.default,
|
|
||||||
subMod.default,
|
|
||||||
taskListMod.default,
|
|
||||||
taskItemMod.default.configure({ nested: false }),
|
|
||||||
highlightMod.default.configure({ multicolor: false }),
|
|
||||||
extra.MathMark,
|
|
||||||
extra.CommentMark,
|
|
||||||
marks.HeadingMarks,
|
|
||||||
],
|
|
||||||
content: html,
|
|
||||||
editorProps: {
|
|
||||||
attributes: { class: "reading writer-doc", spellcheck: "true" },
|
|
||||||
handleDOMEvents: {
|
|
||||||
contextmenu: (pmView, event) => {
|
|
||||||
if (useSpellingMenu(event, pmView.state.selection.empty)) { menu = null; return false; }
|
|
||||||
event.preventDefault();
|
|
||||||
if (!editor) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const hit = pmView.posAtCoords({ left: event.clientX, top: event.clientY });
|
|
||||||
if (hit) {
|
|
||||||
const { from, to } = editor.state.selection;
|
|
||||||
if (from === to || hit.pos < from || hit.pos > to) {
|
|
||||||
editor.chain().setTextSelection(hit.pos).run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
menu = {
|
|
||||||
x: event.clientX,
|
|
||||||
y: event.clientY,
|
|
||||||
items: writerContextItems(editor),
|
|
||||||
};
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
handleClick: (_view, _pos, event) => {
|
|
||||||
const node = event.target instanceof Element ? event.target.closest("a") : null;
|
|
||||||
if (!node) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const href = node.getAttribute("href") ?? "";
|
|
||||||
const kind = node.getAttribute("data-kind");
|
|
||||||
if (kind === "wiki") {
|
|
||||||
event.preventDefault();
|
|
||||||
onOpenLink?.("wiki", href);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (kind === "md" && !/^https?:\/\//i.test(href)) {
|
|
||||||
event.preventDefault();
|
|
||||||
onOpenLink?.("md", href);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
onCreate: () => {
|
|
||||||
applying = false;
|
|
||||||
queueMicrotask(() => {
|
|
||||||
skipUpdate = false;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
onUpdate: ({ editor: current }) => {
|
|
||||||
if (applying || skipUpdate) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
onChange(htmlToMarkdown(current.getHTML()));
|
|
||||||
},
|
|
||||||
});
|
|
||||||
editor = view;
|
|
||||||
ready += 1;
|
|
||||||
view.commands.focus("end");
|
|
||||||
});
|
|
||||||
return () => {
|
|
||||||
cancelled = true;
|
|
||||||
editor?.destroy();
|
|
||||||
editor = undefined;
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
$effect(() => {
|
|
||||||
void ready;
|
|
||||||
const contents = buffer.contents;
|
|
||||||
const view = editor;
|
|
||||||
if (!view) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (view.isFocused) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const current = htmlToMarkdown(view.getHTML());
|
|
||||||
if (contents === current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
applying = true;
|
|
||||||
void loadUrls(buffer.relPath, contents).then((urls) => {
|
|
||||||
if (editor !== view) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
view.commands.setContent(renderMarkdownHtml(contents, urls) || "<p></p>");
|
|
||||||
applying = false;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<div
|
|
||||||
class="writer-host"
|
|
||||||
role="application"
|
|
||||||
aria-label={$t("Writer")}
|
|
||||||
bind:this={host}
|
|
||||||
ondragover={(event) => {
|
|
||||||
if (imageFiles(event.dataTransfer).length > 0) {
|
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
ondrop={(event) => {
|
|
||||||
const files = imageFiles(event.dataTransfer);
|
|
||||||
if (files.length === 0 || !onInsertImage) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
event.preventDefault();
|
|
||||||
void (async () => {
|
|
||||||
for (const file of files) {
|
|
||||||
const snippet = await onInsertImage(file);
|
|
||||||
const rel = snippet ? /\(([^)]+)\)/.exec(snippet)?.[1] : null;
|
|
||||||
if (!rel || !editor) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const src = await workspaceReadDataUrl(rel);
|
|
||||||
editor.commands.insertContent(
|
|
||||||
`<img src="${src}" alt="${file.name}" data-md-src="${rel}" data-md-kind="md" />`,
|
|
||||||
);
|
|
||||||
} catch {
|
|
||||||
editor.commands.insertContent(snippet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
}}
|
|
||||||
></div>
|
|
||||||
|
|
||||||
{#if menu}
|
|
||||||
<ContextMenu
|
|
||||||
x={menu.x}
|
|
||||||
y={menu.y}
|
|
||||||
items={menu.items}
|
|
||||||
onClose={() => (menu = null)}
|
|
||||||
/>
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<style>
|
|
||||||
.writer-host {
|
|
||||||
position: absolute;
|
|
||||||
inset: 0;
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.writer-host :global(.tiptap),
|
|
||||||
.writer-host :global(.ProseMirror) {
|
|
||||||
height: 100%;
|
|
||||||
overflow: auto;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
import { Extension } from "@tiptap/core";
|
|
||||||
import type { Node as PmNode } from "@tiptap/pm/model";
|
|
||||||
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
|
||||||
import { Decoration, DecorationSet } from "@tiptap/pm/view";
|
|
||||||
|
|
||||||
export function headingMarkText(level: number): string {
|
|
||||||
const n = Math.min(6, Math.max(1, Math.floor(level)));
|
|
||||||
return "#".repeat(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function fenceOpenText(lang: string): string {
|
|
||||||
const name = lang.trim();
|
|
||||||
return name ? `\`\`\`${name}` : "```";
|
|
||||||
}
|
|
||||||
|
|
||||||
export function findInlineCodeRange(doc: PmNode, pos: number): { from: number; to: number } | null {
|
|
||||||
const $pos = doc.resolve(pos);
|
|
||||||
const type = doc.type.schema.marks.code;
|
|
||||||
if (!type || !$pos.parent.inlineContent) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const parentStart = $pos.start();
|
|
||||||
let runFrom = -1;
|
|
||||||
let runTo = -1;
|
|
||||||
let hit: { from: number; to: number } | null = null;
|
|
||||||
$pos.parent.forEach((child, offset) => {
|
|
||||||
const from = parentStart + offset;
|
|
||||||
const to = from + child.nodeSize;
|
|
||||||
if (type.isInSet(child.marks)) {
|
|
||||||
if (runFrom === -1) {
|
|
||||||
runFrom = from;
|
|
||||||
}
|
|
||||||
runTo = to;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (runFrom !== -1 && pos >= runFrom && pos <= runTo) {
|
|
||||||
hit = { from: runFrom, to: runTo };
|
|
||||||
}
|
|
||||||
runFrom = -1;
|
|
||||||
runTo = -1;
|
|
||||||
});
|
|
||||||
if (hit) {
|
|
||||||
return hit;
|
|
||||||
}
|
|
||||||
if (runFrom !== -1 && pos >= runFrom && pos <= runTo) {
|
|
||||||
return { from: runFrom, to: runTo };
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const HeadingMarks = Extension.create({
|
|
||||||
name: "headingMarks",
|
|
||||||
addProseMirrorPlugins() {
|
|
||||||
return [
|
|
||||||
new Plugin({
|
|
||||||
key: new PluginKey("headingMarks"),
|
|
||||||
props: {
|
|
||||||
decorations(state) {
|
|
||||||
const { $from } = state.selection;
|
|
||||||
if ($from.parent.type.name === "codeBlock") {
|
|
||||||
const lang = String($from.parent.attrs.language ?? "");
|
|
||||||
return DecorationSet.create(state.doc, [
|
|
||||||
Decoration.node($from.before(), $from.after(), {
|
|
||||||
class: "is-editing",
|
|
||||||
"data-fence-open": fenceOpenText(lang),
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
if ($from.parent.type.name === "heading") {
|
|
||||||
const level = Number($from.parent.attrs.level ?? 1);
|
|
||||||
return DecorationSet.create(state.doc, [
|
|
||||||
Decoration.node($from.before(), $from.after(), {
|
|
||||||
class: "is-editing",
|
|
||||||
"data-heading-marks": headingMarkText(level),
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
const range = findInlineCodeRange(state.doc, $from.pos);
|
|
||||||
if (!range) {
|
|
||||||
return DecorationSet.empty;
|
|
||||||
}
|
|
||||||
return DecorationSet.create(state.doc, [
|
|
||||||
Decoration.inline(range.from, range.to, { class: "is-editing-ticks" }),
|
|
||||||
]);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
},
|
|
||||||
});
|
|
||||||
@@ -220,19 +220,40 @@ export function tableControls(root: HTMLElement, view: EditorView, initialSource
|
|||||||
// Native cursor movement can precede ProseMirror's selectionchange
|
// Native cursor movement can precede ProseMirror's selectionchange
|
||||||
// observer (especially during key repeat). Test the visible caret.
|
// observer (especially during key repeat). Test the visible caret.
|
||||||
const native = document.getSelection();
|
const native = document.getSelection();
|
||||||
if (native?.anchorNode && native.focusNode && _editorView.dom.contains(native.anchorNode) && _editorView.dom.contains(native.focusNode)) {
|
const readNative = (scroll = false) => {
|
||||||
|
if (!native?.anchorNode || !native.focusNode || !_editorView.dom.contains(native.anchorNode) || !_editorView.dom.contains(native.focusNode)) return;
|
||||||
const anchor = _editorView.posAtDOM(native.anchorNode, native.anchorOffset);
|
const anchor = _editorView.posAtDOM(native.anchorNode, native.anchorOffset);
|
||||||
const head = _editorView.posAtDOM(native.focusNode, native.focusOffset);
|
const head = _editorView.posAtDOM(native.focusNode, native.focusOffset);
|
||||||
if (anchor !== _editorView.state.selection.anchor || head !== _editorView.state.selection.head) editor!.commands.setTextSelection({ from: anchor, to: head });
|
if (anchor === _editorView.state.selection.anchor && head === _editorView.state.selection.head) return;
|
||||||
}
|
const chain = editor!.chain().setTextSelection({ from: anchor, to: head });
|
||||||
if (event.shiftKey || event.ctrlKey || event.metaKey || event.altKey || !_editorView.state.selection.empty) return false;
|
(scroll ? chain.scrollIntoView() : chain).run();
|
||||||
|
};
|
||||||
|
readNative();
|
||||||
|
// ProseMirror reads a native move on the next selectionchange. Its
|
||||||
|
// delayed focus check can run first and restore the old selection,
|
||||||
|
// so the move is lost under load. Move the DOM selection here and
|
||||||
|
// read the result at once. Other shortcuts stay with the browser.
|
||||||
|
const mac = navigator.platform.includes("Mac");
|
||||||
|
const vertical = event.key === "ArrowUp" || event.key === "ArrowDown";
|
||||||
|
const word = mac ? event.altKey : event.ctrlKey;
|
||||||
|
const moveNative = () => {
|
||||||
|
if (!native || event.metaKey || (mac ? event.ctrlKey : event.altKey) || (vertical && word)) return false;
|
||||||
|
native.modify(
|
||||||
|
event.shiftKey ? "extend" : "move",
|
||||||
|
vertical ? (event.key === "ArrowUp" ? "backward" : "forward") : event.key === "ArrowLeft" ? "left" : "right",
|
||||||
|
vertical ? "line" : word ? "word" : "character",
|
||||||
|
);
|
||||||
|
readNative(true);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
if (event.shiftKey || event.ctrlKey || event.metaKey || event.altKey || !_editorView.state.selection.empty) return moveNative();
|
||||||
const direction = event.key === "ArrowLeft" ? "left" : event.key === "ArrowRight" ? "right" : event.key === "ArrowUp" ? "up" : "down";
|
const direction = event.key === "ArrowLeft" ? "left" : event.key === "ArrowRight" ? "right" : event.key === "ArrowUp" ? "up" : "down";
|
||||||
const horizontal = direction === "left" || direction === "right";
|
const horizontal = direction === "left" || direction === "right";
|
||||||
const backwards = direction === "left" || direction === "up";
|
const backwards = direction === "left" || direction === "up";
|
||||||
const { $head } = _editorView.state.selection;
|
const { $head } = _editorView.state.selection;
|
||||||
// Pasted rich text can have several paragraphs within one cell.
|
// Pasted rich text can have several paragraphs within one cell.
|
||||||
if ($head.depth && (backwards ? $head.before(1) > 0 : $head.after(1) < _editorView.state.doc.content.size)) return false;
|
if ($head.depth && (backwards ? $head.before(1) > 0 : $head.after(1) < _editorView.state.doc.content.size)) return moveNative();
|
||||||
if (!_editorView.endOfTextblock(direction)) return false;
|
if (!_editorView.endOfTextblock(direction)) return moveNative();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const cells = [...table!.querySelectorAll<HTMLTableCellElement>("th, td")];
|
const cells = [...table!.querySelectorAll<HTMLTableCellElement>("th, td")];
|
||||||
const next = horizontal
|
const next = horizontal
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
import { Mark } from "@tiptap/core";
|
|
||||||
|
|
||||||
export const MathMark = Mark.create({
|
|
||||||
name: "math",
|
|
||||||
parseHTML() {
|
|
||||||
return [{ tag: "span[data-math]" }];
|
|
||||||
},
|
|
||||||
renderHTML() {
|
|
||||||
return ["span", { "data-math": "1", class: "md-math" }, 0];
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export const CommentMark = Mark.create({
|
|
||||||
name: "comment",
|
|
||||||
parseHTML() {
|
|
||||||
return [{ tag: "span[data-comment]" }];
|
|
||||||
},
|
|
||||||
renderHTML() {
|
|
||||||
return ["span", { "data-comment": "1", class: "md-comment" }, 0];
|
|
||||||
},
|
|
||||||
});
|
|
||||||
@@ -1,144 +0,0 @@
|
|||||||
import { tr } from '../i18n/index.ts';
|
|
||||||
import type { Editor } from "@tiptap/core";
|
|
||||||
import type { MenuItem } from "../layout/menuTypes";
|
|
||||||
|
|
||||||
function selectedText(editor: Editor): string {
|
|
||||||
const { from, to } = editor.state.selection;
|
|
||||||
return editor.state.doc.textBetween(from, to, "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
async function writeClipboard(text: string): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
await navigator.clipboard.writeText(text);
|
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function cutSelection(editor: Editor): Promise<void> {
|
|
||||||
const text = selectedText(editor);
|
|
||||||
if (text && !(await writeClipboard(text))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
editor.chain().focus().deleteSelection().run();
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function copySelection(editor: Editor): Promise<void> {
|
|
||||||
const text = selectedText(editor);
|
|
||||||
if (text) {
|
|
||||||
await writeClipboard(text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function pasteClipboard(editor: Editor): Promise<void> {
|
|
||||||
let text = "";
|
|
||||||
try {
|
|
||||||
text = await navigator.clipboard.readText();
|
|
||||||
} catch {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!text) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
editor.chain().focus().run();
|
|
||||||
editor.view.dispatch(editor.state.tr.insertText(text));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function writerContextItems(editor: Editor): MenuItem[] {
|
|
||||||
const inTable = editor.isActive("table");
|
|
||||||
return [
|
|
||||||
{ kind: "action", label: tr("Cut"), run: () => void cutSelection(editor) },
|
|
||||||
{ kind: "action", label: tr("Copy"), run: () => void copySelection(editor) },
|
|
||||||
{ kind: "action", label: tr("Paste"), run: () => void pasteClipboard(editor) },
|
|
||||||
{ kind: "sep" },
|
|
||||||
{
|
|
||||||
kind: "group",
|
|
||||||
label: tr("Style"),
|
|
||||||
items: [
|
|
||||||
{ kind: "action", label: tr("Heading {level}", { level: 1 }), run: () => editor.chain().focus().toggleHeading({ level: 1 }).run() },
|
|
||||||
{ kind: "action", label: tr("Heading {level}", { level: 2 }), run: () => editor.chain().focus().toggleHeading({ level: 2 }).run() },
|
|
||||||
{ kind: "action", label: tr("Heading {level}", { level: 3 }), run: () => editor.chain().focus().toggleHeading({ level: 3 }).run() },
|
|
||||||
{ kind: "action", label: tr("Heading {level}", { level: 4 }), run: () => editor.chain().focus().toggleHeading({ level: 4 }).run() },
|
|
||||||
{ kind: "action", label: tr("Heading {level}", { level: 5 }), run: () => editor.chain().focus().toggleHeading({ level: 5 }).run() },
|
|
||||||
{ kind: "action", label: tr("Heading {level}", { level: 6 }), run: () => editor.chain().focus().toggleHeading({ level: 6 }).run() },
|
|
||||||
{ kind: "action", label: tr("Body"), run: () => editor.chain().focus().setParagraph().run() },
|
|
||||||
{ kind: "action", label: tr("Quote"), run: () => editor.chain().focus().toggleBlockquote().run() },
|
|
||||||
{ kind: "sep" },
|
|
||||||
{ kind: "action", label: tr("Bullet list"), run: () => editor.chain().focus().toggleBulletList().run() },
|
|
||||||
{ kind: "action", label: tr("Numbered list"), run: () => editor.chain().focus().toggleOrderedList().run() },
|
|
||||||
{ kind: "action", label: tr("Task list"), run: () => editor.chain().focus().toggleTaskList().run() },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
kind: "group",
|
|
||||||
label: tr("Format"),
|
|
||||||
items: [
|
|
||||||
{ kind: "action", label: tr("Bold"), run: () => editor.chain().focus().toggleBold().run() },
|
|
||||||
{ kind: "action", label: tr("Italic"), run: () => editor.chain().focus().toggleItalic().run() },
|
|
||||||
{ kind: "action", label: tr("Strikethrough"), run: () => editor.chain().focus().toggleStrike().run() },
|
|
||||||
{ kind: "action", label: tr("Highlight"), run: () => editor.chain().focus().toggleHighlight().run() },
|
|
||||||
{ kind: "action", label: tr("Code"), run: () => editor.chain().focus().toggleCode().run() },
|
|
||||||
{ kind: "action", label: tr("Math"), run: () => editor.chain().focus().toggleMark("math").run() },
|
|
||||||
{ kind: "action", label: tr("Comment"), run: () => editor.chain().focus().toggleMark("comment").run() },
|
|
||||||
{ kind: "sep" },
|
|
||||||
{ kind: "action", label: tr("Clear formatting"), run: () => editor.chain().focus().unsetAllMarks().run() },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
kind: "group",
|
|
||||||
label: tr("Table"),
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
kind: "action",
|
|
||||||
label: tr("Insert table"),
|
|
||||||
run: () => editor.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(),
|
|
||||||
},
|
|
||||||
{ kind: "sep" },
|
|
||||||
{
|
|
||||||
kind: "action",
|
|
||||||
label: tr("Add row above"),
|
|
||||||
disabled: !inTable,
|
|
||||||
run: () => editor.chain().focus().addRowBefore().run(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
kind: "action",
|
|
||||||
label: tr("Add row below"),
|
|
||||||
disabled: !inTable,
|
|
||||||
run: () => editor.chain().focus().addRowAfter().run(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
kind: "action",
|
|
||||||
label: tr("Delete row"),
|
|
||||||
disabled: !inTable,
|
|
||||||
run: () => editor.chain().focus().deleteRow().run(),
|
|
||||||
},
|
|
||||||
{ kind: "sep" },
|
|
||||||
{
|
|
||||||
kind: "action",
|
|
||||||
label: tr("Add column left"),
|
|
||||||
disabled: !inTable,
|
|
||||||
run: () => editor.chain().focus().addColumnBefore().run(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
kind: "action",
|
|
||||||
label: tr("Add column right"),
|
|
||||||
disabled: !inTable,
|
|
||||||
run: () => editor.chain().focus().addColumnAfter().run(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
kind: "action",
|
|
||||||
label: tr("Delete column"),
|
|
||||||
disabled: !inTable,
|
|
||||||
run: () => editor.chain().focus().deleteColumn().run(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
kind: "action",
|
|
||||||
label: tr("Delete table"),
|
|
||||||
disabled: !inTable,
|
|
||||||
run: () => editor.chain().focus().deleteTable().run(),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
@@ -1206,7 +1206,7 @@
|
|||||||
let refreshTimer: ReturnType<typeof setTimeout>;
|
let refreshTimer: ReturnType<typeof setTimeout>;
|
||||||
const register = (off: () => void) => { if (disposed) off(); else unlisteners.push(off); };
|
const register = (off: () => void) => { if (disposed) off(); else unlisteners.push(off); };
|
||||||
void onWorkspaceEvent((event) => {
|
void onWorkspaceEvent((event) => {
|
||||||
if (event.root !== workspace?.root || event.kind === "access") return;
|
if (event.root !== workspace?.root) return;
|
||||||
if (event.paths.every(path => path.startsWith(".runestone/") || path.split("/").pop()?.startsWith(".runestone-save-"))) return;
|
if (event.paths.every(path => path.startsWith(".runestone/") || path.split("/").pop()?.startsWith(".runestone-save-"))) return;
|
||||||
clearTimeout(refreshTimer);
|
clearTimeout(refreshTimer);
|
||||||
refreshTimer = setTimeout(() => {
|
refreshTimer = setTimeout(() => {
|
||||||
|
|||||||
+2
-2
@@ -2,8 +2,8 @@
|
|||||||
// Type checks for the unit tests. They run in Node with --experimental-strip-types.
|
// Type checks for the unit tests. They run in Node with --experimental-strip-types.
|
||||||
"extends": "../tsconfig.json",
|
"extends": "../tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
// The TipTap packages declare the editor commands that src/lib/editor/writerMenu.ts uses.
|
// The TipTap packages declare the editor commands that the table cell editor uses.
|
||||||
"types": ["node", "@tiptap/starter-kit", "@tiptap/extension-highlight", "@tiptap/extension-table", "@tiptap/extension-task-list"],
|
"types": ["node", "@tiptap/starter-kit", "@tiptap/extension-highlight"],
|
||||||
"checkJs": false
|
"checkJs": false
|
||||||
},
|
},
|
||||||
"include": ["unit/**/*.ts", "../src/**/*.d.ts"],
|
"include": ["unit/**/*.ts", "../src/**/*.d.ts"],
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
import assert from "node:assert/strict";
|
|
||||||
import test from "node:test";
|
|
||||||
import { fenceOpenText, headingMarkText } from "../../../src/lib/editor/headingMarks.ts";
|
|
||||||
|
|
||||||
test("heading marks match the heading level", () => {
|
|
||||||
assert.equal(headingMarkText(1), "#");
|
|
||||||
assert.equal(headingMarkText(3), "###");
|
|
||||||
assert.equal(headingMarkText(6), "######");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("fence open marks include the language", () => {
|
|
||||||
assert.equal(fenceOpenText(""), "```");
|
|
||||||
assert.equal(fenceOpenText("js"), "```js");
|
|
||||||
assert.equal(fenceOpenText(" pp "), "```pp");
|
|
||||||
});
|
|
||||||
@@ -1,244 +0,0 @@
|
|||||||
import assert from "node:assert/strict";
|
|
||||||
import test from "node:test";
|
|
||||||
import {
|
|
||||||
copySelection,
|
|
||||||
cutSelection,
|
|
||||||
pasteClipboard,
|
|
||||||
writerContextItems,
|
|
||||||
} from "../../../src/lib/editor/writerMenu.ts";
|
|
||||||
|
|
||||||
type Harness = {
|
|
||||||
selected: string;
|
|
||||||
inTable: boolean;
|
|
||||||
deleted: boolean;
|
|
||||||
focused: boolean;
|
|
||||||
inserted: string | null;
|
|
||||||
dispatched: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
function fakeEditor(init?: { selected?: string; inTable?: boolean }) {
|
|
||||||
const harness: Harness = {
|
|
||||||
selected: init?.selected ?? "",
|
|
||||||
inTable: init?.inTable ?? false,
|
|
||||||
deleted: false,
|
|
||||||
focused: false,
|
|
||||||
inserted: null,
|
|
||||||
dispatched: false,
|
|
||||||
};
|
|
||||||
const tr = {
|
|
||||||
insertText(text: string) {
|
|
||||||
harness.inserted = text;
|
|
||||||
return tr;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const chainApi = {
|
|
||||||
focus() {
|
|
||||||
harness.focused = true;
|
|
||||||
return chainApi;
|
|
||||||
},
|
|
||||||
deleteSelection() {
|
|
||||||
harness.deleted = true;
|
|
||||||
return chainApi;
|
|
||||||
},
|
|
||||||
run() {
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const editor = {
|
|
||||||
isActive(name: string) {
|
|
||||||
return name === "table" && harness.inTable;
|
|
||||||
},
|
|
||||||
chain() {
|
|
||||||
return chainApi;
|
|
||||||
},
|
|
||||||
state: {
|
|
||||||
selection: { from: 0, to: harness.selected.length },
|
|
||||||
doc: {
|
|
||||||
textBetween() {
|
|
||||||
return harness.selected;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
tr,
|
|
||||||
},
|
|
||||||
view: {
|
|
||||||
dispatch() {
|
|
||||||
harness.dispatched = true;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
return { editor, harness };
|
|
||||||
}
|
|
||||||
|
|
||||||
function withClipboard(
|
|
||||||
impl: { writeText?: (text: string) => Promise<void>; readText?: () => Promise<string> },
|
|
||||||
run: () => Promise<void>,
|
|
||||||
): Promise<void> {
|
|
||||||
const previous = Object.getOwnPropertyDescriptor(globalThis, "navigator");
|
|
||||||
Object.defineProperty(globalThis, "navigator", {
|
|
||||||
configurable: true,
|
|
||||||
value: { clipboard: impl },
|
|
||||||
});
|
|
||||||
return run().finally(() => {
|
|
||||||
if (previous) {
|
|
||||||
Object.defineProperty(globalThis, "navigator", previous);
|
|
||||||
} else {
|
|
||||||
Reflect.deleteProperty(globalThis, "navigator");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function actionLabels(items: ReturnType<typeof writerContextItems>): string[] {
|
|
||||||
const labels: string[] = [];
|
|
||||||
for (const item of items) {
|
|
||||||
if (item.kind === "action") {
|
|
||||||
labels.push(item.label);
|
|
||||||
} else if (item.kind === "group") {
|
|
||||||
for (const sub of item.items) {
|
|
||||||
if (sub.kind === "action") {
|
|
||||||
labels.push(sub.label);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return labels;
|
|
||||||
}
|
|
||||||
|
|
||||||
test("paste inserts clipboard text without parsing tags", async () => {
|
|
||||||
const { editor, harness } = fakeEditor();
|
|
||||||
await withClipboard(
|
|
||||||
{ readText: async () => "<b>bold</b>" },
|
|
||||||
async () => {
|
|
||||||
await pasteClipboard(editor as never);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
assert.equal(harness.focused, true);
|
|
||||||
assert.equal(harness.dispatched, true);
|
|
||||||
assert.equal(harness.inserted, "<b>bold</b>");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("paste swallows clipboard errors", async () => {
|
|
||||||
const { editor, harness } = fakeEditor();
|
|
||||||
await withClipboard(
|
|
||||||
{
|
|
||||||
readText: async () => {
|
|
||||||
throw new Error("denied");
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async () => {
|
|
||||||
await pasteClipboard(editor as never);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
assert.equal(harness.dispatched, false);
|
|
||||||
assert.equal(harness.inserted, null);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("cut keeps the selection when clipboard write fails", async () => {
|
|
||||||
const { editor, harness } = fakeEditor({ selected: "gone" });
|
|
||||||
await withClipboard(
|
|
||||||
{
|
|
||||||
writeText: async () => {
|
|
||||||
throw new Error("denied");
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async () => {
|
|
||||||
await cutSelection(editor as never);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
assert.equal(harness.deleted, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("copy swallows clipboard errors", async () => {
|
|
||||||
const { editor } = fakeEditor({ selected: "keep" });
|
|
||||||
let attempted = false;
|
|
||||||
await withClipboard(
|
|
||||||
{
|
|
||||||
writeText: async () => {
|
|
||||||
attempted = true;
|
|
||||||
throw new Error("denied");
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async () => {
|
|
||||||
await copySelection(editor as never);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
assert.equal(attempted, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("cut writes selected text then deletes it", async () => {
|
|
||||||
const { editor, harness } = fakeEditor({ selected: "clip" });
|
|
||||||
let written = "";
|
|
||||||
await withClipboard(
|
|
||||||
{
|
|
||||||
writeText: async (text) => {
|
|
||||||
written = text;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
async () => {
|
|
||||||
await cutSelection(editor as never);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
assert.equal(written, "clip");
|
|
||||||
assert.equal(harness.deleted, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("table row and column actions disable outside a table", () => {
|
|
||||||
const { editor } = fakeEditor({ inTable: false });
|
|
||||||
const items = writerContextItems(editor as never);
|
|
||||||
const table = items.find((item) => item.kind === "group" && item.label === "Table");
|
|
||||||
assert.equal(table?.kind, "group");
|
|
||||||
if (table?.kind !== "group") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const byLabel = new Map(
|
|
||||||
table.items.filter((item) => item.kind === "action").map((item) => [item.label, item]),
|
|
||||||
);
|
|
||||||
assert.equal(byLabel.get("Insert table")?.disabled, undefined);
|
|
||||||
assert.equal(byLabel.get("Add row above")?.disabled, true);
|
|
||||||
assert.equal(byLabel.get("Delete table")?.disabled, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("table row and column actions enable inside a table", () => {
|
|
||||||
const { editor } = fakeEditor({ inTable: true });
|
|
||||||
const items = writerContextItems(editor as never);
|
|
||||||
const table = items.find((item) => item.kind === "group" && item.label === "Table");
|
|
||||||
assert.equal(table?.kind, "group");
|
|
||||||
if (table?.kind !== "group") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const byLabel = new Map(
|
|
||||||
table.items.filter((item) => item.kind === "action").map((item) => [item.label, item]),
|
|
||||||
);
|
|
||||||
assert.equal(byLabel.get("Insert table")?.disabled, undefined);
|
|
||||||
assert.equal(byLabel.get("Add row above")?.disabled, false);
|
|
||||||
assert.equal(byLabel.get("Delete table")?.disabled, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("style and format actions cover the requested commands", () => {
|
|
||||||
const { editor } = fakeEditor();
|
|
||||||
const labels = actionLabels(writerContextItems(editor as never));
|
|
||||||
for (const label of [
|
|
||||||
"Cut",
|
|
||||||
"Copy",
|
|
||||||
"Paste",
|
|
||||||
"Heading 1",
|
|
||||||
"Heading 6",
|
|
||||||
"Body",
|
|
||||||
"Quote",
|
|
||||||
"Bullet list",
|
|
||||||
"Numbered list",
|
|
||||||
"Task list",
|
|
||||||
"Bold",
|
|
||||||
"Italic",
|
|
||||||
"Strikethrough",
|
|
||||||
"Highlight",
|
|
||||||
"Code",
|
|
||||||
"Math",
|
|
||||||
"Comment",
|
|
||||||
"Clear formatting",
|
|
||||||
"Insert table",
|
|
||||||
]) {
|
|
||||||
assert.equal(labels.includes(label), true, label);
|
|
||||||
}
|
|
||||||
assert.equal(labels.includes("Superscript"), false);
|
|
||||||
assert.equal(labels.includes("Subscript"), false);
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user