@weasel-js/ui-canvas 0.0.0-stage → 1.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -2
- package/dist/chunks/AnchorFields-Cam1xwpt.js +69 -0
- package/dist/chunks/AnchorFields-Cam1xwpt.js.map +1 -0
- package/dist/chunks/CharacterOptions-BcT_lWjg.js +284 -0
- package/dist/chunks/CharacterOptions-BcT_lWjg.js.map +1 -0
- package/dist/chunks/PlotDataPanel-DUNod3NJ.js +257 -0
- package/dist/chunks/PlotDataPanel-DUNod3NJ.js.map +1 -0
- package/dist/chunks/Rulers-hkPFdPkH.js +136 -0
- package/dist/chunks/Rulers-hkPFdPkH.js.map +1 -0
- package/dist/chunks/SceneGradientHandles-CHNSWx7U.js +77 -0
- package/dist/chunks/SceneGradientHandles-CHNSWx7U.js.map +1 -0
- package/dist/chunks/SceneLayerList-DCYwqZkF.js +182 -0
- package/dist/chunks/SceneLayerList-DCYwqZkF.js.map +1 -0
- package/dist/chunks/SelectionPanel-n-dIMvQ2.js +323 -0
- package/dist/chunks/SelectionPanel-n-dIMvQ2.js.map +1 -0
- package/dist/components/PlotDataPanel/index.js +3 -0
- package/dist/components/Rulers/index.js +3 -0
- package/dist/components/SceneGradientHandles/index.js +3 -0
- package/dist/components/SceneLayerList/index.js +3 -0
- package/dist/components/inspector/AnchorFields/index.js +3 -0
- package/dist/components/inspector/CharacterOptions/index.js +3 -0
- package/dist/components/inspector/SelectionPanel/index.js +3 -0
- package/dist/index.js +9 -0
- package/dist/kitIcons.js +18 -0
- package/dist/kitIcons.js.map +1 -0
- package/dist/style.css +119 -0
- package/package.json +83 -4
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
|
+
import { PropertyControl, PropertyField, PropertyList, PropertyNote, PropertyPanel, Subpanel } from "@weasel-js/ui/components/Properties";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { plotSchemaOf } from "@weasel-js/plot";
|
|
5
|
+
import { Button, CLOSE_BUTTON_PROPS } from "@weasel-js/ui/components/Button";
|
|
6
|
+
import { DataGrid } from "@weasel-js/ui/components/DataGrid";
|
|
7
|
+
|
|
8
|
+
// src/components/PlotDataPanel/PlotDataPanel.module.css
|
|
9
|
+
var table = "_table_8c611e";
|
|
10
|
+
var cell = "_cell_8c611e";
|
|
11
|
+
var heading = "_heading_8c611e";
|
|
12
|
+
var nested = "_nested_8c611e";
|
|
13
|
+
var rowActions = "_rowActions_8c611e";
|
|
14
|
+
var add = "_add_8c611e";
|
|
15
|
+
var PlotDataPanel_module_default = {
|
|
16
|
+
table,
|
|
17
|
+
cell,
|
|
18
|
+
heading,
|
|
19
|
+
nested,
|
|
20
|
+
rowActions,
|
|
21
|
+
add
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/components/PlotDataPanel/valueControl.ts
|
|
25
|
+
/** The props of the control for `spec`, reporting each committed value through `set`. */
|
|
26
|
+
function valueControl(spec, name, set) {
|
|
27
|
+
const { type, value, optional, readOnly } = spec;
|
|
28
|
+
const clear = optional ? () => set(void 0) : void 0;
|
|
29
|
+
switch (type.kind) {
|
|
30
|
+
case "number": return {
|
|
31
|
+
kind: "number",
|
|
32
|
+
name,
|
|
33
|
+
readOnly,
|
|
34
|
+
value: typeof value === "number" ? value : void 0,
|
|
35
|
+
min: type.min,
|
|
36
|
+
max: type.max,
|
|
37
|
+
step: type.step,
|
|
38
|
+
onChange: set,
|
|
39
|
+
onClear: clear
|
|
40
|
+
};
|
|
41
|
+
case "boolean": return {
|
|
42
|
+
kind: "boolean",
|
|
43
|
+
name,
|
|
44
|
+
readOnly,
|
|
45
|
+
value: value === true,
|
|
46
|
+
onChange: set
|
|
47
|
+
};
|
|
48
|
+
case "enum": return {
|
|
49
|
+
kind: "enum",
|
|
50
|
+
name,
|
|
51
|
+
readOnly,
|
|
52
|
+
value: typeof value === "string" ? value : void 0,
|
|
53
|
+
options: type.options.map((o) => ({
|
|
54
|
+
value: o,
|
|
55
|
+
label: o
|
|
56
|
+
})),
|
|
57
|
+
onChange: set,
|
|
58
|
+
onClear: clear
|
|
59
|
+
};
|
|
60
|
+
case "color": return {
|
|
61
|
+
kind: "color",
|
|
62
|
+
name,
|
|
63
|
+
readOnly,
|
|
64
|
+
value: typeof value === "string" ? value : void 0,
|
|
65
|
+
onChange: set
|
|
66
|
+
};
|
|
67
|
+
case "string": return {
|
|
68
|
+
kind: "string",
|
|
69
|
+
name,
|
|
70
|
+
readOnly,
|
|
71
|
+
value: typeof value === "string" ? value : void 0,
|
|
72
|
+
placeholder: type.placeholder,
|
|
73
|
+
onChange: (next) => set(next === "" && optional ? void 0 : next)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/components/PlotDataPanel/rowIds.ts
|
|
79
|
+
var insertedAt = (at) => (ids, fresh) => [
|
|
80
|
+
...ids.slice(0, at),
|
|
81
|
+
fresh(),
|
|
82
|
+
...ids.slice(at)
|
|
83
|
+
];
|
|
84
|
+
var removedAt = (at) => (ids) => ids.filter((_, i) => i !== at);
|
|
85
|
+
var movedTo = (from, to) => (ids) => {
|
|
86
|
+
const next = ids.filter((_, i) => i !== from);
|
|
87
|
+
next.splice(to, 0, ids[from]);
|
|
88
|
+
return next;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* An id for each of a table's `count` rows that stays with its item through the panel's own inserts, removals and
|
|
92
|
+
* moves, so what a row holds open follows the item rather than its slot. `track` wraps an edit with what it does to
|
|
93
|
+
* the order; the order shifts only once `data` comes back as that edit made it, so an edit the host drops shifts
|
|
94
|
+
* nothing. A change from elsewhere keeps the ids by position.
|
|
95
|
+
*/
|
|
96
|
+
function useRowIds(data, count) {
|
|
97
|
+
const s = useRef({
|
|
98
|
+
ids: [],
|
|
99
|
+
made: 0,
|
|
100
|
+
seen: void 0,
|
|
101
|
+
pending: []
|
|
102
|
+
}).current;
|
|
103
|
+
const fresh = () => `row-${s.made++}`;
|
|
104
|
+
if (s.seen !== data) {
|
|
105
|
+
const mine = s.pending.find((p) => p.made === data);
|
|
106
|
+
if (mine) s.ids = mine.shift(s.ids, fresh);
|
|
107
|
+
s.pending = [];
|
|
108
|
+
s.seen = data;
|
|
109
|
+
}
|
|
110
|
+
if (s.ids.length !== count) s.ids = Array.from({ length: count }, (_, i) => s.ids[i] ?? fresh());
|
|
111
|
+
return {
|
|
112
|
+
ids: s.ids,
|
|
113
|
+
track: (change, shift) => (d) => {
|
|
114
|
+
const made = change(d);
|
|
115
|
+
s.pending.push({
|
|
116
|
+
made,
|
|
117
|
+
shift
|
|
118
|
+
});
|
|
119
|
+
return made;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// src/components/PlotDataPanel/PlotTableView.tsx
|
|
125
|
+
/** One table of a plot schema as a grid of controls: a row per item, which can be added, removed, dragged into
|
|
126
|
+
* another order, and opened onto the tables it holds. */
|
|
127
|
+
function PlotTableView({ data, table, scope, edit }) {
|
|
128
|
+
const count = table.count(data, scope);
|
|
129
|
+
const { ids, track } = useRowIds(data, count);
|
|
130
|
+
const rows = ids.map((id, index) => ({
|
|
131
|
+
id,
|
|
132
|
+
index
|
|
133
|
+
}));
|
|
134
|
+
const rowName = (r) => table.rowLabel?.(data, scope, r) || `${table.item} ${r + 1}`;
|
|
135
|
+
const columns = table.columns(data, scope).map((column) => ({
|
|
136
|
+
id: column.key,
|
|
137
|
+
header: column.label,
|
|
138
|
+
sortable: false,
|
|
139
|
+
numeric: column.type.kind === "number",
|
|
140
|
+
render: ({ index }) => {
|
|
141
|
+
if (column.applies?.(data, scope, index) === false) return null;
|
|
142
|
+
const spec = {
|
|
143
|
+
...column,
|
|
144
|
+
value: column.get(data, scope, index)
|
|
145
|
+
};
|
|
146
|
+
const set = (value) => edit((d) => column.set(d, scope, index, value), `Edit ${column.label.toLowerCase()}`);
|
|
147
|
+
return /* @__PURE__ */ jsx(PropertyControl, {
|
|
148
|
+
...valueControl(spec, `${column.label} of ${rowName(index)}`, set),
|
|
149
|
+
className: PlotDataPanel_module_default.cell
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}));
|
|
153
|
+
const { insert, remove } = table;
|
|
154
|
+
if (insert || remove) columns.push({
|
|
155
|
+
id: "__row",
|
|
156
|
+
header: "",
|
|
157
|
+
sortable: false,
|
|
158
|
+
render: ({ index }) => /* @__PURE__ */ jsxs("span", {
|
|
159
|
+
className: PlotDataPanel_module_default.rowActions,
|
|
160
|
+
children: [insert && /* @__PURE__ */ jsx(Button, {
|
|
161
|
+
size: "sm",
|
|
162
|
+
variant: "ghost",
|
|
163
|
+
"aria-label": `Insert ${table.item} after ${rowName(index)}`,
|
|
164
|
+
onClick: () => edit(track((d) => insert(d, scope, index + 1), insertedAt(index + 1)), `Add ${table.item}`),
|
|
165
|
+
children: "+"
|
|
166
|
+
}), remove && /* @__PURE__ */ jsx(Button, {
|
|
167
|
+
...CLOSE_BUTTON_PROPS,
|
|
168
|
+
"aria-label": `Remove ${rowName(index)}`,
|
|
169
|
+
onClick: () => edit(track((d) => remove(d, scope, index), removedAt(index)), `Remove ${table.item}`)
|
|
170
|
+
})]
|
|
171
|
+
})
|
|
172
|
+
});
|
|
173
|
+
const move = table.move;
|
|
174
|
+
const children = table.children;
|
|
175
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
176
|
+
className: PlotDataPanel_module_default.table,
|
|
177
|
+
children: [
|
|
178
|
+
scope.length > 0 && /* @__PURE__ */ jsx("div", {
|
|
179
|
+
className: PlotDataPanel_module_default.heading,
|
|
180
|
+
children: table.label
|
|
181
|
+
}),
|
|
182
|
+
/* @__PURE__ */ jsx(DataGrid, {
|
|
183
|
+
rows,
|
|
184
|
+
columns,
|
|
185
|
+
rowLabel: (r) => rowName(r.index),
|
|
186
|
+
empty: `No ${table.label.toLowerCase()}`,
|
|
187
|
+
onReorder: move && ((moved, target) => {
|
|
188
|
+
const from = rows.findIndex((r) => r.id === moved[0]);
|
|
189
|
+
const to = target > from ? target - 1 : target;
|
|
190
|
+
if (from >= 0 && to !== from) edit(track((d) => move(d, scope, from, to), movedTo(from, to)), `Reorder ${table.label.toLowerCase()}`);
|
|
191
|
+
}),
|
|
192
|
+
renderDetail: children && (({ index }) => /* @__PURE__ */ jsx("div", {
|
|
193
|
+
className: PlotDataPanel_module_default.nested,
|
|
194
|
+
children: children.map((child) => /* @__PURE__ */ jsx(PlotTableView, {
|
|
195
|
+
data,
|
|
196
|
+
table: child,
|
|
197
|
+
scope: [...scope, index],
|
|
198
|
+
edit
|
|
199
|
+
}, child.key))
|
|
200
|
+
}))
|
|
201
|
+
}),
|
|
202
|
+
insert && /* @__PURE__ */ jsx(Button, {
|
|
203
|
+
size: "sm",
|
|
204
|
+
variant: "ghost",
|
|
205
|
+
className: PlotDataPanel_module_default.add,
|
|
206
|
+
onClick: () => edit(track((d) => insert(d, scope, table.count(d, scope)), insertedAt(count)), `Add ${table.item}`),
|
|
207
|
+
children: `Add ${table.item}`
|
|
208
|
+
})
|
|
209
|
+
]
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/components/PlotDataPanel/PlotDataPanel.tsx
|
|
214
|
+
/**
|
|
215
|
+
* Authors a chart's data, whatever its kind, from the kind's schema (`plotSchemaOf`): a row per field, and a grid
|
|
216
|
+
* per table whose rows can be added, removed, reordered, and opened onto the tables they hold.
|
|
217
|
+
*/
|
|
218
|
+
function PlotDataPanel({ data, onChange, title = "Data", className }) {
|
|
219
|
+
const [error, setError] = useState(null);
|
|
220
|
+
const schema = plotSchemaOf(data);
|
|
221
|
+
const edit = (change, label) => {
|
|
222
|
+
try {
|
|
223
|
+
const next = change(data);
|
|
224
|
+
setError(null);
|
|
225
|
+
if (next !== data) onChange(next, label);
|
|
226
|
+
} catch (e) {
|
|
227
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
const fields = schema.fields.filter((f) => f.applies?.(data) !== false);
|
|
231
|
+
return /* @__PURE__ */ jsxs(PropertyPanel, {
|
|
232
|
+
title,
|
|
233
|
+
className,
|
|
234
|
+
children: [/* @__PURE__ */ jsxs(PropertyList, { children: [fields.map((field) => {
|
|
235
|
+
const set = (value) => edit((d) => field.set(d, value), `Edit ${field.label.toLowerCase()}`);
|
|
236
|
+
const control = valueControl({
|
|
237
|
+
...field,
|
|
238
|
+
value: field.get(data)
|
|
239
|
+
}, field.label, set);
|
|
240
|
+
return /* @__PURE__ */ jsx(PropertyField, {
|
|
241
|
+
label: field.label,
|
|
242
|
+
...control
|
|
243
|
+
}, field.key);
|
|
244
|
+
}), error !== null && /* @__PURE__ */ jsx(PropertyNote, { children: error })] }), schema.tables.map((table) => /* @__PURE__ */ jsx(Subpanel, {
|
|
245
|
+
title: table.label,
|
|
246
|
+
children: /* @__PURE__ */ jsx(PlotTableView, {
|
|
247
|
+
data,
|
|
248
|
+
table,
|
|
249
|
+
scope: [],
|
|
250
|
+
edit
|
|
251
|
+
})
|
|
252
|
+
}, table.key))]
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export { PlotTableView as n, PlotDataPanel as t };
|
|
257
|
+
//# sourceMappingURL=PlotDataPanel-DUNod3NJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlotDataPanel-DUNod3NJ.js","names":["s"],"sources":["../../src/components/PlotDataPanel/PlotDataPanel.module.css","../../src/components/PlotDataPanel/valueControl.ts","../../src/components/PlotDataPanel/rowIds.ts","../../src/components/PlotDataPanel/PlotTableView.tsx","../../src/components/PlotDataPanel/PlotDataPanel.tsx"],"sourcesContent":[".table {\n display: flex;\n flex-direction: column;\n align-items: flex-start;\n gap: var(--wzl-space-2);\n /* A grid wider than its sidebar scrolls inside it rather than stretching the panel. */\n max-inline-size: 100%;\n overflow-x: auto;\n}\n\n.cell {\n min-inline-size: 4rem;\n inline-size: 100%;\n}\n\n.heading {\n font-size: var(--wzl-font-size-sm);\n font-weight: var(--wzl-font-weight-bold);\n color: var(--wzl-fg-muted);\n}\n\n.nested {\n display: flex;\n flex-direction: column;\n gap: var(--wzl-space-3);\n padding-inline-start: var(--wzl-space-4);\n}\n\n.rowActions {\n display: inline-flex;\n align-items: center;\n gap: var(--wzl-space-1);\n}\n\n.add {\n align-self: flex-start;\n}\n","/** A plot schema field or cell as the props of the params control that edits it. */\nimport type { PlotValue, PlotValueType } from '@weasel-js/plot';\nimport type { PropertyControlProps } from '@weasel-js/ui/components/Properties';\n\n/** What a field or cell is, for its control: its type, whether it can be unset or only read, and its value. */\nexport interface ValueSpec {\n type: PlotValueType;\n value: PlotValue;\n optional?: boolean;\n readOnly?: boolean;\n}\n\n/** The props of the control for `spec`, reporting each committed value through `set`. */\nexport function valueControl(spec: ValueSpec, name: string, set: (value: PlotValue) => void): PropertyControlProps {\n const { type, value, optional, readOnly } = spec;\n const clear = optional ? () => set(undefined) : undefined;\n switch (type.kind) {\n case 'number':\n return {\n kind: 'number', name, readOnly, value: typeof value === 'number' ? value : undefined,\n min: type.min, max: type.max, step: type.step, onChange: set, onClear: clear,\n };\n case 'boolean':\n return { kind: 'boolean', name, readOnly, value: value === true, onChange: set };\n case 'enum':\n return {\n kind: 'enum', name, readOnly, value: typeof value === 'string' ? value : undefined,\n options: type.options.map((o) => ({ value: o, label: o })), onChange: set, onClear: clear,\n };\n case 'color':\n return { kind: 'color', name, readOnly, value: typeof value === 'string' ? value : undefined, onChange: set };\n case 'string':\n return {\n kind: 'string', name, readOnly, value: typeof value === 'string' ? value : undefined, placeholder: type.placeholder,\n onChange: (next) => set(next === '' && optional ? undefined : next),\n };\n }\n}\n","import type { PlotData } from '@weasel-js/plot';\nimport { useRef } from 'react';\n\n/** What an edit does to the rows' order: the ids before it, to the ids after. */\nexport type RowShift = (ids: readonly string[], fresh: () => string) => string[];\n\nexport const insertedAt = (at: number): RowShift => (ids, fresh) => [...ids.slice(0, at), fresh(), ...ids.slice(at)];\nexport const removedAt = (at: number): RowShift => (ids) => ids.filter((_, i) => i !== at);\nexport const movedTo = (from: number, to: number): RowShift => (ids) => {\n const next = ids.filter((_, i) => i !== from);\n next.splice(to, 0, ids[from]!);\n return next;\n};\n\n/**\n * An id for each of a table's `count` rows that stays with its item through the panel's own inserts, removals and\n * moves, so what a row holds open follows the item rather than its slot. `track` wraps an edit with what it does to\n * the order; the order shifts only once `data` comes back as that edit made it, so an edit the host drops shifts\n * nothing. A change from elsewhere keeps the ids by position.\n */\nexport function useRowIds(data: PlotData, count: number): {\n ids: readonly string[];\n track: (change: (d: PlotData) => PlotData, shift: RowShift) => (d: PlotData) => PlotData;\n} {\n const state = useRef({ ids: [] as string[], made: 0, seen: undefined as PlotData | undefined, pending: [] as { made: PlotData; shift: RowShift }[] });\n const s = state.current;\n const fresh = (): string => `row-${s.made++}`;\n if (s.seen !== data) {\n const mine = s.pending.find((p) => p.made === data);\n if (mine) s.ids = mine.shift(s.ids, fresh);\n s.pending = [];\n s.seen = data;\n }\n if (s.ids.length !== count) s.ids = Array.from({ length: count }, (_, i) => s.ids[i] ?? fresh());\n return {\n ids: s.ids,\n track: (change, shift) => (d) => {\n const made = change(d);\n s.pending.push({ made, shift });\n return made;\n },\n };\n}\n","import type { PlotData, PlotScope, PlotTable, PlotValue } from '@weasel-js/plot';\nimport type { ReactElement } from 'react';\nimport { Button, CLOSE_BUTTON_PROPS } from '@weasel-js/ui/components/Button';\nimport { DataGrid, type DataGridColumn } from '@weasel-js/ui/components/DataGrid';\nimport { PropertyControl } from '@weasel-js/ui/components/Properties';\nimport s from './PlotDataPanel.module.css';\nimport { valueControl } from './valueControl';\nimport { insertedAt, movedTo, removedAt, useRowIds } from './rowIds';\n\n/** Props for {@link PlotTableView}. */\nexport interface PlotTableViewProps {\n data: PlotData;\n table: PlotTable;\n scope: PlotScope;\n /** Proposes `next` as the chart's data, an edit named `label`. */\n edit: (next: (data: PlotData) => PlotData, label: string) => void;\n}\n\ninterface Row { id: string; index: number }\n\n/** One table of a plot schema as a grid of controls: a row per item, which can be added, removed, dragged into\n * another order, and opened onto the tables it holds. */\nexport function PlotTableView({ data, table, scope, edit }: PlotTableViewProps): ReactElement {\n const count = table.count(data, scope);\n const { ids, track } = useRowIds(data, count);\n const rows: Row[] = ids.map((id, index) => ({ id, index }));\n const rowName = (r: number) => table.rowLabel?.(data, scope, r) || `${table.item} ${r + 1}`;\n const columns: DataGridColumn<Row>[] = table.columns(data, scope).map((column) => ({\n id: column.key,\n header: column.label,\n sortable: false,\n numeric: column.type.kind === 'number',\n render: ({ index }) => {\n if (column.applies?.(data, scope, index) === false) return null;\n const spec = { ...column, value: column.get(data, scope, index) };\n const set = (value: PlotValue) => edit((d) => column.set(d, scope, index, value), `Edit ${column.label.toLowerCase()}`);\n return <PropertyControl {...valueControl(spec, `${column.label} of ${rowName(index)}`, set)} className={s.cell} />;\n },\n }));\n const { insert, remove } = table;\n if (insert || remove) {\n columns.push({\n id: '__row', header: '', sortable: false,\n render: ({ index }) => (\n <span className={s.rowActions}>\n {insert && (\n <Button\n size=\"sm\" variant=\"ghost\" aria-label={`Insert ${table.item} after ${rowName(index)}`}\n onClick={() => edit(track((d) => insert(d, scope, index + 1), insertedAt(index + 1)), `Add ${table.item}`)}\n >\n +\n </Button>\n )}\n {remove && (\n <Button {...CLOSE_BUTTON_PROPS} aria-label={`Remove ${rowName(index)}`} onClick={() => edit(track((d) => remove(d, scope, index), removedAt(index)), `Remove ${table.item}`)} />\n )}\n </span>\n ),\n });\n }\n const move = table.move;\n const children = table.children;\n return (\n <div className={s.table}>\n {scope.length > 0 && <div className={s.heading}>{table.label}</div>}\n <DataGrid<Row>\n rows={rows}\n columns={columns}\n rowLabel={(r) => rowName(r.index)}\n empty={`No ${table.label.toLowerCase()}`}\n onReorder={move && ((moved, target) => {\n const from = rows.findIndex((r) => r.id === moved[0]);\n const to = target > from ? target - 1 : target;\n if (from >= 0 && to !== from) edit(track((d) => move(d, scope, from, to), movedTo(from, to)), `Reorder ${table.label.toLowerCase()}`);\n })}\n renderDetail={children && (({ index }) => (\n <div className={s.nested}>\n {children.map((child) => (\n <PlotTableView key={child.key} data={data} table={child} scope={[...scope, index]} edit={edit} />\n ))}\n </div>\n ))}\n />\n {insert && (\n <Button size=\"sm\" variant=\"ghost\" className={s.add} onClick={() => edit(track((d) => insert(d, scope, table.count(d, scope)), insertedAt(count)), `Add ${table.item}`)}>\n {`Add ${table.item}`}\n </Button>\n )}\n </div>\n );\n}\n","import { plotSchemaOf, type PlotData, type PlotValue } from '@weasel-js/plot';\nimport { useState, type ReactElement } from 'react';\nimport { PropertyField, PropertyList, PropertyNote, PropertyPanel, Subpanel } from '@weasel-js/ui/components/Properties';\nimport { PlotTableView } from './PlotTableView';\nimport { valueControl } from './valueControl';\n\n/** Props for {@link PlotDataPanel}. */\nexport interface PlotDataPanelProps {\n /** The chart, any kind. */\n data: PlotData;\n /** The chart as an edit leaves it, and a name for the edit, for an undo entry. The panel holds no copy: show the\n * change by passing it back as `data`. */\n onChange: (next: PlotData, label: string) => void;\n /** Default `'Data'`. */\n title?: string;\n className?: string;\n}\n\n/**\n * Authors a chart's data, whatever its kind, from the kind's schema (`plotSchemaOf`): a row per field, and a grid\n * per table whose rows can be added, removed, reordered, and opened onto the tables they hold.\n */\nexport function PlotDataPanel({ data, onChange, title = 'Data', className }: PlotDataPanelProps): ReactElement {\n const [error, setError] = useState<string | null>(null);\n const schema = plotSchemaOf(data);\n const edit = (change: (d: PlotData) => PlotData, label: string) => {\n try {\n const next = change(data);\n setError(null);\n if (next !== data) onChange(next, label);\n } catch (e) {\n setError(e instanceof Error ? e.message : String(e));\n }\n };\n const fields = schema.fields.filter((f) => f.applies?.(data) !== false);\n return (\n <PropertyPanel title={title} className={className}>\n <PropertyList>\n {fields.map((field) => {\n const set = (value: PlotValue) => edit((d) => field.set(d, value), `Edit ${field.label.toLowerCase()}`);\n const control = valueControl({ ...field, value: field.get(data) }, field.label, set);\n return <PropertyField key={field.key} label={field.label} {...control} />;\n })}\n {error !== null && <PropertyNote>{error}</PropertyNote>}\n </PropertyList>\n {schema.tables.map((table) => (\n <Subpanel key={table.key} title={table.label}>\n <PlotTableView data={data} table={table} scope={[]} edit={edit} />\n </Subpanel>\n ))}\n </PropertyPanel>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;ACaA,SAAgB,aAAa,MAAiB,MAAc,KAAuD;CACjH,MAAM,EAAE,MAAM,OAAO,UAAU,aAAa;CAC5C,MAAM,QAAQ,iBAAiB,IAAI,MAAS,IAAI;CAChD,QAAQ,KAAK,MAAb;EACE,KAAK,UACH,OAAO;GACL,MAAM;GAAU;GAAM;GAAU,OAAO,OAAO,UAAU,WAAW,QAAQ;GAC3E,KAAK,KAAK;GAAK,KAAK,KAAK;GAAK,MAAM,KAAK;GAAM,UAAU;GAAK,SAAS;EACzE;EACF,KAAK,WACH,OAAO;GAAE,MAAM;GAAW;GAAM;GAAU,OAAO,UAAU;GAAM,UAAU;EAAI;EACjF,KAAK,QACH,OAAO;GACL,MAAM;GAAQ;GAAM;GAAU,OAAO,OAAO,UAAU,WAAW,QAAQ;GACzE,SAAS,KAAK,QAAQ,KAAK,OAAO;IAAE,OAAO;IAAG,OAAO;GAAE,EAAE;GAAG,UAAU;GAAK,SAAS;EACtF;EACF,KAAK,SACH,OAAO;GAAE,MAAM;GAAS;GAAM;GAAU,OAAO,OAAO,UAAU,WAAW,QAAQ;GAAW,UAAU;EAAI;EAC9G,KAAK,UACH,OAAO;GACL,MAAM;GAAU;GAAM;GAAU,OAAO,OAAO,UAAU,WAAW,QAAQ;GAAW,aAAa,KAAK;GACxG,WAAW,SAAS,IAAI,SAAS,MAAM,WAAW,SAAY,IAAI;EACpE;CACJ;AACF;;;AC/BA,IAAa,cAAc,QAA0B,KAAK,UAAU;CAAC,GAAG,IAAI,MAAM,GAAG,EAAE;CAAG,MAAM;CAAG,GAAG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SAAS,IAAI;GACb,IAAI,SAAS,MAAM,SAAS,MAAM,KAAK;EACzC,SAAS,GAAG;GACV,SAAS,aAAa,QAAQ,EAAE,UAAU,OAAO,CAAC,CAAC;EACrD;CACF;CACA,MAAM,SAAS,OAAO,OAAO,QAAQ,MAAM,EAAE,UAAU,IAAI,MAAM,KAAK;CACtE,OACE,qBAAC,eAAD;EAAsB;EAAkB;EAAxC,UAAA,CACE,qBAAC,cAAD,EAAA,UAAA,CACG,OAAO,KAAK,UAAU;GACrB,MAAM,OAAO,UAAqB,MAAM,MAAM,MAAM,IAAI,GAAG,KAAK,GAAG,QAAQ,MAAM,MAAM,YAAY,GAAG;GACtG,MAAM,UAAU,aAAa;IAAE,GAAG;IAAO,OAAO,MAAM,IAAI,IAAI;GAAE,GAAG,MAAM,OAAO,GAAG;GACnF,OAAO,oBAAC,eAAD;IAA+B,OAAO,MAAM;IAAO,GAAI;GAAU,GAA7C,MAAM,GAAuC;EAC1E,CAAC,GACA,UAAU,QAAQ,oBAAC,cAAD,EAAA,UAAe,MAAoB,CAAA,CAC1C,EAAA,CAAA,GACb,OAAO,OAAO,KAAK,UAClB,oBAAC,UAAD;GAA0B,OAAO,MAAM;GACrC,UAAA,oBAAC,eAAD;IAAqB;IAAa;IAAO,OAAO,CAAC;IAAS;GAAO,CAAA;EACzD,GAFK,MAAM,GAEX,CACX,CACY;;AAEnB"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
// src/components/Rulers/Rulers.module.css
|
|
5
|
+
var rulers = "_rulers_f94859";
|
|
6
|
+
var strip = "_strip_f94859";
|
|
7
|
+
var ticks = "_ticks_f94859";
|
|
8
|
+
var edge = "_edge_f94859";
|
|
9
|
+
var label = "_label_f94859 _numeric_25fd0b";
|
|
10
|
+
var pointer = "_pointer_f94859";
|
|
11
|
+
var unit = "_unit_f94859";
|
|
12
|
+
var Rulers_module_default = {
|
|
13
|
+
rulers,
|
|
14
|
+
strip,
|
|
15
|
+
ticks,
|
|
16
|
+
edge,
|
|
17
|
+
label,
|
|
18
|
+
pointer,
|
|
19
|
+
unit
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/components/Rulers/Rulers.tsx
|
|
23
|
+
var crisp = (n) => Math.round(n) + .5;
|
|
24
|
+
/** One ruler's tick marks as path data: each tick crosses the strip from its
|
|
25
|
+
* inner edge, a labeled one all the way. */
|
|
26
|
+
function tickPath(ticks, size, across) {
|
|
27
|
+
return ticks.ticks.map(({ at, major }) => {
|
|
28
|
+
const a = crisp(at);
|
|
29
|
+
const from = major ? 0 : size * .65;
|
|
30
|
+
return across === "y" ? `M${a} ${from}V${size}` : `M${from} ${a}H${size}`;
|
|
31
|
+
}).join("");
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Rulers along the top and left of a canvas, with a box in the corner naming
|
|
35
|
+
* the unit they read in: the chrome half of `createRulers` from
|
|
36
|
+
* `@weasel-js/guides`, whose contribution follows the view and takes the drags
|
|
37
|
+
* that place guides. Place it in the canvas's positioned container; it covers
|
|
38
|
+
* it and passes every pointer event through to the canvas below.
|
|
39
|
+
*/
|
|
40
|
+
function Rulers({ rulers, className }) {
|
|
41
|
+
const state = useSyncExternalStore(rulers.subscribe, rulers.get, rulers.get);
|
|
42
|
+
const layout = rulers.layout();
|
|
43
|
+
if (layout === null) return null;
|
|
44
|
+
const { size } = rulers;
|
|
45
|
+
const { width, height } = state;
|
|
46
|
+
const { top, left, pointer } = layout;
|
|
47
|
+
const mid = size / 2;
|
|
48
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
49
|
+
className: [Rulers_module_default.rulers, className].filter(Boolean).join(" "),
|
|
50
|
+
"aria-hidden": "true",
|
|
51
|
+
"data-unit": top.unit,
|
|
52
|
+
children: [
|
|
53
|
+
/* @__PURE__ */ jsxs("svg", {
|
|
54
|
+
className: Rulers_module_default.strip,
|
|
55
|
+
width,
|
|
56
|
+
height: size,
|
|
57
|
+
children: [
|
|
58
|
+
/* @__PURE__ */ jsx("path", {
|
|
59
|
+
className: Rulers_module_default.ticks,
|
|
60
|
+
d: tickPath(top, size, "y")
|
|
61
|
+
}),
|
|
62
|
+
top.ticks.map((t) => t.label === void 0 ? null : /* @__PURE__ */ jsx("text", {
|
|
63
|
+
className: Rulers_module_default.label,
|
|
64
|
+
x: t.at + 3,
|
|
65
|
+
y: mid * .8,
|
|
66
|
+
children: t.label
|
|
67
|
+
}, t.at)),
|
|
68
|
+
pointer && /* @__PURE__ */ jsx("line", {
|
|
69
|
+
className: Rulers_module_default.pointer,
|
|
70
|
+
x1: crisp(pointer.x),
|
|
71
|
+
x2: crisp(pointer.x),
|
|
72
|
+
y1: 0,
|
|
73
|
+
y2: size
|
|
74
|
+
}),
|
|
75
|
+
/* @__PURE__ */ jsx("line", {
|
|
76
|
+
className: Rulers_module_default.edge,
|
|
77
|
+
x1: 0,
|
|
78
|
+
x2: width,
|
|
79
|
+
y1: size - .5,
|
|
80
|
+
y2: size - .5
|
|
81
|
+
})
|
|
82
|
+
]
|
|
83
|
+
}),
|
|
84
|
+
/* @__PURE__ */ jsxs("svg", {
|
|
85
|
+
className: Rulers_module_default.strip,
|
|
86
|
+
width: size,
|
|
87
|
+
height,
|
|
88
|
+
children: [
|
|
89
|
+
/* @__PURE__ */ jsx("path", {
|
|
90
|
+
className: Rulers_module_default.ticks,
|
|
91
|
+
d: tickPath(left, size, "x")
|
|
92
|
+
}),
|
|
93
|
+
left.ticks.map((t) => t.label === void 0 ? null : /* @__PURE__ */ jsx("text", {
|
|
94
|
+
className: Rulers_module_default.label,
|
|
95
|
+
x: mid * .8,
|
|
96
|
+
y: t.at + 3,
|
|
97
|
+
textAnchor: "end",
|
|
98
|
+
transform: `rotate(-90 ${mid * .8} ${t.at + 3})`,
|
|
99
|
+
children: t.label
|
|
100
|
+
}, t.at)),
|
|
101
|
+
pointer && /* @__PURE__ */ jsx("line", {
|
|
102
|
+
className: Rulers_module_default.pointer,
|
|
103
|
+
x1: 0,
|
|
104
|
+
x2: size,
|
|
105
|
+
y1: crisp(pointer.y),
|
|
106
|
+
y2: crisp(pointer.y)
|
|
107
|
+
}),
|
|
108
|
+
/* @__PURE__ */ jsx("line", {
|
|
109
|
+
className: Rulers_module_default.edge,
|
|
110
|
+
x1: size - .5,
|
|
111
|
+
x2: size - .5,
|
|
112
|
+
y1: 0,
|
|
113
|
+
y2: height
|
|
114
|
+
})
|
|
115
|
+
]
|
|
116
|
+
}),
|
|
117
|
+
/* @__PURE__ */ jsxs("svg", {
|
|
118
|
+
className: Rulers_module_default.strip,
|
|
119
|
+
width: size,
|
|
120
|
+
height: size,
|
|
121
|
+
children: [/* @__PURE__ */ jsx("text", {
|
|
122
|
+
className: Rulers_module_default.unit,
|
|
123
|
+
x: mid,
|
|
124
|
+
y: mid,
|
|
125
|
+
children: top.unit
|
|
126
|
+
}), /* @__PURE__ */ jsx("path", {
|
|
127
|
+
className: Rulers_module_default.edge,
|
|
128
|
+
d: `M0 ${size - .5}H${size - .5}V0`
|
|
129
|
+
})]
|
|
130
|
+
})
|
|
131
|
+
]
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export { Rulers as t };
|
|
136
|
+
//# sourceMappingURL=Rulers-hkPFdPkH.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Rulers-hkPFdPkH.js","names":["s"],"sources":["../../src/components/Rulers/Rulers.module.css","../../src/components/Rulers/Rulers.tsx"],"sourcesContent":["/* Provisional look: theme tokens only, until the maintainer settles it. */\n.rulers {\n position: absolute;\n inset: 0;\n overflow: hidden;\n pointer-events: none;\n user-select: none;\n}\n\n.strip {\n position: absolute;\n top: 0;\n left: 0;\n color: var(--wzl-fg-muted);\n background: var(--wzl-surface);\n}\n\n.ticks {\n fill: none;\n stroke: currentcolor;\n stroke-width: 1;\n shape-rendering: crispEdges;\n}\n\n.edge {\n fill: none;\n stroke: var(--wzl-border);\n stroke-width: 1;\n shape-rendering: crispEdges;\n}\n\n.label {\n composes: numeric from '@weasel-js/theme/numeric.module.css';\n fill: currentcolor;\n font-size: var(--wzl-font-size-2xs);\n dominant-baseline: central;\n}\n\n.pointer {\n stroke: var(--wzl-accent);\n stroke-width: 1;\n shape-rendering: crispEdges;\n}\n\n.unit {\n fill: currentcolor;\n font-size: var(--wzl-font-size-2xs);\n text-anchor: middle;\n dominant-baseline: central;\n}\n","import { useSyncExternalStore, type ReactElement } from 'react';\nimport type { RulerTicks, Rulers as RulersModel } from '@weasel-js/guides';\nimport s from './Rulers.module.css';\n\n/** Props for {@link Rulers}. */\nexport interface RulersProps {\n /** What `createRulers` returned; its contribution installed on the canvas. */\n rulers: RulersModel;\n /** Additional class for the root, which covers its positioned container. */\n className?: string;\n}\n\nconst MINOR = 0.35;\nconst crisp = (n: number): number => Math.round(n) + 0.5;\n\n/** One ruler's tick marks as path data: each tick crosses the strip from its\n * inner edge, a labeled one all the way. */\nfunction tickPath(ticks: RulerTicks, size: number, across: 'x' | 'y'): string {\n return ticks.ticks.map(({ at, major }) => {\n const a = crisp(at);\n const from = major ? 0 : size * (1 - MINOR);\n return across === 'y' ? `M${a} ${from}V${size}` : `M${from} ${a}H${size}`;\n }).join('');\n}\n\n/**\n * Rulers along the top and left of a canvas, with a box in the corner naming\n * the unit they read in: the chrome half of `createRulers` from\n * `@weasel-js/guides`, whose contribution follows the view and takes the drags\n * that place guides. Place it in the canvas's positioned container; it covers\n * it and passes every pointer event through to the canvas below.\n */\nexport function Rulers({ rulers, className }: RulersProps): ReactElement | null {\n const state = useSyncExternalStore(rulers.subscribe, rulers.get, rulers.get);\n const layout = rulers.layout();\n if (layout === null) return null;\n const { size } = rulers;\n const { width, height } = state;\n const { top, left, pointer } = layout;\n const mid = size / 2;\n return (\n <div className={[s.rulers, className].filter(Boolean).join(' ')} aria-hidden=\"true\" data-unit={top.unit}>\n <svg className={s.strip} width={width} height={size}>\n <path className={s.ticks} d={tickPath(top, size, 'y')} />\n {top.ticks.map((t) => t.label === undefined ? null : (\n <text key={t.at} className={s.label} x={t.at + 3} y={mid * 0.8}>{t.label}</text>\n ))}\n {pointer && <line className={s.pointer} x1={crisp(pointer.x)} x2={crisp(pointer.x)} y1={0} y2={size} />}\n <line className={s.edge} x1={0} x2={width} y1={size - 0.5} y2={size - 0.5} />\n </svg>\n <svg className={s.strip} width={size} height={height}>\n <path className={s.ticks} d={tickPath(left, size, 'x')} />\n {left.ticks.map((t) => t.label === undefined ? null : (\n <text key={t.at} className={s.label} x={mid * 0.8} y={t.at + 3} textAnchor=\"end\" transform={`rotate(-90 ${mid * 0.8} ${t.at + 3})`}>{t.label}</text>\n ))}\n {pointer && <line className={s.pointer} x1={0} x2={size} y1={crisp(pointer.y)} y2={crisp(pointer.y)} />}\n <line className={s.edge} x1={size - 0.5} x2={size - 0.5} y1={0} y2={height} />\n </svg>\n <svg className={s.strip} width={size} height={size}>\n <text className={s.unit} x={mid} y={mid}>{top.unit}</text>\n <path className={s.edge} d={`M0 ${size - 0.5}H${size - 0.5}V0`} />\n </svg>\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;ACaA,IAAM,SAAS,MAAsB,KAAK,MAAM,CAAC,IAAI;;;AAIrD,SAAS,SAAS,OAAmB,MAAc,QAA2B;CAC5E,OAAO,MAAM,MAAM,KAAK,EAAE,IAAI,YAAY;EACxC,MAAM,IAAI,MAAM,EAAE;EAClB,MAAM,OAAO,QAAQ,IAAI,OAAQ;EACjC,OAAO,WAAW,MAAM,IAAI,EAAE,GAAG,KAAK,GAAG,SAAS,IAAI,KAAK,GAAG,EAAE,GAAG;CACrE,CAAC,CAAC,CAAC,KAAK,EAAE;AACZ;;;;;;;;AASA,SAAgB,OAAO,EAAE,QAAQ,aAA+C;CAC9E,MAAM,QAAQ,qBAAqB,OAAO,WAAW,OAAO,KAAK,OAAO,GAAG;CAC3E,MAAM,SAAS,OAAO,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AM,GAAI,GAAG,EAAE,KAAK,EAAE;MAAK,UAAA,EAAE;KAAY,GAAxI,EAAE,EAAsI,CACpJ;KACA,WAAW,oBAAC,QAAD;MAAM,WAAWA,sBAAE;MAAS,IAAI;MAAG,IAAI;MAAM,IAAI,MAAM,QAAQ,CAAC;MAAG,IAAI,MAAM,QAAQ,CAAC;KAAI,CAAA;KACtG,oBAAC,QAAD;MAAM,WAAWA,sBAAE;MAAM,IAAI,OAAO;MAAK,IAAI,OAAO;MAAK,IAAI;MAAG,IAAI;KAAS,CAAA;IAC1E;;GACL,qBAAC,OAAD;IAAK,WAAWA,sBAAE;IAAO,OAAO;IAAM,QAAQ;IAA9C,UAAA,CACE,oBAAC,QAAD;KAAM,WAAWA,sBAAE;KAAM,GAAG;KAAK,GAAG;KAAM,UAAA,IAAI;IAAW,CAAA,GACzD,oBAAC,QAAD;KAAM,WAAWA,sBAAE;KAAM,GAAG,MAAM,OAAO,GAAI,GAAG,OAAO,GAAI;IAAM,CAAA,CAC9D;;EACF;;AAET"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef } from "react";
|
|
2
|
+
import { useNodeOverlayFrame } from "@weasel-js/core";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
import { isGradientPaint, resolveFill, resolveStroke } from "@weasel-js/paint";
|
|
5
|
+
import { fillInPoseFrame, fillToBoundsFrame } from "@weasel-js/renderer";
|
|
6
|
+
import { useOngoingAction } from "@weasel-js/routing/react";
|
|
7
|
+
import { isMeshGradientPaint } from "@weasel-js/renderer/mesh";
|
|
8
|
+
import { MeshHandles } from "@weasel-js/ui/components/MeshEditor";
|
|
9
|
+
import { GradientHandles } from "@weasel-js/ui/components/GradientEditor";
|
|
10
|
+
import "@weasel-js/paint-kinds/mesh";
|
|
11
|
+
|
|
12
|
+
// src/components/SceneGradientHandles/SceneGradientHandles.tsx
|
|
13
|
+
/**
|
|
14
|
+
* Scene-aware paint handles: on-canvas geometry handles for the gradient in
|
|
15
|
+
* one node's `fill` or `stroke` — `GradientHandles` for the three ramps,
|
|
16
|
+
* `MeshHandles` for a mesh — committed through the `setFill` / `setStroke`
|
|
17
|
+
* actions as a single undo entry per drag. A canceled drag cancels the action,
|
|
18
|
+
* and so does unmounting the overlay while a handle is held.
|
|
19
|
+
*
|
|
20
|
+
* Renders nothing unless the targeted slot holds a gradient or a mesh. The
|
|
21
|
+
* node stores its gradient in `units: 'bounds'`, fractions of its own box; the
|
|
22
|
+
* handles get it resolved onto the box and every edit is normalized back on
|
|
23
|
+
* the way out.
|
|
24
|
+
*/
|
|
25
|
+
function SceneGradientHandles(props) {
|
|
26
|
+
const { scene, containerRef, nodeId, slot, view, className } = props;
|
|
27
|
+
const frame = useNodeOverlayFrame(scene, containerRef, nodeId, { view });
|
|
28
|
+
const edit = useOngoingAction(slot === "fill" ? "setFill" : "setStroke");
|
|
29
|
+
const gone = useRef(false);
|
|
30
|
+
useLayoutEffect(() => {
|
|
31
|
+
gone.current = false;
|
|
32
|
+
return () => {
|
|
33
|
+
gone.current = true;
|
|
34
|
+
edit.cancel();
|
|
35
|
+
};
|
|
36
|
+
}, [edit]);
|
|
37
|
+
const paint = frame ? paintOf(scene, nodeId, slot) : null;
|
|
38
|
+
if (!frame || !paint) return null;
|
|
39
|
+
const dispatch = (next, phase) => {
|
|
40
|
+
if (gone.current) return;
|
|
41
|
+
const normalized = fillToBoundsFrame(next, frame.box);
|
|
42
|
+
if (phase === "commit") edit.commit({ paint: normalized });
|
|
43
|
+
else edit.input({ paint: normalized });
|
|
44
|
+
};
|
|
45
|
+
const common = {
|
|
46
|
+
toScreen: frame.toScreen,
|
|
47
|
+
toLocal: frame.toLocal,
|
|
48
|
+
width: frame.width,
|
|
49
|
+
height: frame.height,
|
|
50
|
+
className,
|
|
51
|
+
onInput: (next) => dispatch(next, "input"),
|
|
52
|
+
onChange: (next) => dispatch(next, "commit"),
|
|
53
|
+
onCancel: () => edit.cancel()
|
|
54
|
+
};
|
|
55
|
+
if (isGradientPaint(paint)) return /* @__PURE__ */ jsx(GradientHandles, {
|
|
56
|
+
value: fillInPoseFrame(paint, frame.box),
|
|
57
|
+
...common
|
|
58
|
+
});
|
|
59
|
+
if (isMeshGradientPaint(paint)) {
|
|
60
|
+
const resolved = fillInPoseFrame(paint, frame.box);
|
|
61
|
+
if (resolved.units === "bounds") return null;
|
|
62
|
+
return /* @__PURE__ */ jsx(MeshHandles, {
|
|
63
|
+
value: resolved,
|
|
64
|
+
...common
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
function paintOf(scene, nodeId, slot) {
|
|
70
|
+
if (nodeId == null) return void 0;
|
|
71
|
+
const data = scene.nodes.get(nodeId)?.data;
|
|
72
|
+
if (!data) return void 0;
|
|
73
|
+
return slot === "fill" ? resolveFill(data.fill)?.paint : resolveStroke(data.stroke)?.paint;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { SceneGradientHandles as t };
|
|
77
|
+
//# sourceMappingURL=SceneGradientHandles-CHNSWx7U.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SceneGradientHandles-CHNSWx7U.js","names":[],"sources":["../../src/components/SceneGradientHandles/SceneGradientHandles.tsx"],"sourcesContent":["import { useLayoutEffect, useRef, type ReactElement, type RefObject } from 'react';\nimport { fillInPoseFrame, fillToBoundsFrame } from '@weasel-js/renderer';\nimport { isGradientPaint, resolveFill, resolveStroke, type Paint, type GradientPaint, type Stroke, type Fill, type Inherit } from '@weasel-js/paint';\nimport { useOngoingAction } from '@weasel-js/routing/react';\nimport type { View } from '@weasel-js/geom';\nimport { isMeshGradientPaint, type MeshGradientPaint } from '@weasel-js/renderer/mesh';\nimport {\n useNodeOverlayFrame,\n type RectPose,\n type Scene,\n} from '@weasel-js/core';\nimport { MeshHandles } from '@weasel-js/ui/components/MeshEditor';\nimport { GradientHandles } from '@weasel-js/ui/components/GradientEditor';\nimport type { PaintSlot } from '@weasel-js/paint';\nimport '@weasel-js/paint-kinds/mesh';\n\n/** What `SceneGradientHandles` reads off a node. Every field is optional, so\n * any richer app data shape satisfies it. */\nexport interface PaintedNodeData {\n fill?: Fill | Inherit | null;\n stroke?: Stroke | Inherit | null;\n}\n\n/** Props for {@link SceneGradientHandles}. */\nexport interface SceneGradientHandlesProps<\n TData extends PaintedNodeData,\n TLayer extends string,\n TPose extends RectPose,\n> {\n scene: Scene<TData, TLayer, TPose>;\n /** The element the overlay is positioned in. Must be the canvas's own box,\n * or the handles land somewhere other than the paint. */\n containerRef: RefObject<HTMLElement | null>;\n /** The node whose paint is being edited — usually the lone selected id. */\n nodeId: string | null | undefined;\n /** Which of the node's two paints these handles move. */\n slot: PaintSlot;\n /** Current viewport; a thunk is re-read per projection. See\n * `useNodeOverlayFrame`. */\n view?: View | (() => View);\n className?: string;\n}\n\n/**\n * Scene-aware paint handles: on-canvas geometry handles for the gradient in\n * one node's `fill` or `stroke` — `GradientHandles` for the three ramps,\n * `MeshHandles` for a mesh — committed through the `setFill` / `setStroke`\n * actions as a single undo entry per drag. A canceled drag cancels the action,\n * and so does unmounting the overlay while a handle is held.\n *\n * Renders nothing unless the targeted slot holds a gradient or a mesh. The\n * node stores its gradient in `units: 'bounds'`, fractions of its own box; the\n * handles get it resolved onto the box and every edit is normalized back on\n * the way out.\n */\nexport function SceneGradientHandles<\n TData extends PaintedNodeData,\n TLayer extends string,\n TPose extends RectPose,\n>(props: SceneGradientHandlesProps<TData, TLayer, TPose>): ReactElement | null {\n const { scene, containerRef, nodeId, slot, view, className } = props;\n const frame = useNodeOverlayFrame(scene, containerRef, nodeId, { view });\n const edit = useOngoingAction(slot === 'fill' ? 'setFill' : 'setStroke');\n // An edit is open here only while a handle is held, so one still open at unmount is a drag cut short, and\n // that is a cancel. `useOngoingAction` would commit it in its own cleanup, which this layout cleanup runs\n // ahead of; the handle's unmount cancel comes after both and must not open a second edit to cancel.\n const gone = useRef(false);\n useLayoutEffect(() => {\n gone.current = false;\n return () => {\n gone.current = true;\n edit.cancel();\n };\n }, [edit]);\n\n const paint = frame ? paintOf(scene, nodeId, slot) : null;\n if (!frame || !paint) return null;\n\n const dispatch = (next: Paint, phase: 'input' | 'commit'): void => {\n if (gone.current) return;\n const normalized = fillToBoundsFrame(next, frame.box);\n if (phase === 'commit') edit.commit({ paint: normalized });\n else edit.input({ paint: normalized });\n };\n const common = {\n toScreen: frame.toScreen,\n toLocal: frame.toLocal,\n width: frame.width,\n height: frame.height,\n className,\n onInput: (next: Paint) => dispatch(next, 'input'),\n onChange: (next: Paint) => dispatch(next, 'commit'),\n onCancel: () => edit.cancel(),\n };\n\n if (isGradientPaint(paint)) {\n return <GradientHandles value={fillInPoseFrame(paint, frame.box) as GradientPaint} {...common} />;\n }\n if (isMeshGradientPaint(paint)) {\n const resolved = fillInPoseFrame(paint, frame.box) as unknown as MeshGradientPaint;\n // Still in the bounds frame means the mesh kind has not loaded, and\n // handles drawn now would sit in fractions of the box.\n if (resolved.units === 'bounds') return null;\n return <MeshHandles value={resolved} {...common} />;\n }\n return null;\n}\n\nfunction paintOf<TData extends PaintedNodeData, TLayer extends string, TPose extends RectPose>(\n scene: Scene<TData, TLayer, TPose>,\n nodeId: string | null | undefined,\n slot: PaintSlot,\n): Paint | null | undefined {\n if (nodeId == null) return undefined;\n const data = (scene.nodes as ReadonlyMap<string, { data: TData }>).get(nodeId)?.data;\n if (!data) return undefined;\n // An inherited paint is its container's, and is edited there.\n return slot === 'fill' ? resolveFill(data.fill)?.paint : resolveStroke(data.stroke)?.paint;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAuDA,SAAgB,qBAId,OAA6E;CAC7E,MAAM,EAAE,OAAO,cAAc,QAAQ,MAAM,MAAM,cAAc;CAC/D,MAAM,QAAQ,oBAAoB,OAAO,cAAc,QAAQ,EAAE,KAAK,CAAC;CACvE,MAAM,OAAO,iBAAiB,SAAS,SAAS,YAAY,WAAW;CAIvE,MAAM,O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