@histoire/plugin-designs 0.0.0-stage → 1.0.0-beta.3
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 +23 -2
- package/dist/client.d.ts +2 -0
- package/dist/client.js +1819 -0
- package/dist/designs/embed-url.d.ts +9 -0
- package/dist/designs/iframe-attrs.d.ts +11 -0
- package/dist/designs/resolve.d.ts +30 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +22 -0
- package/dist/options.d.ts +23 -0
- package/dist/options.js +10 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.js +1 -0
- package/dist/workbench/DesignContent.vue.d.ts +8 -0
- package/dist/workbench/DesignPanel.vue.d.ts +11 -0
- package/dist/workbench/EmbedFrame.vue.d.ts +8 -0
- package/dist/workbench/canvas/DesignModeBar.vue.d.ts +12 -0
- package/dist/workbench/canvas/design-element.d.ts +43 -0
- package/dist/workbench/canvas/design-label.d.ts +18 -0
- package/dist/workbench/canvas/mode-bar.d.ts +14 -0
- package/dist/workbench/canvas/overlay-chrome.d.ts +29 -0
- package/dist/workbench/canvas/overlay-geometry.d.ts +62 -0
- package/dist/workbench/canvas/overlay-state.d.ts +30 -0
- package/dist/workbench/canvas/side-by-side-view.d.ts +10 -0
- package/dist/workbench/canvas/view-state.d.ts +21 -0
- package/dist/workbench/image/ImageViewer.vue.d.ts +8 -0
- package/dist/workbench/image/pan-zoom.d.ts +72 -0
- package/dist/workbench/model.d.ts +40 -0
- package/package.json +121 -3
package/dist/client.js
ADDED
|
@@ -0,0 +1,1819 @@
|
|
|
1
|
+
import { defineWorkbenchPlugin, installPluginStyles } from "@histoire/plugin-kit";
|
|
2
|
+
import { WorkbenchEmptyState, WorkbenchErrorState, WorkbenchLoadingState, defineWorkbenchComponent } from "@histoire/plugin-kit/vue";
|
|
3
|
+
import { Fragment, computed, createApp, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, h, mergeProps, normalizeStyle, onScopeDispose, openBlock, ref, renderList, shallowRef, toDisplayString, unref, vShow, watch, withCtx, withDirectives, withModifiers } from "vue";
|
|
4
|
+
import { BuiltinIcon, HstButton, HstButtonGroup, HstSelect, HstSlider } from "@histoire/controls/vue";
|
|
5
|
+
import { getHistoireIcon } from "@histoire/shared/dist/icons/index.js";
|
|
6
|
+
// src/workbench/canvas/overlay-geometry.ts
|
|
7
|
+
/** Canvas modes in toggle order. */
|
|
8
|
+
var CANVAS_MODES = [
|
|
9
|
+
"side-by-side",
|
|
10
|
+
"overlay",
|
|
11
|
+
"split"
|
|
12
|
+
];
|
|
13
|
+
/** Smallest design box edge in canvas units, so the grips stay reachable. */
|
|
14
|
+
var MIN_SIZE = 24;
|
|
15
|
+
/**
|
|
16
|
+
* Clamps a number into a range.
|
|
17
|
+
* @param value Number.
|
|
18
|
+
* @param min Lower bound.
|
|
19
|
+
* @param max Upper bound.
|
|
20
|
+
*/
|
|
21
|
+
function clamp(value, min, max) {
|
|
22
|
+
return Math.min(max, Math.max(min, value));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Initial overlay: top-left on the frame box, scaled down to the frame width when wider.
|
|
26
|
+
* @param frame Frame box in canvas space (label excluded).
|
|
27
|
+
* @param design Design box size.
|
|
28
|
+
*/
|
|
29
|
+
function defaultPlacement(frame, design) {
|
|
30
|
+
const scale = Math.min(1, frame.width / design.width);
|
|
31
|
+
return {
|
|
32
|
+
x: frame.x,
|
|
33
|
+
y: frame.y,
|
|
34
|
+
width: design.width * scale,
|
|
35
|
+
height: design.height * scale,
|
|
36
|
+
split: .5
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Moves the design box.
|
|
41
|
+
* @param placement Current placement.
|
|
42
|
+
* @param dx Horizontal delta in canvas units.
|
|
43
|
+
* @param dy Vertical delta in canvas units.
|
|
44
|
+
*/
|
|
45
|
+
function movePlacement(placement, dx, dy) {
|
|
46
|
+
return {
|
|
47
|
+
...placement,
|
|
48
|
+
x: placement.x + dx,
|
|
49
|
+
y: placement.y + dy
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Resizes the design box from its bottom-right corner.
|
|
54
|
+
* @param placement Current placement.
|
|
55
|
+
* @param dx Horizontal delta in canvas units.
|
|
56
|
+
* @param dy Vertical delta in canvas units.
|
|
57
|
+
* @param keepAspect Keep the width/height ratio; the larger relative change drives both edges.
|
|
58
|
+
*/
|
|
59
|
+
function resizePlacement(placement, dx, dy, keepAspect) {
|
|
60
|
+
if (!keepAspect) return {
|
|
61
|
+
...placement,
|
|
62
|
+
width: Math.max(MIN_SIZE, placement.width + dx),
|
|
63
|
+
height: Math.max(MIN_SIZE, placement.height + dy)
|
|
64
|
+
};
|
|
65
|
+
const widthScale = (placement.width + dx) / placement.width;
|
|
66
|
+
const heightScale = (placement.height + dy) / placement.height;
|
|
67
|
+
const scale = Math.abs(widthScale - 1) >= Math.abs(heightScale - 1) ? widthScale : heightScale;
|
|
68
|
+
const bounded = Math.max(MIN_SIZE / Math.min(placement.width, placement.height), scale);
|
|
69
|
+
return {
|
|
70
|
+
...placement,
|
|
71
|
+
width: placement.width * bounded,
|
|
72
|
+
height: placement.height * bounded
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Moves the split divider to a canvas x position, clamped to the design box.
|
|
77
|
+
* @param placement Current placement.
|
|
78
|
+
* @param canvasX Divider position in canvas units.
|
|
79
|
+
*/
|
|
80
|
+
function setSplit(placement, canvasX) {
|
|
81
|
+
return {
|
|
82
|
+
...placement,
|
|
83
|
+
split: clamp((canvasX - placement.x) / placement.width, 0, 1)
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Union of the variant column and the design box, framed by Fit.
|
|
88
|
+
* @param column Variant column box (label included).
|
|
89
|
+
* @param placement Design box.
|
|
90
|
+
*/
|
|
91
|
+
function overlayBounds(column, placement) {
|
|
92
|
+
const x = Math.min(column.x, placement.x);
|
|
93
|
+
const y = Math.min(column.y, placement.y);
|
|
94
|
+
const right = Math.max(column.x + column.width, placement.x + placement.width);
|
|
95
|
+
const bottom = Math.max(column.y + column.height, placement.y + placement.height);
|
|
96
|
+
return {
|
|
97
|
+
x,
|
|
98
|
+
y,
|
|
99
|
+
width: right - x,
|
|
100
|
+
height: bottom - y
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Stored mode, or side by side for anything unknown (preferences are user storage).
|
|
105
|
+
* @param value Stored value.
|
|
106
|
+
*/
|
|
107
|
+
function parseMode(value) {
|
|
108
|
+
return CANVAS_MODES.includes(value) ? value : "side-by-side";
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Stored placement, or `undefined` when it is not a usable box.
|
|
112
|
+
* @param value Stored value.
|
|
113
|
+
*/
|
|
114
|
+
function parsePlacement(value) {
|
|
115
|
+
if (!value || typeof value !== "object") return;
|
|
116
|
+
const { x, y, width, height, split } = value;
|
|
117
|
+
if (![
|
|
118
|
+
x,
|
|
119
|
+
y,
|
|
120
|
+
width,
|
|
121
|
+
height,
|
|
122
|
+
split
|
|
123
|
+
].every((number) => typeof number === "number" && Number.isFinite(number))) return;
|
|
124
|
+
if (width <= 0 || height <= 0) return;
|
|
125
|
+
return {
|
|
126
|
+
x,
|
|
127
|
+
y,
|
|
128
|
+
width,
|
|
129
|
+
height,
|
|
130
|
+
split: clamp(split, 0, 1)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// src/workbench/canvas/overlay-state.ts
|
|
134
|
+
/** Opacity used before the user picks one. */
|
|
135
|
+
var DEFAULT_OPACITY = .5;
|
|
136
|
+
/** Lowest opacity, so the design never disappears entirely. */
|
|
137
|
+
var MIN_OPACITY = .1;
|
|
138
|
+
/**
|
|
139
|
+
* Clamps an opacity to the visible range.
|
|
140
|
+
* @param value Opacity.
|
|
141
|
+
*/
|
|
142
|
+
function clampOpacity(value) {
|
|
143
|
+
return Math.min(1, Math.max(MIN_OPACITY, value));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Creates the preferences-backed overlay state. Mode and opacity are global; placements are
|
|
147
|
+
* per variant design entry.
|
|
148
|
+
* @param api Plugin API.
|
|
149
|
+
*/
|
|
150
|
+
function createOverlayState(api) {
|
|
151
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
152
|
+
/**
|
|
153
|
+
* Preference key of one variant design placement.
|
|
154
|
+
* @param target Variant.
|
|
155
|
+
* @param entryIndex Design entry index.
|
|
156
|
+
*/
|
|
157
|
+
const placementKey = (target, entryIndex) => `overlay:${target.storyId}:${target.variantId}:${entryIndex}`;
|
|
158
|
+
/** Updates the view and its comparison bar. */
|
|
159
|
+
function notify() {
|
|
160
|
+
for (const listener of [...listeners]) listener();
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
mode: () => parseMode(api.preferences.get("canvas-mode", null)),
|
|
164
|
+
setMode(mode) {
|
|
165
|
+
api.preferences.set("canvas-mode", mode);
|
|
166
|
+
notify();
|
|
167
|
+
},
|
|
168
|
+
opacity() {
|
|
169
|
+
const stored = api.preferences.get("overlay-opacity", DEFAULT_OPACITY);
|
|
170
|
+
return typeof stored === "number" && Number.isFinite(stored) ? clampOpacity(stored) : DEFAULT_OPACITY;
|
|
171
|
+
},
|
|
172
|
+
setOpacity(value) {
|
|
173
|
+
api.preferences.set("overlay-opacity", clampOpacity(value));
|
|
174
|
+
notify();
|
|
175
|
+
},
|
|
176
|
+
placement: (target, entryIndex) => parsePlacement(api.preferences.get(placementKey(target, entryIndex), null)),
|
|
177
|
+
setPlacement(target, entryIndex, placement) {
|
|
178
|
+
api.preferences.set(placementKey(target, entryIndex), { ...placement });
|
|
179
|
+
notify();
|
|
180
|
+
},
|
|
181
|
+
reset(target, entryIndex) {
|
|
182
|
+
api.preferences.set(placementKey(target, entryIndex), null);
|
|
183
|
+
notify();
|
|
184
|
+
},
|
|
185
|
+
subscribe(listener) {
|
|
186
|
+
const entry = () => listener();
|
|
187
|
+
listeners.add(entry);
|
|
188
|
+
return () => void listeners.delete(entry);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
// src/designs/iframe-attrs.ts
|
|
193
|
+
/** Sandbox tokens of every embedded design: scripts, popups (open in tool) and forms (sign-in). */
|
|
194
|
+
var SANDBOX = "allow-scripts allow-popups allow-popups-to-escape-sandbox allow-forms";
|
|
195
|
+
/**
|
|
196
|
+
* Iframe attributes of an embedded design, shared by the inspector tab and canvas view.
|
|
197
|
+
* Design tools run scripts and need their own origin (cookies for sign-in, live viewer); that
|
|
198
|
+
* origin is distinct from the workbench, so they still cannot reach it nor navigate it. A
|
|
199
|
+
* design served from the workbench origin keeps an opaque origin instead: with both
|
|
200
|
+
* `allow-scripts` and `allow-same-origin`, it could script the workbench and lift its sandbox.
|
|
201
|
+
* @param design Embedded design.
|
|
202
|
+
* @param origin Workbench origin; defaults to the current document's.
|
|
203
|
+
*/
|
|
204
|
+
function iframeAttributes(design, origin = globalThis.location?.origin) {
|
|
205
|
+
const sameOrigin = new URL(design.src).origin === origin;
|
|
206
|
+
return {
|
|
207
|
+
src: design.src,
|
|
208
|
+
title: design.name,
|
|
209
|
+
sandbox: sameOrigin ? SANDBOX : `${SANDBOX} allow-same-origin`,
|
|
210
|
+
referrerpolicy: "strict-origin-when-cross-origin",
|
|
211
|
+
loading: "lazy",
|
|
212
|
+
...design.allowFullscreen ? {
|
|
213
|
+
allow: "fullscreen",
|
|
214
|
+
allowfullscreen: ""
|
|
215
|
+
} : {}
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
// src/workbench/canvas/design-element.ts
|
|
219
|
+
/** Default box of iframes, of images before they load and of images without intrinsic size. */
|
|
220
|
+
var EMBED_SIZE = {
|
|
221
|
+
width: 960,
|
|
222
|
+
height: 720
|
|
223
|
+
};
|
|
224
|
+
/** Box of link cards. */
|
|
225
|
+
var LINK_SIZE = {
|
|
226
|
+
width: 360,
|
|
227
|
+
height: 140
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Creates an element with a class and optional text.
|
|
231
|
+
* @param tag Tag name.
|
|
232
|
+
* @param className Class attribute.
|
|
233
|
+
* @param text Text content.
|
|
234
|
+
*/
|
|
235
|
+
function element(tag, className, text) {
|
|
236
|
+
const node = document.createElement(tag);
|
|
237
|
+
node.className = className;
|
|
238
|
+
if (text !== void 0) node.textContent = text;
|
|
239
|
+
return node;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Renders one design for the canvas. Images report their natural size (times the declared
|
|
243
|
+
* scale) once loaded so the view can size its box and re-fit. The image `offset` is ignored
|
|
244
|
+
* here: the canvas is panned by the user, so it only applies to the inspector viewer.
|
|
245
|
+
* @param design Design to render.
|
|
246
|
+
* @param events Image load callbacks.
|
|
247
|
+
*/
|
|
248
|
+
function createDesignElement(design, events) {
|
|
249
|
+
if (design.type === "image") {
|
|
250
|
+
const image = element("img", "histoire-designs-canvas-image");
|
|
251
|
+
image.alt = design.alt;
|
|
252
|
+
image.draggable = false;
|
|
253
|
+
image.addEventListener("load", () => {
|
|
254
|
+
if (!image.naturalWidth || !image.naturalHeight) return;
|
|
255
|
+
const scale = design.scale ?? 1;
|
|
256
|
+
events.onSize({
|
|
257
|
+
width: image.naturalWidth * scale,
|
|
258
|
+
height: image.naturalHeight * scale
|
|
259
|
+
});
|
|
260
|
+
}, { once: true });
|
|
261
|
+
image.addEventListener("error", () => events.onError(), { once: true });
|
|
262
|
+
image.src = design.src;
|
|
263
|
+
return {
|
|
264
|
+
node: image,
|
|
265
|
+
size: EMBED_SIZE
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
if (design.type === "link") {
|
|
269
|
+
const card = element("div", "histoire-designs-canvas-link");
|
|
270
|
+
const anchor = element("a", "", design.label);
|
|
271
|
+
Object.assign(anchor, {
|
|
272
|
+
href: design.externalUrl,
|
|
273
|
+
target: "_blank",
|
|
274
|
+
rel: "noopener noreferrer"
|
|
275
|
+
});
|
|
276
|
+
card.append(anchor, element("span", "histoire-designs-canvas-url", design.externalUrl));
|
|
277
|
+
return {
|
|
278
|
+
node: card,
|
|
279
|
+
size: LINK_SIZE
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
const frame = element("iframe", "histoire-designs-canvas-embed");
|
|
283
|
+
for (const [name, value] of Object.entries(iframeAttributes(design))) frame.setAttribute(name, value);
|
|
284
|
+
return {
|
|
285
|
+
node: frame,
|
|
286
|
+
size: EMBED_SIZE
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Lays an embedded document out at its native size and scales it into a box, so resizing an
|
|
291
|
+
* overlay zooms the embed instead of reflowing it. Without a box, the embed fills its parent.
|
|
292
|
+
* @param node Rendered design element; only iframes are scaled.
|
|
293
|
+
* @param native Native box size of the design.
|
|
294
|
+
* @param box Target size in canvas units.
|
|
295
|
+
*/
|
|
296
|
+
function scaleEmbed(node, native, box) {
|
|
297
|
+
if (!(node instanceof HTMLIFrameElement)) return;
|
|
298
|
+
if (!box) {
|
|
299
|
+
Object.assign(node.style, {
|
|
300
|
+
width: "",
|
|
301
|
+
height: "",
|
|
302
|
+
transform: "",
|
|
303
|
+
transformOrigin: ""
|
|
304
|
+
});
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
Object.assign(node.style, {
|
|
308
|
+
width: `${native.width}px`,
|
|
309
|
+
height: `${native.height}px`,
|
|
310
|
+
transform: `scale(${box.width / native.width}, ${box.height / native.height})`,
|
|
311
|
+
transformOrigin: "0 0"
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
// src/workbench/canvas/design-label.ts
|
|
315
|
+
/**
|
|
316
|
+
* Creates the design label. Switch buttons are reused across renders, only their text and
|
|
317
|
+
* pressed state change, so keyboard focus survives clicks and model notifications.
|
|
318
|
+
* @param onSelect Receives the entry index (`ResolvedDesign.index`) of a clicked design.
|
|
319
|
+
*/
|
|
320
|
+
function createDesignLabel(onSelect) {
|
|
321
|
+
const node = element("div", "histoire-designs-canvas-label");
|
|
322
|
+
const name = element("span", "");
|
|
323
|
+
const group = element("div", "histoire-designs-canvas-switches");
|
|
324
|
+
group.setAttribute("role", "group");
|
|
325
|
+
group.setAttribute("aria-label", "Design");
|
|
326
|
+
/** Switch buttons in display order; each reads its entry index from `data-index`. */
|
|
327
|
+
const buttons = [];
|
|
328
|
+
/**
|
|
329
|
+
* Creates a switch button; its entry index is read on click so the button can be reused.
|
|
330
|
+
*/
|
|
331
|
+
function createButton() {
|
|
332
|
+
const button = element("button", "histoire-designs-canvas-switch");
|
|
333
|
+
button.type = "button";
|
|
334
|
+
button.addEventListener("click", () => onSelect(Number(button.dataset.index)));
|
|
335
|
+
return button;
|
|
336
|
+
}
|
|
337
|
+
return {
|
|
338
|
+
node,
|
|
339
|
+
render(designs, activeIndex) {
|
|
340
|
+
if (designs.length <= 1) {
|
|
341
|
+
name.textContent = designs[0]?.name ?? "";
|
|
342
|
+
if (node.firstChild !== name) node.replaceChildren(name);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
while (buttons.length > designs.length) buttons.pop().remove();
|
|
346
|
+
while (buttons.length < designs.length) {
|
|
347
|
+
const button = createButton();
|
|
348
|
+
buttons.push(button);
|
|
349
|
+
group.append(button);
|
|
350
|
+
}
|
|
351
|
+
designs.forEach((design, position) => {
|
|
352
|
+
const button = buttons[position];
|
|
353
|
+
if (button.textContent !== design.name) button.textContent = design.name;
|
|
354
|
+
button.dataset.index = String(design.index);
|
|
355
|
+
button.setAttribute("aria-pressed", String(design.index === activeIndex));
|
|
356
|
+
});
|
|
357
|
+
if (node.firstChild !== group) node.replaceChildren(group);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
// src/workbench/canvas/DesignModeBar.vue?vue&type=script&setup=true&lang.ts
|
|
362
|
+
var _hoisted_1$3 = {
|
|
363
|
+
class: "histoire-designs histoire-designs-mode",
|
|
364
|
+
role: "toolbar",
|
|
365
|
+
"aria-label": "Design comparison"
|
|
366
|
+
};
|
|
367
|
+
// src/workbench/canvas/DesignModeBar.vue
|
|
368
|
+
var DesignModeBar_default = /* @__PURE__ */ defineComponent({
|
|
369
|
+
__name: "DesignModeBar",
|
|
370
|
+
props: { overlay: {} },
|
|
371
|
+
emits: ["reset"],
|
|
372
|
+
setup(__props, { emit: __emit }) {
|
|
373
|
+
const props = __props;
|
|
374
|
+
const emit = __emit;
|
|
375
|
+
/** Toggle options in display order. */
|
|
376
|
+
const options = [
|
|
377
|
+
{
|
|
378
|
+
label: "Side by side",
|
|
379
|
+
value: "side-by-side"
|
|
380
|
+
},
|
|
381
|
+
{
|
|
382
|
+
label: "Overlay",
|
|
383
|
+
value: "overlay"
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
label: "Split",
|
|
387
|
+
value: "split"
|
|
388
|
+
}
|
|
389
|
+
];
|
|
390
|
+
/** Bumped on every overlay state change. */
|
|
391
|
+
const revision = ref(0);
|
|
392
|
+
onScopeDispose(props.overlay.subscribe(() => revision.value++));
|
|
393
|
+
/** Current comparison mode. */
|
|
394
|
+
const mode = computed(() => {
|
|
395
|
+
revision.value;
|
|
396
|
+
return props.overlay.mode();
|
|
397
|
+
});
|
|
398
|
+
/** Overlay opacity in percent, as the slider shows it. */
|
|
399
|
+
const opacity = computed(() => {
|
|
400
|
+
revision.value;
|
|
401
|
+
return Math.round(props.overlay.opacity() * 100);
|
|
402
|
+
});
|
|
403
|
+
return (_ctx, _cache) => {
|
|
404
|
+
return openBlock(), createElementBlock("div", _hoisted_1$3, [
|
|
405
|
+
createVNode(unref(HstButtonGroup), {
|
|
406
|
+
class: "histoire-designs-mode-switch",
|
|
407
|
+
"aria-label": "Comparison mode",
|
|
408
|
+
classes: { option: "histoire-designs-mode-option" },
|
|
409
|
+
options,
|
|
410
|
+
"model-value": mode.value,
|
|
411
|
+
"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => __props.overlay.setMode($event))
|
|
412
|
+
}, null, 8, ["model-value"]),
|
|
413
|
+
mode.value === "overlay" ? (openBlock(), createBlock(unref(HstSlider), {
|
|
414
|
+
key: 0,
|
|
415
|
+
class: "histoire-designs-mode-opacity",
|
|
416
|
+
"aria-label": "Design opacity",
|
|
417
|
+
min: 10,
|
|
418
|
+
max: 100,
|
|
419
|
+
"model-value": opacity.value,
|
|
420
|
+
"onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => __props.overlay.setOpacity($event / 100))
|
|
421
|
+
}, null, 8, ["model-value"])) : createCommentVNode("", true),
|
|
422
|
+
mode.value !== "side-by-side" ? (openBlock(), createBlock(unref(HstButton), {
|
|
423
|
+
key: 1,
|
|
424
|
+
color: "flat",
|
|
425
|
+
"icon-only": "",
|
|
426
|
+
"aria-label": "Reset design placement",
|
|
427
|
+
title: "Reset design placement",
|
|
428
|
+
onClick: _cache[2] || (_cache[2] = ($event) => emit("reset"))
|
|
429
|
+
}, {
|
|
430
|
+
default: withCtx(() => [createVNode(unref(BuiltinIcon), {
|
|
431
|
+
icon: "reset",
|
|
432
|
+
size: 16
|
|
433
|
+
})]),
|
|
434
|
+
_: 1
|
|
435
|
+
})) : createCommentVNode("", true)
|
|
436
|
+
]);
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
// src/workbench/canvas/mode-bar.ts
|
|
441
|
+
/**
|
|
442
|
+
* Mounts the comparison bar in its own Vue app: the canvas view itself is a DOM view.
|
|
443
|
+
* @param overlay Comparison mode and opacity.
|
|
444
|
+
* @param onReset Restores the default placement of the shown design.
|
|
445
|
+
*/
|
|
446
|
+
function createModeBar(overlay, onReset) {
|
|
447
|
+
const node = element("div", "histoire-designs-canvas-bar");
|
|
448
|
+
const app = createApp({ render: () => h(DesignModeBar_default, {
|
|
449
|
+
overlay,
|
|
450
|
+
onReset
|
|
451
|
+
}) });
|
|
452
|
+
app.mount(node);
|
|
453
|
+
return {
|
|
454
|
+
node,
|
|
455
|
+
dispose: () => app.unmount()
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
// src/workbench/canvas/overlay-chrome.ts
|
|
459
|
+
/**
|
|
460
|
+
* Creates a handle element showing a bundled glyph.
|
|
461
|
+
* @param className Class attribute.
|
|
462
|
+
* @param label Accessible name.
|
|
463
|
+
* @param icon Bundled glyph name; omitted handles are drawn by CSS.
|
|
464
|
+
*/
|
|
465
|
+
function handle(className, label, icon) {
|
|
466
|
+
const node = element("div", className);
|
|
467
|
+
node.tabIndex = 0;
|
|
468
|
+
node.setAttribute("role", "button");
|
|
469
|
+
node.setAttribute("aria-label", label);
|
|
470
|
+
node.title = label;
|
|
471
|
+
const glyph = icon ? getHistoireIcon(icon) : void 0;
|
|
472
|
+
if (glyph) node.innerHTML = `<svg viewBox="0 0 ${glyph.width ?? 32} ${glyph.height ?? 32}" width="14" height="14" aria-hidden="true">${glyph.body}</svg>`;
|
|
473
|
+
return node;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Creates the overlay chrome. Pointer gestures capture the pointer, so moving over the story
|
|
477
|
+
* frame never interrupts them; arrow keys nudge (Shift for bigger steps).
|
|
478
|
+
* @param events View callbacks.
|
|
479
|
+
*/
|
|
480
|
+
function createOverlayChrome(events) {
|
|
481
|
+
const node = element("div", "histoire-designs-overlay-chrome");
|
|
482
|
+
const move = handle("histoire-designs-overlay-grip histoire-designs-overlay-move", "Move design", "move");
|
|
483
|
+
const resize = handle("histoire-designs-overlay-grip histoire-designs-overlay-resize", "Resize design");
|
|
484
|
+
const divider = element("div", "histoire-designs-overlay-divider");
|
|
485
|
+
const knob = handle("histoire-designs-overlay-grip histoire-designs-overlay-knob", "Split position", "arrows-horizontal");
|
|
486
|
+
knob.setAttribute("role", "slider");
|
|
487
|
+
knob.setAttribute("aria-orientation", "horizontal");
|
|
488
|
+
knob.setAttribute("aria-valuemin", "0");
|
|
489
|
+
knob.setAttribute("aria-valuemax", "100");
|
|
490
|
+
divider.append(knob);
|
|
491
|
+
node.append(move, resize, divider);
|
|
492
|
+
/**
|
|
493
|
+
* Makes a handle draggable and keyboard adjustable.
|
|
494
|
+
* @param target Handle element.
|
|
495
|
+
* @param apply Placement update.
|
|
496
|
+
* @param keyStep Canvas units of one arrow key press.
|
|
497
|
+
*/
|
|
498
|
+
function bind(target, apply, keyStep) {
|
|
499
|
+
target.addEventListener("pointerdown", (down) => {
|
|
500
|
+
const start = events.current();
|
|
501
|
+
if (down.button !== 0 || !start) return;
|
|
502
|
+
down.preventDefault();
|
|
503
|
+
target.setPointerCapture(down.pointerId);
|
|
504
|
+
let last = start;
|
|
505
|
+
const moveListener = (event) => {
|
|
506
|
+
const zoom = events.zoom();
|
|
507
|
+
last = apply(start, (event.clientX - down.clientX) / zoom, (event.clientY - down.clientY) / zoom, event.shiftKey);
|
|
508
|
+
events.onChange(last);
|
|
509
|
+
};
|
|
510
|
+
const end = () => {
|
|
511
|
+
target.removeEventListener("pointermove", moveListener);
|
|
512
|
+
target.removeEventListener("lostpointercapture", end);
|
|
513
|
+
events.onCommit(last);
|
|
514
|
+
};
|
|
515
|
+
target.addEventListener("pointermove", moveListener);
|
|
516
|
+
target.addEventListener("lostpointercapture", end);
|
|
517
|
+
});
|
|
518
|
+
target.addEventListener("keydown", (event) => {
|
|
519
|
+
const direction = {
|
|
520
|
+
ArrowLeft: [-1, 0],
|
|
521
|
+
ArrowRight: [1, 0],
|
|
522
|
+
ArrowUp: [0, -1],
|
|
523
|
+
ArrowDown: [0, 1]
|
|
524
|
+
}[event.key];
|
|
525
|
+
const start = events.current();
|
|
526
|
+
if (!direction || !start) return;
|
|
527
|
+
event.preventDefault();
|
|
528
|
+
const step = keyStep(start) * (event.shiftKey ? 10 : 1);
|
|
529
|
+
events.onCommit(apply(start, direction[0] * step, direction[1] * step, false));
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
bind(move, (start, dx, dy) => movePlacement(start, dx, dy), () => 1);
|
|
533
|
+
bind(resize, (start, dx, dy, alternate) => resizePlacement(start, dx, dy, !alternate), () => 1);
|
|
534
|
+
bind(knob, (start, dx) => setSplit(start, start.x + start.split * start.width + dx), (start) => start.width / 100);
|
|
535
|
+
return {
|
|
536
|
+
node,
|
|
537
|
+
render(placement, mode) {
|
|
538
|
+
Object.assign(node.style, {
|
|
539
|
+
left: `${placement.x}px`,
|
|
540
|
+
top: `${placement.y}px`,
|
|
541
|
+
width: `${placement.width}px`,
|
|
542
|
+
height: `${placement.height}px`
|
|
543
|
+
});
|
|
544
|
+
divider.hidden = mode !== "split";
|
|
545
|
+
divider.style.left = `${placement.split * 100}%`;
|
|
546
|
+
knob.setAttribute("aria-valuenow", String(Math.round(placement.split * 100)));
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
// src/designs/embed-url.ts
|
|
551
|
+
/** Figma document kinds that have an embeddable counterpart on `embed.figma.com`. */
|
|
552
|
+
var FIGMA_KINDS = /* @__PURE__ */ new Set([
|
|
553
|
+
"design",
|
|
554
|
+
"file",
|
|
555
|
+
"proto",
|
|
556
|
+
"board",
|
|
557
|
+
"slides"
|
|
558
|
+
]);
|
|
559
|
+
/**
|
|
560
|
+
* Figma Embed Kit 2.0: the same path served from `embed.figma.com`, identified by `embed-host`.
|
|
561
|
+
* Other `figma.com` pages (community, sites, file browser) refuse framing and have no embed
|
|
562
|
+
* counterpart, so they cannot be embedded.
|
|
563
|
+
* @param url Figma link.
|
|
564
|
+
* @param embedHost Value of `embed-host`; an existing one is kept.
|
|
565
|
+
*/
|
|
566
|
+
function figmaEmbedUrl(url, embedHost) {
|
|
567
|
+
const result = new URL(url);
|
|
568
|
+
const kind = url.pathname.split("/")[1];
|
|
569
|
+
if (/(?:^|\.)figma\.com$/.test(url.hostname) && url.hostname !== "embed.figma.com") {
|
|
570
|
+
if (!FIGMA_KINDS.has(kind)) return;
|
|
571
|
+
result.hostname = "embed.figma.com";
|
|
572
|
+
}
|
|
573
|
+
if (result.hostname === "embed.figma.com" && !result.searchParams.has("embed-host")) result.searchParams.set("embed-host", embedHost);
|
|
574
|
+
return result.href;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Sketch share links (`/s/<id>`) are embedded from `/embed/s/<id>`.
|
|
578
|
+
* @param url Sketch link.
|
|
579
|
+
*/
|
|
580
|
+
function sketchEmbedUrl(url) {
|
|
581
|
+
const result = new URL(url);
|
|
582
|
+
if (/(?:^|\.)sketch\.com$/.test(url.hostname) && url.pathname.startsWith("/s/")) result.pathname = `/embed${url.pathname}`;
|
|
583
|
+
return result.href;
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* URL loaded in the iframe of an embedded design, or `undefined` for a Figma page that cannot
|
|
587
|
+
* be embedded. Penpot view links and plain iframes embed as they are.
|
|
588
|
+
* @param type Embedded design type.
|
|
589
|
+
* @param url Validated design link.
|
|
590
|
+
* @param embedHost Figma `embed-host` value.
|
|
591
|
+
*/
|
|
592
|
+
function toEmbedUrl(type, url, embedHost) {
|
|
593
|
+
if (type === "figma") return figmaEmbedUrl(url, embedHost);
|
|
594
|
+
if (type === "sketch") return sketchEmbedUrl(url);
|
|
595
|
+
return url.href;
|
|
596
|
+
}
|
|
597
|
+
// src/designs/resolve.ts
|
|
598
|
+
/** Switcher label of each design tool. */
|
|
599
|
+
var TOOL_NAMES = {
|
|
600
|
+
figma: "Figma",
|
|
601
|
+
penpot: "Penpot",
|
|
602
|
+
sketch: "Sketch",
|
|
603
|
+
image: "Image"
|
|
604
|
+
};
|
|
605
|
+
/** Types rendered in an iframe; checked at runtime since `meta.design` is untyped user data. */
|
|
606
|
+
var EMBED_TYPES = /* @__PURE__ */ new Set([
|
|
607
|
+
"figma",
|
|
608
|
+
"penpot",
|
|
609
|
+
"sketch",
|
|
610
|
+
"iframe"
|
|
611
|
+
]);
|
|
612
|
+
/** Issue reported for `figma.com` pages without an embeddable counterpart. */
|
|
613
|
+
var FIGMA_NOT_EMBEDDABLE = "This Figma link cannot be embedded; use a design, prototype, board or slides link";
|
|
614
|
+
/**
|
|
615
|
+
* Raw `meta.design` of a variant: its own list replaces the story's.
|
|
616
|
+
* @param story Catalog story.
|
|
617
|
+
* @param variantId Variant id.
|
|
618
|
+
*/
|
|
619
|
+
function designMeta(story, variantId) {
|
|
620
|
+
return story.variants.find((item) => item.id === variantId)?.meta?.design ?? story.meta?.design;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Parses a design link. Only `http(s)` links are accepted; paths (images only) resolve
|
|
624
|
+
* against the book base, `/x` included, so books served under a sub-path keep working.
|
|
625
|
+
* @param value Raw URL.
|
|
626
|
+
* @param baseUrl Book base URL.
|
|
627
|
+
* @param allowPath Accept paths relative to the book.
|
|
628
|
+
*/
|
|
629
|
+
function parseUrl(value, baseUrl, allowPath) {
|
|
630
|
+
if (typeof value !== "string" || !value.trim()) return "Missing url";
|
|
631
|
+
const absolute = /^[a-z][a-z\d+.-]*:/i.test(value) || value.startsWith("//");
|
|
632
|
+
if (!absolute && !allowPath) return "Expected an http(s) url";
|
|
633
|
+
let url;
|
|
634
|
+
try {
|
|
635
|
+
url = absolute ? new URL(value, baseUrl) : new URL(value.replace(/^\/+/, ""), baseUrl);
|
|
636
|
+
} catch {
|
|
637
|
+
return "Invalid url";
|
|
638
|
+
}
|
|
639
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") return "Expected an http(s) url";
|
|
640
|
+
if (url.username || url.password) return "Links with credentials are not allowed";
|
|
641
|
+
return url;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Validates one entry of `meta.design`.
|
|
645
|
+
* @param entry Raw entry.
|
|
646
|
+
* @param index Position in the list.
|
|
647
|
+
* @param options Resolution settings.
|
|
648
|
+
*/
|
|
649
|
+
function resolveEntry(entry, index, options) {
|
|
650
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) return "Expected an object with type and url";
|
|
651
|
+
const input = entry;
|
|
652
|
+
const type = input.type;
|
|
653
|
+
if (typeof type !== "string" || !EMBED_TYPES.has(type) && type !== "image" && type !== "link") return `Unknown type ${JSON.stringify(type)}`;
|
|
654
|
+
const url = parseUrl(input.url, options.baseUrl, type === "image");
|
|
655
|
+
if (typeof url === "string") return url;
|
|
656
|
+
const label = typeof input.label === "string" && input.label ? input.label : void 0;
|
|
657
|
+
const name = typeof input.name === "string" && input.name ? input.name : (type === "link" ? label : void 0) ?? TOOL_NAMES[type] ?? url.hostname;
|
|
658
|
+
const base = {
|
|
659
|
+
index,
|
|
660
|
+
name,
|
|
661
|
+
externalUrl: url.href
|
|
662
|
+
};
|
|
663
|
+
if (type === "link") return {
|
|
664
|
+
...base,
|
|
665
|
+
type,
|
|
666
|
+
label: label ?? name
|
|
667
|
+
};
|
|
668
|
+
if (type === "image") {
|
|
669
|
+
const scale = input.scale;
|
|
670
|
+
if (scale !== void 0 && (typeof scale !== "number" || !(scale > 0) || !Number.isFinite(scale))) return "scale must be a number greater than 0";
|
|
671
|
+
const offset = input.offset;
|
|
672
|
+
if (offset !== void 0 && (!Array.isArray(offset) || offset.length !== 2 || offset.some((value) => typeof value !== "number" || !Number.isFinite(value)))) return "offset must be [x, y]";
|
|
673
|
+
return {
|
|
674
|
+
...base,
|
|
675
|
+
type,
|
|
676
|
+
src: url.href,
|
|
677
|
+
alt: typeof input.alt === "string" ? input.alt : name,
|
|
678
|
+
...scale === void 0 ? {} : { scale },
|
|
679
|
+
...offset ? { offset: [offset[0], offset[1]] } : {}
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
const src = toEmbedUrl(type, url, options.embedHost);
|
|
683
|
+
if (!src) return FIGMA_NOT_EMBEDDABLE;
|
|
684
|
+
return {
|
|
685
|
+
...base,
|
|
686
|
+
type,
|
|
687
|
+
src,
|
|
688
|
+
allowFullscreen: input.allowFullscreen === true
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* User-facing line of an invalid entry, shared by the inspector tab and canvas view.
|
|
693
|
+
* @param issue Invalid entry.
|
|
694
|
+
*/
|
|
695
|
+
function formatIssue(issue) {
|
|
696
|
+
return `Entry ${issue.index + 1}: ${issue.message}`;
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Designs declared for one variant through `meta.design` (single entry or list).
|
|
700
|
+
* @param story Catalog story.
|
|
701
|
+
* @param variantId Variant id.
|
|
702
|
+
* @param options Resolution settings.
|
|
703
|
+
*/
|
|
704
|
+
function resolveDesigns(story, variantId, options) {
|
|
705
|
+
const meta = designMeta(story, variantId);
|
|
706
|
+
const result = {
|
|
707
|
+
designs: [],
|
|
708
|
+
issues: []
|
|
709
|
+
};
|
|
710
|
+
if (meta === void 0 || meta === null) return result;
|
|
711
|
+
(Array.isArray(meta) ? meta : [meta]).forEach((entry, index) => {
|
|
712
|
+
const design = resolveEntry(entry, index, options);
|
|
713
|
+
if (typeof design === "string") result.issues.push({
|
|
714
|
+
index,
|
|
715
|
+
message: design
|
|
716
|
+
});
|
|
717
|
+
else result.designs.push(design);
|
|
718
|
+
});
|
|
719
|
+
return result;
|
|
720
|
+
}
|
|
721
|
+
// src/workbench/canvas/view-state.ts
|
|
722
|
+
/**
|
|
723
|
+
* Whether a lease failure only means the lease was released first (view switched, selection changed).
|
|
724
|
+
* @param error Rejection of `acquireFrame` or `ready`.
|
|
725
|
+
*/
|
|
726
|
+
function isReleased(error) {
|
|
727
|
+
return error?.name === "FrameLeaseError" && error.reason === "released";
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Full-view state replacing the variant and its design, or `null` when a design can be shown.
|
|
731
|
+
* @param target Selected variant.
|
|
732
|
+
* @param designs Its designs; `undefined` while loading.
|
|
733
|
+
* @param active Its active design.
|
|
734
|
+
*/
|
|
735
|
+
function viewState(target, designs, active) {
|
|
736
|
+
if (!target?.variantId) return {
|
|
737
|
+
kind: "empty",
|
|
738
|
+
icon: "catalog",
|
|
739
|
+
title: "No variant selected"
|
|
740
|
+
};
|
|
741
|
+
if (!designs) return {
|
|
742
|
+
kind: "loading",
|
|
743
|
+
title: "Loading designs"
|
|
744
|
+
};
|
|
745
|
+
if (designs.error !== void 0) return {
|
|
746
|
+
kind: "error",
|
|
747
|
+
title: "Could not load designs",
|
|
748
|
+
detail: designs.error
|
|
749
|
+
};
|
|
750
|
+
if (active) return null;
|
|
751
|
+
if (designs.issues.length) return {
|
|
752
|
+
kind: "error",
|
|
753
|
+
title: "Design unavailable",
|
|
754
|
+
detail: designs.issues.map(formatIssue).join("; ")
|
|
755
|
+
};
|
|
756
|
+
return {
|
|
757
|
+
kind: "empty",
|
|
758
|
+
icon: "color-palette",
|
|
759
|
+
title: "No design",
|
|
760
|
+
detail: "Declare meta.design on the story or variant"
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Error shown in the design box when an image fails to load.
|
|
765
|
+
* @param design Image design.
|
|
766
|
+
*/
|
|
767
|
+
function imageError(design) {
|
|
768
|
+
const node = element("div", "histoire-designs-canvas-error");
|
|
769
|
+
node.setAttribute("role", "alert");
|
|
770
|
+
node.append(element("span", "", `Could not load ${design.name}`), element("span", "histoire-designs-canvas-url", design.externalUrl));
|
|
771
|
+
return node;
|
|
772
|
+
}
|
|
773
|
+
// src/workbench/canvas/side-by-side-view.ts
|
|
774
|
+
/** Variant frame size in canvas units; fixed, so the leased frame box never goes stale. */
|
|
775
|
+
var FRAME = {
|
|
776
|
+
width: 420,
|
|
777
|
+
height: 720
|
|
778
|
+
};
|
|
779
|
+
/** Height of the column labels (variant title, design name or switch) in canvas units. */
|
|
780
|
+
var LABEL_HEIGHT = 32;
|
|
781
|
+
/** Horizontal space between the variant column and the design column in canvas units. */
|
|
782
|
+
var GAP = 48;
|
|
783
|
+
/** Leased frame box in canvas space; overlays start on it. */
|
|
784
|
+
var FRAME_BOX = {
|
|
785
|
+
x: 0,
|
|
786
|
+
y: LABEL_HEIGHT,
|
|
787
|
+
...FRAME
|
|
788
|
+
};
|
|
789
|
+
/** Variant column box (label included) in canvas space. */
|
|
790
|
+
var FRAME_COLUMN = {
|
|
791
|
+
x: 0,
|
|
792
|
+
y: 0,
|
|
793
|
+
width: FRAME.width,
|
|
794
|
+
height: LABEL_HEIGHT + FRAME.height
|
|
795
|
+
};
|
|
796
|
+
/**
|
|
797
|
+
* Canvas-space box (label included) of the design column beside the variant.
|
|
798
|
+
* @param design Design box size.
|
|
799
|
+
*/
|
|
800
|
+
function sideColumn(design) {
|
|
801
|
+
return {
|
|
802
|
+
x: FRAME.width + GAP,
|
|
803
|
+
y: 0,
|
|
804
|
+
width: design.width,
|
|
805
|
+
height: LABEL_HEIGHT + design.height
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Applies a canvas-space box to an absolutely placed element.
|
|
810
|
+
* @param node Element.
|
|
811
|
+
* @param rect Box in canvas units.
|
|
812
|
+
*/
|
|
813
|
+
function placeBox(node, rect) {
|
|
814
|
+
Object.assign(node.style, {
|
|
815
|
+
left: `${rect.x}px`,
|
|
816
|
+
top: `${rect.y}px`,
|
|
817
|
+
width: `${rect.width}px`,
|
|
818
|
+
height: `${rect.height}px`
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Renders the selected variant in a leased frame with its active design beside it, or laid
|
|
823
|
+
* over it (Overlay and Split modes).
|
|
824
|
+
* @param container Host-owned view container.
|
|
825
|
+
* @param initial First canvas-scoped context.
|
|
826
|
+
* @param host View host services.
|
|
827
|
+
* @param model Design model.
|
|
828
|
+
* @param overlay Comparison mode and design placements.
|
|
829
|
+
* @param onLayout Receives the content boxes, read by the view's `layout` for Fit.
|
|
830
|
+
*/
|
|
831
|
+
function mountSideBySide(container, initial, host, model, overlay, onLayout) {
|
|
832
|
+
let context = initial;
|
|
833
|
+
const layer = element("div", "histoire-designs-canvas-layer");
|
|
834
|
+
const state = element("div", "histoire-designs-canvas-state");
|
|
835
|
+
const root = element("div", "histoire-designs histoire-designs-canvas");
|
|
836
|
+
root.setAttribute("data-histoire-canvas-pan", "");
|
|
837
|
+
root.append(layer, state);
|
|
838
|
+
container.append(root);
|
|
839
|
+
const frameColumn = element("div", "histoire-designs-canvas-column");
|
|
840
|
+
const frameLabel = element("div", "histoire-designs-canvas-label");
|
|
841
|
+
const frameTitle = element("span", "histoire-designs-canvas-title");
|
|
842
|
+
frameLabel.append(frameTitle);
|
|
843
|
+
const frameBox = element("div", "histoire-designs-canvas-box");
|
|
844
|
+
Object.assign(frameBox.style, {
|
|
845
|
+
width: `${FRAME.width}px`,
|
|
846
|
+
height: `${FRAME.height}px`
|
|
847
|
+
});
|
|
848
|
+
frameColumn.append(frameLabel, frameBox);
|
|
849
|
+
placeBox(frameColumn, FRAME_COLUMN);
|
|
850
|
+
const designColumn = element("div", "histoire-designs-canvas-column histoire-designs-canvas-design");
|
|
851
|
+
const designLabel = createDesignLabel((entryIndex) => {
|
|
852
|
+
const target = context.selection.target;
|
|
853
|
+
if (target?.variantId) model.select(target, entryIndex);
|
|
854
|
+
});
|
|
855
|
+
const designBox = element("div", "histoire-designs-canvas-box");
|
|
856
|
+
designColumn.append(designLabel.node, designBox);
|
|
857
|
+
let lease;
|
|
858
|
+
let targetKey;
|
|
859
|
+
let designKey;
|
|
860
|
+
/** Selected variant and entry index of the shown design; placements are stored under them. */
|
|
861
|
+
let shown;
|
|
862
|
+
/** Design box size reported by the design element. */
|
|
863
|
+
let size;
|
|
864
|
+
/** Placement while a grip gesture runs; stored only once it ends. */
|
|
865
|
+
let live;
|
|
866
|
+
/** Set on dispose; late image loads and notifications must not touch a retired view. */
|
|
867
|
+
let disposed = false;
|
|
868
|
+
const bar = createModeBar(overlay, () => {
|
|
869
|
+
if (shown) overlay.reset(shown.target, shown.index);
|
|
870
|
+
});
|
|
871
|
+
bar.node.hidden = true;
|
|
872
|
+
root.insertBefore(bar.node, state);
|
|
873
|
+
/** Placement of the shown design in Overlay and Split modes. */
|
|
874
|
+
const placement = () => {
|
|
875
|
+
if (!shown || !size) return;
|
|
876
|
+
return live ?? overlay.placement(shown.target, shown.index) ?? defaultPlacement(FRAME_BOX, size);
|
|
877
|
+
};
|
|
878
|
+
const chrome = createOverlayChrome({
|
|
879
|
+
zoom: () => context.canvas.zoom,
|
|
880
|
+
current: placement,
|
|
881
|
+
onChange: (next) => {
|
|
882
|
+
live = next;
|
|
883
|
+
arrange(false);
|
|
884
|
+
},
|
|
885
|
+
onCommit: (next) => {
|
|
886
|
+
live = void 0;
|
|
887
|
+
if (shown) overlay.setPlacement(shown.target, shown.index, next);
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
/** Follows pan and zoom like built-in arrangements; grips keep their pixel size. */
|
|
891
|
+
const place = () => {
|
|
892
|
+
const { pan, zoom } = context.canvas;
|
|
893
|
+
layer.style.transform = `translate(${pan.x}px, ${pan.y}px) scale(${zoom})`;
|
|
894
|
+
layer.style.setProperty("--histoire-designs-zoom", String(zoom));
|
|
895
|
+
};
|
|
896
|
+
/**
|
|
897
|
+
* Places the design for the current mode and reports the content boxes.
|
|
898
|
+
* @param refit Ask the canvas to re-read `layout` now; gestures skip it so Fit never moves the camera mid-drag.
|
|
899
|
+
*/
|
|
900
|
+
function arrange(refit) {
|
|
901
|
+
if (disposed || !size) return;
|
|
902
|
+
const mode = overlay.mode();
|
|
903
|
+
root.dataset.mode = mode;
|
|
904
|
+
const box = mode === "side-by-side" ? void 0 : placement();
|
|
905
|
+
if (!box) {
|
|
906
|
+
if (designColumn.firstChild !== designLabel.node) designColumn.prepend(designLabel.node);
|
|
907
|
+
chrome.node.remove();
|
|
908
|
+
const column = sideColumn(size);
|
|
909
|
+
placeBox(designColumn, column);
|
|
910
|
+
Object.assign(designColumn.style, {
|
|
911
|
+
opacity: "",
|
|
912
|
+
clipPath: ""
|
|
913
|
+
});
|
|
914
|
+
scaleEmbed(designBox.firstElementChild, size);
|
|
915
|
+
onLayout([FRAME_COLUMN, column]);
|
|
916
|
+
} else {
|
|
917
|
+
if (designLabel.node.parentNode !== frameLabel) frameLabel.append(designLabel.node);
|
|
918
|
+
if (chrome.node.parentNode !== layer) layer.append(chrome.node);
|
|
919
|
+
placeBox(designColumn, box);
|
|
920
|
+
scaleEmbed(designBox.firstElementChild, size, box);
|
|
921
|
+
designColumn.style.opacity = mode === "overlay" ? String(overlay.opacity()) : "";
|
|
922
|
+
designColumn.style.clipPath = mode === "split" ? `inset(0 0 0 ${box.split * 100}%)` : "";
|
|
923
|
+
chrome.render(box, mode);
|
|
924
|
+
onLayout([overlayBounds(FRAME_COLUMN, box)]);
|
|
925
|
+
}
|
|
926
|
+
if (refit) context.canvas.requestLayout();
|
|
927
|
+
}
|
|
928
|
+
/**
|
|
929
|
+
* Leases one frame for the selected variant, replacing the previous lease.
|
|
930
|
+
* @param target Selected variant.
|
|
931
|
+
*/
|
|
932
|
+
function leaseFrame(target) {
|
|
933
|
+
lease?.abort();
|
|
934
|
+
const own = lease = new AbortController();
|
|
935
|
+
host.signal.addEventListener("abort", () => own.abort(), {
|
|
936
|
+
once: true,
|
|
937
|
+
signal: own.signal
|
|
938
|
+
});
|
|
939
|
+
frameBox.replaceChildren();
|
|
940
|
+
context.canvas.acquireFrame(target, {
|
|
941
|
+
container: frameBox,
|
|
942
|
+
rect: FRAME_BOX,
|
|
943
|
+
queue: true,
|
|
944
|
+
signal: own.signal
|
|
945
|
+
}).then((granted) => granted.ready).catch((error) => {
|
|
946
|
+
if (!isReleased(error) && !own.signal.aborted) host.reportError(error);
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
/** Re-renders after a selection, catalog or design switch change. */
|
|
950
|
+
function render() {
|
|
951
|
+
if (disposed) return;
|
|
952
|
+
const target = context.selection.target;
|
|
953
|
+
const designs = target ? model.get(target) : void 0;
|
|
954
|
+
const active = target ? model.active(target) : void 0;
|
|
955
|
+
const empty = viewState(target, designs, active);
|
|
956
|
+
host.renderState(state, empty);
|
|
957
|
+
bar.node.hidden = Boolean(empty);
|
|
958
|
+
if (empty || !target || !designs || !active) {
|
|
959
|
+
lease?.abort();
|
|
960
|
+
targetKey = designKey = shown = size = live = void 0;
|
|
961
|
+
layer.replaceChildren();
|
|
962
|
+
onLayout([]);
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
if (!layer.childElementCount) layer.append(frameColumn, designColumn);
|
|
966
|
+
const story = `${target.storyId}\0${target.variantId}`;
|
|
967
|
+
const moved = !shown || story !== `${shown.target.storyId}\0${shown.target.variantId}` || active.index !== shown.index;
|
|
968
|
+
shown = {
|
|
969
|
+
target,
|
|
970
|
+
index: active.index
|
|
971
|
+
};
|
|
972
|
+
if (moved) live = void 0;
|
|
973
|
+
const nextDesign = `${active.index}\0${active.externalUrl}`;
|
|
974
|
+
if (nextDesign !== designKey) {
|
|
975
|
+
designKey = nextDesign;
|
|
976
|
+
const current = () => !disposed && designKey === nextDesign;
|
|
977
|
+
const rendered = createDesignElement(active, {
|
|
978
|
+
onSize: (next) => {
|
|
979
|
+
if (!current()) return;
|
|
980
|
+
size = next;
|
|
981
|
+
arrange(true);
|
|
982
|
+
},
|
|
983
|
+
onError: () => {
|
|
984
|
+
if (current()) designBox.replaceChildren(imageError(active));
|
|
985
|
+
}
|
|
986
|
+
});
|
|
987
|
+
designBox.replaceChildren(rendered.node);
|
|
988
|
+
size = rendered.size;
|
|
989
|
+
arrange(true);
|
|
990
|
+
} else if (moved) arrange(true);
|
|
991
|
+
designLabel.render(designs.designs, active.index);
|
|
992
|
+
frameTitle.textContent = model.variant(target)?.title ?? target.variantId;
|
|
993
|
+
if (story !== targetKey) {
|
|
994
|
+
targetKey = story;
|
|
995
|
+
leaseFrame(target);
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* Mode, opacity or placement changed: only the arrangement moves, the frame stays leased.
|
|
1000
|
+
* Fit re-frames on mode switches only, never after a grip gesture.
|
|
1001
|
+
*/
|
|
1002
|
+
const rearrange = () => arrange(overlay.mode() !== root.dataset.mode);
|
|
1003
|
+
const stops = [
|
|
1004
|
+
context.canvas.subscribe(place),
|
|
1005
|
+
model.subscribe(render),
|
|
1006
|
+
overlay.subscribe(rearrange),
|
|
1007
|
+
bar.dispose
|
|
1008
|
+
];
|
|
1009
|
+
let stopContext = context.subscribe(render);
|
|
1010
|
+
place();
|
|
1011
|
+
render();
|
|
1012
|
+
return {
|
|
1013
|
+
update(next) {
|
|
1014
|
+
if (next !== context) {
|
|
1015
|
+
stopContext();
|
|
1016
|
+
stopContext = next.subscribe(render);
|
|
1017
|
+
}
|
|
1018
|
+
context = next;
|
|
1019
|
+
place();
|
|
1020
|
+
render();
|
|
1021
|
+
},
|
|
1022
|
+
dispose() {
|
|
1023
|
+
disposed = true;
|
|
1024
|
+
lease?.abort();
|
|
1025
|
+
host.renderState(state, null);
|
|
1026
|
+
stopContext();
|
|
1027
|
+
stops.forEach((stop) => stop());
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* `canvas.view` comparing the selected variant with its design: side by side, or laid over it
|
|
1033
|
+
* with transparency or a split divider. DOM view: geometry-heavy code stays framework-free.
|
|
1034
|
+
* @param model Design model.
|
|
1035
|
+
* @param overlay Comparison mode and design placements.
|
|
1036
|
+
*/
|
|
1037
|
+
function sideBySideView(model, overlay) {
|
|
1038
|
+
/** Content boxes of the latest mount. */
|
|
1039
|
+
let rects = [];
|
|
1040
|
+
/** Latest mount; earlier mounts lost the right to write `rects` (remount before dispose). */
|
|
1041
|
+
let owner;
|
|
1042
|
+
return {
|
|
1043
|
+
layout: () => rects,
|
|
1044
|
+
toolbar: [
|
|
1045
|
+
"search",
|
|
1046
|
+
"tools",
|
|
1047
|
+
"zoom",
|
|
1048
|
+
"background",
|
|
1049
|
+
"ui",
|
|
1050
|
+
"plugins"
|
|
1051
|
+
],
|
|
1052
|
+
mount: (container, context, host) => {
|
|
1053
|
+
const token = owner = {};
|
|
1054
|
+
const handle = mountSideBySide(container, context, host, model, overlay, (next) => {
|
|
1055
|
+
if (owner === token) rects = next;
|
|
1056
|
+
});
|
|
1057
|
+
return {
|
|
1058
|
+
update: handle.update,
|
|
1059
|
+
dispose() {
|
|
1060
|
+
handle.dispose();
|
|
1061
|
+
if (owner !== token) return;
|
|
1062
|
+
owner = void 0;
|
|
1063
|
+
rects = [];
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
// src/workbench/EmbedFrame.vue?vue&type=script&setup=true&lang.ts
|
|
1070
|
+
var _hoisted_1$2 = { class: "histoire-designs-embed" };
|
|
1071
|
+
// src/workbench/EmbedFrame.vue
|
|
1072
|
+
var EmbedFrame_default = /* @__PURE__ */ defineComponent({
|
|
1073
|
+
__name: "EmbedFrame",
|
|
1074
|
+
props: { design: {} },
|
|
1075
|
+
setup(__props) {
|
|
1076
|
+
const props = __props;
|
|
1077
|
+
/** The current iframe document finished loading; hides the loading overlay. */
|
|
1078
|
+
const loaded = ref(false);
|
|
1079
|
+
watch(() => props.design.src, () => {
|
|
1080
|
+
loaded.value = false;
|
|
1081
|
+
});
|
|
1082
|
+
/** Sandbox, permissions and source of the iframe. */
|
|
1083
|
+
const attributes = computed(() => iframeAttributes(props.design));
|
|
1084
|
+
return (_ctx, _cache) => {
|
|
1085
|
+
return openBlock(), createElementBlock("div", _hoisted_1$2, [(openBlock(), createElementBlock("iframe", mergeProps({ key: __props.design.src }, attributes.value, { onLoad: _cache[0] || (_cache[0] = ($event) => loaded.value = true) }), null, 16)), !loaded.value ? (openBlock(), createBlock(unref(WorkbenchLoadingState), {
|
|
1086
|
+
key: 0,
|
|
1087
|
+
class: "histoire-designs-overlay",
|
|
1088
|
+
title: `Loading ${__props.design.name}`
|
|
1089
|
+
}, null, 8, ["title"])) : createCommentVNode("", true)]);
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
});
|
|
1093
|
+
// src/workbench/image/pan-zoom.ts
|
|
1094
|
+
/** Smallest zoom factor. */
|
|
1095
|
+
var MIN_SCALE = .05;
|
|
1096
|
+
/** Zoom multiplier of one zoom button press or `+` key. */
|
|
1097
|
+
var ZOOM_STEP = 1.25;
|
|
1098
|
+
/** Keyboard pan distance in pixels. */
|
|
1099
|
+
var PAN_STEP = 40;
|
|
1100
|
+
/** Keyboard pan distance with Shift held. */
|
|
1101
|
+
var PAN_STEP_LARGE = 200;
|
|
1102
|
+
/**
|
|
1103
|
+
* Places the image at `scale`, centered in the container, then shifted by `offset`.
|
|
1104
|
+
* @param container Viewer size.
|
|
1105
|
+
* @param image Natural image size.
|
|
1106
|
+
* @param scale Zoom factor.
|
|
1107
|
+
* @param offset Shift from the centered position; positive moves right and down.
|
|
1108
|
+
*/
|
|
1109
|
+
function centeredView(container, image, scale, offset = [0, 0]) {
|
|
1110
|
+
return {
|
|
1111
|
+
scale,
|
|
1112
|
+
x: (container.width - image.width * scale) / 2 + offset[0],
|
|
1113
|
+
y: (container.height - image.height * scale) / 2 + offset[1]
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Shows the whole image centered, then shifted by `offset`; small images keep their natural size.
|
|
1118
|
+
* @param container Viewer size.
|
|
1119
|
+
* @param image Natural image size.
|
|
1120
|
+
* @param offset Shift from the centered position; positive moves right and down.
|
|
1121
|
+
*/
|
|
1122
|
+
function fitView(container, image, offset) {
|
|
1123
|
+
const scale = Math.min(1, container.width / image.width, container.height / image.height);
|
|
1124
|
+
return centeredView(container, image, scale > 0 && Number.isFinite(scale) ? scale : 1, offset);
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Zooms by `factor` around `point` (container pixels): the image pixel under it stays put.
|
|
1128
|
+
* @param view Current placement.
|
|
1129
|
+
* @param factor Zoom multiplier.
|
|
1130
|
+
* @param point Zoom origin in container pixels.
|
|
1131
|
+
* @param point.x Horizontal position.
|
|
1132
|
+
* @param point.y Vertical position.
|
|
1133
|
+
*/
|
|
1134
|
+
function zoomAt(view, factor, point) {
|
|
1135
|
+
const scale = Math.min(16, Math.max(MIN_SCALE, view.scale * factor));
|
|
1136
|
+
const ratio = scale / view.scale;
|
|
1137
|
+
return {
|
|
1138
|
+
scale,
|
|
1139
|
+
x: point.x - (point.x - view.x) * ratio,
|
|
1140
|
+
y: point.y - (point.y - view.y) * ratio
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
/** Image shift per arrow key; arrows reveal the side they point to, so the image moves the other way. */
|
|
1144
|
+
var PAN_DIRECTIONS = {
|
|
1145
|
+
ArrowLeft: [1, 0],
|
|
1146
|
+
ArrowRight: [-1, 0],
|
|
1147
|
+
ArrowUp: [0, 1],
|
|
1148
|
+
ArrowDown: [0, -1]
|
|
1149
|
+
};
|
|
1150
|
+
/**
|
|
1151
|
+
* Maps a key press in the image viewer to a view change: arrows pan (Shift for a bigger step),
|
|
1152
|
+
* `+`/`=` and `-`/`_` zoom, `0` fits. Presses with Ctrl, Meta or Alt are left to the browser.
|
|
1153
|
+
* @param event Key press.
|
|
1154
|
+
* @param event.key Pressed key value.
|
|
1155
|
+
* @param event.shiftKey Shift held.
|
|
1156
|
+
* @param event.ctrlKey Ctrl held.
|
|
1157
|
+
* @param event.metaKey Meta held.
|
|
1158
|
+
* @param event.altKey Alt held.
|
|
1159
|
+
*/
|
|
1160
|
+
function keyCommand(event) {
|
|
1161
|
+
if (event.ctrlKey || event.metaKey || event.altKey) return;
|
|
1162
|
+
const direction = PAN_DIRECTIONS[event.key];
|
|
1163
|
+
if (direction) {
|
|
1164
|
+
const step = event.shiftKey ? PAN_STEP_LARGE : PAN_STEP;
|
|
1165
|
+
return { pan: [direction[0] * step + 0, direction[1] * step + 0] };
|
|
1166
|
+
}
|
|
1167
|
+
if (event.key === "+" || event.key === "=") return { zoom: ZOOM_STEP };
|
|
1168
|
+
if (event.key === "-" || event.key === "_") return { zoom: 1 / ZOOM_STEP };
|
|
1169
|
+
if (event.key === "0") return "fit";
|
|
1170
|
+
}
|
|
1171
|
+
// src/workbench/image/ImageViewer.vue?vue&type=script&setup=true&lang.ts
|
|
1172
|
+
var _hoisted_1$1 = { class: "histoire-designs-image" };
|
|
1173
|
+
var _hoisted_2$1 = {
|
|
1174
|
+
class: "histoire-designs-image-tools",
|
|
1175
|
+
role: "group",
|
|
1176
|
+
"aria-label": "Zoom"
|
|
1177
|
+
};
|
|
1178
|
+
var _hoisted_3$1 = { class: "histoire-designs-zoom" };
|
|
1179
|
+
var _hoisted_4$1 = ["aria-label", "data-panning"];
|
|
1180
|
+
var _hoisted_5 = ["src", "alt"];
|
|
1181
|
+
// src/workbench/image/ImageViewer.vue
|
|
1182
|
+
var ImageViewer_default = /* @__PURE__ */ defineComponent({
|
|
1183
|
+
__name: "ImageViewer",
|
|
1184
|
+
props: { design: {} },
|
|
1185
|
+
setup(__props) {
|
|
1186
|
+
const props = __props;
|
|
1187
|
+
/** Element the image is placed in; receives pointer, wheel and keyboard input. */
|
|
1188
|
+
const viewport = ref();
|
|
1189
|
+
/** Current zoom and position of the image. */
|
|
1190
|
+
const view = shallowRef({
|
|
1191
|
+
scale: 1,
|
|
1192
|
+
x: 0,
|
|
1193
|
+
y: 0
|
|
1194
|
+
});
|
|
1195
|
+
/** Natural size of the decoded image; `undefined` until it loads. */
|
|
1196
|
+
const natural = shallowRef();
|
|
1197
|
+
/** Image loading state. */
|
|
1198
|
+
const status = ref("loading");
|
|
1199
|
+
/**
|
|
1200
|
+
* Set while the image is fitted, holding the offset applied on top of the fit, so the
|
|
1201
|
+
* viewer refits on resize; cleared once the user zooms or pans away.
|
|
1202
|
+
*/
|
|
1203
|
+
let fitted;
|
|
1204
|
+
watch(() => props.design.src, () => {
|
|
1205
|
+
status.value = "loading";
|
|
1206
|
+
natural.value = void 0;
|
|
1207
|
+
});
|
|
1208
|
+
watch([
|
|
1209
|
+
() => props.design.scale,
|
|
1210
|
+
() => props.design.offset?.[0],
|
|
1211
|
+
() => props.design.offset?.[1]
|
|
1212
|
+
], () => {
|
|
1213
|
+
if (status.value === "ready") reset();
|
|
1214
|
+
});
|
|
1215
|
+
/** Current viewer size. */
|
|
1216
|
+
function viewportSize() {
|
|
1217
|
+
return {
|
|
1218
|
+
width: viewport.value?.clientWidth ?? 0,
|
|
1219
|
+
height: viewport.value?.clientHeight ?? 0
|
|
1220
|
+
};
|
|
1221
|
+
}
|
|
1222
|
+
/**
|
|
1223
|
+
* Shows the whole image, optionally shifted.
|
|
1224
|
+
* @param offset Shift from the fitted position.
|
|
1225
|
+
*/
|
|
1226
|
+
function fit(offset) {
|
|
1227
|
+
if (!natural.value) return;
|
|
1228
|
+
view.value = fitView(viewportSize(), natural.value, offset);
|
|
1229
|
+
fitted = { offset };
|
|
1230
|
+
}
|
|
1231
|
+
/**
|
|
1232
|
+
* Centers the image at a zoom factor, optionally shifted.
|
|
1233
|
+
* @param scale Zoom factor.
|
|
1234
|
+
* @param offset Shift from the centered position.
|
|
1235
|
+
*/
|
|
1236
|
+
function center(scale, offset) {
|
|
1237
|
+
if (!natural.value) return;
|
|
1238
|
+
view.value = centeredView(viewportSize(), natural.value, scale, offset);
|
|
1239
|
+
fitted = void 0;
|
|
1240
|
+
}
|
|
1241
|
+
/** Initial placement: declared scale, otherwise fit, then shifted by the declared offset. */
|
|
1242
|
+
function reset() {
|
|
1243
|
+
if (props.design.scale) center(props.design.scale, props.design.offset);
|
|
1244
|
+
else fit(props.design.offset);
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1247
|
+
* Stores the natural size once the image decoded.
|
|
1248
|
+
* @param event Image load event.
|
|
1249
|
+
*/
|
|
1250
|
+
function onLoad(event) {
|
|
1251
|
+
const image = event.target;
|
|
1252
|
+
natural.value = {
|
|
1253
|
+
width: image.naturalWidth,
|
|
1254
|
+
height: image.naturalHeight
|
|
1255
|
+
};
|
|
1256
|
+
status.value = "ready";
|
|
1257
|
+
reset();
|
|
1258
|
+
}
|
|
1259
|
+
/**
|
|
1260
|
+
* Zooms around the viewer center (toolbar buttons and keyboard).
|
|
1261
|
+
* @param factor Zoom multiplier.
|
|
1262
|
+
*/
|
|
1263
|
+
function zoomCenter(factor) {
|
|
1264
|
+
const size = viewportSize();
|
|
1265
|
+
view.value = zoomAt(view.value, factor, {
|
|
1266
|
+
x: size.width / 2,
|
|
1267
|
+
y: size.height / 2
|
|
1268
|
+
});
|
|
1269
|
+
fitted = void 0;
|
|
1270
|
+
}
|
|
1271
|
+
/**
|
|
1272
|
+
* Moves the image.
|
|
1273
|
+
* @param dx Horizontal shift in pixels.
|
|
1274
|
+
* @param dy Vertical shift in pixels.
|
|
1275
|
+
*/
|
|
1276
|
+
function panBy(dx, dy) {
|
|
1277
|
+
view.value = {
|
|
1278
|
+
...view.value,
|
|
1279
|
+
x: view.value.x + dx,
|
|
1280
|
+
y: view.value.y + dy
|
|
1281
|
+
};
|
|
1282
|
+
fitted = void 0;
|
|
1283
|
+
}
|
|
1284
|
+
/**
|
|
1285
|
+
* Wheel zoom around the cursor; trackpad pinch arrives as a ctrl wheel with small deltas.
|
|
1286
|
+
* @param event Wheel event.
|
|
1287
|
+
*/
|
|
1288
|
+
function onWheel(event) {
|
|
1289
|
+
const box = viewport.value.getBoundingClientRect();
|
|
1290
|
+
view.value = zoomAt(view.value, Math.exp(-event.deltaY * (event.ctrlKey ? .01 : .002)), {
|
|
1291
|
+
x: event.clientX - box.left,
|
|
1292
|
+
y: event.clientY - box.top
|
|
1293
|
+
});
|
|
1294
|
+
fitted = void 0;
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Keyboard pan and zoom: arrows pan, `+`/`-` zoom, `0` fits.
|
|
1298
|
+
* @param event Key event.
|
|
1299
|
+
*/
|
|
1300
|
+
function onKeyDown(event) {
|
|
1301
|
+
if (status.value !== "ready") return;
|
|
1302
|
+
const command = keyCommand(event);
|
|
1303
|
+
if (!command) return;
|
|
1304
|
+
event.preventDefault();
|
|
1305
|
+
if (command === "fit") fit();
|
|
1306
|
+
else if ("zoom" in command) zoomCenter(command.zoom);
|
|
1307
|
+
else panBy(...command.pan);
|
|
1308
|
+
}
|
|
1309
|
+
/** Active pointer drag: pointer id and its last position. */
|
|
1310
|
+
let drag;
|
|
1311
|
+
/** A drag is in progress; switches the cursor. */
|
|
1312
|
+
const panning = ref(false);
|
|
1313
|
+
/**
|
|
1314
|
+
* Starts panning with the primary button.
|
|
1315
|
+
* @param event Pointer event.
|
|
1316
|
+
*/
|
|
1317
|
+
function onPointerDown(event) {
|
|
1318
|
+
if (event.button !== 0) return;
|
|
1319
|
+
drag = {
|
|
1320
|
+
pointerId: event.pointerId,
|
|
1321
|
+
x: event.clientX,
|
|
1322
|
+
y: event.clientY
|
|
1323
|
+
};
|
|
1324
|
+
panning.value = true;
|
|
1325
|
+
viewport.value.setPointerCapture(event.pointerId);
|
|
1326
|
+
}
|
|
1327
|
+
/**
|
|
1328
|
+
* Moves the image with the pointer while panning.
|
|
1329
|
+
* @param event Pointer event.
|
|
1330
|
+
*/
|
|
1331
|
+
function onPointerMove(event) {
|
|
1332
|
+
if (drag?.pointerId !== event.pointerId) return;
|
|
1333
|
+
panBy(event.clientX - drag.x, event.clientY - drag.y);
|
|
1334
|
+
drag = {
|
|
1335
|
+
...drag,
|
|
1336
|
+
x: event.clientX,
|
|
1337
|
+
y: event.clientY
|
|
1338
|
+
};
|
|
1339
|
+
}
|
|
1340
|
+
/** Ends panning. */
|
|
1341
|
+
function onPointerUp() {
|
|
1342
|
+
drag = void 0;
|
|
1343
|
+
panning.value = false;
|
|
1344
|
+
}
|
|
1345
|
+
/** CSS transform placing the image. */
|
|
1346
|
+
const transform = computed(() => `translate(${view.value.x}px, ${view.value.y}px) scale(${view.value.scale})`);
|
|
1347
|
+
/** Zoom factor shown in the toolbar. */
|
|
1348
|
+
const percent = computed(() => `${Math.round(view.value.scale * 100)}%`);
|
|
1349
|
+
/** Keeps the image fitted while the inspector is resized, unless the user zoomed or panned away. */
|
|
1350
|
+
const observer = new ResizeObserver(() => {
|
|
1351
|
+
if (status.value === "ready" && fitted) fit(fitted.offset);
|
|
1352
|
+
});
|
|
1353
|
+
watch(viewport, (element, previous) => {
|
|
1354
|
+
if (previous) observer.unobserve(previous);
|
|
1355
|
+
if (element) observer.observe(element);
|
|
1356
|
+
});
|
|
1357
|
+
onScopeDispose(() => observer.disconnect());
|
|
1358
|
+
return (_ctx, _cache) => {
|
|
1359
|
+
return openBlock(), createElementBlock("div", _hoisted_1$1, [createElementVNode("div", _hoisted_2$1, [
|
|
1360
|
+
createVNode(unref(HstButton), {
|
|
1361
|
+
color: "flat",
|
|
1362
|
+
"icon-only": "",
|
|
1363
|
+
"aria-label": "Zoom out",
|
|
1364
|
+
title: "Zoom out",
|
|
1365
|
+
disabled: status.value !== "ready",
|
|
1366
|
+
onClick: _cache[0] || (_cache[0] = ($event) => zoomCenter(1 / unref(1.25)))
|
|
1367
|
+
}, {
|
|
1368
|
+
default: withCtx(() => [createVNode(unref(BuiltinIcon), {
|
|
1369
|
+
icon: "zoom-out",
|
|
1370
|
+
size: 16
|
|
1371
|
+
})]),
|
|
1372
|
+
_: 1
|
|
1373
|
+
}, 8, ["disabled"]),
|
|
1374
|
+
createElementVNode("output", _hoisted_3$1, toDisplayString(percent.value), 1),
|
|
1375
|
+
createVNode(unref(HstButton), {
|
|
1376
|
+
color: "flat",
|
|
1377
|
+
"icon-only": "",
|
|
1378
|
+
"aria-label": "Zoom in",
|
|
1379
|
+
title: "Zoom in",
|
|
1380
|
+
disabled: status.value !== "ready",
|
|
1381
|
+
onClick: _cache[1] || (_cache[1] = ($event) => zoomCenter(unref(1.25)))
|
|
1382
|
+
}, {
|
|
1383
|
+
default: withCtx(() => [createVNode(unref(BuiltinIcon), {
|
|
1384
|
+
icon: "zoom-in",
|
|
1385
|
+
size: 16
|
|
1386
|
+
})]),
|
|
1387
|
+
_: 1
|
|
1388
|
+
}, 8, ["disabled"]),
|
|
1389
|
+
createVNode(unref(HstButton), {
|
|
1390
|
+
color: "flat",
|
|
1391
|
+
disabled: status.value !== "ready",
|
|
1392
|
+
onClick: _cache[2] || (_cache[2] = ($event) => fit())
|
|
1393
|
+
}, {
|
|
1394
|
+
default: withCtx(() => [..._cache[5] || (_cache[5] = [createTextVNode(" Fit ", -1)])]),
|
|
1395
|
+
_: 1
|
|
1396
|
+
}, 8, ["disabled"]),
|
|
1397
|
+
createVNode(unref(HstButton), {
|
|
1398
|
+
color: "flat",
|
|
1399
|
+
disabled: status.value !== "ready",
|
|
1400
|
+
onClick: _cache[3] || (_cache[3] = ($event) => center(1))
|
|
1401
|
+
}, {
|
|
1402
|
+
default: withCtx(() => [..._cache[6] || (_cache[6] = [createTextVNode(" 100% ", -1)])]),
|
|
1403
|
+
_: 1
|
|
1404
|
+
}, 8, ["disabled"])
|
|
1405
|
+
]), createElementVNode("div", {
|
|
1406
|
+
ref_key: "viewport",
|
|
1407
|
+
ref: viewport,
|
|
1408
|
+
class: "histoire-designs-image-viewport",
|
|
1409
|
+
role: "group",
|
|
1410
|
+
tabindex: "0",
|
|
1411
|
+
"aria-label": __props.design.alt || __props.design.name,
|
|
1412
|
+
"data-panning": panning.value || void 0,
|
|
1413
|
+
onWheel: withModifiers(onWheel, ["prevent"]),
|
|
1414
|
+
onKeydown: onKeyDown,
|
|
1415
|
+
onPointerdown: onPointerDown,
|
|
1416
|
+
onPointermove: onPointerMove,
|
|
1417
|
+
onPointerup: onPointerUp,
|
|
1418
|
+
onPointercancel: onPointerUp
|
|
1419
|
+
}, [withDirectives((openBlock(), createElementBlock("img", {
|
|
1420
|
+
key: __props.design.src,
|
|
1421
|
+
src: __props.design.src,
|
|
1422
|
+
alt: __props.design.alt,
|
|
1423
|
+
style: normalizeStyle({ transform: transform.value }),
|
|
1424
|
+
draggable: "false",
|
|
1425
|
+
onLoad,
|
|
1426
|
+
onError: _cache[4] || (_cache[4] = ($event) => status.value = "error")
|
|
1427
|
+
}, null, 44, _hoisted_5)), [[vShow, status.value === "ready"]]), status.value === "loading" ? (openBlock(), createBlock(unref(WorkbenchLoadingState), {
|
|
1428
|
+
key: 0,
|
|
1429
|
+
class: "histoire-designs-overlay",
|
|
1430
|
+
title: "Loading image"
|
|
1431
|
+
})) : status.value === "error" ? (openBlock(), createBlock(unref(WorkbenchErrorState), {
|
|
1432
|
+
key: 1,
|
|
1433
|
+
class: "histoire-designs-overlay",
|
|
1434
|
+
title: "Image unavailable"
|
|
1435
|
+
}, {
|
|
1436
|
+
default: withCtx(() => [createTextVNode(toDisplayString(__props.design.src), 1)]),
|
|
1437
|
+
_: 1
|
|
1438
|
+
})) : createCommentVNode("", true)], 40, _hoisted_4$1)]);
|
|
1439
|
+
};
|
|
1440
|
+
}
|
|
1441
|
+
});
|
|
1442
|
+
// src/workbench/DesignContent.vue
|
|
1443
|
+
var DesignContent_default = /* @__PURE__ */ defineComponent({
|
|
1444
|
+
__name: "DesignContent",
|
|
1445
|
+
props: { design: {} },
|
|
1446
|
+
setup(__props) {
|
|
1447
|
+
return (_ctx, _cache) => {
|
|
1448
|
+
return __props.design.type === "image" ? (openBlock(), createBlock(ImageViewer_default, {
|
|
1449
|
+
key: 0,
|
|
1450
|
+
design: __props.design
|
|
1451
|
+
}, null, 8, ["design"])) : __props.design.type === "link" ? (openBlock(), createBlock(unref(WorkbenchEmptyState), {
|
|
1452
|
+
key: 1,
|
|
1453
|
+
icon: "launch",
|
|
1454
|
+
title: __props.design.label
|
|
1455
|
+
}, {
|
|
1456
|
+
actions: withCtx(() => [createVNode(unref(HstButton), {
|
|
1457
|
+
as: "a",
|
|
1458
|
+
color: "flat",
|
|
1459
|
+
href: __props.design.externalUrl,
|
|
1460
|
+
target: "_blank",
|
|
1461
|
+
rel: "noopener noreferrer"
|
|
1462
|
+
}, {
|
|
1463
|
+
default: withCtx(() => [..._cache[0] || (_cache[0] = [createTextVNode(" Open design ", -1)])]),
|
|
1464
|
+
_: 1
|
|
1465
|
+
}, 8, ["href"])]),
|
|
1466
|
+
default: withCtx(() => [createTextVNode(toDisplayString(__props.design.externalUrl) + " ", 1)]),
|
|
1467
|
+
_: 1
|
|
1468
|
+
}, 8, ["title"])) : (openBlock(), createBlock(EmbedFrame_default, {
|
|
1469
|
+
key: 2,
|
|
1470
|
+
design: __props.design
|
|
1471
|
+
}, null, 8, ["design"]));
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
});
|
|
1475
|
+
// src/workbench/DesignPanel.vue?vue&type=script&setup=true&lang.ts
|
|
1476
|
+
var _hoisted_1 = {
|
|
1477
|
+
class: "histoire-designs histoire-designs-tab",
|
|
1478
|
+
"aria-label": "Design"
|
|
1479
|
+
};
|
|
1480
|
+
var _hoisted_2 = { class: "histoire-designs-toolbar" };
|
|
1481
|
+
var _hoisted_3 = {
|
|
1482
|
+
key: 1,
|
|
1483
|
+
class: "histoire-designs-name"
|
|
1484
|
+
};
|
|
1485
|
+
var _hoisted_4 = {
|
|
1486
|
+
key: 0,
|
|
1487
|
+
class: "histoire-designs-note",
|
|
1488
|
+
role: "status"
|
|
1489
|
+
};
|
|
1490
|
+
// src/workbench/DesignPanel.vue
|
|
1491
|
+
var DesignPanel_default = /* @__PURE__ */ defineComponent({
|
|
1492
|
+
__name: "DesignPanel",
|
|
1493
|
+
props: {
|
|
1494
|
+
context: {},
|
|
1495
|
+
model: {}
|
|
1496
|
+
},
|
|
1497
|
+
setup(__props) {
|
|
1498
|
+
const props = __props;
|
|
1499
|
+
/** Bumped on every model change. */
|
|
1500
|
+
const revision = ref(0);
|
|
1501
|
+
onScopeDispose(props.model.subscribe(() => revision.value++));
|
|
1502
|
+
/** Designs and invalid entries of the selected variant; `undefined` while its story loads. */
|
|
1503
|
+
const designs = computed(() => {
|
|
1504
|
+
revision.value;
|
|
1505
|
+
return props.model.get(props.context.frame.target);
|
|
1506
|
+
});
|
|
1507
|
+
/** Design shown in the tab. */
|
|
1508
|
+
const active = computed(() => {
|
|
1509
|
+
revision.value;
|
|
1510
|
+
return props.model.active(props.context.frame.target);
|
|
1511
|
+
});
|
|
1512
|
+
/** Switcher options; values are entry indexes. */
|
|
1513
|
+
const options = computed(() => designs.value?.designs.map((design) => ({
|
|
1514
|
+
label: design.name,
|
|
1515
|
+
value: design.index
|
|
1516
|
+
})) ?? []);
|
|
1517
|
+
/** Reasons why entries cannot be shown, numbered like the declared list. */
|
|
1518
|
+
const issues = computed(() => designs.value?.issues.map((issue) => `Entry ${issue.index + 1}: ${issue.message}`) ?? []);
|
|
1519
|
+
/**
|
|
1520
|
+
* Switches to another design of this variant.
|
|
1521
|
+
* @param entryIndex Entry index of the chosen design.
|
|
1522
|
+
*/
|
|
1523
|
+
function select(entryIndex) {
|
|
1524
|
+
props.model.select(props.context.frame.target, entryIndex);
|
|
1525
|
+
}
|
|
1526
|
+
return (_ctx, _cache) => {
|
|
1527
|
+
return openBlock(), createElementBlock("section", _hoisted_1, [!designs.value ? (openBlock(), createBlock(unref(WorkbenchLoadingState), {
|
|
1528
|
+
key: 0,
|
|
1529
|
+
title: "Loading designs"
|
|
1530
|
+
})) : designs.value.error ? (openBlock(), createBlock(unref(WorkbenchErrorState), {
|
|
1531
|
+
key: 1,
|
|
1532
|
+
title: "Design unavailable"
|
|
1533
|
+
}, {
|
|
1534
|
+
default: withCtx(() => [createTextVNode(toDisplayString(designs.value.error), 1)]),
|
|
1535
|
+
_: 1
|
|
1536
|
+
})) : !active.value ? (openBlock(), createBlock(unref(WorkbenchErrorState), {
|
|
1537
|
+
key: 2,
|
|
1538
|
+
title: "Design unavailable"
|
|
1539
|
+
}, {
|
|
1540
|
+
default: withCtx(() => [(openBlock(true), createElementBlock(Fragment, null, renderList(issues.value, (issue) => {
|
|
1541
|
+
return openBlock(), createElementBlock("span", {
|
|
1542
|
+
key: issue,
|
|
1543
|
+
class: "histoire-designs-issue"
|
|
1544
|
+
}, toDisplayString(issue), 1);
|
|
1545
|
+
}), 128))]),
|
|
1546
|
+
_: 1
|
|
1547
|
+
})) : (openBlock(), createElementBlock(Fragment, { key: 3 }, [
|
|
1548
|
+
createElementVNode("div", _hoisted_2, [options.value.length > 1 ? (openBlock(), createBlock(unref(HstSelect), {
|
|
1549
|
+
key: 0,
|
|
1550
|
+
class: "histoire-designs-switch",
|
|
1551
|
+
"aria-label": "Design",
|
|
1552
|
+
options: options.value,
|
|
1553
|
+
"model-value": active.value.index,
|
|
1554
|
+
"onUpdate:modelValue": select
|
|
1555
|
+
}, null, 8, ["options", "model-value"])) : (openBlock(), createElementBlock("span", _hoisted_3, toDisplayString(active.value.name), 1)), createVNode(unref(HstButton), {
|
|
1556
|
+
as: "a",
|
|
1557
|
+
color: "flat",
|
|
1558
|
+
"icon-only": "",
|
|
1559
|
+
href: active.value.externalUrl,
|
|
1560
|
+
target: "_blank",
|
|
1561
|
+
rel: "noopener noreferrer",
|
|
1562
|
+
"aria-label": "Open in new tab",
|
|
1563
|
+
title: "Open in new tab"
|
|
1564
|
+
}, {
|
|
1565
|
+
default: withCtx(() => [createVNode(unref(BuiltinIcon), {
|
|
1566
|
+
icon: "launch",
|
|
1567
|
+
size: 16
|
|
1568
|
+
})]),
|
|
1569
|
+
_: 1
|
|
1570
|
+
}, 8, ["href"])]),
|
|
1571
|
+
issues.value.length ? (openBlock(), createElementBlock("p", _hoisted_4, toDisplayString(issues.value.length === 1 ? "1 entry skipped" : `${issues.value.length} entries skipped`) + ": " + toDisplayString(issues.value.join("; ")), 1)) : createCommentVNode("", true),
|
|
1572
|
+
createVNode(DesignContent_default, { design: active.value }, null, 8, ["design"])
|
|
1573
|
+
], 64))]);
|
|
1574
|
+
};
|
|
1575
|
+
}
|
|
1576
|
+
});
|
|
1577
|
+
// src/workbench/model.ts
|
|
1578
|
+
/**
|
|
1579
|
+
* Stories kept and refreshed after catalog publications, most recently read first out last.
|
|
1580
|
+
* Surfaces read the stories they show on every render, so shown stories stay within it.
|
|
1581
|
+
*/
|
|
1582
|
+
var MAX_CACHED_STORIES = 16;
|
|
1583
|
+
/**
|
|
1584
|
+
* Metadata surfaces read from a story; equal signatures mean a refetch changed nothing visible.
|
|
1585
|
+
* @param story Catalog story.
|
|
1586
|
+
*/
|
|
1587
|
+
function signature(story) {
|
|
1588
|
+
return JSON.stringify([story.meta, story.variants.map((variant) => [
|
|
1589
|
+
variant.id,
|
|
1590
|
+
variant.title,
|
|
1591
|
+
variant.meta
|
|
1592
|
+
])]);
|
|
1593
|
+
}
|
|
1594
|
+
/**
|
|
1595
|
+
* Message of a failed story fetch.
|
|
1596
|
+
* @param error Rejection reason.
|
|
1597
|
+
*/
|
|
1598
|
+
function errorMessage(error) {
|
|
1599
|
+
return error instanceof Error && error.message ? error.message : "Story could not be loaded";
|
|
1600
|
+
}
|
|
1601
|
+
/**
|
|
1602
|
+
* Creates the design model. Stories are fetched lazily from the catalog and the most recently
|
|
1603
|
+
* read ones are refetched after each catalog publication; stale data stays readable until the
|
|
1604
|
+
* new version arrives, so surfaces never flash empty while a story file is edited.
|
|
1605
|
+
* @param api Plugin API.
|
|
1606
|
+
* @param options Design resolution settings.
|
|
1607
|
+
*/
|
|
1608
|
+
function createDesignModel(api, options) {
|
|
1609
|
+
/** Loaded stories with their metadata signature. */
|
|
1610
|
+
const stories = /* @__PURE__ */ new Map();
|
|
1611
|
+
/** Failed fetch reason per story id; retried after the next publication. */
|
|
1612
|
+
const failures = /* @__PURE__ */ new Map();
|
|
1613
|
+
/** Story ids in read order, oldest first; bounded by {@link MAX_CACHED_STORIES}. */
|
|
1614
|
+
const recent = /* @__PURE__ */ new Set();
|
|
1615
|
+
/** Story ids with a fetch in flight. */
|
|
1616
|
+
const pending = /* @__PURE__ */ new Set();
|
|
1617
|
+
/** In-flight story ids a publication outdated; refetched once their fetch settles. */
|
|
1618
|
+
const stale = /* @__PURE__ */ new Set();
|
|
1619
|
+
/** Resolved designs per variant key, dropped whenever its story is replaced. */
|
|
1620
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
1621
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1622
|
+
let notifyQueued = false;
|
|
1623
|
+
/** Re-evaluates contribution availability and updates mounted views, once per microtask. */
|
|
1624
|
+
function notify() {
|
|
1625
|
+
if (notifyQueued) return;
|
|
1626
|
+
notifyQueued = true;
|
|
1627
|
+
queueMicrotask(() => {
|
|
1628
|
+
notifyQueued = false;
|
|
1629
|
+
if (api.signal.aborted) return;
|
|
1630
|
+
api.invalidate();
|
|
1631
|
+
for (const listener of [...listeners]) listener();
|
|
1632
|
+
});
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Forgets everything cached for one story.
|
|
1636
|
+
* @param storyId Story id.
|
|
1637
|
+
*/
|
|
1638
|
+
function drop(storyId) {
|
|
1639
|
+
stories.delete(storyId);
|
|
1640
|
+
failures.delete(storyId);
|
|
1641
|
+
for (const key of [...resolved.keys()]) if (key.startsWith(`${storyId}\0`)) resolved.delete(key);
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Marks a story as just read, evicting the least recently read one past the limit.
|
|
1645
|
+
* @param storyId Story id.
|
|
1646
|
+
*/
|
|
1647
|
+
function touch(storyId) {
|
|
1648
|
+
recent.delete(storyId);
|
|
1649
|
+
recent.add(storyId);
|
|
1650
|
+
if (recent.size <= MAX_CACHED_STORIES) return;
|
|
1651
|
+
const oldest = recent.values().next().value;
|
|
1652
|
+
recent.delete(oldest);
|
|
1653
|
+
stale.delete(oldest);
|
|
1654
|
+
drop(oldest);
|
|
1655
|
+
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Stores a fetched story; surfaces are only notified when its metadata changed.
|
|
1658
|
+
* @param storyId Story id.
|
|
1659
|
+
* @param story Fetched story.
|
|
1660
|
+
*/
|
|
1661
|
+
function store(storyId, story) {
|
|
1662
|
+
const next = signature(story);
|
|
1663
|
+
const hadFailed = failures.delete(storyId);
|
|
1664
|
+
if (stories.get(storyId)?.signature === next && !hadFailed) return;
|
|
1665
|
+
drop(storyId);
|
|
1666
|
+
stories.set(storyId, {
|
|
1667
|
+
story,
|
|
1668
|
+
signature: next
|
|
1669
|
+
});
|
|
1670
|
+
notify();
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* Records a failed fetch; a removed story has no designs, surfaces show the reason.
|
|
1674
|
+
* @param storyId Story id.
|
|
1675
|
+
* @param error Rejection reason.
|
|
1676
|
+
*/
|
|
1677
|
+
function fail(storyId, error) {
|
|
1678
|
+
const message = errorMessage(error);
|
|
1679
|
+
if (failures.get(storyId) === message && !stories.has(storyId)) return;
|
|
1680
|
+
drop(storyId);
|
|
1681
|
+
failures.set(storyId, message);
|
|
1682
|
+
notify();
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* Fetches one story; concurrent requests share one fetch. A refresh requested meanwhile
|
|
1686
|
+
* runs once the current fetch settles, so the last publication always wins.
|
|
1687
|
+
* @param storyId Story id.
|
|
1688
|
+
* @param refresh Requested by a catalog publication.
|
|
1689
|
+
*/
|
|
1690
|
+
function load(storyId, refresh) {
|
|
1691
|
+
if (api.signal.aborted) return;
|
|
1692
|
+
if (pending.has(storyId)) {
|
|
1693
|
+
if (refresh) stale.add(storyId);
|
|
1694
|
+
return;
|
|
1695
|
+
}
|
|
1696
|
+
pending.add(storyId);
|
|
1697
|
+
const current = () => !api.signal.aborted && recent.has(storyId);
|
|
1698
|
+
api.context.catalog.getStory(storyId).then((story) => {
|
|
1699
|
+
if (current()) store(storyId, story);
|
|
1700
|
+
}, (error) => {
|
|
1701
|
+
if (current()) fail(storyId, error);
|
|
1702
|
+
}).finally(() => {
|
|
1703
|
+
pending.delete(storyId);
|
|
1704
|
+
if (stale.delete(storyId)) load(storyId, true);
|
|
1705
|
+
});
|
|
1706
|
+
}
|
|
1707
|
+
const stop = api.context.catalog.subscribe?.(() => {
|
|
1708
|
+
for (const storyId of recent) if (stories.has(storyId) || failures.has(storyId) || pending.has(storyId)) load(storyId, true);
|
|
1709
|
+
});
|
|
1710
|
+
api.signal.addEventListener("abort", () => {
|
|
1711
|
+
stop?.();
|
|
1712
|
+
listeners.clear();
|
|
1713
|
+
}, { once: true });
|
|
1714
|
+
/**
|
|
1715
|
+
* Preference key of the selected design of one variant.
|
|
1716
|
+
* @param target Variant.
|
|
1717
|
+
*/
|
|
1718
|
+
const preferenceKey = (target) => `active:${target.storyId}:${target.variantId}`;
|
|
1719
|
+
const model = {
|
|
1720
|
+
get(target) {
|
|
1721
|
+
if (target.variantId === null) return;
|
|
1722
|
+
touch(target.storyId);
|
|
1723
|
+
const failure = failures.get(target.storyId);
|
|
1724
|
+
if (failure !== void 0) return {
|
|
1725
|
+
designs: [],
|
|
1726
|
+
issues: [],
|
|
1727
|
+
error: failure
|
|
1728
|
+
};
|
|
1729
|
+
const story = stories.get(target.storyId)?.story;
|
|
1730
|
+
if (!story) {
|
|
1731
|
+
load(target.storyId, false);
|
|
1732
|
+
return;
|
|
1733
|
+
}
|
|
1734
|
+
const key = `${target.storyId}\0${target.variantId}`;
|
|
1735
|
+
let designs = resolved.get(key);
|
|
1736
|
+
if (!designs) resolved.set(key, designs = resolveDesigns(story, target.variantId, options));
|
|
1737
|
+
return designs;
|
|
1738
|
+
},
|
|
1739
|
+
has(target) {
|
|
1740
|
+
const designs = model.get(target);
|
|
1741
|
+
return !!designs && (designs.designs.length > 0 || designs.issues.length > 0 || !!designs.error);
|
|
1742
|
+
},
|
|
1743
|
+
variant(target) {
|
|
1744
|
+
return stories.get(target.storyId)?.story.variants.find((variant) => variant.id === target.variantId);
|
|
1745
|
+
},
|
|
1746
|
+
active(target) {
|
|
1747
|
+
const designs = model.get(target)?.designs;
|
|
1748
|
+
if (!designs?.length) return;
|
|
1749
|
+
const stored = api.preferences.get(preferenceKey(target), null);
|
|
1750
|
+
return designs.find((design) => design.index === stored) ?? designs[0];
|
|
1751
|
+
},
|
|
1752
|
+
select(target, entryIndex) {
|
|
1753
|
+
if (!Number.isInteger(entryIndex) || entryIndex < 0) return;
|
|
1754
|
+
api.preferences.set(preferenceKey(target), entryIndex);
|
|
1755
|
+
notify();
|
|
1756
|
+
},
|
|
1757
|
+
subscribe(listener) {
|
|
1758
|
+
const entry = () => listener();
|
|
1759
|
+
listeners.add(entry);
|
|
1760
|
+
return () => void listeners.delete(entry);
|
|
1761
|
+
},
|
|
1762
|
+
async preload(storyId) {
|
|
1763
|
+
touch(storyId);
|
|
1764
|
+
if (stories.has(storyId) || failures.has(storyId)) return;
|
|
1765
|
+
try {
|
|
1766
|
+
const story = await api.context.catalog.getStory(storyId);
|
|
1767
|
+
if (!api.signal.aborted) store(storyId, story);
|
|
1768
|
+
} catch (error) {
|
|
1769
|
+
if (!api.signal.aborted) fail(storyId, error);
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
};
|
|
1773
|
+
return model;
|
|
1774
|
+
}
|
|
1775
|
+
// src/workbench/styles.css?inline
|
|
1776
|
+
var styles_default = "/* Inspector tab: the host already pads the content, so blocks share one gap and the design fills the rest. */\n.histoire-designs-tab {\n display: flex;\n flex-direction: column;\n gap: 8px;\n height: 100%;\n min-height: 0;\n color: var(--histoire-text, inherit);\n font-size: 13px;\n}\n.histoire-designs-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: var(--histoire-control-height, 28px); }\n.histoire-designs-switch { flex: 1; min-width: 0; --histoire-control-margin: 0px; --histoire-control-wrapper-padding: 0px; }\n.histoire-designs-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }\n.histoire-designs-note { margin: 0; font-size: 12px; opacity: .75; }\n.histoire-designs-issue { display: block; }\n\n/* Embedded documents and image viewer fill the tab below the toolbar. */\n.histoire-designs-embed, .histoire-designs-image-viewport {\n position: relative;\n flex: 1;\n min-height: 320px;\n overflow: hidden;\n border: 1px solid var(--histoire-border, rgb(128 128 128 / .25));\n border-radius: var(--histoire-radius-control, 6px);\n background: var(--histoire-surface, transparent);\n}\n.histoire-designs-embed iframe { display: block; width: 100%; height: 100%; border: 0; }\n.histoire-designs-overlay { position: absolute; inset: 0; display: grid; place-content: center; background: var(--histoire-surface, transparent); }\n\n.histoire-designs-image { display: flex; flex: 1; flex-direction: column; gap: 6px; min-height: 0; }\n.histoire-designs-image-tools { display: flex; align-items: center; gap: 2px; }\n.histoire-designs-zoom { min-width: 44px; font-size: 12px; text-align: center; font-variant-numeric: tabular-nums; }\n.histoire-designs-image-viewport { cursor: grab; touch-action: none; }\n.histoire-designs-image-viewport[data-panning] { cursor: grabbing; }\n/* Inset ring: the viewport clips its content and may touch the scroll edges of the tab. */\n.histoire-designs-image-viewport:focus-visible { outline: 2px solid var(--histoire-accent, currentColor); outline-offset: -2px; }\n.histoire-designs-image-viewport img { position: absolute; top: 0; left: 0; max-width: none; transform-origin: 0 0; user-select: none; }\n\n/* Canvas arrangement: variant frame and design side by side, following pan and zoom. */\n.histoire-designs-canvas { position: absolute; inset: 0; overflow: hidden; }\n.histoire-designs-canvas-layer { position: absolute; top: 0; left: 0; transform-origin: 0 0; }\n.histoire-designs-canvas-column { position: absolute; display: flex; flex-direction: column; gap: 6px; }\n.histoire-designs-canvas-label {\n display: flex;\n align-items: center;\n justify-content: space-between;\n gap: 4px;\n height: 26px;\n color: var(--histoire-text);\n font: 600 13px/26px var(--histoire-font-sans, \"Manrope\", system-ui, sans-serif);\n}\n/* Groups the design switches next to the label. */\n.histoire-designs-canvas-switches { display: inline-flex; align-items: center; gap: 2px; }\n.histoire-designs-canvas-switch {\n height: 24px;\n padding: 0 8px;\n border: 1px solid transparent;\n border-radius: var(--histoire-radius-control, 6px);\n background: transparent;\n color: inherit;\n font: inherit;\n opacity: .7;\n cursor: pointer;\n}\n.histoire-designs-canvas-switch:hover { opacity: 1; }\n.histoire-designs-canvas-switch[aria-pressed=\"true\"] { opacity: 1; border-color: var(--histoire-border); background: var(--histoire-surface); }\n.histoire-designs-canvas-box {\n flex: 1;\n min-height: 0;\n overflow: hidden;\n border: 1px solid var(--histoire-border);\n border-radius: var(--histoire-radius-panel, 12px);\n background: var(--histoire-surface);\n box-shadow: var(--histoire-shadow-toolbar);\n}\n.histoire-designs-canvas-box > * { display: block; width: 100%; height: 100%; border: 0; }\n.histoire-designs-canvas-image { object-fit: contain; user-select: none; }\n.histoire-designs-canvas-link { display: flex; flex-direction: column; justify-content: center; gap: 6px; padding: 16px; color: var(--histoire-text); font: 14px var(--histoire-font-sans, \"Manrope\", system-ui, sans-serif); box-sizing: border-box; }\n.histoire-designs-canvas-link a { color: inherit; font-weight: 600; text-underline-offset: 3px; }\n.histoire-designs-canvas-url { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; opacity: .7; }\n/* Design that failed to load, shown inside its box. */\n.histoire-designs-canvas-error {\n display: flex;\n flex-direction: column;\n justify-content: center;\n gap: 6px;\n padding: 16px;\n color: rgb(var(--histoire-danger-text-rgb, 220 60 80));\n font: 14px var(--histoire-font-sans, \"Manrope\", system-ui, sans-serif);\n overflow-wrap: anywhere;\n box-sizing: border-box;\n}\n.histoire-designs-canvas-state { position: absolute; inset: 0; display: grid; place-content: center; }\n/* Cleared state: never cover the canvas. */\n.histoire-designs-canvas-state:empty { display: none; }\n\n/* Overlay and Split: the design lies over the frame and lets pointer input through to the story. */\n.histoire-designs-canvas[data-mode=\"overlay\"] .histoire-designs-canvas-design,\n.histoire-designs-canvas[data-mode=\"split\"] .histoire-designs-canvas-design { pointer-events: none; }\n.histoire-designs-canvas[data-mode=\"overlay\"] .histoire-designs-canvas-design > .histoire-designs-canvas-box,\n.histoire-designs-canvas[data-mode=\"split\"] .histoire-designs-canvas-design > .histoire-designs-canvas-box { border: 0; border-radius: 0; box-shadow: none; background: transparent; }\n.histoire-designs-canvas-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }\n\n/* Grips keep their pixel size at any zoom: `--histoire-designs-zoom` follows the canvas. */\n.histoire-designs-overlay-chrome { position: absolute; outline: calc(1px / var(--histoire-designs-zoom, 1)) dashed rgb(var(--histoire-accent-rgb, 5 150 105)); pointer-events: none; }\n.histoire-designs-overlay-grip {\n position: absolute;\n display: grid;\n place-content: center;\n width: 22px;\n height: 22px;\n border: 1px solid var(--histoire-border);\n border-radius: var(--histoire-radius-control, 6px);\n background: var(--histoire-surface);\n color: var(--histoire-text);\n box-shadow: var(--histoire-shadow-toolbar);\n box-sizing: border-box;\n pointer-events: auto;\n touch-action: none;\n scale: calc(1 / var(--histoire-designs-zoom, 1));\n}\n.histoire-designs-overlay-grip:focus-visible { outline: 2px solid rgb(var(--histoire-accent-rgb, 5 150 105)); outline-offset: 1px; }\n.histoire-designs-overlay-move { right: 100%; bottom: 100%; transform-origin: bottom right; cursor: move; }\n.histoire-designs-overlay-resize { right: 0; bottom: 0; transform-origin: bottom right; cursor: nwse-resize; }\n/* Corner mark of the resize grip. */\n.histoire-designs-overlay-resize::after { content: \"\"; width: 8px; height: 8px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; }\n.histoire-designs-overlay-divider {\n position: absolute;\n top: 0;\n bottom: 0;\n width: calc(2px / var(--histoire-designs-zoom, 1));\n translate: -50% 0;\n background: rgb(var(--histoire-accent-rgb, 5 150 105));\n}\n.histoire-designs-overlay-knob { top: 50%; left: 50%; translate: -50% -50%; cursor: ew-resize; }\n\n/* Floating comparison bar, centered at the bottom of the canvas area left of the floating inspector. */\n.histoire-designs-canvas-bar {\n position: absolute;\n bottom: 20px;\n inset-inline: 0 var(--histoire-canvas-inspector-reserve, 0px);\n display: flex;\n justify-content: center;\n padding-inline: 16px;\n pointer-events: none;\n}\n.histoire-designs-canvas-bar[hidden] { display: none; }\n.histoire-designs-mode {\n display: flex;\n align-items: center;\n gap: 8px;\n max-width: 100%;\n padding: 4px;\n border: 1px solid var(--histoire-border);\n border-radius: 12px;\n background: rgb(var(--histoire-surface-rgb, 255 255 255) / 88%);\n box-shadow: var(--histoire-shadow-popover);\n backdrop-filter: blur(10px);\n pointer-events: auto;\n}\n.histoire-designs-mode-switch, .histoire-designs-mode-opacity { --histoire-control-margin: 0px; --histoire-control-wrapper-padding: 0px; }\n.histoire-designs-mode-opacity { width: 96px; }\n/* Labels differ in length: equal-width choices would clip \"Side by side\". */\n.histoire-designs-mode .histoire-designs-mode-option { flex: none; }\n";
|
|
1777
|
+
// src/client.ts
|
|
1778
|
+
var client_default = defineWorkbenchPlugin({
|
|
1779
|
+
id: "designs",
|
|
1780
|
+
apiVersion: 1,
|
|
1781
|
+
async setup(api) {
|
|
1782
|
+
installPluginStyles(document, "designs", styles_default, api.signal);
|
|
1783
|
+
const options = api.options ?? {};
|
|
1784
|
+
const model = createDesignModel(api, {
|
|
1785
|
+
embedHost: options.embedHost ?? "histoire",
|
|
1786
|
+
baseUrl: new URL(import.meta.env.BASE_URL, document.baseURI).href
|
|
1787
|
+
});
|
|
1788
|
+
const selected = api.context.selection.target;
|
|
1789
|
+
if (selected) await model.preload(selected.storyId);
|
|
1790
|
+
api.inspector.addTab({
|
|
1791
|
+
id: "design",
|
|
1792
|
+
label: "Design",
|
|
1793
|
+
icon: "color-palette",
|
|
1794
|
+
available: (context) => model.has(context.frame.target),
|
|
1795
|
+
component: defineWorkbenchComponent(defineComponent({
|
|
1796
|
+
name: "DesignInspector",
|
|
1797
|
+
props: {
|
|
1798
|
+
/** Current frame scope. */
|
|
1799
|
+
context: {
|
|
1800
|
+
type: Object,
|
|
1801
|
+
required: true
|
|
1802
|
+
} },
|
|
1803
|
+
setup: (props) => () => h(DesignPanel_default, {
|
|
1804
|
+
context: props.context,
|
|
1805
|
+
model
|
|
1806
|
+
})
|
|
1807
|
+
}))
|
|
1808
|
+
});
|
|
1809
|
+
if (options.canvasView === false) return;
|
|
1810
|
+
const overlay = createOverlayState(api);
|
|
1811
|
+
api.canvas.addView({
|
|
1812
|
+
id: "side-by-side",
|
|
1813
|
+
label: "Design",
|
|
1814
|
+
icon: "color-palette",
|
|
1815
|
+
...sideBySideView(model, overlay)
|
|
1816
|
+
});
|
|
1817
|
+
}
|
|
1818
|
+
});
|
|
1819
|
+
export { client_default as default };
|