153 lines
5.1 KiB
TypeScript
153 lines
5.1 KiB
TypeScript
import {
|
|
ATTACHMENT_ID_ATTR,
|
|
TEMP_FILE_ID_ATTR,
|
|
isLocalMediaId,
|
|
normalizeContent
|
|
} from "@timi/web/editor";
|
|
import type { ArticleContentType, Attachment } from "./types";
|
|
|
|
/** 文章内容中附件和临时文件的标识前缀。 */
|
|
export const ATTACHMENT_ID_PREFIX = "attachment-id:";
|
|
export const TEMP_FILE_ID_PREFIX = "temp-file-id:";
|
|
|
|
const DEFAULT_ARTICLE_CONTENT_TYPE: ArticleContentType = "TIP_TAP";
|
|
const ARTICLE_CONTENT_TYPE_LIST: ArticleContentType[] = ["TEXT", "MARKDOWN", "TIP_TAP"];
|
|
|
|
/** 将未知正文类型归一化为组件支持的类型。 */
|
|
export function normalizeArticleContentType(contentType?: string): ArticleContentType {
|
|
return ARTICLE_CONTENT_TYPE_LIST.includes(contentType as ArticleContentType)
|
|
? contentType as ArticleContentType
|
|
: DEFAULT_ARTICLE_CONTENT_TYPE;
|
|
}
|
|
|
|
/** 将空正文转换为空字符串。 */
|
|
export function normalizeRawArticleContent(content?: string): string {
|
|
return content || "";
|
|
}
|
|
|
|
/** 从媒体地址中读取附件 ID。 */
|
|
export function getAttachmentIdFromSrc(src?: string): string {
|
|
if (!src?.startsWith(ATTACHMENT_ID_PREFIX)) {
|
|
return "";
|
|
}
|
|
return src.slice(ATTACHMENT_ID_PREFIX.length);
|
|
}
|
|
|
|
/** 从媒体地址中读取临时文件 ID。 */
|
|
export function getTempFileIdFromSrc(src?: string): string {
|
|
if (!src?.startsWith(TEMP_FILE_ID_PREFIX)) {
|
|
return "";
|
|
}
|
|
return src.slice(TEMP_FILE_ID_PREFIX.length);
|
|
}
|
|
|
|
/** 构造带有附件 ID 的内部媒体地址。 */
|
|
function buildAttachmentIdSrc(attachmentId: string): string {
|
|
return `${ATTACHMENT_ID_PREFIX}${attachmentId}`;
|
|
}
|
|
|
|
/** 仅为本地预览地址保留预览元数据。 */
|
|
function buildAttachmentMetadata(src: string): Record<string, string> | undefined {
|
|
if (!src.startsWith("blob:")) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
previewUrl: src
|
|
};
|
|
}
|
|
|
|
/** 过滤前端本地占位 ID。 */
|
|
function normalizeAttachmentTempFileId(tempFileId?: string): string | undefined {
|
|
if (!tempFileId || isLocalMediaId(tempFileId)) {
|
|
return undefined;
|
|
}
|
|
return tempFileId;
|
|
}
|
|
|
|
/** 从正文中提取媒体附件,返回组件需要的最小对象。 */
|
|
export function buildAttachmentListFromContent(content?: string, sourceList?: readonly Attachment[]): Attachment[] {
|
|
const rawContent = normalizeContent(content);
|
|
if (!rawContent) {
|
|
return [];
|
|
}
|
|
const parser = new DOMParser();
|
|
const doc = parser.parseFromString(rawContent, "text/html");
|
|
const sourceMap = new Map<string, Attachment>();
|
|
for (const item of sourceList || []) {
|
|
if (item.id) {
|
|
sourceMap.set(item.id, item);
|
|
}
|
|
if (item.tempFileId) {
|
|
sourceMap.set(item.tempFileId, item);
|
|
}
|
|
}
|
|
const attachmentMap = new Map<string, Attachment>();
|
|
|
|
for (const media of Array.from(doc.querySelectorAll("img, video"))) {
|
|
const src = media.getAttribute("src") || "";
|
|
const attachmentId = media.getAttribute(ATTACHMENT_ID_ATTR) || getAttachmentIdFromSrc(src);
|
|
const tempFileId = media.getAttribute(TEMP_FILE_ID_ATTR) || getTempFileIdFromSrc(src);
|
|
const key = attachmentId || tempFileId;
|
|
if (!key || attachmentMap.has(key)) {
|
|
continue;
|
|
}
|
|
const sourceAttachment = sourceMap.get(key);
|
|
const previewUrl = buildAttachmentMetadata(src);
|
|
attachmentMap.set(key, {
|
|
...(sourceAttachment || {}),
|
|
id: attachmentId || sourceAttachment?.id || key,
|
|
tempFileId: normalizeAttachmentTempFileId(tempFileId) || normalizeAttachmentTempFileId(sourceAttachment?.tempFileId),
|
|
name: media.getAttribute("title") || media.getAttribute("alt") || sourceAttachment?.name || "",
|
|
metadata: previewUrl || sourceAttachment?.metadata
|
|
});
|
|
}
|
|
|
|
return Array.from(attachmentMap.values());
|
|
}
|
|
|
|
/** 将媒体地址转换为组件统一的附件标识地址。 */
|
|
export function normalizeContentForStore(content?: string): string {
|
|
const rawContent = normalizeContent(content);
|
|
if (!rawContent) {
|
|
return "";
|
|
}
|
|
const parser = new DOMParser();
|
|
const doc = parser.parseFromString(rawContent, "text/html");
|
|
for (const media of Array.from(doc.querySelectorAll("img, video"))) {
|
|
const src = media.getAttribute("src") || "";
|
|
const attachmentId = media.getAttribute(ATTACHMENT_ID_ATTR) || getAttachmentIdFromSrc(src);
|
|
const tempFileId = media.getAttribute(TEMP_FILE_ID_ATTR) || getTempFileIdFromSrc(src);
|
|
const finalAttachmentId = attachmentId || tempFileId;
|
|
if (!finalAttachmentId) {
|
|
continue;
|
|
}
|
|
media.setAttribute(ATTACHMENT_ID_ATTR, finalAttachmentId);
|
|
media.removeAttribute(TEMP_FILE_ID_ATTR);
|
|
media.setAttribute("src", buildAttachmentIdSrc(finalAttachmentId));
|
|
}
|
|
return normalizeContent(doc.body.innerHTML);
|
|
}
|
|
|
|
/** 比较附件列表的通用字段,不比较对象引用。 */
|
|
export function sameAttachmentList(left?: readonly Attachment[], right?: readonly Attachment[]): boolean {
|
|
const leftList = left || [];
|
|
const rightList = right || [];
|
|
if (leftList.length !== rightList.length) {
|
|
return false;
|
|
}
|
|
for (let i = 0; i < leftList.length; i += 1) {
|
|
const leftItem = leftList[i];
|
|
const rightItem = rightList[i];
|
|
if (!rightItem || leftItem.id !== rightItem.id) {
|
|
return false;
|
|
}
|
|
if ((leftItem.tempFileId || "") !== (rightItem.tempFileId || "")) {
|
|
return false;
|
|
}
|
|
if ((leftItem.accessKey?.value || "") !== (rightItem.accessKey?.value || "")) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|