@commmerce/ui 1.2.0 → 1.4.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/THIRD_PARTY_NOTICES.txt +101 -0
- package/dist/cdn.js +1144 -547
- package/dist/components/app-frame.d.ts +1 -0
- package/dist/components/app-frame.js +2 -1
- package/dist/components/board.d.ts +338 -0
- package/dist/components/board.js +1017 -0
- package/dist/components/calendar.d.ts +380 -0
- package/dist/components/calendar.js +1479 -0
- package/dist/components/card.d.ts +2 -1
- package/dist/components/card.js +16 -4
- package/dist/components/color-field.d.ts +153 -0
- package/dist/components/color-field.js +369 -0
- package/dist/components/combobox.d.ts +17 -4
- package/dist/components/combobox.js +55 -23
- package/dist/components/file-upload.d.ts +33 -0
- package/dist/components/file-upload.js +80 -9
- package/dist/components/global-search.js +2 -7
- package/dist/components/nested-list.d.ts +33 -1
- package/dist/components/nested-list.js +197 -18
- package/dist/components/option-group.d.ts +15 -6
- package/dist/components/option-group.js +25 -6
- package/dist/components/select.d.ts +20 -3
- package/dist/components/select.js +35 -11
- package/dist/components/signature-field.d.ts +153 -0
- package/dist/components/signature-field.js +726 -0
- package/dist/custom-elements.json +11895 -6087
- package/dist/index.d.ts +10 -0
- package/dist/index.js +5 -0
- package/dist/move-model.d.ts +93 -0
- package/dist/move-model.js +169 -0
- package/dist/popover.d.ts +24 -0
- package/dist/popover.js +59 -0
- package/package.json +3 -3
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _CmSignatureField_instances, _CmSignatureField_internals, _CmSignatureField_id, _CmSignatureField_defaultValue, _CmSignatureField_defaultTyped, _CmSignatureField_strokes, _CmSignatureField_active, _CmSignatureField_image, _CmSignatureField_own, _CmSignatureField_frame, _CmSignatureField_typeTimer, _CmSignatureField_typeFrom, _CmSignatureField_resize, _CmSignatureField_width, _CmSignatureField_observe, _CmSignatureField_canvas_get, _CmSignatureField_fit, _CmSignatureField_draw, _CmSignatureField_paint, _CmSignatureField_render, _CmSignatureField_point, _CmSignatureField_onDown, _CmSignatureField_onMove, _CmSignatureField_onUp, _CmSignatureField_fontFamily_get, _CmSignatureField_loadFont, _CmSignatureField_onType, _CmSignatureField_typed, _CmSignatureField_onTypeChange, _CmSignatureField_sync, _CmSignatureField_emit, _CmSignatureField_export, _CmSignatureField_select, _CmSignatureField_onTabKey, _CmSignatureField_say, _CmSignatureField_announce, _CmSignatureField_keepFocus, _CmSignatureField_onUndo, _CmSignatureField_onClear;
|
|
13
|
+
import { LitElement, html, css, nothing } from 'lit';
|
|
14
|
+
import { live } from 'lit/directives/live.js';
|
|
15
|
+
import { base, define, nextId, logicalKey } from '../base.js';
|
|
16
|
+
import { CmLocalizedElement, defineStrings } from '../localize.js';
|
|
17
|
+
const strings = defineStrings('signatureField', {
|
|
18
|
+
/** Name of the Draw / Type tab list when the field has no label. */
|
|
19
|
+
modes: 'How to sign',
|
|
20
|
+
/** The two tabs. */
|
|
21
|
+
draw: 'Draw',
|
|
22
|
+
type: 'Type',
|
|
23
|
+
/** Accessible name of the drawing area, and the same once something is drawn. */
|
|
24
|
+
pad: 'Signature drawing area',
|
|
25
|
+
padSigned: 'Signature drawing area, signed',
|
|
26
|
+
/** Read with the drawing area: how to draw, and the keyboard alternative. */
|
|
27
|
+
instructions: 'Draw your signature with a mouse, finger or pen. To sign with the keyboard, choose Type.',
|
|
28
|
+
/** Hint above the line while nothing is drawn. */
|
|
29
|
+
signHere: 'Sign here',
|
|
30
|
+
/** Accessible name and placeholder of the box in Type mode. */
|
|
31
|
+
typeLabel: 'Type your full name',
|
|
32
|
+
typePlaceholder: 'Type your full name',
|
|
33
|
+
/** Undo and Clear buttons: visible text and accessible names. */
|
|
34
|
+
undo: 'Undo',
|
|
35
|
+
undoLabel: 'Undo last stroke',
|
|
36
|
+
clear: 'Clear',
|
|
37
|
+
clearLabel: 'Clear signature',
|
|
38
|
+
/** Announced after Clear and Undo. */
|
|
39
|
+
cleared: 'Signature cleared',
|
|
40
|
+
undone: 'Last stroke removed',
|
|
41
|
+
/** Validation message when `required` and nothing is signed. */
|
|
42
|
+
required: 'Please sign here.',
|
|
43
|
+
});
|
|
44
|
+
/** Width of the pad over its height. */
|
|
45
|
+
const RATIO = 3;
|
|
46
|
+
/** Where the signing line sits, as a fraction of the pad's height from the top. */
|
|
47
|
+
const BASELINE = 0.7;
|
|
48
|
+
/** Stroke width at pressure 0.5, as a fraction of the pad's width (3.3px on a 600px pad). */
|
|
49
|
+
const LINE = 0.0055;
|
|
50
|
+
/**
|
|
51
|
+
* Ink of the exported image. Always the light theme's `text-default` (black) on transparent, whatever the theme on
|
|
52
|
+
* screen: a signature goes into documents and PDFs, which are light. On screen the ink is `text-default` of the
|
|
53
|
+
* current theme, so it stays visible in dark.
|
|
54
|
+
*/
|
|
55
|
+
const EXPORT_INK = '#000000';
|
|
56
|
+
/** Space around the ink in the exported image, in CSS pixels (doubled with the 2x scale). */
|
|
57
|
+
const PADDING = 8;
|
|
58
|
+
/** Resolution of the exported image. */
|
|
59
|
+
const SCALE = 2;
|
|
60
|
+
/** Typing waits this long before `change` / `cm-change`. */
|
|
61
|
+
const TYPE_DELAY = 400;
|
|
62
|
+
const mid = (a, b) => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, p: (a.p + b.p) / 2 });
|
|
63
|
+
/**
|
|
64
|
+
* Signature field: sign by drawing with a mouse, finger or pen, or by typing a name that is shown in a handwriting
|
|
65
|
+
* font. Gives a PNG of the signature, cropped to the ink, as a data URL (`value`, `toDataURL()`) or a Blob
|
|
66
|
+
* (`toBlob()`). Participates in native forms: submits the data URL under `name`; `required` blocks submission
|
|
67
|
+
* until something is signed. For e-sign screens ("Sign now") and signing pages.
|
|
68
|
+
*
|
|
69
|
+
* Load `@commmerce/tokens/fonts.css` (the hosted build's fonts.css) for the typed signature's font, Dancing Script;
|
|
70
|
+
* without it, typed signatures use the system's cursive font.
|
|
71
|
+
* @element cm-signature-field
|
|
72
|
+
* @fires cm-change - After a stroke is finished, Undo, Clear, a switch between Draw and Type, or typing stops for 400ms. `detail`: `{ value, empty, mode }`. A plain `change` event (no detail) follows it, for code that treats every form field alike.
|
|
73
|
+
* @csspart label - The label.
|
|
74
|
+
* @csspart tabs - The Draw / Type tab list.
|
|
75
|
+
* @csspart pad - The signing area (both modes).
|
|
76
|
+
* @csspart canvas - The drawing canvas.
|
|
77
|
+
* @csspart input - The text box in Type mode.
|
|
78
|
+
* @csspart undo - The Undo button (Draw mode).
|
|
79
|
+
* @csspart clear - The Clear button.
|
|
80
|
+
* @cssprop --cm-signature-font - Font of typed signatures. Default: Dancing Script, then cursive system fonts.
|
|
81
|
+
*/
|
|
82
|
+
export class CmSignatureField extends CmLocalizedElement {
|
|
83
|
+
constructor() {
|
|
84
|
+
super(...arguments);
|
|
85
|
+
_CmSignatureField_instances.add(this);
|
|
86
|
+
/** Visible label above the field, e.g. "Your signature". Without one, give the element an `aria-label`. */
|
|
87
|
+
this.label = '';
|
|
88
|
+
/** Lowercase bracketed note after the label. */
|
|
89
|
+
this.hint = '';
|
|
90
|
+
/** One sentence under the field in text-tertiary, e.g. what the signer agrees to. */
|
|
91
|
+
this.helpText = '';
|
|
92
|
+
/** Error message from your own validation; turns the pad's border critical and is announced. */
|
|
93
|
+
this.error = '';
|
|
94
|
+
/** Form field name. */
|
|
95
|
+
this.name = '';
|
|
96
|
+
/**
|
|
97
|
+
* The signature as a PNG data URL (`data:image/png;base64,…`), cropped to the ink, or empty when nothing is
|
|
98
|
+
* signed. Updated after every stroke and keystroke. Set it to a PNG or SVG data URL (or any image URL the page
|
|
99
|
+
* may read) to show a saved signature in Draw mode; set it to `''` to clear both modes.
|
|
100
|
+
*/
|
|
101
|
+
this.value = '';
|
|
102
|
+
/** The selected tab: `draw` (pointer, touch, pen) or `type` (keyboard). Each keeps its own signature. */
|
|
103
|
+
this.mode = 'draw';
|
|
104
|
+
/** The name typed in Type mode. Set it to prefill the box with the signer's name. */
|
|
105
|
+
this.typedName = '';
|
|
106
|
+
/** Shows a red asterisk and blocks form submission until something is signed. */
|
|
107
|
+
this.required = false;
|
|
108
|
+
/** Blocks interaction. Also set by a disabled `<fieldset>`. */
|
|
109
|
+
this.disabled = false;
|
|
110
|
+
/** @internal Text in the polite status region. */
|
|
111
|
+
this._announce = '';
|
|
112
|
+
_CmSignatureField_internals.set(this, this.attachInternals());
|
|
113
|
+
_CmSignatureField_id.set(this, nextId('cm-sig'));
|
|
114
|
+
_CmSignatureField_defaultValue.set(this, '');
|
|
115
|
+
_CmSignatureField_defaultTyped.set(this, '');
|
|
116
|
+
_CmSignatureField_strokes.set(this, []);
|
|
117
|
+
/** The stroke being drawn and its pointer. */
|
|
118
|
+
_CmSignatureField_active.set(this, void 0);
|
|
119
|
+
/** A saved signature shown under the strokes (from `value`). */
|
|
120
|
+
_CmSignatureField_image.set(this, void 0);
|
|
121
|
+
/** The value this element produced itself; any other value set from outside is loaded as an image. */
|
|
122
|
+
_CmSignatureField_own.set(this, '');
|
|
123
|
+
_CmSignatureField_frame.set(this, 0);
|
|
124
|
+
_CmSignatureField_typeTimer.set(this, void 0);
|
|
125
|
+
/** The value when the current burst of typing started. */
|
|
126
|
+
_CmSignatureField_typeFrom.set(this, '');
|
|
127
|
+
_CmSignatureField_resize.set(this, void 0);
|
|
128
|
+
/** Last laid-out width of the pad, so exports from a hidden pad keep their size. */
|
|
129
|
+
_CmSignatureField_width.set(this, 0);
|
|
130
|
+
}
|
|
131
|
+
connectedCallback() {
|
|
132
|
+
super.connectedCallback();
|
|
133
|
+
if (!this.hasUpdated) {
|
|
134
|
+
__classPrivateFieldSet(this, _CmSignatureField_defaultValue, this.value, "f");
|
|
135
|
+
__classPrivateFieldSet(this, _CmSignatureField_defaultTyped, this.typedName, "f");
|
|
136
|
+
}
|
|
137
|
+
else
|
|
138
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_observe).call(this); // moved or re-added: watch the pad's size again
|
|
139
|
+
}
|
|
140
|
+
disconnectedCallback() {
|
|
141
|
+
super.disconnectedCallback();
|
|
142
|
+
__classPrivateFieldGet(this, _CmSignatureField_resize, "f")?.disconnect();
|
|
143
|
+
__classPrivateFieldSet(this, _CmSignatureField_resize, undefined, "f");
|
|
144
|
+
cancelAnimationFrame(__classPrivateFieldGet(this, _CmSignatureField_frame, "f"));
|
|
145
|
+
__classPrivateFieldSet(this, _CmSignatureField_frame, 0, "f");
|
|
146
|
+
if (__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f")) {
|
|
147
|
+
clearTimeout(__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"));
|
|
148
|
+
__classPrivateFieldSet(this, _CmSignatureField_typeTimer, undefined, "f");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
get form() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").form; }
|
|
152
|
+
get validity() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").validity; }
|
|
153
|
+
get validationMessage() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").validationMessage; }
|
|
154
|
+
checkValidity() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").checkValidity(); }
|
|
155
|
+
reportValidity() { return __classPrivateFieldGet(this, _CmSignatureField_internals, "f").reportValidity(); }
|
|
156
|
+
formResetCallback() {
|
|
157
|
+
__classPrivateFieldSet(this, _CmSignatureField_strokes, [], "f");
|
|
158
|
+
__classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
|
|
159
|
+
__classPrivateFieldSet(this, _CmSignatureField_image, undefined, "f");
|
|
160
|
+
if (__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f")) {
|
|
161
|
+
clearTimeout(__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"));
|
|
162
|
+
__classPrivateFieldSet(this, _CmSignatureField_typeTimer, undefined, "f");
|
|
163
|
+
}
|
|
164
|
+
this.typedName = __classPrivateFieldGet(this, _CmSignatureField_defaultTyped, "f");
|
|
165
|
+
if (__classPrivateFieldGet(this, _CmSignatureField_defaultValue, "f")) {
|
|
166
|
+
__classPrivateFieldSet(this, _CmSignatureField_own, '', "f");
|
|
167
|
+
this.value = __classPrivateFieldGet(this, _CmSignatureField_defaultValue, "f");
|
|
168
|
+
}
|
|
169
|
+
else
|
|
170
|
+
this.updateComplete.then(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, false));
|
|
171
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
|
|
172
|
+
}
|
|
173
|
+
formDisabledCallback(d) { this.disabled = d; }
|
|
174
|
+
formStateRestoreCallback(state) { this.value = state; }
|
|
175
|
+
/** True when the current mode has nothing signed: no strokes (or saved image) in Draw, no text in Type. */
|
|
176
|
+
get empty() {
|
|
177
|
+
return this.mode === 'type' ? !this.typedName.trim() : !__classPrivateFieldGet(this, _CmSignatureField_strokes, "f").length && !__classPrivateFieldGet(this, _CmSignatureField_image, "f");
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The signature as a PNG data URL: black ink on transparent at 2x the pad's size, cropped to the ink with 8px
|
|
181
|
+
* (16 image pixels) around it. Empty string when nothing is signed.
|
|
182
|
+
*/
|
|
183
|
+
toDataURL() {
|
|
184
|
+
return __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_export).call(this)?.toDataURL('image/png') ?? '';
|
|
185
|
+
}
|
|
186
|
+
/** The same PNG as `toDataURL()` as a Blob, for uploading. `null` when nothing is signed. */
|
|
187
|
+
toBlob() {
|
|
188
|
+
const c = __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_export).call(this);
|
|
189
|
+
return c ? new Promise((resolve) => c.toBlob(resolve, 'image/png')) : Promise.resolve(null);
|
|
190
|
+
}
|
|
191
|
+
/** Removes the current mode's signature (Draw: every stroke and any saved image; Type: the text). */
|
|
192
|
+
clear() {
|
|
193
|
+
if (this.mode === 'type')
|
|
194
|
+
this.typedName = '';
|
|
195
|
+
else {
|
|
196
|
+
__classPrivateFieldSet(this, _CmSignatureField_strokes, [], "f");
|
|
197
|
+
__classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
|
|
198
|
+
__classPrivateFieldSet(this, _CmSignatureField_image, undefined, "f");
|
|
199
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
|
|
200
|
+
}
|
|
201
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, true);
|
|
202
|
+
}
|
|
203
|
+
/** Removes the last stroke in Draw mode (then the saved image, if one was loaded). */
|
|
204
|
+
undo() {
|
|
205
|
+
if (this.mode !== 'draw')
|
|
206
|
+
return;
|
|
207
|
+
if (__classPrivateFieldGet(this, _CmSignatureField_strokes, "f").length)
|
|
208
|
+
__classPrivateFieldGet(this, _CmSignatureField_strokes, "f").pop();
|
|
209
|
+
else
|
|
210
|
+
__classPrivateFieldSet(this, _CmSignatureField_image, undefined, "f");
|
|
211
|
+
__classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
|
|
212
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
|
|
213
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, true);
|
|
214
|
+
}
|
|
215
|
+
willUpdate(changed) {
|
|
216
|
+
if (changed.has('value') && (this.value ?? '') !== __classPrivateFieldGet(this, _CmSignatureField_own, "f")) {
|
|
217
|
+
// Set from outside: '' clears both modes; anything else is a saved signature, shown in Draw.
|
|
218
|
+
const v = this.value ?? '';
|
|
219
|
+
__classPrivateFieldSet(this, _CmSignatureField_own, v, "f");
|
|
220
|
+
__classPrivateFieldSet(this, _CmSignatureField_strokes, [], "f");
|
|
221
|
+
__classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
|
|
222
|
+
if (!v) {
|
|
223
|
+
__classPrivateFieldSet(this, _CmSignatureField_image, undefined, "f");
|
|
224
|
+
if (this.hasUpdated)
|
|
225
|
+
this.typedName = '';
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
const img = new Image();
|
|
229
|
+
img.onload = () => { if (__classPrivateFieldGet(this, _CmSignatureField_image, "f") === img)
|
|
230
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this); };
|
|
231
|
+
img.src = v;
|
|
232
|
+
__classPrivateFieldSet(this, _CmSignatureField_image, img, "f");
|
|
233
|
+
this.mode = 'draw';
|
|
234
|
+
}
|
|
235
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
|
|
236
|
+
}
|
|
237
|
+
else if (this.hasUpdated && (changed.has('mode') || (changed.has('typedName') && this.mode === 'type'))) {
|
|
238
|
+
// Another tab, or a name set in code: the value follows (events are fired by the user actions only).
|
|
239
|
+
const next = this.empty ? '' : this.toDataURL();
|
|
240
|
+
__classPrivateFieldSet(this, _CmSignatureField_own, next, "f");
|
|
241
|
+
this.value = next;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
firstUpdated() {
|
|
245
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_observe).call(this);
|
|
246
|
+
// A typed signature exported before the font arrives would use the fallback font: load it now.
|
|
247
|
+
if (this.typedName && !this.value)
|
|
248
|
+
this.updateComplete.then(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, false));
|
|
249
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_loadFont).call(this);
|
|
250
|
+
}
|
|
251
|
+
updated(changed) {
|
|
252
|
+
if (changed.has('mode') && this.mode === 'type')
|
|
253
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_loadFont).call(this);
|
|
254
|
+
__classPrivateFieldGet(this, _CmSignatureField_internals, "f").setFormValue(this.value || null, this.value);
|
|
255
|
+
const anchor = (this.mode === 'type'
|
|
256
|
+
? this.renderRoot.querySelector('.typed')
|
|
257
|
+
: this.renderRoot.querySelector('.tab[aria-selected="true"]')) ?? undefined;
|
|
258
|
+
if (this.error)
|
|
259
|
+
__classPrivateFieldGet(this, _CmSignatureField_internals, "f").setValidity({ customError: true }, this.error, anchor);
|
|
260
|
+
else if (this.required && this.empty)
|
|
261
|
+
__classPrivateFieldGet(this, _CmSignatureField_internals, "f").setValidity({ valueMissing: true }, this.t('required'), anchor);
|
|
262
|
+
else
|
|
263
|
+
__classPrivateFieldGet(this, _CmSignatureField_internals, "f").setValidity({});
|
|
264
|
+
}
|
|
265
|
+
render() {
|
|
266
|
+
const id = __classPrivateFieldGet(this, _CmSignatureField_id, "f");
|
|
267
|
+
const hostLabel = this.getAttribute('aria-label');
|
|
268
|
+
const message = this.error;
|
|
269
|
+
const describedBy = [message && `${id}-err`, this.helpText && `${id}-help`].filter(Boolean).join(' ');
|
|
270
|
+
const empty = this.empty;
|
|
271
|
+
const drawEmpty = !__classPrivateFieldGet(this, _CmSignatureField_strokes, "f").length && !__classPrivateFieldGet(this, _CmSignatureField_image, "f");
|
|
272
|
+
const tab = (mode) => html `<button type="button" role="tab" class="tab" id="${id}-tab-${mode}"
|
|
273
|
+
aria-selected=${this.mode === mode ? 'true' : 'false'} aria-controls="${id}-panel-${mode}"
|
|
274
|
+
tabindex=${this.mode === mode ? '0' : '-1'} ?disabled=${this.disabled}
|
|
275
|
+
@click=${() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_select).call(this, mode, true)} @keydown=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onTabKey)}>${this.t(mode)}</button>`;
|
|
276
|
+
return html `<div class="field">
|
|
277
|
+
${this.label
|
|
278
|
+
? html `<span class="label" id="${id}-l" part="label">${this.label}${this.required ? html `<span class="req" aria-hidden="true">*</span>` : nothing}${this.hint ? html `<span class="hint">[${this.hint}]</span>` : nothing}</span>`
|
|
279
|
+
: nothing}
|
|
280
|
+
<div class="bar">
|
|
281
|
+
<div class="tabs" role="tablist" part="tabs"
|
|
282
|
+
aria-labelledby=${this.label ? `${id}-l` : nothing}
|
|
283
|
+
aria-label=${this.label ? nothing : hostLabel || this.t('modes')}>${tab('draw')}${tab('type')}</div>
|
|
284
|
+
<div class="actions">
|
|
285
|
+
${this.mode === 'draw'
|
|
286
|
+
? html `<button type="button" class="act" part="undo" aria-label=${this.t('undoLabel')} ?disabled=${this.disabled || drawEmpty} @click=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onUndo)}>${this.t('undo')}</button>`
|
|
287
|
+
: nothing}
|
|
288
|
+
<button type="button" class="act" part="clear" aria-label=${this.t('clearLabel')} ?disabled=${this.disabled || empty} @click=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onClear)}>${this.t('clear')}</button>
|
|
289
|
+
</div>
|
|
290
|
+
</div>
|
|
291
|
+
<div class="panel panel-draw" role="tabpanel" id="${id}-panel-draw" aria-labelledby="${id}-tab-draw"
|
|
292
|
+
tabindex=${this.disabled ? '-1' : '0'} ?hidden=${this.mode !== 'draw'}>
|
|
293
|
+
<div class="pad" part="pad">
|
|
294
|
+
<span class="line" aria-hidden="true"></span>
|
|
295
|
+
${drawEmpty ? html `<span class="here" aria-hidden="true">${this.t('signHere')}</span>` : nothing}
|
|
296
|
+
<canvas part="canvas" role="img" aria-label=${drawEmpty ? this.t('pad') : this.t('padSigned')}
|
|
297
|
+
aria-describedby=${[`${id}-instr`, describedBy].filter(Boolean).join(' ')}
|
|
298
|
+
@pointerdown=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onDown)} @pointermove=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onMove)} @pointerup=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onUp)} @pointercancel=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onUp)}
|
|
299
|
+
@lostpointercapture=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onUp)} @transitionend=${() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_paint).call(this)}></canvas>
|
|
300
|
+
</div>
|
|
301
|
+
<span class="vh" id="${id}-instr">${this.t('instructions')}</span>
|
|
302
|
+
</div>
|
|
303
|
+
<div class="panel panel-type" role="tabpanel" id="${id}-panel-type" aria-labelledby="${id}-tab-type" ?hidden=${this.mode !== 'type'}>
|
|
304
|
+
<div class="pad" part="pad">
|
|
305
|
+
<span class="line" aria-hidden="true"></span>
|
|
306
|
+
<input class="typed" part="input" type="text" autocomplete="name" spellcheck="false" maxlength="80"
|
|
307
|
+
.value=${live(this.typedName ?? '')} placeholder=${this.t('typePlaceholder')}
|
|
308
|
+
aria-label=${this.label ? `${this.label}: ${this.t('typeLabel')}` : this.t('typeLabel')}
|
|
309
|
+
aria-required=${this.required ? 'true' : nothing} aria-invalid=${message ? 'true' : nothing}
|
|
310
|
+
aria-describedby=${describedBy || nothing} ?disabled=${this.disabled}
|
|
311
|
+
@input=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onType)} @change=${__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onTypeChange)} />
|
|
312
|
+
</div>
|
|
313
|
+
</div>
|
|
314
|
+
${message ? html `<p class="error" id="${id}-err" role="alert">${message}</p>` : nothing}
|
|
315
|
+
${this.helpText ? html `<p class="help" id="${id}-help">${this.helpText}</p>` : nothing}
|
|
316
|
+
<div class="vh" role="status" aria-live="polite">${this._announce}</div>
|
|
317
|
+
</div>`;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
_CmSignatureField_internals = new WeakMap(), _CmSignatureField_id = new WeakMap(), _CmSignatureField_defaultValue = new WeakMap(), _CmSignatureField_defaultTyped = new WeakMap(), _CmSignatureField_strokes = new WeakMap(), _CmSignatureField_active = new WeakMap(), _CmSignatureField_image = new WeakMap(), _CmSignatureField_own = new WeakMap(), _CmSignatureField_frame = new WeakMap(), _CmSignatureField_typeTimer = new WeakMap(), _CmSignatureField_typeFrom = new WeakMap(), _CmSignatureField_resize = new WeakMap(), _CmSignatureField_width = new WeakMap(), _CmSignatureField_instances = new WeakSet(), _CmSignatureField_observe = function _CmSignatureField_observe() {
|
|
321
|
+
const pad = this.renderRoot.querySelector('.panel-draw .pad');
|
|
322
|
+
if (!pad || __classPrivateFieldGet(this, _CmSignatureField_resize, "f"))
|
|
323
|
+
return;
|
|
324
|
+
__classPrivateFieldSet(this, _CmSignatureField_resize, new ResizeObserver(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_fit).call(this)), "f");
|
|
325
|
+
__classPrivateFieldGet(this, _CmSignatureField_resize, "f").observe(pad);
|
|
326
|
+
}, _CmSignatureField_canvas_get = function _CmSignatureField_canvas_get() { return this.renderRoot.querySelector('canvas'); }, _CmSignatureField_fit = function _CmSignatureField_fit() {
|
|
327
|
+
const canvas = __classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_canvas_get);
|
|
328
|
+
if (!canvas)
|
|
329
|
+
return;
|
|
330
|
+
const w = canvas.clientWidth, h = canvas.clientHeight;
|
|
331
|
+
if (!w || !h)
|
|
332
|
+
return;
|
|
333
|
+
__classPrivateFieldSet(this, _CmSignatureField_width, w, "f");
|
|
334
|
+
const dpr = window.devicePixelRatio || 1;
|
|
335
|
+
const pw = Math.round(w * dpr), ph = Math.round(h * dpr);
|
|
336
|
+
if (canvas.width !== pw || canvas.height !== ph) {
|
|
337
|
+
canvas.width = pw;
|
|
338
|
+
canvas.height = ph;
|
|
339
|
+
}
|
|
340
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_paint).call(this);
|
|
341
|
+
}, _CmSignatureField_draw = function _CmSignatureField_draw() {
|
|
342
|
+
if (__classPrivateFieldGet(this, _CmSignatureField_frame, "f"))
|
|
343
|
+
return;
|
|
344
|
+
__classPrivateFieldSet(this, _CmSignatureField_frame, requestAnimationFrame(() => { __classPrivateFieldSet(this, _CmSignatureField_frame, 0, "f"); __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_paint).call(this); }), "f");
|
|
345
|
+
}, _CmSignatureField_paint = function _CmSignatureField_paint() {
|
|
346
|
+
const canvas = __classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_canvas_get);
|
|
347
|
+
if (!canvas || !canvas.width)
|
|
348
|
+
return;
|
|
349
|
+
const ctx = canvas.getContext('2d');
|
|
350
|
+
const w = canvas.clientWidth || __classPrivateFieldGet(this, _CmSignatureField_width, "f");
|
|
351
|
+
if (!w)
|
|
352
|
+
return;
|
|
353
|
+
const scale = canvas.width / w;
|
|
354
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
355
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
356
|
+
ctx.setTransform(scale, 0, 0, scale, 0, 0);
|
|
357
|
+
const ink = getComputedStyle(canvas).color || EXPORT_INK;
|
|
358
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_render).call(this, ctx, w, ink, true);
|
|
359
|
+
}, _CmSignatureField_render = function _CmSignatureField_render(ctx, w, ink, screen) {
|
|
360
|
+
const h = w / RATIO;
|
|
361
|
+
const img = __classPrivateFieldGet(this, _CmSignatureField_image, "f");
|
|
362
|
+
if (img?.complete && img.naturalWidth) {
|
|
363
|
+
const k = Math.min((w * 0.9) / img.naturalWidth, (h * 0.9) / img.naturalHeight);
|
|
364
|
+
const iw = img.naturalWidth * k, ih = img.naturalHeight * k;
|
|
365
|
+
const x = (w - iw) / 2, y = (h - ih) / 2;
|
|
366
|
+
if (screen && !sameColor(ink, EXPORT_INK)) {
|
|
367
|
+
// Dark theme: show the saved (dark) ink in the screen's ink colour, so it stays visible.
|
|
368
|
+
const tmp = document.createElement('canvas');
|
|
369
|
+
tmp.width = Math.max(1, Math.round(iw * 2));
|
|
370
|
+
tmp.height = Math.max(1, Math.round(ih * 2));
|
|
371
|
+
const t = tmp.getContext('2d');
|
|
372
|
+
t.drawImage(img, 0, 0, tmp.width, tmp.height);
|
|
373
|
+
t.globalCompositeOperation = 'source-in';
|
|
374
|
+
t.fillStyle = ink;
|
|
375
|
+
t.fillRect(0, 0, tmp.width, tmp.height);
|
|
376
|
+
ctx.drawImage(tmp, x, y, iw, ih);
|
|
377
|
+
}
|
|
378
|
+
else
|
|
379
|
+
ctx.drawImage(img, x, y, iw, ih);
|
|
380
|
+
}
|
|
381
|
+
ctx.strokeStyle = ink;
|
|
382
|
+
ctx.fillStyle = ink;
|
|
383
|
+
ctx.lineCap = 'round';
|
|
384
|
+
ctx.lineJoin = 'round';
|
|
385
|
+
for (const s of __classPrivateFieldGet(this, _CmSignatureField_strokes, "f"))
|
|
386
|
+
drawStroke(ctx, s, w);
|
|
387
|
+
}, _CmSignatureField_point = function _CmSignatureField_point(e, rect) {
|
|
388
|
+
// Pressure only from pens: mice report a fixed 0.5 and many touch screens 0 or 1.
|
|
389
|
+
const p = e.pointerType === 'pen' && e.pressure > 0 ? e.pressure : 0.5;
|
|
390
|
+
return { x: (e.clientX - rect.left) / rect.width, y: (e.clientY - rect.top) / rect.width, p };
|
|
391
|
+
}, _CmSignatureField_onDown = function _CmSignatureField_onDown(e) {
|
|
392
|
+
if (this.disabled || __classPrivateFieldGet(this, _CmSignatureField_active, "f") || !e.isPrimary || (e.pointerType === 'mouse' && e.button !== 0))
|
|
393
|
+
return;
|
|
394
|
+
const canvas = e.currentTarget;
|
|
395
|
+
e.preventDefault();
|
|
396
|
+
try {
|
|
397
|
+
canvas.setPointerCapture(e.pointerId);
|
|
398
|
+
}
|
|
399
|
+
catch { /* synthetic events have no active pointer */ }
|
|
400
|
+
const stroke = [__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_point).call(this, e, canvas.getBoundingClientRect())];
|
|
401
|
+
__classPrivateFieldSet(this, _CmSignatureField_active, { id: e.pointerId, stroke }, "f");
|
|
402
|
+
__classPrivateFieldGet(this, _CmSignatureField_strokes, "f").push(stroke);
|
|
403
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
|
|
404
|
+
}, _CmSignatureField_onMove = function _CmSignatureField_onMove(e) {
|
|
405
|
+
const a = __classPrivateFieldGet(this, _CmSignatureField_active, "f");
|
|
406
|
+
if (!a || e.pointerId !== a.id)
|
|
407
|
+
return;
|
|
408
|
+
e.preventDefault();
|
|
409
|
+
const rect = e.currentTarget.getBoundingClientRect();
|
|
410
|
+
const events = typeof e.getCoalescedEvents === 'function' ? e.getCoalescedEvents() : [];
|
|
411
|
+
for (const ev of events.length ? events : [e]) {
|
|
412
|
+
const pt = __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_point).call(this, ev, rect);
|
|
413
|
+
const last = a.stroke[a.stroke.length - 1];
|
|
414
|
+
// Skip points under 1.5 CSS px apart: they add noise, not shape.
|
|
415
|
+
if (Math.hypot(pt.x - last.x, pt.y - last.y) * rect.width < 1.5)
|
|
416
|
+
continue;
|
|
417
|
+
a.stroke.push(pt);
|
|
418
|
+
}
|
|
419
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
|
|
420
|
+
}, _CmSignatureField_onUp = function _CmSignatureField_onUp(e) {
|
|
421
|
+
const a = __classPrivateFieldGet(this, _CmSignatureField_active, "f");
|
|
422
|
+
if (!a || e.pointerId !== a.id)
|
|
423
|
+
return;
|
|
424
|
+
if (e.type === 'pointerup')
|
|
425
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_onMove).call(this, e);
|
|
426
|
+
__classPrivateFieldSet(this, _CmSignatureField_active, undefined, "f");
|
|
427
|
+
try {
|
|
428
|
+
e.currentTarget.releasePointerCapture(e.pointerId);
|
|
429
|
+
}
|
|
430
|
+
catch { /* not captured */ }
|
|
431
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_draw).call(this);
|
|
432
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, true);
|
|
433
|
+
}, _CmSignatureField_fontFamily_get = function _CmSignatureField_fontFamily_get() {
|
|
434
|
+
const input = this.renderRoot.querySelector('.typed');
|
|
435
|
+
return (input && getComputedStyle(input).fontFamily) || 'cursive';
|
|
436
|
+
}, _CmSignatureField_loadFont = function _CmSignatureField_loadFont() {
|
|
437
|
+
const fonts = document.fonts;
|
|
438
|
+
if (!fonts || !this.isConnected)
|
|
439
|
+
return;
|
|
440
|
+
const font = `400 40px ${__classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_fontFamily_get)}`;
|
|
441
|
+
let ready = true;
|
|
442
|
+
try {
|
|
443
|
+
ready = fonts.check(font, this.typedName || 'Signature');
|
|
444
|
+
}
|
|
445
|
+
catch { /* invalid font string: use what there is */ }
|
|
446
|
+
if (ready)
|
|
447
|
+
return;
|
|
448
|
+
fonts.load(font, this.typedName || 'Signature').then(() => {
|
|
449
|
+
// Re-export once the font is in, so the PNG matches what the signer sees.
|
|
450
|
+
if (this.mode === 'type' && this.typedName.trim())
|
|
451
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_sync).call(this, false);
|
|
452
|
+
}, () => { });
|
|
453
|
+
}, _CmSignatureField_onType = function _CmSignatureField_onType(e) {
|
|
454
|
+
e.stopPropagation();
|
|
455
|
+
// The value follows in willUpdate, so the form value never lags; the events wait until typing stops.
|
|
456
|
+
if (!__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"))
|
|
457
|
+
__classPrivateFieldSet(this, _CmSignatureField_typeFrom, this.value, "f");
|
|
458
|
+
else
|
|
459
|
+
clearTimeout(__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"));
|
|
460
|
+
this.typedName = e.target.value;
|
|
461
|
+
__classPrivateFieldSet(this, _CmSignatureField_typeTimer, setTimeout(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_typed).call(this), TYPE_DELAY), "f");
|
|
462
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_loadFont).call(this);
|
|
463
|
+
}, _CmSignatureField_typed = function _CmSignatureField_typed() {
|
|
464
|
+
if (!__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"))
|
|
465
|
+
return;
|
|
466
|
+
clearTimeout(__classPrivateFieldGet(this, _CmSignatureField_typeTimer, "f"));
|
|
467
|
+
__classPrivateFieldSet(this, _CmSignatureField_typeTimer, undefined, "f");
|
|
468
|
+
if (this.value !== __classPrivateFieldGet(this, _CmSignatureField_typeFrom, "f"))
|
|
469
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_emit).call(this);
|
|
470
|
+
}, _CmSignatureField_onTypeChange = function _CmSignatureField_onTypeChange(e) {
|
|
471
|
+
// The native change of the inner box doesn't cross the shadow boundary on its own; ours is fired by #emit.
|
|
472
|
+
e.stopPropagation();
|
|
473
|
+
this.updateComplete.then(() => __classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_typed).call(this));
|
|
474
|
+
}, _CmSignatureField_sync = function _CmSignatureField_sync(emit) {
|
|
475
|
+
const next = this.empty ? '' : this.toDataURL();
|
|
476
|
+
const changed = next !== this.value;
|
|
477
|
+
if (changed) {
|
|
478
|
+
__classPrivateFieldSet(this, _CmSignatureField_own, next, "f");
|
|
479
|
+
this.value = next;
|
|
480
|
+
}
|
|
481
|
+
this.requestUpdate();
|
|
482
|
+
if (emit && changed)
|
|
483
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_emit).call(this);
|
|
484
|
+
}, _CmSignatureField_emit = function _CmSignatureField_emit() {
|
|
485
|
+
const detail = { value: this.value, empty: this.empty, mode: this.mode };
|
|
486
|
+
this.dispatchEvent(new CustomEvent('cm-change', { detail, bubbles: true, composed: true }));
|
|
487
|
+
this.dispatchEvent(new Event('change', { bubbles: true, composed: true }));
|
|
488
|
+
}, _CmSignatureField_export = function _CmSignatureField_export() {
|
|
489
|
+
if (this.empty)
|
|
490
|
+
return undefined;
|
|
491
|
+
const w = Math.max(__classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_canvas_get)?.clientWidth || __classPrivateFieldGet(this, _CmSignatureField_width, "f") || 600, 300);
|
|
492
|
+
const h = w / RATIO;
|
|
493
|
+
const full = document.createElement('canvas');
|
|
494
|
+
full.width = Math.round(w * SCALE);
|
|
495
|
+
full.height = Math.round(h * SCALE);
|
|
496
|
+
const ctx = full.getContext('2d', { willReadFrequently: true });
|
|
497
|
+
ctx.scale(SCALE, SCALE);
|
|
498
|
+
if (this.mode === 'type') {
|
|
499
|
+
let size = Math.min(h * 0.45, 64);
|
|
500
|
+
const family = __classPrivateFieldGet(this, _CmSignatureField_instances, "a", _CmSignatureField_fontFamily_get);
|
|
501
|
+
ctx.font = `400 ${size}px ${family}`;
|
|
502
|
+
const text = this.typedName.trim();
|
|
503
|
+
const max = w - 2 * PADDING;
|
|
504
|
+
const measured = ctx.measureText(text).width;
|
|
505
|
+
if (measured > max) {
|
|
506
|
+
size *= max / measured;
|
|
507
|
+
ctx.font = `400 ${size}px ${family}`;
|
|
508
|
+
}
|
|
509
|
+
ctx.direction = getComputedStyle(this).direction === 'rtl' ? 'rtl' : 'ltr';
|
|
510
|
+
ctx.textAlign = 'center';
|
|
511
|
+
ctx.textBaseline = 'alphabetic';
|
|
512
|
+
ctx.fillStyle = EXPORT_INK;
|
|
513
|
+
ctx.fillText(text, w / 2, h * BASELINE);
|
|
514
|
+
}
|
|
515
|
+
else
|
|
516
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_render).call(this, ctx, w, EXPORT_INK, false);
|
|
517
|
+
return crop(full, PADDING * SCALE);
|
|
518
|
+
}, _CmSignatureField_select = function _CmSignatureField_select(mode, focus) {
|
|
519
|
+
if (this.disabled)
|
|
520
|
+
return;
|
|
521
|
+
if (focus)
|
|
522
|
+
this.updateComplete.then(() => this.renderRoot.querySelector(`#${__classPrivateFieldGet(this, _CmSignatureField_id, "f")}-tab-${mode}`)?.focus());
|
|
523
|
+
if (mode === this.mode)
|
|
524
|
+
return;
|
|
525
|
+
const before = this.value;
|
|
526
|
+
this.mode = mode;
|
|
527
|
+
this.updateComplete.then(() => {
|
|
528
|
+
if (mode === 'draw')
|
|
529
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_fit).call(this);
|
|
530
|
+
if (this.value !== before)
|
|
531
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_emit).call(this);
|
|
532
|
+
});
|
|
533
|
+
}, _CmSignatureField_onTabKey = function _CmSignatureField_onTabKey(e) {
|
|
534
|
+
const key = logicalKey(e, this);
|
|
535
|
+
let next;
|
|
536
|
+
if (key === 'ArrowRight' || key === 'ArrowLeft')
|
|
537
|
+
next = this.mode === 'draw' ? 'type' : 'draw';
|
|
538
|
+
else if (e.key === 'Home')
|
|
539
|
+
next = 'draw';
|
|
540
|
+
else if (e.key === 'End')
|
|
541
|
+
next = 'type';
|
|
542
|
+
else
|
|
543
|
+
return;
|
|
544
|
+
e.preventDefault();
|
|
545
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_select).call(this, next, true);
|
|
546
|
+
}, _CmSignatureField_say = function _CmSignatureField_say(key) {
|
|
547
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_announce).call(this, this.t(key));
|
|
548
|
+
}, _CmSignatureField_announce = function _CmSignatureField_announce(text) {
|
|
549
|
+
// Empty first, so the same message twice in a row is read again.
|
|
550
|
+
this._announce = '';
|
|
551
|
+
this.updateComplete.then(() => { this._announce = text; });
|
|
552
|
+
}, _CmSignatureField_keepFocus = function _CmSignatureField_keepFocus(hadFocus) {
|
|
553
|
+
this.updateComplete.then(() => {
|
|
554
|
+
const active = this.shadowRoot?.activeElement;
|
|
555
|
+
if (!hadFocus || (active && !active.disabled))
|
|
556
|
+
return;
|
|
557
|
+
const target = this.mode === 'type'
|
|
558
|
+
? this.renderRoot.querySelector('.typed')
|
|
559
|
+
: this.renderRoot.querySelector('.tab[aria-selected="true"]');
|
|
560
|
+
target?.focus();
|
|
561
|
+
});
|
|
562
|
+
}, _CmSignatureField_onUndo = function _CmSignatureField_onUndo() {
|
|
563
|
+
if (this.disabled || this.mode !== 'draw' || this.empty)
|
|
564
|
+
return;
|
|
565
|
+
const hadFocus = this.matches(':focus-within');
|
|
566
|
+
this.undo();
|
|
567
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_say).call(this, 'undone');
|
|
568
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_keepFocus).call(this, hadFocus);
|
|
569
|
+
}, _CmSignatureField_onClear = function _CmSignatureField_onClear() {
|
|
570
|
+
if (this.disabled || this.empty)
|
|
571
|
+
return;
|
|
572
|
+
const hadFocus = this.matches(':focus-within');
|
|
573
|
+
this.clear();
|
|
574
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_say).call(this, 'cleared');
|
|
575
|
+
__classPrivateFieldGet(this, _CmSignatureField_instances, "m", _CmSignatureField_keepFocus).call(this, hadFocus);
|
|
576
|
+
};
|
|
577
|
+
CmSignatureField.formAssociated = true;
|
|
578
|
+
CmSignatureField.shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };
|
|
579
|
+
CmSignatureField.properties = {
|
|
580
|
+
label: {},
|
|
581
|
+
hint: {},
|
|
582
|
+
helpText: { attribute: 'help-text' },
|
|
583
|
+
error: { reflect: true },
|
|
584
|
+
name: { reflect: true },
|
|
585
|
+
value: {},
|
|
586
|
+
mode: { reflect: true },
|
|
587
|
+
typedName: { attribute: 'typed-name' },
|
|
588
|
+
required: { type: Boolean, reflect: true },
|
|
589
|
+
disabled: { type: Boolean, reflect: true },
|
|
590
|
+
_announce: { state: true },
|
|
591
|
+
};
|
|
592
|
+
CmSignatureField.styles = [
|
|
593
|
+
base,
|
|
594
|
+
css `
|
|
595
|
+
:host { display: block; }
|
|
596
|
+
.field { display: flex; flex-direction: column; gap: var(--cm-space-10); }
|
|
597
|
+
.label { font-size: var(--cm-type-label-size); color: var(--cm-text-default); }
|
|
598
|
+
.req { color: var(--cm-text-critical); margin-inline-start: 2px; }
|
|
599
|
+
.hint { margin-inline-start: var(--cm-space-10); font-size: 12px; color: var(--cm-text-tertiary); text-transform: lowercase; }
|
|
600
|
+
.bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--cm-space-10); }
|
|
601
|
+
.tabs { display: flex; gap: var(--cm-space-20); }
|
|
602
|
+
.tab {
|
|
603
|
+
position: relative; min-height: 32px; padding: 0 2px; font: inherit; font-size: 14px; font-weight: 500;
|
|
604
|
+
color: var(--cm-text-tertiary); background: none; border: 0; cursor: pointer;
|
|
605
|
+
}
|
|
606
|
+
.tab::after { content: ''; position: absolute; inset-inline: 0; bottom: 2px; height: 2px; border-radius: 1px; background: transparent; }
|
|
607
|
+
.tab:hover:not(:disabled), .tab[aria-selected='true'] { color: var(--cm-text-default); }
|
|
608
|
+
.tab[aria-selected='true']::after { background: var(--cm-border-strong); }
|
|
609
|
+
.tab:focus-visible { outline: 2px solid var(--cm-border-strong); outline-offset: 2px; border-radius: var(--cm-radius-xs); }
|
|
610
|
+
.tab:disabled { color: var(--cm-text-secondary); cursor: not-allowed; }
|
|
611
|
+
.actions { display: flex; gap: var(--cm-space-10); margin-inline-start: auto; }
|
|
612
|
+
/* Secondary medium Button. */
|
|
613
|
+
.act {
|
|
614
|
+
display: inline-flex; align-items: center; height: 32px; padding: 0 var(--cm-space-15);
|
|
615
|
+
font: inherit; font-size: 12px; font-weight: 500; line-height: 1; white-space: nowrap; cursor: pointer;
|
|
616
|
+
background: var(--cm-bg-hover); color: var(--cm-text-default);
|
|
617
|
+
border: var(--cm-border-1) solid var(--cm-border-strong); border-radius: var(--cm-radius-sm);
|
|
618
|
+
}
|
|
619
|
+
.act:hover:not(:disabled) { background: var(--cm-bg-surface); }
|
|
620
|
+
.act:focus-visible { outline: 2px solid var(--cm-border-strong); outline-offset: 2px; }
|
|
621
|
+
.act:disabled { background: var(--cm-action-disabled); border-color: var(--cm-action-disabled); color: var(--cm-action-disabled-text); cursor: not-allowed; }
|
|
622
|
+
.panel:focus { outline: none; }
|
|
623
|
+
.pad {
|
|
624
|
+
position: relative; width: 100%; aspect-ratio: ${RATIO} / 1; overflow: hidden; container-type: inline-size;
|
|
625
|
+
background: var(--cm-bg-surface); border: var(--cm-border-1) solid var(--cm-border-input); border-radius: var(--cm-radius-sm);
|
|
626
|
+
}
|
|
627
|
+
.panel:focus-visible .pad, .pad:focus-within { border-color: var(--cm-border-strong); box-shadow: 0 0 0 1px var(--cm-border-strong); }
|
|
628
|
+
:host([error]:not([error=''])) .pad { border-color: var(--cm-border-critical); }
|
|
629
|
+
canvas {
|
|
630
|
+
position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair;
|
|
631
|
+
/* The ink: read in script. The transition makes a theme change fire transitionend, which repaints the ink. */
|
|
632
|
+
color: var(--cm-text-default); transition: color 1ms;
|
|
633
|
+
}
|
|
634
|
+
.line {
|
|
635
|
+
position: absolute; inset-inline: var(--cm-space-15); top: ${BASELINE * 100}%; height: 0;
|
|
636
|
+
border-top: var(--cm-border-1) dashed var(--cm-border-input); pointer-events: none;
|
|
637
|
+
}
|
|
638
|
+
.here {
|
|
639
|
+
position: absolute; inset-inline-start: var(--cm-space-15); bottom: calc(${(1 - BASELINE) * 100}% + 4px);
|
|
640
|
+
font-size: 12px; color: var(--cm-text-tertiary); pointer-events: none; user-select: none;
|
|
641
|
+
}
|
|
642
|
+
.typed {
|
|
643
|
+
position: absolute; inset-inline: var(--cm-space-15); bottom: calc(${(1 - BASELINE) * 100}% - 0.18em);
|
|
644
|
+
width: calc(100% - 2 * var(--cm-space-15)); padding: 0; margin: 0;
|
|
645
|
+
font-family: var(--cm-signature-font, 'Dancing Script', 'Segoe Script', 'Bradley Hand', 'Snell Roundhand', 'Brush Script MT', cursive);
|
|
646
|
+
font-size: clamp(24px, 9cqi, 48px); font-weight: 400; line-height: 1.25; text-align: center;
|
|
647
|
+
color: var(--cm-text-default); background: transparent; border: 0; outline: none;
|
|
648
|
+
}
|
|
649
|
+
.typed::placeholder {
|
|
650
|
+
font-family: var(--cm-font-sans, 'Poppins', system-ui, sans-serif); font-size: 14px; color: var(--cm-text-tertiary);
|
|
651
|
+
}
|
|
652
|
+
:host([disabled]) .pad { background: var(--cm-bg-input-filled); border-color: var(--cm-border-default); }
|
|
653
|
+
:host([disabled]) canvas, :host([disabled]) .typed { cursor: not-allowed; }
|
|
654
|
+
:host([disabled]) .typed { color: var(--cm-text-secondary); }
|
|
655
|
+
.help { margin: 0; font-size: 12px; color: var(--cm-text-tertiary); }
|
|
656
|
+
.error { margin: 0; font-size: 12px; color: var(--cm-text-critical); }
|
|
657
|
+
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
|
658
|
+
`,
|
|
659
|
+
];
|
|
660
|
+
/** Draws one stroke through the midpoints of its points (quadratic smoothing), each piece as wide as its pressure. */
|
|
661
|
+
function drawStroke(ctx, s, w) {
|
|
662
|
+
const width = (p) => w * LINE * (0.35 + p * 1.3);
|
|
663
|
+
if (s.length === 1) {
|
|
664
|
+
ctx.beginPath();
|
|
665
|
+
ctx.arc(s[0].x * w, s[0].y * w, width(s[0].p) / 2, 0, Math.PI * 2);
|
|
666
|
+
ctx.fill();
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
const P = (q) => [q.x * w, q.y * w];
|
|
670
|
+
let start = s[0];
|
|
671
|
+
for (let i = 1; i < s.length - 1; i++) {
|
|
672
|
+
const end = mid(s[i], s[i + 1]);
|
|
673
|
+
ctx.beginPath();
|
|
674
|
+
ctx.moveTo(...P(start));
|
|
675
|
+
ctx.quadraticCurveTo(...P(s[i]), ...P(end));
|
|
676
|
+
ctx.lineWidth = width(s[i].p);
|
|
677
|
+
ctx.stroke();
|
|
678
|
+
start = end;
|
|
679
|
+
}
|
|
680
|
+
const last = s[s.length - 1];
|
|
681
|
+
ctx.beginPath();
|
|
682
|
+
ctx.moveTo(...P(start));
|
|
683
|
+
ctx.lineTo(...P(last));
|
|
684
|
+
ctx.lineWidth = width(last.p);
|
|
685
|
+
ctx.stroke();
|
|
686
|
+
}
|
|
687
|
+
/** A copy of the canvas cut to its non-transparent pixels plus `pad` pixels each side; undefined if it is blank. */
|
|
688
|
+
function crop(c, pad) {
|
|
689
|
+
const { width, height } = c;
|
|
690
|
+
const data = c.getContext('2d', { willReadFrequently: true }).getImageData(0, 0, width, height).data;
|
|
691
|
+
let top = height, left = width, right = -1, bottom = -1;
|
|
692
|
+
for (let y = 0; y < height; y++) {
|
|
693
|
+
const row = y * width * 4;
|
|
694
|
+
for (let x = 0; x < width; x++) {
|
|
695
|
+
if (data[row + x * 4 + 3] > 8) {
|
|
696
|
+
if (x < left)
|
|
697
|
+
left = x;
|
|
698
|
+
if (x > right)
|
|
699
|
+
right = x;
|
|
700
|
+
if (y < top)
|
|
701
|
+
top = y;
|
|
702
|
+
bottom = y;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
if (right < 0)
|
|
707
|
+
return undefined;
|
|
708
|
+
const out = document.createElement('canvas');
|
|
709
|
+
out.width = right - left + 1 + 2 * pad;
|
|
710
|
+
out.height = bottom - top + 1 + 2 * pad;
|
|
711
|
+
out.getContext('2d').drawImage(c, left, top, right - left + 1, bottom - top + 1, pad, pad, right - left + 1, bottom - top + 1);
|
|
712
|
+
return out;
|
|
713
|
+
}
|
|
714
|
+
/** Whether two CSS colours (as computed: `rgb(…)`, or hex) are the same. */
|
|
715
|
+
function sameColor(a, b) {
|
|
716
|
+
const norm = (c) => {
|
|
717
|
+
const m = /^#([0-9a-f]{6})$/i.exec(c.trim());
|
|
718
|
+
if (m) {
|
|
719
|
+
const n = parseInt(m[1], 16);
|
|
720
|
+
return `${(n >> 16) & 255},${(n >> 8) & 255},${n & 255}`;
|
|
721
|
+
}
|
|
722
|
+
return (/\d+\s*,\s*\d+\s*,\s*\d+/.exec(c)?.[0] ?? c).replace(/\s/g, '');
|
|
723
|
+
};
|
|
724
|
+
return norm(a) === norm(b);
|
|
725
|
+
}
|
|
726
|
+
define('cm-signature-field', CmSignatureField);
|