@antcde/component-library 0.1.25-rc.5 → 0.1.25-rc.7
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 +9281 -9187
- package/dist/types/src/components/FilePreviewContent/_PreviewDetails.vue.d.ts +18 -0
- package/dist/types/src/components/FilePreviewContent/_PreviewMeta.vue.d.ts +13 -0
- package/dist/types/src/components/FilePreviewContent/_PreviewUsages.vue.d.ts +13 -0
- package/dist/types/src/components/FilePreviewContent/fileFormat.d.ts +7 -0
- package/dist/types/src/components/FilePreviewContent/index.vue.d.ts +20 -14
- package/dist/types/src/components/FilePreviewContent/types.d.ts +27 -0
- package/dist/types/src/components/FilePreviewContent/usePreviewDetails.d.ts +49 -0
- package/dist/types/src/components/UnifiedWindow/_WindowPresetMenu.vue.d.ts +11 -0
- package/dist/types/src/components/UnifiedWindow/_WindowTabs.vue.d.ts +16 -0
- package/dist/types/src/components/UnifiedWindow/index.vue.d.ts +3 -9
- package/dist/types/src/components/UnifiedWindow/types.d.ts +11 -0
- package/dist/types/src/components/UnifiedWindow/useNativeFullscreen.d.ts +18 -0
- package/dist/types/src/components/UnifiedWindow/useWindowDrag.d.ts +8 -0
- package/dist/types/src/index.d.ts +1 -1
- package/lib/PdfViewer/PdfViewer.vue +245 -0
- package/lib/PdfViewer/_PdfToolbar.vue +196 -0
- package/lib/PdfViewer/canvasBudget.ts +57 -0
- package/lib/PdfViewer/index.ts +5 -0
- package/lib/PdfViewer/labels.ts +52 -0
- package/lib/PdfViewer/usePdfDocument.ts +195 -0
- package/lib/PdfViewer/usePdfKeyboard.ts +38 -0
- package/lib/PdfViewer/usePdfRaster.ts +85 -0
- package/lib/PdfViewer/usePdfTransform.ts +238 -0
- package/package.json +19 -12
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
<script lang="ts" setup>
|
|
2
|
+
import type { PdfViewerLabels } from './labels'
|
|
3
|
+
|
|
4
|
+
defineProps<{
|
|
5
|
+
labels: PdfViewerLabels
|
|
6
|
+
/** Floats the controls over the drawing instead of giving them their own row. */
|
|
7
|
+
compact: boolean
|
|
8
|
+
zoomPercent: string
|
|
9
|
+
canZoomIn: boolean
|
|
10
|
+
canZoomOut: boolean
|
|
11
|
+
hasPages: boolean
|
|
12
|
+
pageNumber: number
|
|
13
|
+
pageCount: number
|
|
14
|
+
}>()
|
|
15
|
+
|
|
16
|
+
const emit = defineEmits<{
|
|
17
|
+
zoom: [factor: number]
|
|
18
|
+
fitPage: []
|
|
19
|
+
fitWidth: []
|
|
20
|
+
rotate: []
|
|
21
|
+
page: [delta: number]
|
|
22
|
+
}>()
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<template>
|
|
26
|
+
<!-- In a short pane the controls float over the drawing instead of taking a
|
|
27
|
+
row of their own: a bench pane is a couple of hundred pixels tall, and a
|
|
28
|
+
second band of chrome costs more than the zoom buttons are worth. -->
|
|
29
|
+
<div class="pdfv__toolbar" :class="{ 'pdfv__toolbar--floating': compact }">
|
|
30
|
+
<button
|
|
31
|
+
type="button"
|
|
32
|
+
class="pdfv__btn"
|
|
33
|
+
:disabled="!canZoomOut"
|
|
34
|
+
:aria-label="labels.zoomOut"
|
|
35
|
+
:title="labels.zoomOut"
|
|
36
|
+
@click="emit('zoom', 0.8)"
|
|
37
|
+
>
|
|
38
|
+
<v-icon icon="mdi-minus" :size="compact ? 16 : 18" />
|
|
39
|
+
</button>
|
|
40
|
+
|
|
41
|
+
<span class="pdfv__zoom" aria-live="polite">{{ zoomPercent }}</span>
|
|
42
|
+
|
|
43
|
+
<button
|
|
44
|
+
type="button"
|
|
45
|
+
class="pdfv__btn"
|
|
46
|
+
:disabled="!canZoomIn"
|
|
47
|
+
:aria-label="labels.zoomIn"
|
|
48
|
+
:title="labels.zoomIn"
|
|
49
|
+
@click="emit('zoom', 1.25)"
|
|
50
|
+
>
|
|
51
|
+
<v-icon icon="mdi-plus" :size="compact ? 16 : 18" />
|
|
52
|
+
</button>
|
|
53
|
+
|
|
54
|
+
<span v-if="!compact" class="pdfv__sep" />
|
|
55
|
+
|
|
56
|
+
<button
|
|
57
|
+
type="button"
|
|
58
|
+
class="pdfv__btn"
|
|
59
|
+
:aria-label="labels.fitPage"
|
|
60
|
+
:title="labels.fitPage"
|
|
61
|
+
@click="emit('fitPage')"
|
|
62
|
+
>
|
|
63
|
+
<v-icon icon="mdi-fit-to-page-outline" :size="compact ? 16 : 18" />
|
|
64
|
+
</button>
|
|
65
|
+
|
|
66
|
+
<button
|
|
67
|
+
v-if="!compact"
|
|
68
|
+
type="button"
|
|
69
|
+
class="pdfv__btn"
|
|
70
|
+
:aria-label="labels.fitWidth"
|
|
71
|
+
:title="labels.fitWidth"
|
|
72
|
+
@click="emit('fitWidth')"
|
|
73
|
+
>
|
|
74
|
+
<v-icon icon="mdi-arrow-expand-horizontal" :size="18" />
|
|
75
|
+
</button>
|
|
76
|
+
|
|
77
|
+
<button
|
|
78
|
+
v-if="!compact"
|
|
79
|
+
type="button"
|
|
80
|
+
class="pdfv__btn"
|
|
81
|
+
:aria-label="labels.rotate"
|
|
82
|
+
:title="labels.rotate"
|
|
83
|
+
@click="emit('rotate')"
|
|
84
|
+
>
|
|
85
|
+
<v-icon icon="mdi-rotate-right" :size="18" />
|
|
86
|
+
</button>
|
|
87
|
+
|
|
88
|
+
<template v-if="hasPages">
|
|
89
|
+
<span v-if="!compact" class="pdfv__sep" />
|
|
90
|
+
<button
|
|
91
|
+
type="button"
|
|
92
|
+
class="pdfv__btn"
|
|
93
|
+
:disabled="pageNumber <= 1"
|
|
94
|
+
:aria-label="labels.previousPage"
|
|
95
|
+
:title="labels.previousPage"
|
|
96
|
+
@click="emit('page', -1)"
|
|
97
|
+
>
|
|
98
|
+
<v-icon icon="mdi-chevron-left" :size="compact ? 16 : 18" />
|
|
99
|
+
</button>
|
|
100
|
+
<span class="pdfv__pages">{{ pageNumber }} / {{ pageCount }}</span>
|
|
101
|
+
<button
|
|
102
|
+
type="button"
|
|
103
|
+
class="pdfv__btn"
|
|
104
|
+
:disabled="pageNumber >= pageCount"
|
|
105
|
+
:aria-label="labels.nextPage"
|
|
106
|
+
:title="labels.nextPage"
|
|
107
|
+
@click="emit('page', 1)"
|
|
108
|
+
>
|
|
109
|
+
<v-icon icon="mdi-chevron-right" :size="compact ? 16 : 18" />
|
|
110
|
+
</button>
|
|
111
|
+
</template>
|
|
112
|
+
|
|
113
|
+
<span v-if="!compact" class="pdfv__spacer" />
|
|
114
|
+
<slot name="actions" />
|
|
115
|
+
</div>
|
|
116
|
+
</template>
|
|
117
|
+
|
|
118
|
+
<style scoped>
|
|
119
|
+
.pdfv__toolbar {
|
|
120
|
+
display: flex;
|
|
121
|
+
align-items: center;
|
|
122
|
+
gap: 2px;
|
|
123
|
+
flex-shrink: 0;
|
|
124
|
+
padding: 4px 8px;
|
|
125
|
+
background: #2b2e30;
|
|
126
|
+
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
|
|
127
|
+
color: rgba(255, 255, 255, 0.92);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.pdfv__toolbar--floating {
|
|
131
|
+
position: absolute;
|
|
132
|
+
right: 10px;
|
|
133
|
+
bottom: 10px;
|
|
134
|
+
z-index: 2;
|
|
135
|
+
gap: 0;
|
|
136
|
+
padding: 3px;
|
|
137
|
+
border: 1px solid rgba(255, 255, 255, 0.14);
|
|
138
|
+
border-radius: 12px;
|
|
139
|
+
background: rgba(30, 33, 35, 0.9);
|
|
140
|
+
backdrop-filter: blur(6px);
|
|
141
|
+
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.pdfv__btn {
|
|
145
|
+
display: inline-flex;
|
|
146
|
+
align-items: center;
|
|
147
|
+
justify-content: center;
|
|
148
|
+
width: 40px;
|
|
149
|
+
height: 40px;
|
|
150
|
+
flex-shrink: 0;
|
|
151
|
+
border: none;
|
|
152
|
+
border-radius: 8px;
|
|
153
|
+
background: transparent;
|
|
154
|
+
color: inherit;
|
|
155
|
+
cursor: pointer;
|
|
156
|
+
transition: background 0.12s ease, opacity 0.12s ease;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/* Deliberately still a 36px target in the floating pill: the operator is often
|
|
160
|
+
wearing gloves, and the 24px icon buttons this would otherwise shrink to are
|
|
161
|
+
unusable through one. */
|
|
162
|
+
.pdfv__toolbar--floating .pdfv__btn {
|
|
163
|
+
width: 36px;
|
|
164
|
+
height: 36px;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
.pdfv__btn:hover:not(:disabled) { background: rgba(255, 255, 255, 0.12); }
|
|
168
|
+
.pdfv__btn:focus-visible { outline: 2px solid rgba(255, 255, 255, 0.7); outline-offset: -2px; }
|
|
169
|
+
.pdfv__btn:disabled { opacity: 0.3; cursor: default; }
|
|
170
|
+
|
|
171
|
+
.pdfv__zoom {
|
|
172
|
+
min-width: 52px;
|
|
173
|
+
text-align: center;
|
|
174
|
+
font-size: 12px;
|
|
175
|
+
font-variant-numeric: tabular-nums;
|
|
176
|
+
user-select: none;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.pdfv__toolbar--floating .pdfv__zoom { min-width: 44px; }
|
|
180
|
+
|
|
181
|
+
.pdfv__pages {
|
|
182
|
+
padding: 0 6px;
|
|
183
|
+
font-size: 12px;
|
|
184
|
+
font-variant-numeric: tabular-nums;
|
|
185
|
+
user-select: none;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
.pdfv__sep {
|
|
189
|
+
width: 1px;
|
|
190
|
+
height: 20px;
|
|
191
|
+
margin: 0 6px;
|
|
192
|
+
background: rgba(255, 255, 255, 0.15);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
.pdfv__spacer { flex: 1 1 auto; }
|
|
196
|
+
</style>
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canvas size limits, per rendering engine.
|
|
3
|
+
*
|
|
4
|
+
* An over-budget canvas does not throw — WebKit silently hands back a blank
|
|
5
|
+
* backing store. A drawing that vanishes the moment the operator zooms in is a
|
|
6
|
+
* far worse failure than one that stays slightly soft, so the budget is
|
|
7
|
+
* enforced before the render rather than discovered after it.
|
|
8
|
+
*
|
|
9
|
+
* WebKit's ceiling is the binding one: iOS/iPadOS cap a canvas at roughly
|
|
10
|
+
* 4096x4096 device pixels regardless of how much memory the device has, and
|
|
11
|
+
* every browser on iOS is WebKit underneath whatever brand is on the icon.
|
|
12
|
+
*/
|
|
13
|
+
const WEBKIT_MAX_SIDE = 4096
|
|
14
|
+
const WEBKIT_MAX_AREA = 4096 * 4096
|
|
15
|
+
const DEFAULT_MAX_SIDE = 8192
|
|
16
|
+
const DEFAULT_MAX_AREA = 8192 * 8192
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Chromium and Gecko both stamp their own token into the UA string. Anything
|
|
20
|
+
* left that still claims AppleWebKit is a genuine WebKit engine — desktop
|
|
21
|
+
* Safari, or any iOS browser, since they are all WebKit-backed.
|
|
22
|
+
*/
|
|
23
|
+
export function isWebKit(): boolean {
|
|
24
|
+
if (typeof navigator === 'undefined')
|
|
25
|
+
return false
|
|
26
|
+
const ua = navigator.userAgent
|
|
27
|
+
return /AppleWebKit/.test(ua) && !/Chrome|Chromium|Edg\/|OPR\/|SamsungBrowser/.test(ua)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The largest scale factor a page of this intrinsic size can be rasterised at
|
|
32
|
+
* without exceeding the engine's canvas budget. Beyond it the viewer keeps
|
|
33
|
+
* zooming, but by stretching the last sharp raster rather than re-rendering.
|
|
34
|
+
*/
|
|
35
|
+
export function maxRenderScale(pageWidth: number, pageHeight: number): number {
|
|
36
|
+
if (pageWidth <= 0 || pageHeight <= 0)
|
|
37
|
+
return 1
|
|
38
|
+
const webkit = isWebKit()
|
|
39
|
+
const maxSide = webkit ? WEBKIT_MAX_SIDE : DEFAULT_MAX_SIDE
|
|
40
|
+
const maxArea = webkit ? WEBKIT_MAX_AREA : DEFAULT_MAX_AREA
|
|
41
|
+
return Math.min(
|
|
42
|
+
maxSide / pageWidth,
|
|
43
|
+
maxSide / pageHeight,
|
|
44
|
+
Math.sqrt(maxArea / (pageWidth * pageHeight)),
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Device pixel ratio, capped. A 3x phone screen triples the canvas area for a
|
|
50
|
+
* sharpness gain nobody can see at arm's length on a shop floor, and it is the
|
|
51
|
+
* quickest way to hit the budget above.
|
|
52
|
+
*/
|
|
53
|
+
export function renderDpr(): number {
|
|
54
|
+
if (typeof window === 'undefined')
|
|
55
|
+
return 1
|
|
56
|
+
return Math.min(window.devicePixelRatio || 1, 2)
|
|
57
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The viewer is shared by the ant-os shell and by iframe apps, which run
|
|
3
|
+
* separate vue-i18n instances with separate message catalogues. Rather than
|
|
4
|
+
* pick one and make the other wrong, it takes its strings as a prop and each
|
|
5
|
+
* host passes its own translations. English is the fallback, not the design.
|
|
6
|
+
*/
|
|
7
|
+
export interface PdfViewerLabels {
|
|
8
|
+
zoomIn: string
|
|
9
|
+
zoomOut: string
|
|
10
|
+
fitPage: string
|
|
11
|
+
fitWidth: string
|
|
12
|
+
rotate: string
|
|
13
|
+
previousPage: string
|
|
14
|
+
nextPage: string
|
|
15
|
+
pageOf: string
|
|
16
|
+
loading: string
|
|
17
|
+
loadFailed: string
|
|
18
|
+
passwordProtected: string
|
|
19
|
+
renderFailed: string
|
|
20
|
+
openInNewTab: string
|
|
21
|
+
document: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const defaultPdfViewerLabels: PdfViewerLabels = {
|
|
25
|
+
zoomIn: 'Zoom in',
|
|
26
|
+
zoomOut: 'Zoom out',
|
|
27
|
+
fitPage: 'Fit page',
|
|
28
|
+
fitWidth: 'Fit width',
|
|
29
|
+
rotate: 'Rotate',
|
|
30
|
+
previousPage: 'Previous page',
|
|
31
|
+
nextPage: 'Next page',
|
|
32
|
+
pageOf: 'Page {page} of {total}',
|
|
33
|
+
loading: 'Loading drawing…',
|
|
34
|
+
loadFailed: 'This document could not be opened',
|
|
35
|
+
passwordProtected: 'This document is password protected',
|
|
36
|
+
renderFailed: 'This page could not be displayed',
|
|
37
|
+
openInNewTab: 'Open in a new tab',
|
|
38
|
+
document: 'Document',
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Maps a load/render failure onto the host's wording for it. */
|
|
42
|
+
export function pdfErrorMessage(
|
|
43
|
+
error: 'load_failed' | 'password_protected' | 'render_failed' | null,
|
|
44
|
+
labels: PdfViewerLabels,
|
|
45
|
+
): string | null {
|
|
46
|
+
switch (error) {
|
|
47
|
+
case 'password_protected': return labels.passwordProtected
|
|
48
|
+
case 'render_failed': return labels.renderFailed
|
|
49
|
+
case 'load_failed': return labels.loadFailed
|
|
50
|
+
default: return null
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type { PDFDocumentProxy, PDFPageProxy, RenderTask } from 'pdfjs-dist'
|
|
2
|
+
import type { Ref } from 'vue'
|
|
3
|
+
// The worker is a static asset reference, not a module import — it costs a URL
|
|
4
|
+
// string here and is fetched only once pdf.js actually starts. Resolving it in
|
|
5
|
+
// this file (rather than shipping a baked path in the library bundle) is why
|
|
6
|
+
// PdfViewer is exported as source: every consuming app's bundler emits its own.
|
|
7
|
+
import PdfWorkerUrl from 'pdfjs-dist/legacy/build/pdf.worker.min.mjs?url'
|
|
8
|
+
import { onBeforeUnmount, ref, shallowRef, watch } from 'vue'
|
|
9
|
+
|
|
10
|
+
import { maxRenderScale, renderDpr } from './canvasBudget'
|
|
11
|
+
|
|
12
|
+
// The legacy build's declarations are `export * from "pdfjs-dist"`, so this is
|
|
13
|
+
// the full API surface, not a narrowed one.
|
|
14
|
+
type PdfJsModule = typeof import('pdfjs-dist/legacy/build/pdf.mjs')
|
|
15
|
+
|
|
16
|
+
// The legacy build, deliberately: it carries the transpilation and polyfills
|
|
17
|
+
// that keep pdf.js working on older WebKit. The modern build needs Safari 16.4+,
|
|
18
|
+
// and a shop-floor iPad is exactly the device least likely to be current.
|
|
19
|
+
let pdfjsPromise: Promise<PdfJsModule> | null = null
|
|
20
|
+
|
|
21
|
+
async function loadPdfJs(): Promise<PdfJsModule> {
|
|
22
|
+
if (!pdfjsPromise) {
|
|
23
|
+
pdfjsPromise = import('pdfjs-dist/legacy/build/pdf.mjs').then((mod) => {
|
|
24
|
+
mod.GlobalWorkerOptions.workerSrc = PdfWorkerUrl
|
|
25
|
+
return mod
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
return pdfjsPromise
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type PdfLoadError = 'load_failed' | 'password_protected' | 'render_failed'
|
|
32
|
+
|
|
33
|
+
export function usePdfDocument(source: Ref<string | null | undefined>) {
|
|
34
|
+
const doc = shallowRef<PDFDocumentProxy | null>(null)
|
|
35
|
+
const page = shallowRef<PDFPageProxy | null>(null)
|
|
36
|
+
const pageCount = ref(0)
|
|
37
|
+
const pageNumber = ref(1)
|
|
38
|
+
const rotation = ref(0)
|
|
39
|
+
/** Intrinsic page size in CSS pixels at scale 1, with rotation applied. */
|
|
40
|
+
const pageWidth = ref(0)
|
|
41
|
+
const pageHeight = ref(0)
|
|
42
|
+
const isLoading = ref(false)
|
|
43
|
+
const error = ref<PdfLoadError | null>(null)
|
|
44
|
+
|
|
45
|
+
let renderTask: RenderTask | null = null
|
|
46
|
+
/** Guards against a slow load resolving after the source has moved on. */
|
|
47
|
+
let loadToken = 0
|
|
48
|
+
|
|
49
|
+
function cancelRender() {
|
|
50
|
+
renderTask?.cancel()
|
|
51
|
+
renderTask = null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function destroyDoc() {
|
|
55
|
+
cancelRender()
|
|
56
|
+
const current = doc.value
|
|
57
|
+
doc.value = null
|
|
58
|
+
page.value = null
|
|
59
|
+
await current?.destroy().catch(() => { /* teardown is best-effort */ })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function load(url: string) {
|
|
63
|
+
const token = ++loadToken
|
|
64
|
+
isLoading.value = true
|
|
65
|
+
error.value = null
|
|
66
|
+
await destroyDoc()
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const pdfjs = await loadPdfJs()
|
|
70
|
+
// Range and streaming are switched off on purpose. The DMS stream endpoint
|
|
71
|
+
// is a Laravel StreamedResponse, which does not serve HTTP 206 — leaving
|
|
72
|
+
// partial loading on means pdf.js probes for it on every document and then
|
|
73
|
+
// has to unwind. One plain GET is both faster here and predictable.
|
|
74
|
+
const task = pdfjs.getDocument({ url, disableRange: true, disableStream: true })
|
|
75
|
+
const loaded = await task.promise
|
|
76
|
+
if (token !== loadToken) {
|
|
77
|
+
await loaded.destroy().catch(() => {})
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
doc.value = loaded
|
|
81
|
+
pageCount.value = loaded.numPages
|
|
82
|
+
pageNumber.value = 1
|
|
83
|
+
rotation.value = 0
|
|
84
|
+
await selectPage(1)
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
if (token !== loadToken)
|
|
88
|
+
return
|
|
89
|
+
error.value = (err as { name?: string })?.name === 'PasswordException'
|
|
90
|
+
? 'password_protected'
|
|
91
|
+
: 'load_failed'
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
if (token === loadToken)
|
|
95
|
+
isLoading.value = false
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function selectPage(next: number) {
|
|
100
|
+
const current = doc.value
|
|
101
|
+
if (!current)
|
|
102
|
+
return
|
|
103
|
+
const clamped = Math.min(Math.max(next, 1), current.numPages)
|
|
104
|
+
pageNumber.value = clamped
|
|
105
|
+
cancelRender()
|
|
106
|
+
const loaded = await current.getPage(clamped)
|
|
107
|
+
page.value = loaded
|
|
108
|
+
measurePage()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function measurePage() {
|
|
112
|
+
const loaded = page.value
|
|
113
|
+
if (!loaded)
|
|
114
|
+
return
|
|
115
|
+
const viewport = loaded.getViewport({ scale: 1, rotation: rotation.value })
|
|
116
|
+
pageWidth.value = viewport.width
|
|
117
|
+
pageHeight.value = viewport.height
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function rotate() {
|
|
121
|
+
rotation.value = (rotation.value + 90) % 360
|
|
122
|
+
measurePage()
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Rasterise the current page to `canvas` at `scale` CSS pixels per page unit.
|
|
127
|
+
* The backing store is scaled by device pixel ratio for sharpness and then
|
|
128
|
+
* clamped to the engine's canvas budget, so zooming past the budget stretches
|
|
129
|
+
* the last sharp raster instead of producing a blank canvas.
|
|
130
|
+
*/
|
|
131
|
+
async function renderTo(canvas: HTMLCanvasElement, scale: number): Promise<void> {
|
|
132
|
+
const loaded = page.value
|
|
133
|
+
if (!loaded || scale <= 0)
|
|
134
|
+
return
|
|
135
|
+
cancelRender()
|
|
136
|
+
|
|
137
|
+
const cap = maxRenderScale(pageWidth.value, pageHeight.value)
|
|
138
|
+
const viewport = loaded.getViewport({
|
|
139
|
+
scale: Math.min(scale * renderDpr(), cap),
|
|
140
|
+
rotation: rotation.value,
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
// Only the backing store is touched here. The canvas's CSS box is bound to
|
|
144
|
+
// the committed scale by the caller, so the box resizes in the same tick the
|
|
145
|
+
// transform does — the old raster stretches for a frame and is then repainted
|
|
146
|
+
// sharp, instead of the box and the transform disagreeing for a frame.
|
|
147
|
+
canvas.width = Math.max(1, Math.floor(viewport.width))
|
|
148
|
+
canvas.height = Math.max(1, Math.floor(viewport.height))
|
|
149
|
+
|
|
150
|
+
const task = loaded.render({ canvas, viewport })
|
|
151
|
+
renderTask = task
|
|
152
|
+
try {
|
|
153
|
+
await task.promise
|
|
154
|
+
error.value = null
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
// Cancellation is the normal path whenever a newer render supersedes this
|
|
158
|
+
// one; only a genuine failure is worth surfacing.
|
|
159
|
+
if ((err as { name?: string })?.name !== 'RenderingCancelledException')
|
|
160
|
+
error.value = 'render_failed'
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
if (renderTask === task)
|
|
164
|
+
renderTask = null
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
watch(source, (url) => {
|
|
169
|
+
if (url)
|
|
170
|
+
void load(url)
|
|
171
|
+
else
|
|
172
|
+
void destroyDoc()
|
|
173
|
+
}, { immediate: true })
|
|
174
|
+
|
|
175
|
+
onBeforeUnmount(() => {
|
|
176
|
+
loadToken++
|
|
177
|
+
void destroyDoc()
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
doc,
|
|
182
|
+
page,
|
|
183
|
+
pageCount,
|
|
184
|
+
pageNumber,
|
|
185
|
+
rotation,
|
|
186
|
+
pageWidth,
|
|
187
|
+
pageHeight,
|
|
188
|
+
isLoading,
|
|
189
|
+
error,
|
|
190
|
+
selectPage,
|
|
191
|
+
rotate,
|
|
192
|
+
renderTo,
|
|
193
|
+
cancelRender,
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** How far one arrow-key press nudges the page, in CSS pixels. */
|
|
2
|
+
const PAN_STEP = 40
|
|
3
|
+
|
|
4
|
+
export interface PdfKeyboardActions {
|
|
5
|
+
zoomBy: (factor: number) => void
|
|
6
|
+
fitPage: () => void
|
|
7
|
+
panBy: (dx: number, dy: number) => void
|
|
8
|
+
goToPage: (delta: number) => void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Keyboard equivalents for every pointer gesture. The stage is focusable, so a
|
|
13
|
+
* desktop user (and anyone driving the page from a keyboard) can reach the same
|
|
14
|
+
* controls the touch gestures provide.
|
|
15
|
+
*/
|
|
16
|
+
export function usePdfKeyboard(actions: PdfKeyboardActions) {
|
|
17
|
+
const handlers: Record<string, () => void> = {
|
|
18
|
+
'+': () => actions.zoomBy(1.25),
|
|
19
|
+
'=': () => actions.zoomBy(1.25),
|
|
20
|
+
'-': () => actions.zoomBy(0.8),
|
|
21
|
+
'_': () => actions.zoomBy(0.8),
|
|
22
|
+
'0': () => actions.fitPage(),
|
|
23
|
+
'ArrowLeft': () => actions.panBy(PAN_STEP, 0),
|
|
24
|
+
'ArrowRight': () => actions.panBy(-PAN_STEP, 0),
|
|
25
|
+
'ArrowUp': () => actions.panBy(0, PAN_STEP),
|
|
26
|
+
'ArrowDown': () => actions.panBy(0, -PAN_STEP),
|
|
27
|
+
'PageUp': () => actions.goToPage(-1),
|
|
28
|
+
'PageDown': () => actions.goToPage(1),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return function onKeydown(e: KeyboardEvent) {
|
|
32
|
+
const handler = handlers[e.key]
|
|
33
|
+
if (!handler)
|
|
34
|
+
return
|
|
35
|
+
handler()
|
|
36
|
+
e.preventDefault()
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { ComputedRef, Ref, ShallowRef } from 'vue'
|
|
2
|
+
import { computed, onBeforeUnmount, ref, watch } from 'vue'
|
|
3
|
+
|
|
4
|
+
/** Below this the difference is invisible and not worth a re-render. */
|
|
5
|
+
const RESCALE_THRESHOLD = 0.02
|
|
6
|
+
/** Long enough to sit out a flick or a pinch, short enough to feel immediate. */
|
|
7
|
+
const SETTLE_MS = 180
|
|
8
|
+
|
|
9
|
+
export interface PdfRasterDeps {
|
|
10
|
+
canvas: Ref<HTMLCanvasElement | null>
|
|
11
|
+
/** Truthy once a page is available to draw. */
|
|
12
|
+
page: ShallowRef<unknown>
|
|
13
|
+
pageWidth: Ref<number>
|
|
14
|
+
pageHeight: Ref<number>
|
|
15
|
+
/** Scale the page should appear at right now. */
|
|
16
|
+
displayScale: ComputedRef<number>
|
|
17
|
+
/** True while a pointer gesture is in flight. */
|
|
18
|
+
isGesturing: Ref<boolean>
|
|
19
|
+
renderTo: (canvas: HTMLCanvasElement, scale: number) => Promise<void>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Bridges the gap between how big the page *should* look and how big it was last
|
|
24
|
+
* actually rasterised.
|
|
25
|
+
*
|
|
26
|
+
* Re-rendering on every frame of a pinch would drop frames on a tablet for
|
|
27
|
+
* sharpness nobody can perceive while the drawing is still moving, so the gap is
|
|
28
|
+
* carried by a CSS transform — which is free — and closed by a fresh render once
|
|
29
|
+
* the gesture settles.
|
|
30
|
+
*/
|
|
31
|
+
export function usePdfRaster(deps: PdfRasterDeps) {
|
|
32
|
+
const { canvas, page, pageWidth, pageHeight, displayScale, isGesturing, renderTo } = deps
|
|
33
|
+
|
|
34
|
+
/** The scale the canvas was last rasterised at. */
|
|
35
|
+
const committedScale = ref(0)
|
|
36
|
+
const liveRatio = computed(() => (committedScale.value > 0 ? displayScale.value / committedScale.value : 1))
|
|
37
|
+
|
|
38
|
+
const canvasStyle = computed(() => ({
|
|
39
|
+
width: `${Math.round(pageWidth.value * committedScale.value)}px`,
|
|
40
|
+
height: `${Math.round(pageHeight.value * committedScale.value)}px`,
|
|
41
|
+
}))
|
|
42
|
+
|
|
43
|
+
let settleTimer: ReturnType<typeof setTimeout> | undefined
|
|
44
|
+
|
|
45
|
+
async function commit() {
|
|
46
|
+
const el = canvas.value
|
|
47
|
+
const scale = displayScale.value
|
|
48
|
+
if (!el || !page.value || scale <= 0)
|
|
49
|
+
return
|
|
50
|
+
// Set first, await second: the CSS box and the transform change together, so
|
|
51
|
+
// the browser stretches the existing raster for the frame or two it takes the
|
|
52
|
+
// sharp one to arrive rather than showing a box that disagrees with itself.
|
|
53
|
+
committedScale.value = scale
|
|
54
|
+
await renderTo(el, scale)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function schedule(immediate = false) {
|
|
58
|
+
clearTimeout(settleTimer)
|
|
59
|
+
if (immediate) {
|
|
60
|
+
void commit()
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
settleTimer = setTimeout(() => void commit(), SETTLE_MS)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Drop back to "nothing rasterised yet" — a new page or a new rotation. */
|
|
67
|
+
function invalidate() {
|
|
68
|
+
committedScale.value = 0
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
watch([displayScale, isGesturing], ([scale, gesturing]) => {
|
|
72
|
+
if (gesturing || scale <= 0)
|
|
73
|
+
return
|
|
74
|
+
if (committedScale.value <= 0) {
|
|
75
|
+
schedule(true)
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
if (Math.abs(scale / committedScale.value - 1) > RESCALE_THRESHOLD)
|
|
79
|
+
schedule()
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
onBeforeUnmount(() => clearTimeout(settleTimer))
|
|
83
|
+
|
|
84
|
+
return { committedScale, liveRatio, canvasStyle, schedule, invalidate }
|
|
85
|
+
}
|