collabmd 0.1.45 → 0.1.46
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/README.md +39 -19
- package/bin/collabmd.js +27 -1
- package/dist/client/assets/CodeMirrorEditor-UCMRAMCV-CYM5nxUi.js +1 -0
- package/dist/client/assets/browser-utils--H_DkyEF.js +1 -0
- package/dist/client/assets/chunk-ERSWUTIN-hHdvnhaY.js +41 -0
- package/dist/client/assets/comment-thread-store-CdcwrObc.js +1 -0
- package/dist/client/assets/comment-threads-9J6Em-_y.js +4 -0
- package/dist/client/assets/comment-ui-shared-BC4pt4w8.js +7 -0
- package/dist/client/assets/dist-B5HTH2BO.js +1 -0
- package/dist/client/assets/{dist-DhLvC8DS.js → dist-B5Jf1SLZ.js} +2 -2
- package/dist/client/assets/{dist-CnL7Y2sM.js → dist-BDt3rz7l.js} +1 -1
- package/dist/client/assets/dist-BEwK4csK.js +1 -0
- package/dist/client/assets/dist-BKW4PALF.js +1 -0
- package/dist/client/assets/dist-BWcHHOx-.js +1 -0
- package/dist/client/assets/{dist-CYZmbokb.js → dist-BalScOwZ.js} +9 -9
- package/dist/client/assets/dist-Bf8f4HmM.js +6 -0
- package/dist/client/assets/dist-BjHJDtyU.js +1 -0
- package/dist/client/assets/dist-Bm6pf9Bl.js +1 -0
- package/dist/client/assets/dist-BoVBdSjk.js +1 -0
- package/dist/client/assets/dist-CC5IrQMG.js +1 -0
- package/dist/client/assets/{dist-C11hHqv_.js → dist-CCP_AKPG.js} +12 -12
- package/dist/client/assets/dist-CZgOmxK6.js +1 -0
- package/dist/client/assets/{dist-D2AQ-76g.js → dist-Cqhv5LAk.js} +1 -1
- package/dist/client/assets/{dist-MvcV4190.js → dist-D-Z6FLem.js} +1 -1
- package/dist/client/assets/dist-DLMiIsNC.js +11 -0
- package/dist/client/assets/{dist-3bgCp7iY.js → dist-DZKu9Cs8.js} +1 -1
- package/dist/client/assets/dist-DaYdhcVs.js +1 -0
- package/dist/client/assets/dist-FWJsgGYI.js +1 -0
- package/dist/client/assets/dist-Kh2COra_.js +1 -0
- package/dist/client/assets/{dist-JYEDRrPT.js → dist-PDZ3bSqm.js} +1 -1
- package/dist/client/assets/{dist-Drp8uu5J.js → dist-TjMh8n1u.js} +1 -1
- package/dist/client/assets/{dist-DGb27Dl6.js → dist-dtAdgL39.js} +1 -1
- package/dist/client/assets/dist-jv4WdPnK.js +9 -0
- package/dist/client/assets/{dist-DK5Gs2wa.js → dist-rGBmqxC7.js} +1 -1
- package/dist/client/assets/{dist-DCzuX1AF.js → dist-shrl6K_0.js} +1 -1
- package/dist/client/assets/dist-tOaegDYS2.js +1 -0
- package/dist/client/assets/dist-ule2n6ak.js +1 -0
- package/dist/client/assets/dist-xElaxbia.js +1 -0
- package/dist/client/assets/{drawio-editor-ClE8jpoG.js → drawio-editor-FzY1cVw8.js} +1 -1
- package/dist/client/assets/drawioEditor-BjiKJqlc.js +2 -0
- package/dist/client/assets/editor-session-Cm9ekeNo.js +54 -0
- package/dist/client/assets/embedded-editor-base-CwTkk911.css +1 -0
- package/dist/client/assets/{preview-render-profile-BZzGSg6K.js → es-DXGV3RHd.js} +13 -13
- package/dist/client/assets/excalidraw-editor-B46zsTFI.js +9 -0
- package/dist/client/assets/{excalidraw-editor-CyGwOhLz.css → excalidraw-editor-Bt7gMSoh.css} +1 -1
- package/dist/client/assets/excalidraw-scene-BOben2gr.js +1 -0
- package/dist/client/assets/excalidrawEditor-CT2pHq7e.js +2 -0
- package/dist/client/assets/{export-page-CUfws2T-.js → export-page-A7NHawJY.js} +3 -3
- package/dist/client/assets/exportDocument-B2L8fe34.css +1 -0
- package/dist/client/assets/exportDocument-BaqcOAAW.js +2 -0
- package/dist/client/assets/file-icon-svg-CQAjlWjK.js +1 -0
- package/dist/client/assets/file-kind-BbqgGzpc.js +1 -0
- package/dist/client/assets/image-QXRYHUNP-SWEV8H0J.js +1 -0
- package/dist/client/assets/index-ClbjMBVt.js +2 -0
- package/dist/client/assets/index-Pum8ET-5.css +1 -0
- package/dist/client/assets/main-B3uPyqic.js +893 -0
- package/dist/client/assets/pdf-BL9Pr2D5.js +54 -0
- package/dist/client/assets/pdf.worker-CLesOks4.mjs +64857 -0
- package/dist/client/assets/pdf_viewer-FVkkThnz.js +551 -0
- package/dist/client/assets/{preview-render-compiler-BSaQO_p3.js → preview-render-compiler-BD-XhkS2.js} +2 -2
- package/dist/client/assets/preview-render-compiler-BKedaMxx.js +1 -0
- package/dist/client/assets/preview-render-profile-CE-5rVTC.js +1 -0
- package/dist/client/assets/{preview-render-worker-B42LWvrD.js → preview-render-worker-CiPqc9n6.js} +20 -20
- package/dist/client/assets/prod-Dy20X7eT.js +1 -0
- package/dist/client/assets/prod-hnwh7wRM.js +177 -0
- package/dist/client/assets/quick-switcher-controller-fqOYR7qR.js +12 -0
- package/dist/client/assets/runtime-config-BLrEKVO1.js +1 -0
- package/dist/client/assets/vault-api-client-_LpV1aIn.js +6 -0
- package/dist/client/drawio-editor.html +1 -1
- package/dist/client/excalidraw-editor.html +1 -1
- package/dist/client/export-document.html +2 -2
- package/dist/client/index.html +15 -3
- package/docker-compose.yml +42 -0
- package/package.json +17 -4
- package/scripts/local-structurizr-compose.mjs +101 -0
- package/src/client/app/index.html +13 -1
- package/src/client/application/app-shell/comments-feature.js +19 -10
- package/src/client/application/app-shell/git-feature.js +12 -0
- package/src/client/application/app-shell/presence-feature.js +2 -1
- package/src/client/application/app-shell/ui-feature-shell.js +45 -2
- package/src/client/application/app-shell/ui-feature-toolbar.js +2 -52
- package/src/client/application/app-shell/workspace-feature.js +5 -1
- package/src/client/application/app-shell-elements.js +2 -0
- package/src/client/application/pdf-preview-controller.js +992 -0
- package/src/client/application/structurizr-preview-controller.js +395 -0
- package/src/client/application/workspace-coordinator.js +25 -3
- package/src/client/application/workspace-preview-controller.js +91 -2
- package/src/client/bootstrap/collabmd-app-shell.js +31 -5
- package/src/client/browser-utils.js +44 -0
- package/src/client/domain/runtime-paths.js +26 -5
- package/src/client/domain/structurizr-language.js +99 -0
- package/src/client/domain/vault-paths.js +48 -0
- package/src/client/excalidraw-editor.js +833 -51
- package/src/client/infrastructure/browser-preferences-port.js +10 -0
- package/src/client/infrastructure/comment-thread-store.js +34 -13
- package/src/client/infrastructure/editor-session.js +10 -0
- package/src/client/infrastructure/editor-view-adapter.js +46 -2
- package/src/client/infrastructure/excalidraw-room-client.js +103 -14
- package/src/client/infrastructure/git-api-client.js +5 -0
- package/src/client/infrastructure/vault-api-client.js +26 -0
- package/src/client/presentation/comment-ui/comment-ui-shared.js +4 -0
- package/src/client/presentation/drawio-embed-controller.js +9 -102
- package/src/client/presentation/drawio-viewer.js +118 -0
- package/src/client/presentation/excalidraw-embed-controller.js +74 -8
- package/src/client/presentation/file-action-controller.js +102 -0
- package/src/client/presentation/file-explorer-view.js +10 -29
- package/src/client/presentation/file-icon-svg.js +40 -0
- package/src/client/presentation/file-tree-state.js +2 -0
- package/src/client/presentation/git-diff-view-controller.js +731 -3
- package/src/client/presentation/outline-controller.js +86 -0
- package/src/client/presentation/quick-switcher-controller.js +25 -13
- package/src/client/presentation/quick-switcher-text-search.js +1 -1
- package/src/client/styles/components/navigation.css +11 -1
- package/src/client/styles/features/diagram-preview.css +76 -0
- package/src/client/styles/features/diff-special.css +309 -0
- package/src/client/styles/foundation/themes.css +6 -0
- package/src/client/styles/layout/shell.css +358 -1
- package/src/client/styles/layout/view-modes.css +1 -0
- package/src/client/styles/style.css +1 -0
- package/src/client/styles/surfaces/excalidraw-editor.css +294 -0
- package/src/domain/comment-threads.js +63 -5
- package/src/domain/excalidraw-room-codec.js +6 -4
- package/src/domain/file-kind.js +35 -3
- package/src/server/config/env.js +79 -0
- package/src/server/create-app-server.js +12 -0
- package/src/server/domain/bases/base-definition.js +1 -1
- package/src/server/domain/bases/base-expression-runtime.js +1 -4
- package/src/server/domain/bases/base-index-snapshot-store.js +1 -4
- package/src/server/domain/bases/base-transform.js +1 -4
- package/src/server/domain/collaboration/collaboration-room.js +55 -21
- package/src/server/domain/comment-overview.js +15 -5
- package/src/server/domain/ripgrep-search-service.js +4 -0
- package/src/server/infrastructure/git/diff-service.js +5 -0
- package/src/server/infrastructure/git/git-service.js +21 -0
- package/src/server/infrastructure/git/history-service.js +54 -0
- package/src/server/infrastructure/git/image-mime.js +14 -0
- package/src/server/infrastructure/git/untracked-files.js +28 -1
- package/src/server/infrastructure/http/create-git-api-query-handler.js +31 -1
- package/src/server/infrastructure/http/create-request-handler.js +16 -23
- package/src/server/infrastructure/http/create-static-handler.js +5 -2
- package/src/server/infrastructure/http/create-structurizr-api-handler.js +145 -0
- package/src/server/infrastructure/http/create-vault-api-command-handler.js +66 -4
- package/src/server/infrastructure/http/create-vault-api-query-handler.js +51 -3
- package/src/server/infrastructure/http/http-request-helpers.js +22 -0
- package/src/server/infrastructure/persistence/path-utils.js +1 -1
- package/src/server/infrastructure/persistence/vault-file-store.js +3 -0
- package/src/server/infrastructure/structurizr/structurizr-embed.css +43 -0
- package/src/server/infrastructure/structurizr/structurizr-workspace-service.js +374 -0
- package/src/server/infrastructure/websocket/attach-collaboration-gateway.js +4 -22
- package/dist/client/assets/CodeMirrorEditor-UCMRAMCV-CbSISC1w.js +0 -1
- package/dist/client/assets/browser-utils-CKtI_rZl.js +0 -1
- package/dist/client/assets/chunk-ERSWUTIN-ME9RVNel.js +0 -41
- package/dist/client/assets/comment-threads-DDegmaFO.js +0 -4
- package/dist/client/assets/dist-BH87JoHx.js +0 -11
- package/dist/client/assets/dist-BIkPRZO7.js +0 -6
- package/dist/client/assets/dist-BqCsFpx4.js +0 -1
- package/dist/client/assets/dist-C2JHCt8U.js +0 -1
- package/dist/client/assets/dist-CZjTtmTu.js +0 -1
- package/dist/client/assets/dist-Cig4Ov_X.js +0 -1
- package/dist/client/assets/dist-CqQEC8gR.js +0 -1
- package/dist/client/assets/dist-D4Rzqod1.js +0 -1
- package/dist/client/assets/dist-D5sn-mwk.js +0 -9
- package/dist/client/assets/dist-D7G-n87v.js +0 -1
- package/dist/client/assets/dist-DRqOrNgN.js +0 -1
- package/dist/client/assets/dist-DetkMS_4.js +0 -1
- package/dist/client/assets/dist-Dp6FVkQ1.js +0 -1
- package/dist/client/assets/dist-DtbjNB28.js +0 -1
- package/dist/client/assets/dist-DyrYcvpo2.js +0 -1
- package/dist/client/assets/dist-GrygD2zE.js +0 -1
- package/dist/client/assets/dist-HmEpWUow.js +0 -1
- package/dist/client/assets/dist-RTa81Km7.js +0 -1
- package/dist/client/assets/drawioEditor-CXaTM9Ds.js +0 -2
- package/dist/client/assets/editor-session-GTwZS6Df.js +0 -16
- package/dist/client/assets/embedded-editor-base-CiNEc8vy.css +0 -1
- package/dist/client/assets/excalidraw-editor-BZVM81BO.js +0 -9
- package/dist/client/assets/excalidraw-scene-C8n9CHsK.js +0 -176
- package/dist/client/assets/excalidrawEditor-DxJanCze.js +0 -2
- package/dist/client/assets/exportDocument-BCuxqXDY.js +0 -2
- package/dist/client/assets/exportDocument-DV-OX74E.css +0 -1
- package/dist/client/assets/file-kind-BKPOIRPE.js +0 -1
- package/dist/client/assets/image-QXRYHUNP-lfTHE3bb.js +0 -1
- package/dist/client/assets/index-C3YnUgMD.css +0 -1
- package/dist/client/assets/index-D2wXraJ1.js +0 -2
- package/dist/client/assets/main-DtiGz7uI.js +0 -795
- package/dist/client/assets/preview-render-compiler-csGM5ZFz.js +0 -1
- package/dist/client/assets/quick-switcher-controller-shW_6vPY.js +0 -12
- package/dist/client/assets/room-CQLoqtMo.js +0 -1
- package/dist/client/assets/runtime-config-DSGmGE9V.js +0 -1
- package/dist/client/assets/vault-api-client-C6bcGwy-.js +0 -5
- package/src/server/domain/collaboration/room-persistence-controller.js +0 -80
|
@@ -0,0 +1,992 @@
|
|
|
1
|
+
import { clamp } from '../domain/vault-utils.js';
|
|
2
|
+
import { resolveApiUrl } from '../domain/runtime-paths.js';
|
|
3
|
+
|
|
4
|
+
const DEFAULT_PAGE_WIDTH = 612;
|
|
5
|
+
const DEFAULT_PAGE_HEIGHT = 792;
|
|
6
|
+
const MAX_PAGE_WIDTH = 960;
|
|
7
|
+
const PAGE_RENDER_ROOT_MARGIN = '720px 0px';
|
|
8
|
+
const MAX_DEVICE_PIXEL_RATIO = 2;
|
|
9
|
+
const PDF_ZOOM = Object.freeze({
|
|
10
|
+
default: 1,
|
|
11
|
+
max: 2,
|
|
12
|
+
min: 0.5,
|
|
13
|
+
renderDebounceMs: 100,
|
|
14
|
+
step: 0.25,
|
|
15
|
+
wheelSensitivity: 0.01,
|
|
16
|
+
});
|
|
17
|
+
function isRenderingCancellation(error) {
|
|
18
|
+
return error?.name === 'RenderingCancelledException' || error?.name === 'AbortException';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class PdfPreviewController {
|
|
22
|
+
constructor({ previewContainer, outlineController = null }) {
|
|
23
|
+
this.previewContainer = previewContainer;
|
|
24
|
+
this.outlineController = outlineController;
|
|
25
|
+
this.pdfJsPromise = null;
|
|
26
|
+
this.pdfJsApi = null;
|
|
27
|
+
this.pdfViewerApi = null;
|
|
28
|
+
this.loadingTask = null;
|
|
29
|
+
this.pdfDocument = null;
|
|
30
|
+
this.findEventBus = null;
|
|
31
|
+
this.findController = null;
|
|
32
|
+
this.pdfLinkService = null;
|
|
33
|
+
this.intersectionObserver = null;
|
|
34
|
+
this.resizeObserver = null;
|
|
35
|
+
this.pageStates = new Map();
|
|
36
|
+
this.visiblePages = new Set();
|
|
37
|
+
this.renderToken = 0;
|
|
38
|
+
this.shell = null;
|
|
39
|
+
this.zoom = PDF_ZOOM.default;
|
|
40
|
+
this.zoomControls = null;
|
|
41
|
+
this.navigationControls = null;
|
|
42
|
+
this.searchControls = null;
|
|
43
|
+
this.pdfNavigationStack = [];
|
|
44
|
+
this.findState = null;
|
|
45
|
+
this.currentPageNumber = 1;
|
|
46
|
+
this.zoomRenderTimerId = 0;
|
|
47
|
+
this.pageFitWidth = DEFAULT_PAGE_WIDTH;
|
|
48
|
+
this.pageLayoutZoom = PDF_ZOOM.default;
|
|
49
|
+
this.pinchStartDistance = 0;
|
|
50
|
+
this.pinchStartZoom = PDF_ZOOM.default;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async loadPdfJs() {
|
|
54
|
+
if (!this.pdfJsPromise) {
|
|
55
|
+
this.pdfJsPromise = import('pdfjs-dist').then(async (pdfjs) => {
|
|
56
|
+
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
|
|
57
|
+
'pdfjs-dist/build/pdf.worker.mjs',
|
|
58
|
+
import.meta.url,
|
|
59
|
+
).toString();
|
|
60
|
+
globalThis.pdfjsLib = pdfjs;
|
|
61
|
+
const pdfViewer = await import('pdfjs-dist/web/pdf_viewer.mjs');
|
|
62
|
+
return { pdfjs, pdfViewer };
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return this.pdfJsPromise;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async render({ filePath, renderHost, displayName }) {
|
|
70
|
+
const token = this.renderToken;
|
|
71
|
+
this.zoom = PDF_ZOOM.default;
|
|
72
|
+
this.pageLayoutZoom = PDF_ZOOM.default;
|
|
73
|
+
this.pinchStartDistance = 0;
|
|
74
|
+
this.pinchStartZoom = PDF_ZOOM.default;
|
|
75
|
+
this.currentPageNumber = 1;
|
|
76
|
+
this.pdfNavigationStack = [];
|
|
77
|
+
this.outlineController?.clearPdfOutline?.();
|
|
78
|
+
const url = resolveApiUrl(`/download/file?path=${encodeURIComponent(filePath)}`);
|
|
79
|
+
const shell = this.createShell({ displayName, filePath, url });
|
|
80
|
+
renderHost.replaceChildren(shell);
|
|
81
|
+
this.shell = shell;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const { pdfjs, pdfViewer } = await this.loadPdfJs();
|
|
85
|
+
this.pdfJsApi = pdfjs;
|
|
86
|
+
this.pdfViewerApi = pdfViewer;
|
|
87
|
+
if (token !== this.renderToken) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this.loadingTask = pdfjs.getDocument({ url });
|
|
92
|
+
this.loadingTask.onPassword = (updatePassword, reason) => {
|
|
93
|
+
if (token !== this.renderToken || this.shell !== shell) {
|
|
94
|
+
updatePassword(new Error('PDF preview cancelled'));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this.showPasswordPrompt(shell, updatePassword, reason);
|
|
99
|
+
};
|
|
100
|
+
const pdfDocument = await this.loadingTask.promise;
|
|
101
|
+
if (token !== this.renderToken) {
|
|
102
|
+
await pdfDocument.destroy();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.pdfDocument = pdfDocument;
|
|
107
|
+
this.initializeFindController(pdfDocument, pdfViewer);
|
|
108
|
+
void this.loadPdfOutline(pdfDocument, token);
|
|
109
|
+
this.buildPageStates(pdfDocument.numPages, shell.querySelector('.pdf-file-preview-pages'));
|
|
110
|
+
this.setStatus(`${pdfDocument.numPages} ${pdfDocument.numPages === 1 ? 'page' : 'pages'}`);
|
|
111
|
+
this.observePages(token);
|
|
112
|
+
if (this.searchControls?.input.value.trim()) {
|
|
113
|
+
this.dispatchFind();
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (token === this.renderToken && !isRenderingCancellation(error)) {
|
|
117
|
+
this.showError(shell);
|
|
118
|
+
}
|
|
119
|
+
} finally {
|
|
120
|
+
if (token === this.renderToken) {
|
|
121
|
+
this.loadingTask = null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
showPasswordPrompt(shell, updatePassword, reason) {
|
|
127
|
+
const pages = shell.querySelector('.pdf-file-preview-pages');
|
|
128
|
+
if (!pages) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const isIncorrectPassword = reason === this.pdfJsApi?.PasswordResponses?.INCORRECT_PASSWORD;
|
|
133
|
+
this.setStatus(isIncorrectPassword ? 'Incorrect password' : 'Password required');
|
|
134
|
+
|
|
135
|
+
const prompt = document.createElement('form');
|
|
136
|
+
prompt.className = 'pdf-file-preview-password-prompt';
|
|
137
|
+
|
|
138
|
+
const heading = document.createElement('h2');
|
|
139
|
+
heading.className = 'pdf-file-preview-password-title';
|
|
140
|
+
heading.textContent = isIncorrectPassword ? 'Incorrect PDF password' : 'Password required';
|
|
141
|
+
|
|
142
|
+
const copy = document.createElement('p');
|
|
143
|
+
copy.className = 'pdf-file-preview-password-copy';
|
|
144
|
+
copy.textContent = isIncorrectPassword
|
|
145
|
+
? 'The password was incorrect. Try again.'
|
|
146
|
+
: 'Enter the password to preview this PDF.';
|
|
147
|
+
|
|
148
|
+
const label = document.createElement('label');
|
|
149
|
+
label.className = 'pdf-file-preview-password-field';
|
|
150
|
+
label.textContent = 'PDF password';
|
|
151
|
+
|
|
152
|
+
const input = document.createElement('input');
|
|
153
|
+
input.type = 'password';
|
|
154
|
+
input.className = 'ui-input';
|
|
155
|
+
input.autocomplete = 'off';
|
|
156
|
+
input.spellcheck = false;
|
|
157
|
+
input.setAttribute('aria-label', 'PDF password');
|
|
158
|
+
label.appendChild(input);
|
|
159
|
+
|
|
160
|
+
const submit = document.createElement('button');
|
|
161
|
+
submit.type = 'submit';
|
|
162
|
+
submit.className = 'ui-button ui-button--primary';
|
|
163
|
+
submit.textContent = 'Unlock PDF';
|
|
164
|
+
prompt.addEventListener('submit', (event) => {
|
|
165
|
+
event.preventDefault();
|
|
166
|
+
submit.disabled = true;
|
|
167
|
+
input.disabled = true;
|
|
168
|
+
updatePassword(input.value);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
prompt.append(heading, copy, label, submit);
|
|
172
|
+
pages.replaceChildren(prompt);
|
|
173
|
+
input.focus();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async loadPdfOutline(pdfDocument, token) {
|
|
177
|
+
try {
|
|
178
|
+
const outline = typeof pdfDocument.getOutline === 'function'
|
|
179
|
+
? await pdfDocument.getOutline()
|
|
180
|
+
: null;
|
|
181
|
+
if (token !== this.renderToken || this.pdfDocument !== pdfDocument) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (!Array.isArray(outline) || outline.length === 0) {
|
|
186
|
+
this.outlineController?.clearPdfOutline?.();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
this.outlineController?.setPdfOutline?.(
|
|
191
|
+
outline,
|
|
192
|
+
(item) => this.navigateToPdfOutline(item, pdfDocument, token),
|
|
193
|
+
);
|
|
194
|
+
} catch {
|
|
195
|
+
if (token === this.renderToken && this.pdfDocument === pdfDocument) {
|
|
196
|
+
this.outlineController?.clearPdfOutline?.();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async navigateToPdfOutline(item, pdfDocument, token) {
|
|
202
|
+
if (token !== this.renderToken || this.pdfDocument !== pdfDocument) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const url = typeof item?.url === 'string' ? item.url.trim() : '';
|
|
207
|
+
if (url) {
|
|
208
|
+
try {
|
|
209
|
+
const parsedUrl = new URL(url, window.location.href);
|
|
210
|
+
if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') {
|
|
211
|
+
window.open(parsedUrl.href, '_blank', 'noopener,noreferrer');
|
|
212
|
+
}
|
|
213
|
+
} catch {
|
|
214
|
+
// Ignore malformed PDF outline URLs.
|
|
215
|
+
}
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const linkService = this.pdfLinkService;
|
|
220
|
+
if (!linkService || linkService.pdfDocument !== pdfDocument || item?.dest == null) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
await linkService.goToDestination(item.dest);
|
|
226
|
+
} catch {
|
|
227
|
+
// Ignore outline entries with invalid destinations.
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
initializeFindController(pdfDocument, pdfViewer) {
|
|
232
|
+
const previewController = this;
|
|
233
|
+
const eventBus = new pdfViewer.EventBus();
|
|
234
|
+
const setPage = (value) => {
|
|
235
|
+
if (previewController.pdfDocument !== pdfDocument) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
previewController.currentPageNumber = clamp(
|
|
240
|
+
Number(value) || 1,
|
|
241
|
+
1,
|
|
242
|
+
pdfDocument.numPages,
|
|
243
|
+
);
|
|
244
|
+
previewController.scrollToPage(previewController.currentPageNumber);
|
|
245
|
+
};
|
|
246
|
+
const pageViewer = {
|
|
247
|
+
get currentPageNumber() {
|
|
248
|
+
return previewController.currentPageNumber;
|
|
249
|
+
},
|
|
250
|
+
set currentPageNumber(value) {
|
|
251
|
+
setPage(value);
|
|
252
|
+
},
|
|
253
|
+
scrollPageIntoView({ pageNumber, destArray }) {
|
|
254
|
+
if (destArray) {
|
|
255
|
+
previewController.recordPdfNavigation(previewController.currentPageNumber, pageNumber);
|
|
256
|
+
}
|
|
257
|
+
setPage(pageNumber);
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
const linkService = new pdfViewer.PDFLinkService({ eventBus });
|
|
261
|
+
linkService.setDocument(pdfDocument);
|
|
262
|
+
linkService.setViewer(pageViewer);
|
|
263
|
+
const findController = new pdfViewer.PDFFindController({
|
|
264
|
+
eventBus,
|
|
265
|
+
linkService,
|
|
266
|
+
delay: 0,
|
|
267
|
+
updateMatchesCountOnProgress: true,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
this.findState = pdfViewer.FindState.FOUND;
|
|
271
|
+
findController.onIsPageVisible = (pageNumber) => this.visiblePages.has(pageNumber);
|
|
272
|
+
eventBus.on('updatefindmatchescount', ({ matchesCount }) => (
|
|
273
|
+
this.updateFindStatus(matchesCount, true)
|
|
274
|
+
));
|
|
275
|
+
eventBus.on('updatefindcontrolstate', ({ state, matchesCount }) => {
|
|
276
|
+
this.findState = state;
|
|
277
|
+
this.updateFindStatus(matchesCount);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
this.findEventBus = eventBus;
|
|
281
|
+
this.findController = findController;
|
|
282
|
+
this.pdfLinkService = linkService;
|
|
283
|
+
findController.setDocument(pdfDocument);
|
|
284
|
+
this.updateFindControls();
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
scrollToPage(pageNumber) {
|
|
288
|
+
const pageShell = this.pageStates.get(pageNumber)?.pageShell;
|
|
289
|
+
pageShell?.scrollIntoView?.({
|
|
290
|
+
behavior: 'auto',
|
|
291
|
+
block: 'center',
|
|
292
|
+
inline: 'nearest',
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
recordPdfNavigation(fromPageNumber, toPageNumber) {
|
|
297
|
+
if (!Number.isInteger(fromPageNumber)
|
|
298
|
+
|| !Number.isInteger(toPageNumber)
|
|
299
|
+
|| fromPageNumber === toPageNumber) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
this.pdfNavigationStack.push(fromPageNumber);
|
|
304
|
+
this.updateNavigationControls();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
goBack() {
|
|
308
|
+
const pageNumber = this.pdfNavigationStack.pop();
|
|
309
|
+
if (!Number.isInteger(pageNumber)) {
|
|
310
|
+
this.updateNavigationControls();
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
this.currentPageNumber = pageNumber;
|
|
315
|
+
this.scrollToPage(pageNumber);
|
|
316
|
+
this.updateNavigationControls();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
updateNavigationControls() {
|
|
320
|
+
if (this.navigationControls?.backButton) {
|
|
321
|
+
this.navigationControls.backButton.disabled = this.pdfNavigationStack.length === 0;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
setFindStatus(text) {
|
|
326
|
+
if (this.searchControls?.status) {
|
|
327
|
+
this.searchControls.status.textContent = text;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
updateFindStatus(matchesCount, showProgress = false) {
|
|
332
|
+
if (!this.searchControls?.input.value.trim()) {
|
|
333
|
+
this.setFindStatus('');
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const current = Number(matchesCount?.current) || 0;
|
|
338
|
+
const total = Number(matchesCount?.total) || 0;
|
|
339
|
+
const pending = this.findState === this.pdfViewerApi?.FindState.PENDING;
|
|
340
|
+
const status = pending && (!showProgress || total === 0)
|
|
341
|
+
? 'Searching…'
|
|
342
|
+
: total > 0
|
|
343
|
+
? `${current} of ${total}`
|
|
344
|
+
: 'No matches';
|
|
345
|
+
this.setFindStatus(status);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
updateFindControls() {
|
|
349
|
+
const controls = this.searchControls;
|
|
350
|
+
if (!controls) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const disabled = !controls.input.value.trim() || !this.findController;
|
|
355
|
+
controls.previousButton.disabled = disabled;
|
|
356
|
+
controls.nextButton.disabled = disabled;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
dispatchFind() {
|
|
360
|
+
const query = this.searchControls?.input.value.trim() || '';
|
|
361
|
+
if (!query) {
|
|
362
|
+
this.findState = this.pdfViewerApi?.FindState.FOUND ?? null;
|
|
363
|
+
this.findEventBus?.dispatch('findbarclose', { source: this });
|
|
364
|
+
this.setFindStatus('');
|
|
365
|
+
this.updateFindControls();
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (!this.findEventBus || !this.findController) {
|
|
370
|
+
this.setFindStatus('Loading…');
|
|
371
|
+
this.updateFindControls();
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
this.findState = this.pdfViewerApi?.FindState.PENDING ?? null;
|
|
376
|
+
this.setFindStatus('Searching…');
|
|
377
|
+
this.updateFindControls();
|
|
378
|
+
this.findEventBus.dispatch('find', {
|
|
379
|
+
source: this,
|
|
380
|
+
type: '',
|
|
381
|
+
query,
|
|
382
|
+
phraseSearch: true,
|
|
383
|
+
caseSensitive: false,
|
|
384
|
+
entireWord: false,
|
|
385
|
+
highlightAll: true,
|
|
386
|
+
findPrevious: false,
|
|
387
|
+
matchDiacritics: false,
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
dispatchFindAgain(findPrevious = false) {
|
|
392
|
+
const query = this.searchControls?.input.value.trim() || '';
|
|
393
|
+
if (!query || !this.findEventBus || !this.findController) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
this.findEventBus.dispatch('find', {
|
|
398
|
+
source: this,
|
|
399
|
+
type: 'again',
|
|
400
|
+
query,
|
|
401
|
+
phraseSearch: true,
|
|
402
|
+
caseSensitive: false,
|
|
403
|
+
entireWord: false,
|
|
404
|
+
highlightAll: true,
|
|
405
|
+
findPrevious,
|
|
406
|
+
matchDiacritics: false,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
createShell({ displayName, filePath, url }) {
|
|
411
|
+
const shell = document.createElement('section');
|
|
412
|
+
shell.className = 'pdf-file-preview-shell';
|
|
413
|
+
shell.setAttribute('aria-label', `${displayName} PDF preview`);
|
|
414
|
+
|
|
415
|
+
const toolbar = document.createElement('div');
|
|
416
|
+
toolbar.className = 'pdf-file-preview-toolbar';
|
|
417
|
+
|
|
418
|
+
const status = document.createElement('span');
|
|
419
|
+
status.className = 'pdf-file-preview-status';
|
|
420
|
+
status.setAttribute('role', 'status');
|
|
421
|
+
status.setAttribute('aria-live', 'polite');
|
|
422
|
+
status.textContent = 'Loading PDF preview…';
|
|
423
|
+
|
|
424
|
+
const zoomControls = document.createElement('div');
|
|
425
|
+
zoomControls.className = 'pdf-file-preview-zoom-controls';
|
|
426
|
+
|
|
427
|
+
const zoomOutButton = document.createElement('button');
|
|
428
|
+
zoomOutButton.type = 'button';
|
|
429
|
+
zoomOutButton.className = 'pdf-file-preview-zoom-button ui-preview-action';
|
|
430
|
+
zoomOutButton.setAttribute('aria-label', 'Zoom out');
|
|
431
|
+
zoomOutButton.title = 'Zoom out';
|
|
432
|
+
zoomOutButton.textContent = '−';
|
|
433
|
+
zoomOutButton.addEventListener('click', () => this.setZoom(this.zoom - PDF_ZOOM.step));
|
|
434
|
+
|
|
435
|
+
const zoomLabel = document.createElement('span');
|
|
436
|
+
zoomLabel.className = 'pdf-file-preview-zoom-label';
|
|
437
|
+
zoomLabel.setAttribute('aria-live', 'polite');
|
|
438
|
+
|
|
439
|
+
const zoomInButton = document.createElement('button');
|
|
440
|
+
zoomInButton.type = 'button';
|
|
441
|
+
zoomInButton.className = 'pdf-file-preview-zoom-button ui-preview-action';
|
|
442
|
+
zoomInButton.setAttribute('aria-label', 'Zoom in');
|
|
443
|
+
zoomInButton.title = 'Zoom in';
|
|
444
|
+
zoomInButton.textContent = '+';
|
|
445
|
+
zoomInButton.addEventListener('click', () => this.setZoom(this.zoom + PDF_ZOOM.step));
|
|
446
|
+
|
|
447
|
+
zoomControls.append(zoomOutButton, zoomLabel, zoomInButton);
|
|
448
|
+
this.zoomControls = { zoomInButton, zoomLabel, zoomOutButton };
|
|
449
|
+
this.updateZoomControls();
|
|
450
|
+
|
|
451
|
+
const backButton = document.createElement('button');
|
|
452
|
+
backButton.type = 'button';
|
|
453
|
+
backButton.className = 'pdf-file-preview-back-button ui-preview-action';
|
|
454
|
+
backButton.setAttribute('aria-label', 'Back to previous page');
|
|
455
|
+
backButton.title = 'Back to previous page';
|
|
456
|
+
backButton.textContent = '←';
|
|
457
|
+
backButton.addEventListener('click', () => this.goBack());
|
|
458
|
+
this.navigationControls = { backButton };
|
|
459
|
+
this.updateNavigationControls();
|
|
460
|
+
|
|
461
|
+
const download = document.createElement('a');
|
|
462
|
+
download.className = 'pdf-file-preview-download';
|
|
463
|
+
download.href = url;
|
|
464
|
+
download.download = String(filePath).split('/').pop() || displayName;
|
|
465
|
+
download.textContent = 'Download';
|
|
466
|
+
download.setAttribute('aria-label', `Download ${displayName}`);
|
|
467
|
+
|
|
468
|
+
const findControls = document.createElement('div');
|
|
469
|
+
findControls.className = 'pdf-file-preview-find-controls';
|
|
470
|
+
|
|
471
|
+
const findInput = document.createElement('input');
|
|
472
|
+
findInput.type = 'search';
|
|
473
|
+
findInput.className = 'pdf-file-preview-find-input';
|
|
474
|
+
findInput.placeholder = 'Find in PDF';
|
|
475
|
+
findInput.setAttribute('aria-label', 'Find in PDF');
|
|
476
|
+
findInput.addEventListener('input', () => this.dispatchFind());
|
|
477
|
+
findInput.addEventListener('keydown', (event) => {
|
|
478
|
+
if (event.key === 'Escape') {
|
|
479
|
+
event.preventDefault();
|
|
480
|
+
findInput.value = '';
|
|
481
|
+
this.dispatchFind();
|
|
482
|
+
findInput.blur();
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (event.key === 'Enter') {
|
|
487
|
+
event.preventDefault();
|
|
488
|
+
this.dispatchFindAgain(event.shiftKey);
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
const findStatus = document.createElement('span');
|
|
493
|
+
findStatus.className = 'pdf-file-preview-find-status';
|
|
494
|
+
findStatus.setAttribute('role', 'status');
|
|
495
|
+
findStatus.setAttribute('aria-live', 'polite');
|
|
496
|
+
|
|
497
|
+
const previousButton = document.createElement('button');
|
|
498
|
+
previousButton.type = 'button';
|
|
499
|
+
previousButton.className = 'pdf-file-preview-find-button ui-preview-action';
|
|
500
|
+
previousButton.setAttribute('aria-label', 'Previous match');
|
|
501
|
+
previousButton.title = 'Previous match';
|
|
502
|
+
previousButton.textContent = '‹';
|
|
503
|
+
previousButton.addEventListener('click', () => this.dispatchFindAgain(true));
|
|
504
|
+
|
|
505
|
+
const nextButton = document.createElement('button');
|
|
506
|
+
nextButton.type = 'button';
|
|
507
|
+
nextButton.className = 'pdf-file-preview-find-button ui-preview-action';
|
|
508
|
+
nextButton.setAttribute('aria-label', 'Next match');
|
|
509
|
+
nextButton.title = 'Next match';
|
|
510
|
+
nextButton.textContent = '›';
|
|
511
|
+
nextButton.addEventListener('click', () => this.dispatchFindAgain());
|
|
512
|
+
|
|
513
|
+
findControls.append(findInput, findStatus, previousButton, nextButton);
|
|
514
|
+
this.searchControls = {
|
|
515
|
+
input: findInput,
|
|
516
|
+
nextButton,
|
|
517
|
+
previousButton,
|
|
518
|
+
status: findStatus,
|
|
519
|
+
};
|
|
520
|
+
this.updateFindControls();
|
|
521
|
+
|
|
522
|
+
const toolbarStart = document.createElement('div');
|
|
523
|
+
toolbarStart.className = 'pdf-file-preview-toolbar-start';
|
|
524
|
+
toolbarStart.append(backButton, status, zoomControls, findControls);
|
|
525
|
+
toolbar.append(toolbarStart, download);
|
|
526
|
+
|
|
527
|
+
const pages = document.createElement('div');
|
|
528
|
+
pages.className = 'pdf-file-preview-pages';
|
|
529
|
+
pages.setAttribute('aria-label', 'PDF pages');
|
|
530
|
+
pages.addEventListener('wheel', (event) => this.handleWheelZoom(event), { passive: false });
|
|
531
|
+
pages.addEventListener('touchstart', (event) => this.handleTouchStart(event), { passive: true });
|
|
532
|
+
pages.addEventListener('touchmove', (event) => this.handleTouchMove(event), { passive: false });
|
|
533
|
+
const finishPinch = (event) => this.handleTouchEnd(event);
|
|
534
|
+
pages.addEventListener('touchend', finishPinch, { passive: true });
|
|
535
|
+
pages.addEventListener('touchcancel', finishPinch, { passive: true });
|
|
536
|
+
shell.append(toolbar, pages);
|
|
537
|
+
return shell;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
buildPageStates(pageCount, pagesElement) {
|
|
541
|
+
this.pageStates.clear();
|
|
542
|
+
this.visiblePages.clear();
|
|
543
|
+
pagesElement.replaceChildren();
|
|
544
|
+
|
|
545
|
+
for (let pageNumber = 1; pageNumber <= pageCount; pageNumber += 1) {
|
|
546
|
+
const pageShell = document.createElement('article');
|
|
547
|
+
pageShell.className = 'pdf-page';
|
|
548
|
+
pageShell.dataset.pageNumber = String(pageNumber);
|
|
549
|
+
pageShell.setAttribute('aria-label', `Page ${pageNumber} of ${pageCount}`);
|
|
550
|
+
pageShell.style.aspectRatio = `${DEFAULT_PAGE_WIDTH} / ${DEFAULT_PAGE_HEIGHT}`;
|
|
551
|
+
pagesElement.appendChild(pageShell);
|
|
552
|
+
this.pageStates.set(pageNumber, {
|
|
553
|
+
canvas: null,
|
|
554
|
+
needsRender: false,
|
|
555
|
+
page: null,
|
|
556
|
+
pageShell,
|
|
557
|
+
renderPromise: null,
|
|
558
|
+
renderTask: null,
|
|
559
|
+
renderedWidth: 0,
|
|
560
|
+
pageView: null,
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
this.commitPageZoom();
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
observePages(token) {
|
|
568
|
+
const pagesElement = this.shell?.querySelector('.pdf-file-preview-pages');
|
|
569
|
+
if (!pagesElement) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (typeof IntersectionObserver === 'function') {
|
|
574
|
+
this.intersectionObserver = new IntersectionObserver(
|
|
575
|
+
(entries) => this.handlePageIntersections(entries, token),
|
|
576
|
+
{
|
|
577
|
+
root: this.previewContainer,
|
|
578
|
+
rootMargin: PAGE_RENDER_ROOT_MARGIN,
|
|
579
|
+
},
|
|
580
|
+
);
|
|
581
|
+
this.pageStates.forEach(({ pageShell }) => this.intersectionObserver.observe(pageShell));
|
|
582
|
+
} else {
|
|
583
|
+
this.visiblePages.add(1);
|
|
584
|
+
void this.renderPage(1, token);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
if (typeof ResizeObserver === 'function') {
|
|
588
|
+
this.resizeObserver = new ResizeObserver(() => {
|
|
589
|
+
this.commitPageZoom({ preserveLiveZoom: Boolean(this.zoomRenderTimerId) });
|
|
590
|
+
this.scheduleVisiblePageRender();
|
|
591
|
+
});
|
|
592
|
+
this.resizeObserver.observe(pagesElement);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
handlePageIntersections(entries, token) {
|
|
597
|
+
if (token !== this.renderToken) {
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
entries.forEach((entry) => {
|
|
602
|
+
const pageNumber = Number(entry.target.dataset.pageNumber);
|
|
603
|
+
if (entry.isIntersecting) {
|
|
604
|
+
this.visiblePages.add(pageNumber);
|
|
605
|
+
this.currentPageNumber = Math.min(...this.visiblePages);
|
|
606
|
+
void this.renderPage(pageNumber, token);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
this.visiblePages.delete(pageNumber);
|
|
611
|
+
this.releasePage(pageNumber);
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
disposePageView(state, pageView = state.pageView) {
|
|
616
|
+
if (!pageView) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
pageView.reset?.();
|
|
621
|
+
pageView.div?.remove();
|
|
622
|
+
if (state.pageView === pageView) {
|
|
623
|
+
state.pageView = null;
|
|
624
|
+
state.canvas = null;
|
|
625
|
+
state.renderTask = null;
|
|
626
|
+
state.renderedWidth = 0;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
async renderPage(pageNumber, token) {
|
|
631
|
+
const state = this.pageStates.get(pageNumber);
|
|
632
|
+
if (!state || !this.pdfDocument || token !== this.renderToken) {
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const width = this.getPageWidth(state.pageShell);
|
|
637
|
+
if (state.canvas && Math.abs(state.renderedWidth - width) < 1) {
|
|
638
|
+
state.needsRender = false;
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
if (state.renderPromise) {
|
|
642
|
+
return state.renderPromise;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
state.needsRender = false;
|
|
646
|
+
state.renderPromise = (async () => {
|
|
647
|
+
let pageView = null;
|
|
648
|
+
let committed = false;
|
|
649
|
+
try {
|
|
650
|
+
const page = state.page ?? await this.pdfDocument.getPage(pageNumber);
|
|
651
|
+
if (token !== this.renderToken) {
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
state.page = page;
|
|
656
|
+
const baseViewport = page.getViewport({ scale: 1 });
|
|
657
|
+
state.pageShell.style.aspectRatio = `${baseViewport.width} / ${baseViewport.height}`;
|
|
658
|
+
const scale = width / baseViewport.width;
|
|
659
|
+
const viewport = page.getViewport({ scale });
|
|
660
|
+
const userUnit = viewport.userUnit || 1;
|
|
661
|
+
state.pageShell.style.setProperty('--scale-factor', String(viewport.scale));
|
|
662
|
+
state.pageShell.style.setProperty('--user-unit', String(userUnit));
|
|
663
|
+
state.pageShell.style.setProperty('--total-scale-factor', String(viewport.scale * userUnit));
|
|
664
|
+
state.pageShell.style.setProperty('--scale-round-x', '1px');
|
|
665
|
+
state.pageShell.style.setProperty('--scale-round-y', '1px');
|
|
666
|
+
|
|
667
|
+
this.disposePageView(state);
|
|
668
|
+
pageView = new this.pdfViewerApi.PDFPageView({
|
|
669
|
+
annotationMode: this.pdfJsApi.AnnotationMode.ENABLE,
|
|
670
|
+
container: state.pageShell,
|
|
671
|
+
defaultViewport: viewport,
|
|
672
|
+
enableAutoLinking: false,
|
|
673
|
+
eventBus: this.findEventBus,
|
|
674
|
+
id: pageNumber,
|
|
675
|
+
layerProperties: {
|
|
676
|
+
findController: this.findController,
|
|
677
|
+
linkService: this.pdfLinkService,
|
|
678
|
+
},
|
|
679
|
+
maxCanvasPixels: Math.ceil(
|
|
680
|
+
viewport.width * viewport.height * MAX_DEVICE_PIXEL_RATIO ** 2,
|
|
681
|
+
),
|
|
682
|
+
scale: viewport.scale / this.pdfJsApi.PixelsPerInch.PDF_TO_CSS_UNITS,
|
|
683
|
+
textLayerMode: 1,
|
|
684
|
+
});
|
|
685
|
+
pageView.setPdfPage(page);
|
|
686
|
+
state.pageView = pageView;
|
|
687
|
+
const drawPromise = pageView.draw();
|
|
688
|
+
state.renderTask = pageView.renderTask;
|
|
689
|
+
await drawPromise;
|
|
690
|
+
|
|
691
|
+
if (pageView.canvas) {
|
|
692
|
+
pageView.canvas.className = 'pdf-page-canvas';
|
|
693
|
+
pageView.canvas.setAttribute('aria-label', `Rendered page ${pageNumber}`);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const isCurrentRender = token === this.renderToken
|
|
697
|
+
&& this.visiblePages.has(pageNumber)
|
|
698
|
+
&& state.pageView === pageView
|
|
699
|
+
&& pageView.canvas
|
|
700
|
+
&& Math.abs(this.getPageWidth(state.pageShell) - width) < 1;
|
|
701
|
+
if (!isCurrentRender) {
|
|
702
|
+
this.disposePageView(state, pageView);
|
|
703
|
+
if (token === this.renderToken && this.visiblePages.has(pageNumber)) {
|
|
704
|
+
this.scheduleVisiblePageRender();
|
|
705
|
+
}
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
state.canvas = pageView.canvas;
|
|
710
|
+
state.renderedWidth = width;
|
|
711
|
+
committed = true;
|
|
712
|
+
} catch (error) {
|
|
713
|
+
if (pageView && !committed) {
|
|
714
|
+
this.disposePageView(state, pageView);
|
|
715
|
+
}
|
|
716
|
+
if (!isRenderingCancellation(error) && token === this.renderToken) {
|
|
717
|
+
state.pageShell.replaceChildren();
|
|
718
|
+
this.setStatus(`Unable to render page ${pageNumber}`);
|
|
719
|
+
}
|
|
720
|
+
} finally {
|
|
721
|
+
if (state.pageView === pageView) {
|
|
722
|
+
state.renderTask = null;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
})().finally(() => {
|
|
726
|
+
state.renderPromise = null;
|
|
727
|
+
const shouldRetry = state.needsRender;
|
|
728
|
+
state.needsRender = false;
|
|
729
|
+
if (shouldRetry && this.visiblePages.has(pageNumber) && token === this.renderToken) {
|
|
730
|
+
void this.renderPage(pageNumber, token);
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
return state.renderPromise;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
handleWheelZoom(event) {
|
|
738
|
+
if (!event.ctrlKey) {
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
event.preventDefault();
|
|
743
|
+
const deltaY = Number.isFinite(event.deltaY) ? event.deltaY : 0;
|
|
744
|
+
const wheelDelta = clamp(
|
|
745
|
+
-deltaY * PDF_ZOOM.wheelSensitivity,
|
|
746
|
+
-PDF_ZOOM.step / 2,
|
|
747
|
+
PDF_ZOOM.step / 2,
|
|
748
|
+
);
|
|
749
|
+
if (wheelDelta !== 0) {
|
|
750
|
+
this.setZoom(this.zoom + wheelDelta);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
getPinchDistance(touches) {
|
|
755
|
+
if (!touches || touches.length !== 2) {
|
|
756
|
+
return 0;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const [first, second] = touches;
|
|
760
|
+
const distance = Math.hypot(
|
|
761
|
+
second.clientX - first.clientX,
|
|
762
|
+
second.clientY - first.clientY,
|
|
763
|
+
);
|
|
764
|
+
return Number.isFinite(distance) && distance > 0 ? distance : 0;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
handleTouchStart(event) {
|
|
768
|
+
if (event.touches?.length !== 2) {
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
const distance = this.getPinchDistance(event.touches);
|
|
773
|
+
if (distance > 0) {
|
|
774
|
+
this.pinchStartDistance = distance;
|
|
775
|
+
this.pinchStartZoom = this.zoom;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
handleTouchMove(event) {
|
|
780
|
+
if (event.touches?.length !== 2) {
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
if (!this.pinchStartDistance) {
|
|
785
|
+
this.handleTouchStart(event);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
const distance = this.getPinchDistance(event.touches);
|
|
789
|
+
if (!distance || !this.pinchStartDistance) {
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
this.setZoom(this.pinchStartZoom * (distance / this.pinchStartDistance));
|
|
794
|
+
event.preventDefault();
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
handleTouchEnd(event) {
|
|
798
|
+
if (event.touches?.length === 2) {
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
this.pinchStartDistance = 0;
|
|
803
|
+
this.pinchStartZoom = this.zoom;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
cancelVisiblePageRenders() {
|
|
807
|
+
this.visiblePages.forEach((pageNumber) => {
|
|
808
|
+
const state = this.pageStates.get(pageNumber);
|
|
809
|
+
if (!state) {
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
state.needsRender = false;
|
|
814
|
+
this.disposePageView(state);
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
scheduleVisiblePageRender() {
|
|
819
|
+
if (this.zoomRenderTimerId) {
|
|
820
|
+
window.clearTimeout(this.zoomRenderTimerId);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
this.zoomRenderTimerId = window.setTimeout(() => {
|
|
824
|
+
this.zoomRenderTimerId = 0;
|
|
825
|
+
this.commitPageZoom({ measure: false });
|
|
826
|
+
this.visiblePages.forEach((pageNumber) => {
|
|
827
|
+
const state = this.pageStates.get(pageNumber);
|
|
828
|
+
if (state) {
|
|
829
|
+
state.needsRender = true;
|
|
830
|
+
}
|
|
831
|
+
void this.renderPage(pageNumber, this.renderToken);
|
|
832
|
+
});
|
|
833
|
+
}, PDF_ZOOM.renderDebounceMs);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
setZoom(zoom) {
|
|
837
|
+
const nextZoom = clamp(zoom, PDF_ZOOM.min, PDF_ZOOM.max);
|
|
838
|
+
if (nextZoom === this.zoom) {
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
this.cancelVisiblePageRenders();
|
|
843
|
+
this.zoom = nextZoom;
|
|
844
|
+
this.updateZoomControls();
|
|
845
|
+
this.applyLivePageZoom();
|
|
846
|
+
this.scheduleVisiblePageRender();
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
updateZoomControls() {
|
|
850
|
+
if (!this.zoomControls) {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
const percentage = `${Math.round(this.zoom * 100)}%`;
|
|
855
|
+
this.zoomControls.zoomLabel.textContent = percentage;
|
|
856
|
+
this.zoomControls.zoomLabel.setAttribute('aria-label', `Zoom ${percentage}`);
|
|
857
|
+
this.zoomControls.zoomOutButton.disabled = this.zoom <= PDF_ZOOM.min;
|
|
858
|
+
this.zoomControls.zoomInButton.disabled = this.zoom >= PDF_ZOOM.max;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
applyLivePageZoom() {
|
|
862
|
+
const pagesElement = this.shell?.querySelector('.pdf-file-preview-pages');
|
|
863
|
+
if (!pagesElement) {
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
const scale = this.pageLayoutZoom > 0 ? this.zoom / this.pageLayoutZoom : 1;
|
|
868
|
+
if (Math.abs(scale - 1) < 0.001) {
|
|
869
|
+
pagesElement.style.removeProperty('transform');
|
|
870
|
+
pagesElement.style.removeProperty('transform-origin');
|
|
871
|
+
pagesElement.style.removeProperty('will-change');
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
pagesElement.style.transformOrigin = 'top center';
|
|
876
|
+
pagesElement.style.transform = `scale(${scale})`;
|
|
877
|
+
pagesElement.style.willChange = 'transform';
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
commitPageZoom({ measure = true, preserveLiveZoom = false } = {}) {
|
|
881
|
+
const pagesElement = this.shell?.querySelector('.pdf-file-preview-pages');
|
|
882
|
+
if (!pagesElement) {
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
if (measure) {
|
|
887
|
+
const styles = window.getComputedStyle(pagesElement);
|
|
888
|
+
const horizontalPadding = Number.parseFloat(styles.paddingLeft || '0')
|
|
889
|
+
+ Number.parseFloat(styles.paddingRight || '0');
|
|
890
|
+
const availableWidth = pagesElement.clientWidth - horizontalPadding;
|
|
891
|
+
this.pageFitWidth = Math.min(
|
|
892
|
+
MAX_PAGE_WIDTH,
|
|
893
|
+
availableWidth > 0 ? availableWidth : (this.previewContainer?.clientWidth || DEFAULT_PAGE_WIDTH),
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
const layoutZoom = preserveLiveZoom ? this.pageLayoutZoom : this.zoom;
|
|
898
|
+
pagesElement.style.setProperty(
|
|
899
|
+
'--pdf-page-width',
|
|
900
|
+
`${Math.max(1, this.pageFitWidth * layoutZoom)}px`,
|
|
901
|
+
);
|
|
902
|
+
if (preserveLiveZoom) {
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
this.pageLayoutZoom = this.zoom;
|
|
907
|
+
pagesElement.style.removeProperty('transform');
|
|
908
|
+
pagesElement.style.removeProperty('transform-origin');
|
|
909
|
+
pagesElement.style.removeProperty('will-change');
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
getPageWidth(pageShell) {
|
|
913
|
+
return pageShell.offsetWidth
|
|
914
|
+
|| pageShell.getBoundingClientRect().width
|
|
915
|
+
|| this.previewContainer?.clientWidth
|
|
916
|
+
|| DEFAULT_PAGE_WIDTH;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
releasePage(pageNumber) {
|
|
920
|
+
const state = this.pageStates.get(pageNumber);
|
|
921
|
+
if (!state) {
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
state.needsRender = true;
|
|
926
|
+
this.disposePageView(state);
|
|
927
|
+
state.pageShell.replaceChildren();
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
setStatus(text) {
|
|
931
|
+
const status = this.shell?.querySelector('.pdf-file-preview-status');
|
|
932
|
+
if (status) {
|
|
933
|
+
status.textContent = text;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
showError(shell) {
|
|
938
|
+
this.setStatus('Unable to render PDF');
|
|
939
|
+
const pages = shell.querySelector('.pdf-file-preview-pages');
|
|
940
|
+
if (!pages) {
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
const error = document.createElement('div');
|
|
945
|
+
error.className = 'pdf-file-preview-error';
|
|
946
|
+
error.textContent = 'This PDF could not be rendered in the browser.';
|
|
947
|
+
|
|
948
|
+
pages.replaceChildren(error);
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
cancel() {
|
|
952
|
+
this.renderToken += 1;
|
|
953
|
+
if (this.zoomRenderTimerId) {
|
|
954
|
+
window.clearTimeout(this.zoomRenderTimerId);
|
|
955
|
+
this.zoomRenderTimerId = 0;
|
|
956
|
+
}
|
|
957
|
+
this.intersectionObserver?.disconnect();
|
|
958
|
+
this.resizeObserver?.disconnect();
|
|
959
|
+
this.intersectionObserver = null;
|
|
960
|
+
this.resizeObserver = null;
|
|
961
|
+
this.pageStates.forEach((state) => this.disposePageView(state));
|
|
962
|
+
this.findEventBus?.dispatch('findbarclose', { source: this });
|
|
963
|
+
this.findController?.setDocument(null);
|
|
964
|
+
this.outlineController?.clearPdfOutline?.();
|
|
965
|
+
const loadingTaskDestroy = this.loadingTask?.destroy?.();
|
|
966
|
+
loadingTaskDestroy?.catch?.(() => {});
|
|
967
|
+
const pdfDocumentDestroy = this.pdfDocument?.destroy?.();
|
|
968
|
+
pdfDocumentDestroy?.catch?.(() => {});
|
|
969
|
+
this.loadingTask = null;
|
|
970
|
+
this.pdfDocument = null;
|
|
971
|
+
this.findEventBus = null;
|
|
972
|
+
this.findController = null;
|
|
973
|
+
this.pdfLinkService = null;
|
|
974
|
+
this.pdfJsApi = null;
|
|
975
|
+
this.pdfViewerApi = null;
|
|
976
|
+
this.pageStates.clear();
|
|
977
|
+
this.visiblePages.clear();
|
|
978
|
+
this.zoomControls = null;
|
|
979
|
+
this.navigationControls = null;
|
|
980
|
+
this.searchControls = null;
|
|
981
|
+
this.pdfNavigationStack = [];
|
|
982
|
+
this.findState = null;
|
|
983
|
+
this.currentPageNumber = 1;
|
|
984
|
+
this.zoom = PDF_ZOOM.default;
|
|
985
|
+
this.pageFitWidth = DEFAULT_PAGE_WIDTH;
|
|
986
|
+
this.pageLayoutZoom = PDF_ZOOM.default;
|
|
987
|
+
this.pinchStartDistance = 0;
|
|
988
|
+
this.pinchStartZoom = PDF_ZOOM.default;
|
|
989
|
+
this.shell = null;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
}
|