@astl/editor 0.0.0 → 0.1.0
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/CHANGELOG.md +96 -0
- package/LICENSE +202 -0
- package/README.md +43 -0
- package/dist/authoring.d.ts +284 -0
- package/dist/authoring.d.ts.map +1 -0
- package/dist/authoring.js +566 -0
- package/dist/authoring.js.map +1 -0
- package/dist/define.d.ts +8 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +35 -0
- package/dist/define.js.map +1 -0
- package/dist/edit.d.ts +45 -0
- package/dist/edit.d.ts.map +1 -0
- package/dist/edit.js +150 -0
- package/dist/edit.js.map +1 -0
- package/dist/elements/base.d.ts +31 -0
- package/dist/elements/base.d.ts.map +1 -0
- package/dist/elements/base.js +75 -0
- package/dist/elements/base.js.map +1 -0
- package/dist/elements/color-control.d.ts +25 -0
- package/dist/elements/color-control.d.ts.map +1 -0
- package/dist/elements/color-control.js +139 -0
- package/dist/elements/color-control.js.map +1 -0
- package/dist/elements/help-form.d.ts +31 -0
- package/dist/elements/help-form.d.ts.map +1 -0
- package/dist/elements/help-form.js +371 -0
- package/dist/elements/help-form.js.map +1 -0
- package/dist/elements/insert-menu.d.ts +79 -0
- package/dist/elements/insert-menu.d.ts.map +1 -0
- package/dist/elements/insert-menu.js +362 -0
- package/dist/elements/insert-menu.js.map +1 -0
- package/dist/elements/inspector.d.ts +24 -0
- package/dist/elements/inspector.d.ts.map +1 -0
- package/dist/elements/inspector.js +846 -0
- package/dist/elements/inspector.js.map +1 -0
- package/dist/elements/issue-list.d.ts +18 -0
- package/dist/elements/issue-list.d.ts.map +1 -0
- package/dist/elements/issue-list.js +85 -0
- package/dist/elements/issue-list.js.map +1 -0
- package/dist/elements/issues.d.ts +24 -0
- package/dist/elements/issues.d.ts.map +1 -0
- package/dist/elements/issues.js +81 -0
- package/dist/elements/issues.js.map +1 -0
- package/dist/elements/picture-dialog.d.ts +40 -0
- package/dist/elements/picture-dialog.d.ts.map +1 -0
- package/dist/elements/picture-dialog.js +433 -0
- package/dist/elements/picture-dialog.js.map +1 -0
- package/dist/elements/picture-field.d.ts +33 -0
- package/dist/elements/picture-field.d.ts.map +1 -0
- package/dist/elements/picture-field.js +692 -0
- package/dist/elements/picture-field.js.map +1 -0
- package/dist/elements/preview.d.ts +34 -0
- package/dist/elements/preview.d.ts.map +1 -0
- package/dist/elements/preview.js +165 -0
- package/dist/elements/preview.js.map +1 -0
- package/dist/elements/style-form.d.ts +30 -0
- package/dist/elements/style-form.d.ts.map +1 -0
- package/dist/elements/style-form.js +640 -0
- package/dist/elements/style-form.js.map +1 -0
- package/dist/elements/style-load.d.ts +26 -0
- package/dist/elements/style-load.d.ts.map +1 -0
- package/dist/elements/style-load.js +50 -0
- package/dist/elements/style-load.js.map +1 -0
- package/dist/elements/style-popups.d.ts +39 -0
- package/dist/elements/style-popups.d.ts.map +1 -0
- package/dist/elements/style-popups.js +173 -0
- package/dist/elements/style-popups.js.map +1 -0
- package/dist/elements/style-rules.d.ts +21 -0
- package/dist/elements/style-rules.d.ts.map +1 -0
- package/dist/elements/style-rules.js +274 -0
- package/dist/elements/style-rules.js.map +1 -0
- package/dist/elements/styles.d.ts +9 -0
- package/dist/elements/styles.d.ts.map +1 -0
- package/dist/elements/styles.js +161 -0
- package/dist/elements/styles.js.map +1 -0
- package/dist/elements/toolbar.d.ts +65 -0
- package/dist/elements/toolbar.d.ts.map +1 -0
- package/dist/elements/toolbar.js +1270 -0
- package/dist/elements/toolbar.js.map +1 -0
- package/dist/help.d.ts +33 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +111 -0
- package/dist/help.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/inspect.d.ts +251 -0
- package/dist/inspect.d.ts.map +1 -0
- package/dist/inspect.js +428 -0
- package/dist/inspect.js.map +1 -0
- package/dist/messages/en.json +252 -0
- package/dist/messages/help/en.json +18 -0
- package/dist/messages/help/pl.json +18 -0
- package/dist/messages/pl.json +252 -0
- package/dist/messages/style/en.json +67 -0
- package/dist/messages/style/pl.json +67 -0
- package/dist/messages.d.ts +5 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +9 -0
- package/dist/messages.js.map +1 -0
- package/dist/pages.d.ts +28 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +147 -0
- package/dist/pages.js.map +1 -0
- package/dist/scan.d.ts +82 -0
- package/dist/scan.d.ts.map +1 -0
- package/dist/scan.js +329 -0
- package/dist/scan.js.map +1 -0
- package/dist/style-messages.d.ts +7 -0
- package/dist/style-messages.d.ts.map +1 -0
- package/dist/style-messages.js +12 -0
- package/dist/style-messages.js.map +1 -0
- package/dist/style-model.d.ts +50 -0
- package/dist/style-model.d.ts.map +1 -0
- package/dist/style-model.js +76 -0
- package/dist/style-model.js.map +1 -0
- package/dist/style-placing.d.ts +38 -0
- package/dist/style-placing.d.ts.map +1 -0
- package/dist/style-placing.js +98 -0
- package/dist/style-placing.js.map +1 -0
- package/dist/textarea.d.ts +17 -0
- package/dist/textarea.d.ts.map +1 -0
- package/dist/textarea.js +175 -0
- package/dist/textarea.js.map +1 -0
- package/package.json +42 -4
- package/src/authoring.ts +896 -0
- package/src/define.ts +34 -0
- package/src/edit.ts +195 -0
- package/src/elements/base.ts +93 -0
- package/src/elements/color-control.ts +159 -0
- package/src/elements/help-form.ts +404 -0
- package/src/elements/insert-menu.ts +481 -0
- package/src/elements/inspector.ts +941 -0
- package/src/elements/issue-list.ts +103 -0
- package/src/elements/issues.ts +89 -0
- package/src/elements/picture-dialog.ts +461 -0
- package/src/elements/picture-field.ts +770 -0
- package/src/elements/preview.ts +186 -0
- package/src/elements/style-form.ts +751 -0
- package/src/elements/style-load.ts +64 -0
- package/src/elements/style-popups.ts +257 -0
- package/src/elements/style-rules.ts +294 -0
- package/src/elements/styles.ts +162 -0
- package/src/elements/toolbar.ts +1386 -0
- package/src/help.ts +156 -0
- package/src/index.ts +81 -0
- package/src/inspect.ts +774 -0
- package/src/messages/en.json +252 -0
- package/src/messages/help/en.json +18 -0
- package/src/messages/help/pl.json +18 -0
- package/src/messages/pl.json +252 -0
- package/src/messages/style/en.json +67 -0
- package/src/messages/style/pl.json +67 -0
- package/src/messages.ts +11 -0
- package/src/pages.ts +158 -0
- package/src/scan.ts +382 -0
- package/src/style-messages.ts +13 -0
- package/src/style-model.ts +125 -0
- package/src/style-placing.ts +117 -0
- package/src/textarea.ts +182 -0
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
import { PALETTE } from '@astl/tokens';
|
|
2
|
+
import { css, html, LitElement, nothing, svg, } from 'lit';
|
|
3
|
+
import { refusalOf } from '../authoring.js';
|
|
4
|
+
import { EDITOR_NAMESPACE } from '../messages.js';
|
|
5
|
+
import { kitStyles } from './styles.js';
|
|
6
|
+
/** Fired with a FieldChange for the inspector to apply. */
|
|
7
|
+
export const FIELD_CHANGE = 'astl-field-change';
|
|
8
|
+
const round = (n) => Math.round(Math.min(100, Math.max(0, n)) * 10) / 10;
|
|
9
|
+
const num = (n) => String(round(n));
|
|
10
|
+
/** Most zones, pins or polygon points the field lists (and polygons take): more are edited in the source. */
|
|
11
|
+
const MAX_LISTED = 64;
|
|
12
|
+
/** Palette slot of the n-th item (from 0), as a class: `c1` to `c8`. */
|
|
13
|
+
const slot = (index) => `c${(index % PALETTE.length) + 1}`;
|
|
14
|
+
/** The attributes that describe a zone of `shape` around the zone's centre: switching the shape keeps the centre. */
|
|
15
|
+
function reshaped(region, shape) {
|
|
16
|
+
const box = boundsOf(region);
|
|
17
|
+
const cx = (box.left + box.right) / 2;
|
|
18
|
+
const cy = (box.top + box.bottom) / 2;
|
|
19
|
+
const w = Math.max(1, box.right - box.left);
|
|
20
|
+
const h = Math.max(1, box.bottom - box.top);
|
|
21
|
+
const cleared = { x: null, y: null, width: null, height: null, r: null, points: null };
|
|
22
|
+
if (shape === 'circle')
|
|
23
|
+
return { ...cleared, shape, x: num(cx), y: num(cy), r: num(w / 2) };
|
|
24
|
+
if (shape === 'polygon') {
|
|
25
|
+
const points = [
|
|
26
|
+
[box.left, box.top],
|
|
27
|
+
[box.right, box.top],
|
|
28
|
+
[box.right, box.bottom],
|
|
29
|
+
[box.left, box.bottom],
|
|
30
|
+
];
|
|
31
|
+
return { ...cleared, shape, points: points.map(([x = 0, y = 0]) => `${num(x)},${num(y)}`).join(' ') };
|
|
32
|
+
}
|
|
33
|
+
return { ...cleared, shape, x: num(cx - w / 2), y: num(cy - h / 2), width: num(w), height: num(h) };
|
|
34
|
+
}
|
|
35
|
+
/** A zone's bounding box in percent. */
|
|
36
|
+
function boundsOf(region) {
|
|
37
|
+
if (region.shape === 'circle') {
|
|
38
|
+
return {
|
|
39
|
+
left: region.x - region.r,
|
|
40
|
+
top: region.y - region.r,
|
|
41
|
+
right: region.x + region.r,
|
|
42
|
+
bottom: region.y + region.r,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
if (region.shape === 'polygon' && region.points.length > 0) {
|
|
46
|
+
// A loop, not Math.min(...xs): a polygon of any size never overflows the call stack.
|
|
47
|
+
const box = { left: 100, top: 100, right: 0, bottom: 0 };
|
|
48
|
+
for (const [x, y] of region.points) {
|
|
49
|
+
box.left = Math.min(box.left, x);
|
|
50
|
+
box.top = Math.min(box.top, y);
|
|
51
|
+
box.right = Math.max(box.right, x);
|
|
52
|
+
box.bottom = Math.max(box.bottom, y);
|
|
53
|
+
}
|
|
54
|
+
return box;
|
|
55
|
+
}
|
|
56
|
+
return { left: region.x, top: region.y, right: region.x + region.width, bottom: region.y + region.height };
|
|
57
|
+
}
|
|
58
|
+
/** Where a zone's number sits: its centre. */
|
|
59
|
+
function anchorOf(region) {
|
|
60
|
+
const box = boundsOf(region);
|
|
61
|
+
return [(box.left + box.right) / 2, (box.top + box.bottom) / 2];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* `<astl-picture-field>`: the inspector's `picture-regions` and `picture-points` fields. The picture (by address,
|
|
65
|
+
* uploaded with the host's `uploadAsset` or chosen from its library, `pickPicture`) with its zones or pins drawn on
|
|
66
|
+
* it in palette colours and numbered; drag on it to draw a zone, click to place a pin; and every value in number
|
|
67
|
+
* fields, so nothing needs a pointer. Changes go to the inspector as `astl-field-change` events.
|
|
68
|
+
*/
|
|
69
|
+
export class AstlPictureField extends LitElement {
|
|
70
|
+
static properties = {
|
|
71
|
+
field: { attribute: false },
|
|
72
|
+
authoring: { attribute: false },
|
|
73
|
+
element: { attribute: false },
|
|
74
|
+
};
|
|
75
|
+
static styles = [
|
|
76
|
+
kitStyles,
|
|
77
|
+
css `
|
|
78
|
+
fieldset {
|
|
79
|
+
display: flex;
|
|
80
|
+
flex-direction: column;
|
|
81
|
+
gap: var(--astl-space-3);
|
|
82
|
+
min-inline-size: 0;
|
|
83
|
+
margin: 0;
|
|
84
|
+
padding: var(--astl-space-3);
|
|
85
|
+
border: var(--astl-outline-width) solid var(--astl-line);
|
|
86
|
+
border-radius: var(--astl-radius-m);
|
|
87
|
+
}
|
|
88
|
+
legend {
|
|
89
|
+
padding-inline: var(--astl-space-1);
|
|
90
|
+
font-weight: 600;
|
|
91
|
+
}
|
|
92
|
+
label {
|
|
93
|
+
display: flex;
|
|
94
|
+
flex-direction: column;
|
|
95
|
+
gap: var(--astl-space-1);
|
|
96
|
+
}
|
|
97
|
+
.canvas {
|
|
98
|
+
position: relative;
|
|
99
|
+
inline-size: 100%;
|
|
100
|
+
touch-action: none;
|
|
101
|
+
cursor: crosshair;
|
|
102
|
+
user-select: none;
|
|
103
|
+
border: var(--astl-outline-width) solid var(--astl-line);
|
|
104
|
+
background: var(--astl-surface-raised);
|
|
105
|
+
}
|
|
106
|
+
.canvas img {
|
|
107
|
+
display: block;
|
|
108
|
+
inline-size: 100%;
|
|
109
|
+
block-size: auto;
|
|
110
|
+
}
|
|
111
|
+
.canvas .blank {
|
|
112
|
+
aspect-ratio: 4 / 3;
|
|
113
|
+
}
|
|
114
|
+
.canvas svg {
|
|
115
|
+
position: absolute;
|
|
116
|
+
inset: 0;
|
|
117
|
+
inline-size: 100%;
|
|
118
|
+
block-size: 100%;
|
|
119
|
+
overflow: visible;
|
|
120
|
+
}
|
|
121
|
+
.shape {
|
|
122
|
+
fill: var(--soft);
|
|
123
|
+
fill-opacity: 0.4;
|
|
124
|
+
stroke: var(--line);
|
|
125
|
+
stroke-width: 2;
|
|
126
|
+
vector-effect: non-scaling-stroke;
|
|
127
|
+
}
|
|
128
|
+
.shape.unflagged {
|
|
129
|
+
fill-opacity: 0.15;
|
|
130
|
+
stroke-dasharray: 6 4;
|
|
131
|
+
}
|
|
132
|
+
.draft {
|
|
133
|
+
fill: none;
|
|
134
|
+
stroke: var(--astl-ink);
|
|
135
|
+
stroke-width: 2;
|
|
136
|
+
stroke-dasharray: 4 3;
|
|
137
|
+
vector-effect: non-scaling-stroke;
|
|
138
|
+
}
|
|
139
|
+
.badge {
|
|
140
|
+
position: absolute;
|
|
141
|
+
inset-inline-start: var(--x);
|
|
142
|
+
inset-block-start: var(--y);
|
|
143
|
+
transform: translate(-50%, -50%);
|
|
144
|
+
display: inline-flex;
|
|
145
|
+
align-items: center;
|
|
146
|
+
justify-content: center;
|
|
147
|
+
box-sizing: border-box;
|
|
148
|
+
min-inline-size: 1.5rem;
|
|
149
|
+
block-size: 1.5rem;
|
|
150
|
+
padding-inline: var(--astl-space-1);
|
|
151
|
+
border: var(--astl-outline-width) solid var(--line);
|
|
152
|
+
border-radius: 999px;
|
|
153
|
+
background: var(--astl-surface);
|
|
154
|
+
color: var(--astl-ink);
|
|
155
|
+
font-size: var(--astl-size-small);
|
|
156
|
+
font-weight: 700;
|
|
157
|
+
pointer-events: none;
|
|
158
|
+
}
|
|
159
|
+
.pin {
|
|
160
|
+
border-width: calc(2 * var(--astl-outline-width));
|
|
161
|
+
}
|
|
162
|
+
.swatch {
|
|
163
|
+
display: inline-flex;
|
|
164
|
+
align-items: center;
|
|
165
|
+
justify-content: center;
|
|
166
|
+
min-inline-size: 1.5rem;
|
|
167
|
+
block-size: 1.5rem;
|
|
168
|
+
margin-inline-end: var(--astl-space-2);
|
|
169
|
+
border: calc(2 * var(--astl-outline-width)) solid var(--line);
|
|
170
|
+
border-radius: 999px;
|
|
171
|
+
background: var(--soft);
|
|
172
|
+
font-weight: 700;
|
|
173
|
+
}
|
|
174
|
+
.items {
|
|
175
|
+
display: flex;
|
|
176
|
+
flex-direction: column;
|
|
177
|
+
gap: var(--astl-space-3);
|
|
178
|
+
margin: 0;
|
|
179
|
+
padding: 0;
|
|
180
|
+
list-style: none;
|
|
181
|
+
}
|
|
182
|
+
.numbers {
|
|
183
|
+
display: grid;
|
|
184
|
+
grid-template-columns: repeat(auto-fill, minmax(7rem, 1fr));
|
|
185
|
+
gap: var(--astl-space-2);
|
|
186
|
+
}
|
|
187
|
+
.numbers label {
|
|
188
|
+
justify-content: flex-end;
|
|
189
|
+
}
|
|
190
|
+
.upload {
|
|
191
|
+
display: flex;
|
|
192
|
+
flex-direction: column;
|
|
193
|
+
align-items: flex-start;
|
|
194
|
+
}
|
|
195
|
+
.upload :is(.note, .error) {
|
|
196
|
+
margin-block-start: var(--astl-space-2);
|
|
197
|
+
}
|
|
198
|
+
.sources {
|
|
199
|
+
display: flex;
|
|
200
|
+
flex-wrap: wrap;
|
|
201
|
+
gap: var(--astl-space-2);
|
|
202
|
+
}
|
|
203
|
+
.error {
|
|
204
|
+
padding: var(--astl-space-2);
|
|
205
|
+
border-inline-start: var(--astl-space-1) solid var(--astl-danger);
|
|
206
|
+
background: var(--astl-danger-soft);
|
|
207
|
+
}
|
|
208
|
+
.vertex {
|
|
209
|
+
display: flex;
|
|
210
|
+
flex-wrap: wrap;
|
|
211
|
+
align-items: flex-end;
|
|
212
|
+
gap: var(--astl-space-2);
|
|
213
|
+
}
|
|
214
|
+
.vertex label {
|
|
215
|
+
flex: 1 1 6rem;
|
|
216
|
+
}
|
|
217
|
+
.c1 { --line: var(--astl-palette-1); --soft: var(--astl-palette-1-soft); }
|
|
218
|
+
.c2 { --line: var(--astl-palette-2); --soft: var(--astl-palette-2-soft); }
|
|
219
|
+
.c3 { --line: var(--astl-palette-3); --soft: var(--astl-palette-3-soft); }
|
|
220
|
+
.c4 { --line: var(--astl-palette-4); --soft: var(--astl-palette-4-soft); }
|
|
221
|
+
.c5 { --line: var(--astl-palette-5); --soft: var(--astl-palette-5-soft); }
|
|
222
|
+
.c6 { --line: var(--astl-palette-6); --soft: var(--astl-palette-6-soft); }
|
|
223
|
+
.c7 { --line: var(--astl-palette-7); --soft: var(--astl-palette-7-soft); }
|
|
224
|
+
.c8 { --line: var(--astl-palette-8); --soft: var(--astl-palette-8-soft); }
|
|
225
|
+
@media (forced-colors: active) {
|
|
226
|
+
.shape {
|
|
227
|
+
fill: none;
|
|
228
|
+
stroke: CanvasText;
|
|
229
|
+
}
|
|
230
|
+
.badge,
|
|
231
|
+
.swatch {
|
|
232
|
+
border-color: CanvasText;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
`,
|
|
236
|
+
];
|
|
237
|
+
/** A zone being drawn: where the pointer went down and where it is, in percent. */
|
|
238
|
+
#draft = null;
|
|
239
|
+
/** Width over height of the picture (1 until it loads), so circles stay round. */
|
|
240
|
+
#aspect = 1;
|
|
241
|
+
#status = null;
|
|
242
|
+
/** Aborts the upload or the pick under way (its `signal`). */
|
|
243
|
+
#abort = null;
|
|
244
|
+
constructor() {
|
|
245
|
+
super();
|
|
246
|
+
this.field = null;
|
|
247
|
+
this.authoring = null;
|
|
248
|
+
this.element = null;
|
|
249
|
+
}
|
|
250
|
+
willUpdate(changed) {
|
|
251
|
+
// Another element's field: an upload message was about the one before.
|
|
252
|
+
if (changed.has('element') && changed.get('element') !== undefined)
|
|
253
|
+
this.#status = null;
|
|
254
|
+
}
|
|
255
|
+
#t(key, params) {
|
|
256
|
+
return this.authoring?.t(EDITOR_NAMESPACE, key, params) ?? key;
|
|
257
|
+
}
|
|
258
|
+
#change(change, element = this.element) {
|
|
259
|
+
this.dispatchEvent(new CustomEvent(FIELD_CHANGE, { detail: { change, element } }));
|
|
260
|
+
}
|
|
261
|
+
/** Whether the URL policy allows the picture, asked as the engine and the player ask it: the element's attribute. */
|
|
262
|
+
#allowed(field, src) {
|
|
263
|
+
const context = { tag: field.elementTag, attribute: field.srcAttribute };
|
|
264
|
+
return src.trim() !== '' && (this.authoring?.urlPolicy(src, context) ?? false);
|
|
265
|
+
}
|
|
266
|
+
/** A fresh local id for a new child: `<tag without astl->-<n>`. */
|
|
267
|
+
#freshId(field) {
|
|
268
|
+
const taken = new Set(field.kind === 'picture-points' ? field.points.map((p) => p.id) : field.regions.map((r) => r.id));
|
|
269
|
+
const prefix = field.tag.replace(/^astl-/, '');
|
|
270
|
+
let n = 1;
|
|
271
|
+
while (taken.has(`${prefix}-${n}`))
|
|
272
|
+
n++;
|
|
273
|
+
return `${prefix}-${n}`;
|
|
274
|
+
}
|
|
275
|
+
#add(field, at) {
|
|
276
|
+
const id = this.#freshId(field);
|
|
277
|
+
if (field.kind === 'picture-points') {
|
|
278
|
+
const label = this.#t('field.point', { n: field.points.length + 1 });
|
|
279
|
+
this.#change({
|
|
280
|
+
kind: 'child-add',
|
|
281
|
+
tag: field.tag,
|
|
282
|
+
attributes: { id, x: num(at?.x ?? 50), y: num(at?.y ?? 50), label },
|
|
283
|
+
});
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const width = at?.width ?? 20;
|
|
287
|
+
const height = at?.height ?? 20;
|
|
288
|
+
const x = at === undefined ? 10 : at.x;
|
|
289
|
+
const y = at === undefined ? 10 : at.y;
|
|
290
|
+
// A zone that takes no other shape is written without one.
|
|
291
|
+
const shape = field.shapes.length > 1 ? { shape: 'rect' } : {};
|
|
292
|
+
this.#change({
|
|
293
|
+
kind: 'child-add',
|
|
294
|
+
tag: field.tag,
|
|
295
|
+
attributes: { id, ...shape, x: num(x), y: num(y), width: num(width), height: num(height) },
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
/** Writes the address of an uploaded or picked picture into the field's attribute of `element`, if the policy allows. */
|
|
299
|
+
#write(field, asset, element) {
|
|
300
|
+
const src = typeof asset?.src === 'string' ? asset.src.trim() : '';
|
|
301
|
+
if (!this.#allowed(field, src))
|
|
302
|
+
return false;
|
|
303
|
+
this.#change({ kind: 'attribute', name: field.srcAttribute, value: src }, element);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
async #upload(field, file) {
|
|
307
|
+
const upload = this.authoring?.uploadAsset ?? null;
|
|
308
|
+
if (upload === null)
|
|
309
|
+
return;
|
|
310
|
+
// The address goes to the element of this field, wherever the cursor is when the upload ends.
|
|
311
|
+
const element = this.element;
|
|
312
|
+
// Another upload or pick replaces this upload.
|
|
313
|
+
this.#abort?.abort();
|
|
314
|
+
const abort = new AbortController();
|
|
315
|
+
this.#abort = abort;
|
|
316
|
+
this.#status = { key: 'picture.uploading', error: false };
|
|
317
|
+
this.requestUpdate();
|
|
318
|
+
let status;
|
|
319
|
+
try {
|
|
320
|
+
const asset = await upload(file, { signal: abort.signal });
|
|
321
|
+
if (abort.signal.aborted)
|
|
322
|
+
return;
|
|
323
|
+
status = this.#write(field, asset, element)
|
|
324
|
+
? { key: 'picture.uploaded', error: false }
|
|
325
|
+
: { key: 'picture.uploadBlocked', error: true };
|
|
326
|
+
}
|
|
327
|
+
catch (error) {
|
|
328
|
+
if (abort.signal.aborted)
|
|
329
|
+
return;
|
|
330
|
+
status = { key: 'picture.uploadFailed', error: true, text: refusalOf(error) };
|
|
331
|
+
}
|
|
332
|
+
this.#abort = null;
|
|
333
|
+
// Said where it happened only: the field may show another element by now.
|
|
334
|
+
this.#status = this.element === element ? status : null;
|
|
335
|
+
this.requestUpdate();
|
|
336
|
+
}
|
|
337
|
+
/** Opens the host's library (`pickPicture`) and writes the address it gives, as an upload does. */
|
|
338
|
+
async #pick(field) {
|
|
339
|
+
const pick = this.authoring?.pickPicture ?? null;
|
|
340
|
+
if (pick === null)
|
|
341
|
+
return;
|
|
342
|
+
const element = this.element;
|
|
343
|
+
// Another upload or pick replaces this pick.
|
|
344
|
+
this.#abort?.abort();
|
|
345
|
+
const abort = new AbortController();
|
|
346
|
+
this.#abort = abort;
|
|
347
|
+
// What the field said was about an earlier upload or pick.
|
|
348
|
+
this.#status = null;
|
|
349
|
+
this.requestUpdate();
|
|
350
|
+
let status = null;
|
|
351
|
+
try {
|
|
352
|
+
const asset = await pick({ signal: abort.signal });
|
|
353
|
+
if (abort.signal.aborted)
|
|
354
|
+
return;
|
|
355
|
+
// Null: the author closed the library, and nothing changes.
|
|
356
|
+
if (asset != null) {
|
|
357
|
+
status = this.#write(field, asset, element)
|
|
358
|
+
? { key: 'picture.picked', error: false }
|
|
359
|
+
: { key: 'picture.pickBlocked', error: true };
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
if (abort.signal.aborted)
|
|
364
|
+
return;
|
|
365
|
+
status = { key: 'picture.pickFailed', error: true, text: refusalOf(error) };
|
|
366
|
+
}
|
|
367
|
+
this.#abort = null;
|
|
368
|
+
this.#status = this.element === element ? status : null;
|
|
369
|
+
this.requestUpdate();
|
|
370
|
+
// The focus goes back to the library's button, as the dialog gives it back: a host's modal restoring its own
|
|
371
|
+
// cannot see into this shadow root, and the author's place would be lost.
|
|
372
|
+
await this.updateComplete;
|
|
373
|
+
if (this.isConnected)
|
|
374
|
+
this.renderRoot.querySelector('[data-library]')?.focus();
|
|
375
|
+
}
|
|
376
|
+
/** Pointer position in percent of the picture: the canvas inside its border. */
|
|
377
|
+
#at(event) {
|
|
378
|
+
const canvas = event.currentTarget;
|
|
379
|
+
const box = canvas.getBoundingClientRect();
|
|
380
|
+
return {
|
|
381
|
+
x: round(((event.clientX - box.left - canvas.clientLeft) / Math.max(1, canvas.clientWidth)) * 100),
|
|
382
|
+
y: round(((event.clientY - box.top - canvas.clientTop) / Math.max(1, canvas.clientHeight)) * 100),
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
#down(event) {
|
|
386
|
+
if (event.button !== 0)
|
|
387
|
+
return;
|
|
388
|
+
const { x, y } = this.#at(event);
|
|
389
|
+
event.currentTarget.setPointerCapture?.(event.pointerId);
|
|
390
|
+
this.#draft = { x0: x, y0: y, x1: x, y1: y };
|
|
391
|
+
}
|
|
392
|
+
#move(event) {
|
|
393
|
+
if (this.#draft === null)
|
|
394
|
+
return;
|
|
395
|
+
const { x, y } = this.#at(event);
|
|
396
|
+
this.#draft.x1 = x;
|
|
397
|
+
this.#draft.y1 = y;
|
|
398
|
+
this.requestUpdate();
|
|
399
|
+
}
|
|
400
|
+
#up(field, event) {
|
|
401
|
+
const draft = this.#draft;
|
|
402
|
+
this.#draft = null;
|
|
403
|
+
if (draft === null)
|
|
404
|
+
return;
|
|
405
|
+
const { x, y } = this.#at(event);
|
|
406
|
+
if (field.kind === 'picture-points') {
|
|
407
|
+
this.#add(field, { x, y });
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const width = Math.abs(x - draft.x0);
|
|
411
|
+
const height = Math.abs(y - draft.y0);
|
|
412
|
+
// A click without a drag: a small zone around it.
|
|
413
|
+
if (width < 2 && height < 2)
|
|
414
|
+
this.#add(field, { x: draft.x0 - 5, y: draft.y0 - 5, width: 10, height: 10 });
|
|
415
|
+
else
|
|
416
|
+
this.#add(field, { x: Math.min(x, draft.x0), y: Math.min(y, draft.y0), width, height });
|
|
417
|
+
this.requestUpdate();
|
|
418
|
+
}
|
|
419
|
+
/** A zone in its palette colour: solid, or dashed without the field's flag (a wrong place among right ones). */
|
|
420
|
+
#shape(field, region) {
|
|
421
|
+
const unflagged = field.flag !== null && !region.flagged;
|
|
422
|
+
const cls = `shape ${slot(region.index)}${unflagged ? ' unflagged' : ''}`;
|
|
423
|
+
if (region.shape === 'circle') {
|
|
424
|
+
return svg `<ellipse class=${cls} cx=${region.x} cy=${region.y} rx=${region.r} ry=${region.r * this.#aspect}></ellipse>`;
|
|
425
|
+
}
|
|
426
|
+
if (region.shape === 'polygon') {
|
|
427
|
+
return svg `<polygon class=${cls} points=${region.points.map(([x, y]) => `${x},${y}`).join(' ')}></polygon>`;
|
|
428
|
+
}
|
|
429
|
+
return svg `<rect class=${cls} x=${region.x} y=${region.y} width=${region.width} height=${region.height}></rect>`;
|
|
430
|
+
}
|
|
431
|
+
#badge(index, [x, y], pin) {
|
|
432
|
+
return html `<span
|
|
433
|
+
class="badge ${pin ? 'pin ' : ''}${slot(index)}"
|
|
434
|
+
aria-hidden="true"
|
|
435
|
+
data-x=${x}
|
|
436
|
+
data-y=${y}
|
|
437
|
+
>${index + 1}</span
|
|
438
|
+
>`;
|
|
439
|
+
}
|
|
440
|
+
updated(changed) {
|
|
441
|
+
super.updated(changed);
|
|
442
|
+
// Badges are placed through the CSSOM: a strict Content Security Policy blocks a style attribute, which Lit's
|
|
443
|
+
// styleMap writes on its first render.
|
|
444
|
+
for (const badge of this.renderRoot.querySelectorAll('.badge')) {
|
|
445
|
+
badge.style.setProperty('--x', `${badge.dataset.x}%`);
|
|
446
|
+
badge.style.setProperty('--y', `${badge.dataset.y}%`);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
#canvas(field) {
|
|
450
|
+
const draft = this.#draft;
|
|
451
|
+
const shown = this.#allowed(field, field.src);
|
|
452
|
+
const shapes = field.kind === 'picture-regions' ? field.regions.map((r) => this.#shape(field, r)) : [];
|
|
453
|
+
const badges = field.kind === 'picture-regions'
|
|
454
|
+
? field.regions.map((r) => this.#badge(r.index, anchorOf(r), false))
|
|
455
|
+
: field.points.map((p) => this.#badge(p.index, [p.x, p.y], true));
|
|
456
|
+
return html `<div
|
|
457
|
+
class="canvas"
|
|
458
|
+
dir="ltr"
|
|
459
|
+
@pointerdown=${(event) => this.#down(event)}
|
|
460
|
+
@pointermove=${(event) => this.#move(event)}
|
|
461
|
+
@pointerup=${(event) => this.#up(field, event)}
|
|
462
|
+
@pointercancel=${() => {
|
|
463
|
+
this.#draft = null;
|
|
464
|
+
this.requestUpdate();
|
|
465
|
+
}}
|
|
466
|
+
>
|
|
467
|
+
${shown
|
|
468
|
+
? html `<img
|
|
469
|
+
src=${field.src}
|
|
470
|
+
alt=""
|
|
471
|
+
referrerpolicy="no-referrer"
|
|
472
|
+
draggable="false"
|
|
473
|
+
@load=${(event) => {
|
|
474
|
+
const img = event.target;
|
|
475
|
+
if (img.naturalHeight > 0)
|
|
476
|
+
this.#aspect = img.naturalWidth / img.naturalHeight;
|
|
477
|
+
this.requestUpdate();
|
|
478
|
+
}}
|
|
479
|
+
/>`
|
|
480
|
+
: html `<div class="blank"></div>`}
|
|
481
|
+
<svg viewBox="0 0 100 100" preserveAspectRatio="none" aria-hidden="true" focusable="false">
|
|
482
|
+
${shapes}
|
|
483
|
+
${draft === null || field.kind === 'picture-points'
|
|
484
|
+
? nothing
|
|
485
|
+
: svg `<rect class="draft" x=${Math.min(draft.x0, draft.x1)} y=${Math.min(draft.y0, draft.y1)}
|
|
486
|
+
width=${Math.abs(draft.x1 - draft.x0)} height=${Math.abs(draft.y1 - draft.y0)}></rect>`}
|
|
487
|
+
</svg>
|
|
488
|
+
${badges}
|
|
489
|
+
</div>`;
|
|
490
|
+
}
|
|
491
|
+
/** Says that only the first items are listed, when there are more. */
|
|
492
|
+
#more(count) {
|
|
493
|
+
return count > MAX_LISTED
|
|
494
|
+
? html `<p class="note">${this.#t('field.more', { shown: MAX_LISTED, count })}</p>`
|
|
495
|
+
: nothing;
|
|
496
|
+
}
|
|
497
|
+
#number(label, value, commit) {
|
|
498
|
+
return html `<label>
|
|
499
|
+
${label}
|
|
500
|
+
<input
|
|
501
|
+
type="number"
|
|
502
|
+
min="0"
|
|
503
|
+
max="100"
|
|
504
|
+
step="0.1"
|
|
505
|
+
.value=${num(value)}
|
|
506
|
+
@change=${(event) => {
|
|
507
|
+
const raw = event.target.value;
|
|
508
|
+
if (raw !== '' && Number.isFinite(Number(raw)))
|
|
509
|
+
commit(num(Number(raw)));
|
|
510
|
+
}}
|
|
511
|
+
/>
|
|
512
|
+
</label>`;
|
|
513
|
+
}
|
|
514
|
+
#labelField(field, index, value) {
|
|
515
|
+
const optional = field.kind === 'picture-regions';
|
|
516
|
+
return html `<label>
|
|
517
|
+
${this.#t(optional ? 'field.label.optional' : 'field.label')}
|
|
518
|
+
<input
|
|
519
|
+
type="text"
|
|
520
|
+
dir="auto"
|
|
521
|
+
.value=${value}
|
|
522
|
+
@change=${(event) => {
|
|
523
|
+
const text = event.target.value.trim();
|
|
524
|
+
this.#change({
|
|
525
|
+
kind: 'child-attributes',
|
|
526
|
+
tag: field.tag,
|
|
527
|
+
index,
|
|
528
|
+
attributes: { label: text === '' && optional ? null : text },
|
|
529
|
+
});
|
|
530
|
+
}}
|
|
531
|
+
/>
|
|
532
|
+
</label>`;
|
|
533
|
+
}
|
|
534
|
+
#region(field, region) {
|
|
535
|
+
const n = region.index + 1;
|
|
536
|
+
const set = (attributes) => this.#change({ kind: 'child-attributes', tag: field.tag, index: region.index, attributes });
|
|
537
|
+
const points = region.points;
|
|
538
|
+
const setPoints = (next) => set({ points: next.map(([x, y]) => `${num(x)},${num(y)}`).join(' ') });
|
|
539
|
+
const numbers = region.shape === 'circle'
|
|
540
|
+
? [
|
|
541
|
+
this.#number(this.#t('field.cx'), region.x, (x) => set({ x })),
|
|
542
|
+
this.#number(this.#t('field.cy'), region.y, (y) => set({ y })),
|
|
543
|
+
this.#number(this.#t('field.r'), region.r, (r) => set({ r })),
|
|
544
|
+
]
|
|
545
|
+
: region.shape === 'rect'
|
|
546
|
+
? [
|
|
547
|
+
this.#number(this.#t('field.x'), region.x, (x) => set({ x })),
|
|
548
|
+
this.#number(this.#t('field.y'), region.y, (y) => set({ y })),
|
|
549
|
+
this.#number(this.#t('field.width'), region.width, (width) => set({ width })),
|
|
550
|
+
this.#number(this.#t('field.height'), region.height, (height) => set({ height })),
|
|
551
|
+
]
|
|
552
|
+
: [];
|
|
553
|
+
return html `<li data-index=${region.index}>
|
|
554
|
+
<fieldset>
|
|
555
|
+
<legend><span class="swatch ${slot(region.index)}" aria-hidden="true">${n}</span>${this.#t('field.region', { n })}</legend>
|
|
556
|
+
${
|
|
557
|
+
// Only zones that take more than one shape choose theirs.
|
|
558
|
+
field.shapes.length > 1
|
|
559
|
+
? html `<label>
|
|
560
|
+
${this.#t('field.shape')}
|
|
561
|
+
<select
|
|
562
|
+
@change=${(event) => set(reshaped(region, event.target.value))}
|
|
563
|
+
>
|
|
564
|
+
${field.shapes.map((shape) => html `<option value=${shape} ?selected=${shape === region.shape}>${this.#t(`field.shape.${shape}`)}</option>`)}
|
|
565
|
+
</select>
|
|
566
|
+
</label>`
|
|
567
|
+
: nothing}
|
|
568
|
+
${numbers.length > 0 ? html `<div class="numbers">${numbers}</div>` : nothing}
|
|
569
|
+
${region.shape === 'polygon'
|
|
570
|
+
? html `<ol class="items">
|
|
571
|
+
${points.slice(0, MAX_LISTED).map(([x, y], k) => html `<li class="vertex">
|
|
572
|
+
${this.#number(this.#t('field.vertex.x', { n: k + 1 }), x, (value) => setPoints(points.map((p, j) => (j === k ? [Number(value), p[1]] : p))))}
|
|
573
|
+
${this.#number(this.#t('field.vertex.y', { n: k + 1 }), y, (value) => setPoints(points.map((p, j) => (j === k ? [p[0], Number(value)] : p))))}
|
|
574
|
+
<button
|
|
575
|
+
type="button"
|
|
576
|
+
?disabled=${points.length <= 3}
|
|
577
|
+
@click=${() => setPoints(points.filter((_, j) => j !== k))}
|
|
578
|
+
>
|
|
579
|
+
${this.#t('field.vertex.remove', { n: k + 1 })}
|
|
580
|
+
</button>
|
|
581
|
+
</li>`)}
|
|
582
|
+
</ol>
|
|
583
|
+
${this.#more(points.length)}
|
|
584
|
+
<button
|
|
585
|
+
type="button"
|
|
586
|
+
?disabled=${points.length >= MAX_LISTED}
|
|
587
|
+
@click=${() => {
|
|
588
|
+
const [lx, ly] = points[points.length - 1] ?? [50, 50];
|
|
589
|
+
setPoints([...points, [round(lx + 5), round(ly + 5)]]);
|
|
590
|
+
}}
|
|
591
|
+
>
|
|
592
|
+
${this.#t('field.vertex.add')}
|
|
593
|
+
</button>`
|
|
594
|
+
: nothing}
|
|
595
|
+
${this.#labelField(field, region.index, region.label)}
|
|
596
|
+
<button type="button" @click=${() => this.#change({ kind: 'child-remove', tag: field.tag, index: region.index })}>
|
|
597
|
+
${this.#t('field.region.remove', { n })}
|
|
598
|
+
</button>
|
|
599
|
+
</fieldset>
|
|
600
|
+
</li>`;
|
|
601
|
+
}
|
|
602
|
+
#point(field, point) {
|
|
603
|
+
const n = point.index + 1;
|
|
604
|
+
const set = (attributes) => this.#change({ kind: 'child-attributes', tag: field.tag, index: point.index, attributes });
|
|
605
|
+
return html `<li data-index=${point.index}>
|
|
606
|
+
<fieldset>
|
|
607
|
+
<legend><span class="swatch ${slot(point.index)}" aria-hidden="true">${n}</span>${this.#t('field.point', { n })}</legend>
|
|
608
|
+
<div class="numbers">
|
|
609
|
+
${this.#number(this.#t('field.x'), point.x, (x) => set({ x }))}
|
|
610
|
+
${this.#number(this.#t('field.y'), point.y, (y) => set({ y }))}
|
|
611
|
+
</div>
|
|
612
|
+
${this.#labelField(field, point.index, point.label)}
|
|
613
|
+
<button type="button" @click=${() => this.#change({ kind: 'child-remove', tag: field.tag, index: point.index })}>
|
|
614
|
+
${this.#t('field.point.remove', { n })}
|
|
615
|
+
</button>
|
|
616
|
+
</fieldset>
|
|
617
|
+
</li>`;
|
|
618
|
+
}
|
|
619
|
+
render() {
|
|
620
|
+
const field = this.field;
|
|
621
|
+
if (field === null || this.authoring === null)
|
|
622
|
+
return nothing;
|
|
623
|
+
const regions = field.kind === 'picture-regions';
|
|
624
|
+
const upload = this.authoring.uploadAsset !== null;
|
|
625
|
+
const library = this.authoring.pickPicture !== null;
|
|
626
|
+
const status = this.#status;
|
|
627
|
+
const said = status === null ? '' : (status.text ?? this.#t(status.key));
|
|
628
|
+
return html `<fieldset data-tag=${field.tag}>
|
|
629
|
+
<legend>${field.label}</legend>
|
|
630
|
+
${field.help === null ? nothing : html `<p class="note">${field.help}</p>`}
|
|
631
|
+
<label>
|
|
632
|
+
${this.#t('field.picture.src')}
|
|
633
|
+
<input
|
|
634
|
+
type="url"
|
|
635
|
+
autocomplete="off"
|
|
636
|
+
.value=${field.src}
|
|
637
|
+
@change=${(event) => {
|
|
638
|
+
const value = event.target.value.trim();
|
|
639
|
+
this.#change({ kind: 'attribute', name: field.srcAttribute, value: value === '' ? null : value });
|
|
640
|
+
}}
|
|
641
|
+
/>
|
|
642
|
+
</label>
|
|
643
|
+
${upload || library
|
|
644
|
+
? html `<div class="upload">
|
|
645
|
+
<div class="sources">
|
|
646
|
+
${upload
|
|
647
|
+
? html `<button
|
|
648
|
+
type="button"
|
|
649
|
+
@click=${() => this.renderRoot.querySelector('input[type="file"]')?.click()}
|
|
650
|
+
>
|
|
651
|
+
${this.#t('picture.file')}
|
|
652
|
+
</button>`
|
|
653
|
+
: nothing}
|
|
654
|
+
${library
|
|
655
|
+
? html `<button type="button" data-library @click=${() => void this.#pick(field)}>
|
|
656
|
+
${this.#t('picture.library')}
|
|
657
|
+
</button>`
|
|
658
|
+
: nothing}
|
|
659
|
+
</div>
|
|
660
|
+
${upload
|
|
661
|
+
? html `<input
|
|
662
|
+
type="file"
|
|
663
|
+
accept="image/*"
|
|
664
|
+
hidden
|
|
665
|
+
@change=${(event) => {
|
|
666
|
+
const input = event.target;
|
|
667
|
+
const file = input.files?.[0];
|
|
668
|
+
input.value = '';
|
|
669
|
+
if (file !== undefined)
|
|
670
|
+
void this.#upload(field, file);
|
|
671
|
+
}}
|
|
672
|
+
/>`
|
|
673
|
+
: nothing}
|
|
674
|
+
<div role="status">${status !== null && !status.error ? html `<p class="note">${said}</p>` : nothing}</div>
|
|
675
|
+
<div role="alert">${status?.error ? html `<p class="error">${said}</p>` : nothing}</div>
|
|
676
|
+
</div>`
|
|
677
|
+
: html `<p class="note">${this.#t('picture.urlOnly')}</p>`}
|
|
678
|
+
${this.#canvas(field)}
|
|
679
|
+
<p class="note">${this.#t(regions ? 'field.regions.draw' : 'field.points.place')}</p>
|
|
680
|
+
<ol class="items">
|
|
681
|
+
${regions
|
|
682
|
+
? field.regions.slice(0, MAX_LISTED).map((r) => this.#region(field, r))
|
|
683
|
+
: field.points.slice(0, MAX_LISTED).map((p) => this.#point(field, p))}
|
|
684
|
+
</ol>
|
|
685
|
+
${this.#more(regions ? field.regions.length : field.points.length)}
|
|
686
|
+
<button type="button" data-add @click=${() => this.#add(field)}>
|
|
687
|
+
${this.#t(regions ? 'field.region.add' : 'field.point.add')}
|
|
688
|
+
</button>
|
|
689
|
+
</fieldset>`;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
//# sourceMappingURL=picture-field.js.map
|