From 80c09e4c3f688ea80acabe4c5e8d55665d629ad0 Mon Sep 17 00:00:00 2001 From: Ben Henning Date: Mon, 12 May 2025 22:07:03 +0000 Subject: [PATCH 1/2] fix: Ensure LineCursor synchronizes with focus. --- core/interfaces/i_navigable.ts | 10 ++++++++++ core/keyboard_nav/line_cursor.ts | 11 +++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/core/interfaces/i_navigable.ts b/core/interfaces/i_navigable.ts index f41e7882934..a21f743dd23 100644 --- a/core/interfaces/i_navigable.ts +++ b/core/interfaces/i_navigable.ts @@ -29,3 +29,13 @@ export interface INavigable { */ getClass(): new (...args: any) => T; } + +/** + * Determines whether the provided object fulfills the contract of INavigable. + * + * @param object The object to test. + * @returns Whether the provided object can be used as an INavigable. + */ +export function isNavigable(object: any | null): object is INavigable { + return object && 'isNavigable' in object && 'getClass' in object; +} diff --git a/core/keyboard_nav/line_cursor.ts b/core/keyboard_nav/line_cursor.ts index 1dcfdd8a295..fe9892f5260 100644 --- a/core/keyboard_nav/line_cursor.ts +++ b/core/keyboard_nav/line_cursor.ts @@ -26,7 +26,7 @@ import {FlyoutButton} from '../flyout_button.js'; import {FlyoutSeparator} from '../flyout_separator.js'; import {getFocusManager} from '../focus_manager.js'; import {isFocusableNode} from '../interfaces/i_focusable_node.js'; -import type {INavigable} from '../interfaces/i_navigable.js'; +import {isNavigable, type INavigable} from '../interfaces/i_navigable.js'; import * as registry from '../registry.js'; import {RenderedConnection} from '../rendered_connection.js'; import {Renderer} from '../renderers/zelos/renderer.js'; @@ -593,11 +593,10 @@ export class LineCursor extends Marker { private updateCurNodeFromFocus() { const focused = getFocusManager().getFocusedNode(); - if (focused instanceof BlockSvg) { - const block: BlockSvg | null = focused; - if (block && block.workspace === this.workspace) { - this.setCurNode(block); - } + if (isNavigable(focused)) { + const block = this.getSourceBlockFromNode(focused); + if (block && block.workspace !== this.workspace) return; + this.setCurNode(focused); } } From e97985cd8d2144dd28434e4e7dd5952bc053a22f Mon Sep 17 00:00:00 2001 From: Ben Henning Date: Tue, 13 May 2025 22:26:35 +0000 Subject: [PATCH 2/2] fix: Post-merge fixes. This is also a bit simpler than the previous approach now that INavigable has been removed. --- core/keyboard_nav/line_cursor.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/core/keyboard_nav/line_cursor.ts b/core/keyboard_nav/line_cursor.ts index b97740aab8c..9d83f6554d3 100644 --- a/core/keyboard_nav/line_cursor.ts +++ b/core/keyboard_nav/line_cursor.ts @@ -374,7 +374,15 @@ export class LineCursor extends Marker { * @returns The current field, connection, or block the cursor is on. */ override getCurNode(): IFocusableNode | null { - this.updateCurNodeFromFocus(); + // Ensure the current node matches what's currently focused. + const focused = getFocusManager().getFocusedNode(); + const block = this.getSourceBlockFromNode(focused); + if (!block || block.workspace === this.workspace) { + // If the current focused node corresponds to a block then ensure that it + // belongs to the correct workspace for this cursor. + this.setCurNode(focused); + } + return super.getCurNode(); } @@ -401,19 +409,6 @@ export class LineCursor extends Marker { } } - /** - * Updates the current node to match what's currently focused. - */ - private updateCurNodeFromFocus() { - const focused = getFocusManager().getFocusedNode(); - - if (isNavigable(focused)) { - const block = this.getSourceBlockFromNode(focused); - if (block && block.workspace !== this.workspace) return; - this.setCurNode(focused); - } - } - /** * Get the first navigable node on the workspace, or null if none exist. *