From 3e4fb937e6a4136205dfe5c8b4b583637ac5ddbe Mon Sep 17 00:00:00 2001 From: Kartikey Gupta <58729031+krtkygpta@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:26:51 +0530 Subject: [PATCH 1/2] Make duplicated layers independent and expose watch customization modes --- .../modules/editor/core/document/factory.ts | 17 +++- src/lib/modules/editor/model/doc.model.ts | 10 ++- src/lib/modules/editor/model/edit.model.ts | 79 +++++++++++++++---- src/lib/modules/editor/pages/editor.svelte | 32 ++++++++ tests/doc-factory.test.ts | 13 +++ tests/editor-clipboard.browser.test.ts | 28 +++++-- tests/editor-new-nodes.browser.test.ts | 8 +- 7 files changed, 160 insertions(+), 27 deletions(-) diff --git a/src/lib/modules/editor/core/document/factory.ts b/src/lib/modules/editor/core/document/factory.ts index e274ee8..5c9a075 100644 --- a/src/lib/modules/editor/core/document/factory.ts +++ b/src/lib/modules/editor/core/document/factory.ts @@ -232,7 +232,7 @@ export const newCondition = (): Condition => ({ source: 0x19, op: "gte", value: // byte (meaning unknown) so a round trip stays exact. The 0x86 node toLegacy writes is derived // from `name`, so it needs nothing here. export function withName(doc: Doc, name: string): Doc { - const tail = doc.nameRaw ? unhex(doc.nameRaw)[15] : 0x0a; + const tail = capabilityByte(doc); const head = new Uint8Array(16); new TextEncoder().encodeInto(name.slice(0, 14), head.subarray(0, 15)); @@ -240,6 +240,21 @@ export function withName(doc: Doc, name: string): Doc { return { ...doc, name, nameRaw: hex(head) }; } +/** Byte 0x17 controls which on-watch customization modes a face exposes. It is the last byte + * of the 16-byte header name field and buildBin mirrors the whole header into the footer. */ +export const capabilityByte = (doc: Doc): number => + doc.nameRaw ? (unhex(doc.nameRaw)[15] ?? 0x0a) : 0x0a; + +export function withCapabilityByte(doc: Doc, byte: number): Doc { + const head = doc.nameRaw ? unhex(doc.nameRaw).slice() : new Uint8Array(16); + + if (!doc.nameRaw) { + new TextEncoder().encodeInto((doc.name ?? "").slice(0, 14), head.subarray(0, 15)); + } + head[15] = byte & 0xff; + return { ...doc, nameRaw: hex(head) }; +} + const assetOf = (r: Resource): ImageAsset => ({ id: newImageId(), cf: r.cf, diff --git a/src/lib/modules/editor/model/doc.model.ts b/src/lib/modules/editor/model/doc.model.ts index 1ebf14a..0252504 100644 --- a/src/lib/modules/editor/model/doc.model.ts +++ b/src/lib/modules/editor/model/doc.model.ts @@ -8,7 +8,7 @@ import { createEvent, createStore, sample } from "effector"; import { reset } from "patronum"; import { findLayer, patchLayer } from "../core/document/edits"; -import { withName } from "../core/document/factory"; +import { withCapabilityByte, withName } from "../core/document/factory"; import { toLegacy, type Doc, @@ -70,6 +70,7 @@ export const checkpoint = createEvent(); export const undo = createEvent(); export const redo = createEvent(); export const renameFace = createEvent(); +export const capabilityByteSet = createEvent(); /** A freshly opened or created document, replacing whatever was loaded. `cache` carries the * decoded pixels, so the document and something to draw arrive in the same update. */ export const docLoaded = createEvent<{ @@ -151,6 +152,13 @@ sample({ fn: (doc, name) => withName(doc!, name), target: $doc, }); +sample({ + clock: capabilityByteSet, + source: $doc, + filter: Boolean, + fn: (doc, byte) => ({ edit: (current: Doc) => withCapabilityByte(current, byte) }), + target: committed, +}); // ---- history ---- sample({ diff --git a/src/lib/modules/editor/model/edit.model.ts b/src/lib/modules/editor/model/edit.model.ts index 52ea7b8..aff854b 100644 --- a/src/lib/modules/editor/model/edit.model.ts +++ b/src/lib/modules/editor/model/edit.model.ts @@ -292,23 +292,69 @@ sample({ target: committed, }); -// ponytail: the copy shares its assets with the original, so resize/adjust/invert on one hits -// both. Splitting them means cloning every frame — do it if that ever surprises anyone. +const duplicated = createEvent<{ + edit: (doc: Doc) => Doc; + added: NodeId; + cache: Map; +}>(); + +sample({ + clock: duplicated, + fn: ({ edit }) => ({ edit }), + target: committed, +}); +sample({ + clock: duplicated, + fn: ({ added }) => ({ added }), + target: selectAdded, +}); +sample({ + clock: duplicated, + filter: ({ cache }) => cache.size > 0, + fn: ({ cache }) => cache, + target: cachePatched, +}); + +// Duplicates own their image assets, so adjustments to either layer stay independent. sample({ clock: duplicateRequested, - source: $selected, - filter: (selected) => selected.length > 0, + source: { selected: $selected, doc: $doc, cache: $cache }, + filter: ({ selected, doc }) => selected.length > 0 && Boolean(doc), // cloned here rather than inside the edit so the new ids are known now and the selection can // move to the copy, the way duplicating in any editor behaves - fn: (selected) => { - const copies = selected.map(cloneLayer); + fn: ({ selected, doc, cache }) => { + const images = new Map(); + const cached = new Map(); + const copies = selected.map((layer) => { + const copy = cloneLayer(layer); + const remap = new Map(); + + for (const id of assetsOf(layer)) { + const asset = doc!.images.get(id); + + if (!asset) continue; + const fresh = newImageId(); + + remap.set(id, fresh); + images.set(fresh, { ...asset, id: fresh, data: asset.data.slice() }); + const imageCache = cache.get(id); + + if (imageCache) cached.set(fresh, { ...imageCache }); + } + return repointFrames(copy, remap); + }); return { - edit: (doc: Doc) => selected.reduce((d, l, i) => insertAfter(d, l.id, copies[i]), doc), + edit: (current: Doc) => { + const withCopies = { ...current, images: new Map([...current.images, ...images]) }; + + return selected.reduce((d, l, i) => insertAfter(d, l.id, copies[i]), withCopies); + }, added: copies[0].id, + cache: cached, }; }, - target: [committed, selectAdded], + target: duplicated, }); // ---- clipboard ---- @@ -382,21 +428,20 @@ sample({ }), target: committed, }); -const resolvePaste = (clip: Clip, doc: Doc): Pasted => { - // An id already in this document is the same picture only when it's the very same object — - // the counters restart per document, so everything else gets a fresh id and a copied asset. +const resolvePaste = (clip: Clip): Pasted => { + // Every paste gets private assets, even within the same document, so edits don't leak to the + // source layer. Asset IDs are also per-document, so copies need fresh IDs across documents. const remap = new Map(); const images = new Map(); const cache = new Map(); for (const [id, a] of clip.images) { - if (doc.images.get(id) === a) continue; const fresh = newImageId(); const c = clip.cache.get(id); remap.set(id, fresh); - images.set(fresh, { ...a, id: fresh }); - if (c) cache.set(fresh, c); + images.set(fresh, { ...a, id: fresh, data: a.data.slice() }); + if (c) cache.set(fresh, { ...c }); } return { layers: clip.layers.map((l) => repointFrames(cloneLayer(l), remap)), images, cache }; }; @@ -405,7 +450,7 @@ sample({ clock: pasteRequested, source: { clip: $clipboard, doc: $doc }, filter: ({ clip, doc }) => Boolean(clip?.layers.length && doc), - fn: ({ clip, doc }) => resolvePaste(clip!, doc!), + fn: ({ clip }) => resolvePaste(clip!), target: pasted, }); @@ -446,9 +491,9 @@ sample({ }); sample({ clock: imported, - fn: ({ doc, clips }) => + fn: ({ clips }) => clips - .map((clip) => resolvePaste(clip, doc!)) + .map((clip) => resolvePaste(clip)) .reduce((a, b) => ({ layers: [...a.layers, ...b.layers], images: new Map([...a.images, ...b.images]), diff --git a/src/lib/modules/editor/pages/editor.svelte b/src/lib/modules/editor/pages/editor.svelte index 42098c8..79947a2 100644 --- a/src/lib/modules/editor/pages/editor.svelte +++ b/src/lib/modules/editor/pages/editor.svelte @@ -7,6 +7,7 @@ import { Icon } from "$lib/shared/components/icon"; import { holdCursor } from "$lib/shared/lib/hold-cursor"; import { Menu, MenuItem } from "$lib/shared/components/menu"; + import { Select } from "$lib/shared/components/select"; import { authModel } from "$lib/modules/auth/model"; import { marketModel } from "$lib/modules/market/model"; import PublishDialog from "../components/PublishDialog.svelte"; @@ -48,6 +49,7 @@ type ShortcutActions, } from "../shared/shortcuts"; import { editorModel } from "../model"; + import { capabilityByte } from "../core/document/factory"; import TreePanel from "../components/TreePanel.svelte"; import PropsPanel from "../components/PropsPanel.svelte"; import SimPanel from "../components/SimPanel.svelte"; @@ -120,6 +122,7 @@ buildCurrentBin, previewBlob, previewThumb, + capabilityByteSet, $showWatchFrame: showWatchFrame, watchFrameToggled, $rightPanel: rightPanel, @@ -127,6 +130,22 @@ $loading: loading, } = editorModel; + const customizationOptions = [ + { value: "0x08", label: "No customization", short: "None" }, + { value: "0x0a", label: "Accent color customization", short: "Accent color" }, + { value: "0x0e", label: "Functions and color customization", short: "Functions + colors" }, + ]; + + function customizationValue() { + const byte = $doc ? capabilityByte($doc) : 0x0a; + + // Stock files also contain function-only and mixed legacy flags. Classify them into the + // closest option until the user chooses a mode explicitly. + if (byte & 0x04) return "0x0e"; + if (byte & 0x02) return "0x0a"; + return "0x08"; + } + let canvas = $state(null); let fileInput = $state(); let dirInput = $state(); @@ -640,6 +659,13 @@ />
+ +