Skip to content

Build your personal knowledge base with Trilium Notes

perf(text): let the block drag handle follow scrolls only while focused

Elian Doran5a1eaa1f624d
diff --git a/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts b/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
index 35f8fbfd82..b0de2f215a 100644
--- a/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
+++ b/packages/ckeditor5/src/plugins/block_drag_handle.spec.ts
@@ -102,6 +102,23 @@ describe("BlockDragHandle", () => {
expect(requestAnimationFrame).toHaveBeenCalledOnce();
});
+ it("follows scrolls and resizes only while the editor has focus", async () => {
+ const { editor } = await createEditor("<paragraph>on[]e</paragraph>");
+ getEditable(editor).blur();
+ await expect.poll(() => editor.ui.focusTracker.isFocused).toBe(false);
+ const requestAnimationFrame = vi.spyOn(window, "requestAnimationFrame");
+ onTestFinished(() => requestAnimationFrame.mockRestore());
+
+ document.dispatchEvent(new Event("scroll"));
+ window.dispatchEvent(new Event("resize"));
+ expect(requestAnimationFrame).not.toHaveBeenCalled();
+
+ editor.editing.view.focus();
+ await expect.poll(() => editor.ui.focusTracker.isFocused).toBe(true);
+ document.dispatchEvent(new Event("scroll"));
+ expect(requestAnimationFrame).toHaveBeenCalledOnce();
+ });
+
it("drags the selected block to where it is dropped", async () => {
const { editor, button } = await createEditor(
"<paragraph>fi[]rst</paragraph><paragraph>second</paragraph>"
diff --git a/packages/ckeditor5/src/plugins/block_drag_handle.ts b/packages/ckeditor5/src/plugins/block_drag_handle.ts
index ebbc8ae344..dcae4cdce7 100644
--- a/packages/ckeditor5/src/plugins/block_drag_handle.ts
+++ b/packages/ckeditor5/src/plugins/block_drag_handle.ts
@@ -15,6 +15,7 @@ export default class BlockDragHandle extends Plugin {
private isUpdateScheduled = false;
private buttonSize?: { width: number; height: number };
private readonly domEmitter = new (DomEmitterMixin())();
+ private readonly layoutEmitter = new (DomEmitterMixin())();
static get pluginName() {
return "BlockDragHandle" as const;
@@ -52,14 +53,10 @@ export default class BlockDragHandle extends Plugin {
this.listenTo(editor.ui, "update", () => this.update());
this.listenTo(editor, "change:isReadOnly", () => this.update(), { priority: "low" });
- this.listenTo(editor.ui.focusTracker, "change:isFocused", () => this.update());
-
- this.domEmitter.listenTo(window, "resize", () => this.scheduleUpdate());
- this.domEmitter.listenTo(document, "scroll", (_evt, domEvent: Event) => {
- if (this.canMoveEditable(domEvent.target as Node)) {
- this.scheduleUpdate();
- }
- }, { useCapture: true, usePassive: true });
+ this.listenTo(editor.ui.focusTracker, "change:isFocused", (_evt, _name, isFocused) => {
+ this.followLayoutChanges(isFocused);
+ this.update();
+ });
this.domEmitter.listenTo(document, "dragover", (_evt, domEvent: DragEvent) => {
this.forwardDrag(domEvent);
@@ -75,10 +72,29 @@ export default class BlockDragHandle extends Plugin {
override destroy() {
this.domEmitter.stopListening();
+ this.layoutEmitter.stopListening();
this.buttonView?.destroy();
super.destroy();
}
+ /**
+ * Moves the handle with its block on resizes and scrolls while the editor has focus. Without
+ * focus there is no handle to move.
+ */
+ private followLayoutChanges(isFocused: boolean) {
+ if (!isFocused) {
+ this.layoutEmitter.stopListening();
+ return;
+ }
+
+ this.layoutEmitter.listenTo(window, "resize", () => this.scheduleUpdate());
+ this.layoutEmitter.listenTo(document, "scroll", (_evt, domEvent: Event) => {
+ if (this.canMoveEditable(domEvent.target as Node)) {
+ this.scheduleUpdate();
+ }
+ }, { useCapture: true, usePassive: true });
+ }
+
private scheduleUpdate() {
if (this.isUpdateScheduled) {
return;