Skip to content

Build your personal knowledge base with Trilium Notes

fix an issue

Adorian Dorandf62a9c0c784
diff --git a/packages/ckeditor5/src/plugins/containers/container_keyboard.spec.ts b/packages/ckeditor5/src/plugins/containers/container_keyboard.spec.ts
index ef37015469..ad905a3d5e 100644
--- a/packages/ckeditor5/src/plugins/containers/container_keyboard.spec.ts
+++ b/packages/ckeditor5/src/plugins/containers/container_keyboard.spec.ts
@@ -5,12 +5,16 @@ import {
Essentials,
Paragraph
} from "ckeditor5";
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import { createTestEditor, pressKey as press } from "../../../test/editor-kit.js";
+import { installGlobMock } from "../../../test/globals-test-kit.js";
+import ContentEmbed from "../content_embed/content_embed.js";
import Multicolumn from "../multicolumn/multicolumn.js";
import Tabs from "../tabs/tabs.js";
+const EMBED = "<contentEmbed boxSize=\"medium\" noteId=\"n1\"></contentEmbed>";
+
describe("container keyboard shortcuts", () => {
it("act in the innermost column or tab around the selection", async () => {
@@ -42,6 +46,26 @@ describe("container keyboard shortcuts", () => {
expect(press(editor, "ArrowRight", { ctrlKey: true })).toBe(true);
expect(model(editor)).toBe(columns(p(""), `[${tabs}]${p("C")}`));
});
+
+ it("leave the keys pressed in an embed to the editor of the embed", async () => {
+ installGlobMock({ getComponentByEl: () => ({ loadEmbeddedNote: vi.fn() }) });
+ const editor = await createTestEditor([Essentials, Paragraph, Multicolumn, ContentEmbed]);
+ const data = columns(p("a[]"), EMBED);
+ setModelData(editor.model, data);
+ const wrapper = editor.editing.view.getDomRoot()?.querySelector(".include-note-wrapper");
+ expect(wrapper).toBeInstanceOf(HTMLElement);
+ const nestedRoot = document.createElement("div");
+ nestedRoot.tabIndex = -1;
+ wrapper?.append(nestedRoot);
+
+ const event = new KeyboardEvent("keydown", {
+ key: "Enter", ctrlKey: true, bubbles: true, cancelable: true
+ });
+ nestedRoot.dispatchEvent(event);
+ expect(event.defaultPrevented).toBe(false);
+ expect(model(editor)).toBe(data);
+ expect(press(editor, "Enter", { ctrlKey: true })).toBe(true);
+ });
});
async function createEditor() {
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.ts
index 29aa4db475..aed7b1962e 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.ts
@@ -11,7 +11,7 @@ import {
} from "ckeditor5";
import TriliumMentionUI from "../mention/trilium_mention_ui.js";
-import { addEditingKeydownCapture } from "../move_block_updown.js";
+import { addEditingKeydownCapture, isFromEditable } from "../move_block_updown.js";
import {
getCaption, getContentEmbedAt, getContentEmbedDom, isContentEmbed
} from "./content_embed.js";
@@ -32,11 +32,10 @@ export default class ContentEmbedCaret extends Plugin {
}
init() {
- // The capture phase runs before the arrow keys of `Widget` and `WidgetTypeAround`. It also
- // receives the keys pressed inside an embed, such as in its editor, which are skipped.
+ // The capture phase runs before the arrow keys of `Widget` and `WidgetTypeAround`.
addEditingKeydownCapture(this, (event) => {
const move = getArrowMove(event, this.isRtl());
- if (!move || this.editor.isReadOnly || !isFromEditable(this.editor, event)) {
+ if (!move || this.editor.isReadOnly) {
return;
}
@@ -250,19 +249,6 @@ function getArrowMove(event: KeyboardEvent, isRtl: boolean): ArrowMove | null {
}
}
-/**
- * Whether `event` comes from an editable of `editor`, such as its root, a caption or a table cell,
- * rather than from the content of an embed, which the wrapper of the embed marks with
- * `data-cke-ignore-events`.
- */
-function isFromEditable(editor: Editor, event: Event) {
- const root = editor.editing.view.getDomRoot();
- const target = event.target instanceof Element ? event.target : null;
- const embedWrapper = target?.closest("[data-cke-ignore-events]");
- return !!root && !!target && root.contains(target)
- && !(embedWrapper && root.contains(embedWrapper));
-}
-
/**
* Scrolls to the caret of `editor`, or to the fake caret of `WidgetTypeAround` beside the selected
* widget. For a widget taller than the window, `scrollToTheSelection()` can show its top only.
diff --git a/packages/ckeditor5/src/plugins/move_block_updown.ts b/packages/ckeditor5/src/plugins/move_block_updown.ts
index 8871415011..e9ec9af9d1 100644
--- a/packages/ckeditor5/src/plugins/move_block_updown.ts
+++ b/packages/ckeditor5/src/plugins/move_block_updown.ts
@@ -71,21 +71,41 @@ export function hasMoveModifier(e: KeyboardEvent): boolean {
* container — see #10095). Without this cleanup the orphaned listener stays attached, captures
* the keystroke first and runs editor.execute() against the destroyed editor — silently
* swallowing the shortcut until a reload.
+ *
+ * The keys pressed in the content of an embed, such as in its editor, are skipped.
*/
export function addEditingKeydownCapture(plugin: Plugin, handleKeydown: (e: KeyboardEvent) => void) {
const editor = plugin.editor;
+ const onKeydown = (e: KeyboardEvent) => {
+ if (isFromEditable(editor, e)) {
+ handleKeydown(e);
+ }
+ };
editor.editing.view.once('render', () => {
const domRoot = editor.editing.view.getDomRoot();
/* v8 ignore next 1 -- domRoot is always set while the editor is alive; only null after destroy */
if (!domRoot) return;
- domRoot.addEventListener('keydown', handleKeydown, { capture: true });
+ domRoot.addEventListener('keydown', onKeydown, { capture: true });
plugin.listenTo(editor, 'destroy', () => {
- domRoot.removeEventListener('keydown', handleKeydown, { capture: true });
+ domRoot.removeEventListener('keydown', onKeydown, { capture: true });
});
});
}
+/**
+ * Whether `event` comes from an editable of `editor`, such as its root, a caption or a table cell,
+ * rather than from the content of an embed, which the wrapper of the embed marks with
+ * `data-cke-ignore-events`.
+ */
+export function isFromEditable(editor: Editor, event: Event) {
+ const root = editor.editing.view.getDomRoot();
+ const target = event.target instanceof Element ? event.target : null;
+ const embedWrapper = target?.closest('[data-cke-ignore-events]');
+ return !!root && !!target && root.contains(target)
+ && !(embedWrapper && root.contains(embedWrapper));
+}
+
abstract class MoveBlockUpDownCommand extends Command {
abstract getSibling(selectedBlock: ModelElement): ModelNode | null;