refactor timi-web

This commit is contained in:
Timi
2026-08-27 16:04:06 +08:00
parent 13bd61326d
commit 7ef7653deb
33 changed files with 1193 additions and 1097 deletions
+152
View File
@@ -0,0 +1,152 @@
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;
}
+8 -1
View File
@@ -1,6 +1,13 @@
import view from "./index.vue";
import { Toolkit } from "timi-web";
import type { Article } from "timi-web/editor";
import type { Article } from "./types";
export type {
Article,
ArticleContentType,
Attachment,
UploadResult
} from "./types";
export type ArticleEditorExpose = {
submit: () => Promise<Article>;
+25 -40
View File
@@ -24,23 +24,24 @@
</template>
<script lang="ts" setup>
import type { Article, ArticleContentType, Attachment, UploadResult } from "timi-web/editor";
import { AttachmentAPI } from "timi-web/editor";
import type { Article, ArticleContentType, Attachment, UploadResult } from "./types";
import { MarkdownEditor } from "timi-web";
import { TiptapEditor } from "../tiptap-editor";
import type { TiptapEditorExpose } from "../tiptap-editor";
import {
ATTACHMENT_ID_ATTR,
TEMP_FILE_ID_ATTR,
buildAttachmentListFromContent,
getAttachmentIdFromSrc,
getTempFileIdFromSrc,
normalizeArticleContentType,
normalizeContent,
normalizeContentForStore,
normalizeRawArticleContent,
sameAttachmentList
} from "timi-web/editor";
} from "./content";
import {
ATTACHMENT_ID_ATTR,
TEMP_FILE_ID_ATTR,
normalizeContent
} from "timi-web/editor";
defineOptions({
name: "ArticleEditor"
@@ -83,39 +84,18 @@ const attachmentMap = computed(() => {
return new Map((props.modelValue?.attachmentList || []).map(item => [item.id, item]));
});
async function defaultUploadImage(file: File) {
return defaultUploadMedia(file);
}
async function defaultUploadVideo(file: File) {
return defaultUploadMedia(file);
}
async function defaultUploadMedia(file: File) {
const tempFileList = await AttachmentAPI.upload([file]);
const tempFileId = tempFileList[0]?.id || "";
if (!tempFileId) {
throw new Error("上传临时文件失败");
}
return {
id: tempFileId,
url: AttachmentAPI.getTempReadURL(tempFileId),
name: file.name
};
}
async function handleUploadImage(file: File) {
if (props.uploadImage) {
return props.uploadImage(file);
if (!props.uploadImage) {
throw new Error("请为 ArticleEditor 配置 uploadImage");
}
return defaultUploadImage(file);
return await props.uploadImage(file);
}
async function handleUploadVideo(file: File) {
if (props.uploadVideo) {
return props.uploadVideo(file);
if (!props.uploadVideo) {
throw new Error("请为 ArticleEditor 配置 uploadVideo");
}
return defaultUploadVideo(file);
return await props.uploadVideo(file);
}
async function resolveAttachmentPreviewUrl(attachmentId: string) {
@@ -126,18 +106,20 @@ async function resolveAttachmentPreviewUrl(attachmentId: string) {
return customUrl;
}
}
const previewUrl = `${attachment?.metadata?.previewUrl || ""}`;
const previewUrl = typeof attachment?.metadata?.previewUrl === "string"
? attachment.metadata.previewUrl
: "";
if (previewUrl) {
return previewUrl;
}
return attachmentId ? AttachmentAPI.getReadURL(attachmentId) : "";
return attachment?.url || "";
}
async function resolveTempPreviewUrl(tempFileId: string) {
if (props.resolveTempUrl) {
return props.resolveTempUrl(tempFileId);
return await props.resolveTempUrl(tempFileId);
}
return AttachmentAPI.getTempReadURL(tempFileId);
return "";
}
async function convertContentForEditor(value?: string) {
@@ -158,7 +140,10 @@ async function convertContentForEditor(value?: string) {
if (tempFileId) {
media.setAttribute(TEMP_FILE_ID_ATTR, tempFileId);
media.setAttribute(ATTACHMENT_ID_ATTR, attachmentId || tempFileId);
media.setAttribute("src", await resolveTempPreviewUrl(tempFileId));
const previewUrl = await resolveTempPreviewUrl(tempFileId);
if (previewUrl) {
media.setAttribute("src", previewUrl);
}
continue;
}
if (!attachmentId) {
@@ -189,7 +174,7 @@ function buildStoredContent(content: string, currentContentType: ArticleContentT
function buildArticleAttachmentList(content: string, currentContentType: ArticleContentType) {
if (currentContentType === "TIP_TAP") {
return buildAttachmentListFromContent(content);
return buildAttachmentListFromContent(content, props.modelValue?.attachmentList);
}
return props.modelValue?.attachmentList || [];
}
@@ -263,7 +248,7 @@ watch(() => props.modelValue?.attachmentList, async (value) => {
if (sameAttachmentList(lastOutgoingAttachmentList.value, value)) {
return;
}
if (sameAttachmentList(buildAttachmentListFromContent(editorContent.value), value)) {
if (sameAttachmentList(buildAttachmentListFromContent(editorContent.value, value), value)) {
return;
}
const nextContent = await convertContentForEditor(props.modelValue?.content);
+28
View File
@@ -0,0 +1,28 @@
import type { UploadResult as EditorUploadResult } from "timi-web/editor";
/** 文章编辑器支持的正文类型。 */
export type ArticleContentType = "TEXT" | "MARKDOWN" | "TIP_TAP";
/** 文章编辑器需要的附件最小结构,允许业务对象携带其他字段。 */
export type Attachment = {
[key: string]: unknown;
id?: string;
tempFileId?: string;
name?: string;
url?: string;
metadata?: Record<string, unknown>;
accessKey?: {
value?: string;
};
};
/** 文章编辑器需要的文章最小结构,业务对象的其他字段会原样保留。 */
export type Article = {
[key: string]: unknown;
contentType?: string;
content?: string;
attachmentList?: Attachment[];
};
/** 媒体上传结果。 */
export type UploadResult = EditorUploadResult;