@embedpdf/plugin-annotation 3.2.0 → 3.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/dist/index.cjs CHANGED
@@ -389,7 +389,7 @@ function createEditHandler(anno, interaction) {
389
389
  claimsTouch: (sample) => !!sample.page && anno.claimsTouchAt(sample.page.ref, sample.page.point, sample.page.scale, sample.page.rotation, sample.page.zoom),
390
390
  onPointerDown: (sample) => {
391
391
  if (!sample.page) return false;
392
- if (anno.getDistanceCreationPage?.() != null) return false;
392
+ if (anno.hasCreationAwaitingClick()) return false;
393
393
  const touch = sample.pointerType === "touch";
394
394
  const wasEditing = anno.getEditingId() != null;
395
395
  if (wasEditing) anno.text.end();
@@ -429,13 +429,14 @@ function createEditHandler(anno, interaction) {
429
429
  anno.editPointer("up", page, downPoint, false, void 0, void 0, void 0, touch);
430
430
  },
431
431
  onHover: (sample) => {
432
- interaction.claimCursor("annotation", sample.page ? anno.getCursorAt(sample.page.ref, sample.page.point, sample.page.scale, sample.page.rotation, sample.page.zoom) : null, 20);
433
- anno.hoverAt(sample.page ? {
434
- page: sample.page.ref,
435
- point: sample.page.point,
436
- scale: sample.page.scale,
437
- rotation: sample.page.rotation,
438
- zoom: sample.page.zoom
432
+ const page = anno.hasCreationAwaitingClick() ? void 0 : sample.page;
433
+ interaction.claimCursor("annotation", page ? anno.getCursorAt(page.ref, page.point, page.scale, page.rotation, page.zoom) : null, 20);
434
+ anno.hoverAt(page ? {
435
+ page: page.ref,
436
+ point: page.point,
437
+ scale: page.scale,
438
+ rotation: page.rotation,
439
+ zoom: page.zoom
439
440
  } : null);
440
441
  }
441
442
  };
@@ -508,23 +509,14 @@ function createMarqueeHandler(anno) {
508
509
  * Its hover shows the tool's ghost where a click lands; the ghost stays while
509
510
  * the press is still a click, and clears when the gesture ends.
510
511
  */
511
- function createDrawHandler(anno, interaction, clock, onSettle) {
512
+ function createDrawHandler(anno, interaction) {
512
513
  const toolId = () => interaction.getActiveToolId();
513
514
  const subtypeOf = (id) => anno.getToolSubtype(id);
514
515
  let drawingPoly = false;
515
516
  let drawingCallout = false;
516
517
  let followPage = null;
517
518
  let origin = null;
518
- let pendingInk = null;
519
- const flushPendingInk = () => {
520
- if (!pendingInk) return;
521
- pendingInk.cancelWait();
522
- pendingInk = null;
523
- anno.finishInkDraft();
524
- };
525
- onSettle?.(flushPendingInk);
526
519
  interaction.onToolChanged(() => {
527
- flushPendingInk();
528
520
  drawingPoly = false;
529
521
  drawingCallout = false;
530
522
  followPage = null;
@@ -545,10 +537,6 @@ function createDrawHandler(anno, interaction, clock, onSettle) {
545
537
  return true;
546
538
  }
547
539
  if (!sample.page) return false;
548
- if (st === "ink" && pendingInk) if (pendingInk.tool === tool && pendingInk.page.objectNumber === sample.page.ref.objectNumber) {
549
- pendingInk.cancelWait();
550
- pendingInk = null;
551
- } else flushPendingInk();
552
540
  origin = {
553
541
  page: sample.page.ref,
554
542
  point: sample.page.point
@@ -580,28 +568,11 @@ function createDrawHandler(anno, interaction, clock, onSettle) {
580
568
  onPointerUp: (sample) => {
581
569
  const tool = toolId();
582
570
  const st = subtypeOf(tool);
583
- if (origin && !isPolyTool(st)) {
584
- anno.createPointer(tool, "up", origin.page, pointOn(sample, origin.page) ?? origin.point);
585
- if (st === "ink") {
586
- const groupStrokesMs = anno.getResolvedTool(tool)?.ink?.groupStrokesMs ?? 0;
587
- if (groupStrokesMs > 0) pendingInk = {
588
- tool,
589
- page: origin.page,
590
- cancelWait: clock.after(groupStrokesMs, () => {
591
- pendingInk = null;
592
- anno.finishInkDraft();
593
- })
594
- };
595
- }
596
- }
571
+ if (origin && !isPolyTool(st)) anno.createPointer(tool, "up", origin.page, pointOn(sample, origin.page) ?? origin.point);
597
572
  origin = null;
598
573
  anno.clearGhost();
599
574
  },
600
575
  onPointerCancel: () => {
601
- if (pendingInk) {
602
- pendingInk.cancelWait();
603
- pendingInk = null;
604
- }
605
576
  drawingPoly = false;
606
577
  drawingCallout = false;
607
578
  followPage = null;
@@ -676,16 +647,18 @@ function connectAnnotation(ctx, annotation) {
676
647
  id: tool.id,
677
648
  cursor: tool.cursor,
678
649
  enables: tool.enables,
679
- touch: require_contract.toolTouchOf(tool.enables)
650
+ touch: require_contract.toolTouchOf(tool.enables),
651
+ canUse: () => annotation.canCreate()
680
652
  }));
681
653
  }
682
654
  for (const handler of [
683
655
  createPlaceHandler(annotation, interaction),
684
656
  createEditHandler(annotation, interaction),
685
657
  createMarqueeHandler(annotation),
686
- createDrawHandler(annotation, interaction, ctx.clock, (flush) => ctx.onSettle(flush))
658
+ createDrawHandler(annotation, interaction)
687
659
  ]) ctx.cleanup(interaction.registerHandler(handler));
688
660
  ctx.listen(interaction.onToolChanged, () => {
661
+ annotation.finishInkDraft();
689
662
  annotation.cancel();
690
663
  annotation.clearGhost();
691
664
  annotation.pruneEngagedSelection();
@@ -1342,6 +1315,7 @@ const FORMATS = /* @__PURE__ */ new Set([
1342
1315
  /** One property's value, by its key (the border's is its three fields). */
1343
1316
  function propertyValue(spec, { data, text, link }) {
1344
1317
  if (spec.key === "link") return link ?? null;
1318
+ if (spec.key === "orientation") return String(data.rotation ?? 0);
1345
1319
  if (spec.key === "borderStyle") return {
1346
1320
  borderStyle: data.borderStyle ?? "solid",
1347
1321
  dashArray: data.dashArray ?? null,
@@ -2009,11 +1983,17 @@ function createView(ctx, records, lending) {
2009
1983
  const painter = lender.painter(shown);
2010
1984
  return painter ? { painter } : {};
2011
1985
  };
2012
- /** A lent record as the model has it, cached per shown row and the confirmed row it was made from. */
1986
+ /** The same accent: none on both sides, or the same color as strong. */
1987
+ const sameAccent = (left, right) => left === right || left !== null && right !== null && left.color === right.color && left.strength === right.strength;
1988
+ /**
1989
+ * A lent record as the model has it, cached per shown row, the confirmed row it was made from
1990
+ * and the accent its lender gives it.
1991
+ */
2013
1992
  const lent = /* @__PURE__ */ new WeakMap();
2014
1993
  const lentRecord = (shown, truth, lender) => {
1994
+ const accent = lender.accentOf?.(shown) ?? null;
2015
1995
  const cached = lent.get(shown);
2016
- if (cached && cached.truth === truth) return cached.record;
1996
+ if (cached && cached.truth === truth && sameAccent(cached.accent, accent)) return cached.record;
2017
1997
  let record;
2018
1998
  if (!truth) record = {
2019
1999
  ...(0, _embedpdf_core_annotation.fromDTO)(shown),
@@ -2045,8 +2025,13 @@ function createView(ctx, records, lending) {
2045
2025
  source: "vector"
2046
2026
  };
2047
2027
  }
2028
+ if (accent) record = {
2029
+ ...record,
2030
+ accent
2031
+ };
2048
2032
  lent.set(shown, {
2049
2033
  truth,
2034
+ accent,
2050
2035
  record
2051
2036
  });
2052
2037
  return record;
@@ -3085,6 +3070,7 @@ const NOTHING_WRITTEN = {
3085
3070
  const noNumbers = () => new _embedpdf_core.PluginError("not-ready", "annotation", "the document holds no object number for a new annotation right now");
3086
3071
  function createStore(ctx, view, records, events, lending, copySources) {
3087
3072
  let followUp = null;
3073
+ let finishWaitingInk = null;
3088
3074
  const builders = /* @__PURE__ */ new Map();
3089
3075
  const model = view.model;
3090
3076
  /** The changes this plugin staged that the engine hasn't answered: their refusals are reported. */
@@ -3158,6 +3144,10 @@ function createStore(ctx, view, records, events, lending, copySources) {
3158
3144
  } catch (error) {
3159
3145
  throw (0, _embedpdf_core.toPluginError)("annotation", error);
3160
3146
  }
3147
+ if (finishWaitingInk && (0, _embedpdf_core_annotation.dropsWaitingInk)(before, result.session, message)) {
3148
+ finishWaitingInk();
3149
+ if (model().draft !== before.draft) return commit(message, options);
3150
+ }
3161
3151
  if (options.adjust) result = options.adjust(result);
3162
3152
  const after = modelAfter(before, result);
3163
3153
  const shown = records.view();
@@ -3349,6 +3339,9 @@ function createStore(ctx, view, records, events, lending, copySources) {
3349
3339
  onUpdate: (next) => {
3350
3340
  followUp = next;
3351
3341
  },
3342
+ onWaitingInk: (finish) => {
3343
+ finishWaitingInk = finish;
3344
+ },
3352
3345
  onEffect: (kind, build) => {
3353
3346
  builders.set(kind, (effect, current, before) => build(effect, current, before));
3354
3347
  },
@@ -3725,11 +3718,13 @@ function createToolRegistry(ctx, settingTools, store, events) {
3725
3718
  /** Put `tool` on the hub, in place of any tool with its id. */
3726
3719
  const registerOnHub = (tool) => {
3727
3720
  const { id, cursor, enables } = tool;
3721
+ const canUse = () => ctx.allows("annotations:create");
3728
3722
  const hubTool = {
3729
3723
  id,
3730
3724
  cursor,
3731
3725
  enables,
3732
- touch: require_contract.toolTouchOf(enables)
3726
+ touch: require_contract.toolTouchOf(enables),
3727
+ canUse
3733
3728
  };
3734
3729
  return ctx.tryGet(_embedpdf_plugin_interaction_contract_host.InteractionToken)?.registerTool(hubTool, { replace: true });
3735
3730
  };
@@ -4181,7 +4176,11 @@ function createGhost(ctx, { store, geometry, tools }, stamps) {
4181
4176
  }
4182
4177
  const placingTool = placing && tools.get(placing.toolId);
4183
4178
  if (placing?.page.objectNumber === pageObjectNumber && placingTool && (0, _embedpdf_core_annotation.isDrag)("box", placing.from, placing.to)) {
4184
- const placement = (0, _embedpdf_core_annotation.gesturePlacement)("box", placing.from, placing.to, placingTool.clickCreate, { pageBox: geometry.pageBoxOf(pageObjectNumber) });
4179
+ const placement = (0, _embedpdf_core_annotation.gesturePlacement)("box", placing.from, placing.to, placingTool.clickCreate, {
4180
+ pageBox: geometry.pageBoxOf(pageObjectNumber),
4181
+ upright: placingTool.upright,
4182
+ displayRotation: placing.displayRotation
4183
+ });
4185
4184
  const annotation = annotationOfTool(model, placingTool);
4186
4185
  const shape = placement && (0, _embedpdf_core_annotation.placedShape)(annotation, placement);
4187
4186
  if (shape) items.push((0, _embedpdf_core_annotation.unmadeItem)(PLACING_ID, annotation, shape, "draft", view));
@@ -4221,12 +4220,13 @@ function createGhost(ctx, { store, geometry, tools }, stamps) {
4221
4220
  api: {
4222
4221
  hoverGhostAt,
4223
4222
  clearGhost,
4224
- previewPlacement: (toolId, page, from, to) => {
4223
+ previewPlacement: (toolId, page, from, to, displayRotation) => {
4225
4224
  ctx.state.update(setPlacing, {
4226
4225
  toolId,
4227
4226
  page,
4228
4227
  from,
4229
- to
4228
+ to,
4229
+ ...displayRotation !== void 0 ? { displayRotation } : {}
4230
4230
  });
4231
4231
  },
4232
4232
  clearPlacementPreview: () => {
@@ -4450,9 +4450,10 @@ function createCrud(ctx, { store, tools, records, lending }, annotations, settle
4450
4450
  //#endregion
4451
4451
  //#region src/write/drafts.ts
4452
4452
  /**
4453
- * Creation drafts: the polygon or polyline a click per point builds (and the
4454
- * buffered ink strokes), its finish and cancel doors, and the captured-draft
4455
- * seam the measurement plugin calibrates from.
4453
+ * Creation drafts: the polygon or polyline a click per point builds, its
4454
+ * finish and cancel doors, and the captured-draft seam the measurement plugin
4455
+ * calibrates from. Also the pen drawing that waits for its next stroke: the
4456
+ * wait, and everything that ends it.
4456
4457
  */
4457
4458
  function createDrafts(ctx, { store, events, tools, afterCreate }, annotations) {
4458
4459
  let anchorCache = null;
@@ -4486,6 +4487,60 @@ function createDrafts(ctx, { store, events, tools, afterCreate }, annotations) {
4486
4487
  const annotation = annotations.get(ref);
4487
4488
  return annotation ? { annotation } : null;
4488
4489
  };
4490
+ /**
4491
+ * The wait that is running: the draft as the pen left it, the tool that
4492
+ * drew it, the tool that was active then, and the wait's cancel. It speaks
4493
+ * for the drawing only while the model's draft is that same object: the
4494
+ * next stroke, and whatever ends the drawing, put another draft there.
4495
+ */
4496
+ let wait = null;
4497
+ /** The ink draft whose pen is up, or null: a stroke still being drawn is not waiting. */
4498
+ const waitingInk = () => {
4499
+ const draft = store.model().draft;
4500
+ return draft?.kind === "create-ink" && !draft.drawing ? draft : null;
4501
+ };
4502
+ /**
4503
+ * Make the drawing that waits an annotation now, as its tool's
4504
+ * `afterCreate` says. With `selects: false` the selection stays as it is:
4505
+ * what ended the wait decides it.
4506
+ */
4507
+ const finishWaitingInk = ({ selects = true } = {}) => {
4508
+ const draft = waitingInk();
4509
+ if (!draft) return;
4510
+ const running = wait?.draft === draft ? wait : null;
4511
+ wait?.cancel();
4512
+ wait = null;
4513
+ const toolId = running ? running.toolId : tools.activeTool()?.id;
4514
+ const { selected } = store.model();
4515
+ const commit = store.commit({ type: "finishInkDraft" }, { adjust: selects ? afterCreate.shape(toolId) : (result) => ({
4516
+ ...result,
4517
+ session: {
4518
+ ...result.session,
4519
+ selected
4520
+ }
4521
+ }) });
4522
+ if (!running || running.activeToolId === tools.activeTool()?.id) afterCreate.done(toolId, commit);
4523
+ };
4524
+ /**
4525
+ * The pen is up and its stroke is held back: wait `delayMs` for the next
4526
+ * stroke of the same drawing. When none comes, the drawing is finished.
4527
+ */
4528
+ const waitForNextStroke = (toolId, delayMs) => {
4529
+ const draft = waitingInk();
4530
+ if (!draft) return;
4531
+ wait?.cancel();
4532
+ const cancel = ctx.clock.after(delayMs, () => {
4533
+ if (store.model().draft === draft) finishWaitingInk();
4534
+ });
4535
+ wait = {
4536
+ draft,
4537
+ toolId,
4538
+ activeToolId: tools.activeTool()?.id,
4539
+ cancel
4540
+ };
4541
+ };
4542
+ store.onWaitingInk(() => finishWaitingInk({ selects: false }));
4543
+ ctx.onSettle(() => finishWaitingInk());
4489
4544
  return {
4490
4545
  draft: {
4491
4546
  get: () => draftAnchorOf(),
@@ -4495,10 +4550,7 @@ function createDrafts(ctx, { store, events, tools, afterCreate }, annotations) {
4495
4550
  }
4496
4551
  },
4497
4552
  api: {
4498
- finishInkDraft: () => {
4499
- const toolId = tools.activeTool()?.id;
4500
- afterCreate.done(toolId, store.commit({ type: "finishInkDraft" }, { adjust: afterCreate.shape(toolId) }));
4501
- },
4553
+ finishInkDraft: () => finishWaitingInk(),
4502
4554
  cancel: () => {
4503
4555
  store.commit({ type: "cancel" });
4504
4556
  },
@@ -4506,8 +4558,13 @@ function createDrafts(ctx, { store, events, tools, afterCreate }, annotations) {
4506
4558
  const draft = store.model().draft;
4507
4559
  return draft?.kind === "create-distance" && draft.step === "offset" ? draft.page : null;
4508
4560
  },
4509
- onDraftCaptured: events.draftCaptured.on
4510
- }
4561
+ onDraftCaptured: events.draftCaptured.on,
4562
+ hasCreationAwaitingClick: () => {
4563
+ const draft = store.model().draft;
4564
+ return draft?.kind === "create-poly" || draft?.kind === "create-callout" || draft?.kind === "create-distance" && draft.step === "offset";
4565
+ }
4566
+ },
4567
+ waitForNextStroke
4511
4568
  };
4512
4569
  }
4513
4570
  //#endregion
@@ -4946,7 +5003,7 @@ function createAnnotationHoverFeed(actions, annotOf) {
4946
5003
  * (edit, marquee, create, hover), each folded into the pure core with this
4947
5004
  * plugin's inputs filled in (page box, chrome geometry, engaged ids, view).
4948
5005
  */
4949
- function createPointer(ctx, { store, geometry, authority, tools, behaviors, afterCreate }, chrome, measurement) {
5006
+ function createPointer(ctx, { store, geometry, authority, tools, behaviors, afterCreate }, chrome, measurement, drafts) {
4950
5007
  const actionsForHover = ctx.tryGet(_embedpdf_plugin_actions_contract.ActionsToken);
4951
5008
  const hoverFeed = actionsForHover ? createAnnotationHoverFeed(actionsForHover, (id) => store.model().byId[id] ?? null) : null;
4952
5009
  return { api: {
@@ -4988,6 +5045,7 @@ function createPointer(ctx, { store, geometry, authority, tools, behaviors, afte
4988
5045
  if (resolvedTool?.capture === true ? !ctx.doc?.security.allows("doc.annotate.modify") : !authority.canCreate()) return;
4989
5046
  const laidOut = geometry.sizeOf(pageObjectNumber) !== null;
4990
5047
  const cache = measurement.viewportsOf(pageObjectNumber);
5048
+ const groupStrokesMs = resolvedTool?.ink?.groupStrokesMs ?? 0;
4991
5049
  const draft = store.model().draft;
4992
5050
  const continuingMeasurement = (draft?.kind === "create-distance" || draft?.kind === "create-poly") && draft.page.objectNumber === pageObjectNumber && draft.preset === (resolvedTool?.preset ?? tool);
4993
5051
  const own = resolvedTool ? (0, _embedpdf_core_annotation.defaultsFor)(store.model(), resolvedTool.preset) : {};
@@ -5053,7 +5111,7 @@ function createPointer(ctx, { store, geometry, authority, tools, behaviors, afte
5053
5111
  intent: intent === "ink-highlight" ? intent : void 0,
5054
5112
  clickCreate: resolvedTool?.clickCreate,
5055
5113
  flags: resolvedTool?.flags,
5056
- deferInkCommit: (resolvedTool?.ink?.groupStrokesMs ?? 0) > 0,
5114
+ deferInkCommit: groupStrokesMs > 0,
5057
5115
  straightenInk: resolvedTool?.ink?.straighten,
5058
5116
  in: {
5059
5117
  page,
@@ -5066,6 +5124,7 @@ function createPointer(ctx, { store, geometry, authority, tools, behaviors, afte
5066
5124
  }
5067
5125
  }, { adjust: afterCreate.shape(resolvedTool?.id) });
5068
5126
  afterCreate.done(resolvedTool?.id, committed);
5127
+ if (phase === "up" && groupStrokesMs > 0) drafts.waitForNextStroke(resolvedTool?.id, groupStrokesMs);
5069
5128
  },
5070
5129
  hoverAt: (at) => {
5071
5130
  const model = store.model();
@@ -5236,6 +5295,15 @@ function createSelectionWrites(ctx, { store, authority, fonts, behaviors, lendin
5236
5295
  y: flips("y", "height") ? -10 : DUPLICATE_OFFSET
5237
5296
  };
5238
5297
  };
5298
+ /** The box of each page the selection is on: where a turn keeps what it turned. */
5299
+ const selectionPageBoxes = () => {
5300
+ const boxes = {};
5301
+ for (const { annotation } of annotations.selectedRecords()) {
5302
+ const box = geometry.pageBoxOf(annotation.page.objectNumber);
5303
+ if (box) boxes[annotation.page.objectNumber] = box;
5304
+ }
5305
+ return boxes;
5306
+ };
5239
5307
  /** Commit a selection message; resolves with its outcome over the refs that were selected. */
5240
5308
  const commitOverSelection = async (commit, signal) => {
5241
5309
  return batchResultOf(selectedRefs(), { failed: (await ctx.cancellable(signal, Promise.all(commit().map((committed) => committed.written)))).flatMap((outcome) => outcome.failed) });
@@ -5348,12 +5416,16 @@ function createSelectionWrites(ctx, { store, authority, fonts, behaviors, lendin
5348
5416
  rotateBy: async (degrees, options = {}) => {
5349
5417
  const commit = store.commit({
5350
5418
  type: "rotateSelection",
5351
- degrees
5419
+ degrees,
5420
+ pageBoxes: selectionPageBoxes()
5352
5421
  });
5353
5422
  throwIfFailed(await ctx.cancellable(options.signal, commit.written));
5354
5423
  },
5355
5424
  resetRotation: async (options = {}) => {
5356
- const commit = store.commit({ type: "resetRotation" });
5425
+ const commit = store.commit({
5426
+ type: "resetRotation",
5427
+ pageBoxes: selectionPageBoxes()
5428
+ });
5357
5429
  throwIfFailed(await ctx.cancellable(options.signal, commit.written));
5358
5430
  },
5359
5431
  getAnchor: () => chrome.selectionAnchor(),
@@ -5896,8 +5968,8 @@ function createAnnotationController(ctx) {
5896
5968
  const icons = createIcons(ctx, services, stamps);
5897
5969
  const selection = createSelectionWrites(ctx, services, annotations, properties, chrome, text, crud);
5898
5970
  const measurement = createMeasurement(ctx, services);
5899
- const pointer = createPointer(ctx, services, chrome, measurement);
5900
5971
  const drafts = createDrafts(ctx, services, annotations);
5972
+ const pointer = createPointer(ctx, services, chrome, measurement, drafts);
5901
5973
  const markup = createMarkupWrites(ctx, services, annotations);
5902
5974
  const scripts = createScriptEffects(ctx, services);
5903
5975
  const comments = createComments(ctx, services, createThreadIndex(ctx, services), crud);
@@ -6030,6 +6102,12 @@ Object.defineProperty(exports, "kindNamed", {
6030
6102
  return _embedpdf_core_annotation.kindNamed;
6031
6103
  }
6032
6104
  });
6105
+ Object.defineProperty(exports, "orientationPatch", {
6106
+ enumerable: true,
6107
+ get: function() {
6108
+ return _embedpdf_core_annotation.orientationPatch;
6109
+ }
6110
+ });
6033
6111
  Object.defineProperty(exports, "propertiesOf", {
6034
6112
  enumerable: true,
6035
6113
  get: function() {