@embedpdf/plugin-form 3.0.0-next.19 → 3.0.0-next.20
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/contract/host.cjs.map +1 -1
- package/dist/contract/host.d.cts +3 -3
- package/dist/contract/host.d.ts +3 -3
- package/dist/contract/host.js +1 -1
- package/dist/contract/host.js.map +1 -1
- package/dist/{contract-DcncvPcF.js → contract-BdEaLXUK.js} +3 -3
- package/dist/contract-BdEaLXUK.js.map +1 -0
- package/dist/contract-CU0jrI_D.cjs.map +1 -1
- package/dist/{contract-D-yQxw3H.d.cts → contract-D_9fiTkd.d.ts} +58 -5
- package/dist/{contract-BwcgtCOD.d.ts → contract-c_wuhKk1.d.cts} +58 -5
- package/dist/contract.d.cts +2 -2
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +1 -1
- package/dist/{host-contract-BRYUbpT_.d.ts → host-contract-DaauiYfw.d.cts} +38 -6
- package/dist/{host-contract-1epLfK1S.d.cts → host-contract-DdOsYM83.d.ts} +38 -6
- package/dist/index.cjs +280 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -6
- package/dist/index.d.ts +9 -6
- package/dist/index.js +283 -25
- package/dist/index.js.map +1 -1
- package/dist/scripting.d.cts +1 -1
- package/dist/scripting.d.ts +1 -1
- package/package.json +17 -14
- package/dist/contract-DcncvPcF.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -5,13 +5,16 @@ const require_contract_host = require("./contract/host.cjs");
|
|
|
5
5
|
let _embedpdf_core = require("@embedpdf/core");
|
|
6
6
|
let _embedpdf_plugin_actions_contract = require("@embedpdf/plugin-actions/contract");
|
|
7
7
|
let _embedpdf_plugin_annotation_contract = require("@embedpdf/plugin-annotation/contract");
|
|
8
|
+
let _embedpdf_plugin_fonts_contract = require("@embedpdf/plugin-fonts/contract");
|
|
8
9
|
let _embedpdf_plugin_interaction_contract_host = require("@embedpdf/plugin-interaction/contract/host");
|
|
9
10
|
let _embedpdf_plugin_render_contract = require("@embedpdf/plugin-render/contract");
|
|
10
11
|
let _embedpdf_engine_core_runtime = require("@embedpdf/engine-core/runtime");
|
|
11
12
|
let _embedpdf_plugin_actions_contract_host = require("@embedpdf/plugin-actions/contract/host");
|
|
12
13
|
let _embedpdf_plugin_annotation_contract_host = require("@embedpdf/plugin-annotation/contract/host");
|
|
13
14
|
let _embedpdf_core_annotation = require("@embedpdf/core-annotation");
|
|
15
|
+
let _embedpdf_plugin_stage_contract = require("@embedpdf/plugin-stage/contract");
|
|
14
16
|
let _embedpdf_core_form = require("@embedpdf/core-form");
|
|
17
|
+
let _embedpdf_core_fonts = require("@embedpdf/core-fonts");
|
|
15
18
|
let _embedpdf_plugin_render_contract_host = require("@embedpdf/plugin-render/contract/host");
|
|
16
19
|
let _embedpdf_engine_core_public = require("@embedpdf/engine-core/public");
|
|
17
20
|
//#region src/tools/definitions.ts
|
|
@@ -313,6 +316,7 @@ function connectForm(ctx, form, lender) {
|
|
|
313
316
|
if (!actions) return;
|
|
314
317
|
ctx.cleanup(actions.registerFormCommitSink((effects) => form.commitScriptFormEffects(effects)));
|
|
315
318
|
ctx.cleanup(actions.registerFormReader(() => form.getSnapshot()));
|
|
319
|
+
ctx.cleanup(actions.registerFormFocus((name) => form.focus((0, _embedpdf_engine_core_runtime.toFieldRef)(name))));
|
|
316
320
|
ctx.cleanup(actions.registerSubmitResolver((intent, actionContext, diagnose) => form.resolveSubmitDataset(intent, actionContext, diagnose)));
|
|
317
321
|
ctx.cleanup(actions.registerExecutor("reset-form", async (node, actionContext) => {
|
|
318
322
|
if (node.type !== "reset-form") return {
|
|
@@ -644,7 +648,7 @@ function widgetAt(index, widgets, point) {
|
|
|
644
648
|
}
|
|
645
649
|
return best;
|
|
646
650
|
}
|
|
647
|
-
const initialFormModel = () => ({});
|
|
651
|
+
const initialFormModel = () => ({ focused: null });
|
|
648
652
|
//#endregion
|
|
649
653
|
//#region src/read/fields.ts
|
|
650
654
|
/**
|
|
@@ -1032,8 +1036,6 @@ function fillItems(index, pageObjectNumber, widgets, mayFill) {
|
|
|
1032
1036
|
* folded in, and the widget hit test.
|
|
1033
1037
|
*/
|
|
1034
1038
|
const NO_WIDGETS = Object.freeze([]);
|
|
1035
|
-
/** Whether the `fillable` setting lets people fill in a field of this family by hand. */
|
|
1036
|
-
const isFillableByHand = (fillable, family) => typeof fillable === "boolean" ? fillable : fillable.includes(family);
|
|
1037
1039
|
const NOTHING_SHOWN$1 = Object.freeze([]);
|
|
1038
1040
|
function createWidgetReads(ctx, { fields, rights, looks, pictures }) {
|
|
1039
1041
|
/** Every page's widgets come with the form: one read for the document. */
|
|
@@ -1045,14 +1047,12 @@ function createWidgetReads(ctx, { fields, rights, looks, pictures }) {
|
|
|
1045
1047
|
if (!page) return null;
|
|
1046
1048
|
return widgetAt(fields.view(), widgetsOn(page.objectNumber), point);
|
|
1047
1049
|
};
|
|
1048
|
-
const fillable = () => ctx.settings().get().fillable;
|
|
1049
|
-
const mayWrite = (field) => isFillableByHand(fillable(), field.family) && (field.family === "signature" ? rights.maySign(field) : rights.mayFill(field));
|
|
1050
1050
|
const widgetsOfPage = (0, _embedpdf_core.memoByKey)((pageObjectNumber) => [
|
|
1051
1051
|
fields.view(),
|
|
1052
1052
|
widgetsOn(pageObjectNumber),
|
|
1053
1053
|
rights.key(),
|
|
1054
|
-
|
|
1055
|
-
], (pageObjectNumber, index, widgets) => fillItems(index, pageObjectNumber, widgets,
|
|
1054
|
+
ctx.settings().get().fillable
|
|
1055
|
+
], (pageObjectNumber, index, widgets) => fillItems(index, pageObjectNumber, widgets, rights.mayFillByHand));
|
|
1056
1056
|
/** The page's content box (`{0, 0, width, height}`), for page-bound placement. */
|
|
1057
1057
|
const getPageBox = (page) => {
|
|
1058
1058
|
const size = ctx.getPage(page)?.size;
|
|
@@ -1078,6 +1078,7 @@ function createWidgetReads(ctx, { fields, rights, looks, pictures }) {
|
|
|
1078
1078
|
renderAppearances: pictures.render,
|
|
1079
1079
|
getWidgetPainting: looks.getWidgetPainting,
|
|
1080
1080
|
getTextEditorLayout: looks.getTextEditorLayout,
|
|
1081
|
+
getWidgetFontCss: looks.getWidgetFontCss,
|
|
1081
1082
|
ensureLoaded: loadWidgets,
|
|
1082
1083
|
getWidgetAt,
|
|
1083
1084
|
getPageBox
|
|
@@ -1383,6 +1384,90 @@ function createFieldsMirror(ctx, events) {
|
|
|
1383
1384
|
return fields;
|
|
1384
1385
|
}
|
|
1385
1386
|
//#endregion
|
|
1387
|
+
//#region src/services/cursor.ts
|
|
1388
|
+
/** How long a box asked for keeps its claim on the cursor while its page comes in. */
|
|
1389
|
+
const PENDING_MS = 2e3;
|
|
1390
|
+
function createCursor(ctx, fields, events) {
|
|
1391
|
+
const targets = /* @__PURE__ */ new Map();
|
|
1392
|
+
let pending = null;
|
|
1393
|
+
let leaving = null;
|
|
1394
|
+
let last = null;
|
|
1395
|
+
const setFocused = (next) => {
|
|
1396
|
+
const previous = ctx.state.get().focused;
|
|
1397
|
+
if (previous?.widget === next?.widget && previous?.field === next?.field) return;
|
|
1398
|
+
ctx.state.update((state) => ({
|
|
1399
|
+
...state,
|
|
1400
|
+
focused: next
|
|
1401
|
+
}));
|
|
1402
|
+
const field = next ? fieldByKey(fields.view(), next.field) : null;
|
|
1403
|
+
const row = next ? widgetRowOf(fields.view(), next.widget) : null;
|
|
1404
|
+
events.focusChanged.emit({
|
|
1405
|
+
field,
|
|
1406
|
+
widget: row?.ref ?? null
|
|
1407
|
+
});
|
|
1408
|
+
};
|
|
1409
|
+
const clearPending = () => {
|
|
1410
|
+
pending?.expire();
|
|
1411
|
+
pending = null;
|
|
1412
|
+
};
|
|
1413
|
+
return {
|
|
1414
|
+
focused: () => ctx.state.get().focused,
|
|
1415
|
+
last: () => last,
|
|
1416
|
+
note(event, fieldRef, widgetRef) {
|
|
1417
|
+
const widget = widgetObjectOf(widgetRef);
|
|
1418
|
+
if (event === "focus") {
|
|
1419
|
+
leaving?.();
|
|
1420
|
+
leaving = null;
|
|
1421
|
+
if (pending && pending.widget !== widget) clearPending();
|
|
1422
|
+
const field = fieldByRef(fields.view(), fieldRef);
|
|
1423
|
+
last = widget;
|
|
1424
|
+
setFocused(field ? {
|
|
1425
|
+
field: fieldKeyOf(field),
|
|
1426
|
+
widget
|
|
1427
|
+
} : null);
|
|
1428
|
+
} else if (ctx.state.get().focused?.widget === widget) {
|
|
1429
|
+
leaving?.();
|
|
1430
|
+
leaving = ctx.clock.after(0, () => {
|
|
1431
|
+
leaving = null;
|
|
1432
|
+
setFocused(null);
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
},
|
|
1436
|
+
goTo(row, select) {
|
|
1437
|
+
const widget = widgetObjectOf(row.ref);
|
|
1438
|
+
const stage = ctx.tryGet(_embedpdf_plugin_stage_contract.StageToken);
|
|
1439
|
+
stage?.reveal(row.page, { rect: row.rect });
|
|
1440
|
+
clearPending();
|
|
1441
|
+
const target = targets.get(widget);
|
|
1442
|
+
if (target) {
|
|
1443
|
+
target.focus({ select });
|
|
1444
|
+
return true;
|
|
1445
|
+
}
|
|
1446
|
+
if (!stage) return false;
|
|
1447
|
+
pending = {
|
|
1448
|
+
widget,
|
|
1449
|
+
select,
|
|
1450
|
+
expire: ctx.clock.after(PENDING_MS, () => {
|
|
1451
|
+
if (pending?.widget === widget) pending = null;
|
|
1452
|
+
})
|
|
1453
|
+
};
|
|
1454
|
+
return true;
|
|
1455
|
+
},
|
|
1456
|
+
attach(widgetRef, target) {
|
|
1457
|
+
const widget = widgetObjectOf(widgetRef);
|
|
1458
|
+
targets.set(widget, target);
|
|
1459
|
+
if (pending?.widget === widget) {
|
|
1460
|
+
const { select } = pending;
|
|
1461
|
+
clearPending();
|
|
1462
|
+
target.focus({ select });
|
|
1463
|
+
}
|
|
1464
|
+
return () => {
|
|
1465
|
+
if (targets.get(widget) === target) targets.delete(widget);
|
|
1466
|
+
};
|
|
1467
|
+
}
|
|
1468
|
+
};
|
|
1469
|
+
}
|
|
1470
|
+
//#endregion
|
|
1386
1471
|
//#region src/services/design.ts
|
|
1387
1472
|
/**
|
|
1388
1473
|
* Whether the form's widgets are lent to the annotation plugin now
|
|
@@ -1403,7 +1488,8 @@ function createEvents(ctx) {
|
|
|
1403
1488
|
fieldUpdated: ctx.events.source(),
|
|
1404
1489
|
fieldDeleted: ctx.events.source(),
|
|
1405
1490
|
validationRejected: ctx.events.source(),
|
|
1406
|
-
resynced: ctx.events.source()
|
|
1491
|
+
resynced: ctx.events.source(),
|
|
1492
|
+
focusChanged: ctx.events.source()
|
|
1407
1493
|
};
|
|
1408
1494
|
}
|
|
1409
1495
|
//#endregion
|
|
@@ -1414,14 +1500,19 @@ function createEvents(ctx) {
|
|
|
1414
1500
|
* write. A field's group decides: a `fields:fill` (`fields:sign`) permission
|
|
1415
1501
|
* for its group, or `doc.forms.fill` (`doc.sign`) for every field.
|
|
1416
1502
|
*/
|
|
1503
|
+
/** Whether the `fillable` setting lets people fill in a field of this family by hand. */
|
|
1504
|
+
const isFillableByHand = (fillable, family) => typeof fillable === "boolean" ? fillable : fillable.includes(family);
|
|
1417
1505
|
function createFillRights(ctx, fields) {
|
|
1418
1506
|
const mayFill = (field) => ctx.allowsField("fill", field);
|
|
1419
1507
|
const maySign = (field) => ctx.allowsField("sign", field);
|
|
1508
|
+
const mayFillByHand = (field) => isFillableByHand(ctx.settings().get().fillable, field.family) && (field.family === "signature" ? maySign(field) : mayFill(field));
|
|
1420
1509
|
/** The form's groups, `null` (no group) among them. */
|
|
1421
1510
|
const groups = (0, _embedpdf_core.memo)(() => [fields.view()], (index) => [...new Set((index.snapshot?.fields ?? []).map((field) => field.groupId))]);
|
|
1422
1511
|
return {
|
|
1423
1512
|
mayFill,
|
|
1424
1513
|
maySign,
|
|
1514
|
+
mayFillByHand,
|
|
1515
|
+
takesInput: (field) => !field.readOnly && mayFillByHand(field),
|
|
1425
1516
|
assertMayFill: (field, operation) => ctx.assertAllowedField("fill", field, operation),
|
|
1426
1517
|
key: () => groups().map((groupId) => `${mayFill({ groupId }) ? 1 : 0}${maySign({ groupId }) ? 1 : 0}`).join("")
|
|
1427
1518
|
};
|
|
@@ -1474,7 +1565,14 @@ function createFormattedTexts(ctx, fields) {
|
|
|
1474
1565
|
*/
|
|
1475
1566
|
const NOTHING_SHOWN = Object.freeze([]);
|
|
1476
1567
|
const numberOf = (ref) => ref.kind === "objectNumber" ? ref.objectNumber : -1;
|
|
1477
|
-
function createWidgetLooks({ fields, formatted, design }) {
|
|
1568
|
+
function createWidgetLooks({ fields, formatted, design, fonts }) {
|
|
1569
|
+
/** Moves when the fonts plugin may draw a widget's text differently. */
|
|
1570
|
+
const fontsEpoch = () => fonts()?.getPaintFontsEpoch() ?? 0;
|
|
1571
|
+
/**
|
|
1572
|
+
* The fonts `row` draws `text` in: the fonts plugin's (`null` while it finds them), else
|
|
1573
|
+
* `undefined`, the widget's standard font.
|
|
1574
|
+
*/
|
|
1575
|
+
const paintFontsOf = (row, text) => fonts()?.getPaintFonts(row.fontFamily ?? "helvetica", text);
|
|
1478
1576
|
/** What the widget's picture depends on, as one string. */
|
|
1479
1577
|
const lookKeyOf = (row, field) => {
|
|
1480
1578
|
const box = row.box ?? row.rect;
|
|
@@ -1518,7 +1616,8 @@ function createWidgetLooks({ fields, formatted, design }) {
|
|
|
1518
1616
|
fields.view(),
|
|
1519
1617
|
fields.get(),
|
|
1520
1618
|
formatted.version(),
|
|
1521
|
-
design.lending()
|
|
1619
|
+
design.lending(),
|
|
1620
|
+
fontsEpoch()
|
|
1522
1621
|
], (pageObjectNumber, view, truth, _formatted, lent) => lent ? NOTHING_SHOWN : shownWidgetsOf(view, pageObjectNumber, (row) => {
|
|
1523
1622
|
const number = numberOf(row.ref);
|
|
1524
1623
|
return {
|
|
@@ -1526,15 +1625,22 @@ function createWidgetLooks({ fields, formatted, design }) {
|
|
|
1526
1625
|
pictureVersion: pictureVersionOf(truth, number)
|
|
1527
1626
|
};
|
|
1528
1627
|
}));
|
|
1529
|
-
/**
|
|
1530
|
-
|
|
1628
|
+
/**
|
|
1629
|
+
* The painter's drawing of `row` (in the view's field, as this session formatted it) at `box`,
|
|
1630
|
+
* turned. While the fonts its text needs are found, `null`, so the engine's picture stays;
|
|
1631
|
+
* unless `now`, then in the widget's standard font.
|
|
1632
|
+
*/
|
|
1633
|
+
const paint = (row, box, rotation, now = false) => {
|
|
1531
1634
|
const field = fieldForWidget(fields.view(), numberOf(row.ref));
|
|
1532
1635
|
if (!field) return null;
|
|
1533
1636
|
const text = formatted.textOf(field);
|
|
1637
|
+
const shown = text === null ? {} : { text };
|
|
1638
|
+
const painted = paintFontsOf(row, (0, _embedpdf_core_form.widgetText)(row, field, shown));
|
|
1639
|
+
if (painted === null && !now) return null;
|
|
1534
1640
|
const drawing = (0, _embedpdf_core_form.widgetDrawing)({
|
|
1535
1641
|
...row,
|
|
1536
1642
|
box
|
|
1537
|
-
}, field,
|
|
1643
|
+
}, field, shown, painted ?? void 0);
|
|
1538
1644
|
return drawing && (0, _embedpdf_core_form.widgetScene)(drawing, {
|
|
1539
1645
|
box,
|
|
1540
1646
|
rotation
|
|
@@ -1548,12 +1654,36 @@ function createWidgetLooks({ fields, formatted, design }) {
|
|
|
1548
1654
|
return [
|
|
1549
1655
|
row,
|
|
1550
1656
|
field,
|
|
1551
|
-
field ? formatted.textOf(field) : null
|
|
1657
|
+
field ? formatted.textOf(field) : null,
|
|
1658
|
+
fontsEpoch()
|
|
1552
1659
|
];
|
|
1553
1660
|
}, (_number, row) => {
|
|
1554
1661
|
const scene = row && paint(row, row.box ?? row.rect, row.rotation);
|
|
1555
1662
|
return scene ? { scene } : null;
|
|
1556
1663
|
});
|
|
1664
|
+
/**
|
|
1665
|
+
* How a browser draws text in a widget's font: the fonts plugin's CSS for
|
|
1666
|
+
* it (the first widget in a font that isn't standard reads the document's
|
|
1667
|
+
* fonts); without the plugin, the standard font the engine falls back to.
|
|
1668
|
+
*/
|
|
1669
|
+
const fontCssOf = (font) => fonts()?.getFontCss(font) ?? (0, _embedpdf_core_fonts.standardFontCss)((0, _embedpdf_core_fonts.standardFontOf)(font));
|
|
1670
|
+
/**
|
|
1671
|
+
* Until the fonts plugin has found a field's fonts, the layout in its own
|
|
1672
|
+
* font, when it isn't a standard one: its program, the fallbacks and the
|
|
1673
|
+
* last resort, as the fonts plugin draws it. The layout's numbers stay the
|
|
1674
|
+
* standard face's until then.
|
|
1675
|
+
*/
|
|
1676
|
+
const inItsFont = (layout, font) => {
|
|
1677
|
+
if (!fonts() || !font || (0, _embedpdf_engine_core_runtime.isStandardFontName)(font)) return layout;
|
|
1678
|
+
const css = fontCssOf(font);
|
|
1679
|
+
return {
|
|
1680
|
+
...layout,
|
|
1681
|
+
programs: css.programs,
|
|
1682
|
+
fontFamily: css.fontFamily,
|
|
1683
|
+
fontWeight: css.fontWeight,
|
|
1684
|
+
fontStyle: css.fontStyle
|
|
1685
|
+
};
|
|
1686
|
+
};
|
|
1557
1687
|
return {
|
|
1558
1688
|
listShownWidgets: (pageObjectNumber) => shownOn(pageObjectNumber),
|
|
1559
1689
|
getWidgetPainting: (ref) => sceneOf(numberOf(ref)),
|
|
@@ -1568,14 +1698,21 @@ function createWidgetLooks({ fields, formatted, design }) {
|
|
|
1568
1698
|
*/
|
|
1569
1699
|
painterOf: (row) => {
|
|
1570
1700
|
const field = fieldForWidget(fields.view(), numberOf(row.ref));
|
|
1571
|
-
return field && (0, _embedpdf_core_form.paints)(field) ? (box) => paint(row, box, 0) ?? [] : null;
|
|
1701
|
+
return field && (0, _embedpdf_core_form.paints)(field) ? (box) => paint(row, box, 0, true) ?? [] : null;
|
|
1572
1702
|
},
|
|
1573
1703
|
getTextEditorLayout: (ref, text) => {
|
|
1574
1704
|
const view = fields.view();
|
|
1575
1705
|
const number = numberOf(ref);
|
|
1576
1706
|
const row = widgetRowOf(view, number);
|
|
1577
1707
|
const field = row ? fieldForWidget(view, number) : null;
|
|
1578
|
-
|
|
1708
|
+
if (!row || !field) return null;
|
|
1709
|
+
const painted = paintFontsOf(row, (0, _embedpdf_core_form.widgetText)(row, field, { text }));
|
|
1710
|
+
const layout = (0, _embedpdf_core_form.textEditorLayout)(row, field, text, painted ?? void 0);
|
|
1711
|
+
return layout && (painted ? layout : inItsFont(layout, row.fontFamily ?? null));
|
|
1712
|
+
},
|
|
1713
|
+
getWidgetFontCss: (ref) => {
|
|
1714
|
+
const row = widgetRowOf(fields.view(), numberOf(ref));
|
|
1715
|
+
return row ? fontCssOf(row.fontFamily ?? "helvetica") : null;
|
|
1579
1716
|
}
|
|
1580
1717
|
};
|
|
1581
1718
|
}
|
|
@@ -1690,6 +1827,7 @@ function createServices(ctx) {
|
|
|
1690
1827
|
fields,
|
|
1691
1828
|
rights: createFillRights(ctx, fields),
|
|
1692
1829
|
events,
|
|
1830
|
+
cursor: createCursor(ctx, fields, events),
|
|
1693
1831
|
siblings,
|
|
1694
1832
|
scripting: createScriptingSeam(ctx, siblings),
|
|
1695
1833
|
design,
|
|
@@ -1698,7 +1836,8 @@ function createServices(ctx) {
|
|
|
1698
1836
|
looks: createWidgetLooks({
|
|
1699
1837
|
fields,
|
|
1700
1838
|
formatted,
|
|
1701
|
-
design
|
|
1839
|
+
design,
|
|
1840
|
+
fonts: () => ctx.tryGet(_embedpdf_plugin_fonts_contract.FontsToken)
|
|
1702
1841
|
}),
|
|
1703
1842
|
enqueue: ctx.serialQueue("writes"),
|
|
1704
1843
|
keyOf: (ref) => canonicalKey(fields.view(), ref)
|
|
@@ -1712,7 +1851,7 @@ function createServices(ctx) {
|
|
|
1712
1851
|
* tree-less widget falls back to the form's own scripted activation.
|
|
1713
1852
|
*/
|
|
1714
1853
|
function createActivation(ctx, services, { fill }) {
|
|
1715
|
-
const { fields, scripting } = services;
|
|
1854
|
+
const { fields, scripting, cursor } = services;
|
|
1716
1855
|
/** The widget's activation action, from its row in the form. */
|
|
1717
1856
|
const annotationActivation = async (ref) => {
|
|
1718
1857
|
await fields.settled();
|
|
@@ -1790,6 +1929,7 @@ function createActivation(ctx, services, { fill }) {
|
|
|
1790
1929
|
};
|
|
1791
1930
|
},
|
|
1792
1931
|
notifyWidgetEvent: (fieldRef, annotationRef, event) => {
|
|
1932
|
+
if (event === "focus" || event === "blur") cursor.note(event, fieldRef, annotationRef);
|
|
1793
1933
|
const actions = ctx.tryGet(_embedpdf_plugin_actions_contract.ActionsToken);
|
|
1794
1934
|
if (!actions) return;
|
|
1795
1935
|
const source = widgetSource(fieldRef, annotationRef);
|
|
@@ -2368,6 +2508,115 @@ function failedFill(result) {
|
|
|
2368
2508
|
return new _embedpdf_core.PluginError("operation-failed", "form", result.error?.message ?? result.diagnostics[0]?.message ?? "the write failed");
|
|
2369
2509
|
}
|
|
2370
2510
|
//#endregion
|
|
2511
|
+
//#region src/write/focus.ts
|
|
2512
|
+
/**
|
|
2513
|
+
* Moving the cursor between fields: `focus()`, `focusNext()` (what Tab does), the arrow keys in a
|
|
2514
|
+
* radio group, and the field the cursor is in. This area decides where the cursor goes: which
|
|
2515
|
+
* field, which of its boxes, in Tab's order (the engine's `tabOrderOf()`); the cursor service
|
|
2516
|
+
* (`services/cursor.ts`) brings the box into view and hands it to its control.
|
|
2517
|
+
*/
|
|
2518
|
+
/** A radio button as its field lists it: whether it's the checked one, and the value it picks. */
|
|
2519
|
+
const radioButtonOf = (field, widget) => field.family === "radio" ? field.widgets.find((candidate) => candidate.objectNumber === widget) ?? null : null;
|
|
2520
|
+
function createFocus(ctx, { fields, rights, events, cursor }, fill) {
|
|
2521
|
+
/** Every box that shows, in Tab's order, and the ones Tab stops at. */
|
|
2522
|
+
const order = (0, _embedpdf_core.memo)(() => [
|
|
2523
|
+
fields.view(),
|
|
2524
|
+
ctx.document()?.pages,
|
|
2525
|
+
rights.key(),
|
|
2526
|
+
ctx.settings().get().fillable
|
|
2527
|
+
], (index, pages) => {
|
|
2528
|
+
const rows = (0, _embedpdf_engine_core_runtime.tabOrderOf)((index.snapshot?.widgets ?? []).filter(shownOnScreen), pages ?? []);
|
|
2529
|
+
const position = /* @__PURE__ */ new Map();
|
|
2530
|
+
const stops = [];
|
|
2531
|
+
const groups = /* @__PURE__ */ new Set();
|
|
2532
|
+
rows.forEach((row, at) => {
|
|
2533
|
+
const widget = widgetObjectOf(row.ref);
|
|
2534
|
+
position.set(widget, at);
|
|
2535
|
+
const field = fieldForWidget(index, widget);
|
|
2536
|
+
if (!field || !rights.takesInput(field)) return;
|
|
2537
|
+
if (field.family === "radio") {
|
|
2538
|
+
const group = `${fieldKeyOf(field)}@${row.page.objectNumber}`;
|
|
2539
|
+
if (groups.has(group)) return;
|
|
2540
|
+
groups.add(group);
|
|
2541
|
+
}
|
|
2542
|
+
stops.push({
|
|
2543
|
+
row,
|
|
2544
|
+
field,
|
|
2545
|
+
at
|
|
2546
|
+
});
|
|
2547
|
+
});
|
|
2548
|
+
return {
|
|
2549
|
+
rows,
|
|
2550
|
+
position,
|
|
2551
|
+
stops
|
|
2552
|
+
};
|
|
2553
|
+
});
|
|
2554
|
+
/** The buttons of a radio group, in Tab's order; on one page, with `page`. */
|
|
2555
|
+
const buttonsOf = (field, page) => order().rows.filter((row) => (page === void 0 || row.page.objectNumber === page) && fieldForWidget(fields.view(), widgetObjectOf(row.ref)) === field);
|
|
2556
|
+
/**
|
|
2557
|
+
* The box that takes the cursor at a stop: a radio group's checked button on that page, else
|
|
2558
|
+
* its first, or its last when Shift+Tab comes to it from after it.
|
|
2559
|
+
*/
|
|
2560
|
+
const boxAt = (stop, backward) => {
|
|
2561
|
+
if (stop.field.family !== "radio") return stop.row;
|
|
2562
|
+
const buttons = buttonsOf(stop.field, stop.row.page.objectNumber);
|
|
2563
|
+
return buttons.find((button) => radioButtonOf(stop.field, widgetObjectOf(button.ref))?.checked) ?? (backward ? buttons.at(-1) ?? stop.row : stop.row);
|
|
2564
|
+
};
|
|
2565
|
+
const focus = (ref, options = {}) => {
|
|
2566
|
+
const index = fields.view();
|
|
2567
|
+
const field = fieldByRef(index, ref);
|
|
2568
|
+
if (!field || !rights.takesInput(field)) return false;
|
|
2569
|
+
let row;
|
|
2570
|
+
if (options.widget) {
|
|
2571
|
+
const widget = widgetObjectOf(options.widget);
|
|
2572
|
+
if (fieldForWidget(index, widget) !== field) return false;
|
|
2573
|
+
row = order().rows.find((candidate) => widgetObjectOf(candidate.ref) === widget);
|
|
2574
|
+
} else {
|
|
2575
|
+
const stop = order().stops.find((candidate) => candidate.field === field);
|
|
2576
|
+
row = stop && boxAt(stop, false);
|
|
2577
|
+
}
|
|
2578
|
+
return row ? cursor.goTo(row, options.select ?? false) : false;
|
|
2579
|
+
};
|
|
2580
|
+
const focusNext = (options = {}) => {
|
|
2581
|
+
const index = fields.view();
|
|
2582
|
+
const { position, stops } = order();
|
|
2583
|
+
const backward = options.backward ?? false;
|
|
2584
|
+
const fromField = options.from ? fieldByRef(index, options.from) : null;
|
|
2585
|
+
const from = options.from ? fromField?.widgets.map(({ objectNumber }) => objectNumber).find((w) => position.has(w)) ?? null : cursor.focused()?.widget ?? cursor.last();
|
|
2586
|
+
const at = from === null ? null : position.get(from) ?? null;
|
|
2587
|
+
const field = from === null ? null : fieldForWidget(index, from);
|
|
2588
|
+
const page = from === null ? null : widgetRowOf(index, from)?.page.objectNumber;
|
|
2589
|
+
const candidates = stops.filter((stop) => (!options.empty || isEmpty(stop.field)) && !(stop.field === field && field.family === "radio" && stop.row.page.objectNumber === page));
|
|
2590
|
+
const ahead = candidates.filter((stop) => at === null ? true : backward ? stop.at < at : stop.at > at);
|
|
2591
|
+
const next = (backward ? ahead.at(-1) : ahead[0]) ?? (options.wrap ? backward ? candidates.at(-1) : candidates[0] : void 0);
|
|
2592
|
+
return next ? cursor.goTo(boxAt(next, backward), true) : false;
|
|
2593
|
+
};
|
|
2594
|
+
const moveInGroup = (ref, backward) => {
|
|
2595
|
+
const widget = widgetObjectOf(ref);
|
|
2596
|
+
const field = fieldForWidget(fields.view(), widget);
|
|
2597
|
+
if (!field || field.family !== "radio" || !rights.takesInput(field)) return false;
|
|
2598
|
+
const buttons = buttonsOf(field);
|
|
2599
|
+
const here = buttons.findIndex((row) => widgetObjectOf(row.ref) === widget);
|
|
2600
|
+
if (here < 0 || buttons.length < 2) return false;
|
|
2601
|
+
const next = buttons[(here + (backward ? buttons.length - 1 : 1)) % buttons.length];
|
|
2602
|
+
if (!cursor.goTo(next, false)) return false;
|
|
2603
|
+
const button = radioButtonOf(field, widgetObjectOf(next.ref));
|
|
2604
|
+
if (button) {
|
|
2605
|
+
const activate = () => void fill.activateWidget(next.ref).catch(() => {});
|
|
2606
|
+
fill.setValue(field.ref, { value: button.exportValue }).then(activate, activate);
|
|
2607
|
+
}
|
|
2608
|
+
return true;
|
|
2609
|
+
};
|
|
2610
|
+
return { api: {
|
|
2611
|
+
focus,
|
|
2612
|
+
focusNext,
|
|
2613
|
+
getFocusedField: (0, _embedpdf_core.memo)(() => [fields.view(), cursor.focused()], (index, focused) => focused ? fieldByKey(index, focused.field) : null),
|
|
2614
|
+
onFocusChanged: events.focusChanged.on,
|
|
2615
|
+
moveInGroup,
|
|
2616
|
+
attachFocusTarget: cursor.attach
|
|
2617
|
+
} };
|
|
2618
|
+
}
|
|
2619
|
+
//#endregion
|
|
2371
2620
|
//#region src/write/interchange.ts
|
|
2372
2621
|
function createInterchange(ctx, services) {
|
|
2373
2622
|
const { enqueue, siblings } = services;
|
|
@@ -2939,6 +3188,10 @@ function createFormController(ctx) {
|
|
|
2939
3188
|
const interchange = createInterchange(ctx, services);
|
|
2940
3189
|
const activation = createActivation(ctx, services, fills);
|
|
2941
3190
|
const scripts = createScriptEffects(ctx, services, fills);
|
|
3191
|
+
const focus = createFocus(ctx, services, {
|
|
3192
|
+
setValue: values.api.setValue,
|
|
3193
|
+
activateWidget: activation.api.activateWidget
|
|
3194
|
+
});
|
|
2942
3195
|
const api = (0, _embedpdf_core.composeApi)("form", [
|
|
2943
3196
|
settings.api,
|
|
2944
3197
|
fields.api,
|
|
@@ -2951,6 +3204,7 @@ function createFormController(ctx) {
|
|
|
2951
3204
|
interchange.api,
|
|
2952
3205
|
activation.api,
|
|
2953
3206
|
scripts.api,
|
|
3207
|
+
focus.api,
|
|
2954
3208
|
{
|
|
2955
3209
|
canRead: () => ctx.allows("doc.forms.read"),
|
|
2956
3210
|
canFill: (ref) => {
|
|
@@ -2984,7 +3238,8 @@ function createFormController(ctx) {
|
|
|
2984
3238
|
* fills every widget. Design mode is the 'form-edit' and palette tools: with
|
|
2985
3239
|
* the annotation plugin present, the form lends it the widgets, which its
|
|
2986
3240
|
* tools then select, move, restyle and delete. The render plugin draws the
|
|
2987
|
-
* widgets' pictures in both modes.
|
|
3241
|
+
* widgets' pictures in both modes. With the fonts plugin, a text field in a
|
|
3242
|
+
* font of its own is edited in that font's program. `config` is the settings the app
|
|
2988
3243
|
* registers, over {@link FORM_DEFAULTS}.
|
|
2989
3244
|
*/
|
|
2990
3245
|
const formPlugin = (config) => (0, _embedpdf_core.definePlugin)({
|
|
@@ -2995,7 +3250,8 @@ const formPlugin = (config) => (0, _embedpdf_core.definePlugin)({
|
|
|
2995
3250
|
optional: [
|
|
2996
3251
|
_embedpdf_plugin_annotation_contract.AnnotationToken,
|
|
2997
3252
|
_embedpdf_plugin_actions_contract.ActionsToken,
|
|
2998
|
-
_embedpdf_plugin_render_contract.RenderToken
|
|
3253
|
+
_embedpdf_plugin_render_contract.RenderToken,
|
|
3254
|
+
_embedpdf_plugin_fonts_contract.FontsToken
|
|
2999
3255
|
],
|
|
3000
3256
|
state: initialFormModel,
|
|
3001
3257
|
settings: {
|
|
@@ -3018,13 +3274,15 @@ const formState = (0, _embedpdf_core.defineState)(require_contract.FormToken, {
|
|
|
3018
3274
|
fields: form.list(),
|
|
3019
3275
|
status: form.getStatus(),
|
|
3020
3276
|
formKind: form.getFormKind(),
|
|
3021
|
-
selectedField: form.getSelectedField()
|
|
3277
|
+
selectedField: form.getSelectedField(),
|
|
3278
|
+
focusedField: form.getFocusedField()
|
|
3022
3279
|
}),
|
|
3023
3280
|
empty: {
|
|
3024
3281
|
fields: NO_FIELDS,
|
|
3025
3282
|
status: "idle",
|
|
3026
3283
|
formKind: "none",
|
|
3027
|
-
selectedField: null
|
|
3284
|
+
selectedField: null,
|
|
3285
|
+
focusedField: null
|
|
3028
3286
|
}
|
|
3029
3287
|
});
|
|
3030
3288
|
//#endregion
|