vditor 4.0.2-alpha.7 → 4.0.2-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -5
- package/dist/index.css +35 -29
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6484 -5444
- package/dist/index.min.js +1 -1
- package/dist/index.mjs +17599 -0
- package/dist/js/graphviz/LICENSE.expat +21 -0
- package/dist/js/graphviz/LICENSE.graphviz +277 -0
- package/dist/js/graphviz/LICENSE.viz +21 -0
- package/dist/js/graphviz/README.md +10 -0
- package/dist/js/graphviz/viz.js +6 -330
- package/dist/js/lute/lute.min.js +3 -3
- package/dist/method.d.ts +2 -2
- package/dist/method.js +2017 -1903
- package/dist/method.min.js +1 -1
- package/dist/method.mjs +3243 -0
- package/dist/ts/constants.d.ts +2 -0
- package/dist/ts/ir/index.d.ts +1 -0
- package/dist/ts/markdown/mathRender.d.ts +24 -1
- package/dist/ts/markdown/outlineRender.d.ts +2 -1
- package/dist/ts/markdown/plantumlRender.d.ts +1 -1
- package/dist/ts/outline/index.d.ts +2 -0
- package/dist/ts/sv/index.d.ts +1 -0
- package/dist/ts/tip/index.d.ts +6 -1
- package/dist/ts/undo/index.d.ts +1 -0
- package/dist/ts/upload/linkToImg.d.ts +1 -0
- package/dist/ts/util/addScript.d.ts +1 -2
- package/dist/ts/util/compatibility.d.ts +1 -1
- package/dist/ts/util/expandHTMLTags.d.ts +1 -0
- package/dist/ts/util/imageDrag.d.ts +1 -0
- package/dist/ts/util/notifyConfigChange.d.ts +1 -0
- package/dist/ts/util/pairedSymbols.d.ts +5 -0
- package/dist/ts/util/pasteIntoTable.d.ts +6 -0
- package/dist/ts/util/pasteLink.d.ts +1 -0
- package/dist/ts/util/processCode.d.ts +1 -0
- package/dist/ts/util/tableWheel.d.ts +1 -0
- package/dist/ts/util/taskNavigation.d.ts +1 -0
- package/dist/ts/wysiwyg/index.d.ts +1 -0
- package/dist/types/index.d.ts +56 -1
- package/package.json +3 -2
- package/src/assets/less/_hint.less +6 -0
- package/src/assets/less/_reset.less +29 -0
- package/src/assets/less/_tooltipped.less +2 -2
- package/src/assets/less/_wysiwyg.less +4 -0
- package/src/index.ts +28 -6
- package/src/ts/constants.ts +4 -0
- package/src/ts/ir/expandMarker.ts +3 -0
- package/src/ts/ir/index.ts +11 -1
- package/src/ts/ir/process.ts +3 -0
- package/src/ts/markdown/chartRender.ts +28 -2
- package/src/ts/markdown/graphvizRender.ts +51 -39
- package/src/ts/markdown/highlightRender.ts +6 -2
- package/src/ts/markdown/mathRender.ts +26 -38
- package/src/ts/markdown/mediaRender.ts +1 -1
- package/src/ts/markdown/mermaidRender.ts +4 -3
- package/src/ts/markdown/outlineRender.ts +44 -15
- package/src/ts/markdown/plantumlRender.ts +24 -5
- package/src/ts/markdown/previewRender.ts +5 -1
- package/src/ts/markdown/setLute.ts +1 -0
- package/src/ts/outline/index.ts +72 -0
- package/src/ts/preview/index.ts +1 -1
- package/src/ts/sv/index.ts +1 -0
- package/src/ts/sv/process.ts +3 -0
- package/src/ts/tip/index.ts +25 -9
- package/src/ts/toolbar/CodeTheme.ts +2 -0
- package/src/ts/toolbar/ContentTheme.ts +2 -0
- package/src/ts/toolbar/EditMode.ts +4 -0
- package/src/ts/toolbar/Headings.ts +4 -6
- package/src/ts/toolbar/Outline.ts +2 -0
- package/src/ts/toolbar/Record.ts +5 -0
- package/src/ts/ui/initUI.ts +9 -1
- package/src/ts/ui/setPreviewMode.ts +2 -0
- package/src/ts/undo/index.ts +14 -4
- package/src/ts/upload/index.ts +1 -1
- package/src/ts/upload/linkToImg.ts +91 -0
- package/src/ts/util/Options.ts +2 -0
- package/src/ts/util/addScript.ts +15 -24
- package/src/ts/util/compatibility.ts +2 -2
- package/src/ts/util/editorCommonEvent.ts +47 -4
- package/src/ts/util/expandHTMLTags.ts +36 -0
- package/src/ts/util/fixBrowserBehavior.ts +29 -61
- package/src/ts/util/imageDrag.ts +108 -0
- package/src/ts/util/notifyConfigChange.ts +16 -0
- package/src/ts/util/pairedSymbols.ts +109 -0
- package/src/ts/util/pasteIntoTable.ts +136 -0
- package/src/ts/util/pasteLink.ts +51 -0
- package/src/ts/util/processCode.ts +33 -8
- package/src/ts/util/selection.ts +11 -2
- package/src/ts/util/tableWheel.ts +31 -0
- package/src/ts/util/taskNavigation.ts +113 -0
- package/src/ts/util/toc.ts +9 -3
- package/src/ts/wysiwyg/afterRenderEvent.ts +3 -0
- package/src/ts/wysiwyg/highlightToolbarWYSIWYG.ts +5 -4
- package/src/ts/wysiwyg/index.ts +17 -6
- package/src/ts/wysiwyg/setHeading.ts +4 -0
- package/dist/js/graphviz/full.render.js +0 -90
|
@@ -4,9 +4,14 @@ import {chartRenderAdapter} from "./adapterRender";
|
|
|
4
4
|
import {looseJsonParse} from "../util/function";
|
|
5
5
|
|
|
6
6
|
declare const echarts: {
|
|
7
|
-
init(element: HTMLElement, theme?: string): IEChart;
|
|
7
|
+
init(element: HTMLElement, theme?: string): IEChart & {dispose(): void};
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
+
const charts = new WeakMap<HTMLElement, {
|
|
11
|
+
instance: IEChart & {dispose(): void},
|
|
12
|
+
observer?: ResizeObserver,
|
|
13
|
+
}>();
|
|
14
|
+
|
|
10
15
|
export const chartRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN, theme: string) => {
|
|
11
16
|
const echartsElements = chartRenderAdapter.getElements(element);
|
|
12
17
|
if (echartsElements.length > 0) {
|
|
@@ -26,7 +31,28 @@ export const chartRender = (element: (HTMLElement | Document) = document, cdn =
|
|
|
26
31
|
return;
|
|
27
32
|
}
|
|
28
33
|
const option = await looseJsonParse(text);
|
|
29
|
-
|
|
34
|
+
const previous = charts.get(e);
|
|
35
|
+
previous?.observer?.disconnect();
|
|
36
|
+
previous?.instance.dispose();
|
|
37
|
+
const instance = echarts.init(e, theme === "dark" ? "dark" : undefined);
|
|
38
|
+
instance.setOption(option);
|
|
39
|
+
const state: {instance: typeof instance, observer?: ResizeObserver} = {instance};
|
|
40
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
41
|
+
state.observer = new ResizeObserver(() => {
|
|
42
|
+
if (!e.isConnected) {
|
|
43
|
+
state.observer.disconnect();
|
|
44
|
+
instance.dispose();
|
|
45
|
+
charts.delete(e);
|
|
46
|
+
e.removeAttribute("data-processed");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (e.clientWidth > 0 && e.clientHeight > 0) {
|
|
50
|
+
instance.resize();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
state.observer.observe(e);
|
|
54
|
+
}
|
|
55
|
+
charts.set(e, state);
|
|
30
56
|
e.setAttribute("data-processed", "true");
|
|
31
57
|
} catch (error) {
|
|
32
58
|
e.className = "vditor-reset--error";
|
|
@@ -1,49 +1,61 @@
|
|
|
1
1
|
import {Constants} from "../constants";
|
|
2
|
-
import {addScript} from "../util/addScript";
|
|
3
2
|
import {graphvizRenderAdapter} from "./adapterRender";
|
|
4
3
|
|
|
5
|
-
declare class Viz {
|
|
6
|
-
public renderSVGElement: (code: string) => Promise<any>;
|
|
7
|
-
|
|
8
|
-
constructor({ }: { worker: Worker });
|
|
9
|
-
}
|
|
10
|
-
|
|
11
4
|
export const graphvizRender = (element: HTMLElement, cdn = Constants.CDN) => {
|
|
12
5
|
const graphvizElements = graphvizRenderAdapter.getElements(element);
|
|
6
|
+
graphvizElements.forEach((e: HTMLDivElement) => {
|
|
7
|
+
if (e.parentElement.classList.contains("vditor-wysiwyg__pre") ||
|
|
8
|
+
e.parentElement.classList.contains("vditor-ir__marker--pre")) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const code = graphvizRenderAdapter.getCode(e);
|
|
12
|
+
if (e.getAttribute("data-processed") === "true" || code.trim() === "") {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (e.parentElement.classList.contains("vditor-wysiwyg__pre") ||
|
|
21
|
-
e.parentElement.classList.contains("vditor-ir__marker--pre")) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (e.getAttribute("data-processed") === "true" || code.trim() === "") {
|
|
26
|
-
return;
|
|
16
|
+
let worker: Worker;
|
|
17
|
+
let blobUrl: string;
|
|
18
|
+
const cleanup = () => {
|
|
19
|
+
worker?.terminate();
|
|
20
|
+
if (blobUrl) {
|
|
21
|
+
URL.revokeObjectURL(blobUrl);
|
|
27
22
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
23
|
+
};
|
|
24
|
+
const fail = (error: string) => {
|
|
25
|
+
e.textContent = `graphviz render error: ${error}`;
|
|
26
|
+
e.className = "vditor-reset--error";
|
|
27
|
+
cleanup();
|
|
28
|
+
};
|
|
29
|
+
try {
|
|
30
|
+
const scriptUrl = new URL(`${cdn}/dist/js/graphviz/viz.js`, document.baseURI).href;
|
|
31
|
+
const blob = new Blob([`
|
|
32
|
+
importScripts(${JSON.stringify(scriptUrl)});
|
|
33
|
+
const renderer = Viz.instance();
|
|
34
|
+
self.onmessage = event => {
|
|
35
|
+
renderer.then(viz => viz.renderString(event.data, {format: "svg"})).then(
|
|
36
|
+
svg => self.postMessage({svg}),
|
|
37
|
+
error => self.postMessage({error: String(error)})
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
`], {type: "application/javascript"});
|
|
41
|
+
blobUrl = URL.createObjectURL(blob);
|
|
42
|
+
worker = new Worker(blobUrl);
|
|
43
|
+
worker.onmessage = (event: MessageEvent<{svg?: string, error?: string}>) => {
|
|
44
|
+
if (event.data.error) {
|
|
45
|
+
fail(event.data.error);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
e.innerHTML = event.data.svg;
|
|
49
|
+
cleanup();
|
|
50
|
+
};
|
|
51
|
+
worker.onerror = event => {
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
fail(event.message);
|
|
54
|
+
};
|
|
46
55
|
e.setAttribute("data-processed", "true");
|
|
47
|
-
|
|
56
|
+
worker.postMessage(code);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
fail(String(error));
|
|
59
|
+
}
|
|
48
60
|
});
|
|
49
61
|
};
|
|
@@ -47,6 +47,8 @@ export const highlightRender = (hljsOption?: IHljs, element: HTMLElement | Docum
|
|
|
47
47
|
if (!canRenderEditable && hljsOption.defaultLang !== "" && block.className.indexOf("language-") === -1) {
|
|
48
48
|
block.classList.add("language-" + hljsOption.defaultLang);
|
|
49
49
|
}
|
|
50
|
+
block.querySelectorAll(".vditor-linenumber__temp, .vditor-linenumber__rows").forEach(item => item.remove());
|
|
51
|
+
block.classList.remove("vditor-linenumber");
|
|
50
52
|
let language = Array.from(block.classList).find(name => name.startsWith("language-"))?.slice(9) ||
|
|
51
53
|
hljsOption.defaultLang;
|
|
52
54
|
if (!window.hljs.getLanguage(language)) {
|
|
@@ -84,7 +86,9 @@ export const highlightRender = (hljsOption?: IHljs, element: HTMLElement | Docum
|
|
|
84
86
|
}
|
|
85
87
|
let lineNumberHTML = "";
|
|
86
88
|
const lineList = block.textContent.split(/\r\n|\r|\n/g);
|
|
87
|
-
lineList.
|
|
89
|
+
if (lineList.length > 1 && lineList[lineList.length - 1] === "") {
|
|
90
|
+
lineList.pop();
|
|
91
|
+
}
|
|
88
92
|
lineList.map((line) => {
|
|
89
93
|
let lineHeight = "";
|
|
90
94
|
if (isSoftWrap) {
|
|
@@ -94,7 +98,7 @@ export const highlightRender = (hljsOption?: IHljs, element: HTMLElement | Docum
|
|
|
94
98
|
lineNumberHTML += `<span${lineHeight}></span>`;
|
|
95
99
|
});
|
|
96
100
|
|
|
97
|
-
linenNumberTemp.
|
|
101
|
+
linenNumberTemp.remove();
|
|
98
102
|
lineNumberHTML = `<span class="vditor-linenumber__rows">${lineNumberHTML}</span>`;
|
|
99
103
|
block.insertAdjacentHTML("beforeend", lineNumberHTML);
|
|
100
104
|
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Constants} from "../constants";
|
|
2
|
-
import {addScript
|
|
2
|
+
import {addScript} from "../util/addScript";
|
|
3
3
|
import {addStyle} from "../util/addStyle";
|
|
4
4
|
import {code160to32} from "../util/code160to32";
|
|
5
5
|
import {mathRenderAdapter} from "./adapterRender";
|
|
@@ -14,7 +14,17 @@ declare const katex: {
|
|
|
14
14
|
|
|
15
15
|
declare global {
|
|
16
16
|
interface Window {
|
|
17
|
-
MathJax:
|
|
17
|
+
MathJax: {
|
|
18
|
+
loader?: {paths: {mathjax: string}};
|
|
19
|
+
startup: {
|
|
20
|
+
typeset?: boolean;
|
|
21
|
+
promise?: Promise<void>;
|
|
22
|
+
document?: {clear(): void; updateDocument(): void};
|
|
23
|
+
};
|
|
24
|
+
tex?: {macros: object};
|
|
25
|
+
getMetricsFor?(element: Element): {display?: boolean};
|
|
26
|
+
tex2svgPromise?(math: string, options: {display?: boolean}): Promise<Element>;
|
|
27
|
+
};
|
|
18
28
|
}
|
|
19
29
|
}
|
|
20
30
|
|
|
@@ -77,18 +87,6 @@ export const mathRender = (element: (HTMLElement | Document) = document, options
|
|
|
77
87
|
});
|
|
78
88
|
});
|
|
79
89
|
} else if (options.math.engine === "MathJax") {
|
|
80
|
-
const chainAsync = (fns: any) => {
|
|
81
|
-
if (fns.length === 0) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
let curr = 0;
|
|
85
|
-
const last = fns[fns.length - 1];
|
|
86
|
-
const next = () => {
|
|
87
|
-
const fn = fns[curr++];
|
|
88
|
-
fn === last ? fn() : fn(next);
|
|
89
|
-
};
|
|
90
|
-
next();
|
|
91
|
-
};
|
|
92
90
|
if (!window.MathJax) {
|
|
93
91
|
window.MathJax = {
|
|
94
92
|
loader: {
|
|
@@ -104,13 +102,11 @@ export const mathRender = (element: (HTMLElement | Document) = document, options
|
|
|
104
102
|
// https://github.com/Vanessa219/vditor/issues/1453
|
|
105
103
|
Object.assign(window.MathJax, options.math.mathJaxOptions);
|
|
106
104
|
}
|
|
107
|
-
|
|
108
|
-
addScriptSync(`${options.cdn}/dist/js/mathjax/tex-svg-full.js`, "protyleMathJaxScript");
|
|
109
|
-
const renderMath = (mathElement: Element, next?: () => void) => {
|
|
105
|
+
const renderMath = (mathElement: Element) => {
|
|
110
106
|
const math = code160to32(mathElement.textContent).trim();
|
|
111
107
|
const mathOptions = window.MathJax.getMetricsFor(mathElement);
|
|
112
108
|
mathOptions.display = mathElement.tagName === "DIV";
|
|
113
|
-
window.MathJax.tex2svgPromise(math, mathOptions).then((node: Element) => {
|
|
109
|
+
return window.MathJax.tex2svgPromise(math, mathOptions).then((node: Element) => {
|
|
114
110
|
mathElement.innerHTML = "";
|
|
115
111
|
mathElement.setAttribute("data-math", math);
|
|
116
112
|
mathElement.append(node);
|
|
@@ -121,28 +117,20 @@ export const mathRender = (element: (HTMLElement | Document) = document, options
|
|
|
121
117
|
mathElement.innerHTML = errorTextElement.textContent.trim();
|
|
122
118
|
mathElement.className = "vditor-reset--error";
|
|
123
119
|
}
|
|
124
|
-
if (next) {
|
|
125
|
-
next();
|
|
126
|
-
}
|
|
127
120
|
});
|
|
128
121
|
};
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
!mathElement.parentElement.classList.contains("vditor-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
} else {
|
|
140
|
-
renderMath(mathElement, next);
|
|
141
|
-
}
|
|
142
|
-
});
|
|
122
|
+
addScript(`${options.cdn}/dist/js/mathjax/tex-svg-full.js`, "protyleMathJaxScript")
|
|
123
|
+
.then(() => window.MathJax.startup.promise).then(() => {
|
|
124
|
+
let rendering = Promise.resolve();
|
|
125
|
+
for (let i = 0; i < mathElements.length; i++) {
|
|
126
|
+
const mathElement = mathElements[i];
|
|
127
|
+
if (!mathElement.parentElement.classList.contains("vditor-wysiwyg__pre") &&
|
|
128
|
+
!mathElement.parentElement.classList.contains("vditor-ir__marker--pre") &&
|
|
129
|
+
!mathElement.getAttribute("data-math") && code160to32(mathElement.textContent).trim()) {
|
|
130
|
+
rendering = rendering.then(() => renderMath(mathElement));
|
|
131
|
+
}
|
|
143
132
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
});
|
|
133
|
+
return rendering;
|
|
134
|
+
});
|
|
147
135
|
}
|
|
148
136
|
};
|
|
@@ -81,7 +81,7 @@ export const mediaRender = (element: HTMLElement) => {
|
|
|
81
81
|
}
|
|
82
82
|
if (url.match(/^.+.(mp4|m4v|ogg|ogv|webm)$/)) {
|
|
83
83
|
replaceMedia(aElement, "video", url);
|
|
84
|
-
} else if (url.match(
|
|
84
|
+
} else if (url.match(/^.+\.(mp3|wav|flac|aac)(?:[?#].*)?$/i)) {
|
|
85
85
|
replaceMedia(aElement, "audio", url);
|
|
86
86
|
} else {
|
|
87
87
|
iframeRender(aElement, url);
|
|
@@ -4,8 +4,8 @@ import {mermaidRenderAdapter} from "./adapterRender";
|
|
|
4
4
|
import {genUUID} from "../util/function";
|
|
5
5
|
|
|
6
6
|
declare const mermaid: {
|
|
7
|
-
initialize(options:
|
|
8
|
-
render(id: string, text: string): {
|
|
7
|
+
initialize(options: Record<string, unknown>): void,
|
|
8
|
+
render(id: string, text: string): Promise<{svg: string, bindFunctions?: (element: Element) => void}>
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
export const mermaidRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN, theme: string) => {
|
|
@@ -14,7 +14,7 @@ export const mermaidRender = (element: (HTMLElement | Document) = document, cdn
|
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
16
|
addScript(`${cdn}/dist/js/mermaid/mermaid.min.js?v=11.17.2`, "vditorMermaidScript").then(() => {
|
|
17
|
-
const config:
|
|
17
|
+
const config: Record<string, unknown> = {
|
|
18
18
|
securityLevel: "loose", // 升级后无 https://github.com/siyuan-note/siyuan/issues/3587,可使用该选项
|
|
19
19
|
altFontFamily: "sans-serif",
|
|
20
20
|
fontFamily: "sans-serif",
|
|
@@ -48,6 +48,7 @@ export const mermaidRender = (element: (HTMLElement | Document) = document, cdn
|
|
|
48
48
|
try {
|
|
49
49
|
const mermaidData = await mermaid.render(id, item.textContent);
|
|
50
50
|
item.innerHTML = mermaidData.svg;
|
|
51
|
+
mermaidData.bindFunctions?.(item);
|
|
51
52
|
} catch (e) {
|
|
52
53
|
const errorElement = document.querySelector("#" + id);
|
|
53
54
|
item.innerHTML = `${errorElement.outerHTML}<br>
|
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
import {hasClosestByHeadings} from "../util/hasClosestByHeadings";
|
|
2
2
|
import {mathRender} from "./mathRender";
|
|
3
3
|
|
|
4
|
-
export const
|
|
5
|
-
|
|
6
|
-
const ids: string[] = [];
|
|
4
|
+
export const getOutlineHeadings = (contentElement: HTMLElement, assignIDs = false) => {
|
|
5
|
+
const headings: HTMLElement[] = [];
|
|
7
6
|
Array.from(contentElement.children).forEach((item: HTMLElement, index: number) => {
|
|
8
|
-
if (hasClosestByHeadings(item)) {
|
|
9
|
-
|
|
10
|
-
const lastIndex = item.id.lastIndexOf("_");
|
|
11
|
-
item.id = item.id.substring(0, lastIndex === -1 ? undefined : lastIndex) + "_" + index;
|
|
12
|
-
}
|
|
13
|
-
ids.push(item.id);
|
|
14
|
-
tocHTML += item.outerHTML.replace("<wbr>", "");
|
|
7
|
+
if (!hasClosestByHeadings(item)) {
|
|
8
|
+
return;
|
|
15
9
|
}
|
|
10
|
+
if (assignIDs) {
|
|
11
|
+
const lastIndex = item.id.lastIndexOf("_");
|
|
12
|
+
item.id = item.id.substring(0, lastIndex === -1 ? undefined : lastIndex) + "_" + index;
|
|
13
|
+
}
|
|
14
|
+
headings.push(item);
|
|
15
|
+
});
|
|
16
|
+
return headings;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const outlineRender = (contentElement: HTMLElement, targetElement: Element,
|
|
20
|
+
vditorOrClick?: IVditor | ((heading: HTMLElement, event: Event) => void)) => {
|
|
21
|
+
const vditor = typeof vditorOrClick === "function" ? undefined : vditorOrClick;
|
|
22
|
+
const click = typeof vditorOrClick === "function" ? vditorOrClick : undefined;
|
|
23
|
+
let tocHTML = "";
|
|
24
|
+
const ids: string[] = [];
|
|
25
|
+
getOutlineHeadings(contentElement, !!vditor).forEach(item => {
|
|
26
|
+
ids.push(item.id);
|
|
27
|
+
tocHTML += item.outerHTML.replace("<wbr>", "");
|
|
16
28
|
});
|
|
17
29
|
if (tocHTML === "") {
|
|
18
30
|
targetElement.innerHTML = "";
|
|
@@ -77,17 +89,34 @@ export const outlineRender = (contentElement: HTMLElement, targetElement: Elemen
|
|
|
77
89
|
} else if (target.getAttribute("data-target-id")) {
|
|
78
90
|
event.preventDefault();
|
|
79
91
|
event.stopPropagation();
|
|
80
|
-
const idElement =
|
|
92
|
+
const idElement = Array.from(contentElement.children).find(item =>
|
|
93
|
+
item.id === target.getAttribute("data-target-id")) as HTMLElement;
|
|
81
94
|
if (!idElement) {
|
|
82
95
|
return;
|
|
83
96
|
}
|
|
97
|
+
if (click) {
|
|
98
|
+
click(idElement, event);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
84
101
|
if (vditor) {
|
|
85
102
|
if (vditor.options.height === "auto") {
|
|
86
|
-
|
|
87
|
-
if (
|
|
88
|
-
|
|
103
|
+
idElement.scrollIntoView({block: "start", inline: "nearest"});
|
|
104
|
+
if (vditor.options.toolbarConfig.pin) {
|
|
105
|
+
let parent = idElement.parentElement;
|
|
106
|
+
while (parent && parent !== document.documentElement) {
|
|
107
|
+
if (/auto|scroll/.test(getComputedStyle(parent).overflowY) &&
|
|
108
|
+
parent.scrollHeight > parent.clientHeight) {
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
parent = parent.parentElement;
|
|
112
|
+
}
|
|
113
|
+
const offset = -vditor.toolbar.element.offsetHeight;
|
|
114
|
+
if (parent && parent !== document.documentElement) {
|
|
115
|
+
parent.scrollTop += offset;
|
|
116
|
+
} else {
|
|
117
|
+
window.scrollBy(0, offset);
|
|
118
|
+
}
|
|
89
119
|
}
|
|
90
|
-
window.scrollTo(window.scrollX, windowScrollY);
|
|
91
120
|
} else {
|
|
92
121
|
if (vditor.element.offsetTop < window.scrollY) {
|
|
93
122
|
window.scrollTo(window.scrollX, vditor.element.offsetTop);
|
|
@@ -6,12 +6,15 @@ declare const plantumlEncoder: {
|
|
|
6
6
|
encode(options: string): string,
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
export const plantumlRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN
|
|
9
|
+
export const plantumlRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN,
|
|
10
|
+
plantuml?: IPlantumlOptions) => {
|
|
10
11
|
const plantumlElements = plantumlRenderAdapter.getElements(element);
|
|
11
12
|
if (plantumlElements.length === 0) {
|
|
12
13
|
return;
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
+
const renderer = plantuml?.renderer;
|
|
16
|
+
const server = plantuml?.server || Constants.PLANTUML_OPTIONS.server;
|
|
17
|
+
const renderElements = () => {
|
|
15
18
|
plantumlElements.forEach((e: HTMLDivElement) => {
|
|
16
19
|
if (e.parentElement.classList.contains("vditor-wysiwyg__pre") ||
|
|
17
20
|
e.parentElement.classList.contains("vditor-ir__marker--pre")) {
|
|
@@ -21,12 +24,28 @@ export const plantumlRender = (element: (HTMLElement | Document) = document, cdn
|
|
|
21
24
|
if (!text) {
|
|
22
25
|
return;
|
|
23
26
|
}
|
|
27
|
+
if (renderer) {
|
|
28
|
+
Promise.resolve().then(() => renderer(text, e)).catch((error) => {
|
|
29
|
+
e.className = "vditor-reset--error";
|
|
30
|
+
e.textContent = `plantuml render error: ${error}`;
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
24
34
|
try {
|
|
25
|
-
|
|
35
|
+
const image = document.createElement("object");
|
|
36
|
+
image.type = "image/svg+xml";
|
|
37
|
+
image.data = `${server}~1${plantumlEncoder.encode(text)}`;
|
|
38
|
+
e.replaceChildren(image);
|
|
26
39
|
} catch (error) {
|
|
27
40
|
e.className = "vditor-reset--error";
|
|
28
|
-
e.
|
|
41
|
+
e.textContent = `plantuml render error: ${error}`;
|
|
29
42
|
}
|
|
30
43
|
});
|
|
31
|
-
}
|
|
44
|
+
};
|
|
45
|
+
if (renderer) {
|
|
46
|
+
// 自定义渲染无需加载 plantuml-encoder
|
|
47
|
+
renderElements();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
addScript(`${cdn}/dist/js/plantuml/plantuml-encoder.min.js`, "vditorPlantumlScript").then(renderElements);
|
|
32
51
|
};
|
|
@@ -3,6 +3,7 @@ import {setContentTheme} from "../ui/setContentTheme";
|
|
|
3
3
|
import {addScript} from "../util/addScript";
|
|
4
4
|
import {hasClosestByClassName, hasClosestByMatchTag} from "../util/hasClosest";
|
|
5
5
|
import {merge} from "../util/merge";
|
|
6
|
+
import {bindTableWheel} from "../util/tableWheel";
|
|
6
7
|
import {abcRender} from "./abcRender";
|
|
7
8
|
import {anchorRender} from "./anchorRender";
|
|
8
9
|
import {chartRender} from "./chartRender";
|
|
@@ -35,6 +36,7 @@ const mergeOptions = (options?: IPreviewOptions) => {
|
|
|
35
36
|
markdown: Constants.MARKDOWN_OPTIONS,
|
|
36
37
|
math: Constants.MATH_OPTIONS,
|
|
37
38
|
mode: "light",
|
|
39
|
+
plantuml: Constants.PLANTUML_OPTIONS,
|
|
38
40
|
speech: {
|
|
39
41
|
enable: false,
|
|
40
42
|
},
|
|
@@ -70,6 +72,7 @@ export const md2html = (mdText: string, options?: IPreviewOptions) => {
|
|
|
70
72
|
fixTermTypo: mergedOptions.markdown.fixTermTypo,
|
|
71
73
|
footnotes: mergedOptions.markdown.footnotes,
|
|
72
74
|
headingAnchor: mergedOptions.anchor !== 0,
|
|
75
|
+
imgPathAllowSpace: mergedOptions.markdown.imgPathAllowSpace,
|
|
73
76
|
inlineMathDigit: mergedOptions.math.inlineDigit,
|
|
74
77
|
lazyLoadImage: mergedOptions.lazyLoadImage,
|
|
75
78
|
linkBase: mergedOptions.markdown.linkBase,
|
|
@@ -103,6 +106,7 @@ export const previewRender = async (previewElement: HTMLDivElement, markdown: st
|
|
|
103
106
|
}
|
|
104
107
|
previewElement.innerHTML = html;
|
|
105
108
|
previewElement.classList.add("vditor-reset");
|
|
109
|
+
bindTableWheel(previewElement);
|
|
106
110
|
|
|
107
111
|
if (!mergedOptions.i18n) {
|
|
108
112
|
if (!["de_DE", "en_US", "es_ES", "fr_FR", "ja_JP", "ko_KR", "pt_BR", "ru_RU", "sv_SE", "vi_VN", "zh_CN", "zh_TW"].includes(mergedOptions.lang)) {
|
|
@@ -145,7 +149,7 @@ export const previewRender = async (previewElement: HTMLDivElement, markdown: st
|
|
|
145
149
|
wavedromRender(previewElement, mergedOptions.cdn);
|
|
146
150
|
chartRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
|
|
147
151
|
mindmapRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
|
|
148
|
-
plantumlRender(previewElement, mergedOptions.cdn);
|
|
152
|
+
plantumlRender(previewElement, mergedOptions.cdn, mergedOptions.plantuml);
|
|
149
153
|
abcRender(previewElement, mergedOptions.cdn);
|
|
150
154
|
if (mergedOptions.render.media.enable) {
|
|
151
155
|
mediaRender(previewElement);
|
|
@@ -26,6 +26,7 @@ export const setLute = (options: ILuteOptions) => {
|
|
|
26
26
|
}
|
|
27
27
|
lute.SetSup(options.sup);
|
|
28
28
|
lute.SetSub(options.sub);
|
|
29
|
+
lute.SetImgPathAllowSpace(options.imgPathAllowSpace);
|
|
29
30
|
// 编辑区的高亮是临时展示,传入 Lute 的代码正文需保留纯文本和光标标记。
|
|
30
31
|
const conversions = ["SpinVditorDOM", "SpinVditorIRDOM", "VditorDOM2Md", "VditorIRDOM2Md",
|
|
31
32
|
"VditorDOM2HTML", "VditorIRDOM2HTML"] as const;
|
package/src/ts/outline/index.ts
CHANGED
|
@@ -2,15 +2,84 @@ import {Constants} from "../constants";
|
|
|
2
2
|
import {outlineRender} from "../markdown/outlineRender";
|
|
3
3
|
import {setPadding} from "../ui/initUI";
|
|
4
4
|
import {setSelectionFocus} from "../util/selection";
|
|
5
|
+
import {notifyConfigChange} from "../util/notifyConfigChange";
|
|
5
6
|
|
|
6
7
|
export class Outline {
|
|
7
8
|
public element: HTMLElement;
|
|
9
|
+
public resizeElement: HTMLElement;
|
|
8
10
|
|
|
9
11
|
constructor(outlineLabel: string) {
|
|
10
12
|
this.element = document.createElement("div");
|
|
11
13
|
this.element.className = "vditor-outline";
|
|
12
14
|
this.element.innerHTML = `<div class="vditor-outline__title">${outlineLabel}</div>
|
|
13
15
|
<div class="vditor-outline__content"></div>`;
|
|
16
|
+
this.resizeElement = document.createElement("div");
|
|
17
|
+
this.resizeElement.className = "vditor-outline__resize";
|
|
18
|
+
this.resizeElement.setAttribute("role", "separator");
|
|
19
|
+
this.resizeElement.setAttribute("aria-orientation", "vertical");
|
|
20
|
+
this.resizeElement.setAttribute("aria-label", outlineLabel);
|
|
21
|
+
this.resizeElement.tabIndex = 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public bindResize(vditor: IVditor) {
|
|
25
|
+
let pointerId: number;
|
|
26
|
+
let startX: number;
|
|
27
|
+
let startWidth: number;
|
|
28
|
+
const direction = vditor.options.outline.position === "left" ? 1 : -1;
|
|
29
|
+
const bounds = () => {
|
|
30
|
+
const max = Math.max(0, vditor.element.clientWidth - 140);
|
|
31
|
+
return {min: Math.min(120, max), max};
|
|
32
|
+
};
|
|
33
|
+
const resize = (width: number) => {
|
|
34
|
+
const {min, max} = bounds();
|
|
35
|
+
width = Math.round(Math.max(min, Math.min(max, width)));
|
|
36
|
+
this.element.style.width = `${width}px`;
|
|
37
|
+
vditor.options.outline.width = width;
|
|
38
|
+
this.resizeElement.setAttribute("aria-valuemin", String(min));
|
|
39
|
+
this.resizeElement.setAttribute("aria-valuemax", String(max));
|
|
40
|
+
this.resizeElement.setAttribute("aria-valuenow", String(width));
|
|
41
|
+
setPadding(vditor);
|
|
42
|
+
};
|
|
43
|
+
this.resizeElement.addEventListener("pointerdown", (event: PointerEvent) => {
|
|
44
|
+
if (event.button !== 0) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
pointerId = event.pointerId;
|
|
49
|
+
startX = event.clientX;
|
|
50
|
+
startWidth = this.element.clientWidth;
|
|
51
|
+
this.resizeElement.setPointerCapture(pointerId);
|
|
52
|
+
});
|
|
53
|
+
this.resizeElement.addEventListener("pointermove", (event: PointerEvent) => {
|
|
54
|
+
if (event.pointerId === pointerId) {
|
|
55
|
+
resize(startWidth + direction * (event.clientX - startX));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const finish = (event: PointerEvent) => {
|
|
59
|
+
if (event.pointerId !== pointerId) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
pointerId = undefined;
|
|
63
|
+
if (this.element.isConnected && this.element.clientWidth !== startWidth) {
|
|
64
|
+
notifyConfigChange(vditor);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
this.resizeElement.addEventListener("pointerup", finish);
|
|
68
|
+
this.resizeElement.addEventListener("pointercancel", finish);
|
|
69
|
+
this.resizeElement.addEventListener("lostpointercapture", finish);
|
|
70
|
+
this.resizeElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
|
71
|
+
if (!["ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key)) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
event.preventDefault();
|
|
75
|
+
const {min, max} = bounds();
|
|
76
|
+
const step = (event.shiftKey ? 50 : 10) * direction * (event.key === "ArrowLeft" ? -1 : 1);
|
|
77
|
+
resize(event.key === "Home" ? min : event.key === "End" ? max : this.element.clientWidth + step);
|
|
78
|
+
notifyConfigChange(vditor);
|
|
79
|
+
});
|
|
80
|
+
if (Number.isFinite(vditor.options.outline.width)) {
|
|
81
|
+
resize(vditor.options.outline.width);
|
|
82
|
+
}
|
|
14
83
|
}
|
|
15
84
|
|
|
16
85
|
public render(vditor: IVditor) {
|
|
@@ -28,10 +97,13 @@ export class Outline {
|
|
|
28
97
|
const btnElement = vditor.toolbar.elements.outline?.firstElementChild;
|
|
29
98
|
if (show && window.innerWidth >= Constants.MOBILE_WIDTH) {
|
|
30
99
|
this.element.style.display = "block";
|
|
100
|
+
this.resizeElement.style.display = "block";
|
|
101
|
+
this.resizeElement.setAttribute("aria-valuenow", String(this.element.clientWidth));
|
|
31
102
|
this.render(vditor);
|
|
32
103
|
btnElement?.classList.add("vditor-menu--current");
|
|
33
104
|
} else {
|
|
34
105
|
this.element.style.display = "none";
|
|
106
|
+
this.resizeElement.style.display = "none";
|
|
35
107
|
btnElement?.classList.remove("vditor-menu--current");
|
|
36
108
|
}
|
|
37
109
|
if (focus && getSelection().rangeCount > 0) {
|
package/src/ts/preview/index.ts
CHANGED
|
@@ -236,7 +236,7 @@ export class Preview {
|
|
|
236
236
|
wavedromRender(vditor.preview.previewElement, vditor.options.cdn);
|
|
237
237
|
chartRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.theme);
|
|
238
238
|
mindmapRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.theme);
|
|
239
|
-
plantumlRender(vditor.preview.previewElement, vditor.options.cdn);
|
|
239
|
+
plantumlRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.plantuml);
|
|
240
240
|
abcRender(vditor.preview.previewElement, vditor.options.cdn);
|
|
241
241
|
if (vditor.options.preview.render.media.enable) {
|
|
242
242
|
mediaRender(vditor.preview.previewElement);
|
package/src/ts/sv/index.ts
CHANGED
package/src/ts/sv/process.ts
CHANGED
|
@@ -16,6 +16,7 @@ export const processAfterRender = (vditor: IVditor, options = {
|
|
|
16
16
|
enableHint: false,
|
|
17
17
|
enableInput: true,
|
|
18
18
|
}) => {
|
|
19
|
+
vditor.sv.pendingUndoStack = options.enableAddUndoStack && vditor.options.undoDelay !== 0;
|
|
19
20
|
if (options.enableAddUndoStack && vditor.options.undoDelay === 0) {
|
|
20
21
|
vditor.undo.addToUndoStack(vditor);
|
|
21
22
|
}
|
|
@@ -43,6 +44,8 @@ export const processAfterRender = (vditor: IVditor, options = {
|
|
|
43
44
|
|
|
44
45
|
clearTimeout(vditor.sv.processTimeoutId);
|
|
45
46
|
vditor.sv.processTimeoutId = window.setTimeout(() => {
|
|
47
|
+
vditor.sv.processTimeoutId = 0;
|
|
48
|
+
vditor.sv.pendingUndoStack = false;
|
|
46
49
|
if (options.enableAddUndoStack && vditor.options.undoDelay !== 0 && !vditor.sv.composingLock) {
|
|
47
50
|
vditor.undo.addToUndoStack(vditor);
|
|
48
51
|
}
|