refactor editor

This commit is contained in:
Timi
2026-08-25 17:35:19 +08:00
parent 0c5a4d4fd9
commit 6fa75b3045
44 changed files with 334 additions and 3608 deletions
+18 -1
View File
@@ -1,5 +1,23 @@
import Prism from "prismjs";
// 平台组件会直接编译 src/editor,不能依赖 timi-web 构建阶段的 Prism 插件注入语言。
import "prismjs/components/prism-apacheconf";
import "prismjs/components/prism-bash";
import "prismjs/components/prism-css";
import "prismjs/components/prism-ini";
import "prismjs/components/prism-java";
import "prismjs/components/prism-javascript";
import "prismjs/components/prism-json";
import "prismjs/components/prism-less";
import "prismjs/components/prism-markup";
import "prismjs/components/prism-markup-templating";
import "prismjs/components/prism-nginx";
import "prismjs/components/prism-php";
import "prismjs/components/prism-properties";
import "prismjs/components/prism-sql";
import "prismjs/components/prism-typescript";
import "prismjs/components/prism-yaml";
const prismLanguageAliasMap: Record<string, string> = {
html: "markup",
xml: "markup",
@@ -16,4 +34,3 @@ export function resolvePrismLanguage(language?: string | null) {
const nextLanguage = prismLanguageAliasMap[language] || language;
return Prism.languages[nextLanguage] ? nextLanguage : "";
}
-123
View File
@@ -1,123 +0,0 @@
export function enhancePrismCodeBlock(root: ParentNode) {
const codeList = Array.from(root.querySelectorAll("pre > code[class*='language-'], pre[class*='language-'] > code"));
for (const code of codeList) {
if (!(code instanceof HTMLElement)) {
continue;
}
const pre = code.parentElement;
if (!(pre instanceof HTMLElement)) {
continue;
}
syncPreLanguageClass(pre, code);
resetCodeBlock(code);
normalizeCodeContent(code);
const maxHeight = resolveCodeBlockMaxHeight(pre);
if (maxHeight && maxHeight !== "auto") {
pre.style.maxHeight = maxHeight;
} else {
pre.style.removeProperty("max-height");
}
const lineCount = buildLineCount(code.textContent || "");
const rows = buildLineNumberRows(lineCount);
const codes = document.createElement("span");
codes.className = "codes";
codes.innerHTML = code.innerHTML;
pre.classList.add("has-line-numbers");
code.replaceChildren(rows, codes);
bindCodeBlockToggle(pre, code, lineCount, maxHeight);
}
}
function syncPreLanguageClass(pre: HTMLElement, code: HTMLElement) {
const classList = Array.from(code.classList).filter((item) => item.startsWith("language-"));
if (!classList.length) {
return;
}
for (const className of classList) {
if (!pre.classList.contains(className)) {
pre.classList.add(className);
}
}
}
function resetCodeBlock(code: HTMLElement) {
code.parentElement?.classList.remove("has-line-numbers");
const codes = code.querySelector(":scope > .codes");
if (codes instanceof HTMLElement) {
code.innerHTML = codes.innerHTML;
}
}
function normalizeCodeContent(code: HTMLElement) {
const firstNode = code.firstChild;
if (firstNode instanceof Text && firstNode.data.startsWith("\n")) {
firstNode.data = firstNode.data.slice(1);
if (!firstNode.data) {
firstNode.remove();
}
}
const lastNode = code.lastChild;
if (lastNode instanceof Text && lastNode.data.endsWith("\n")) {
lastNode.data = lastNode.data.replace(/\n+$/, "");
if (!lastNode.data) {
lastNode.remove();
}
}
}
function resolveCodeBlockMaxHeight(pre: HTMLElement) {
const container = pre.closest("[data-max-height]");
if (!(container instanceof HTMLElement)) {
return "";
}
return container.dataset.maxHeight || "";
}
function buildLineCount(text: string) {
if (!text) {
return 1;
}
return Math.max(text.split("\n").length, 1);
}
function buildLineNumberRows(lineCount: number) {
const rows = document.createElement("span");
rows.className = "line-numbers-rows diselect";
for (let i = 0; i < lineCount; i += 1) {
const row = document.createElement("span");
row.textContent = `${i + 1}`;
rows.appendChild(row);
}
return rows;
}
function bindCodeBlockToggle(pre: HTMLElement, code: HTMLElement, lineCount: number, maxHeight: string) {
pre.classList.remove("expand");
pre.dataset.lineCount = `${lineCount}`;
pre.ondblclick = null;
if (!maxHeight || maxHeight === "auto" || lineCount < 18) {
return;
}
pre.ondblclick = () => {
const isExpand = pre.classList.contains("expand");
if (isExpand) {
pre.style.maxHeight = maxHeight;
pre.classList.remove("expand");
} else {
const lineHeight = Number.parseFloat(getComputedStyle(code).lineHeight || "22") || 22;
pre.style.maxHeight = `${Math.ceil(lineCount * lineHeight + 2)}px`;
pre.classList.add("expand");
}
window.getSelection()?.removeAllRanges();
};
}
-27
View File
@@ -1,27 +0,0 @@
import type { AppContext } from "vue";
type MessageType = "info" | "success" | "warning" | "error";
type MessageHandler = (content: string) => unknown | Promise<unknown>;
type MessageApi = {
[type in MessageType]?: MessageHandler;
};
export const showTDesignMessage = async (
appContext: AppContext | undefined,
type: MessageType,
content: string
) => {
const handler = (appContext?.config.globalProperties.$message as MessageApi | undefined)?.[type];
if (handler) {
await handler(content);
return;
}
if ("error" === type) {
console.error(content);
return;
}
console.warn(content);
};