Skip to content

Build your personal knowledge base with Trilium Notes

client: fix some issues

Adorian Doran37f9792019b1
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index b16ad7aea7..8785163e56 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -67,7 +67,7 @@ interface EditableTextProps extends TypeWidgetProps {
}
export default function EditableText({
- note, parentComponent, ntxId, noteContext, block, onBlockChange
+ note, parentComponent, ntxId, noteContext, isVisible, block, onBlockChange
}: EditableTextProps) {
const containerRef = useRef<HTMLDivElement>(null);
const contentRef = useRef<string>("");
@@ -158,8 +158,9 @@ export default function EditableText({
const templates = useTemplates();
useSearchTermsConsumer(note, noteContext, ntxId);
+ // A hidden widget leaves the link target to the one on display.
useSameNoteSwitch(note, ntxId, () => {
- if (contentNoteIdRef.current === note?.noteId) {
+ if (isVisible !== false && contentNoteIdRef.current === note?.noteId) {
revealLinkTarget();
}
});
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
index a01cebc28d..02e12f0593 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
@@ -264,7 +264,7 @@ describe("ReadOnlyText ?block= handling", () => {
}
});
- async function mountWithBlock(block: string) {
+ async function mountWithBlock(block: string | undefined, isVisible = true) {
const scrollIntoView = vi.fn();
Element.prototype.scrollIntoView = scrollIntoView;
const note = buildNote({
@@ -290,7 +290,7 @@ describe("ReadOnlyText ?block= handling", () => {
ntxId={noteContext.ntxId}
parentComponent={parent}
viewScope={noteContext.viewScope}
- isVisible={true}
+ isVisible={isVisible}
/>
</ParentComponent.Provider>,
container
@@ -329,6 +329,21 @@ describe("ReadOnlyText ?block= handling", () => {
expect(scrollIntoView.mock.contexts.at(-1))
.toBe(container.querySelector("[data-trilium-block-id='b1']"));
});
+
+ it("leaves a link to a block of its note to the editor while hidden", async () => {
+ const { parent, noteContext, container, scrollIntoView } = await mountWithBlock(
+ undefined,
+ false
+ );
+ expect(container.querySelector("[data-trilium-block-id='b1']")).not.toBeNull();
+
+ noteContext.viewScope = { block: "b1" };
+ await act(async () => {
+ await parent.handleEvent("noteSwitched", { noteContext, notePath: "" });
+ });
+ expect(noteContext.viewScope?.block).toBe("b1");
+ expect(scrollIntoView).not.toHaveBeenCalled();
+ });
});
/**
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
index d7e00184c7..7e07b2de4b 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
@@ -56,8 +56,9 @@ export default function ReadOnlyText({ note, noteContext, ntxId, parentComponent
renderedNoteIdRef.current = note.noteId;
revealLinkTarget();
}, [blob]);
+ // A hidden widget leaves the link target to the one on display.
useSameNoteSwitch(note, ntxId, () => {
- if (renderedNoteIdRef.current === note.noteId) {
+ if (isVisible !== false && renderedNoteIdRef.current === note.noteId) {
revealLinkTarget();
}
});
diff --git a/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts b/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
index a4f7184130..5ecc45e651 100644
--- a/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
+++ b/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
@@ -7,6 +7,7 @@ import { beforeAll, describe, expect, it } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
import BlockDragHandle from "./block_drag_handle.js";
+import Collapsible from "./collapsible/collapsible.js";
describe("BlockDragHandle", () => {
beforeAll(() => new Promise<void>((resolve, reject) => {
@@ -84,6 +85,46 @@ describe("BlockDragHandle", () => {
expect(editor.getData()).toBe("<p>second</p><p>third</p><p>first</p>");
});
+ it("drags a whole collapsible from its title, with or without its body selected", async () => {
+ for (const [ title, body ] of [ [ "Ti[]tle", "body" ], [ "Ti[tle", "bo]dy" ] ]) {
+ const { editor, button } = await createEditor(`<details open="true"><summary>${title}`
+ + `</summary><paragraph>${body}</paragraph><paragraph>more</paragraph></details>`
+ + "<paragraph>after</paragraph>");
+ const editableRect = getEditable(editor).getBoundingClientRect();
+ const lastBlockRect = getBlock(editor, 1).getBoundingClientRect();
+
+ drop(button, startDrag(button), {
+ clientX: editableRect.left - 50,
+ clientY: lastBlockRect.bottom - 2
+ });
+ expect(editor.getData()).toBe("<p>after</p>"
+ + "<details class=\"trilium-collapsible\" open=\"\"><summary>Title</summary>"
+ + "<p>body</p><p>more</p></details>");
+ }
+ });
+
+ it("takes drops beside a narrow editor or far out in a wide margin", async () => {
+ const layouts = [
+ { width: "90px", margin: "0 100px", distance: 10 },
+ { width: "200px", margin: "0 190px", distance: 150 }
+ ];
+ for (const { width, margin, distance } of layouts) {
+ const { editor, button } = await createEditor(
+ "<paragraph>fi[]rst</paragraph><paragraph>second</paragraph>"
+ );
+ editor.ui.view.element?.style.setProperty("width", width);
+ editor.ui.view.element?.style.setProperty("margin", margin);
+ const editableRect = getEditable(editor).getBoundingClientRect();
+ const lastBlockRect = getBlock(editor, 1).getBoundingClientRect();
+
+ drop(button, startDrag(button), {
+ clientX: editableRect.left - distance,
+ clientY: lastBlockRect.bottom - 2
+ });
+ expect(editor.getData()).toBe("<p>second</p><p>first</p>");
+ }
+ });
+
it("mirrors the handle and its drops in a right-to-left editor", async () => {
const { editor, button } = await createEditor(
"<paragraph>fi[]rst</paragraph><paragraph>second</paragraph>",
@@ -144,7 +185,7 @@ async function createEditor(
config: Parameters<typeof createTestEditor>[1] = {}
) {
const editor = await createTestEditor(
- [ Essentials, Paragraph, Table, TableCaption, BlockDragHandle ],
+ [ Essentials, Paragraph, Table, TableCaption, Collapsible, BlockDragHandle ],
config
);
editor.ui.view.element?.style.setProperty("margin", "0 100px");
@@ -181,7 +222,7 @@ function startDrag(button: HTMLElement) {
return dataTransfer;
}
-/** Drags over `point`, drops there and ends the drag. Returns whether the drag over was accepted. */
+/** Drags over `point`, drops there and ends the drag. Returns whether the dragover was accepted. */
function drop(
button: HTMLElement,
dataTransfer: DataTransfer,
diff --git a/packages/ckeditor5/src/plugins/block_drag_handle.ts b/packages/ckeditor5/src/plugins/block_drag_handle.ts
index ee91b7e472..7b717ad2a3 100644
--- a/packages/ckeditor5/src/plugins/block_drag_handle.ts
+++ b/packages/ckeditor5/src/plugins/block_drag_handle.ts
@@ -1,6 +1,6 @@
import {
- BlockButtonView, ClipboardObserver, DomEmitterMixin, DragDrop, env, IconDragIndicator, Plugin,
- Rect
+ BlockButtonView, ClipboardObserver, DomEmitterMixin, DragDrop, env, IconDragIndicator,
+ type ModelElement, Plugin, Rect
} from "ckeditor5";
/**
@@ -167,7 +167,7 @@ export default class BlockDragHandle extends Plugin {
private startDrag(domEvent: DragEvent) {
const editor = this.editor;
const model = editor.model;
- const blocks = Array.from(model.document.selection.getSelectedBlocks());
+ const blocks = Array.from(model.document.selection.getSelectedBlocks(), getDraggedBlock);
const firstBlock = blocks.at(0);
const lastBlock = blocks.at(-1);
if (editor.isReadOnly || !firstBlock || !lastBlock) {
@@ -177,7 +177,9 @@ export default class BlockDragHandle extends Plugin {
const range = model.createRange(
model.createPositionBefore(firstBlock),
- model.createPositionAfter(lastBlock)
+ model.createPositionAfter(
+ lastBlock.getAncestors().includes(firstBlock) ? firstBlock : lastBlock
+ )
);
model.change((writer) => writer.setSelection(range));
@@ -187,20 +189,24 @@ export default class BlockDragHandle extends Plugin {
}
/**
- * Passes a `dragover` or `drop` to the editing view, 100px into the content from the pointer,
- * so the pointer can stay in the margin beside the blocks.
+ * Passes a `dragover` or `drop` to the editing view, 100px into the content from the pointer
+ * and within the editable, so the pointer can stay in the margin beside the blocks.
*/
private forwardDrag(domEvent: DragEvent) {
- if (!this.isDragging) {
+ const domEditable = this.editor.ui.getEditableElement();
+ if (!this.isDragging || !domEditable) {
return;
}
const isLtr = this.editor.locale.contentLanguageDirection === "ltr";
- const clientX = domEvent.clientX + (isLtr ? 100 : -100);
+ const editableRect = domEditable.getBoundingClientRect();
+ const clientX = Math.min(
+ Math.max(domEvent.clientX + (isLtr ? 100 : -100), editableRect.left + 1),
+ editableRect.right - 1
+ );
const clientY = domEvent.clientY;
const target = document.elementFromPoint(clientX, clientY);
- const domEditable = this.editor.ui.getEditableElement();
- if (!target || !domEditable?.contains(target)) {
+ if (!target || !domEditable.contains(target)) {
return;
}
@@ -222,6 +228,11 @@ interface BlockTarget {
domEditable: HTMLElement;
}
+/** The block that the handle drags for `block`: the whole collapsible for its title. */
+function getDraggedBlock(block: ModelElement) {
+ return block.is("element", "summary") ? block.parent as ModelElement : block;
+}
+
declare module "ckeditor5" {
interface PluginsMap {
[BlockDragHandle.pluginName]: BlockDragHandle;