vditor 3.8.18 → 3.9.1

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.
@@ -0,0 +1,57 @@
1
+ import {Constants} from "../constants";
2
+ import {addScript} from "../util/addScript";
3
+ import {markmapRenderAdapter} from "./adapterRender";
4
+
5
+ declare const window: any;
6
+ const enabled: Record<string, boolean> = {};
7
+
8
+ const transform = (transformer: any,content: string)=>{
9
+ const result = transformer.transform(content);
10
+ const keys = Object.keys(result.features).filter((key) => !enabled[key]);
11
+ keys.forEach((key) => {
12
+ enabled[key] = true;
13
+ });
14
+ const { styles, scripts } = transformer.getAssets(keys);
15
+ const { markmap } = window;
16
+ if (styles) markmap.loadCSS(styles);
17
+ if (scripts) markmap.loadJS(scripts);
18
+ return result;
19
+ }
20
+
21
+ const init = (el: HTMLElement,code: string) => {
22
+ const { Transformer, Markmap, deriveOptions , globalCSS} = window.markmap;
23
+ const transformer = new Transformer();
24
+ el.innerHTML = '<svg style="width:100%"></svg>';
25
+ const svg = el.firstChild as SVGElement;
26
+ const mm = Markmap.create(svg, null);
27
+ const { root, frontmatter } = transform(transformer, code);
28
+ const markmapOptions = frontmatter?.markmap;
29
+ const frontmatterOptions = deriveOptions(markmapOptions);
30
+ mm.setData(root, frontmatterOptions);
31
+ mm.fit();
32
+ }
33
+
34
+
35
+ export const markmapRender = (element: HTMLElement, cdn = Constants.CDN, theme: string) => {
36
+ const markmapElements = markmapRenderAdapter.getElements(element);
37
+ if (markmapElements.length === 0) {
38
+ return;
39
+ }
40
+ addScript(`${cdn}/src/js/markmap/markmap.min.js`, "vditorMermaidScript").then(() => {
41
+ markmapElements.forEach((item) => {
42
+ const code = markmapRenderAdapter.getCode(item);
43
+ if (item.getAttribute("data-processed") === "true" || code.trim() === "") {
44
+ return;
45
+ }
46
+ const render = document.createElement("div")
47
+ render.className = "language-markmap"
48
+ item.parentNode.appendChild(render)
49
+ init(render,code)
50
+
51
+ if(item.parentNode.childNodes[0].nodeName == "CODE"){
52
+ item.parentNode.removeChild(item.parentNode.childNodes[0])
53
+ }
54
+ });
55
+ });
56
+
57
+ };
@@ -14,6 +14,7 @@ import {lazyLoadImageRender} from "./lazyLoadImageRender";
14
14
  import {mathRender} from "./mathRender";
15
15
  import {mediaRender} from "./mediaRender";
16
16
  import {mermaidRender} from "./mermaidRender";
17
+ import {markmapRender} from "../markdown/markmapRender";
17
18
  import {mindmapRender} from "./mindmapRender";
18
19
  import {plantumlRender} from "./plantumlRender";
19
20
  import {setLute} from "./setLute";
@@ -85,7 +86,7 @@ export const previewRender = async (previewElement: HTMLDivElement, markdown: st
85
86
  previewElement.classList.add("vditor-reset");
86
87
 
87
88
  if (!mergedOptions.i18n) {
88
- if (!["en_US", "ja_JP", "ko_KR", "ru_RU", "zh_CN", "zh_TW"].includes(mergedOptions.lang)) {
89
+ if (!["en_US", "fr_FR", "ja_JP", "ko_KR", "ru_RU", "sv_SE", "zh_CN", "zh_TW"].includes(mergedOptions.lang)) {
89
90
  throw new Error(
90
91
  "options.lang error, see https://ld246.com/article/1549638745630#options",
91
92
  );
@@ -118,6 +119,7 @@ export const previewRender = async (previewElement: HTMLDivElement, markdown: st
118
119
  math: mergedOptions.math,
119
120
  });
120
121
  mermaidRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
122
+ markmapRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
121
123
  flowchartRender(previewElement, mergedOptions.cdn);
122
124
  graphvizRender(previewElement, mergedOptions.cdn);
123
125
  chartRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
@@ -24,7 +24,7 @@ export class Outline {
24
24
  return html;
25
25
  }
26
26
 
27
- public toggle(vditor: IVditor, show = true) {
27
+ public toggle(vditor: IVditor, show = true, focus = true) {
28
28
  const btnElement = vditor.toolbar.elements.outline?.firstElementChild;
29
29
  if (show && window.innerWidth >= Constants.MOBILE_WIDTH) {
30
30
  this.element.style.display = "block";
@@ -34,12 +34,10 @@ export class Outline {
34
34
  this.element.style.display = "none";
35
35
  btnElement?.classList.remove("vditor-menu--current");
36
36
  }
37
- if (getSelection().rangeCount > 0) {
37
+ if (focus && getSelection().rangeCount > 0) {
38
38
  const range = getSelection().getRangeAt(0);
39
39
  if (vditor[vditor.currentMode].element.contains(range.startContainer)) {
40
40
  setSelectionFocus(range);
41
- } else {
42
- vditor[vditor.currentMode].element.focus();
43
41
  }
44
42
  }
45
43
  setPadding(vditor);
@@ -8,6 +8,7 @@ import {highlightRender} from "../markdown/highlightRender";
8
8
  import {mathRender} from "../markdown/mathRender";
9
9
  import {mediaRender} from "../markdown/mediaRender";
10
10
  import {mermaidRender} from "../markdown/mermaidRender";
11
+ import {markmapRender} from "../markdown/markmapRender";
11
12
  import {mindmapRender} from "../markdown/mindmapRender";
12
13
  import {plantumlRender} from "../markdown/plantumlRender";
13
14
  import {getEventName} from "../util/compatibility";
@@ -51,8 +52,21 @@ export class Preview {
51
52
  }
52
53
  return;
53
54
  }
55
+ if (event.target.tagName === "A") {
56
+ if (vditor.options.link.click) {
57
+ vditor.options.link.click(event.target);
58
+ } else if (vditor.options.link.isOpen) {
59
+ window.open(event.target.getAttribute("href"));
60
+ }
61
+ event.preventDefault();
62
+ return;
63
+ }
54
64
  if (event.target.tagName === "IMG") {
55
- previewImage(event.target as HTMLImageElement, vditor.options.lang, vditor.options.theme);
65
+ if (vditor.options.image.preview) {
66
+ vditor.options.image.preview(event.target)
67
+ } else if (vditor.options.image.isPreview) {
68
+ previewImage(event.target as HTMLImageElement, vditor.options.lang, vditor.options.theme);
69
+ }
56
70
  }
57
71
  });
58
72
 
@@ -211,6 +225,7 @@ export class Preview {
211
225
  highlightRender(vditor.options.preview.hljs, vditor.preview.element.lastElementChild as HTMLElement,
212
226
  vditor.options.cdn);
213
227
  mermaidRender(vditor.preview.element.lastElementChild as HTMLElement, vditor.options.cdn, vditor.options.theme);
228
+ markmapRender(vditor.preview.element.lastElementChild as HTMLElement, vditor.options.cdn, vditor.options.theme);
214
229
  flowchartRender(vditor.preview.element.lastElementChild as HTMLElement, vditor.options.cdn);
215
230
  graphvizRender(vditor.preview.element.lastElementChild as HTMLElement, vditor.options.cdn);
216
231
  chartRender(vditor.preview.element.lastElementChild as HTMLElement, vditor.options.cdn, vditor.options.theme);
@@ -145,7 +145,7 @@ export const setEditMode = (vditor: IVditor, type: string, event: Event | string
145
145
  vditor.toolbar.elements["edit-mode"].querySelector(`button[data-mode="${vditor.currentMode}"]`).classList.add("vditor-menu--current");
146
146
  }
147
147
 
148
- vditor.outline.toggle(vditor, vditor.currentMode !== "sv" && vditor.options.outline.enable);
148
+ vditor.outline.toggle(vditor, vditor.currentMode !== "sv" && vditor.options.outline.enable, typeof event !== "string");
149
149
  };
150
150
 
151
151
  export class EditMode extends MenuItem {
@@ -81,7 +81,7 @@ export const initUI = (vditor: IVditor) => {
81
81
  if (vditor.toolbar.elements.export) {
82
82
  // for export pdf
83
83
  vditor.element.insertAdjacentHTML("beforeend",
84
- '<iframe style="width: 100%;height: 0;border: 0"></iframe>');
84
+ '<iframe id="vditorExportIframe" style="width: 100%;height: 0;border: 0"></iframe>');
85
85
  }
86
86
 
87
87
  setEditMode(vditor, vditor.options.mode, afterRender(vditor));
@@ -1,5 +1,5 @@
1
- import { Constants } from "../constants";
2
- import { merge } from "./merge";
1
+ import {Constants} from "../constants";
2
+ import {merge} from "./merge";
3
3
 
4
4
  export class Options {
5
5
  public options: IOptions;
@@ -59,6 +59,12 @@ export class Options {
59
59
  mode: "both",
60
60
  theme: Constants.THEME_OPTIONS,
61
61
  },
62
+ link: {
63
+ isOpen: true,
64
+ },
65
+ image: {
66
+ isPreview: true,
67
+ },
62
68
  resize: {
63
69
  enable: false,
64
70
  position: "bottom",
@@ -29,7 +29,11 @@ export const focusEvent = (vditor: IVditor, editorElement: HTMLElement) => {
29
29
  export const dblclickEvent = (vditor: IVditor, editorElement: HTMLElement) => {
30
30
  editorElement.addEventListener("dblclick", (event: MouseEvent & { target: HTMLElement }) => {
31
31
  if (event.target.tagName === "IMG") {
32
- previewImage(event.target as HTMLImageElement, vditor.options.lang, vditor.options.theme);
32
+ if (vditor.options.image.preview) {
33
+ vditor.options.image.preview(event.target);
34
+ } else if (vditor.options.image.isPreview) {
35
+ previewImage(event.target as HTMLImageElement, vditor.options.lang, vditor.options.theme);
36
+ }
33
37
  }
34
38
  });
35
39
  };
@@ -51,7 +51,9 @@ export const fixCJKPosition = (range: Range, vditor: IVditor, event: KeyboardEve
51
51
  if (pLiElement && getSelectPosition(pLiElement, vditor[vditor.currentMode].element, range).start === 0) {
52
52
 
53
53
  // https://github.com/Vanessa219/vditor/issues/1289 WKWebView切换输入法产生六分之一空格,造成光标错位
54
- pLiElement.nodeValue = pLiElement.nodeValue.replace(/\u2006/g, '');
54
+ if (pLiElement.nodeValue) {
55
+ pLiElement.nodeValue = pLiElement.nodeValue.replace(/\u2006/g, '');
56
+ }
55
57
 
56
58
  const zwspNode = document.createTextNode(Constants.ZWSP);
57
59
  range.insertNode(zwspNode);
@@ -6,6 +6,7 @@ import {graphvizRender} from "../markdown/graphvizRender";
6
6
  import {highlightRender} from "../markdown/highlightRender";
7
7
  import {mathRender} from "../markdown/mathRender";
8
8
  import {mermaidRender} from "../markdown/mermaidRender";
9
+ import {markmapRender} from "../markdown/markmapRender";
9
10
  import {mindmapRender} from "../markdown/mindmapRender";
10
11
  import {plantumlRender} from "../markdown/plantumlRender";
11
12
 
@@ -63,12 +64,15 @@ export const processCodeRender = (previewPanel: HTMLElement, vditor: IVditor) =>
63
64
  }
64
65
  const language = previewPanel.firstElementChild.className.replace("language-", "");
65
66
  if (!language) {
67
+ codeRender(previewPanel);
66
68
  return;
67
69
  }
68
70
  if (language === "abc") {
69
71
  abcRender(previewPanel, vditor.options.cdn);
70
72
  } else if (language === "mermaid") {
71
73
  mermaidRender(previewPanel, vditor.options.cdn, vditor.options.theme);
74
+ } else if (language === "markmap") {
75
+ markmapRender(previewPanel, vditor.options.cdn, vditor.options.theme);
72
76
  } else if (language === "flowchart") {
73
77
  flowchartRender(previewPanel, vditor.options.cdn);
74
78
  } else if (language === "echarts") {
@@ -425,7 +425,13 @@ class WYSIWYG {
425
425
 
426
426
  // 打开链接
427
427
  if (event.target.tagName === "A") {
428
- window.open(event.target.getAttribute("href"));
428
+ if (vditor.options.link.click) {
429
+ vditor.options.link.click(event.target);
430
+ } else if (vditor.options.link.isOpen) {
431
+ window.open(event.target.getAttribute("href"));
432
+ }
433
+ event.preventDefault();
434
+ return;
429
435
  }
430
436
 
431
437
  const range = getEditorRange(vditor);