BlueGit

timfa questward

Add editor chunk copy/paste/clear and arrow-key navigation.

Paste and clear stay draft-only until Save; arrow keys mirror the chunk nav buttons when not typing in a field.

timfa Tim Falken · pushed by timfa

Authored 2026-09-07 · 2 months ago

Hash
52ffc316d733d86006cd3c1251bbb15191430041
Parent
0a6728de
3 files changed +186 −1
client/editor.html +5 −0
@@ -43,6 +43,11 @@
43 43 <button type="button" id="tool-erase">Erase block</button>
44 44 </div>
45 45 <button id="save" type="button" disabled title="No unsaved changes">Save chunk</button>
46 <div class="chunk-clipboard">
47 <button id="copy-chunk" type="button" title="Copy this chunk into the editor clipboard">Copy chunk</button>
48 <button id="paste-chunk" type="button" disabled title="Paste a previously copied chunk">Paste chunk</button>
49 <button id="clear-chunk" type="button" title="Erase this chunk to a blank slate">Clear chunk</button>
50 </div>
46 51 <button id="apply" type="button" disabled title="Save a chunk first to create a pending revision">Apply now</button>
47 52 <p id="apply-status" class="hint" aria-live="polite"></p>
48 53 <label>Night preview <input id="night" type="range" min="0" max="24" step="0.1" value="12"><span id="night-label">12.0</span></label>
@@ -43,6 +43,11 @@
43 <button type="button" id="tool-erase">Erase block</button>
43 <button type="button" id="tool-erase">Erase block</button>
44 </div>
44 </div>
45 <button id="save" type="button" disabled title="No unsaved changes">Save chunk</button>
45 <button id="save" type="button" disabled title="No unsaved changes">Save chunk</button>
46 <div class="chunk-clipboard">
47 <button id="copy-chunk" type="button" title="Copy this chunk into the editor clipboard">Copy chunk</button>
48 <button id="paste-chunk" type="button" disabled title="Paste a previously copied chunk">Paste chunk</button>
49 <button id="clear-chunk" type="button" title="Erase this chunk to a blank slate">Clear chunk</button>
50 </div>
46 <button id="apply" type="button" disabled title="Save a chunk first to create a pending revision">Apply now</button>
51 <button id="apply" type="button" disabled title="Save a chunk first to create a pending revision">Apply now</button>
47 <p id="apply-status" class="hint" aria-live="polite"></p>
52 <p id="apply-status" class="hint" aria-live="polite"></p>
48 <label>Night preview <input id="night" type="range" min="0" max="24" step="0.1" value="12"><span id="night-label">12.0</span></label>
53 <label>Night preview <input id="night" type="range" min="0" max="24" step="0.1" value="12"><span id="night-label">12.0</span></label>
client/src/editor/main.js +179 −0
@@ -56,6 +56,8 @@ let view;
56 56 let draft = null;
57 57 /** @type {string|null} JSON snapshot of last loaded/saved draft (grounds/blocks/spawners/open). */
58 58 let draftBaseline = null;
59 /** @type {ReturnType<typeof cloneDraftContents>|null} In-memory chunk clipboard for copy/paste. */
60 let chunkClipboard = null;
59 61 /**
60 62 * Neighbor chunks whose connect masks were updated while editing the selected chunk.
61 63 * Key `"chunkX,chunkY"` → `{ open, grounds, blocks, spawners }`.
@@ -293,6 +295,141 @@ function draftPersistable ()
293 295 };
294 296 }
295 297
298 function cloneDraftContents (source)
299 {
300 if (!source)
301 { return null; }
302 const tileCount = CHUNK_SIZE * CHUNK_SIZE;
303 return {
304 open: !!source.open,
305 grounds: (source.grounds ?? []).map((ground) => normalizeGround(ground)),
306 blocks: (source.blocks ?? []).map((block) => (block
307 ? {
308 ...block,
309 config: block.config != null ? structuredClone(block.config) : null
310 }
311 : null)),
312 light: Array.isArray(source.light) && source.light.length === tileCount
313 ? source.light.slice()
314 : new Array(tileCount).fill(0),
315 spawners: (source.spawners ?? []).map((entry) => ({
316 localX: entry.localX | 0,
317 localY: entry.localY | 0,
318 config: structuredClone(entry.config ?? {})
319 }))
320 };
321 }
322
323 function blankChunkContents ()
324 {
325 const groundTypeId = dimensionsById.get(selectedDimensionId)?.defaultGroundTypeId
326 ?? GroundType.grass;
327 const tileCount = CHUNK_SIZE * CHUNK_SIZE;
328 return {
329 open: false,
330 grounds: new Array(tileCount).fill(null).map(() => normalizeGround(groundTypeId, groundTypeId)),
331 blocks: new Array(tileCount).fill(null),
332 light: new Array(tileCount).fill(0),
333 spawners: []
334 };
335 }
336
337 function applyDraftContents (next)
338 {
339 if (!draft || !next || isSelectedDimensionSoftDeleted())
340 { return; }
341 clearNeighborEditState();
342 draft.open = !!next.open;
343 draft.grounds = next.grounds.map((ground) => normalizeGround(ground));
344 draft.blocks = next.blocks.map((block) => (block
345 ? { ...block, config: block.config ?? null }
346 : null));
347 draft.light = Array.isArray(next.light) ? next.light.slice() : new Array(CHUNK_SIZE * CHUNK_SIZE).fill(0);
348 draft.spawners = (next.spawners ?? []).map((entry) => ({
349 localX: entry.localX | 0,
350 localY: entry.localY | 0,
351 config: { ...(entry.config ?? {}) }
352 }));
353 $("open").checked = draft.open;
354 const existing = view?.chunks.get(chunkKey(chunkX, chunkY));
355 view.applySnapshot({
356 chunkX,
357 chunkY,
358 open: draft.open,
359 closedEdges: existing?.closedEdges,
360 grounds: draft.grounds,
361 blocks: draft.blocks,
362 light: draft.light,
363 entities: []
364 });
365 syncSpawnersOverlay(chunkX, chunkY, draft.spawners);
366 afterDraftMutation();
367 }
368
369 function syncChunkClipboardButtons ()
370 {
371 const softDeleted = isSelectedDimensionSoftDeleted();
372 const copyBtn = $("copy-chunk");
373 const pasteBtn = $("paste-chunk");
374 const clearBtn = $("clear-chunk");
375 if (copyBtn)
376 {
377 copyBtn.disabled = !draft || softDeleted;
378 copyBtn.title = softDeleted
379 ? "Cannot copy while this dimension is soft-deleted"
380 : "Copy this chunk into the editor clipboard";
381 }
382 if (pasteBtn)
383 {
384 const canPaste = !!draft && !!chunkClipboard && !softDeleted;
385 pasteBtn.disabled = !canPaste;
386 pasteBtn.title = softDeleted
387 ? "Cannot paste while this dimension is soft-deleted"
388 : chunkClipboard
389 ? "Paste the copied chunk over the current draft (does not save)"
390 : "Copy a chunk first";
391 }
392 if (clearBtn)
393 {
394 clearBtn.disabled = !draft || softDeleted;
395 clearBtn.title = softDeleted
396 ? "Cannot clear while this dimension is soft-deleted"
397 : "Erase this chunk to a blank slate (does not save)";
398 }
399 }
400
401 function copySelectedChunk ()
402 {
403 if (!draft || isSelectedDimensionSoftDeleted())
404 { return; }
405 chunkClipboard = cloneDraftContents(draft);
406 syncChunkClipboardButtons();
407 }
408
409 function pasteSelectedChunk ()
410 {
411 if (!draft || !chunkClipboard || isSelectedDimensionSoftDeleted())
412 { return; }
413 const ok = window.confirm(
414 "This will replace your current chunk entirely with your copied chunk. Are you sure?"
415 );
416 if (!ok)
417 { return; }
418 applyDraftContents(cloneDraftContents(chunkClipboard));
419 }
420
421 function clearSelectedChunk ()
422 {
423 if (!draft || isSelectedDimensionSoftDeleted())
424 { return; }
425 const ok = window.confirm(
426 "this will erase the chunk and reset it to a default blank slate. Are you certain?"
427 );
428 if (!ok)
429 { return; }
430 applyDraftContents(blankChunkContents());
431 }
432
296 433 function captureDraftBaseline ()
297 434 {
298 435 const payload = draftPersistable();
@@ -328,6 +465,7 @@ function syncSaveButton ()
328 465 : dirty
329 466 ? "Save unsaved chunk changes"
330 467 : "No unsaved changes";
468 syncChunkClipboardButtons();
331 469 }
332 470
333 471 function afterDraftMutation ()
@@ -1591,6 +1729,47 @@ $("north").addEventListener("click", () => loadNeighborhood(chunkX, chunkY - 1))
1591 1729 $("south").addEventListener("click", () => loadNeighborhood(chunkX, chunkY + 1));
1592 1730 $("west").addEventListener("click", () => loadNeighborhood(chunkX - 1, chunkY));
1593 1731 $("east").addEventListener("click", () => loadNeighborhood(chunkX + 1, chunkY));
1732 $("copy-chunk")?.addEventListener("click", () => copySelectedChunk());
1733 $("paste-chunk")?.addEventListener("click", () => pasteSelectedChunk());
1734 $("clear-chunk")?.addEventListener("click", () => clearSelectedChunk());
1735
1736 function isTypingTarget (target)
1737 {
1738 if (!(target instanceof Element))
1739 { return false; }
1740 const tag = target.tagName;
1741 if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT")
1742 { return true; }
1743 return !!target.closest("[contenteditable='true']");
1744 }
1745
1746 window.addEventListener("keydown", (event) =>
1747 {
1748 if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
1749 { return; }
1750 if (anyModalOpen() || isTypingTarget(event.target))
1751 { return; }
1752 if (event.key === "ArrowUp")
1753 {
1754 event.preventDefault();
1755 loadNeighborhood(chunkX, chunkY - 1);
1756 }
1757 else if (event.key === "ArrowDown")
1758 {
1759 event.preventDefault();
1760 loadNeighborhood(chunkX, chunkY + 1);
1761 }
1762 else if (event.key === "ArrowLeft")
1763 {
1764 event.preventDefault();
1765 loadNeighborhood(chunkX - 1, chunkY);
1766 }
1767 else if (event.key === "ArrowRight")
1768 {
1769 event.preventDefault();
1770 loadNeighborhood(chunkX + 1, chunkY);
1771 }
1772 });
1594 1773 $("open").addEventListener("change", () =>
1595 1774 {
1596 1775 if (isSelectedDimensionSoftDeleted())
@@ -56,6 +56,8 @@ let view;
56let draft = null;
56let draft = null;
57/** @type {string|null} JSON snapshot of last loaded/saved draft (grounds/blocks/spawners/open). */
57/** @type {string|null} JSON snapshot of last loaded/saved draft (grounds/blocks/spawners/open). */
58let draftBaseline = null;
58let draftBaseline = null;
59/** @type {ReturnType<typeof cloneDraftContents>|null} In-memory chunk clipboard for copy/paste. */
60let chunkClipboard = null;
59/**
61/**
60 * Neighbor chunks whose connect masks were updated while editing the selected chunk.
62 * Neighbor chunks whose connect masks were updated while editing the selected chunk.
61 * Key `"chunkX,chunkY"` → `{ open, grounds, blocks, spawners }`.
63 * Key `"chunkX,chunkY"` → `{ open, grounds, blocks, spawners }`.
@@ -293,6 +295,141 @@ function draftPersistable ()
293 };
295 };
294}
296}
295
297
298function cloneDraftContents (source)
299{
300 if (!source)
301 { return null; }
302 const tileCount = CHUNK_SIZE * CHUNK_SIZE;
303 return {
304 open: !!source.open,
305 grounds: (source.grounds ?? []).map((ground) => normalizeGround(ground)),
306 blocks: (source.blocks ?? []).map((block) => (block
307 ? {
308 ...block,
309 config: block.config != null ? structuredClone(block.config) : null
310 }
311 : null)),
312 light: Array.isArray(source.light) && source.light.length === tileCount
313 ? source.light.slice()
314 : new Array(tileCount).fill(0),
315 spawners: (source.spawners ?? []).map((entry) => ({
316 localX: entry.localX | 0,
317 localY: entry.localY | 0,
318 config: structuredClone(entry.config ?? {})
319 }))
320 };
321}
322
323function blankChunkContents ()
324{
325 const groundTypeId = dimensionsById.get(selectedDimensionId)?.defaultGroundTypeId
326 ?? GroundType.grass;
327 const tileCount = CHUNK_SIZE * CHUNK_SIZE;
328 return {
329 open: false,
330 grounds: new Array(tileCount).fill(null).map(() => normalizeGround(groundTypeId, groundTypeId)),
331 blocks: new Array(tileCount).fill(null),
332 light: new Array(tileCount).fill(0),
333 spawners: []
334 };
335}
336
337function applyDraftContents (next)
338{
339 if (!draft || !next || isSelectedDimensionSoftDeleted())
340 { return; }
341 clearNeighborEditState();
342 draft.open = !!next.open;
343 draft.grounds = next.grounds.map((ground) => normalizeGround(ground));
344 draft.blocks = next.blocks.map((block) => (block
345 ? { ...block, config: block.config ?? null }
346 : null));
347 draft.light = Array.isArray(next.light) ? next.light.slice() : new Array(CHUNK_SIZE * CHUNK_SIZE).fill(0);
348 draft.spawners = (next.spawners ?? []).map((entry) => ({
349 localX: entry.localX | 0,
350 localY: entry.localY | 0,
351 config: { ...(entry.config ?? {}) }
352 }));
353 $("open").checked = draft.open;
354 const existing = view?.chunks.get(chunkKey(chunkX, chunkY));
355 view.applySnapshot({
356 chunkX,
357 chunkY,
358 open: draft.open,
359 closedEdges: existing?.closedEdges,
360 grounds: draft.grounds,
361 blocks: draft.blocks,
362 light: draft.light,
363 entities: []
364 });
365 syncSpawnersOverlay(chunkX, chunkY, draft.spawners);
366 afterDraftMutation();
367}
368
369function syncChunkClipboardButtons ()
370{
371 const softDeleted = isSelectedDimensionSoftDeleted();
372 const copyBtn = $("copy-chunk");
373 const pasteBtn = $("paste-chunk");
374 const clearBtn = $("clear-chunk");
375 if (copyBtn)
376 {
377 copyBtn.disabled = !draft || softDeleted;
378 copyBtn.title = softDeleted
379 ? "Cannot copy while this dimension is soft-deleted"
380 : "Copy this chunk into the editor clipboard";
381 }
382 if (pasteBtn)
383 {
384 const canPaste = !!draft && !!chunkClipboard && !softDeleted;
385 pasteBtn.disabled = !canPaste;
386 pasteBtn.title = softDeleted
387 ? "Cannot paste while this dimension is soft-deleted"
388 : chunkClipboard
389 ? "Paste the copied chunk over the current draft (does not save)"
390 : "Copy a chunk first";
391 }
392 if (clearBtn)
393 {
394 clearBtn.disabled = !draft || softDeleted;
395 clearBtn.title = softDeleted
396 ? "Cannot clear while this dimension is soft-deleted"
397 : "Erase this chunk to a blank slate (does not save)";
398 }
399}
400
401function copySelectedChunk ()
402{
403 if (!draft || isSelectedDimensionSoftDeleted())
404 { return; }
405 chunkClipboard = cloneDraftContents(draft);
406 syncChunkClipboardButtons();
407}
408
409function pasteSelectedChunk ()
410{
411 if (!draft || !chunkClipboard || isSelectedDimensionSoftDeleted())
412 { return; }
413 const ok = window.confirm(
414 "This will replace your current chunk entirely with your copied chunk. Are you sure?"
415 );
416 if (!ok)
417 { return; }
418 applyDraftContents(cloneDraftContents(chunkClipboard));
419}
420
421function clearSelectedChunk ()
422{
423 if (!draft || isSelectedDimensionSoftDeleted())
424 { return; }
425 const ok = window.confirm(
426 "this will erase the chunk and reset it to a default blank slate. Are you certain?"
427 );
428 if (!ok)
429 { return; }
430 applyDraftContents(blankChunkContents());
431}
432
296function captureDraftBaseline ()
433function captureDraftBaseline ()
297{
434{
298 const payload = draftPersistable();
435 const payload = draftPersistable();
@@ -328,6 +465,7 @@ function syncSaveButton ()
328 : dirty
465 : dirty
329 ? "Save unsaved chunk changes"
466 ? "Save unsaved chunk changes"
330 : "No unsaved changes";
467 : "No unsaved changes";
468 syncChunkClipboardButtons();
331}
469}
332
470
333function afterDraftMutation ()
471function afterDraftMutation ()
@@ -1591,6 +1729,47 @@ $("north").addEventListener("click", () => loadNeighborhood(chunkX, chunkY - 1))
1591$("south").addEventListener("click", () => loadNeighborhood(chunkX, chunkY + 1));
1729$("south").addEventListener("click", () => loadNeighborhood(chunkX, chunkY + 1));
1592$("west").addEventListener("click", () => loadNeighborhood(chunkX - 1, chunkY));
1730$("west").addEventListener("click", () => loadNeighborhood(chunkX - 1, chunkY));
1593$("east").addEventListener("click", () => loadNeighborhood(chunkX + 1, chunkY));
1731$("east").addEventListener("click", () => loadNeighborhood(chunkX + 1, chunkY));
1732$("copy-chunk")?.addEventListener("click", () => copySelectedChunk());
1733$("paste-chunk")?.addEventListener("click", () => pasteSelectedChunk());
1734$("clear-chunk")?.addEventListener("click", () => clearSelectedChunk());
1735
1736function isTypingTarget (target)
1737{
1738 if (!(target instanceof Element))
1739 { return false; }
1740 const tag = target.tagName;
1741 if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT")
1742 { return true; }
1743 return !!target.closest("[contenteditable='true']");
1744}
1745
1746window.addEventListener("keydown", (event) =>
1747{
1748 if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
1749 { return; }
1750 if (anyModalOpen() || isTypingTarget(event.target))
1751 { return; }
1752 if (event.key === "ArrowUp")
1753 {
1754 event.preventDefault();
1755 loadNeighborhood(chunkX, chunkY - 1);
1756 }
1757 else if (event.key === "ArrowDown")
1758 {
1759 event.preventDefault();
1760 loadNeighborhood(chunkX, chunkY + 1);
1761 }
1762 else if (event.key === "ArrowLeft")
1763 {
1764 event.preventDefault();
1765 loadNeighborhood(chunkX - 1, chunkY);
1766 }
1767 else if (event.key === "ArrowRight")
1768 {
1769 event.preventDefault();
1770 loadNeighborhood(chunkX + 1, chunkY);
1771 }
1772});
1594$("open").addEventListener("change", () =>
1773$("open").addEventListener("change", () =>
1595{
1774{
1596 if (isSelectedDimensionSoftDeleted())
1775 if (isSelectedDimensionSoftDeleted())
client/ui/editor.css +2 −1
@@ -145,7 +145,8 @@ body>aside {
145 145 }
146 146
147 147 .nav,
148 .palette {
148 .palette,
149 .chunk-clipboard {
149 150 display: flex;
150 151 flex-direction: column;
151 152 gap: 0.35rem;
@@ -145,7 +145,8 @@ body>aside {
145}
145}
146
146
147.nav,
147.nav,
148.palette {
148.palette,
149.chunk-clipboard {
149 display: flex;
150 display: flex;
150 flex-direction: column;
151 flex-direction: column;
151 gap: 0.35rem;
152 gap: 0.35rem;