@embedpdf/plugin-form 3.0.0-next.1 → 3.0.0-next.10

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.js CHANGED
@@ -2,6 +2,7 @@ import { createCapabilityToken, definePlugin } from "@embedpdf/core";
2
2
  import { AnnotationToken, MIN_DRAG, resolveClickPlacement, widgetAppearanceFromProps } from "@embedpdf/plugin-annotation";
3
3
  import { AnnotationToken as AnnotationToken$1 } from "@embedpdf/plugin-annotation/internal";
4
4
  import { InteractionToken, samplePointOn } from "@embedpdf/plugin-interaction";
5
+ import { PermissionDenied } from "@embedpdf/engine-core/runtime";
5
6
  import { DEFAULT_SCRIPT_BUDGET, javaScriptProgramFromActionTree, scriptFieldsFromSnapshot } from "@embedpdf/core-acrojs";
6
7
  //#region src/core/model.ts
7
8
  const fieldKeyOf = (field) => field.fieldObjectNumber > 0 ? `obj:${field.fieldObjectNumber}` : `fqn:${field.name}`;
@@ -780,10 +781,12 @@ function createFormCapability(ctx, options = {}) {
780
781
  for (const diagnostic of result.diagnostics) observe(options.scripting?.onDiagnostic ? () => options.scripting.onDiagnostic(diagnostic) : void 0);
781
782
  if (result.error) observe(options.scripting?.onError ? () => options.scripting.onError(result.error) : void 0);
782
783
  };
784
+ const can = (cap) => ctx.doc?.security.allows(cap) ?? false;
783
785
  let refreshPromise = null;
784
786
  const refresh = async (force = false) => {
785
787
  const doc = ctx.doc;
786
788
  if (!doc) return;
789
+ if (!can("doc.forms.read")) return;
787
790
  if (refreshPromise) {
788
791
  await refreshPromise;
789
792
  if (!force) return;
@@ -791,7 +794,9 @@ function createFormCapability(ctx, options = {}) {
791
794
  refreshPromise = doc.forms.list().then((snapshot) => apply({
792
795
  t: "snapshot",
793
796
  snapshot
794
- })).finally(() => {
797
+ })).catch((err) => {
798
+ console.warn("[form] form snapshot failed to load", err);
799
+ }).finally(() => {
795
800
  refreshPromise = null;
796
801
  });
797
802
  return refreshPromise;
@@ -819,14 +824,20 @@ function createFormCapability(ctx, options = {}) {
819
824
  geomLoading.delete(pon);
820
825
  });
821
826
  };
827
+ const fuseFill = (item, fillable) => item === null || fillable || item.disabled ? item : {
828
+ ...item,
829
+ disabled: true
830
+ };
822
831
  const fillCache = /* @__PURE__ */ new Map();
823
832
  const fillItems$1 = (pon) => {
824
833
  const m = model();
834
+ const fillable = can("doc.forms.fill");
825
835
  const hit = fillCache.get(pon);
826
- if (hit && hit.seq === m.seq) return hit.items;
827
- const items = fillItems(m, pon);
836
+ if (hit && hit.seq === m.seq && hit.fillable === fillable) return hit.items;
837
+ const items = fillItems(m, pon).map((item) => fuseFill(item, fillable));
828
838
  fillCache.set(pon, {
829
839
  seq: m.seq,
840
+ fillable,
830
841
  items
831
842
  });
832
843
  return items;
@@ -834,11 +845,13 @@ function createFormCapability(ctx, options = {}) {
834
845
  const fillItemCache = /* @__PURE__ */ new Map();
835
846
  const fillItem = (annotObjectNumber) => {
836
847
  const m = model();
848
+ const fillable = can("doc.forms.fill");
837
849
  const hit = fillItemCache.get(annotObjectNumber);
838
- if (hit && hit.seq === m.seq) return hit.item;
839
- const item = fillItemForWidget(m, annotObjectNumber);
850
+ if (hit && hit.seq === m.seq && hit.fillable === fillable) return hit.item;
851
+ const item = fuseFill(fillItemForWidget(m, annotObjectNumber), fillable);
840
852
  fillItemCache.set(annotObjectNumber, {
841
853
  seq: m.seq,
854
+ fillable,
842
855
  item
843
856
  });
844
857
  return item;
@@ -862,31 +875,33 @@ function createFormCapability(ctx, options = {}) {
862
875
  diagnostics: []
863
876
  };
864
877
  };
865
- const write = (key, value) => enqueueMutation(async () => {
866
- if (!ctx.doc) return;
867
- apply({
868
- t: "writeStart",
869
- key
870
- });
871
- try {
872
- const result = await commitValueNow(refKeyOf(key), value);
873
- if (result.status === "rejected" || result.status === "failed") apply({
874
- t: "writeFailed",
875
- key
876
- });
877
- else if (result.effectsResult === null && result.scripted) apply({
878
- t: "writeFailed",
879
- key
880
- });
881
- } catch (err) {
878
+ const write = (key, value) => {
879
+ if (!can("doc.forms.fill")) return Promise.reject(new PermissionDenied("doc.forms.fill", "form.fill"));
880
+ return enqueueMutation(async () => {
881
+ if (!ctx.doc) return;
882
882
  apply({
883
- t: "writeFailed",
883
+ t: "writeStart",
884
884
  key
885
885
  });
886
- throw err;
887
- }
888
- });
889
- const can = (cap) => ctx.doc?.security.allows(cap) ?? false;
886
+ try {
887
+ const result = await commitValueNow(refKeyOf(key), value);
888
+ if (result.status === "rejected" || result.status === "failed") apply({
889
+ t: "writeFailed",
890
+ key
891
+ });
892
+ else if (result.effectsResult === null && result.scripted) apply({
893
+ t: "writeFailed",
894
+ key
895
+ });
896
+ } catch (err) {
897
+ apply({
898
+ t: "writeFailed",
899
+ key
900
+ });
901
+ throw err;
902
+ }
903
+ });
904
+ };
890
905
  const annotationHost = ctx.tryGet(AnnotationToken$1);
891
906
  const annotationActivation = async (ref) => {
892
907
  const doc = ctx.doc;
@@ -1063,28 +1078,31 @@ function createFormCapability(ctx, options = {}) {
1063
1078
  type: "choice",
1064
1079
  values
1065
1080
  }),
1066
- reset: (key) => enqueueMutation(async () => {
1067
- const doc = ctx.doc;
1068
- if (!doc) return;
1069
- apply({
1070
- t: "writeStart",
1071
- key
1072
- });
1073
- try {
1074
- const result = await doc.forms.reset(refKeyOf(key));
1081
+ reset: (key) => {
1082
+ if (!can("doc.forms.fill")) return Promise.reject(new PermissionDenied("doc.forms.fill", "form.reset"));
1083
+ return enqueueMutation(async () => {
1084
+ const doc = ctx.doc;
1085
+ if (!doc) return;
1075
1086
  apply({
1076
- t: "writeDone",
1077
- key,
1078
- field: result.field
1079
- });
1080
- } catch (err) {
1081
- apply({
1082
- t: "writeFailed",
1087
+ t: "writeStart",
1083
1088
  key
1084
1089
  });
1085
- throw err;
1086
- }
1087
- }),
1090
+ try {
1091
+ const result = await doc.forms.reset(refKeyOf(key));
1092
+ apply({
1093
+ t: "writeDone",
1094
+ key,
1095
+ field: result.field
1096
+ });
1097
+ } catch (err) {
1098
+ apply({
1099
+ t: "writeFailed",
1100
+ key
1101
+ });
1102
+ throw err;
1103
+ }
1104
+ });
1105
+ },
1088
1106
  commitValue: (ref, value) => enqueueMutation(() => commitValueNow(ref, value)),
1089
1107
  activateWidget: (key, annotationRef) => enqueueMutation(() => activateWidgetNow(key, annotationRef)),
1090
1108
  setUiEffectProvider: (provider) => {
@@ -1124,8 +1142,9 @@ function createFormCapability(ctx, options = {}) {
1124
1142
  updateField: (key, patch) => enqueueMutation(() => updateFieldNow(key, patch)),
1125
1143
  deleteField: (key) => enqueueMutation(() => deleteFieldNow(key)),
1126
1144
  detachWidget: (key, annotObjectNumber) => enqueueMutation(() => detachWidgetNow(key, annotObjectNumber)),
1145
+ canRead: () => can("doc.forms.read"),
1127
1146
  canFill: () => can("doc.forms.fill"),
1128
- canModify: () => can("doc.forms.modify")
1147
+ canDesign: () => can("doc.forms.modify")
1129
1148
  };
1130
1149
  }
1131
1150
  //#endregion
@@ -1159,7 +1178,11 @@ function registerFormEffects(ctx) {
1159
1178
  }
1160
1179
  };
1161
1180
  const unsubscribe = doc.events.subscribe((event) => {
1162
- if (!event.type.startsWith("form.")) return;
1181
+ if (event.type === "stream.desynced") {
1182
+ refresh();
1183
+ return;
1184
+ }
1185
+ if (!event.type.startsWith("form.") || !("origin" in event)) return;
1163
1186
  const structural = STRUCTURAL.has(event.type);
1164
1187
  if (event.origin.kind !== "remote" && !structural) return;
1165
1188
  if (structural) ctx.dispatch({
@@ -1275,7 +1298,7 @@ function createPlaceHandler(form, interaction, annotation) {
1275
1298
  enabledFor: (t) => t.enables.has("form-place"),
1276
1299
  onDown: (s) => {
1277
1300
  if (!s.page || !FORM_TOOL_BY_ID.has(interaction.activeToolId())) return false;
1278
- if (!form.canModify()) return false;
1301
+ if (!form.canDesign()) return false;
1279
1302
  origin = {
1280
1303
  pon: s.page.pon,
1281
1304
  start: s.page.point,