@ggraphic/editor 0.1.0-alpha.0 → 0.1.0-alpha.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/README.md +16 -3
- package/THIRD_PARTY_NOTICES +25 -1
- package/dist/components/CanvasContextMenu/CanvasActionSheet.js +2 -1
- package/dist/components/CanvasContextMenu/CanvasContextMenu.js +3 -2
- package/dist/components/CanvasEditor/CanvasEditorShell.js +4 -1
- package/dist/components/CanvasTextOverlay.js +10 -3
- package/dist/components/GenericCanvas.js +12 -3
- package/dist/components/RichTextField.js +1 -0
- package/dist/components/SelectionPill.js +2 -1
- package/dist/components/TextFormatButtons.js +7 -2
- package/dist/i18n/de.d.ts +12 -0
- package/dist/i18n/de.js +12 -0
- package/dist/i18n/en.js +11 -0
- package/dist/page/actions.d.ts +66 -0
- package/dist/page/actions.js +353 -0
- package/dist/page/config.d.ts +15 -0
- package/dist/page/config.js +91 -0
- package/dist/page/fitting.d.ts +6 -0
- package/dist/page/fitting.js +26 -0
- package/dist/page/hug.d.ts +10 -0
- package/dist/page/hug.js +18 -0
- package/dist/page/instantiate.d.ts +8 -0
- package/dist/page/instantiate.js +30 -0
- package/dist/page/logoSize.d.ts +8 -0
- package/dist/page/logoSize.js +29 -0
- package/dist/page/pageState.d.ts +41 -0
- package/dist/page/pageState.js +59 -0
- package/dist/page/seeds.d.ts +14 -0
- package/dist/page/seeds.js +40 -0
- package/dist/page/slotBudgets.d.ts +22 -0
- package/dist/page/slotBudgets.js +18 -0
- package/dist/page/templatePack.d.ts +31 -0
- package/dist/page/templatePack.js +55 -0
- package/dist/page/typography.d.ts +11 -0
- package/dist/page/typography.js +13 -0
- package/dist/sidebar/sections/CombinedTextSection.js +1 -1
- package/dist/sidebar/sections/GenericShareSection.js +3 -2
- package/dist/styles/compiled.css +2 -0
- package/dist/ui/alert-dialog.js +3 -2
- package/dist/ui/dialog.js +3 -2
- package/dist/ui/editorScope.d.ts +10 -0
- package/dist/ui/editorScope.js +9 -0
- package/dist/ui/popover.js +2 -1
- package/dist/ui/select.js +2 -1
- package/dist/utils/canvasLayerManager.d.ts +14 -0
- package/dist/utils/canvasLayerManager.js +24 -0
- package/dist/utils/fontMarkSupport.d.ts +10 -0
- package/dist/utils/fontMarkSupport.js +22 -6
- package/dist/utils/layerLabels.js +3 -6
- package/dist/utils/textUtils.js +2 -14
- package/package.json +13 -6
- package/skills/ggraphic-embed/SKILL.md +145 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* What a free page can do with its elements: texts, shapes, icons, emoji,
|
|
4
|
+
* illustrations, placed images and the background colour. The editor calls these by name
|
|
5
|
+
* (`OptionalCanvasActions`): the canvas for moves and edits, the sidebar
|
|
6
|
+
* sections for adding, the delete key for removing (`utils/elementKinds.ts`).
|
|
7
|
+
*
|
|
8
|
+
* Every action is one user intent and records one history entry.
|
|
9
|
+
*/
|
|
10
|
+
import { resolveRef, withBinding, withoutBinding, } from '@ggraphic/core';
|
|
11
|
+
import { activeBrand } from '@ggraphic/editor/brand/brandRegistry';
|
|
12
|
+
import { blockElementIds } from '@ggraphic/editor/blocks/liftedBlock';
|
|
13
|
+
import { createAssetInstance } from '@ggraphic/editor/utils/canvasAssets';
|
|
14
|
+
import { createInstanceId } from '@ggraphic/editor/utils/duplicateElement';
|
|
15
|
+
import { iconInstanceIdsFor, placedIconId } from '@ggraphic/editor/utils/iconInstances';
|
|
16
|
+
import { createIllustration } from '@ggraphic/editor/utils/illustrations/registry';
|
|
17
|
+
import { createShape } from '@ggraphic/editor/utils/shapes';
|
|
18
|
+
import { createUserImageInstanceFromUrl, } from '@ggraphic/editor/utils/userImageUtils';
|
|
19
|
+
import { fittedFontSize } from './fitting.js';
|
|
20
|
+
import { withHuggedPlates } from './hug.js';
|
|
21
|
+
import { noTemplates } from './slotBudgets.js';
|
|
22
|
+
import { headlineFont, bodyFont, fontStackFor, scaleFor } from './typography.js';
|
|
23
|
+
let idCounter = 0;
|
|
24
|
+
const newId = (prefix) => `${prefix}-${Date.now().toString(36)}-${(idCounter++).toString(36)}`;
|
|
25
|
+
const withLayer = (order, id) => [...(order ?? []), id];
|
|
26
|
+
const withoutLayer = (order, id) => (order ?? []).filter((entry) => entry !== id);
|
|
27
|
+
/** The shape fields a person sets by moving or resizing it on the canvas. */
|
|
28
|
+
const HAND_GEOMETRY = ['x', 'y', 'width', 'height'];
|
|
29
|
+
/** A readable ink on the page's current background. */
|
|
30
|
+
function inkFor(background) {
|
|
31
|
+
const match = activeBrand().backgrounds.find((b) => b.color.toLowerCase() === background.toLowerCase());
|
|
32
|
+
if (match)
|
|
33
|
+
return match.ink;
|
|
34
|
+
const hex = background.replace('#', '');
|
|
35
|
+
if (hex.length !== 6)
|
|
36
|
+
return activeBrand().colors.textOnDark;
|
|
37
|
+
const [r, g, b] = [0, 2, 4].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255);
|
|
38
|
+
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
39
|
+
return luminance > 0.5 ? activeBrand().colors.textOnLight : activeBrand().colors.textOnDark;
|
|
40
|
+
}
|
|
41
|
+
/** `role` is the brand font the preset's family stands for: a new text follows it from the start. */
|
|
42
|
+
const TEXT_PRESETS = {
|
|
43
|
+
header: {
|
|
44
|
+
text: 'Headline',
|
|
45
|
+
size: 88,
|
|
46
|
+
font: headlineFont,
|
|
47
|
+
role: 'headline',
|
|
48
|
+
style: 'bold',
|
|
49
|
+
},
|
|
50
|
+
subheader: {
|
|
51
|
+
text: 'Subheading',
|
|
52
|
+
size: 52,
|
|
53
|
+
font: headlineFont,
|
|
54
|
+
role: 'headline',
|
|
55
|
+
style: 'normal',
|
|
56
|
+
},
|
|
57
|
+
body: {
|
|
58
|
+
text: 'Your text here',
|
|
59
|
+
size: 36,
|
|
60
|
+
font: bodyFont,
|
|
61
|
+
role: 'body',
|
|
62
|
+
style: 'normal',
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* The actions of a `width` × `height` page. `templateOf` is the host's catalogue: a text on a page
|
|
67
|
+
* made from one of its templates fits within the line limit of its slot.
|
|
68
|
+
*/
|
|
69
|
+
export function createPageActions(getState, setState, saveToHistory, debouncedSaveToHistory, width, height, templateOf = noTemplates) {
|
|
70
|
+
const s = scaleFor(width, height);
|
|
71
|
+
/** Applies `change` and records it, immediately or coalesced with its neighbours. */
|
|
72
|
+
const commit = (change, debounced = false) => {
|
|
73
|
+
setState(change);
|
|
74
|
+
(debounced ? debouncedSaveToHistory : saveToHistory)(change(getState()));
|
|
75
|
+
};
|
|
76
|
+
const addText = (type, content) => {
|
|
77
|
+
const preset = TEXT_PRESETS[type];
|
|
78
|
+
const textWidth = Math.round(width * 0.7);
|
|
79
|
+
const text = {
|
|
80
|
+
id: newId('text'),
|
|
81
|
+
text: content ?? preset.text,
|
|
82
|
+
type,
|
|
83
|
+
x: Math.round((width - textWidth) / 2),
|
|
84
|
+
y: Math.round(height * (type === 'header' ? 0.3 : type === 'subheader' ? 0.42 : 0.52)),
|
|
85
|
+
width: textWidth,
|
|
86
|
+
fontSize: Math.round(preset.size * s),
|
|
87
|
+
fontFamily: preset.font(),
|
|
88
|
+
bind: withBinding(undefined, 'fontFamily', {
|
|
89
|
+
ref: `font.${preset.role}`,
|
|
90
|
+
value: activeBrand().fonts[preset.role],
|
|
91
|
+
}),
|
|
92
|
+
fontStyle: preset.style,
|
|
93
|
+
fill: inkFor(getState().backgroundColor),
|
|
94
|
+
align: 'center',
|
|
95
|
+
lineHeight: type === 'body' ? 1.3 : activeBrand().lineHeightFactor,
|
|
96
|
+
};
|
|
97
|
+
commit((prev) => ({
|
|
98
|
+
...prev,
|
|
99
|
+
additionalTexts: [...prev.additionalTexts, text],
|
|
100
|
+
layerOrder: withLayer(prev.layerOrder, text.id),
|
|
101
|
+
}));
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* A text with a prototype size (`maxFontSize`) at the size it fits at on `page`, which a new
|
|
105
|
+
* text, a new font or a new brand font changes; any other text, or one that already fits, as it is.
|
|
106
|
+
*/
|
|
107
|
+
const refitted = (text, page) => {
|
|
108
|
+
const fitted = fittedFontSize(text, page, height, templateOf);
|
|
109
|
+
return fitted === null || fitted === text.fontSize ? text : { ...text, fontSize: fitted };
|
|
110
|
+
};
|
|
111
|
+
return {
|
|
112
|
+
// ── Texts ────────────────────────────────────────────────────────────────
|
|
113
|
+
addHeader: () => addText('header'),
|
|
114
|
+
addSubheader: () => addText('subheader'),
|
|
115
|
+
addText: () => addText('body'),
|
|
116
|
+
addBodyTextWithContent: (content) => addText('body', content),
|
|
117
|
+
updateAdditionalText: (id, partial) => commit((prev) =>
|
|
118
|
+
// The plates that hug a text follow its new lines, width or size.
|
|
119
|
+
withHuggedPlates({
|
|
120
|
+
...prev,
|
|
121
|
+
additionalTexts: prev.additionalTexts.map((t) => {
|
|
122
|
+
if (t.id !== id)
|
|
123
|
+
return t;
|
|
124
|
+
const next = { ...t, ...partial };
|
|
125
|
+
// A committed text change refits a text with a prototype size: shrink as it grows,
|
|
126
|
+
// grow back as it shrinks. A size set by hand in the same change stands.
|
|
127
|
+
if (!('text' in partial) || 'fontSize' in partial)
|
|
128
|
+
return next;
|
|
129
|
+
return refitted(next, prev);
|
|
130
|
+
}),
|
|
131
|
+
}), true),
|
|
132
|
+
/**
|
|
133
|
+
* Sets the font of a free text to a brand role (`fontRoleOptions`): the family and the binding
|
|
134
|
+
* to the role are written together, so the text keeps following the role when the brand changes
|
|
135
|
+
* (a family set without the binding would lose it on the next rebind). One history step; the
|
|
136
|
+
* size is refitted to the new font and the plates that hug the text follow.
|
|
137
|
+
*/
|
|
138
|
+
setAdditionalTextFontRole: (id, role) => {
|
|
139
|
+
const ref = `font.${role}`;
|
|
140
|
+
const family = resolveRef(ref, activeBrand(), null);
|
|
141
|
+
if (family === null || !getState().additionalTexts.some((t) => t.id === id))
|
|
142
|
+
return;
|
|
143
|
+
commit((prev) => withHuggedPlates({
|
|
144
|
+
...prev,
|
|
145
|
+
additionalTexts: prev.additionalTexts.map((t) => t.id === id
|
|
146
|
+
? refitted({
|
|
147
|
+
...t,
|
|
148
|
+
fontFamily: fontStackFor(role, family),
|
|
149
|
+
bind: withBinding(t.bind, 'fontFamily', { ref, value: family }),
|
|
150
|
+
}, prev)
|
|
151
|
+
: t),
|
|
152
|
+
}));
|
|
153
|
+
},
|
|
154
|
+
/**
|
|
155
|
+
* How the page will look once a text edit is committed, while it is still being typed: the
|
|
156
|
+
* size the draft fits at and the plates that hug it. A function of the committed page that
|
|
157
|
+
* changes and records nothing; the canvas draws the result over the page until the edit ends,
|
|
158
|
+
* and the commit (`updateAdditionalText`) is what writes. Null for a text that is gone.
|
|
159
|
+
*/
|
|
160
|
+
previewAdditionalText: (id, draft) => {
|
|
161
|
+
const prev = getState();
|
|
162
|
+
const current = prev.additionalTexts.find((t) => t.id === id);
|
|
163
|
+
if (!current)
|
|
164
|
+
return null;
|
|
165
|
+
const fitted = fittedFontSize({ ...current, text: draft }, prev, height, templateOf);
|
|
166
|
+
const fontSize = fitted ?? current.fontSize;
|
|
167
|
+
// The text itself stays as it was: the editor shows the draft, the commit writes it.
|
|
168
|
+
const additionalTexts = prev.additionalTexts.map((t) => t.id === id ? { ...t, fontSize } : t);
|
|
169
|
+
const hugged = withHuggedPlates({
|
|
170
|
+
...prev,
|
|
171
|
+
additionalTexts: prev.additionalTexts.map((t) => t.id === id ? { ...t, text: draft, fontSize } : t),
|
|
172
|
+
});
|
|
173
|
+
return {
|
|
174
|
+
fontSize: fitted,
|
|
175
|
+
overrides: { additionalTexts, shapeInstances: hugged.shapeInstances },
|
|
176
|
+
};
|
|
177
|
+
},
|
|
178
|
+
removeAdditionalText: (id) => commit((prev) => ({
|
|
179
|
+
...prev,
|
|
180
|
+
additionalTexts: prev.additionalTexts.filter((t) => t.id !== id),
|
|
181
|
+
layerOrder: withoutLayer(prev.layerOrder, id),
|
|
182
|
+
})),
|
|
183
|
+
// ── Shapes ───────────────────────────────────────────────────────────────
|
|
184
|
+
addShape: (type, color, options) => {
|
|
185
|
+
const center = options?.center ?? { x: width / 2, y: height / 2 };
|
|
186
|
+
const shape = createShape(type, center.x, center.y, activeBrand().colors.secondary, color);
|
|
187
|
+
const placed = { ...shape, id: newId('shape') };
|
|
188
|
+
commit((prev) => ({
|
|
189
|
+
...prev,
|
|
190
|
+
shapeInstances: [...prev.shapeInstances, placed],
|
|
191
|
+
layerOrder: withLayer(prev.layerOrder, placed.id),
|
|
192
|
+
}));
|
|
193
|
+
},
|
|
194
|
+
updateShape: (id, partial) => commit((prev) => ({
|
|
195
|
+
...prev,
|
|
196
|
+
shapeInstances: prev.shapeInstances.map((x) => {
|
|
197
|
+
if (x.id !== id)
|
|
198
|
+
return x;
|
|
199
|
+
const next = { ...x, ...partial };
|
|
200
|
+
// A plate placed or sized by hand stops hugging its text; otherwise the next text
|
|
201
|
+
// edit, rebind or AI fill would snap it back.
|
|
202
|
+
if (!('hug' in next) || !HAND_GEOMETRY.some((k) => k in partial))
|
|
203
|
+
return next;
|
|
204
|
+
const { hug: _hug, ...free } = next;
|
|
205
|
+
return free;
|
|
206
|
+
}),
|
|
207
|
+
}), true),
|
|
208
|
+
removeShape: (id) => commit((prev) => ({
|
|
209
|
+
...prev,
|
|
210
|
+
shapeInstances: prev.shapeInstances.filter((x) => x.id !== id),
|
|
211
|
+
layerOrder: withoutLayer(prev.layerOrder, id),
|
|
212
|
+
})),
|
|
213
|
+
// ── Icons ────────────────────────────────────────────────────────────────
|
|
214
|
+
// `selectedIcons` lists instance ids; the first copy of an icon carries its catalogue id.
|
|
215
|
+
toggleIcon: (id, selected, options, external) => {
|
|
216
|
+
if (selected) {
|
|
217
|
+
const placed = placedIconId(id, getState().selectedIcons, () => createInstanceId('icon'));
|
|
218
|
+
const iconState = {
|
|
219
|
+
x: options?.center?.x ?? width / 2,
|
|
220
|
+
y: options?.center?.y ?? height / 2,
|
|
221
|
+
scale: s,
|
|
222
|
+
rotation: 0,
|
|
223
|
+
color: inkFor(getState().backgroundColor),
|
|
224
|
+
opacity: 1,
|
|
225
|
+
// An icon from the icon search: the page keeps its markup (to draw it) and its source (to credit it).
|
|
226
|
+
...(placed.iconId ? { iconId: placed.iconId } : {}),
|
|
227
|
+
...(external ? { svg: external.svg, source: external.source } : {}),
|
|
228
|
+
};
|
|
229
|
+
commit((prev) => ({
|
|
230
|
+
...prev,
|
|
231
|
+
selectedIcons: [...prev.selectedIcons, placed.id],
|
|
232
|
+
iconStates: { ...prev.iconStates, [placed.id]: iconState },
|
|
233
|
+
layerOrder: withLayer(prev.layerOrder, placed.id),
|
|
234
|
+
}));
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
commit((prev) => {
|
|
238
|
+
const doomed = new Set(iconInstanceIdsFor(id, prev.selectedIcons, prev.iconStates));
|
|
239
|
+
return {
|
|
240
|
+
...prev,
|
|
241
|
+
selectedIcons: prev.selectedIcons.filter((i) => !doomed.has(i)),
|
|
242
|
+
iconStates: Object.fromEntries(Object.entries(prev.iconStates).filter(([key]) => !doomed.has(key))),
|
|
243
|
+
layerOrder: (prev.layerOrder ?? []).filter((entry) => !doomed.has(entry)),
|
|
244
|
+
};
|
|
245
|
+
});
|
|
246
|
+
},
|
|
247
|
+
updateIcon: (id, partial) => commit((prev) => ({
|
|
248
|
+
...prev,
|
|
249
|
+
iconStates: { ...prev.iconStates, [id]: { ...prev.iconStates[id], ...partial } },
|
|
250
|
+
}), true),
|
|
251
|
+
// ── Emoji and other catalogue graphics ───────────────────────────────────
|
|
252
|
+
addAsset: (assetId, options) => {
|
|
253
|
+
const placed = {
|
|
254
|
+
...createAssetInstance(assetId, width, height),
|
|
255
|
+
...options?.center,
|
|
256
|
+
};
|
|
257
|
+
commit((prev) => ({
|
|
258
|
+
...prev,
|
|
259
|
+
assetInstances: [...prev.assetInstances, placed],
|
|
260
|
+
layerOrder: withLayer(prev.layerOrder, placed.id),
|
|
261
|
+
}));
|
|
262
|
+
},
|
|
263
|
+
updateAsset: (id, partial) => commit((prev) => ({
|
|
264
|
+
...prev,
|
|
265
|
+
assetInstances: prev.assetInstances.map((x) => (x.id === id ? { ...x, ...partial } : x)),
|
|
266
|
+
}), true),
|
|
267
|
+
removeAsset: (id) => commit((prev) => ({
|
|
268
|
+
...prev,
|
|
269
|
+
assetInstances: prev.assetInstances.filter((x) => x.id !== id),
|
|
270
|
+
layerOrder: withoutLayer(prev.layerOrder, id),
|
|
271
|
+
})),
|
|
272
|
+
// ── Illustrations ────────────────────────────────────────────────────────
|
|
273
|
+
addIllustration: (id, options) => {
|
|
274
|
+
void createIllustration(id, width, height).then((created) => {
|
|
275
|
+
// The primitive centres the artwork on x/y.
|
|
276
|
+
const placed = { ...created, ...options?.center };
|
|
277
|
+
commit((prev) => ({
|
|
278
|
+
...prev,
|
|
279
|
+
illustrationInstances: [...prev.illustrationInstances, placed],
|
|
280
|
+
layerOrder: withLayer(prev.layerOrder, placed.id),
|
|
281
|
+
}));
|
|
282
|
+
});
|
|
283
|
+
},
|
|
284
|
+
updateIllustration: (id, partial) => commit((prev) => ({
|
|
285
|
+
...prev,
|
|
286
|
+
illustrationInstances: prev.illustrationInstances.map((x) => x.id === id ? { ...x, ...partial } : x),
|
|
287
|
+
}), true),
|
|
288
|
+
removeIllustration: (id) => commit((prev) => ({
|
|
289
|
+
...prev,
|
|
290
|
+
illustrationInstances: prev.illustrationInstances.filter((x) => x.id !== id),
|
|
291
|
+
layerOrder: withoutLayer(prev.layerOrder, id),
|
|
292
|
+
})),
|
|
293
|
+
// ── Placed images (uploads) ──────────────────────────────────────────────
|
|
294
|
+
addUserImageFromUrl: (url, fileName, options) => {
|
|
295
|
+
void createUserImageInstanceFromUrl(url, fileName, width, height, options).then((image) => commit((prev) => ({
|
|
296
|
+
...prev,
|
|
297
|
+
userImageInstances: [...prev.userImageInstances, image],
|
|
298
|
+
layerOrder: withLayer(prev.layerOrder, image.id),
|
|
299
|
+
})));
|
|
300
|
+
},
|
|
301
|
+
updateUserImage: (id, partial) => commit((prev) => ({
|
|
302
|
+
...prev,
|
|
303
|
+
userImageInstances: prev.userImageInstances.map((x) => x.id === id ? { ...x, ...partial } : x),
|
|
304
|
+
}), true),
|
|
305
|
+
removeUserImage: (id) => commit((prev) => ({
|
|
306
|
+
...prev,
|
|
307
|
+
userImageInstances: prev.userImageInstances.filter((x) => x.id !== id),
|
|
308
|
+
layerOrder: withoutLayer(prev.layerOrder, id),
|
|
309
|
+
})),
|
|
310
|
+
// ── Blocks ───────────────────────────────────────────────────────────────
|
|
311
|
+
// A placed block is one history step; its parts form a group, and the page takes the block's
|
|
312
|
+
// colour world when it has none, so a brand change later rebinds the block as it does the page.
|
|
313
|
+
insertBlock: (block) => {
|
|
314
|
+
// Once, outside `commit`: it runs the change for the state and again for the history entry.
|
|
315
|
+
const ids = blockElementIds(block);
|
|
316
|
+
const group = {
|
|
317
|
+
id: newId('group'),
|
|
318
|
+
...(block.blockRef && { blockRef: block.blockRef }),
|
|
319
|
+
elementIds: ids,
|
|
320
|
+
};
|
|
321
|
+
commit((prev) => {
|
|
322
|
+
return {
|
|
323
|
+
...prev,
|
|
324
|
+
additionalTexts: [...prev.additionalTexts, ...block.additionalTexts],
|
|
325
|
+
shapeInstances: [...prev.shapeInstances, ...block.shapeInstances],
|
|
326
|
+
assetInstances: [...(prev.assetInstances ?? []), ...block.assetInstances],
|
|
327
|
+
chartInstances: [...(prev.chartInstances ?? []), ...block.chartInstances],
|
|
328
|
+
userImageInstances: [...prev.userImageInstances, ...block.userImageInstances],
|
|
329
|
+
pillBadgeInstances: [...(prev.pillBadgeInstances ?? []), ...block.pillBadgeInstances],
|
|
330
|
+
circleBadgeInstances: [
|
|
331
|
+
...(prev.circleBadgeInstances ?? []),
|
|
332
|
+
...block.circleBadgeInstances,
|
|
333
|
+
],
|
|
334
|
+
selectedIcons: [...prev.selectedIcons, ...block.selectedIcons],
|
|
335
|
+
iconStates: { ...prev.iconStates, ...block.iconStates },
|
|
336
|
+
layerOrder: [...(prev.layerOrder ?? []), ...block.layerOrder],
|
|
337
|
+
// A group of one is no group (`pruneGroups` would drop it anyway).
|
|
338
|
+
...(ids.length > 1 && { groups: [...(prev.groups ?? []), group] }),
|
|
339
|
+
...(!prev.brandScheme && block.brandScheme && { brandScheme: block.brandScheme }),
|
|
340
|
+
};
|
|
341
|
+
});
|
|
342
|
+
},
|
|
343
|
+
// ── Background ───────────────────────────────────────────────────────────
|
|
344
|
+
// A swatch pick carries its role: the ground then follows the brand. A free colour unbinds it.
|
|
345
|
+
setBackgroundColor: (color, ref) => commit((prev) => {
|
|
346
|
+
const { bind: _previous, ...rest } = prev;
|
|
347
|
+
const bind = ref
|
|
348
|
+
? withBinding(prev.bind, 'backgroundColor', { ref, value: color })
|
|
349
|
+
: withoutBinding(prev.bind, 'backgroundColor');
|
|
350
|
+
return { ...rest, backgroundColor: color, ...(bind ? { bind } : {}) };
|
|
351
|
+
}, true),
|
|
352
|
+
};
|
|
353
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type FullCanvasConfig } from '@ggraphic/editor/configs/types';
|
|
2
|
+
import { type PageActions } from './actions.js';
|
|
3
|
+
import { type PageSeed, type PageState } from './pageState.js';
|
|
4
|
+
import { type TemplateLookup } from './slotBudgets.js';
|
|
5
|
+
export interface PageConfigOptions {
|
|
6
|
+
/** The config id the pages document stores. */
|
|
7
|
+
id: string;
|
|
8
|
+
/** The canvas format; the default format when unknown. */
|
|
9
|
+
formatId: string | undefined;
|
|
10
|
+
/** What a page that has never been saved starts with, on a `width` × `height` sheet. */
|
|
11
|
+
seedFor: (width: number, height: number) => PageSeed;
|
|
12
|
+
/** The host's catalogue, for the line limits of template slots. */
|
|
13
|
+
templateOf?: TemplateLookup;
|
|
14
|
+
}
|
|
15
|
+
export declare function createPageConfig({ id, formatId, seedFor, templateOf, }: PageConfigOptions): FullCanvasConfig<PageState, PageActions>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* The one page config for templates as data: a colour ground and the editor's free elements, with
|
|
4
|
+
* the Text, Elements, Uploads and Tools tabs. Templates differ only in what a fresh page starts
|
|
5
|
+
* with (`seedFor`). The config lays out on the document's format, so one template serves every
|
|
6
|
+
* format.
|
|
7
|
+
*/
|
|
8
|
+
import { activeBrand } from '@ggraphic/editor/brand/brandRegistry';
|
|
9
|
+
import { makeSectionDefiner } from '@ggraphic/editor/configs/factory/defineSection';
|
|
10
|
+
import { injectFeatureProps } from '@ggraphic/editor/configs/featureInjector';
|
|
11
|
+
import { getCanvasFormatOrDefault } from '@ggraphic/editor/formats/index';
|
|
12
|
+
import { messages } from '@ggraphic/editor/i18n/messages';
|
|
13
|
+
import { AssetsSection, CombinedTextSection, ToolsSection, UploadsSection, } from '@ggraphic/editor/sidebar/sections/index';
|
|
14
|
+
import { PiSquaresFour, PiTextAa, PiToolbox, PiUploadSimple } from 'react-icons/pi';
|
|
15
|
+
import { createPageActions } from './actions.js';
|
|
16
|
+
import { pageStateFrom } from './pageState.js';
|
|
17
|
+
import { noTemplates } from './slotBudgets.js';
|
|
18
|
+
const section = makeSectionDefiner();
|
|
19
|
+
// Read when the picker renders, so a project of another organisation offers its colours.
|
|
20
|
+
const backgroundColors = () => activeBrand().palette.map(({ id, label, color, ref }) => ({
|
|
21
|
+
id,
|
|
22
|
+
label,
|
|
23
|
+
color,
|
|
24
|
+
...(ref && { ref }),
|
|
25
|
+
}));
|
|
26
|
+
export function createPageConfig({ id, formatId, seedFor, templateOf = noTemplates, }) {
|
|
27
|
+
const { width, height } = getCanvasFormatOrDefault(formatId);
|
|
28
|
+
const t = messages().pageTabs;
|
|
29
|
+
return {
|
|
30
|
+
id,
|
|
31
|
+
canvas: { width, height },
|
|
32
|
+
features: { icons: true, shapes: true, illustrations: true },
|
|
33
|
+
multiPage: { enabled: true, maxPages: 10, heterogeneous: true },
|
|
34
|
+
backgroundType: 'color',
|
|
35
|
+
fonts: { primary: activeBrand().fonts.headline, fontSize: 60, requireFontLoad: true },
|
|
36
|
+
tabs: [
|
|
37
|
+
{ id: 'text', icon: PiTextAa, label: t.text, ariaLabel: t.textAriaLabel },
|
|
38
|
+
{ id: 'elements', icon: PiSquaresFour, label: t.elements, ariaLabel: t.elementsAriaLabel },
|
|
39
|
+
{ id: 'uploads', icon: PiUploadSimple, label: t.uploads, ariaLabel: t.uploadsAriaLabel },
|
|
40
|
+
{ id: 'tools', icon: PiToolbox, label: t.tools, ariaLabel: t.toolsAriaLabel },
|
|
41
|
+
],
|
|
42
|
+
sections: {
|
|
43
|
+
text: section({
|
|
44
|
+
component: CombinedTextSection,
|
|
45
|
+
propsFactory: (state, actions) => ({
|
|
46
|
+
additionalTexts: state.additionalTexts,
|
|
47
|
+
onAddHeader: actions.addHeader,
|
|
48
|
+
onAddSubheader: actions.addSubheader,
|
|
49
|
+
onAddText: actions.addText,
|
|
50
|
+
onUpdateText: actions.updateAdditionalText,
|
|
51
|
+
onRemoveText: actions.removeAdditionalText,
|
|
52
|
+
}),
|
|
53
|
+
}),
|
|
54
|
+
elements: section({
|
|
55
|
+
component: AssetsSection,
|
|
56
|
+
propsFactory: (state, actions, context) => ({
|
|
57
|
+
onAddAsset: actions.addAsset,
|
|
58
|
+
...injectFeatureProps(state, actions, context),
|
|
59
|
+
}),
|
|
60
|
+
}),
|
|
61
|
+
uploads: section({
|
|
62
|
+
component: UploadsSection,
|
|
63
|
+
propsFactory: (_state, actions) => ({ onPlaceFromUrl: actions.addUserImageFromUrl }),
|
|
64
|
+
}),
|
|
65
|
+
tools: section({
|
|
66
|
+
component: ToolsSection,
|
|
67
|
+
propsFactory: (_state, actions) => ({ onPlaceImageUrl: actions.addUserImageFromUrl }),
|
|
68
|
+
}),
|
|
69
|
+
},
|
|
70
|
+
background: (state, actions) => ({
|
|
71
|
+
colors: backgroundColors(),
|
|
72
|
+
currentColor: state.backgroundColor,
|
|
73
|
+
onColorChange: actions.setBackgroundColor,
|
|
74
|
+
}),
|
|
75
|
+
elements: [
|
|
76
|
+
{
|
|
77
|
+
id: 'background',
|
|
78
|
+
type: 'background',
|
|
79
|
+
x: 0,
|
|
80
|
+
y: 0,
|
|
81
|
+
order: -1,
|
|
82
|
+
width,
|
|
83
|
+
height,
|
|
84
|
+
fillStateKey: 'backgroundColor',
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
calculateLayout: () => ({}),
|
|
88
|
+
createInitialState: (props) => pageStateFrom(props, seedFor(width, height)),
|
|
89
|
+
createActions: (getState, setState, saveToHistory, debouncedSaveToHistory) => createPageActions(getState, setState, saveToHistory, debouncedSaveToHistory, width, height, templateOf),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type TemplateLookup } from './slotBudgets.js';
|
|
2
|
+
/**
|
|
3
|
+
* The size a text with a numeric `maxFontSize` fits at on `page` (a page of a template of `templateOf`
|
|
4
|
+
* takes its line limit from the slot of that name); null for a text without one, or one that cannot be fitted.
|
|
5
|
+
*/
|
|
6
|
+
export declare function fittedFontSize(el: Readonly<Record<string, unknown>>, page: Readonly<Record<string, unknown>>, canvasHeight: number, templateOf: TemplateLookup): number | null;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Text sizes fitted to the brand's fonts, up to the prototype's size (`maxFontSize`). The fit
|
|
4
|
+
* itself is the gate adapter's (`TEMPLATE_GATE_ADAPTER.fitText`, the width tables the gate
|
|
5
|
+
* measures with); this adds what a page knows beyond the element: the line limit of its
|
|
6
|
+
* template slot and the room down to the element below it.
|
|
7
|
+
*/
|
|
8
|
+
import { roomBelow } from '@ggraphic/core';
|
|
9
|
+
import { TEMPLATE_GATE_ADAPTER } from '@ggraphic/editor/templates/templateGeometry';
|
|
10
|
+
import { slotBudgetFor } from './slotBudgets.js';
|
|
11
|
+
/**
|
|
12
|
+
* The size a text with a numeric `maxFontSize` fits at on `page` (a page of a template of `templateOf`
|
|
13
|
+
* takes its line limit from the slot of that name); null for a text without one, or one that cannot be fitted.
|
|
14
|
+
*/
|
|
15
|
+
export function fittedFontSize(el, page, canvasHeight, templateOf) {
|
|
16
|
+
if (typeof el['maxFontSize'] !== 'number')
|
|
17
|
+
return null;
|
|
18
|
+
const maxLines = slotBudgetFor(page, String(el['id']), templateOf)?.budget.maxLines;
|
|
19
|
+
const maxHeight = roomBelow(page, 'additionalTexts', el, (k, e) => TEMPLATE_GATE_ADAPTER.box(k, e, 1));
|
|
20
|
+
const fit = TEMPLATE_GATE_ADAPTER.fitText?.(el, {
|
|
21
|
+
...(maxLines === undefined ? {} : { maxLines }),
|
|
22
|
+
...(maxHeight === undefined ? {} : { maxHeight }),
|
|
23
|
+
canvasHeight,
|
|
24
|
+
});
|
|
25
|
+
return fit ? fit.fontSize : null;
|
|
26
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plates that hug their text (`hug` on a shape), kept in step with the text wherever a page
|
|
3
|
+
* changes a text's geometry after placing: a committed edit, a brand rebind, an AI fill.
|
|
4
|
+
* Placing and the gate hug through core; this is the page-level door for the rest.
|
|
5
|
+
*/
|
|
6
|
+
import { type HugAdapter } from '@ggraphic/core';
|
|
7
|
+
/** The editor's measures for hugging; null parts of the gate adapter never occur in the editor. */
|
|
8
|
+
export declare const HUG_ADAPTER: HugAdapter;
|
|
9
|
+
/** `page` with every hugging plate brought to its text; the same object when none moves. */
|
|
10
|
+
export declare function withHuggedPlates<P extends Readonly<Record<string, unknown>>>(page: P): P;
|
package/dist/page/hug.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Plates that hug their text (`hug` on a shape), kept in step with the text wherever a page
|
|
4
|
+
* changes a text's geometry after placing: a committed edit, a brand rebind, an AI fill.
|
|
5
|
+
* Placing and the gate hug through core; this is the page-level door for the rest.
|
|
6
|
+
*/
|
|
7
|
+
import { hugPlates } from '@ggraphic/core';
|
|
8
|
+
import { TEMPLATE_GATE_ADAPTER } from '@ggraphic/editor/templates/templateGeometry';
|
|
9
|
+
/** The editor's measures for hugging; null parts of the gate adapter never occur in the editor. */
|
|
10
|
+
export const HUG_ADAPTER = {
|
|
11
|
+
box: TEMPLATE_GATE_ADAPTER.box,
|
|
12
|
+
textExtent: TEMPLATE_GATE_ADAPTER.textExtent,
|
|
13
|
+
placeBox: TEMPLATE_GATE_ADAPTER.placeBox,
|
|
14
|
+
};
|
|
15
|
+
/** `page` with every hugging plate brought to its text; the same object when none moves. */
|
|
16
|
+
export function withHuggedPlates(page) {
|
|
17
|
+
return hugPlates(page, HUG_ADAPTER);
|
|
18
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Places a template on a page: the one door from template data to page state. */
|
|
2
|
+
import { type BrandPack, type SchemeId } from '@ggraphic/core';
|
|
3
|
+
import { type TemplateFile, type TemplateFormat } from '@ggraphic/schema';
|
|
4
|
+
/** `template` placed on a `width` × `height` sheet of the given aspect; null when it cannot be placed. */
|
|
5
|
+
export declare function placeTemplate(template: TemplateFile, canvas: {
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
}, format: TemplateFormat | null, schemeId?: SchemeId | null, brand?: BrandPack): Record<string, unknown> | null;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
/** Places a template on a page: the one door from template data to page state. */
|
|
3
|
+
import { instantiateTemplatePage, } from '@ggraphic/core';
|
|
4
|
+
import { activeBrand } from '@ggraphic/editor/brand/brandRegistry';
|
|
5
|
+
import { TEMPLATE_GATE_ADAPTER, TEMPLATE_GEOMETRY, } from '@ggraphic/editor/templates/templateGeometry';
|
|
6
|
+
import { HUG_ADAPTER } from './hug.js';
|
|
7
|
+
import { logoSizeOf } from './logoSize.js';
|
|
8
|
+
import { fontFallbackFor } from './typography.js';
|
|
9
|
+
// A parsed template file is what placing takes; if the schema drifts from core's input, this stops compiling.
|
|
10
|
+
const _assert = {};
|
|
11
|
+
void _assert;
|
|
12
|
+
/** `template` placed on a `width` × `height` sheet of the given aspect; null when it cannot be placed. */
|
|
13
|
+
export function placeTemplate(template, canvas, format, schemeId = null, brand = activeBrand()) {
|
|
14
|
+
if (!format)
|
|
15
|
+
return null;
|
|
16
|
+
const result = instantiateTemplatePage(template, {
|
|
17
|
+
brand,
|
|
18
|
+
canvas,
|
|
19
|
+
format,
|
|
20
|
+
geometry: TEMPLATE_GEOMETRY,
|
|
21
|
+
locale: 'en',
|
|
22
|
+
fontFallback: fontFallbackFor,
|
|
23
|
+
fitText: TEMPLATE_GATE_ADAPTER.fitText,
|
|
24
|
+
boxOf: (key, el) => TEMPLATE_GATE_ADAPTER.box(key, el, 1),
|
|
25
|
+
hug: HUG_ADAPTER,
|
|
26
|
+
schemeId,
|
|
27
|
+
logoSizeOf,
|
|
28
|
+
});
|
|
29
|
+
return result.ok ? result.state : null;
|
|
30
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function rememberLogoSize(src: string, width: number, height: number): void;
|
|
2
|
+
/** The measured size, or null while unknown or when the image has no area. */
|
|
3
|
+
export declare function logoSizeOf(src: string): {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
} | null;
|
|
7
|
+
/** Loads the image to learn its size; a logo that does not load stays unmeasured. */
|
|
8
|
+
export declare function measureLogo(src: string): Promise<void>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Natural sizes of brand logos. A template seed is built synchronously but needs the
|
|
4
|
+
* logo's proportions, so the host measures the image first (`measureLogo`, when it applies a
|
|
5
|
+
* brand) and placing reads it here.
|
|
6
|
+
*/
|
|
7
|
+
const sizes = new Map();
|
|
8
|
+
export function rememberLogoSize(src, width, height) {
|
|
9
|
+
sizes.set(src, { width, height });
|
|
10
|
+
}
|
|
11
|
+
/** The measured size, or null while unknown or when the image has no area. */
|
|
12
|
+
export function logoSizeOf(src) {
|
|
13
|
+
const size = sizes.get(src);
|
|
14
|
+
return size && size.width > 0 && size.height > 0 ? size : null;
|
|
15
|
+
}
|
|
16
|
+
/** Loads the image to learn its size; a logo that does not load stays unmeasured. */
|
|
17
|
+
export function measureLogo(src) {
|
|
18
|
+
if (logoSizeOf(src) || typeof Image === 'undefined')
|
|
19
|
+
return Promise.resolve();
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
const img = new Image();
|
|
22
|
+
img.onload = () => {
|
|
23
|
+
rememberLogoSize(src, img.naturalWidth, img.naturalHeight);
|
|
24
|
+
resolve();
|
|
25
|
+
};
|
|
26
|
+
img.onerror = () => resolve();
|
|
27
|
+
img.src = src;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state of a free page: what a template seeds, what the editor stores and reads back. Every
|
|
3
|
+
* page of a host that offers templates as data (`createPageConfig`) has this shape.
|
|
4
|
+
*/
|
|
5
|
+
import { type Bindings } from '@ggraphic/core';
|
|
6
|
+
import { type BaseCanvasState } from '@ggraphic/editor/configs/factory/baseTypes';
|
|
7
|
+
/** An element group on a page; the full semantics come with the groups feature. */
|
|
8
|
+
export interface PageGroup {
|
|
9
|
+
id: string;
|
|
10
|
+
blockRef?: {
|
|
11
|
+
id: string;
|
|
12
|
+
version: number;
|
|
13
|
+
};
|
|
14
|
+
elementIds: string[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A free page: the editor's element collections plus a colour ground. Only
|
|
18
|
+
* keys the editor writes back into the pages document persist
|
|
19
|
+
* (`PAGE_PERSISTED_STATE_KEYS`): the collections, `backgroundColor` and the
|
|
20
|
+
* brand binding keys, `templateRef` and `groups`.
|
|
21
|
+
*/
|
|
22
|
+
export interface PageState extends BaseCanvasState {
|
|
23
|
+
backgroundColor: string;
|
|
24
|
+
layerOrder: string[];
|
|
25
|
+
bind?: Bindings;
|
|
26
|
+
brandScheme?: string;
|
|
27
|
+
brandStamp?: string;
|
|
28
|
+
templateRef?: {
|
|
29
|
+
id: string;
|
|
30
|
+
version: number;
|
|
31
|
+
};
|
|
32
|
+
groups?: PageGroup[];
|
|
33
|
+
}
|
|
34
|
+
/** What a template puts on a fresh page. */
|
|
35
|
+
export type PageSeed = Partial<Pick<PageState, 'backgroundColor' | 'additionalTexts' | 'shapeInstances' | 'userImageInstances' | 'bind' | 'brandScheme' | 'brandStamp'>>;
|
|
36
|
+
/**
|
|
37
|
+
* The page state from what is stored (`props`). A page that has never been
|
|
38
|
+
* saved has no collections yet and gets the template's seed; a saved one keeps
|
|
39
|
+
* exactly what it holds, also when the user emptied it.
|
|
40
|
+
*/
|
|
41
|
+
export declare function pageStateFrom(props: Record<string, unknown>, seed: PageSeed): PageState;
|