Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/content embed: fix an issue

Adorian Doran73788450e7a9
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.spec.ts
index b32cdd8b57..d14b434996 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.spec.ts
@@ -218,6 +218,29 @@ describe("ContentEmbedCaret leaving an embed", () => {
expect(editor.model.document.selection.getAttribute(TYPE_AROUND_ATTRIBUTE)).toBe("before");
});
+ it("scrolls to the fake caret below an embed taller than the window", () => {
+ installGlobMock({
+ getComponentByEl: () => ({
+ loadEmbeddedNote: (_noteId: string, wrapper: HTMLElement) => {
+ const content = document.createElement("div");
+ content.style.height = `${window.innerHeight * 3}px`;
+ wrapper.append(content);
+ }
+ })
+ });
+ setModelData(editor.model, AB + EMBED);
+ // As after reading down to the end of the embed, its top is above the window.
+ window.scrollTo(0, document.documentElement.scrollHeight);
+
+ expect(plugin.leaveEmbed(getEmbedContent(editor), "end")).toBe(true);
+ const fakeCaret = getEmbedDom(editor).querySelector(
+ ":scope > .ck-widget__type-around > .ck-widget__type-around__fake-caret"
+ );
+ const rect = fakeCaret?.getBoundingClientRect();
+ expect(rect?.top).toBeGreaterThanOrEqual(0);
+ expect(rect?.bottom).toBeLessThanOrEqual(window.innerHeight);
+ });
+
it("puts the caret in the caption below the content", () => {
setModelData(editor.model, AB + CAPTIONED_EMBED + CD);
expect(plugin.leaveEmbed(getEmbedContent(editor), "end")).toBe(true);
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 ef6036c00c..b07a20cd7c 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_caret.ts
@@ -6,7 +6,8 @@ import {
type ModelPosition,
type ModelRange,
Plugin,
- Rect
+ Rect,
+ scrollViewportToShowTarget
} from "ckeditor5";
import { addEditingKeydownCapture } from "../move_block_updown.js";
@@ -78,7 +79,7 @@ export default class ContentEmbedCaret extends Plugin {
writer.setSelection(embed, "on");
writer.setSelectionAttribute(TYPE_AROUND_ATTRIBUTE, isForward ? "after" : "before");
});
- editor.editing.view.scrollToTheSelection();
+ scrollToTheCaret(editor);
return true;
}
@@ -104,7 +105,7 @@ export default class ContentEmbedCaret extends Plugin {
});
}
editor.editing.view.focus();
- editor.editing.view.scrollToTheSelection();
+ scrollToTheCaret(editor);
}
/**
@@ -258,6 +259,26 @@ function isFromEditable(editor: Editor, event: Event) {
&& !(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.
+ */
+function scrollToTheCaret(editor: Editor) {
+ const selection = editor.model.document.selection;
+ const widget = selection.getAttribute(TYPE_AROUND_ATTRIBUTE)
+ ? getContentEmbedDom(editor, selection.getSelectedElement())
+ : null;
+ const fakeCaret = widget?.querySelector<HTMLElement>(
+ ":scope > .ck-widget__type-around > .ck-widget__type-around__fake-caret"
+ );
+
+ if (fakeCaret) {
+ scrollViewportToShowTarget({ target: fakeCaret, viewportOffset: 20, ancestorOffset: 20 });
+ } else {
+ editor.editing.view.scrollToTheSelection();
+ }
+}
+
/** The embed right beside the collapsed selection, in the direction of the move, or `null`. */
function getEmbedNextTo(model: Model, isForward: boolean) {
const selection = model.document.selection;