Skip to content

Commit bca7177

Browse files
author
DavidQ
committed
Fix workspace handoff sync: remove source-tool-state lock for tool surfaces and force Palette Browser to follow shared palette handoff on launch/update. v2
1 parent 88e4716 commit bca7177

3 files changed

Lines changed: 56 additions & 25 deletions

File tree

tools/Palette Browser/main.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
createPaletteHandoff,
33
getSharedLaunchContext,
44
getToolDisplayName,
5+
SHARED_PALETTE_HANDOFF_EVENT,
56
readSharedPaletteHandoff,
67
writeSharedPaletteHandoff
78
} from "../shared/assetUsageIntegration.js";
@@ -283,6 +284,24 @@ function selectSharedPaletteFromHandoff() {
283284
return true;
284285
}
285286

287+
function syncSelectionFromSharedHandoff(options = {}) {
288+
const shouldRender = options.render === true;
289+
const previousPaletteId = state.selectedPaletteId;
290+
const selected = selectSharedPaletteFromHandoff();
291+
if (!selected) {
292+
return false;
293+
}
294+
if (previousPaletteId !== state.selectedPaletteId) {
295+
state.selectedSwatchIndex = 0;
296+
}
297+
if (shouldRender) {
298+
renderPaletteList();
299+
renderSelectedPalette();
300+
renderStoredSelection();
301+
}
302+
return true;
303+
}
304+
286305
function isCustomPalette(palette) {
287306
return Boolean(palette && palette.source === "custom");
288307
}
@@ -871,10 +890,13 @@ function bindEvents() {
871890
refs.copyPaletteJsonButton.addEventListener("click", copyPaletteJson);
872891
refs.exportPaletteJsonButton.addEventListener("click", exportPaletteJson);
873892
refs.usePaletteButton.addEventListener("click", usePaletteInActiveTools);
893+
window.addEventListener(SHARED_PALETTE_HANDOFF_EVENT, () => {
894+
syncSelectionFromSharedHandoff({ render: true });
895+
});
874896
}
875897

876898
function init() {
877-
const selectedFromHandoff = selectSharedPaletteFromHandoff();
899+
const selectedFromHandoff = syncSelectionFromSharedHandoff();
878900
if (!selectedFromHandoff) {
879901
const firstPalette = getAllPalettes()[0] ?? null;
880902
state.selectedPaletteId = firstPalette?.id ?? "";
@@ -884,6 +906,9 @@ function init() {
884906
renderSelectedPalette();
885907
renderStoredSelection();
886908
bindEvents();
909+
window.setTimeout(() => {
910+
syncSelectionFromSharedHandoff({ render: true });
911+
}, 400);
887912
}
888913

889914
let initialized = false;
@@ -903,7 +928,7 @@ const paletteBrowserApi = {
903928
state.selectedSwatchIndex = Number.isInteger(snapshot?.selectedSwatchIndex) ? snapshot.selectedSwatchIndex : 0;
904929
state.customPalettes = Array.isArray(snapshot?.customPalettes) ? structuredClone(snapshot.customPalettes) : [];
905930
if (isWorkspaceContext()) {
906-
selectSharedPaletteFromHandoff();
931+
syncSelectionFromSharedHandoff();
907932
}
908933
saveCustomPalettes();
909934
refs.searchInput.value = state.search;

tools/shared/assetUsageIntegration.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { getToolById } from "../toolRegistry.js";
22

33
export const SHARED_ASSET_HANDOFF_KEY = "toolboxaid.shared.assetHandoff";
44
export const SHARED_PALETTE_HANDOFF_KEY = "toolboxaid.shared.paletteHandoff";
5+
export const SHARED_ASSET_HANDOFF_EVENT = "toolboxaid.shared.assetHandoff.changed";
6+
export const SHARED_PALETTE_HANDOFF_EVENT = "toolboxaid.shared.paletteHandoff.changed";
57

68
export const SHARED_ACTION_LABELS = Object.freeze({
79
browseAssets: "Browse Assets",
@@ -65,6 +67,13 @@ function normalizePaletteColors(value) {
6567
}));
6668
}
6769

70+
function dispatchHandoffChanged(eventName, detail = {}) {
71+
if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") {
72+
return;
73+
}
74+
window.dispatchEvent(new CustomEvent(eventName, { detail }));
75+
}
76+
6877
export function normalizeSharedAssetHandoff(raw) {
6978
if (!isRecord(raw)) {
7079
return null;
@@ -235,6 +244,11 @@ export function writeSharedAssetHandoff(handoff) {
235244
return false;
236245
}
237246
window.localStorage.setItem(SHARED_ASSET_HANDOFF_KEY, JSON.stringify(normalized));
247+
dispatchHandoffChanged(SHARED_ASSET_HANDOFF_EVENT, {
248+
key: SHARED_ASSET_HANDOFF_KEY,
249+
action: "write",
250+
handoff: normalized
251+
});
238252
return true;
239253
}
240254

@@ -247,6 +261,11 @@ export function writeSharedPaletteHandoff(handoff) {
247261
return false;
248262
}
249263
window.localStorage.setItem(SHARED_PALETTE_HANDOFF_KEY, JSON.stringify(normalized));
264+
dispatchHandoffChanged(SHARED_PALETTE_HANDOFF_EVENT, {
265+
key: SHARED_PALETTE_HANDOFF_KEY,
266+
action: "write",
267+
handoff: normalized
268+
});
250269
return true;
251270
}
252271

@@ -255,6 +274,10 @@ export function clearSharedAssetHandoff() {
255274
return false;
256275
}
257276
window.localStorage.removeItem(SHARED_ASSET_HANDOFF_KEY);
277+
dispatchHandoffChanged(SHARED_ASSET_HANDOFF_EVENT, {
278+
key: SHARED_ASSET_HANDOFF_KEY,
279+
action: "clear"
280+
});
258281
return true;
259282
}
260283

@@ -263,5 +286,9 @@ export function clearSharedPaletteHandoff() {
263286
return false;
264287
}
265288
window.localStorage.removeItem(SHARED_PALETTE_HANDOFF_KEY);
289+
dispatchHandoffChanged(SHARED_PALETTE_HANDOFF_EVENT, {
290+
key: SHARED_PALETTE_HANDOFF_KEY,
291+
action: "clear"
292+
});
266293
return true;
267294
}

tools/shared/platformShell.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ const PRESERVED_TOOL_STATE_KEYS = new Set([
3535
HEADER_EXPANDED_STORAGE_KEY,
3636
WORKSPACE_LAUNCH_SIGNATURE_STORAGE_KEY
3737
]);
38-
const TOOLS_REQUIRING_WORKSPACE_TOOL_STATE = new Set([
39-
"skin-editor",
40-
"sprite-editor",
41-
"tile-map-editor",
42-
"parallax-editor",
43-
"vector-map-editor",
44-
"vector-asset-studio"
45-
]);
46-
4738
function getPageMode() {
4839
return document.body.dataset.toolsPlatformPage || "tool";
4940
}
@@ -982,30 +973,18 @@ function applyDocumentMetadata(currentTool) {
982973
document.body.classList.add("tools-platform-surface");
983974
const workspaceContext = isWorkspaceManagerContext();
984975
const lockState = resolveWorkspaceToolLockState();
985-
const searchParams = typeof window !== "undefined"
986-
? new URLSearchParams(window.location.search)
987-
: null;
988-
const launchHasSourcePreset = searchParams?.has("samplePresetPath") === true;
989976
const isToolSurfacePage = getPageMode() !== "landing";
990977
const isPaletteBrowser = currentTool?.id === "palette-browser";
991-
const toolRequiresState = TOOLS_REQUIRING_WORKSPACE_TOOL_STATE.has(currentTool?.id || "");
992-
const toolStateMissingLock = workspaceContext
993-
&& isToolSurfacePage
994-
&& toolRequiresState
995-
&& !launchHasSourcePreset
996-
&& !lockState.hasToolState(currentTool?.id);
997978
const workspaceMissingLock = workspaceContext && isToolSurfacePage && !lockState.workspaceReady;
998979
const paletteMissingLock = workspaceContext
999980
&& isToolSurfacePage
1000981
&& lockState.workspaceReady
1001982
&& !lockState.paletteReady
1002983
&& !isPaletteBrowser;
1003-
const shouldLockToolSurface = workspaceMissingLock || paletteMissingLock || toolStateMissingLock;
984+
const shouldLockToolSurface = workspaceMissingLock || paletteMissingLock;
1004985
const lockMessage = workspaceMissingLock
1005986
? "Create or open a workspace to use this tool."
1006-
: toolStateMissingLock
1007-
? "Workspace loaded with no source tool JSON for this tool. Load workspace JSON that includes this tool state."
1008-
: "Select a shared palette in Palette Browser to use this tool.";
987+
: "Select a shared palette in Palette Browser to use this tool.";
1009988
document.body.classList.toggle("tools-platform-workspace-context", workspaceContext);
1010989
document.body.classList.toggle("tools-platform-workspace-tool-locked", shouldLockToolSurface);
1011990
if (lastLockedSurfaceElement) {

0 commit comments

Comments
 (0)