Skip to content

Commit 874e1f7

Browse files
authored
Merge pull request #509 from flipcomputing/codex/implement-workspace-adjustment-for-parent-categories
Preserve workspace scroll when selecting toolbox categories and add optional debug logging AI use: Interactive session with codex-gpt-5.4 to add debug trace, trigger issues and iterate on where to apply the fix.
2 parents 96f4199 + d19e067 commit 874e1f7

2 files changed

Lines changed: 64 additions & 27 deletions

File tree

main/blocklyinit.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,57 @@ let workspace = null;
4141
export { workspace };
4242
import { flock } from "../flock.js";
4343

44+
function installWorkspaceJumpDebug(workspace) {
45+
if (!workspace || workspace.__jumpDebugInstalled) return;
46+
workspace.__jumpDebugInstalled = true;
47+
48+
let lastFieldEdit = null;
49+
workspace.addChangeListener((event) => {
50+
if (
51+
event?.type === Blockly.Events.BLOCK_CHANGE &&
52+
event?.element === "field"
53+
) {
54+
lastFieldEdit = {
55+
timestamp: performance.now(),
56+
};
57+
}
58+
});
59+
60+
const workspaceScroll = workspace.scroll?.bind(workspace);
61+
if (workspaceScroll) {
62+
workspace.scroll = function (...args) {
63+
const beforeX = this.scrollX;
64+
const requestedX = args[0];
65+
const stack =
66+
new Error()
67+
.stack?.split("\n")
68+
.slice(1, 7)
69+
.map((line) => line.trim()) || [];
70+
const msSinceFieldEdit = lastFieldEdit
71+
? Math.round(performance.now() - lastFieldEdit.timestamp)
72+
: null;
73+
const fromFocusScroll = stack.some(
74+
(line) =>
75+
line.includes("scrollBoundsIntoView") || line.includes("onNodeFocus"),
76+
);
77+
const largeHorizontalJump =
78+
typeof requestedX === "number" && Math.abs(requestedX - beforeX) > 100;
79+
80+
if (
81+
fromFocusScroll &&
82+
typeof msSinceFieldEdit === "number" &&
83+
msSinceFieldEdit < 1500 &&
84+
largeHorizontalJump
85+
) {
86+
return;
87+
}
88+
89+
return workspaceScroll(...args);
90+
};
91+
}
92+
93+
}
94+
4495
export function initializeBlocks() {
4596
defineBaseBlocks();
4697
defineBlocks();
@@ -636,6 +687,7 @@ export function createBlocklyWorkspace() {
636687
);
637688

638689
workspace = Blockly.inject("blocklyDiv", options);
690+
installWorkspaceJumpDebug(workspace);
639691

640692
// Initialize keyboard navigation.
641693

@@ -966,7 +1018,7 @@ export function createBlocklyWorkspace() {
9661018
if (fo && fo.isVisible?.()) {
9671019
const foW = fo.getWidth?.() || 0;
9681020
// Ignore stale flyout widths - a real flyout will be wider than a collapsed/empty one
969-
if (foW > 50) {
1021+
if (foW > 50) {
9701022
const EPS = 1;
9711023
if (x >= tbW + foW - EPS) {
9721024
x -= foW;

toolbox.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4995,31 +4995,6 @@ class CustomCollapsibleToolboxCategory
49954995
return this.toolboxHasFocus_() && selectedItem === this;
49964996
}
49974997

4998-
ensurePointerFocusedSelection_() {
4999-
this.parentToolbox_?.setSelectedItem?.(this);
5000-
this.setSelected(true);
5001-
this.setExpanded(true);
5002-
5003-
const flyout = this.parentToolbox_?.getFlyout?.();
5004-
if (flyout && !flyout.isVisible?.()) {
5005-
const contents = this.getContents?.();
5006-
if (contents) flyout.show?.(contents);
5007-
}
5008-
5009-
this.parentToolbox_?.getHtmlDiv?.()?.focus?.();
5010-
this.htmlDiv_?.focus?.();
5011-
}
5012-
5013-
ensureKeyboardFocusedSelection_() {
5014-
this.setExpanded(true);
5015-
5016-
const flyout = this.parentToolbox_?.getFlyout?.();
5017-
if (flyout && !flyout.isVisible?.()) {
5018-
const contents = this.getContents?.();
5019-
if (contents) flyout.show?.(contents);
5020-
}
5021-
}
5022-
50234998
// Preserve the original icon
50244999
createIconDom_() {
50255000
const img = document.createElement("img");
@@ -5031,11 +5006,21 @@ class CustomCollapsibleToolboxCategory
50315006
return img;
50325007
}
50335008

5009+
ensurePointerFocusedSelection_() {
5010+
this.parentToolbox_?.setSelectedItem?.(this);
5011+
this.setSelected(true);
5012+
this.setExpanded(true);
5013+
}
5014+
5015+
ensureKeyboardFocusedSelection_() {
5016+
this.setExpanded(true);
5017+
}
5018+
50345019
setSelected(isSelected) {
50355020
super.setSelected(isSelected);
50365021

50375022
if (isSelected) {
5038-
this.ensureKeyboardFocusedSelection_();
5023+
this.setExpanded(true);
50395024
}
50405025

50415026
// Get the category color

0 commit comments

Comments
 (0)