add tiptap
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import Prism from "prismjs";
|
||||
|
||||
const prismLanguageAliasMap: Record<string, string> = {
|
||||
html: "markup",
|
||||
xml: "markup",
|
||||
js: "javascript",
|
||||
ts: "typescript",
|
||||
sh: "bash",
|
||||
vue: "markup"
|
||||
};
|
||||
|
||||
export function resolvePrismLanguage(language?: string | null) {
|
||||
if (!language) {
|
||||
return "";
|
||||
}
|
||||
const nextLanguage = prismLanguageAliasMap[language] || language;
|
||||
return Prism.languages[nextLanguage] ? nextLanguage : "";
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
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();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user