siyuan/app/src/asset/renderAssets.ts

72 lines
3.7 KiB
TypeScript
Raw Normal View History

import {Constants} from "../constants";
2022-09-09 20:40:35 +08:00
/// #if !MOBILE
import {getAllModels} from "../layout/getAll";
2022-09-09 20:40:35 +08:00
/// #endif
import {pathPosix} from "../util/pathName";
import * as dayjs from "dayjs";
export const renderAssetsPreview = (pathString: string) => {
2022-08-18 23:47:22 +08:00
if (!pathString) {
return "";
}
2022-07-17 11:06:57 +08:00
const type = pathPosix().extname(pathString).toLowerCase();
if (Constants.SIYUAN_ASSETS_IMAGE.includes(type)) {
return `<img style="max-height: 100%" src="${pathString}">`;
} else if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) {
return `<audio style="max-width: 100%" controls="controls" src="${pathString}"></audio>`;
} else if (Constants.SIYUAN_ASSETS_VIDEO.includes(type)) {
return `<video style="max-width: 100%" controls="controls" src="${pathString}"></video>`;
} else {
return pathString;
}
2022-07-17 11:06:57 +08:00
};
export const pdfResize = () => {
2022-09-09 20:40:35 +08:00
/// #if !MOBILE
2022-09-07 22:19:58 +08:00
getAllModels().asset.forEach(item => {
const pdfInstance = item.pdfObject;
if (!pdfInstance) {
return;
}
const {pdfDocument, pdfViewer} = pdfInstance;
if (!pdfDocument) {
2022-09-07 22:19:58 +08:00
return;
}
2022-12-22 22:19:04 +08:00
const pdfViewerElement = item.element.querySelector("#viewerContainer");
if (pdfViewerElement) {
// https://github.com/siyuan-note/siyuan/issues/6890
pdfViewerElement.scrollTo(0, parseInt(pdfViewerElement.getAttribute("data-scrolltop")));
2022-12-22 22:19:04 +08:00
pdfViewerElement.removeAttribute("data-scrolltop");
}
2022-09-07 22:19:58 +08:00
const currentScaleValue = pdfViewer.currentScaleValue;
if (
2022-09-07 22:19:58 +08:00
currentScaleValue === "auto" ||
currentScaleValue === "page-fit" ||
currentScaleValue === "page-width"
) {
// Note: the scale is constant for 'page-actual'.
2022-09-07 22:19:58 +08:00
pdfViewer.currentScaleValue = currentScaleValue;
}
2022-09-07 22:19:58 +08:00
pdfViewer.update();
});
2022-09-09 20:40:35 +08:00
/// #endif
2022-09-07 22:19:58 +08:00
};
export const genAssetHTML = (type: string, pathString: string, imgName: string, linkName: string) => {
2022-11-11 16:05:32 +08:00
let html = "";
if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) {
html = `<div data-node-id="${Lute.NewNodeID()}" data-type="NodeAudio" class="iframe" updated="${dayjs().format("YYYYMMDDHHmmss")}"><div class="iframe-content"><audio controls="controls" src="${pathString}" data-src="${pathString}"></audio>${Constants.ZWSP}</div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
} else if (Constants.SIYUAN_ASSETS_IMAGE.includes(type)) {
2022-11-11 16:05:32 +08:00
let netHTML = "";
if (!pathString.startsWith("assets/")) {
2022-11-11 16:05:32 +08:00
netHTML = '<span class="img__net"><svg><use xlink:href="#iconLanguage"></use></svg></span>';
}
html = `<span contenteditable="false" data-type="img" class="img"><span> </span><span><span class="protyle-action protyle-icons"><span class="protyle-icon protyle-icon--only"><svg class="svg"><use xlink:href="#iconMore"></use></svg></span></span><img src="${pathString}" data-src="${pathString}" alt="${imgName}" /><span class="protyle-action__drag"></span>${netHTML}<span class="protyle-action__title"></span></span><span> </span></span>`;
} else if (Constants.SIYUAN_ASSETS_VIDEO.includes(type)) {
html = `<div data-node-id="${Lute.NewNodeID()}" data-type="NodeVideo" class="iframe" updated="${dayjs().format("YYYYMMDDHHmmss")}"><div class="iframe-content">${Constants.ZWSP}<video controls="controls" src="${pathString}" data-src="${pathString}"></video><span class="protyle-action__drag" contenteditable="false"></span></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
} else {
html = `<span data-type="a" data-href="${pathString}">${linkName}</span>`;
}
return html;
2022-11-11 16:05:32 +08:00
};