Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(ckeditor5): fold collapsible into the aggregate package
Elian Doran535c6c7dad36
diff --git a/packages/ckeditor5-collapsible/package.json b/packages/ckeditor5-collapsible/package.jsondeleted file mode 100644index fd655b0405..0000000000--- a/packages/ckeditor5-collapsible/package.json+++ /dev/null@@ -1,41 +0,0 @@-{- "name": "@triliumnext/ckeditor5-collapsible",- "version": "1.0.0",- "description": "Collapsible block (HTML <details>/<summary>) feature for CKEditor 5.",- "keywords": [- "ckeditor",- "ckeditor5",- "ckeditor 5",- "ckeditor5-feature",- "ckeditor5-plugin",- "collapsible",- "details"- ],- "type": "module",- "main": "src/index.ts",- "engines": {- "node": ">=18.0.0",- "npm": ">=5.7.1"- },- "devDependencies": {- "ckeditor5": "48.3.1",- "happy-dom": "20.11.1",- "typescript": "6.0.3",- "vite-plugin-svgo": "2.0.0",- "vitest": "4.1.10"- },- "dependencies": {- "@triliumnext/ckeditor5-utils": "workspace:*",- "@triliumnext/commons": "workspace:*"- },- "peerDependencies": {- "bootstrap": "^5.3.0",- "ckeditor5": "48.3.1"- },- "author": "Trilium contributors",- "license": "GPL-2.0-or-later",- "scripts": {- "test": "vitest",- "test:debug": "vitest --inspect-brk --no-file-parallelism"- }-}diff --git a/packages/ckeditor5-collapsible/src/augmentation.ts b/packages/ckeditor5-collapsible/src/augmentation.tsdeleted file mode 100644index 5f069c3355..0000000000--- a/packages/ckeditor5-collapsible/src/augmentation.ts+++ /dev/null@@ -1,16 +0,0 @@-import CollapsibleCommand from "./collapsible-command.js";-import CollapsibleEditing from "./collapsible-editing.js";-import CollapsibleUI from "./collapsible-ui.js";-import type { Collapsible } from "./index.js";--declare module "ckeditor5" {- interface PluginsMap {- [Collapsible.pluginName]: Collapsible;- [CollapsibleEditing.pluginName]: CollapsibleEditing;- [CollapsibleUI.pluginName]: CollapsibleUI;- }-- interface CommandsMap {- collapsible: CollapsibleCommand;- }-}diff --git a/packages/ckeditor5-collapsible/src/collapsible.ts b/packages/ckeditor5-collapsible/src/collapsible.tsdeleted file mode 100644index afd72d4b9c..0000000000--- a/packages/ckeditor5-collapsible/src/collapsible.ts+++ /dev/null@@ -1,16 +0,0 @@-import { Plugin } from "ckeditor5";--import CollapsibleEditing from "./collapsible-editing.js";-import CollapsibleUI from "./collapsible-ui.js";--export default class Collapsible extends Plugin {-- public static get requires() {- return [CollapsibleEditing, CollapsibleUI] as const;- }-- public static get pluginName() {- return "Collapsible" as const;- }--}diff --git a/packages/ckeditor5-collapsible/src/index.ts b/packages/ckeditor5-collapsible/src/index.tsdeleted file mode 100644index d8588c29d8..0000000000--- a/packages/ckeditor5-collapsible/src/index.ts+++ /dev/null@@ -1,12 +0,0 @@-import collapsibleIcon from "../theme/icons/collapsible.svg?raw";-import "./augmentation.js";-import "../theme/collapsible.css";--export { default as Collapsible } from "./collapsible.js";-export { default as CollapsibleEditing } from "./collapsible-editing.js";-export { default as CollapsibleUI } from "./collapsible-ui.js";-export type { default as CollapsibleCommand } from "./collapsible-command.js";--export const icons = {- collapsibleIcon-};diff --git a/packages/ckeditor5-collapsible/tsconfig.json b/packages/ckeditor5-collapsible/tsconfig.jsondeleted file mode 100644index 4b991ad444..0000000000--- a/packages/ckeditor5-collapsible/tsconfig.json+++ /dev/null@@ -1,32 +0,0 @@-{- "compilerOptions": {- "types": [],- "lib": [- "ES2019",- "ES2020.String",- "DOM",- "DOM.Iterable"- ],- "noImplicitAny": true,- "noImplicitOverride": true,- "composite": true,- "strict": true,- "target": "es2019",- "sourceMap": true,- "allowJs": true,- "moduleDetection": "force",- "moduleResolution": "NodeNext",- "module": "NodeNext",- "skipLibCheck": true,- "noUncheckedSideEffectImports": false,- "outDir": "out-tsc",- "typeRoots": [- "typings",- "node_modules/@types"- ]- },- "include": [- "./src",- "./typings"- ]-}diff --git a/packages/ckeditor5-collapsible/tsconfig.test.json b/packages/ckeditor5-collapsible/tsconfig.test.jsondeleted file mode 100644index aae1597bdb..0000000000--- a/packages/ckeditor5-collapsible/tsconfig.test.json+++ /dev/null@@ -1,14 +0,0 @@-{- "extends": "./tsconfig.json",- "compilerOptions": {- "types": [- "vitest/globals"- ],- "sourceMap": true- },- "include": [- "./src",- "./tests",- "./typings"- ]-}diff --git a/packages/ckeditor5-collapsible/typings/types.d.ts b/packages/ckeditor5-collapsible/typings/types.d.tsdeleted file mode 100644index 09be57e1c9..0000000000--- a/packages/ckeditor5-collapsible/typings/types.d.ts+++ /dev/null@@ -1,4 +0,0 @@-declare module '*.svg?raw' {- const content: string;- export default content;-}diff --git a/packages/ckeditor5-collapsible/vitest.config.ts b/packages/ckeditor5-collapsible/vitest.config.tsdeleted file mode 100644index 1e3f43ac10..0000000000--- a/packages/ckeditor5-collapsible/vitest.config.ts+++ /dev/null@@ -1,17 +0,0 @@-import { defineConfig } from "vitest/config";-import svg from "vite-plugin-svgo";--export default defineConfig({- plugins: [- svg()- ],- test: {- environment: "happy-dom",- include: [- "tests/**/*.[jt]s"- ],- globals: true,- watch: false,- passWithNoTests: true- }-});diff --git a/packages/ckeditor5/package.json b/packages/ckeditor5/package.jsonindex d3327016e9..a0c7ca84e8 100644--- a/packages/ckeditor5/package.json+++ b/packages/ckeditor5/package.json@@ -15,7 +15,6 @@},"dependencies": {"@triliumnext/commons": "workspace:*",- "@triliumnext/ckeditor5-collapsible": "workspace:*","@triliumnext/ckeditor5-utils": "workspace:*","@triliumnext/ckeditor5-footnotes": "workspace:*","@triliumnext/ckeditor5-math": "workspace:*",diff --git a/packages/ckeditor5/src/extra_slash_commands.ts b/packages/ckeditor5/src/extra_slash_commands.tsindex 53a47591d8..28c430b54c 100644--- a/packages/ckeditor5/src/extra_slash_commands.ts+++ b/packages/ckeditor5/src/extra_slash_commands.ts@@ -12,7 +12,7 @@ import { COMMAND_NAME as INCLUDE_NOTE_COMMAND } from './plugins/includenote.js';import { COMMAND_NAME as MARKDOWN_IMPORT_COMMAND } from './plugins/markdownimport.js';import { ADMONITION_TYPES } from "./plugins/admonition/admonition_ui.js";import type { AdmonitionType } from "./plugins/admonition/admonition_command.js";-import { icons as collapsibleIcons } from '@triliumnext/ckeditor5-collapsible';+import collapsibleIcon from './icons/collapsible.svg?raw';import dateTimeIcon from './icons/date-time.svg?raw';import internalLinkIcon from './icons/trilium.svg?raw';import noteIcon from './icons/note.svg?raw';@@ -45,7 +45,7 @@ export default function buildExtraCommands(title: "Collapsible block",description: t("slash_commands.collapsible_description"),aliases: [ "details", "fold", "toggle" ],- icon: collapsibleIcons.collapsibleIcon,+ icon: collapsibleIcon,commandName: "collapsible"},{diff --git a/packages/ckeditor5-collapsible/theme/icons/collapsible.svg b/packages/ckeditor5/src/icons/collapsible.svgsimilarity index 100%rename from packages/ckeditor5-collapsible/theme/icons/collapsible.svgrename to packages/ckeditor5/src/icons/collapsible.svgdiff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.tsindex 56e9f0f238..fc89068d7e 100644--- a/packages/ckeditor5/src/plugins.ts+++ b/packages/ckeditor5/src/plugins.ts@@ -26,7 +26,7 @@ import SyntaxHighlighting from "./plugins/syntax_highlighting/index.js";import Kbd from "./plugins/keyboard_marker/keyboard_marker.js";import { Mermaid } from "@triliumnext/ckeditor5-mermaid";import Admonition from "./plugins/admonition/admonition.js";-import { Collapsible } from "@triliumnext/ckeditor5-collapsible";+import Collapsible from "./plugins/collapsible/collapsible.js";import { Footnotes } from "@triliumnext/ckeditor5-footnotes";import { Math, AutoformatMath } from "@triliumnext/ckeditor5-math";import CopyAnchorLinkButton from "./plugins/copy_anchor_link.js";diff --git a/packages/ckeditor5/src/plugins/collapsible/block_drag_handle.spec.ts b/packages/ckeditor5/src/plugins/collapsible/block_drag_handle.spec.tsnew file mode 100644index 0000000000..a4c1c848de--- /dev/null+++ b/packages/ckeditor5/src/plugins/collapsible/block_drag_handle.spec.ts@@ -0,0 +1,239 @@+import { _getModelData as getModelData, _setModelData as setModelData, ClassicEditor, Essentials, Paragraph } from "ckeditor5";+import { beforeEach, describe, expect, it, vi } from "vitest";++import { createTestEditor, getEditorElement } from "../../../test/editor-kit.js";+import BlockDragHandle from "./block_drag_handle.js";+import CollapsibleEditing from "./collapsible_editing.js";++const INDICATOR_CLASS = "test-drop-indicator";++describe("BlockDragHandle", () => {+ let editor: ClassicEditor;+ let handle: BlockDragHandle;+ let root: HTMLElement;++ /** Centre of the DOM element rendering the nth top-level block. */+ function blockCentre(index: number): { x: number; y: number } {+ const rect = topLevelDom(index).getBoundingClientRect();+ return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };+ }++ function topLevelDom(index: number): HTMLElement {+ const child = root.children[index];+ if (!(child instanceof HTMLElement)) {+ throw new Error(`No top-level block rendered at index ${index}.`);+ }+ return child;+ }++ function modelBlock(index: number) {+ const block = editor.model.document.getRoot()?.getChild(index);+ if (!block?.is("element")) {+ throw new Error(`No model block at index ${index}.`);+ }+ return block;+ }++ function mouse(type: "mousemove" | "mouseup", x: number, y: number): void {+ document.dispatchEvent(new MouseEvent(type, { clientX: x, clientY: y, bubbles: true }));+ }++ function indicator(): HTMLElement | null {+ return document.querySelector(`.${INDICATOR_CLASS}`);+ }++ beforeEach(async () => {+ editor = await createTestEditor([Essentials, Paragraph, CollapsibleEditing]);+ // The editable needs real layout for elementFromPoint()/getBoundingClientRect().+ getEditorElement(editor).style.cssText = "width: 600px; position: absolute; top: 0; left: 0;";+ root = editor.editing.view.getDomRoot() as HTMLElement;+ handle = new BlockDragHandle({ editor, indicatorClass: INDICATOR_CLASS });+ });++ describe("click vs drag", () => {+ it("treats a mouseup below the movement threshold as a click", () => {+ setModelData(editor.model, "<paragraph>one[]</paragraph><paragraph>two</paragraph>");+ const onClick = vi.fn();+ const clickHandle = new BlockDragHandle({ editor, indicatorClass: INDICATOR_CLASS, onClick });+ const block = modelBlock(0);++ clickHandle.start(100, 100, block, root);+ // Under the 4px threshold — jitter, not a drag.+ mouse("mousemove", 101, 101);+ expect(indicator()).toBeNull();++ mouse("mouseup", 101, 101);++ expect(onClick).toHaveBeenCalledWith(block);+ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<paragraph>one</paragraph><paragraph>two</paragraph>"+ );+ });++ it("does not require an onClick callback to be supplied", () => {+ setModelData(editor.model, "<paragraph>one[]</paragraph>");++ handle.start(100, 100, modelBlock(0), root);+ expect(() => mouse("mouseup", 100, 100)).not.toThrow();+ });++ it("shows the drop indicator once the pointer crosses the threshold", () => {+ setModelData(editor.model, "<paragraph>one[]</paragraph><paragraph>two</paragraph>");+ const target = blockCentre(1);++ handle.start(100, 100, modelBlock(0), root);+ mouse("mousemove", target.x, target.y);++ const bar = indicator();+ expect(bar).not.toBeNull();+ expect(bar?.style.display).toBe("block");+ expect(bar?.style.width).not.toBe("");+ });+ });++ describe("dropping", () => {+ it("moves the block after the target when dropped on its lower half", () => {+ setModelData(editor.model, "<paragraph>one[]</paragraph><paragraph>two</paragraph>");+ const rect = topLevelDom(1).getBoundingClientRect();+ const lowerHalf = { x: rect.left + 5, y: rect.bottom - 2 };++ handle.start(0, 0, modelBlock(0), root);+ mouse("mousemove", lowerHalf.x, lowerHalf.y);+ mouse("mouseup", lowerHalf.x, lowerHalf.y);++ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<paragraph>two</paragraph><paragraph>one</paragraph>"+ );+ });++ it("moves the block before the target when dropped on its upper half", () => {+ setModelData(editor.model, "<paragraph>one</paragraph><paragraph>two[]</paragraph>");+ const rect = topLevelDom(0).getBoundingClientRect();+ const upperHalf = { x: rect.left + 5, y: rect.top + 1 };++ handle.start(0, 0, modelBlock(1), root);+ mouse("mousemove", upperHalf.x, upperHalf.y);+ mouse("mouseup", upperHalf.x, upperHalf.y);++ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<paragraph>two</paragraph><paragraph>one</paragraph>"+ );+ });++ it("ignores a drop onto the dragged block itself", () => {+ setModelData(editor.model, "<paragraph>one[]</paragraph><paragraph>two</paragraph>");+ const self = blockCentre(0);++ handle.start(0, 0, modelBlock(0), root);+ mouse("mousemove", self.x, self.y);+ mouse("mouseup", self.x, self.y);++ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<paragraph>one</paragraph><paragraph>two</paragraph>"+ );+ });++ it("rejects a drop into the dragged block's own subtree", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>inside[]</paragraph></details>"+ );+ const inner = root.querySelector("details p");+ if (!(inner instanceof HTMLElement)) {+ throw new Error("Expected a paragraph inside the collapsible.");+ }+ const rect = inner.getBoundingClientRect();++ handle.start(0, 0, modelBlock(0), root);+ mouse("mousemove", rect.left + 5, rect.top + 2);+ mouse("mouseup", rect.left + 5, rect.top + 2);++ // Still a single top-level <details>; nothing was moved into itself.+ expect(editor.model.document.getRoot()?.childCount).toBe(1);+ });++ it("does nothing when the drop lands outside any block", () => {+ setModelData(editor.model, "<paragraph>one[]</paragraph><paragraph>two</paragraph>");++ handle.start(0, 0, modelBlock(0), root);+ mouse("mousemove", 50, 50);+ // Far below the document — elementFromPoint finds nothing mappable.+ mouse("mouseup", -500, -500);++ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<paragraph>one</paragraph><paragraph>two</paragraph>"+ );+ });+ });++ describe("target refinement", () => {+ it("re-routes the target through refineTarget", () => {+ setModelData(+ editor.model,+ "<paragraph>one[]</paragraph>" ++ "<details open=\"true\"><summary>T</summary><paragraph>body</paragraph></details>"+ );+ // A hit anywhere inside the collapsible retargets to the <details> itself.+ const refineTarget = vi.fn((model) => {+ for (let node = model; node; node = node.parent) {+ if (node.is?.("element", "details")) {+ return node;+ }+ }+ return model;+ });+ const refining = new BlockDragHandle({ editor, indicatorClass: INDICATOR_CLASS, refineTarget });++ const body = root.querySelector("details p");+ if (!(body instanceof HTMLElement)) {+ throw new Error("Expected a paragraph inside the collapsible.");+ }+ const rect = body.getBoundingClientRect();++ refining.start(0, 0, modelBlock(0), root);+ mouse("mousemove", rect.left + 5, rect.top + 2);+ mouse("mouseup", rect.left + 5, rect.bottom + 200);++ expect(refineTarget).toHaveBeenCalled();+ });+ });++ describe("lifecycle", () => {+ it("cancel() removes the indicator and stops tracking", () => {+ setModelData(editor.model, "<paragraph>one[]</paragraph><paragraph>two</paragraph>");+ const target = blockCentre(1);++ handle.start(0, 0, modelBlock(0), root);+ mouse("mousemove", target.x, target.y);+ expect(indicator()).not.toBeNull();++ handle.cancel();++ expect(indicator()).toBeNull();+ // Further events are inert — the model must not change.+ mouse("mouseup", target.x, target.y);+ expect(getModelData(editor.model, { withoutSelection: true })).toBe(+ "<paragraph>one</paragraph><paragraph>two</paragraph>"+ );+ });++ it("cancel() is safe when no drag is in progress", () => {+ expect(() => handle.cancel()).not.toThrow();+ });++ it("starting a new drag cancels the previous one", () => {+ setModelData(editor.model, "<paragraph>one[]</paragraph><paragraph>two</paragraph>");+ const target = blockCentre(1);++ handle.start(0, 0, modelBlock(0), root);+ mouse("mousemove", target.x, target.y);+ expect(document.querySelectorAll(`.${INDICATOR_CLASS}`)).toHaveLength(1);++ handle.start(0, 0, modelBlock(1), root);+ expect(indicator()).toBeNull();++ mouse("mousemove", target.x, target.y);+ expect(document.querySelectorAll(`.${INDICATOR_CLASS}`)).toHaveLength(1);+ });+ });+});diff --git a/packages/ckeditor5-collapsible/src/block-drag-handle.ts b/packages/ckeditor5/src/plugins/collapsible/block_drag_handle.tssimilarity index 100%rename from packages/ckeditor5-collapsible/src/block-drag-handle.tsrename to packages/ckeditor5/src/plugins/collapsible/block_drag_handle.tsdiff --git a/packages/ckeditor5/src/plugins/collapsible/collapsible.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible.tsnew file mode 100644index 0000000000..3d94d2ac3b--- /dev/null+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible.ts@@ -0,0 +1,35 @@+import { Plugin } from "ckeditor5";++import "../../theme/collapsible.css";+import type CollapsibleCommand from "./collapsible_command.js";+import CollapsibleEditing from "./collapsible_editing.js";+import CollapsibleUI from "./collapsible_ui.js";++/**+ * The collapsible block feature: a `<details>` / `<summary>` pair holding block content.+ *+ * This is a "glue" plugin which loads {@link CollapsibleEditing} and {@link CollapsibleUI}.+ */+export default class Collapsible extends Plugin {++ public static get requires() {+ return [CollapsibleEditing, CollapsibleUI] as const;+ }++ public static get pluginName() {+ return "Collapsible" as const;+ }++}++declare module "ckeditor5" {+ interface PluginsMap {+ [Collapsible.pluginName]: Collapsible;+ [CollapsibleEditing.pluginName]: CollapsibleEditing;+ [CollapsibleUI.pluginName]: CollapsibleUI;+ }++ interface CommandsMap {+ collapsible: CollapsibleCommand;+ }+}diff --git a/packages/ckeditor5-collapsible/src/collapsible-command.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_command.tssimilarity index 87%rename from packages/ckeditor5-collapsible/src/collapsible-command.tsrename to packages/ckeditor5/src/plugins/collapsible/collapsible_command.tsindex df4a153bb5..f2485021eb 100644--- a/packages/ckeditor5-collapsible/src/collapsible-command.ts+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_command.ts@@ -73,9 +73,15 @@ export default class CollapsibleCommand extends Command {for (const child of [...fragment.getChildren()]) {// `child.is("element")` is true for inline elements too (soft breaks,// inline widgets, …), and dropping those directly under <details>- // violates its block-only schema. Gate on isBlock so they fall through- // to `pending` and get wrapped in the next flushed paragraph.- if (child.is("element") && model.schema.isBlock(child)) {+ // violates its block-only schema, so they must fall through to `pending`+ // and get wrapped in the next flushed paragraph.+ //+ // Ask the schema what may sit directly inside <details> rather than testing+ // `isBlock`: a container — a nested <details> among them — reports+ // `isBlock: false`, so an isBlock gate sent whole collapsibles down the+ // inline path, where their summary and body text were flattened together+ // into one paragraph instead of being unwrapped by `appendToBody`.+ if (child.is("element") && model.schema.checkChild(details, child)) {flushPending();appendToBody(child);} else {diff --git a/packages/ckeditor5-collapsible/src/collapsible-editing.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.tssimilarity index 99%rename from packages/ckeditor5-collapsible/src/collapsible-editing.tsrename to packages/ckeditor5/src/plugins/collapsible/collapsible_editing.tsindex b31094800a..daea1cd8f2 100644--- a/packages/ckeditor5-collapsible/src/collapsible-editing.ts+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_editing.ts@@ -1,8 +1,8 @@import { Plugin, Enter, Delete, enableViewPlaceholder, env, type ViewDocumentEnterEvent, type ViewDocumentDeleteEvent, type ViewDocumentArrowKeyEvent } from "ckeditor5";import { formatShortcut, joinShortcut } from "@triliumnext/commons";import { ContentHintManager, type HintHandle } from "@triliumnext/ckeditor5-utils";-import BlockDragHandle from "./block-drag-handle.js";-import CollapsibleCommand from "./collapsible-command.js";+import BlockDragHandle from "./block_drag_handle.js";+import CollapsibleCommand from "./collapsible_command.js";import { OPEN_ATTRIBUTE, TRANSIENT_OPEN_ATTRIBUTE } from "./constants.js";type TranslateFn = (key: string, params?: Record<string, unknown>) => string;diff --git a/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.spec.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.spec.tsnew file mode 100644index 0000000000..0994114fbf--- /dev/null+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.spec.ts@@ -0,0 +1,79 @@+import { _setModelData as setModelData, ButtonView, ClassicEditor, Essentials, Paragraph } from "ckeditor5";+import { describe, expect, it, vi } from "vitest";++import { createTestEditor } from "../../../test/editor-kit.js";+import Collapsible from "./collapsible.js";+import CollapsibleEditing from "./collapsible_editing.js";+import CollapsibleUI from "./collapsible_ui.js";++function createButton(editor: ClassicEditor): ButtonView {+ return editor.ui.componentFactory.create("collapsible") as unknown as ButtonView;+}++describe("CollapsibleUI", () => {+ it("registers a labelled toolbar button", async () => {+ const editor = await createTestEditor([Essentials, Paragraph, Collapsible]);+ const button = createButton(editor);++ expect(button).toBeInstanceOf(ButtonView);+ expect(button.icon).toContain("<svg");+ expect(button.tooltip).toBe(true);+ expect(CollapsibleUI.pluginName).toBe("CollapsibleUI");+ });++ it("falls back to the raw key when the host supplies no translate callback", async () => {+ const editor = await createTestEditor([Essentials, Paragraph, Collapsible]);++ expect(createButton(editor).label).toBe("text-editor.collapsible-button-label");+ });++ it("labels the button through the host's translate callback when one is configured", async () => {+ const translate = vi.fn(() => "Collapsible block");+ const editor = await createTestEditor([Essentials, Paragraph, Collapsible], { translate });++ expect(createButton(editor).label).toBe("Collapsible block");+ expect(translate).toHaveBeenCalledWith("text-editor.collapsible-button-label");+ });++ it("executes the command and returns focus to the editing view", async () => {+ const editor = await createTestEditor([Essentials, Paragraph, Collapsible]);+ setModelData(editor.model, "<paragraph>foo[]</paragraph>");+ const focusSpy = vi.spyOn(editor.editing.view, "focus");+ const executeSpy = vi.spyOn(editor, "execute");++ createButton(editor).fire("execute");++ expect(executeSpy).toHaveBeenCalledWith("collapsible");+ expect(focusSpy).toHaveBeenCalled();+ });++ it("tracks the command's enabled state", async () => {+ const editor = await createTestEditor([Essentials, Paragraph, Collapsible]);+ const button = createButton(editor);+ const command = editor.commands.get("collapsible");++ expect(button.isEnabled).toBe(true);++ command?.forceDisabled("test");++ expect(button.isEnabled).toBe(false);+ });++ it("still builds a button when the editing plugin is absent, with nothing to bind to", async () => {+ const editor = await createTestEditor([Essentials, Paragraph, CollapsibleUI]);++ expect(editor.commands.get("collapsible")).toBeUndefined();+ expect(createButton(editor)).toBeInstanceOf(ButtonView);+ });++ it("declares the editing plugin as a requirement of the glue plugin", async () => {+ const editor = await createTestEditor([Essentials, Paragraph, Collapsible]);++ expect(Collapsible.requires).toContain(CollapsibleEditing);+ expect(Collapsible.requires).toContain(CollapsibleUI);+ expect(Collapsible.pluginName).toBe("Collapsible");+ expect(editor.plugins.get(Collapsible)).toBeInstanceOf(Collapsible);+ expect(editor.plugins.get(CollapsibleEditing)).toBeInstanceOf(CollapsibleEditing);+ expect(editor.plugins.get(CollapsibleUI)).toBeInstanceOf(CollapsibleUI);+ });+});diff --git a/packages/ckeditor5-collapsible/src/collapsible-ui.ts b/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.tssimilarity index 79%rename from packages/ckeditor5-collapsible/src/collapsible-ui.tsrename to packages/ckeditor5/src/plugins/collapsible/collapsible_ui.tsindex c2232398b3..c49e6542bc 100644--- a/packages/ckeditor5-collapsible/src/collapsible-ui.ts+++ b/packages/ckeditor5/src/plugins/collapsible/collapsible_ui.ts@@ -1,5 +1,5 @@import { ButtonView, Plugin } from "ckeditor5";-import collapsibleIcon from "../theme/icons/collapsible.svg?raw";+import collapsibleIcon from "../../icons/collapsible.svg?raw";export default class CollapsibleUI extends Plugin {@@ -13,7 +13,7 @@ export default class CollapsibleUI extends Plugin {?? ((key: string) => key);editor.ui.componentFactory.add("collapsible", locale => {- const command = editor.commands.get("collapsible")!;+ const command = editor.commands.get("collapsible");const button = new ButtonView(locale);button.set({@@ -22,7 +22,9 @@ export default class CollapsibleUI extends Plugin {tooltip: true});- button.bind("isEnabled").to(command, "isEnabled");+ if (command) {+ button.bind("isEnabled").to(command, "isEnabled");+ }this.listenTo(button, "execute", () => {editor.execute("collapsible");diff --git a/packages/ckeditor5-collapsible/tests/command.ts b/packages/ckeditor5/src/plugins/collapsible/command.spec.tssimilarity index 71%rename from packages/ckeditor5-collapsible/tests/command.tsrename to packages/ckeditor5/src/plugins/collapsible/command.spec.tsindex 8a4addd6da..165d9a4bb3 100644--- a/packages/ckeditor5-collapsible/tests/command.ts+++ b/packages/ckeditor5/src/plugins/collapsible/command.spec.ts@@ -1,6 +1,6 @@import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";+import CollapsibleEditing from "./collapsible_editing.js";describe("CollapsibleCommand", () => {let domElement: HTMLDivElement;@@ -71,6 +71,36 @@ describe("CollapsibleCommand", () => {// new summary is the editable title for the user to fill in).expect(data).not.toContain("<summary>Inner");});++ it("unwraps a whole <details> caught in a wider selection, keeping only its body", () => {+ // Unlike the case above, the selection spans several top-level blocks, so the+ // <details> survives into the copied fragment as a block child and is unwrapped there.+ setModelData(model,+ "<paragraph>[before</paragraph>" ++ "<details><summary>Inner</summary><paragraph>Inner body</paragraph></details>" ++ "<paragraph>after]</paragraph>"+ );++ editor.execute("collapsible");++ const data = getModelData(model, { withoutSelection: true });+ expect((data.match(/<details[ >]/g) ?? []).length).toBe(1);+ expect(data).toContain("<paragraph>before</paragraph>");+ expect(data).toContain("<paragraph>Inner body</paragraph>");+ expect(data).toContain("<paragraph>after</paragraph>");+ expect(data).not.toContain("<summary>Inner");+ });++ it("drops a whitespace-only run rather than wrapping it in an empty paragraph", () => {+ setModelData(model, "<paragraph>[ ]</paragraph><paragraph>keep</paragraph>");++ editor.execute("collapsible");++ const data = getModelData(model, { withoutSelection: true });+ // Body holds the single empty paragraph the command guarantees, not a+ // paragraph wrapping the stray space.+ expect(data).toContain("<details open=\"true\"><summary></summary><paragraph></paragraph></details>");+ });});describe("refresh", () => {diff --git a/packages/ckeditor5-collapsible/src/constants.ts b/packages/ckeditor5/src/plugins/collapsible/constants.tssimilarity index 100%rename from packages/ckeditor5-collapsible/src/constants.tsrename to packages/ckeditor5/src/plugins/collapsible/constants.tsdiff --git a/packages/ckeditor5-collapsible/tests/conversion.ts b/packages/ckeditor5/src/plugins/collapsible/conversion.spec.tssimilarity index 98%rename from packages/ckeditor5-collapsible/tests/conversion.tsrename to packages/ckeditor5/src/plugins/collapsible/conversion.spec.tsindex 0b007e249a..169c3fafce 100644--- a/packages/ckeditor5-collapsible/tests/conversion.ts+++ b/packages/ckeditor5/src/plugins/collapsible/conversion.spec.ts@@ -1,6 +1,6 @@import { ClassicEditor, Essentials, Paragraph, _getModelData as getModelData } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";+import CollapsibleEditing from "./collapsible_editing.js";describe("CollapsibleEditing conversion", () => {let domElement: HTMLDivElement;diff --git a/packages/ckeditor5-collapsible/tests/enter-in-summary.ts b/packages/ckeditor5/src/plugins/collapsible/enter_in_summary.spec.tssimilarity index 98%rename from packages/ckeditor5-collapsible/tests/enter-in-summary.tsrename to packages/ckeditor5/src/plugins/collapsible/enter_in_summary.spec.tsindex 5910f3b025..be200da704 100644--- a/packages/ckeditor5-collapsible/tests/enter-in-summary.ts+++ b/packages/ckeditor5/src/plugins/collapsible/enter_in_summary.spec.ts@@ -1,6 +1,6 @@import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";+import CollapsibleEditing from "./collapsible_editing.js";describe("onEnterInSummary", () => {let domElement: HTMLDivElement;diff --git a/packages/ckeditor5-collapsible/tests/find-reveal.ts b/packages/ckeditor5/src/plugins/collapsible/find_reveal.spec.tssimilarity index 96%rename from packages/ckeditor5-collapsible/tests/find-reveal.tsrename to packages/ckeditor5/src/plugins/collapsible/find_reveal.spec.tsindex 09dfd24e9f..195d519894 100644--- a/packages/ckeditor5-collapsible/tests/find-reveal.ts+++ b/packages/ckeditor5/src/plugins/collapsible/find_reveal.spec.ts@@ -1,7 +1,7 @@import { ClassicEditor, Essentials, FindAndReplace, Paragraph, _getModelData as getModelData } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";-import { TRANSIENT_OPEN_ATTRIBUTE } from "../src/constants.js";+import CollapsibleEditing from "./collapsible_editing.js";+import { TRANSIENT_OPEN_ATTRIBUTE } from "./constants.js";/*** Find-in-note reveal: when the find highlight lands inside a collapsed block the@@ -115,7 +115,7 @@ describe("collapsible find-in-note reveal", () => {// User genuinely opens it via the arrow while the match is highlighted.domRoot().querySelector<HTMLElement>(".trilium-collapsible-arrow")?.dispatchEvent(- new MouseEvent("click", { bubbles: true })+ new MouseEvent("click", { bubbles: true, cancelable: true }));// Highlight moves away (search cleared).editor.plugins.get("FindAndReplaceEditing").state?.clear(model);diff --git a/packages/ckeditor5/src/plugins/collapsible/keyboard_navigation.spec.ts b/packages/ckeditor5/src/plugins/collapsible/keyboard_navigation.spec.tsnew file mode 100644index 0000000000..1edcd5f0e6--- /dev/null+++ b/packages/ckeditor5/src/plugins/collapsible/keyboard_navigation.spec.ts@@ -0,0 +1,216 @@+import { _getModelData as getModelData, _setModelData as setModelData, ClassicEditor, Essentials, Paragraph } from "ckeditor5";+import { beforeEach, describe, expect, it, vi } from "vitest";++import { createTestEditor } from "../../../test/editor-kit.js";+import CollapsibleEditing from "./collapsible_editing.js";++/**+ * Caret movement and deletion around a collapsible: the arrow-up jump into the block above,+ * the two-step delete next to one, and Backspace unwrapping an empty title.+ */+describe("collapsible keyboard navigation", () => {+ let editor: ClassicEditor;++ beforeEach(async () => {+ editor = await createTestEditor([Essentials, Paragraph, CollapsibleEditing]);+ });++ function fireArrowUp(overrides: Record<string, unknown> = {}): ReturnType<typeof vi.fn> {+ const preventDefault = vi.fn();+ editor.editing.view.document.fire("arrowKey", {+ keyCode: 38,+ shiftKey: false,+ domTarget: editor.editing.view.getDomRoot(),+ preventDefault,+ ...overrides+ });+ return preventDefault;+ }++ function fireDelete(direction: "backward" | "forward"): ReturnType<typeof vi.fn> {+ const preventDefault = vi.fn();+ editor.editing.view.document.fire("delete", { direction, unit: "character", preventDefault });+ return preventDefault;+ }++ describe("arrow up into the preceding collapsible", () => {+ it("jumps from a following paragraph into an open collapsible's last block", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body</paragraph></details>" ++ "<paragraph>[]after</paragraph>"+ );++ const preventDefault = fireArrowUp();++ expect(preventDefault).toHaveBeenCalled();+ expect(getModelData(editor.model)).toBe(+ "<details open=\"true\"><summary>T</summary><paragraph>[]body</paragraph></details>" ++ "<paragraph>after</paragraph>"+ );+ });++ it("jumps into a collapsed collapsible's summary instead of its hidden body", () => {+ setModelData(+ editor.model,+ "<details><summary>T</summary><paragraph>body</paragraph></details>" ++ "<paragraph>[]after</paragraph>"+ );++ fireArrowUp();++ expect(getModelData(editor.model)).toBe(+ "<details><summary>T[]</summary><paragraph>body</paragraph></details>" ++ "<paragraph>after</paragraph>"+ );+ });++ it("jumps from a summary into the collapsible directly above it", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>first</summary><paragraph>one</paragraph></details>" ++ "<details open=\"true\"><summary>[]second</summary><paragraph>two</paragraph></details>"+ );++ const preventDefault = fireArrowUp();++ expect(preventDefault).toHaveBeenCalled();+ expect(getModelData(editor.model)).toContain("<paragraph>one[]</paragraph>");+ });++ it("ignores the jump when the caret is not at the start of its block", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body</paragraph></details>" ++ "<paragraph>af[]ter</paragraph>"+ );++ const preventDefault = fireArrowUp();++ expect(preventDefault).not.toHaveBeenCalled();+ });++ it("ignores the jump when the previous sibling is not a collapsible", () => {+ setModelData(editor.model, "<paragraph>before</paragraph><paragraph>[]after</paragraph>");++ const preventDefault = fireArrowUp();++ expect(preventDefault).not.toHaveBeenCalled();+ });++ it("ignores a summary whose collapsible has no previous sibling", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>[]T</summary><paragraph>body</paragraph></details>"+ );++ const preventDefault = fireArrowUp();++ expect(preventDefault).not.toHaveBeenCalled();+ });++ it("ignores keys other than arrow-up, and shift-extended selections", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body</paragraph></details>" ++ "<paragraph>[]after</paragraph>"+ );++ expect(fireArrowUp({ keyCode: 40 })).not.toHaveBeenCalled();+ expect(fireArrowUp({ shiftKey: true })).not.toHaveBeenCalled();+ });++ it("ignores the jump when the selection is not collapsed", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body</paragraph></details>" ++ "<paragraph>[after]</paragraph>"+ );++ expect(fireArrowUp()).not.toHaveBeenCalled();+ });+ });++ describe("two-step delete next to a collapsible", () => {+ // `Delete` (from Essentials) calls preventDefault on the event itself, so whether the+ // collapsible ends up *selected* is the only signal that this handler acted.+ const selectedName = () => editor.model.document.selection.getSelectedElement()?.name;++ it("leaves an empty block alone — the user is deleting the block, not the collapsible", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body</paragraph></details>" ++ "<paragraph>[]</paragraph>"+ );++ fireDelete("backward");++ expect(selectedName()).toBeUndefined();+ });++ it("does nothing when there is no collapsible adjacent to the caret", () => {+ setModelData(editor.model, "<paragraph>before</paragraph>[]<paragraph>after</paragraph>");++ fireDelete("backward");++ expect(selectedName()).toBeUndefined();+ });++ it("does nothing when the selection is not collapsed", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body</paragraph></details>" ++ "<paragraph>[after]</paragraph>"+ );++ fireDelete("backward");++ expect(selectedName()).toBeUndefined();+ });+ });++ describe("backspace in an empty summary", () => {+ it("unwraps the collapsible, keeping the body and parking the caret in it", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>[]</summary><paragraph>body</paragraph></details>"+ );++ const preventDefault = fireDelete("backward");++ expect(preventDefault).toHaveBeenCalled();+ expect(getModelData(editor.model)).toBe("<paragraph>[]body</paragraph>");+ });++ it("replaces a wholly empty collapsible with a fresh paragraph", () => {+ setModelData(editor.model, "<details open=\"true\"><summary>[]</summary></details>");++ fireDelete("backward");++ expect(getModelData(editor.model, { withoutSelection: true })).toBe("<paragraph></paragraph>");+ });++ it("leaves a non-empty title alone", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>[]T</summary><paragraph>body</paragraph></details>"+ );++ fireDelete("backward");++ // The collapsible survives — the handler only unwraps when the title is empty.+ expect(getModelData(editor.model, { withoutSelection: true })).toContain("<summary>T</summary>");+ });++ it("ignores a forward delete in an empty title", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>[]</summary><paragraph>body</paragraph></details>"+ );++ fireDelete("forward");++ expect(getModelData(editor.model, { withoutSelection: true })).toContain("<summary></summary>");+ });+ });+});diff --git a/packages/ckeditor5-collapsible/tests/open-state.ts b/packages/ckeditor5/src/plugins/collapsible/open_state.spec.tssimilarity index 94%rename from packages/ckeditor5-collapsible/tests/open-state.tsrename to packages/ckeditor5/src/plugins/collapsible/open_state.spec.tsindex 5c03d6092d..fe33a79a2b 100644--- a/packages/ckeditor5-collapsible/tests/open-state.ts+++ b/packages/ckeditor5/src/plugins/collapsible/open_state.spec.ts@@ -1,6 +1,6 @@import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";+import CollapsibleEditing from "./collapsible_editing.js";/*** The expanded state is persisted as the `open` model attribute, which round-trips@@ -33,7 +33,7 @@ describe("collapsible open state", () => {const clickArrow = (index = 0) =>domRoot().querySelectorAll<HTMLElement>(".trilium-collapsible-arrow")[index]- .dispatchEvent(new MouseEvent("click", { bubbles: true }));+ .dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));describe("persistence", () => {it("round-trips the state a user leaves behind: collapsed content loads collapsed, expanding writes `open` into the saved HTML, and reloading that HTML comes back expanded", () => {@@ -165,12 +165,20 @@ describe("collapsible open state", () => {expect(getModelData(model, { withoutSelection: true })).toContain("<details open=\"true\">");});- it("keeps the arrow's aria-expanded in sync", () => {+ it("keeps the arrow's aria-expanded in sync", async () => {setModelData(model, "<details><summary>T</summary><paragraph>body</paragraph></details>");const arrow = domRoot().querySelector(".trilium-collapsible-arrow");expect(arrow?.getAttribute("aria-expanded")).toBe("false");+ // `aria-expanded` is written by the `toggle` DOM listener, and the browser fires+ // `toggle` asynchronously after `open` changes — so wait for the event rather than+ // asserting straight after the click.+ const toggled = new Promise<void>((resolve) => {+ detailsDom().addEventListener("toggle", () => resolve(), { once: true });+ });+clickArrow();+ await toggled;expect(arrow?.getAttribute("aria-expanded")).toBe("true");});diff --git a/packages/ckeditor5-collapsible/tests/post-fixer-gap.ts b/packages/ckeditor5/src/plugins/collapsible/post_fixer_gap.spec.tssimilarity index 98%rename from packages/ckeditor5-collapsible/tests/post-fixer-gap.tsrename to packages/ckeditor5/src/plugins/collapsible/post_fixer_gap.spec.tsindex 62e7f8657c..2f51956400 100644--- a/packages/ckeditor5-collapsible/tests/post-fixer-gap.ts+++ b/packages/ckeditor5/src/plugins/collapsible/post_fixer_gap.spec.ts@@ -1,6 +1,6 @@import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";+import CollapsibleEditing from "./collapsible_editing.js";describe("gapPostFixer + onEnterInBody", () => {let domElement: HTMLDivElement;diff --git a/packages/ckeditor5-collapsible/tests/post-fixer-structural.ts b/packages/ckeditor5/src/plugins/collapsible/post_fixer_structural.spec.tssimilarity index 97%rename from packages/ckeditor5-collapsible/tests/post-fixer-structural.tsrename to packages/ckeditor5/src/plugins/collapsible/post_fixer_structural.spec.tsindex d328e10be2..bb320fadbf 100644--- a/packages/ckeditor5-collapsible/tests/post-fixer-structural.ts+++ b/packages/ckeditor5/src/plugins/collapsible/post_fixer_structural.spec.ts@@ -1,6 +1,6 @@import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";+import CollapsibleEditing from "./collapsible_editing.js";describe("structuralPostFixer + bodyExistsPostFixer", () => {let domElement: HTMLDivElement;diff --git a/packages/ckeditor5-collapsible/tests/post-fixer-summary.ts b/packages/ckeditor5/src/plugins/collapsible/post_fixer_summary.spec.tssimilarity index 96%rename from packages/ckeditor5-collapsible/tests/post-fixer-summary.tsrename to packages/ckeditor5/src/plugins/collapsible/post_fixer_summary.spec.tsindex 70dd43e4b2..763aa40997 100644--- a/packages/ckeditor5-collapsible/tests/post-fixer-summary.ts+++ b/packages/ckeditor5/src/plugins/collapsible/post_fixer_summary.spec.ts@@ -1,6 +1,6 @@import { ClassicEditor, Essentials, Paragraph, _setModelData as setModelData, _getModelData as getModelData } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";+import CollapsibleEditing from "./collapsible_editing.js";describe("summaryInvariantPostFixer", () => {let domElement: HTMLDivElement;diff --git a/packages/ckeditor5-collapsible/tests/schema.ts b/packages/ckeditor5/src/plugins/collapsible/schema.spec.tssimilarity index 97%rename from packages/ckeditor5-collapsible/tests/schema.tsrename to packages/ckeditor5/src/plugins/collapsible/schema.spec.tsindex ce1526d4fd..dc200c7abf 100644--- a/packages/ckeditor5-collapsible/tests/schema.ts+++ b/packages/ckeditor5/src/plugins/collapsible/schema.spec.ts@@ -1,6 +1,6 @@import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import CollapsibleEditing from "../src/collapsible-editing.js";+import CollapsibleEditing from "./collapsible_editing.js";describe("CollapsibleEditing schema", () => {let domElement: HTMLDivElement;diff --git a/packages/ckeditor5/src/plugins/collapsible/summary_affordances.spec.ts b/packages/ckeditor5/src/plugins/collapsible/summary_affordances.spec.tsnew file mode 100644index 0000000000..414db971b4--- /dev/null+++ b/packages/ckeditor5/src/plugins/collapsible/summary_affordances.spec.ts@@ -0,0 +1,228 @@+import { _getModelData as getModelData, _setModelData as setModelData, ClassicEditor, ClickObserver, Essentials, Paragraph } from "ckeditor5";+import { beforeEach, describe, expect, it } from "vitest";++import { createTestEditor, getEditorElement } from "../../../test/editor-kit.js";+import CollapsibleEditing from "./collapsible_editing.js";++/**+ * The two non-editable affordances rendered into every `<summary>`: the drag/select handle and+ * the expand/collapse arrow.+ */+describe("collapsible summary affordances", () => {+ let editor: ClassicEditor;+ let root: HTMLElement;++ beforeEach(async () => {+ editor = await createTestEditor([Essentials, Paragraph, CollapsibleEditing]);+ getEditorElement(editor).style.cssText = "width: 600px; position: absolute; top: 0; left: 0;";+ root = editor.editing.view.getDomRoot() as HTMLElement;+ // The click handler listens on the view document; the observer that feeds it is added by+ // other plugins (Link) in the real editor, so a bare test editor has to add it itself.+ editor.editing.view.addObserver(ClickObserver);+ });++ /**+ * Names of the elements the model selection lands on while `run` executes. The handle sets the+ * selection and then focuses the view, and that focus round-trip can move it again — so record+ * what was selected rather than inspecting the end state.+ */+ function selectionsDuring(run: () => void): Array<string | undefined> {+ const seen: Array<string | undefined> = [];+ const record = () => seen.push(editor.model.document.selection.getSelectedElement()?.name);+ editor.model.document.selection.on("change:range", record);+ try {+ run();+ } finally {+ editor.model.document.selection.off("change:range", record);+ }+ return seen;+ }++ const handleDom = (index = 0) =>+ root.querySelectorAll<HTMLElement>(".trilium-collapsible-handle")[index];+ const arrowDom = (index = 0) =>+ root.querySelectorAll<HTMLElement>(".trilium-collapsible-arrow")[index];++ function key(target: HTMLElement, key: string): void {+ target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true }));+ }++ describe("the select/drag handle", () => {+ it("selects the whole collapsible when clicked without dragging", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body[]</paragraph></details>"+ );+ const handle = handleDom();+ const rect = handle.getBoundingClientRect();++ const selected = selectionsDuring(() => {+ handle.dispatchEvent(new MouseEvent("mousedown", {+ button: 0, clientX: rect.left, clientY: rect.top, bubbles: true, cancelable: true+ }));+ document.dispatchEvent(new MouseEvent("mouseup", {+ clientX: rect.left, clientY: rect.top, bubbles: true+ }));+ });++ expect(selected).toContain("details");+ });++ it("ignores a non-primary mouse button", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body[]</paragraph></details>"+ );++ handleDom().dispatchEvent(new MouseEvent("mousedown", {+ button: 2, bubbles: true, cancelable: true+ }));+ document.dispatchEvent(new MouseEvent("mouseup", { bubbles: true }));++ expect(editor.model.document.selection.getSelectedElement()).toBeNull();+ });++ it("swallows the trailing click so the caret is not repositioned", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body[]</paragraph></details>"+ );+ const click = new MouseEvent("click", { bubbles: true, cancelable: true });++ handleDom().dispatchEvent(click);++ expect(click.defaultPrevented).toBe(true);+ });++ it("selects the collapsible from the keyboard via Enter and Space", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body[]</paragraph></details>"+ );++ expect(selectionsDuring(() => key(handleDom(), "Enter"))).toContain("details");++ editor.model.change((writer) => {+ const details = editor.model.document.getRoot()?.getChild(0);+ const body = details?.is("element") ? details.getChild(1) : null;+ if (body?.is("element")) {+ writer.setSelection(body, 0);+ }+ });++ expect(selectionsDuring(() => key(handleDom(), " "))).toContain("details");+ });++ it("ignores other keys", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body[]</paragraph></details>"+ );++ key(handleDom(), "a");++ expect(editor.model.document.selection.getSelectedElement()).toBeNull();+ });++ it("reorders relative to the whole collapsible when dropped on another one's summary", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>first</summary><paragraph>one</paragraph></details>" ++ "<details open=\"true\"><summary>second</summary><paragraph>two[]</paragraph></details>"+ );++ const source = handleDom(1);+ const start = source.getBoundingClientRect();+ const targetSummary = root.querySelectorAll("summary")[0].getBoundingClientRect();++ source.dispatchEvent(new MouseEvent("mousedown", {+ button: 0, clientX: start.left, clientY: start.top, bubbles: true, cancelable: true+ }));+ // Past the drag threshold, onto the upper half of the first collapsible's summary.+ document.dispatchEvent(new MouseEvent("mousemove", {+ clientX: targetSummary.left + 40, clientY: targetSummary.top + 1, bubbles: true+ }));+ document.dispatchEvent(new MouseEvent("mouseup", {+ clientX: targetSummary.left + 40, clientY: targetSummary.top + 1, bubbles: true+ }));++ // The dragged collapsible landed before the other one, not nested inside it.+ const data = getModelData(editor.model, { withoutSelection: true });+ expect(data.indexOf("second")).toBeLessThan(data.indexOf("first"));+ expect(editor.model.document.getRoot()?.childCount).toBe(2);+ });+ });++ describe("the toggle arrow", () => {+ it("toggles from the keyboard via Enter and Space", () => {+ setModelData(editor.model, "<details><summary>T[]</summary><paragraph>body</paragraph></details>");++ key(arrowDom(), "Enter");+ expect(getModelData(editor.model, { withoutSelection: true })).toContain("open=\"true\"");++ key(arrowDom(), " ");+ expect(getModelData(editor.model, { withoutSelection: true })).not.toContain("open=\"true\"");+ });++ it("ignores other keys", () => {+ setModelData(editor.model, "<details><summary>T[]</summary><paragraph>body</paragraph></details>");++ key(arrowDom(), "a");++ expect(getModelData(editor.model, { withoutSelection: true })).not.toContain("open=\"true\"");+ });++ it("keeps the browser from placing a caret on it", () => {+ setModelData(editor.model, "<details><summary>T[]</summary><paragraph>body</paragraph></details>");+ const mousedown = new MouseEvent("mousedown", { bubbles: true, cancelable: true });++ arrowDom().dispatchEvent(mousedown);++ expect(mousedown.defaultPrevented).toBe(true);+ });+ });++ describe("clicking the summary itself", () => {+ it("suppresses the native toggle so only the arrow controls the state", () => {+ setModelData(editor.model, "<details><summary>T[]</summary><paragraph>body</paragraph></details>");+ const summary = root.querySelector("summary");+ if (!(summary instanceof HTMLElement)) {+ throw new Error("Expected a rendered summary.");+ }++ const click = new MouseEvent("click", { bubbles: true, cancelable: true });+ summary.dispatchEvent(click);++ expect(click.defaultPrevented).toBe(true);+ expect(getModelData(editor.model, { withoutSelection: true })).not.toContain("open=\"true\"");+ });++ it("lets a modifier-click on a link inside the title through", () => {+ setModelData(editor.model, "<details><summary>T[]</summary><paragraph>body</paragraph></details>");+ const summary = root.querySelector("summary");+ if (!(summary instanceof HTMLElement)) {+ throw new Error("Expected a rendered summary.");+ }++ const click = new MouseEvent("click", { bubbles: true, cancelable: true, ctrlKey: true });+ summary.dispatchEvent(click);++ // Still handled by the summary branch — there is no link here — but the+ // modifier path is what an <a> inside the title relies on.+ expect(click.defaultPrevented).toBe(true);+ });+ });++ describe("the data downcast", () => {+ it("writes a bare open attribute for an expanded collapsible", () => {+ setModelData(+ editor.model,+ "<details open=\"true\"><summary>T</summary><paragraph>body</paragraph></details>"+ );++ expect(editor.getData()).toBe(+ "<details class=\"trilium-collapsible\" open=\"\"><summary>T</summary><p>body</p></details>"+ );+ });+ });+});diff --git a/packages/ckeditor5-collapsible/theme/collapsible.css b/packages/ckeditor5/src/theme/collapsible.csssimilarity index 100%rename from packages/ckeditor5-collapsible/theme/collapsible.cssrename to packages/ckeditor5/src/theme/collapsible.cssdiff --git a/packages/ckeditor5/tsconfig.lib.json b/packages/ckeditor5/tsconfig.lib.jsonindex 0e74666220..0b2bf1cdc2 100644--- a/packages/ckeditor5/tsconfig.lib.json+++ b/packages/ckeditor5/tsconfig.lib.json@@ -27,9 +27,6 @@{"path": "../ckeditor5-math"},- {- "path": "../ckeditor5-collapsible"- },{"path": "../ckeditor5-mermaid"},diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex cd3b7c9dd1..35546bc9d7 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1094,9 +1094,6 @@ importers:packages/ckeditor5:dependencies:- '@triliumnext/ckeditor5-collapsible':- specifier: workspace:*- version: link:../ckeditor5-collapsible'@triliumnext/ckeditor5-footnotes':specifier: workspace:*version: link:../ckeditor5-footnotes@@ -1126,34 +1123,6 @@ importers:specifier: 9.30.0version: 9.30.0([email protected])- packages/ckeditor5-collapsible:- dependencies:- '@triliumnext/ckeditor5-utils':- specifier: workspace:*- version: link:../ckeditor5-utils- '@triliumnext/commons':- specifier: workspace:*- version: link:../commons- bootstrap:- specifier: ^5.3.0- version: 5.3.8(@popperjs/[email protected])- devDependencies:- ckeditor5:- specifier: 48.3.1- version: 48.3.1([email protected])- happy-dom:- specifier: 20.11.1- version: 20.11.1- typescript:- specifier: 6.0.3- version: 6.0.3- vite-plugin-svgo:- specifier: 2.0.0- version: 2.0.0([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))- vitest:- specifier: 4.1.10- version: 4.1.10(@opentelemetry/[email protected])(@types/[email protected])(@vitest/[email protected])(@vitest/[email protected])(@vitest/[email protected])([email protected])([email protected](@noble/[email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))-packages/ckeditor5-footnotes:devDependencies:'@ckeditor/ckeditor5-inspector':