Skip to content

Commit e097f75

Browse files
committed
Fix some issues in Svelte version
1 parent 238ad6f commit e097f75

8 files changed

Lines changed: 106 additions & 38 deletions

File tree

packages/plugin-capture/src/svelte/components/MarqueeCapture.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
fill = 'rgba(33,150,243,0.15)',
2727
}: MarqueeCaptureProps = $props();
2828
29-
const { provides: capturePlugin } = useCaptureCapability();
29+
const captureCapability = useCaptureCapability();
3030
const documentState = useDocumentState(documentId);
3131
3232
let rect = $state<Rect | null>(null);
@@ -38,11 +38,11 @@
3838
$effect(() => {
3939
rect = null;
4040
41-
if (!capturePlugin) {
41+
if (!captureCapability.provides) {
4242
return;
4343
}
4444
45-
return capturePlugin.registerMarqueeOnPage({
45+
return captureCapability.provides.registerMarqueeOnPage({
4646
documentId,
4747
pageIndex,
4848
scale: actualScale,

packages/plugin-interaction-manager/src/svelte/components/GlobalPointerProvider.svelte

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
}: GlobalPointerProviderProps = $props();
1919
2020
let ref = $state<HTMLDivElement | null>(null);
21-
const { provides: cap } = useInteractionManagerCapability();
21+
const interactionManagerCapability = useInteractionManagerCapability();
2222
2323
$effect(() => {
24-
if (!cap || !ref) return;
24+
if (!interactionManagerCapability.provides || !ref) return;
2525
26-
return createPointerProvider(cap, { type: 'global', documentId }, ref);
26+
return createPointerProvider(
27+
interactionManagerCapability.provides,
28+
{ type: 'global', documentId },
29+
ref,
30+
);
2731
});
2832
</script>
2933

packages/plugin-interaction-manager/src/svelte/components/PagePointerProvider.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
3030
let ref = $state<HTMLDivElement | null>(null);
3131
32-
const { provides: cap } = useInteractionManagerCapability();
32+
const interactionManagerCapability = useInteractionManagerCapability();
3333
const isPageExclusive = useIsPageExclusive(documentId);
3434
const documentState = useDocumentState(documentId);
3535
@@ -64,10 +64,10 @@
6464
});
6565
6666
$effect(() => {
67-
if (!cap || !ref) return;
67+
if (!interactionManagerCapability.provides || !ref) return;
6868
6969
return createPointerProvider(
70-
cap,
70+
interactionManagerCapability.provides,
7171
{ type: 'page', documentId, pageIndex },
7272
ref,
7373
convertEventToPoint || defaultConvertEventToPoint,

packages/plugin-render/src/svelte/components/RenderLayer.svelte

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,56 +22,106 @@
2222
*/
2323
dpr?: number;
2424
class?: string;
25+
style?: string;
2526
}
2627
28+
// Single allowed $props() call
29+
const allProps: RenderLayerProps = $props();
30+
31+
// Keep the rest reactive (Svelte will wire these to prop updates)
2732
let {
2833
documentId,
29-
pageIndex,
3034
scale: scaleOverride,
3135
dpr: dprOverride,
3236
class: propsClass,
33-
...props
34-
}: RenderLayerProps = $props();
37+
style: propsStyle,
38+
pageIndex,
39+
...attrs
40+
} = allProps;
41+
42+
// Local non-reactive page index that only updates on actual change
43+
let localPageIndex = $state(pageIndex);
44+
45+
// Watcher effect: only update localPageIndex if prop actually changes
46+
$effect(() => {
47+
if (pageIndex !== localPageIndex) {
48+
localPageIndex = pageIndex;
49+
}
50+
});
51+
52+
const renderCapability = useRenderCapability();
3553
36-
const { provides: renderProvides } = useRenderCapability();
54+
// Make document state follow the (reactive) documentId
3755
const documentState = useDocumentState(documentId);
3856
3957
let imageUrl = $state<string | null>(null);
4058
let urlRef: string | null = null;
4159
42-
// Get refresh version from core state
60+
// Track page refreshes from core
4361
const refreshVersion = $derived(documentState.current?.pageRefreshVersions?.[pageIndex] ?? 0);
4462
45-
// Determine actual render options: use overrides if provided, otherwise fall back to document state
63+
// Resolve actual scale / dpr (overrides win, otherwise follow document state)
4664
const actualScale = $derived(
4765
scaleOverride !== undefined ? scaleOverride : (documentState.current?.scale ?? 1),
4866
);
4967
5068
const actualDpr = $derived(dprOverride !== undefined ? dprOverride : window.devicePixelRatio);
5169
52-
// Render whenever inputs change (documentId, pageIndex, actualScale, actualDpr, renderProvides, refreshVersion)
70+
// Effect: reruns when:
71+
// - documentId changes
72+
// - actualScale changes
73+
// - actualDpr changes
74+
// - refreshVersion changes
75+
// - renderCapability.provides changes
76+
// It does NOT track pageIndex reactively.
5377
$effect(() => {
54-
if (!renderProvides) return;
78+
const capability = renderCapability.provides;
79+
const docId = documentId;
80+
const scale = actualScale;
81+
const dpr = actualDpr;
82+
const refresh = refreshVersion;
83+
const page = localPageIndex;
5584
56-
const task = renderProvides.forDocument(documentId).renderPage({
57-
pageIndex,
85+
if (!capability || !docId) {
86+
// Cleanup if no capability/doc
87+
if (urlRef) {
88+
URL.revokeObjectURL(urlRef);
89+
urlRef = null;
90+
}
91+
imageUrl = null;
92+
return;
93+
}
94+
95+
const scoped = capability.forDocument(docId);
96+
97+
const task = scoped.renderPage({
98+
pageIndex: page,
5899
options: {
59-
scaleFactor: actualScale,
60-
dpr: actualDpr,
100+
scaleFactor: scale,
101+
dpr,
61102
},
62103
});
63104
64105
task.wait((blob) => {
65106
const url = URL.createObjectURL(blob);
66-
imageUrl = url;
107+
108+
// Revoke previous URL if it existed
109+
if (urlRef) {
110+
URL.revokeObjectURL(urlRef);
111+
}
112+
67113
urlRef = url;
114+
imageUrl = url;
68115
}, ignore);
69116
70117
return () => {
118+
// Cleanup for this render run
71119
if (urlRef) {
72120
URL.revokeObjectURL(urlRef);
73121
urlRef = null;
122+
imageUrl = null;
74123
} else {
124+
// If render not finished, abort task
75125
task.abort({
76126
code: PdfErrorCode.Cancelled,
77127
message: 'canceled render task',
@@ -81,6 +131,7 @@
81131
});
82132
83133
function handleImageLoad() {
134+
// Once image is loaded, we can drop the objectURL reference
84135
if (urlRef) {
85136
URL.revokeObjectURL(urlRef);
86137
urlRef = null;
@@ -92,8 +143,8 @@
92143
<img
93144
src={imageUrl}
94145
onload={handleImageLoad}
95-
{...props}
96-
style="width: 100%; height: 100%;"
146+
style={`width: 100%; height: 100%; ${propsStyle ?? ''}`}
147+
{...attrs}
97148
class={propsClass}
98149
alt=""
99150
/>

packages/plugin-rotate/src/svelte/components/Rotate.svelte

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
<script lang="ts">
22
import type { Rotation } from '@embedpdf/models';
33
import type { Snippet } from 'svelte';
4+
import type { HTMLAttributes } from 'svelte/elements';
45
import { useDocumentState } from '@embedpdf/core/svelte';
56
import { useRotatePlugin } from '../hooks';
67
7-
interface RotateProps {
8+
type RotateProps = HTMLAttributes<HTMLDivElement> & {
89
documentId: string;
910
pageIndex: number;
1011
rotation?: Rotation;
1112
scale?: number;
1213
children?: Snippet;
13-
}
14+
class?: string;
15+
style?: string;
16+
};
1417
1518
let {
1619
documentId,
1720
pageIndex,
1821
rotation: rotationOverride,
1922
scale: scaleOverride,
2023
children,
24+
class: propsClass,
25+
style: propsStyle,
26+
...restProps
2127
}: RotateProps = $props();
2228
23-
const { plugin: rotatePlugin } = useRotatePlugin();
29+
const rotatePlugin = useRotatePlugin();
2430
const documentState = useDocumentState(documentId);
2531
2632
const page = $derived(documentState.current?.document?.pages?.[pageIndex]);
@@ -36,8 +42,8 @@
3642
);
3743
3844
const matrix = $derived(
39-
rotatePlugin
40-
? rotatePlugin.getMatrixAsString({
45+
rotatePlugin.plugin
46+
? rotatePlugin.plugin.getMatrixAsString({
4147
width: width * scale,
4248
height: height * scale,
4349
rotation: rotation,
@@ -47,7 +53,14 @@
4753
</script>
4854

4955
{#if page}
50-
<div style:position="absolute" style:transform-origin="0 0" style:transform={matrix}>
56+
<div
57+
class={propsClass}
58+
style:position="absolute"
59+
style:transform-origin="0 0"
60+
style:transform={matrix}
61+
style={propsStyle}
62+
{...restProps}
63+
>
5164
{@render children?.()}
5265
</div>
5366
{/if}

packages/plugin-search/src/svelte/components/SearchLayer.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
...divProps
2222
}: SearchLayerProps = $props();
2323
24-
const { provides: searchProvides } = useSearchCapability();
24+
const searchCapability = useSearchCapability();
2525
const documentState = useDocumentState(documentId);
2626
2727
let searchResultState = $state<SearchResultState | null>(null);
2828
29-
const scope = $derived(searchProvides?.forDocument(documentId) ?? null);
29+
const scope = $derived(searchCapability.provides?.forDocument(documentId) ?? null);
3030
3131
const actualScale = $derived(
3232
scaleOverride !== undefined ? scaleOverride : (documentState.current?.scale ?? 1),

packages/plugin-selection/src/svelte/components/SelectionLayer.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
background = 'rgba(33,150,243)',
2222
}: SelectionLayerProps = $props();
2323
24-
const { plugin: selPlugin } = useSelectionPlugin();
24+
const selectionPlugin = useSelectionPlugin();
2525
const documentState = useDocumentState(documentId);
2626
2727
let rects = $state<Rect[]>([]);
@@ -32,13 +32,13 @@
3232
);
3333
3434
$effect(() => {
35-
if (!selPlugin || !documentId) {
35+
if (!selectionPlugin.plugin || !documentId) {
3636
rects = [];
3737
boundingRect = null;
3838
return;
3939
}
4040
41-
return selPlugin.registerSelectionOnPage({
41+
return selectionPlugin.plugin.registerSelectionOnPage({
4242
documentId,
4343
pageIndex,
4444
onRectsChange: ({ rects: newRects, boundingRect: newBoundingRect }) => {

packages/plugin-zoom/src/svelte/components/MarqueeZoom.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
fill = 'rgba(33,150,243,0.15)',
2727
}: MarqueeZoomProps = $props();
2828
29-
const { provides: zoomPlugin } = useZoomCapability();
29+
const zoomCapability = useZoomCapability();
3030
const documentState = useDocumentState(documentId);
3131
3232
let rect = $state<Rect | null>(null);
@@ -38,11 +38,11 @@
3838
$effect(() => {
3939
rect = null;
4040
41-
if (!zoomPlugin) {
41+
if (!zoomCapability.provides) {
4242
return;
4343
}
4444
45-
return zoomPlugin.registerMarqueeOnPage({
45+
return zoomCapability.provides.registerMarqueeOnPage({
4646
documentId,
4747
pageIndex,
4848
scale: actualScale,

0 commit comments

Comments
 (0)