BlueGit

timfa questward

Merge branch 'cursor/fix-chunk-editor-discard-862b' into 'master'

Fix chunk-editor discard leaving dirty paint that can overwrite another chunk

See merge request Timfa/questward!3

timfa Tim Falken · pushed by timfa

Authored 2026-09-17 · 2 months ago

Hash
266d6d0347d09413eca7a8b33f5587612b8c40d1
3 files changed +212 −26
client/src/editor/main.js +69 −21
@@ -56,7 +56,14 @@ import
56 56 isOverworld
57 57 } from "/shared/world/dimensions.js";
58 58 import { normalizeDoorConfig } from "/shared/world/doorRequirements.js";
59 import { editorChunkSaveKey, shouldApplySelectedEditorChunk } from "./saveAck.js";
59 import
60 {
61 editorChunkSaveKey,
62 editorDirtyChunk,
63 editorDraftOwner,
64 sameEditorChunk,
65 shouldApplySelectedEditorChunk
66 } from "./saveAck.js";
60 67
61 68 const game = new Game();
62 69 let socket;
@@ -65,6 +72,12 @@ let view;
65 72 let draft = null;
66 73 /** @type {string|null} JSON snapshot of last loaded/saved draft (grounds/blocks/spawners/open). */
67 74 let draftBaseline = null;
75 /**
76 * Chunk the current `draft` belongs to. Navigation may update `chunkX`/`chunkY`
77 * (viewed chunk) before save/discard; save must still write to this owner.
78 * @type {{ dimensionId: string, chunkX: number, chunkY: number }|null}
79 */
80 let draftOwner = null;
68 81 /** @type {ReturnType<typeof cloneDraftContents>|null} In-memory chunk clipboard for copy/paste. */
69 82 let chunkClipboard = null;
70 83 /**
@@ -333,17 +346,23 @@ function draftStorageKey (cx = chunkX, cy = chunkY, dimensionId = selectedDimens
333 346 return `${EDITOR_DRAFT_STORAGE_PREFIX}${who}:${normalizeDimensionId(dimensionId)}:${cx},${cy}`;
334 347 }
335 348
349 function viewedChunkOwner ()
350 {
351 return editorDraftOwner(selectedDimensionId, chunkX, chunkY);
352 }
353
336 354 function persistDirtyDraftBackup ()
337 355 {
338 356 if (!draft || !isSelectedChunkDirty())
339 357 { return; }
358 const owner = editorDirtyChunk(draftOwner, viewedChunkOwner());
340 359 try
341 360 {
342 361 const payload = {
343 362 savedAt: Date.now(),
344 dimensionId: selectedDimensionId,
345 chunkX,
346 chunkY,
363 dimensionId: owner.dimensionId,
364 chunkX: owner.chunkX,
365 chunkY: owner.chunkY,
347 366 authSubject: sessionAuthSubject,
348 367 open: !!draft.open,
349 368 grounds: draft.grounds,
@@ -353,7 +372,10 @@ function persistDirtyDraftBackup ()
353 372 enclosed: draft.enclosed,
354 373 baseline: draftBaseline
355 374 };
356 sessionStorage.setItem(draftStorageKey(), JSON.stringify(payload));
375 sessionStorage.setItem(
376 draftStorageKey(owner.chunkX, owner.chunkY, owner.dimensionId),
377 JSON.stringify(payload)
378 );
357 379 }
358 380 catch
359 381 { /* quota / private mode */ }
@@ -426,7 +448,8 @@ function applyRestoredDraftBackup (backup)
426 448
427 449 function tryRestoreDraftBackupAfterServerLoad (payload)
428 450 {
429 const backup = readDraftBackup(payload.chunkX, payload.chunkY, selectedDimensionId);
451 const dim = normalizeDimensionId(payload.dimensionId ?? selectedDimensionId);
452 const backup = readDraftBackup(payload.chunkX, payload.chunkY, dim);
430 453 if (!backup)
431 454 { return false; }
432 455 const serverPersistable = {
@@ -443,7 +466,7 @@ function tryRestoreDraftBackupAfterServerLoad (payload)
443 466 };
444 467 if (JSON.stringify(serverPersistable) === JSON.stringify(backupPersistable))
445 468 {
446 clearDraftBackup(payload.chunkX, payload.chunkY, selectedDimensionId);
469 clearDraftBackup(payload.chunkX, payload.chunkY, dim);
447 470 return false;
448 471 }
449 472 // Treat server payload as the clean baseline so the restored paint stays dirty.
@@ -594,7 +617,19 @@ function captureDraftBaseline ()
594 617 {
595 618 const payload = draftPersistable();
596 619 draftBaseline = payload ? JSON.stringify(payload) : null;
597 clearDraftBackup();
620 const owner = editorDirtyChunk(draftOwner, viewedChunkOwner());
621 clearDraftBackup(owner.chunkX, owner.chunkY, owner.dimensionId);
622 syncSaveButton();
623 }
624
625 function discardUnsavedDraft ()
626 {
627 const owner = editorDirtyChunk(draftOwner, viewedChunkOwner());
628 clearDraftBackup(owner.chunkX, owner.chunkY, owner.dimensionId);
629 clearNeighborEditState();
630 draft = null;
631 draftBaseline = null;
632 draftOwner = null;
598 633 syncSaveButton();
599 634 }
600 635
@@ -630,10 +665,11 @@ function syncSaveButton ()
630 665 function afterDraftMutation ()
631 666 {
632 667 syncSaveButton();
668 const owner = editorDirtyChunk(draftOwner, viewedChunkOwner());
633 669 if (isSelectedChunkDirty())
634 670 { persistDirtyDraftBackup(); }
635 671 else
636 { clearDraftBackup(); }
672 { clearDraftBackup(owner.chunkX, owner.chunkY, owner.dimensionId); }
637 673 }
638 674
639 675 function clearNeighborEditState ()
@@ -837,15 +873,16 @@ function saveSelectedChunk ()
837 873 $("error").textContent = "";
838 874 $("apply-status").textContent = "";
839 875 $("apply-status").className = "hint";
876 const target = editorDirtyChunk(draftOwner, viewedChunkOwner());
840 877 const neighborSaves = [...dirtyNeighborChunks.entries()];
841 878 clearNeighborEditState();
842 const savedChunks = [{ chunkX, chunkY }];
843 pendingSaveAcks.add(editorChunkSaveKey(selectedDimensionId, chunkX, chunkY));
879 const savedChunks = [{ chunkX: target.chunkX, chunkY: target.chunkY }];
880 pendingSaveAcks.add(editorChunkSaveKey(target.dimensionId, target.chunkX, target.chunkY));
844 881 socket.send({
845 882 type: MessageType.editorSaveChunk,
846 chunkX,
847 chunkY,
848 dimensionId: selectedDimensionId,
883 chunkX: target.chunkX,
884 chunkY: target.chunkY,
885 dimensionId: target.dimensionId,
849 886 draft: {
850 887 open: $("open").checked,
851 888 grounds: draft.grounds,
@@ -857,12 +894,12 @@ function saveSelectedChunk ()
857 894 {
858 895 const [cx, cy] = key.split(",").map(Number);
859 896 savedChunks.push({ chunkX: cx, chunkY: cy });
860 pendingSaveAcks.add(editorChunkSaveKey(selectedDimensionId, cx, cy));
897 pendingSaveAcks.add(editorChunkSaveKey(target.dimensionId, cx, cy));
861 898 socket.send({
862 899 type: MessageType.editorSaveChunk,
863 900 chunkX: cx,
864 901 chunkY: cy,
865 dimensionId: selectedDimensionId,
902 dimensionId: target.dimensionId,
866 903 draft: {
867 904 open: data.open,
868 905 grounds: data.grounds,
@@ -979,6 +1016,7 @@ function clearNeighborhoodView ()
979 1016 {
980 1017 draft = null;
981 1018 draftBaseline = null;
1019 draftOwner = null;
982 1020 clearNeighborEditState();
983 1021 neighborSpawnersByChunk.clear();
984 1022 if (!view)
@@ -1135,15 +1173,23 @@ function setRevisionState ({ activeRevisionId: active, pendingRevisionId: pendin
1135 1173
1136 1174 function showSelectedChunk (payload, { isSaveAck = false } = {})
1137 1175 {
1176 const incoming = editorDraftOwner(
1177 payload.dimensionId ?? selectedDimensionId,
1178 payload.chunkX,
1179 payload.chunkY
1180 );
1138 1181 const keepDirtyDraft = !shouldApplySelectedEditorChunk({
1139 1182 isSaveAck,
1140 isDirty: !!draft
1141 && payload.chunkX === chunkX
1142 && payload.chunkY === chunkY
1143 && isSelectedChunkDirty()
1183 isDirty: !!draft && isSelectedChunkDirty(),
1184 incoming,
1185 draftOwner
1144 1186 });
1145 1187 if (!keepDirtyDraft)
1146 { clearNeighborEditState(); }
1188 {
1189 if (draftOwner && !sameEditorChunk(incoming, draftOwner))
1190 { clearDraftBackup(draftOwner.chunkX, draftOwner.chunkY, draftOwner.dimensionId); }
1191 clearNeighborEditState();
1192 }
1147 1193 if (keepDirtyDraft)
1148 1194 {
1149 1195 setRevisionState({
@@ -1168,6 +1214,7 @@ function showSelectedChunk (payload, { isSaveAck = false } = {})
1168 1214 config: { ...entry.config }
1169 1215 }))
1170 1216 };
1217 draftOwner = incoming;
1171 1218 $("coords").textContent = `${payload.chunkX}, ${payload.chunkY}`;
1172 1219 $("open").checked = draft.open;
1173 1220 setRevisionState({
@@ -2162,6 +2209,7 @@ $("unsaved-chunk-discard")?.addEventListener("click", () =>
2162 2209 hideUnsavedModal();
2163 2210 pendingNavigate = null;
2164 2211 navigateAfterSave = false;
2212 discardUnsavedDraft();
2165 2213 if (!next)
2166 2214 { return; }
2167 2215 const nextDim = next.dimensionId != null ? normalizeDimensionId(next.dimensionId) : selectedDimensionId;
@@ -56,7 +56,14 @@ import
56 isOverworld
56 isOverworld
57} from "/shared/world/dimensions.js";
57} from "/shared/world/dimensions.js";
58import { normalizeDoorConfig } from "/shared/world/doorRequirements.js";
58import { normalizeDoorConfig } from "/shared/world/doorRequirements.js";
59import { editorChunkSaveKey, shouldApplySelectedEditorChunk } from "./saveAck.js";
59import
60{
61 editorChunkSaveKey,
62 editorDirtyChunk,
63 editorDraftOwner,
64 sameEditorChunk,
65 shouldApplySelectedEditorChunk
66} from "./saveAck.js";
60
67
61const game = new Game();
68const game = new Game();
62let socket;
69let socket;
@@ -65,6 +72,12 @@ let view;
65let draft = null;
72let draft = null;
66/** @type {string|null} JSON snapshot of last loaded/saved draft (grounds/blocks/spawners/open). */
73/** @type {string|null} JSON snapshot of last loaded/saved draft (grounds/blocks/spawners/open). */
67let draftBaseline = null;
74let draftBaseline = null;
75/**
76 * Chunk the current `draft` belongs to. Navigation may update `chunkX`/`chunkY`
77 * (viewed chunk) before save/discard; save must still write to this owner.
78 * @type {{ dimensionId: string, chunkX: number, chunkY: number }|null}
79 */
80let draftOwner = null;
68/** @type {ReturnType<typeof cloneDraftContents>|null} In-memory chunk clipboard for copy/paste. */
81/** @type {ReturnType<typeof cloneDraftContents>|null} In-memory chunk clipboard for copy/paste. */
69let chunkClipboard = null;
82let chunkClipboard = null;
70/**
83/**
@@ -333,17 +346,23 @@ function draftStorageKey (cx = chunkX, cy = chunkY, dimensionId = selectedDimens
333 return `${EDITOR_DRAFT_STORAGE_PREFIX}${who}:${normalizeDimensionId(dimensionId)}:${cx},${cy}`;
346 return `${EDITOR_DRAFT_STORAGE_PREFIX}${who}:${normalizeDimensionId(dimensionId)}:${cx},${cy}`;
334}
347}
335
348
349function viewedChunkOwner ()
350{
351 return editorDraftOwner(selectedDimensionId, chunkX, chunkY);
352}
353
336function persistDirtyDraftBackup ()
354function persistDirtyDraftBackup ()
337{
355{
338 if (!draft || !isSelectedChunkDirty())
356 if (!draft || !isSelectedChunkDirty())
339 { return; }
357 { return; }
358 const owner = editorDirtyChunk(draftOwner, viewedChunkOwner());
340 try
359 try
341 {
360 {
342 const payload = {
361 const payload = {
343 savedAt: Date.now(),
362 savedAt: Date.now(),
344 dimensionId: selectedDimensionId,
363 dimensionId: owner.dimensionId,
345 chunkX,
364 chunkX: owner.chunkX,
346 chunkY,
365 chunkY: owner.chunkY,
347 authSubject: sessionAuthSubject,
366 authSubject: sessionAuthSubject,
348 open: !!draft.open,
367 open: !!draft.open,
349 grounds: draft.grounds,
368 grounds: draft.grounds,
@@ -353,7 +372,10 @@ function persistDirtyDraftBackup ()
353 enclosed: draft.enclosed,
372 enclosed: draft.enclosed,
354 baseline: draftBaseline
373 baseline: draftBaseline
355 };
374 };
356 sessionStorage.setItem(draftStorageKey(), JSON.stringify(payload));
375 sessionStorage.setItem(
376 draftStorageKey(owner.chunkX, owner.chunkY, owner.dimensionId),
377 JSON.stringify(payload)
378 );
357 }
379 }
358 catch
380 catch
359 { /* quota / private mode */ }
381 { /* quota / private mode */ }
@@ -426,7 +448,8 @@ function applyRestoredDraftBackup (backup)
426
448
427function tryRestoreDraftBackupAfterServerLoad (payload)
449function tryRestoreDraftBackupAfterServerLoad (payload)
428{
450{
429 const backup = readDraftBackup(payload.chunkX, payload.chunkY, selectedDimensionId);
451 const dim = normalizeDimensionId(payload.dimensionId ?? selectedDimensionId);
452 const backup = readDraftBackup(payload.chunkX, payload.chunkY, dim);
430 if (!backup)
453 if (!backup)
431 { return false; }
454 { return false; }
432 const serverPersistable = {
455 const serverPersistable = {
@@ -443,7 +466,7 @@ function tryRestoreDraftBackupAfterServerLoad (payload)
443 };
466 };
444 if (JSON.stringify(serverPersistable) === JSON.stringify(backupPersistable))
467 if (JSON.stringify(serverPersistable) === JSON.stringify(backupPersistable))
445 {
468 {
446 clearDraftBackup(payload.chunkX, payload.chunkY, selectedDimensionId);
469 clearDraftBackup(payload.chunkX, payload.chunkY, dim);
447 return false;
470 return false;
448 }
471 }
449 // Treat server payload as the clean baseline so the restored paint stays dirty.
472 // Treat server payload as the clean baseline so the restored paint stays dirty.
@@ -594,7 +617,19 @@ function captureDraftBaseline ()
594{
617{
595 const payload = draftPersistable();
618 const payload = draftPersistable();
596 draftBaseline = payload ? JSON.stringify(payload) : null;
619 draftBaseline = payload ? JSON.stringify(payload) : null;
597 clearDraftBackup();
620 const owner = editorDirtyChunk(draftOwner, viewedChunkOwner());
621 clearDraftBackup(owner.chunkX, owner.chunkY, owner.dimensionId);
622 syncSaveButton();
623}
624
625function discardUnsavedDraft ()
626{
627 const owner = editorDirtyChunk(draftOwner, viewedChunkOwner());
628 clearDraftBackup(owner.chunkX, owner.chunkY, owner.dimensionId);
629 clearNeighborEditState();
630 draft = null;
631 draftBaseline = null;
632 draftOwner = null;
598 syncSaveButton();
633 syncSaveButton();
599}
634}
600
635
@@ -630,10 +665,11 @@ function syncSaveButton ()
630function afterDraftMutation ()
665function afterDraftMutation ()
631{
666{
632 syncSaveButton();
667 syncSaveButton();
668 const owner = editorDirtyChunk(draftOwner, viewedChunkOwner());
633 if (isSelectedChunkDirty())
669 if (isSelectedChunkDirty())
634 { persistDirtyDraftBackup(); }
670 { persistDirtyDraftBackup(); }
635 else
671 else
636 { clearDraftBackup(); }
672 { clearDraftBackup(owner.chunkX, owner.chunkY, owner.dimensionId); }
637}
673}
638
674
639function clearNeighborEditState ()
675function clearNeighborEditState ()
@@ -837,15 +873,16 @@ function saveSelectedChunk ()
837 $("error").textContent = "";
873 $("error").textContent = "";
838 $("apply-status").textContent = "";
874 $("apply-status").textContent = "";
839 $("apply-status").className = "hint";
875 $("apply-status").className = "hint";
876 const target = editorDirtyChunk(draftOwner, viewedChunkOwner());
840 const neighborSaves = [...dirtyNeighborChunks.entries()];
877 const neighborSaves = [...dirtyNeighborChunks.entries()];
841 clearNeighborEditState();
878 clearNeighborEditState();
842 const savedChunks = [{ chunkX, chunkY }];
879 const savedChunks = [{ chunkX: target.chunkX, chunkY: target.chunkY }];
843 pendingSaveAcks.add(editorChunkSaveKey(selectedDimensionId, chunkX, chunkY));
880 pendingSaveAcks.add(editorChunkSaveKey(target.dimensionId, target.chunkX, target.chunkY));
844 socket.send({
881 socket.send({
845 type: MessageType.editorSaveChunk,
882 type: MessageType.editorSaveChunk,
846 chunkX,
883 chunkX: target.chunkX,
847 chunkY,
884 chunkY: target.chunkY,
848 dimensionId: selectedDimensionId,
885 dimensionId: target.dimensionId,
849 draft: {
886 draft: {
850 open: $("open").checked,
887 open: $("open").checked,
851 grounds: draft.grounds,
888 grounds: draft.grounds,
@@ -857,12 +894,12 @@ function saveSelectedChunk ()
857 {
894 {
858 const [cx, cy] = key.split(",").map(Number);
895 const [cx, cy] = key.split(",").map(Number);
859 savedChunks.push({ chunkX: cx, chunkY: cy });
896 savedChunks.push({ chunkX: cx, chunkY: cy });
860 pendingSaveAcks.add(editorChunkSaveKey(selectedDimensionId, cx, cy));
897 pendingSaveAcks.add(editorChunkSaveKey(target.dimensionId, cx, cy));
861 socket.send({
898 socket.send({
862 type: MessageType.editorSaveChunk,
899 type: MessageType.editorSaveChunk,
863 chunkX: cx,
900 chunkX: cx,
864 chunkY: cy,
901 chunkY: cy,
865 dimensionId: selectedDimensionId,
902 dimensionId: target.dimensionId,
866 draft: {
903 draft: {
867 open: data.open,
904 open: data.open,
868 grounds: data.grounds,
905 grounds: data.grounds,
@@ -979,6 +1016,7 @@ function clearNeighborhoodView ()
979{
1016{
980 draft = null;
1017 draft = null;
981 draftBaseline = null;
1018 draftBaseline = null;
1019 draftOwner = null;
982 clearNeighborEditState();
1020 clearNeighborEditState();
983 neighborSpawnersByChunk.clear();
1021 neighborSpawnersByChunk.clear();
984 if (!view)
1022 if (!view)
@@ -1135,15 +1173,23 @@ function setRevisionState ({ activeRevisionId: active, pendingRevisionId: pendin
1135
1173
1136function showSelectedChunk (payload, { isSaveAck = false } = {})
1174function showSelectedChunk (payload, { isSaveAck = false } = {})
1137{
1175{
1176 const incoming = editorDraftOwner(
1177 payload.dimensionId ?? selectedDimensionId,
1178 payload.chunkX,
1179 payload.chunkY
1180 );
1138 const keepDirtyDraft = !shouldApplySelectedEditorChunk({
1181 const keepDirtyDraft = !shouldApplySelectedEditorChunk({
1139 isSaveAck,
1182 isSaveAck,
1140 isDirty: !!draft
1183 isDirty: !!draft && isSelectedChunkDirty(),
1141 && payload.chunkX === chunkX
1184 incoming,
1142 && payload.chunkY === chunkY
1185 draftOwner
1143 && isSelectedChunkDirty()
1144 });
1186 });
1145 if (!keepDirtyDraft)
1187 if (!keepDirtyDraft)
1146 { clearNeighborEditState(); }
1188 {
1189 if (draftOwner && !sameEditorChunk(incoming, draftOwner))
1190 { clearDraftBackup(draftOwner.chunkX, draftOwner.chunkY, draftOwner.dimensionId); }
1191 clearNeighborEditState();
1192 }
1147 if (keepDirtyDraft)
1193 if (keepDirtyDraft)
1148 {
1194 {
1149 setRevisionState({
1195 setRevisionState({
@@ -1168,6 +1214,7 @@ function showSelectedChunk (payload, { isSaveAck = false } = {})
1168 config: { ...entry.config }
1214 config: { ...entry.config }
1169 }))
1215 }))
1170 };
1216 };
1217 draftOwner = incoming;
1171 $("coords").textContent = `${payload.chunkX}, ${payload.chunkY}`;
1218 $("coords").textContent = `${payload.chunkX}, ${payload.chunkY}`;
1172 $("open").checked = draft.open;
1219 $("open").checked = draft.open;
1173 setRevisionState({
1220 setRevisionState({
@@ -2162,6 +2209,7 @@ $("unsaved-chunk-discard")?.addEventListener("click", () =>
2162 hideUnsavedModal();
2209 hideUnsavedModal();
2163 pendingNavigate = null;
2210 pendingNavigate = null;
2164 navigateAfterSave = false;
2211 navigateAfterSave = false;
2212 discardUnsavedDraft();
2165 if (!next)
2213 if (!next)
2166 { return; }
2214 { return; }
2167 const nextDim = next.dimensionId != null ? normalizeDimensionId(next.dimensionId) : selectedDimensionId;
2215 const nextDim = next.dimensionId != null ? normalizeDimensionId(next.dimensionId) : selectedDimensionId;
client/src/editor/saveAck.js +47 −4
@@ -12,13 +12,56 @@ export function editorChunkSaveKey (dimensionId, chunkX, chunkY)
12 12 return `${normalizeDimensionId(dimensionId ?? OVERWORLD_ID)}:${chunkX | 0},${chunkY | 0}`;
13 13 }
14 14
15 /** Identity of the chunk a local editor draft belongs to. */
16 export function editorDraftOwner (dimensionId, chunkX, chunkY)
17 {
18 return {
19 dimensionId: normalizeDimensionId(dimensionId ?? OVERWORLD_ID),
20 chunkX: chunkX | 0,
21 chunkY: chunkY | 0
22 };
23 }
24
25 export function sameEditorChunk (a, b)
26 {
27 if (!a || !b)
28 { return false; }
29 return editorChunkSaveKey(a.dimensionId, a.chunkX, a.chunkY)
30 === editorChunkSaveKey(b.dimensionId, b.chunkX, b.chunkY);
31 }
32
33 /**
34 * Chunk the pending local edits belong to — used for save, discard, and
35 * sessionStorage backup. Never the chunk merely selected after navigation.
36 */
37 export function editorDirtyChunk (draftOwner, viewed)
38 {
39 return draftOwner ?? viewed;
40 }
41
15 42 /**
16 43 * Whether an incoming selected-chunk `EDITOR_CHUNK` should replace the local draft.
17 * Save acks must apply even while dirty. Non-ack reloads must not wipe unsaved paint.
44 * Save acks must apply even while dirty. Non-ack reloads must not wipe unsaved paint
45 * on the same chunk. A payload for a *different* chunk must not inherit leftover
46 * dirty paint from the chunk that was just left.
18 47 *
19 * @param {{ isSaveAck?: boolean, isDirty?: boolean }} options
48 * @param {{
49 * isSaveAck?: boolean,
50 * isDirty?: boolean,
51 * incoming?: { dimensionId?: string, chunkX?: number, chunkY?: number }|null,
52 * draftOwner?: { dimensionId?: string, chunkX?: number, chunkY?: number }|null
53 * }} options
20 54 */
21 export function shouldApplySelectedEditorChunk ({ isSaveAck = false, isDirty = false } = {})
55 export function shouldApplySelectedEditorChunk ({
56 isSaveAck = false,
57 isDirty = false,
58 incoming = null,
59 draftOwner = null
60 } = {})
22 61 {
23 return !!isSaveAck || !isDirty;
62 if (isSaveAck || !isDirty)
63 { return true; }
64 if (incoming && draftOwner && !sameEditorChunk(incoming, draftOwner))
65 { return true; }
66 return false;
24 67 }
@@ -12,13 +12,56 @@ export function editorChunkSaveKey (dimensionId, chunkX, chunkY)
12 return `${normalizeDimensionId(dimensionId ?? OVERWORLD_ID)}:${chunkX | 0},${chunkY | 0}`;
12 return `${normalizeDimensionId(dimensionId ?? OVERWORLD_ID)}:${chunkX | 0},${chunkY | 0}`;
13}
13}
14
14
15/** Identity of the chunk a local editor draft belongs to. */
16export function editorDraftOwner (dimensionId, chunkX, chunkY)
17{
18 return {
19 dimensionId: normalizeDimensionId(dimensionId ?? OVERWORLD_ID),
20 chunkX: chunkX | 0,
21 chunkY: chunkY | 0
22 };
23}
24
25export function sameEditorChunk (a, b)
26{
27 if (!a || !b)
28 { return false; }
29 return editorChunkSaveKey(a.dimensionId, a.chunkX, a.chunkY)
30 === editorChunkSaveKey(b.dimensionId, b.chunkX, b.chunkY);
31}
32
33/**
34 * Chunk the pending local edits belong to — used for save, discard, and
35 * sessionStorage backup. Never the chunk merely selected after navigation.
36 */
37export function editorDirtyChunk (draftOwner, viewed)
38{
39 return draftOwner ?? viewed;
40}
41
15/**
42/**
16 * Whether an incoming selected-chunk `EDITOR_CHUNK` should replace the local draft.
43 * Whether an incoming selected-chunk `EDITOR_CHUNK` should replace the local draft.
17 * Save acks must apply even while dirty. Non-ack reloads must not wipe unsaved paint.
44 * Save acks must apply even while dirty. Non-ack reloads must not wipe unsaved paint
45 * on the same chunk. A payload for a *different* chunk must not inherit leftover
46 * dirty paint from the chunk that was just left.
18 *
47 *
19 * @param {{ isSaveAck?: boolean, isDirty?: boolean }} options
48 * @param {{
49 * isSaveAck?: boolean,
50 * isDirty?: boolean,
51 * incoming?: { dimensionId?: string, chunkX?: number, chunkY?: number }|null,
52 * draftOwner?: { dimensionId?: string, chunkX?: number, chunkY?: number }|null
53 * }} options
20 */
54 */
21export function shouldApplySelectedEditorChunk ({ isSaveAck = false, isDirty = false } = {})
55export function shouldApplySelectedEditorChunk ({
56 isSaveAck = false,
57 isDirty = false,
58 incoming = null,
59 draftOwner = null
60} = {})
22{
61{
23 return !!isSaveAck || !isDirty;
62 if (isSaveAck || !isDirty)
63 { return true; }
64 if (incoming && draftOwner && !sameEditorChunk(incoming, draftOwner))
65 { return true; }
66 return false;
24}
67}
client/tests/editor-save-ack.test.js +96 −1
@@ -1,7 +1,13 @@
1 1 import test from "node:test";
2 2 import assert from "node:assert/strict";
3 3 import { OVERWORLD_ID } from "../../shared/world/dimensions.js";
4 import { editorChunkSaveKey, shouldApplySelectedEditorChunk } from "../src/editor/saveAck.js";
4 import {
5 editorChunkSaveKey,
6 editorDirtyChunk,
7 editorDraftOwner,
8 sameEditorChunk,
9 shouldApplySelectedEditorChunk
10 } from "../src/editor/saveAck.js";
5 11
6 12 test("editorChunkSaveKey is dimension-scoped", () =>
7 13 {
@@ -25,3 +31,92 @@ test("non-ack reload keeps unsaved paint on the selected chunk", () =>
25 31 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: true }), false);
26 32 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: false }), true);
27 33 });
34
35 test("sameEditorChunk is dimension-aware", () =>
36 {
37 const overworld = editorDraftOwner(OVERWORLD_ID, 0, 0);
38 const cave = editorDraftOwner("Cave A", 0, 0);
39 assert.equal(sameEditorChunk(overworld, editorDraftOwner(OVERWORLD_ID, 0, 0)), true);
40 assert.equal(sameEditorChunk(overworld, editorDraftOwner(OVERWORLD_ID, 1, 0)), false);
41 assert.equal(sameEditorChunk(overworld, cave), false);
42 });
43
44 test("dirty-chunk identity prefers the draft owner over the viewed chunk", () =>
45 {
46 const edited = editorDraftOwner(OVERWORLD_ID, 2, 3);
47 const viewed = editorDraftOwner(OVERWORLD_ID, 4, 5);
48 assert.deepEqual(editorDirtyChunk(edited, viewed), edited);
49 assert.deepEqual(editorDirtyChunk(null, viewed), viewed);
50 assert.deepEqual(
51 editorDirtyChunk(
52 editorDraftOwner("Cave A", 0, 0),
53 editorDraftOwner(OVERWORLD_ID, 0, 0)
54 ),
55 editorDraftOwner("Cave A", 0, 0)
56 );
57 });
58
59 test("incoming payload for a different chunk does not inherit leftover dirty paint", () =>
60 {
61 const chunkA = editorDraftOwner(OVERWORLD_ID, 0, 0);
62 const chunkB = editorDraftOwner(OVERWORLD_ID, 1, 0);
63 assert.equal(
64 shouldApplySelectedEditorChunk({
65 isSaveAck: false,
66 isDirty: true,
67 incoming: chunkB,
68 draftOwner: chunkA
69 }),
70 true
71 );
72 assert.equal(
73 shouldApplySelectedEditorChunk({
74 isSaveAck: false,
75 isDirty: true,
76 incoming: chunkA,
77 draftOwner: chunkA
78 }),
79 false
80 );
81 assert.equal(
82 shouldApplySelectedEditorChunk({
83 isSaveAck: false,
84 isDirty: true,
85 incoming: editorDraftOwner("Cave A", 0, 0),
86 draftOwner: chunkA
87 }),
88 true
89 );
90 });
91
92 test("discard-then-navigate regression: leftover A edits must not save onto B", () =>
93 {
94 const chunkA = editorDraftOwner(OVERWORLD_ID, 0, 0);
95 const chunkB = editorDraftOwner(OVERWORLD_ID, 1, 0);
96
97 // If navigation already moved the view to B while A's draft was still in
98 // memory (the old bug), save/discard still target A and B's load replaces A paint.
99 assert.deepEqual(editorDirtyChunk(chunkA, chunkB), chunkA);
100 assert.equal(
101 shouldApplySelectedEditorChunk({
102 isSaveAck: false,
103 isDirty: true,
104 incoming: chunkB,
105 draftOwner: chunkA
106 }),
107 true
108 );
109 assert.notEqual(
110 editorChunkSaveKey(chunkA.dimensionId, chunkA.chunkX, chunkA.chunkY),
111 editorChunkSaveKey(chunkB.dimensionId, chunkB.chunkX, chunkB.chunkY)
112 );
113 assert.equal(
114 shouldApplySelectedEditorChunk({
115 isSaveAck: false,
116 isDirty: false,
117 incoming: chunkB,
118 draftOwner: null
119 }),
120 true
121 );
122 });
@@ -1,7 +1,13 @@
1import test from "node:test";
1import test from "node:test";
2import assert from "node:assert/strict";
2import assert from "node:assert/strict";
3import { OVERWORLD_ID } from "../../shared/world/dimensions.js";
3import { OVERWORLD_ID } from "../../shared/world/dimensions.js";
4import { editorChunkSaveKey, shouldApplySelectedEditorChunk } from "../src/editor/saveAck.js";
4import {
5 editorChunkSaveKey,
6 editorDirtyChunk,
7 editorDraftOwner,
8 sameEditorChunk,
9 shouldApplySelectedEditorChunk
10} from "../src/editor/saveAck.js";
5
11
6test("editorChunkSaveKey is dimension-scoped", () =>
12test("editorChunkSaveKey is dimension-scoped", () =>
7{
13{
@@ -25,3 +31,92 @@ test("non-ack reload keeps unsaved paint on the selected chunk", () =>
25 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: true }), false);
31 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: true }), false);
26 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: false }), true);
32 assert.equal(shouldApplySelectedEditorChunk({ isSaveAck: false, isDirty: false }), true);
27});
33});
34
35test("sameEditorChunk is dimension-aware", () =>
36{
37 const overworld = editorDraftOwner(OVERWORLD_ID, 0, 0);
38 const cave = editorDraftOwner("Cave A", 0, 0);
39 assert.equal(sameEditorChunk(overworld, editorDraftOwner(OVERWORLD_ID, 0, 0)), true);
40 assert.equal(sameEditorChunk(overworld, editorDraftOwner(OVERWORLD_ID, 1, 0)), false);
41 assert.equal(sameEditorChunk(overworld, cave), false);
42});
43
44test("dirty-chunk identity prefers the draft owner over the viewed chunk", () =>
45{
46 const edited = editorDraftOwner(OVERWORLD_ID, 2, 3);
47 const viewed = editorDraftOwner(OVERWORLD_ID, 4, 5);
48 assert.deepEqual(editorDirtyChunk(edited, viewed), edited);
49 assert.deepEqual(editorDirtyChunk(null, viewed), viewed);
50 assert.deepEqual(
51 editorDirtyChunk(
52 editorDraftOwner("Cave A", 0, 0),
53 editorDraftOwner(OVERWORLD_ID, 0, 0)
54 ),
55 editorDraftOwner("Cave A", 0, 0)
56 );
57});
58
59test("incoming payload for a different chunk does not inherit leftover dirty paint", () =>
60{
61 const chunkA = editorDraftOwner(OVERWORLD_ID, 0, 0);
62 const chunkB = editorDraftOwner(OVERWORLD_ID, 1, 0);
63 assert.equal(
64 shouldApplySelectedEditorChunk({
65 isSaveAck: false,
66 isDirty: true,
67 incoming: chunkB,
68 draftOwner: chunkA
69 }),
70 true
71 );
72 assert.equal(
73 shouldApplySelectedEditorChunk({
74 isSaveAck: false,
75 isDirty: true,
76 incoming: chunkA,
77 draftOwner: chunkA
78 }),
79 false
80 );
81 assert.equal(
82 shouldApplySelectedEditorChunk({
83 isSaveAck: false,
84 isDirty: true,
85 incoming: editorDraftOwner("Cave A", 0, 0),
86 draftOwner: chunkA
87 }),
88 true
89 );
90});
91
92test("discard-then-navigate regression: leftover A edits must not save onto B", () =>
93{
94 const chunkA = editorDraftOwner(OVERWORLD_ID, 0, 0);
95 const chunkB = editorDraftOwner(OVERWORLD_ID, 1, 0);
96
97 // If navigation already moved the view to B while A's draft was still in
98 // memory (the old bug), save/discard still target A and B's load replaces A paint.
99 assert.deepEqual(editorDirtyChunk(chunkA, chunkB), chunkA);
100 assert.equal(
101 shouldApplySelectedEditorChunk({
102 isSaveAck: false,
103 isDirty: true,
104 incoming: chunkB,
105 draftOwner: chunkA
106 }),
107 true
108 );
109 assert.notEqual(
110 editorChunkSaveKey(chunkA.dimensionId, chunkA.chunkX, chunkA.chunkY),
111 editorChunkSaveKey(chunkB.dimensionId, chunkB.chunkX, chunkB.chunkY)
112 );
113 assert.equal(
114 shouldApplySelectedEditorChunk({
115 isSaveAck: false,
116 isDirty: false,
117 incoming: chunkB,
118 draftOwner: null
119 }),
120 true
121 );
122});