@schnsrw/casual-sheets 0.2.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/dist/index.cjs ADDED
@@ -0,0 +1,899 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ DrawnSignaturePad: () => DrawnSignaturePad,
24
+ EmbedTransport: () => EmbedTransport,
25
+ SigningPane: () => SigningPane,
26
+ SigningProvider: () => SigningProvider,
27
+ TypedSignatureField: () => TypedSignatureField,
28
+ UploadedSignatureField: () => UploadedSignatureField,
29
+ createSigningController: () => createSigningController,
30
+ isCasualEnvelope: () => isCasualEnvelope,
31
+ useSigning: () => useSigning
32
+ });
33
+ module.exports = __toCommonJS(src_exports);
34
+
35
+ // src/signing/SigningProvider.tsx
36
+ var import_react = require("react");
37
+
38
+ // src/signing/controller.ts
39
+ function createSigningController(fields, mode) {
40
+ if (fields.length === 0) {
41
+ throw new Error("createSigningController: at least one field required");
42
+ }
43
+ const fieldIds = new Set(fields.map((f) => f.fieldId));
44
+ if (fieldIds.size !== fields.length) {
45
+ throw new Error("createSigningController: duplicate fieldId");
46
+ }
47
+ const signed = {};
48
+ let activeFieldIndex = nextRequiredIndex(fields, signed);
49
+ let isComplete = false;
50
+ let isCancelled = false;
51
+ const listeners = /* @__PURE__ */ new Set();
52
+ function emit() {
53
+ const snap = snapshotInternal();
54
+ for (const l of listeners) l(snap);
55
+ }
56
+ function snapshotInternal() {
57
+ return {
58
+ fields,
59
+ mode,
60
+ signed: { ...signed },
61
+ activeFieldIndex,
62
+ canComplete: allRequiredSigned(fields, signed),
63
+ isComplete,
64
+ isCancelled
65
+ };
66
+ }
67
+ return {
68
+ snapshot: snapshotInternal,
69
+ subscribe(listener) {
70
+ listeners.add(listener);
71
+ return () => {
72
+ listeners.delete(listener);
73
+ };
74
+ },
75
+ signField(payload) {
76
+ if (isComplete || isCancelled) return;
77
+ if (!fieldIds.has(payload.fieldId)) {
78
+ throw new Error(`signField: unknown fieldId ${payload.fieldId}`);
79
+ }
80
+ signed[payload.fieldId] = payload;
81
+ activeFieldIndex = nextRequiredIndex(fields, signed);
82
+ emit();
83
+ },
84
+ focusField(fieldId) {
85
+ if (isComplete || isCancelled) return;
86
+ const idx = fields.findIndex((f) => f.fieldId === fieldId);
87
+ if (idx < 0) return;
88
+ if (mode === "sequential") {
89
+ const next = nextRequiredIndex(fields, signed);
90
+ if (idx !== next) return;
91
+ }
92
+ activeFieldIndex = idx;
93
+ emit();
94
+ },
95
+ complete() {
96
+ if (!allRequiredSigned(fields, signed)) {
97
+ throw new Error("complete: required fields are still unsigned");
98
+ }
99
+ isComplete = true;
100
+ activeFieldIndex = -1;
101
+ emit();
102
+ return snapshotInternal();
103
+ },
104
+ cancel() {
105
+ if (isComplete || isCancelled) return;
106
+ isCancelled = true;
107
+ activeFieldIndex = -1;
108
+ emit();
109
+ }
110
+ };
111
+ }
112
+ function allRequiredSigned(fields, signed) {
113
+ return fields.every((f) => !f.required || signed[f.fieldId] !== void 0);
114
+ }
115
+ function nextRequiredIndex(fields, signed) {
116
+ for (let i = 0; i < fields.length; i++) {
117
+ if (fields[i].required && !signed[fields[i].fieldId]) return i;
118
+ }
119
+ for (let i = 0; i < fields.length; i++) {
120
+ if (!signed[fields[i].fieldId]) return i;
121
+ }
122
+ return -1;
123
+ }
124
+
125
+ // src/signing/SigningProvider.tsx
126
+ var import_jsx_runtime = require("react/jsx-runtime");
127
+ var SigningContext = (0, import_react.createContext)(null);
128
+ function SigningProvider({ session, documentBytes, children }) {
129
+ if (!session) {
130
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
131
+ }
132
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SigningProviderInner, { session, documentBytes, children });
133
+ }
134
+ function SigningProviderInner({
135
+ session,
136
+ documentBytes,
137
+ children
138
+ }) {
139
+ const controller = (0, import_react.useMemo)(
140
+ () => createSigningController(session.fields, session.mode),
141
+ // eslint-disable-next-line react-hooks/exhaustive-deps
142
+ [session.fields, session.mode]
143
+ );
144
+ const [snapshot, setSnapshot] = (0, import_react.useState)(() => controller.snapshot());
145
+ (0, import_react.useEffect)(() => {
146
+ const unsub = controller.subscribe(setSnapshot);
147
+ return unsub;
148
+ }, [controller]);
149
+ const value = (0, import_react.useMemo)(
150
+ () => ({
151
+ controller,
152
+ snapshot,
153
+ signField: async (payload) => {
154
+ controller.signField(payload);
155
+ await session.onFieldSigned?.(payload);
156
+ },
157
+ completeIfReady: async () => {
158
+ if (!controller.snapshot().canComplete) return;
159
+ const final = controller.complete();
160
+ const completePayload = {
161
+ fieldIds: final.fields.map((f) => f.fieldId).filter((id) => final.signed[id] !== void 0),
162
+ bytes: documentBytes ?? new ArrayBuffer(0),
163
+ fields: final.signed
164
+ };
165
+ await session.onComplete?.(completePayload);
166
+ },
167
+ cancel: (reason) => {
168
+ controller.cancel();
169
+ session.onCancel?.({ reason });
170
+ },
171
+ baseDocumentBytes: documentBytes
172
+ }),
173
+ [controller, snapshot, session, documentBytes]
174
+ );
175
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SigningContext.Provider, { value, children });
176
+ }
177
+ function useSigning() {
178
+ return (0, import_react.useContext)(SigningContext);
179
+ }
180
+
181
+ // src/signing/SigningPane.tsx
182
+ var import_react3 = require("react");
183
+
184
+ // src/signing/captures.tsx
185
+ var import_react2 = require("react");
186
+ var import_jsx_runtime2 = require("react/jsx-runtime");
187
+ function DrawnSignaturePad({
188
+ onCapture,
189
+ clearLabel = "Clear",
190
+ saveLabel = "Use this signature",
191
+ width = 480,
192
+ height = 160
193
+ }) {
194
+ const canvasRef = (0, import_react2.useRef)(null);
195
+ const drawingRef = (0, import_react2.useRef)(false);
196
+ const [hasInk, setHasInk] = (0, import_react2.useState)(false);
197
+ (0, import_react2.useEffect)(() => {
198
+ const canvas = canvasRef.current;
199
+ if (!canvas) return;
200
+ const ctx = canvas.getContext("2d");
201
+ if (!ctx) return;
202
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
203
+ ctx.strokeStyle = "#0f172a";
204
+ ctx.lineWidth = 2;
205
+ ctx.lineCap = "round";
206
+ ctx.lineJoin = "round";
207
+ }, []);
208
+ const start = (e) => {
209
+ const canvas = canvasRef.current;
210
+ if (!canvas) return;
211
+ const ctx = canvas.getContext("2d");
212
+ if (!ctx) return;
213
+ const { x, y } = pointerPos(e, canvas);
214
+ ctx.beginPath();
215
+ ctx.moveTo(x, y);
216
+ drawingRef.current = true;
217
+ canvas.setPointerCapture(e.pointerId);
218
+ };
219
+ const move = (e) => {
220
+ if (!drawingRef.current) return;
221
+ const canvas = canvasRef.current;
222
+ if (!canvas) return;
223
+ const ctx = canvas.getContext("2d");
224
+ if (!ctx) return;
225
+ const { x, y } = pointerPos(e, canvas);
226
+ ctx.lineTo(x, y);
227
+ ctx.stroke();
228
+ if (!hasInk) setHasInk(true);
229
+ };
230
+ const end = (e) => {
231
+ drawingRef.current = false;
232
+ canvasRef.current?.releasePointerCapture(e.pointerId);
233
+ };
234
+ const clear = () => {
235
+ const canvas = canvasRef.current;
236
+ if (!canvas) return;
237
+ const ctx = canvas.getContext("2d");
238
+ if (!ctx) return;
239
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
240
+ setHasInk(false);
241
+ };
242
+ const save = async () => {
243
+ const canvas = canvasRef.current;
244
+ if (!canvas || !hasInk) return;
245
+ const blob = await new Promise((resolve) => {
246
+ canvas.toBlob((b) => resolve(b), "image/png");
247
+ });
248
+ if (!blob) return;
249
+ const bytes = await blob.arrayBuffer();
250
+ onCapture({ bytes, mime: "image/png" });
251
+ clear();
252
+ };
253
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: padWrapStyle, "data-testid": "drawn-signature-pad", children: [
254
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
255
+ "canvas",
256
+ {
257
+ ref: canvasRef,
258
+ width,
259
+ height,
260
+ style: padCanvasStyle(width, height),
261
+ onPointerDown: start,
262
+ onPointerMove: move,
263
+ onPointerUp: end,
264
+ onPointerLeave: end,
265
+ "data-testid": "drawn-signature-canvas"
266
+ }
267
+ ),
268
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: padActionsStyle, children: [
269
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "button", onClick: clear, style: secondaryBtnStyle(false), children: clearLabel }),
270
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
271
+ "button",
272
+ {
273
+ type: "button",
274
+ onClick: save,
275
+ disabled: !hasInk,
276
+ style: primaryBtnStyle(!hasInk),
277
+ "data-testid": "drawn-signature-save",
278
+ children: saveLabel
279
+ }
280
+ )
281
+ ] })
282
+ ] });
283
+ }
284
+ function pointerPos(e, canvas) {
285
+ const rect = canvas.getBoundingClientRect();
286
+ const scaleX = canvas.width / rect.width;
287
+ const scaleY = canvas.height / rect.height;
288
+ return {
289
+ x: (e.clientX - rect.left) * scaleX,
290
+ y: (e.clientY - rect.top) * scaleY
291
+ };
292
+ }
293
+ function TypedSignatureField({
294
+ onCapture,
295
+ defaultText = "",
296
+ saveLabel = "Use this signature"
297
+ }) {
298
+ const [value, setValue] = (0, import_react2.useState)(defaultText);
299
+ const save = () => {
300
+ const trimmed = value.trim();
301
+ if (!trimmed) return;
302
+ const bytes = new TextEncoder().encode(trimmed).buffer;
303
+ onCapture({ bytes, mime: "text/plain" });
304
+ setValue("");
305
+ };
306
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: padWrapStyle, "data-testid": "typed-signature-field", children: [
307
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
308
+ "input",
309
+ {
310
+ type: "text",
311
+ value,
312
+ onChange: (e) => setValue(e.target.value),
313
+ placeholder: "Type your full name",
314
+ style: typedInputStyle,
315
+ "data-testid": "typed-signature-input",
316
+ autoFocus: true
317
+ }
318
+ ),
319
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: padActionsStyle, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
320
+ "button",
321
+ {
322
+ type: "button",
323
+ onClick: save,
324
+ disabled: !value.trim(),
325
+ style: primaryBtnStyle(!value.trim()),
326
+ "data-testid": "typed-signature-save",
327
+ children: saveLabel
328
+ }
329
+ ) })
330
+ ] });
331
+ }
332
+ function UploadedSignatureField({
333
+ onCapture,
334
+ accept = "image/png,image/jpeg,image/svg+xml"
335
+ }) {
336
+ const inputRef = (0, import_react2.useRef)(null);
337
+ const [fileName, setFileName] = (0, import_react2.useState)(null);
338
+ const onChange = async (e) => {
339
+ const file = e.target.files?.[0];
340
+ if (!file) return;
341
+ const bytes = await file.arrayBuffer();
342
+ onCapture({ bytes, mime: file.type || "application/octet-stream" });
343
+ setFileName(file.name);
344
+ if (inputRef.current) inputRef.current.value = "";
345
+ };
346
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: padWrapStyle, "data-testid": "uploaded-signature-field", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("label", { style: uploadLabelStyle, children: [
347
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
348
+ "input",
349
+ {
350
+ ref: inputRef,
351
+ type: "file",
352
+ accept,
353
+ onChange,
354
+ style: { display: "none" },
355
+ "data-testid": "uploaded-signature-input"
356
+ }
357
+ ),
358
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: fileName ?? "Choose image\u2026" })
359
+ ] }) });
360
+ }
361
+ var padWrapStyle = {
362
+ display: "flex",
363
+ flexDirection: "column",
364
+ gap: 10
365
+ };
366
+ var padCanvasStyle = (w, h) => ({
367
+ width: w,
368
+ height: h,
369
+ maxWidth: "100%",
370
+ border: "1px dashed var(--doc-border, #cbd5e1)",
371
+ borderRadius: 8,
372
+ background: "var(--doc-surface, #fff)",
373
+ cursor: "crosshair",
374
+ touchAction: "none"
375
+ });
376
+ var padActionsStyle = {
377
+ display: "flex",
378
+ justifyContent: "flex-end",
379
+ gap: 8
380
+ };
381
+ var typedInputStyle = {
382
+ width: "100%",
383
+ padding: "10px 12px",
384
+ border: "1px solid var(--doc-border, #cbd5e1)",
385
+ borderRadius: 6,
386
+ fontSize: 18,
387
+ fontFamily: '"Caveat", "Dancing Script", "Brush Script MT", cursive',
388
+ background: "var(--doc-surface, #fff)",
389
+ color: "var(--doc-text, #0f172a)"
390
+ };
391
+ var uploadLabelStyle = {
392
+ display: "inline-flex",
393
+ padding: "8px 14px",
394
+ border: "1px dashed var(--doc-border, #cbd5e1)",
395
+ borderRadius: 6,
396
+ background: "var(--doc-surface, #fff)",
397
+ color: "var(--doc-text, #0f172a)",
398
+ fontSize: 13,
399
+ cursor: "pointer",
400
+ alignSelf: "flex-start"
401
+ };
402
+ function primaryBtnStyle(disabled) {
403
+ return {
404
+ padding: "8px 16px",
405
+ borderRadius: 6,
406
+ border: "1px solid transparent",
407
+ background: disabled ? "var(--doc-border, #cbd5e1)" : "var(--doc-accent, #2563eb)",
408
+ color: disabled ? "var(--doc-text-muted, #64748b)" : "#fff",
409
+ fontSize: 13,
410
+ fontWeight: 600,
411
+ cursor: disabled ? "not-allowed" : "pointer",
412
+ fontFamily: "inherit"
413
+ };
414
+ }
415
+ function secondaryBtnStyle(disabled) {
416
+ return {
417
+ padding: "8px 16px",
418
+ borderRadius: 6,
419
+ border: "1px solid var(--doc-border, #cbd5e1)",
420
+ background: "transparent",
421
+ color: "var(--doc-text, #0f172a)",
422
+ fontSize: 13,
423
+ fontWeight: 500,
424
+ cursor: disabled ? "not-allowed" : "pointer",
425
+ fontFamily: "inherit"
426
+ };
427
+ }
428
+
429
+ // src/signing/SigningPane.tsx
430
+ var import_jsx_runtime3 = require("react/jsx-runtime");
431
+ function SigningPane({ banner, testId = "signing-pane" }) {
432
+ const ctx = useSigning();
433
+ if (!ctx) return null;
434
+ const { snapshot, signField, completeIfReady, cancel } = ctx;
435
+ if (snapshot.isComplete || snapshot.isCancelled) return null;
436
+ const active = snapshot.activeFieldIndex >= 0 ? snapshot.fields[snapshot.activeFieldIndex] : null;
437
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("aside", { style: paneStyle, role: "region", "aria-label": "Signing pane", "data-testid": testId, children: [
438
+ banner && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: bannerStyle, "data-testid": `${testId}-banner`, children: banner }),
439
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: listStyle, "data-testid": `${testId}-fields`, children: snapshot.fields.map((f, i) => {
440
+ const isSigned = !!snapshot.signed[f.fieldId];
441
+ const isActive = i === snapshot.activeFieldIndex;
442
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
443
+ "div",
444
+ {
445
+ style: listItemStyle(isActive, isSigned),
446
+ "data-testid": `${testId}-field-${f.fieldId}`,
447
+ "data-state": isSigned ? "signed" : isActive ? "active" : "pending",
448
+ children: [
449
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: listIconStyle(isSigned), "aria-hidden": "true", children: isSigned ? "\u2713" : i + 1 }),
450
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: listLabelStyle, children: f.label }),
451
+ !f.required && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: optionalChipStyle, "aria-label": "Optional", children: "optional" })
452
+ ]
453
+ },
454
+ f.fieldId
455
+ );
456
+ }) }),
457
+ active && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
458
+ ActiveFieldEditor,
459
+ {
460
+ field: active,
461
+ testId,
462
+ onCapture: async (cap, method) => {
463
+ const payload = {
464
+ fieldId: active.fieldId,
465
+ method,
466
+ bytes: cap.bytes,
467
+ mime: cap.mime,
468
+ signedAt: (/* @__PURE__ */ new Date()).toISOString()
469
+ };
470
+ await signField(payload);
471
+ }
472
+ }
473
+ ),
474
+ !active && snapshot.canComplete && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: completeBlockStyle, "data-testid": `${testId}-complete-block`, children: "All required signatures collected. Ready to finalise." }),
475
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("footer", { style: footerStyle, children: [
476
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
477
+ "button",
478
+ {
479
+ type: "button",
480
+ onClick: () => cancel("signer_cancelled"),
481
+ style: secondaryBtnStyle2(),
482
+ "data-testid": `${testId}-cancel`,
483
+ children: "Cancel"
484
+ }
485
+ ),
486
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
487
+ "button",
488
+ {
489
+ type: "button",
490
+ onClick: () => void completeIfReady(),
491
+ disabled: !snapshot.canComplete,
492
+ style: primaryBtnStyle2(!snapshot.canComplete),
493
+ "data-testid": `${testId}-complete`,
494
+ children: "Complete"
495
+ }
496
+ )
497
+ ] })
498
+ ] });
499
+ }
500
+ function ActiveFieldEditor({
501
+ field,
502
+ testId,
503
+ onCapture
504
+ }) {
505
+ const [method, setMethod] = (0, import_react3.useState)(field.methods[0]);
506
+ (0, import_react3.useEffect)(() => {
507
+ setMethod(field.methods[0]);
508
+ }, [field]);
509
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: editorStyle, "data-testid": `${testId}-editor`, children: [
510
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: editorHeaderStyle, children: [
511
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: editorLabelStyle, children: field.label }),
512
+ field.signer?.name && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: editorSignerStyle, children: field.signer.name })
513
+ ] }),
514
+ field.methods.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: methodTabsStyle, role: "tablist", "data-testid": `${testId}-methods`, children: field.methods.map((m) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
515
+ "button",
516
+ {
517
+ type: "button",
518
+ role: "tab",
519
+ "aria-selected": method === m,
520
+ onClick: () => setMethod(m),
521
+ style: methodTabStyle(method === m),
522
+ "data-testid": `${testId}-method-${m}`,
523
+ children: methodLabel(m)
524
+ },
525
+ m
526
+ )) }),
527
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: captureWrapStyle, children: [
528
+ method === "drawn" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(DrawnSignaturePad, { onCapture: (c) => onCapture(c, "drawn") }),
529
+ method === "typed" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
530
+ TypedSignatureField,
531
+ {
532
+ defaultText: field.signer?.name ?? "",
533
+ onCapture: (c) => onCapture(c, "typed")
534
+ }
535
+ ),
536
+ method === "uploaded" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(UploadedSignatureField, { onCapture: (c) => onCapture(c, "uploaded") })
537
+ ] })
538
+ ] });
539
+ }
540
+ function methodLabel(m) {
541
+ switch (m) {
542
+ case "drawn":
543
+ return "Draw";
544
+ case "typed":
545
+ return "Type";
546
+ case "uploaded":
547
+ return "Upload";
548
+ }
549
+ }
550
+ var paneStyle = {
551
+ position: "fixed",
552
+ top: 16,
553
+ right: 16,
554
+ bottom: 16,
555
+ width: 360,
556
+ maxWidth: "100vw",
557
+ display: "flex",
558
+ flexDirection: "column",
559
+ gap: 14,
560
+ padding: 16,
561
+ background: "var(--doc-surface, #fff)",
562
+ border: "1px solid var(--doc-border, #cbd5e1)",
563
+ borderRadius: 12,
564
+ boxShadow: "0 1px 1px rgba(0, 0, 0, 0.04), 0 6px 24px rgba(15, 23, 42, 0.12)",
565
+ fontFamily: "inherit",
566
+ zIndex: 9e3
567
+ };
568
+ var bannerStyle = {
569
+ padding: "8px 10px",
570
+ background: "var(--doc-surface-2, #f1f5f9)",
571
+ border: "1px solid var(--doc-border-light, #e2e8f0)",
572
+ borderRadius: 6,
573
+ fontSize: 12,
574
+ color: "var(--doc-text-muted, #475569)"
575
+ };
576
+ var listStyle = {
577
+ display: "flex",
578
+ flexDirection: "column",
579
+ gap: 4
580
+ };
581
+ function listItemStyle(active, signed) {
582
+ return {
583
+ display: "flex",
584
+ alignItems: "center",
585
+ gap: 10,
586
+ padding: "8px 10px",
587
+ borderRadius: 6,
588
+ background: active ? "var(--doc-surface-2, #f1f5f9)" : signed ? "transparent" : "transparent",
589
+ border: active ? "1px solid var(--doc-border, #cbd5e1)" : "1px solid transparent",
590
+ opacity: signed && !active ? 0.7 : 1
591
+ };
592
+ }
593
+ function listIconStyle(signed) {
594
+ return {
595
+ display: "inline-flex",
596
+ alignItems: "center",
597
+ justifyContent: "center",
598
+ width: 22,
599
+ height: 22,
600
+ borderRadius: "50%",
601
+ background: signed ? "var(--doc-accent, #2563eb)" : "var(--doc-surface-2, #f1f5f9)",
602
+ color: signed ? "#fff" : "var(--doc-text-muted, #475569)",
603
+ fontSize: 12,
604
+ fontWeight: 600,
605
+ flexShrink: 0
606
+ };
607
+ }
608
+ var listLabelStyle = {
609
+ flex: 1,
610
+ fontSize: 13,
611
+ color: "var(--doc-text, #0f172a)",
612
+ fontWeight: 500
613
+ };
614
+ var optionalChipStyle = {
615
+ fontSize: 11,
616
+ color: "var(--doc-text-muted, #64748b)",
617
+ padding: "2px 6px",
618
+ background: "var(--doc-surface-2, #f1f5f9)",
619
+ borderRadius: 4
620
+ };
621
+ var editorStyle = {
622
+ display: "flex",
623
+ flexDirection: "column",
624
+ gap: 12
625
+ };
626
+ var editorHeaderStyle = {
627
+ display: "flex",
628
+ flexDirection: "column",
629
+ gap: 2
630
+ };
631
+ var editorLabelStyle = {
632
+ fontSize: 13,
633
+ fontWeight: 600,
634
+ color: "var(--doc-text, #0f172a)"
635
+ };
636
+ var editorSignerStyle = {
637
+ fontSize: 12,
638
+ color: "var(--doc-text-muted, #64748b)"
639
+ };
640
+ var methodTabsStyle = {
641
+ display: "flex",
642
+ gap: 4,
643
+ padding: 2,
644
+ background: "var(--doc-surface-2, #f1f5f9)",
645
+ borderRadius: 6
646
+ };
647
+ function methodTabStyle(selected) {
648
+ return {
649
+ flex: 1,
650
+ padding: "6px 10px",
651
+ background: selected ? "var(--doc-surface, #fff)" : "transparent",
652
+ border: "none",
653
+ borderRadius: 4,
654
+ fontSize: 12,
655
+ fontWeight: 500,
656
+ color: selected ? "var(--doc-text, #0f172a)" : "var(--doc-text-muted, #475569)",
657
+ cursor: "pointer",
658
+ fontFamily: "inherit"
659
+ };
660
+ }
661
+ var captureWrapStyle = {
662
+ display: "flex",
663
+ flexDirection: "column",
664
+ gap: 8
665
+ };
666
+ var completeBlockStyle = {
667
+ padding: "10px 12px",
668
+ background: "rgba(34, 197, 94, 0.08)",
669
+ border: "1px solid rgba(34, 197, 94, 0.28)",
670
+ borderRadius: 6,
671
+ fontSize: 12,
672
+ color: "rgb(20, 83, 45)"
673
+ };
674
+ var footerStyle = {
675
+ display: "flex",
676
+ justifyContent: "flex-end",
677
+ gap: 8,
678
+ marginTop: "auto",
679
+ paddingTop: 12,
680
+ borderTop: "1px solid var(--doc-border-light, #e2e8f0)"
681
+ };
682
+ function primaryBtnStyle2(disabled) {
683
+ return {
684
+ padding: "8px 16px",
685
+ borderRadius: 6,
686
+ border: "1px solid transparent",
687
+ background: disabled ? "var(--doc-border, #cbd5e1)" : "var(--doc-accent, #2563eb)",
688
+ color: disabled ? "var(--doc-text-muted, #64748b)" : "#fff",
689
+ fontSize: 13,
690
+ fontWeight: 600,
691
+ cursor: disabled ? "not-allowed" : "pointer",
692
+ fontFamily: "inherit"
693
+ };
694
+ }
695
+ function secondaryBtnStyle2() {
696
+ return {
697
+ padding: "8px 16px",
698
+ borderRadius: 6,
699
+ border: "1px solid var(--doc-border, #cbd5e1)",
700
+ background: "transparent",
701
+ color: "var(--doc-text, #0f172a)",
702
+ fontSize: 13,
703
+ fontWeight: 500,
704
+ cursor: "pointer",
705
+ fontFamily: "inherit"
706
+ };
707
+ }
708
+
709
+ // src/embed/protocol.ts
710
+ function isCasualEnvelope(value) {
711
+ if (!value || typeof value !== "object") return false;
712
+ const v = value;
713
+ return typeof v.type === "string" && v.type.startsWith("casual.") && (v.app === "docs" || v.app === "sheet") && v.v === 1 && "data" in v;
714
+ }
715
+
716
+ // src/embed/EmbedTransport.ts
717
+ var EmbedTransport = class {
718
+ opts;
719
+ handlers = {};
720
+ destroyed = false;
721
+ pendingRequests = /* @__PURE__ */ new Map();
722
+ constructor(opts) {
723
+ this.opts = {
724
+ ...opts,
725
+ parentWindow: opts.parentWindow ?? (typeof window !== "undefined" ? window.parent : { postMessage: () => void 0 }),
726
+ hostWindow: opts.hostWindow ?? (typeof window !== "undefined" ? window : {
727
+ addEventListener: () => void 0,
728
+ removeEventListener: () => void 0
729
+ })
730
+ };
731
+ this.boundMessage = this.boundMessage.bind(this);
732
+ this.opts.hostWindow.addEventListener("message", this.boundMessage);
733
+ }
734
+ /** Replaces the handler set. Idempotent — call multiple times. */
735
+ on(handlers) {
736
+ this.handlers = { ...this.handlers, ...handlers };
737
+ }
738
+ /** Editor → Host: announce ourselves. Call once after handlers are wired. */
739
+ sendHello() {
740
+ const data = {
741
+ capabilities: this.opts.capabilities,
742
+ version: this.opts.version,
743
+ commit: this.opts.commit
744
+ };
745
+ this.post("casual.hello", data);
746
+ }
747
+ /** Editor → Host: editor is ready to receive commands. */
748
+ sendReady() {
749
+ this.post("casual.ready", {});
750
+ }
751
+ /** Editor → Host: ask the host to supply document bytes for `docId`. */
752
+ async requestLoad(docId, timeoutMs = 3e4) {
753
+ return this.request(
754
+ "casual.load.request",
755
+ { docId },
756
+ timeoutMs
757
+ );
758
+ }
759
+ /** Editor → Host: ask the host to persist `bytes`. */
760
+ async requestSave(req, timeoutMs = 3e4) {
761
+ return this.request("casual.save.request", req, timeoutMs, [req.bytes]);
762
+ }
763
+ /** Editor → Host: selection moved. Fire-and-forget. */
764
+ sendSelectionChanged(data) {
765
+ this.post("casual.selection.changed", data);
766
+ }
767
+ /** Editor → Host: a noteworthy event. */
768
+ sendTelemetry(data) {
769
+ this.post("casual.telemetry.event", data);
770
+ }
771
+ /** Editor → Host: per-field progress during a signing session. */
772
+ sendSignatureFieldSigned(data) {
773
+ this.post("casual.signature.field.signed", data, [data.bytes]);
774
+ }
775
+ /** Editor → Host: signing session is finished. */
776
+ sendSignatureComplete(data) {
777
+ this.post("casual.signature.complete", data, [data.bytes]);
778
+ }
779
+ /** Editor → Host: editor-side cancel of a signing session. */
780
+ sendSignatureCancel(reason) {
781
+ this.post("casual.signature.cancel", { reason });
782
+ }
783
+ /** Tear down listeners. Idempotent. */
784
+ destroy() {
785
+ if (this.destroyed) return;
786
+ this.opts.hostWindow.removeEventListener("message", this.boundMessage);
787
+ this.pendingRequests.clear();
788
+ this.destroyed = true;
789
+ }
790
+ // ---------------------------------------------------------------
791
+ // Internals
792
+ // ---------------------------------------------------------------
793
+ boundMessage(ev) {
794
+ const msg = ev;
795
+ if (msg.origin && msg.origin !== this.opts.hostOrigin) return;
796
+ if (!isCasualEnvelope(msg.data)) return;
797
+ void this.dispatch(msg.data);
798
+ }
799
+ async dispatch(env) {
800
+ if (env.id && this.pendingRequests.has(env.id)) {
801
+ const resolve = this.pendingRequests.get(env.id);
802
+ this.pendingRequests.delete(env.id);
803
+ resolve(env);
804
+ return;
805
+ }
806
+ switch (env.type) {
807
+ case "casual.hello":
808
+ await this.handlers.onHostHello?.(env.data);
809
+ this.sendReady();
810
+ return;
811
+ case "casual.command.setReadOnly":
812
+ await this.handlers.onCommandSetReadOnly?.(env.data);
813
+ return;
814
+ case "casual.command.setTheme":
815
+ await this.handlers.onCommandSetTheme?.(env.data);
816
+ return;
817
+ case "casual.command.setLocale":
818
+ await this.handlers.onCommandSetLocale?.(env.data);
819
+ return;
820
+ case "casual.command.focus":
821
+ await this.handlers.onCommandFocus?.();
822
+ return;
823
+ case "casual.command.save":
824
+ await this.handlers.onCommandSave?.();
825
+ return;
826
+ case "casual.command.load":
827
+ await this.handlers.onCommandLoad?.();
828
+ return;
829
+ case "casual.signature.request": {
830
+ const ack = await this.handlers.onSignatureRequest?.(
831
+ env.data
832
+ ) ?? {
833
+ ok: false,
834
+ code: "unhandled"
835
+ };
836
+ if (env.id) {
837
+ this.postReply(env.id, "casual.signature.request.ack", ack);
838
+ }
839
+ return;
840
+ }
841
+ case "casual.signature.cancel":
842
+ await this.handlers.onSignatureCancel?.(env.data);
843
+ return;
844
+ default:
845
+ return;
846
+ }
847
+ }
848
+ post(type, data, transfer) {
849
+ const env = { type, app: this.opts.app, v: 1, data };
850
+ try {
851
+ this.opts.parentWindow.postMessage(env, this.opts.hostOrigin, transfer);
852
+ } catch {
853
+ }
854
+ }
855
+ postReply(id, type, data) {
856
+ const env = { type, app: this.opts.app, id, v: 1, data };
857
+ try {
858
+ this.opts.parentWindow.postMessage(env, this.opts.hostOrigin);
859
+ } catch {
860
+ }
861
+ }
862
+ async request(type, data, timeoutMs, transfer) {
863
+ const id = newRequestId();
864
+ return new Promise((resolve, reject) => {
865
+ const timer = timeoutMs > 0 ? setTimeout(() => {
866
+ this.pendingRequests.delete(id);
867
+ reject(new Error(`Embed request ${type} timed out after ${timeoutMs}ms`));
868
+ }, timeoutMs) : null;
869
+ this.pendingRequests.set(id, (env2) => {
870
+ if (timer) clearTimeout(timer);
871
+ resolve(env2.data);
872
+ });
873
+ const env = { type, app: this.opts.app, id, v: 1, data };
874
+ try {
875
+ this.opts.parentWindow.postMessage(env, this.opts.hostOrigin, transfer);
876
+ } catch (err) {
877
+ if (timer) clearTimeout(timer);
878
+ this.pendingRequests.delete(id);
879
+ reject(err);
880
+ }
881
+ });
882
+ }
883
+ };
884
+ function newRequestId() {
885
+ return Math.random().toString(16).slice(2, 10);
886
+ }
887
+ // Annotate the CommonJS export names for ESM import in node:
888
+ 0 && (module.exports = {
889
+ DrawnSignaturePad,
890
+ EmbedTransport,
891
+ SigningPane,
892
+ SigningProvider,
893
+ TypedSignatureField,
894
+ UploadedSignatureField,
895
+ createSigningController,
896
+ isCasualEnvelope,
897
+ useSigning
898
+ });
899
+ //# sourceMappingURL=index.cjs.map