@file-viewer/renderer-image 2.1.17 → 2.1.18
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/image.js +61 -13
- package/package.json +2 -2
package/dist/image.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, registerFileViewerZoomProvider, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
1
|
+
import { createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, resolveFileViewerFitScale, registerFileViewerZoomProvider, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
|
|
2
2
|
const imageMimeMap = {
|
|
3
3
|
avif: 'image/avif',
|
|
4
4
|
bmp: 'image/bmp',
|
|
@@ -27,8 +27,8 @@ const imageStyle = `
|
|
|
27
27
|
@media (prefers-color-scheme:dark){.file-viewer[data-viewer-theme='system'] .image-viewer{background:#101820}}
|
|
28
28
|
@media (max-width:767px){.image-stage{padding:12px}.image-lightbox{padding:16px}.image-lightbox button{top:12px;right:12px}}
|
|
29
29
|
`;
|
|
30
|
-
const createStyle = () => {
|
|
31
|
-
const style =
|
|
30
|
+
const createStyle = (documentRef) => {
|
|
31
|
+
const style = documentRef.createElement('style');
|
|
32
32
|
style.textContent = imageStyle;
|
|
33
33
|
return style;
|
|
34
34
|
};
|
|
@@ -71,16 +71,16 @@ const resolveImageUrl = async (buffer, type) => {
|
|
|
71
71
|
const roundImageScale = (value) => {
|
|
72
72
|
return Number(value.toFixed(3));
|
|
73
73
|
};
|
|
74
|
-
const createLightbox = (src, t) => {
|
|
75
|
-
const lightbox =
|
|
74
|
+
const createLightbox = (documentRef, src, t) => {
|
|
75
|
+
const lightbox = documentRef.createElement('div');
|
|
76
76
|
lightbox.className = 'image-lightbox';
|
|
77
77
|
lightbox.hidden = true;
|
|
78
78
|
lightbox.setAttribute('role', 'dialog');
|
|
79
79
|
lightbox.setAttribute('aria-modal', 'true');
|
|
80
|
-
const image =
|
|
80
|
+
const image = documentRef.createElement('img');
|
|
81
81
|
image.alt = t('image.lightbox.alt');
|
|
82
82
|
image.src = src;
|
|
83
|
-
const closeButton =
|
|
83
|
+
const closeButton = documentRef.createElement('button');
|
|
84
84
|
closeButton.type = 'button';
|
|
85
85
|
closeButton.setAttribute('aria-label', t('image.lightbox.close'));
|
|
86
86
|
closeButton.textContent = 'x';
|
|
@@ -108,27 +108,28 @@ const createLightbox = (src, t) => {
|
|
|
108
108
|
};
|
|
109
109
|
};
|
|
110
110
|
export default async function renderImage(buffer, target, type, context) {
|
|
111
|
+
var _a;
|
|
111
112
|
const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
|
|
113
|
+
const documentRef = target.ownerDocument || document;
|
|
112
114
|
const src = await resolveImageUrl(buffer, type);
|
|
113
115
|
let userZoom = 1;
|
|
114
116
|
let fitScale = 1;
|
|
115
117
|
let currentScale = 1;
|
|
116
118
|
let viewportHeight = 0;
|
|
117
119
|
const zoomEmitter = createZoomChangeEmitter();
|
|
118
|
-
const root =
|
|
120
|
+
const root = documentRef.createElement('div');
|
|
119
121
|
root.className = 'image-viewer';
|
|
120
122
|
root.dataset.viewerZoomProvider = 'image';
|
|
121
|
-
const stage =
|
|
123
|
+
const stage = documentRef.createElement('div');
|
|
122
124
|
stage.className = 'image-stage';
|
|
123
|
-
const image =
|
|
125
|
+
const image = documentRef.createElement('img');
|
|
124
126
|
image.alt = t('image.alt');
|
|
125
127
|
image.src = src;
|
|
126
128
|
stage.append(image);
|
|
127
129
|
root.append(stage);
|
|
128
|
-
const lightbox = createLightbox(src, t);
|
|
130
|
+
const lightbox = createLightbox(documentRef, src, t);
|
|
129
131
|
const openLightbox = () => lightbox.open();
|
|
130
132
|
image.addEventListener('click', openLightbox);
|
|
131
|
-
document.body.append(lightbox.element);
|
|
132
133
|
const getMinScale = () => Math.min(0.1, fitScale || 0.1);
|
|
133
134
|
const clampScale = (value) => {
|
|
134
135
|
const minScale = getMinScale();
|
|
@@ -181,6 +182,51 @@ export default async function renderImage(buffer, target, type, context) {
|
|
|
181
182
|
zoomEmitter.emit();
|
|
182
183
|
return getZoomState();
|
|
183
184
|
};
|
|
185
|
+
const fitImage = (request) => {
|
|
186
|
+
var _a, _b;
|
|
187
|
+
const naturalWidth = image.naturalWidth || 0;
|
|
188
|
+
const naturalHeight = image.naturalHeight || 0;
|
|
189
|
+
if (!naturalWidth || !naturalHeight) {
|
|
190
|
+
return {
|
|
191
|
+
applied: false,
|
|
192
|
+
mode: request.mode,
|
|
193
|
+
resize: request.resize,
|
|
194
|
+
source: request.source,
|
|
195
|
+
reason: 'image-not-ready',
|
|
196
|
+
provider: 'zoom',
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
const mode = request.mode === 'auto' ? 'scale-down' : request.mode;
|
|
200
|
+
const scale = resolveFileViewerFitScale({
|
|
201
|
+
mode,
|
|
202
|
+
viewportWidth: Math.max(1, request.viewportWidth || root.clientWidth || 0),
|
|
203
|
+
viewportHeight: Math.max(1, request.viewportHeight || root.clientHeight || viewportHeight || 0),
|
|
204
|
+
contentWidth: naturalWidth,
|
|
205
|
+
contentHeight: naturalHeight,
|
|
206
|
+
currentScale,
|
|
207
|
+
minScale: (_a = request.minScale) !== null && _a !== void 0 ? _a : getMinScale(),
|
|
208
|
+
maxScale: (_b = request.maxScale) !== null && _b !== void 0 ? _b : 5,
|
|
209
|
+
});
|
|
210
|
+
if (!scale) {
|
|
211
|
+
return {
|
|
212
|
+
applied: false,
|
|
213
|
+
mode: request.mode,
|
|
214
|
+
resize: request.resize,
|
|
215
|
+
source: request.source,
|
|
216
|
+
reason: 'unmeasurable',
|
|
217
|
+
provider: 'zoom',
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const state = setZoom(scale);
|
|
221
|
+
return {
|
|
222
|
+
applied: true,
|
|
223
|
+
mode: request.mode,
|
|
224
|
+
resize: request.resize,
|
|
225
|
+
scale: state.scale,
|
|
226
|
+
source: request.source,
|
|
227
|
+
provider: 'zoom',
|
|
228
|
+
};
|
|
229
|
+
};
|
|
184
230
|
registerFileViewerZoomProvider(root, {
|
|
185
231
|
zoomIn: () => setZoom(currentScale + 0.15),
|
|
186
232
|
zoomOut: () => setZoom(currentScale - 0.15),
|
|
@@ -191,10 +237,12 @@ export default async function renderImage(buffer, target, type, context) {
|
|
|
191
237
|
return getZoomState();
|
|
192
238
|
},
|
|
193
239
|
setZoom,
|
|
240
|
+
fit: fitImage,
|
|
194
241
|
getState: getZoomState,
|
|
195
242
|
subscribe: zoomEmitter.subscribe,
|
|
196
243
|
});
|
|
197
|
-
target.replaceChildren(createStyle(), root);
|
|
244
|
+
target.replaceChildren(createStyle(documentRef), root);
|
|
245
|
+
(((_a = context === null || context === void 0 ? void 0 : context.surface) === null || _a === void 0 ? void 0 : _a.shadowRoot) || target).append(lightbox.element);
|
|
198
246
|
updateViewportSize();
|
|
199
247
|
return {
|
|
200
248
|
$el: target,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@file-viewer/renderer-image",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.18",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Standalone image renderer plugin for Flyfish File Viewer with native image preview, HEIC/HEIF conversion, lightbox, and unified zoom support.",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"LICENSE"
|
|
55
55
|
],
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@file-viewer/core": "^2.1.
|
|
57
|
+
"@file-viewer/core": "^2.1.18",
|
|
58
58
|
"heic2any": "^0.0.4"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|