@hussmartinez/pdf-sdk 0.2.0 → 0.3.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.
@@ -1 +1 @@
1
- {"version":3,"file":"processing-worker-DU8GEYFs.js","names":["ProcessingWorker"],"sources":["../../src/adapters/qpdf/processing-worker.ts"],"sourcesContent":["import ProcessingWorker from \"../../workers/processing.worker.ts?worker&inline\";\n\n/** Starts the inlined processing worker, from its own lazy chunk. */\nexport function createInlineWorker(): Worker {\n return new ProcessingWorker({ name: \"hm-pdf-processing\" });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAGA,SAAgB,qBAA6B;CAC3C,OAAO,IAAIA,cAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC3D"}
1
+ {"version":3,"file":"processing-worker-Dy5JQHXJ.js","names":["ProcessingWorker"],"sources":["../../src/adapters/qpdf/processing-worker.ts"],"sourcesContent":["import ProcessingWorker from \"../../workers/processing.worker.ts?worker&inline\";\n\n/** Starts the inlined processing worker, from its own lazy chunk. */\nexport function createInlineWorker(): Worker {\n return new ProcessingWorker({ name: \"hm-pdf-processing\" });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAGA,SAAgB,qBAA6B;CAC3C,OAAO,IAAIA,cAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC3D"}
package/dist/react.d.ts CHANGED
@@ -144,7 +144,7 @@ declare interface PdfCallOptions {
144
144
  }
145
145
 
146
146
  /**
147
- * A colour as `#rrggbb`.
147
+ * A colour as `#rrggbb`, in lowercase.
148
148
  *
149
149
  * @public
150
150
  */
@@ -326,6 +326,9 @@ export declare const pdfEditorMessages: {
326
326
  readonly "errors.TOO_MANY_PAGES": "This document has {actual, number} pages, which is over the {maximum, number}-page limit.";
327
327
  readonly "errors.SESSION_TOO_LARGE": "The open documents would use {actual, fileSize}, which is over the {maximum, fileSize} limit. Close a document, or clear the undo history, and try again.";
328
328
  readonly "errors.IMAGE_TOO_LARGE": "Some images are too large to show. They're kept unchanged in the document.";
329
+ readonly "errors.INVALID_COMMAND": "This edit couldn't be made. Please try again.";
330
+ readonly "errors.INVALID_DOCUMENT": "This project is damaged, or it isn't a PDF project.";
331
+ readonly "errors.UNSUPPORTED_FORMAT": "This project was saved by a newer version, or by another app. Update the app, and try again.";
329
332
  readonly "warnings.maxPages": {
330
333
  readonly $plural: "actual";
331
334
  } & {
@@ -548,13 +551,21 @@ declare class PdfError extends SdkError<PdfErrorCode> {
548
551
  * - `IMAGE_TOO_LARGE`: an image exceeds the `maxImagePixels` limit. It's
549
552
  * reported as a warning, because the image is skipped rather than the
550
553
  * document failing (ADR 0004).
554
+ * - `INVALID_COMMAND`: an edit command doesn't apply to the plan, such as
555
+ * one naming a page the plan doesn't have. `details.reason` says why, as
556
+ * a stable token (`PdfCommandRejection`, sdk-core ADR 0045).
557
+ * - `INVALID_DOCUMENT`: a plan document, such as a saved project, isn't
558
+ * valid in its format. `details.reason` says why (`PdfDocumentRejection`).
559
+ * - `UNSUPPORTED_FORMAT`: a plan document of another format, or of a later
560
+ * version than this release reads, with `details.reason`
561
+ * `unknown-format` or `unknown-format-version`.
551
562
  *
552
563
  * Limit codes carry `details` with the `limit`, its `maximum`, the `actual`
553
564
  * value and the `unit`.
554
565
  *
555
566
  * @public
556
567
  */
557
- declare type PdfErrorCode = SdkCoreErrorCode | "INVALID_PDF" | "PASSWORD_REQUIRED" | "PROCESSING_FAILED" | "TOO_MANY_PAGES" | "SESSION_TOO_LARGE" | "IMAGE_TOO_LARGE";
568
+ declare type PdfErrorCode = SdkCoreErrorCode | "INVALID_PDF" | "PASSWORD_REQUIRED" | "PROCESSING_FAILED" | "TOO_MANY_PAGES" | "SESSION_TOO_LARGE" | "IMAGE_TOO_LARGE" | "INVALID_COMMAND" | "INVALID_DOCUMENT" | "UNSUPPORTED_FORMAT";
558
569
 
559
570
  /**
560
571
  * Options for {@link PdfSession.export}.
@@ -624,6 +635,10 @@ export declare const pdfIcons: Readonly<Record<string, IconComponent>>;
624
635
  /**
625
636
  * A drawn signature, or another image, placed on the page.
626
637
  *
638
+ * The session refuses it for now, with `INVALID_COMMAND` and
639
+ * `unsupported-annotation`: the portable format can't carry its image yet
640
+ * (docs/adr/0003). The editor's signature tool will bring it back.
641
+ *
627
642
  * @public
628
643
  */
629
644
  declare interface PdfImageAnnotation {
@@ -801,6 +816,9 @@ declare const pdfMessages: {
801
816
  readonly "errors.TOO_MANY_PAGES": "This document has {actual, number} pages, which is over the {maximum, number}-page limit.";
802
817
  readonly "errors.SESSION_TOO_LARGE": "The open documents would use {actual, fileSize}, which is over the {maximum, fileSize} limit. Close a document, or clear the undo history, and try again.";
803
818
  readonly "errors.IMAGE_TOO_LARGE": "Some images are too large to show. They're kept unchanged in the document.";
819
+ readonly "errors.INVALID_COMMAND": "This edit couldn't be made. Please try again.";
820
+ readonly "errors.INVALID_DOCUMENT": "This project is damaged, or it isn't a PDF project.";
821
+ readonly "errors.UNSUPPORTED_FORMAT": "This project was saved by a newer version, or by another app. Update the app, and try again.";
804
822
  readonly "warnings.maxPages": {
805
823
  readonly $plural: "actual";
806
824
  } & {
@@ -1074,7 +1092,10 @@ declare interface PdfRotatePagesCommand {
1074
1092
  readonly type: "pdf.rotatePages";
1075
1093
  /** The keys of the pages to rotate. */
1076
1094
  readonly pages: readonly string[];
1077
- /** Degrees, clockwise, in multiples of 90. Negative turns anticlockwise. */
1095
+ /**
1096
+ * Degrees, clockwise, in multiples of 90 from -360 to 360. Negative turns
1097
+ * anticlockwise.
1098
+ */
1078
1099
  readonly angle: number;
1079
1100
  }
1080
1101
 
@@ -1115,9 +1136,11 @@ declare interface PdfSession {
1115
1136
  */
1116
1137
  insert(file: Blob | PdfBytes, options?: PdfInsertOptions): Promise<void>;
1117
1138
  /**
1118
- * Runs one command, or several as one undoable step. Throws `TypeError`
1119
- * or `RangeError` for a command that doesn't fit the plan, and changes
1120
- * nothing then.
1139
+ * Runs one command, or several as one undoable step. Throws `PdfError`
1140
+ * with `INVALID_COMMAND` for a command that doesn't fit the plan, with a
1141
+ * `PdfCommandRejection` as `details.reason`, and changes nothing then.
1142
+ * Positions and sizes are snapped to millionths of the page
1143
+ * (`PDF_POSITION_GRID`).
1121
1144
  */
1122
1145
  execute(commands: PdfCommand | readonly PdfCommand[]): void;
1123
1146
  undo(): void;
package/dist/react.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use client";
2
- import { i as createPdfJsEngine, n as createPdfSession, o as linkDestination, r as createPdfProcessor, s as safeLinkUrl, t as pdfMessages } from "./shared/en-BYB_GHqG.js";
3
- import { createMessages, createShortcutRegistry, createToolRegistry, defineMessages, defineTool, resolveAssetsBase } from "@hussmartinez/sdk-core";
2
+ import { g as createPdfJsEngine, h as createPdfProcessor, n as createPdfSession, t as pdfMessages, v as linkDestination, y as safeLinkUrl } from "./shared/en-CQX26xju.js";
3
+ import { createMessages, createShortcutRegistry, createToolRegistry, defineMessages, defineTool } from "@hussmartinez/sdk-core";
4
4
  import { AnnouncerProvider, IconButton, OpenIcon, RedoIcon, RotateLeftIcon, RotateRightIcon, Toolbar, ToolbarGroup, UiMessagesProvider, UndoIcon, useAnnounce, usePlatform } from "@hussmartinez/ui/react";
5
5
  import { createContext, forwardRef, useCallback, useContext, useEffect, useId, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
6
6
  import { uiMessages } from "@hussmartinez/ui";
@@ -106,46 +106,11 @@ function usePdfDocument() {
106
106
  function usePdfMessages() {
107
107
  return usePdfContext("usePdfMessages").messages;
108
108
  }
109
- // src/react/lazy-ports.ts
110
- function checkAssetsUrl(assetsUrl) {
111
- if (assetsUrl === void 0) return;
112
- if (globalThis.location !== void 0) {
113
- resolveAssetsBase(assetsUrl);
114
- return;
115
- }
116
- const parsed = new URL(String(assetsUrl), "https://server.invalid/");
117
- if (parsed.search || parsed.hash) throw new TypeError("assetsUrl can't have a query or a fragment.");
118
- }
119
- function lazyEngine(create) {
120
- let engine;
121
- return {
122
- open: (source, options) => {
123
- engine ??= create();
124
- return engine.open(source, options);
125
- },
126
- destroy: async () => {
127
- await engine?.destroy();
128
- }
129
- };
130
- }
131
- function lazyProcessor(create) {
132
- let processor;
133
- return {
134
- build: (job, options) => {
135
- processor ??= create();
136
- return processor.build(job, options);
137
- },
138
- destroy: async () => {
139
- await processor?.destroy();
140
- }
141
- };
142
- }
143
109
  // src/react/PdfSessionProvider.tsx
144
110
  function own(props) {
145
111
  const assets = props.assetsUrl === void 0 ? {} : { assetsUrl: props.assetsUrl };
146
- checkAssetsUrl(props.assetsUrl);
147
- const engine = props.engine ?? lazyEngine(() => createPdfJsEngine(assets));
148
- const processor = props.processor ?? lazyProcessor(() => createPdfProcessor(assets));
112
+ const engine = props.engine ?? createPdfJsEngine(assets);
113
+ const processor = props.processor ?? createPdfProcessor(assets);
149
114
  const session = createPdfSession({
150
115
  engine,
151
116
  processor,
@@ -733,10 +698,12 @@ function PdfViewer(props) {
733
698
  const session = usePdfSession();
734
699
  const anchor = useRef(void 0);
735
700
  const own = useRef(void 0);
701
+ const anchoredAt = useRef(void 0);
736
702
  const findPage = useCallback((key) => root.current?.querySelector(`[data-hm-page-key="${CSS.escape(key)}"]`) ?? void 0, []);
737
703
  const scrollOwn = useCallback((element, top) => {
738
704
  element.scrollTop = top;
739
705
  own.current = element.scrollTop;
706
+ anchoredAt.current = element.scrollTop;
740
707
  }, []);
741
708
  useEffect(() => {
742
709
  const element = pages.current;
@@ -746,6 +713,10 @@ function PdfViewer(props) {
746
713
  const viewer = root.current;
747
714
  const held = anchor.current;
748
715
  if (!viewer || !held) return;
716
+ const at = anchoredAt.current;
717
+ const bottom = viewer.scrollHeight - viewer.clientHeight;
718
+ const clamped = at !== void 0 && at > bottom && Math.abs(viewer.scrollTop - bottom) < 1;
719
+ if (at !== void 0 && Math.abs(viewer.scrollTop - at) >= 1 && !clamped) return;
749
720
  const page = findPage(held.key);
750
721
  if (!page) {
751
722
  anchor.current = void 0;
@@ -779,6 +750,7 @@ function PdfViewer(props) {
779
750
  key: top.dataset["hmPageKey"],
780
751
  offset: top.offsetTop - scrollTop
781
752
  };
753
+ anchoredAt.current = scrollTop;
782
754
  }
783
755
  const key = current?.dataset["hmPageKey"];
784
756
  if (key) session.setCurrentPage(key);