2022-05-26 15:18:53 +08:00
|
|
|
|
import {Constants} from "../../constants";
|
|
|
|
|
|
import {addScript} from "../util/addScript";
|
|
|
|
|
|
import {fetchPost} from "../../util/fetch";
|
|
|
|
|
|
|
2025-05-18 12:06:19 +08:00
|
|
|
|
export const previewImages = (srcList: string[], currentSrc?: string) => {
|
2024-12-07 10:27:19 +08:00
|
|
|
|
addScript(`${Constants.PROTYLE_CDN}/js/viewerjs/viewer.js?v=1.11.7`, "protyleViewerScript").then(() => {
|
|
|
|
|
|
const imagesElement = document.createElement("ul");
|
2025-05-18 12:06:19 +08:00
|
|
|
|
let html = "";
|
|
|
|
|
|
let initialViewIndex = -1;
|
|
|
|
|
|
srcList.forEach((item: string, index: number) => {
|
|
|
|
|
|
if (item) {
|
|
|
|
|
|
html += `<li><img src="${item}"></li>`;
|
|
|
|
|
|
if (currentSrc && initialViewIndex === -1 && (currentSrc.endsWith(encodeURI(item)) || currentSrc.endsWith(item))) {
|
|
|
|
|
|
initialViewIndex = index;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
imagesElement.innerHTML = html;
|
2024-12-07 10:27:19 +08:00
|
|
|
|
window.siyuan.viewer = new Viewer(imagesElement, {
|
2025-05-18 12:06:19 +08:00
|
|
|
|
initialViewIndex: currentSrc ? initialViewIndex : 0,
|
2024-12-07 10:27:19 +08:00
|
|
|
|
title: [1, (image: HTMLImageElement, imageData: IObject) => {
|
|
|
|
|
|
let name = image.alt;
|
|
|
|
|
|
if (!name) {
|
|
|
|
|
|
name = image.src.substring(image.src.lastIndexOf("/") + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
name = name.substring(0, name.lastIndexOf(".")).replace(/-\d{14}-\w{7}$/, "");
|
|
|
|
|
|
return `${name} [${imageData.naturalWidth} × ${imageData.naturalHeight}]`;
|
|
|
|
|
|
}],
|
|
|
|
|
|
button: false,
|
|
|
|
|
|
transition: false,
|
|
|
|
|
|
hidden: function () {
|
|
|
|
|
|
window.siyuan.viewer.destroy();
|
|
|
|
|
|
},
|
|
|
|
|
|
toolbar: {
|
|
|
|
|
|
zoomIn: true,
|
|
|
|
|
|
zoomOut: true,
|
|
|
|
|
|
oneToOne: true,
|
|
|
|
|
|
reset: true,
|
|
|
|
|
|
prev: true,
|
|
|
|
|
|
play: true,
|
|
|
|
|
|
next: true,
|
|
|
|
|
|
rotateLeft: true,
|
|
|
|
|
|
rotateRight: true,
|
|
|
|
|
|
flipHorizontal: true,
|
|
|
|
|
|
flipVertical: true,
|
|
|
|
|
|
close: function () {
|
2023-09-22 20:24:00 +08:00
|
|
|
|
window.siyuan.viewer.destroy();
|
|
|
|
|
|
},
|
2024-12-07 10:27:19 +08:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
window.siyuan.viewer.show();
|
2023-09-22 20:24:00 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-05-18 12:06:19 +08:00
|
|
|
|
export const previewDocImage = (currentSrc: string, id: string) => {
|
|
|
|
|
|
fetchPost("/api/asset/getDocImageAssets", {id}, (response) => {
|
|
|
|
|
|
previewImages(response.data, currentSrc);
|
2025-05-18 11:31:22 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-05-20 10:01:25 +08:00
|
|
|
|
export const previewAttrViewImages = (currentSrc: string, avID: string, viewID: string, query: string) => {
|
2025-05-18 12:06:19 +08:00
|
|
|
|
fetchPost("/api/av/getCurrentAttrViewImages", {
|
|
|
|
|
|
id: avID,
|
2025-05-20 10:01:25 +08:00
|
|
|
|
viewID,
|
|
|
|
|
|
query,
|
2025-05-18 12:06:19 +08:00
|
|
|
|
}, (response) => {
|
|
|
|
|
|
previewImages(response.data, currentSrc);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|