新增共享 Preview Engine,统一 Web 连续预览、快速分页、Playwright PDF 与 Electron PDF;实现稳定前缀复用和修改位置后的增量分页,保留媒体块按文档顺序串行回填与单次重排。 完善跨端链接与桌面文档工作流:Web 受控处理锚点和 HTTP/HTTPS 外链;Desktop 支持本地路径、file URI、系统协议、多窗口、同文件单例、Markdown 当前或新窗口打开,以及聚焦时外部文件变化提示。 统一四套内置主题名称并默认使用 Typora Github;修复连续预览双滚动条、ECharts 尺寸、PDF 本地链接、围栏代码块 Typora DOM 与重复行内样式;桌面发行链强制完整重建内嵌 Web,避免安装包携带陈旧资源。 发布 Web/Compose 与 Windows NSIS/ZIP:镜像 yixiong/md-to-pdf:v0.5.0 已健康部署;NSIS SHA-256 为 60992D1FDCA513F46346C78478537EB4159D8C0E76B41ECF3CDC25BE77707D92,ZIP SHA-256 为 D74F82293FB67126E583546CBA894569EFC9A0B1B6343FAC648CCC95F1D188D8,本机安装版已升级至 v0.5.0。 验证:全项目 238 项测试通过,类型检查、生产构建和 git diff --check 通过;Web 快速/连续/精确预览、Compose、Desktop 多窗口、窗口状态、文件关联、链接与代码块均完成真实环境验收。
153 lines
3.9 KiB
TypeScript
153 lines
3.9 KiB
TypeScript
import type { PDFDocumentProxy } from "pdfjs-dist";
|
|
import { decodeLocalDocumentLinkFromPdf } from "@md-to-pdf/core";
|
|
|
|
export type PdfDestination = string | unknown[];
|
|
|
|
export interface PdfAnnotationLinkCallbacks {
|
|
onExternalLink: (href: string) => void;
|
|
onNavigateToPage: (pageNumber: number) => void;
|
|
}
|
|
|
|
export interface PdfAnnotationLinkService {
|
|
externalLinkEnabled: boolean;
|
|
addLinkAttributes(
|
|
link: HTMLAnchorElement,
|
|
url: string,
|
|
newWindow?: boolean
|
|
): void;
|
|
getDestinationHash(destination: PdfDestination): string;
|
|
getAnchorUrl(anchor: string): string;
|
|
goToDestination(destination: PdfDestination): Promise<void>;
|
|
executeNamedAction(action: string): void;
|
|
executeSetOCGState(action: object): Promise<void>;
|
|
getAttachmentContent(id: string): Promise<undefined>;
|
|
}
|
|
|
|
export async function resolvePdfDestinationPage(
|
|
document: Pick<
|
|
PDFDocumentProxy,
|
|
"getDestination" | "getPageIndex" | "numPages"
|
|
>,
|
|
destination: PdfDestination
|
|
) {
|
|
const explicitDestination =
|
|
typeof destination === "string"
|
|
? await document.getDestination(destination)
|
|
: destination;
|
|
if (
|
|
!Array.isArray(explicitDestination) ||
|
|
explicitDestination.length === 0
|
|
) {
|
|
return undefined;
|
|
}
|
|
|
|
const pageReference = explicitDestination[0];
|
|
let pageNumber: number;
|
|
if (
|
|
typeof pageReference === "number" &&
|
|
Number.isInteger(pageReference)
|
|
) {
|
|
pageNumber = pageReference + 1;
|
|
} else if (
|
|
pageReference &&
|
|
typeof pageReference === "object"
|
|
) {
|
|
pageNumber =
|
|
(await document.getPageIndex(
|
|
pageReference as Parameters<
|
|
PDFDocumentProxy["getPageIndex"]
|
|
>[0]
|
|
)) + 1;
|
|
} else {
|
|
return undefined;
|
|
}
|
|
|
|
return pageNumber >= 1 && pageNumber <= document.numPages
|
|
? pageNumber
|
|
: undefined;
|
|
}
|
|
|
|
export function resolvePdfNamedActionPage(
|
|
action: string,
|
|
currentPage: number,
|
|
pageCount: number
|
|
) {
|
|
switch (action) {
|
|
case "FirstPage":
|
|
return 1;
|
|
case "LastPage":
|
|
return pageCount;
|
|
case "NextPage":
|
|
case "PageDown":
|
|
return Math.min(pageCount, currentPage + 1);
|
|
case "PrevPage":
|
|
case "PageUp":
|
|
return Math.max(1, currentPage - 1);
|
|
default:
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function createPdfAnnotationLinkService(
|
|
document: Pick<
|
|
PDFDocumentProxy,
|
|
"getDestination" | "getPageIndex" | "numPages"
|
|
>,
|
|
currentPage: number,
|
|
callbacks: PdfAnnotationLinkCallbacks
|
|
): PdfAnnotationLinkService {
|
|
return {
|
|
externalLinkEnabled: true,
|
|
addLinkAttributes(link, url) {
|
|
const resolvedUrl =
|
|
decodeLocalDocumentLinkFromPdf(url) ?? url;
|
|
link.href = "#";
|
|
link.rel = "noopener noreferrer";
|
|
link.title ||= resolvedUrl;
|
|
link.dataset.pdfExternalHref = resolvedUrl;
|
|
link.onclick = (event) => {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
callbacks.onExternalLink(resolvedUrl);
|
|
return false;
|
|
};
|
|
},
|
|
getDestinationHash() {
|
|
return "#pdf-destination";
|
|
},
|
|
getAnchorUrl() {
|
|
return "#pdf-action";
|
|
},
|
|
async goToDestination(destination) {
|
|
try {
|
|
const pageNumber = await resolvePdfDestinationPage(
|
|
document,
|
|
destination
|
|
);
|
|
if (pageNumber !== undefined) {
|
|
callbacks.onNavigateToPage(pageNumber);
|
|
}
|
|
} catch {
|
|
// 损坏或无法解析的 PDF 目标保持无行为。
|
|
}
|
|
},
|
|
executeNamedAction(action) {
|
|
const pageNumber = resolvePdfNamedActionPage(
|
|
action,
|
|
currentPage,
|
|
document.numPages
|
|
);
|
|
if (pageNumber !== undefined) {
|
|
callbacks.onNavigateToPage(pageNumber);
|
|
}
|
|
},
|
|
async executeSetOCGState() {
|
|
// 精确预览不改变 PDF 可选内容组状态。
|
|
},
|
|
async getAttachmentContent() {
|
|
// 精确预览不下载或执行 PDF 内嵌附件。
|
|
return undefined;
|
|
}
|
|
};
|
|
}
|