-
-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathrender-layer.vue
More file actions
148 lines (132 loc) · 3.81 KB
/
Copy pathrender-layer.vue
File metadata and controls
148 lines (132 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import { ignore, PdfErrorCode, Rotation } from '@embedpdf/models';
import { useDocumentState } from '@embedpdf/core/vue';
import { useRenderCapability } from '../hooks';
interface RenderLayerProps {
/**
* The ID of the document to render from
*/
documentId: string;
/**
* The page index to render (0-based)
*/
pageIndex: number;
/**
* Optional scale override. If not provided, uses document's current scale.
*/
scale?: number;
/**
* Optional device pixel ratio override. If not provided, uses window.devicePixelRatio.
*/
dpr?: number;
/**
* Optional rotation override. If not provided, uses page rotation plus document rotation.
*/
rotation?: Rotation;
}
const props = defineProps<RenderLayerProps>();
const { provides: renderProvides } = useRenderCapability();
const documentState = useDocumentState(() => props.documentId);
const imageUrl = ref<string | null>(null);
let urlRef: string | null = null;
let hasLoaded = false;
// Get refresh version from core state
const refreshVersion = computed(() => {
if (!documentState.value) return 0;
return documentState.value.pageRefreshVersions[props.pageIndex] || 0;
});
// Determine actual render options: use overrides if provided, otherwise fall back to document state
const actualScale = computed(() => {
if (props.scale !== undefined) return props.scale;
return documentState.value?.scale ?? 1;
});
const actualDpr = computed(() => {
if (props.dpr !== undefined) return props.dpr;
return window.devicePixelRatio;
});
const actualRotation = computed(() => {
if (props.rotation !== undefined) return props.rotation;
const pageRotation =
documentState.value?.document?.pages.find((page) => page.index === props.pageIndex)?.rotation ??
Rotation.Degree0;
const documentRotation = documentState.value?.rotation ?? Rotation.Degree0;
return ((pageRotation + documentRotation) % 4) as Rotation;
});
// Render page when dependencies change
watch(
[
() => props.documentId,
() => props.pageIndex,
actualRotation,
actualScale,
actualDpr,
renderProvides,
refreshVersion,
],
([docId, pageIdx, rotation, scale, dpr, capability], [prevDocId], onCleanup) => {
if (!capability) {
imageUrl.value = null;
return;
}
// CRITICAL: Clear image immediately when documentId changes (not for zoom/scale)
if (prevDocId !== undefined && prevDocId !== docId) {
imageUrl.value = null;
if (urlRef && hasLoaded) {
URL.revokeObjectURL(urlRef);
urlRef = null;
hasLoaded = false;
}
}
// Revoke old URL before creating new one (if it's been loaded)
if (urlRef && hasLoaded && prevDocId === docId) {
URL.revokeObjectURL(urlRef);
urlRef = null;
hasLoaded = false;
}
const task = capability.forDocument(docId).renderPage({
pageIndex: pageIdx,
options: {
scaleFactor: scale,
dpr,
rotation,
},
});
task.wait((blob) => {
const objectUrl = URL.createObjectURL(blob);
urlRef = objectUrl;
imageUrl.value = objectUrl;
hasLoaded = false;
}, ignore);
onCleanup(() => {
if (urlRef) {
// Only revoke if image has loaded
if (hasLoaded) {
URL.revokeObjectURL(urlRef);
urlRef = null;
hasLoaded = false;
}
} else {
// Task still in progress, abort it
task.abort({
code: PdfErrorCode.Cancelled,
message: 'canceled render task',
});
}
});
},
{ immediate: true },
);
function handleImageLoad() {
hasLoaded = true;
}
</script>
<template>
<img
v-if="imageUrl"
:src="imageUrl"
:style="{ width: '100%', height: '100%' }"
@load="handleImageLoad"
v-bind="$attrs"
/>
</template>