vditor 4.0.2-alpha.1 → 4.0.2-alpha.10

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.
Files changed (134) hide show
  1. package/README.md +75 -5
  2. package/dist/index.css +108 -29
  3. package/dist/index.d.ts +5 -0
  4. package/dist/index.js +7328 -5406
  5. package/dist/index.min.js +1 -1
  6. package/dist/index.mjs +18244 -0
  7. package/dist/js/graphviz/LICENSE.expat +21 -0
  8. package/dist/js/graphviz/LICENSE.graphviz +277 -0
  9. package/dist/js/graphviz/LICENSE.viz +21 -0
  10. package/dist/js/graphviz/README.md +10 -0
  11. package/dist/js/graphviz/viz.js +6 -330
  12. package/dist/js/i18n/de_DE.js +6 -0
  13. package/dist/js/i18n/en_US.js +6 -0
  14. package/dist/js/i18n/es_ES.js +6 -0
  15. package/dist/js/i18n/fr_FR.js +6 -0
  16. package/dist/js/i18n/ja_JP.js +6 -0
  17. package/dist/js/i18n/ko_KR.js +6 -0
  18. package/dist/js/i18n/pt_BR.js +6 -0
  19. package/dist/js/i18n/ru_RU.js +6 -0
  20. package/dist/js/i18n/sv_SE.js +6 -0
  21. package/dist/js/i18n/vi_VN.js +6 -0
  22. package/dist/js/i18n/zh_CN.js +6 -0
  23. package/dist/js/i18n/zh_TW.js +6 -0
  24. package/dist/js/icons/LICENSE-lucide.txt +46 -0
  25. package/dist/js/icons/ant.js +6 -0
  26. package/dist/js/icons/material.js +6 -0
  27. package/dist/js/lute/lute.min.js +21 -19
  28. package/dist/method.d.ts +3 -3
  29. package/dist/method.js +2125 -1847
  30. package/dist/method.min.js +1 -1
  31. package/dist/method.mjs +3243 -0
  32. package/dist/ts/constants.d.ts +2 -0
  33. package/dist/ts/find/index.d.ts +40 -0
  34. package/dist/ts/find/text.d.ts +4 -0
  35. package/dist/ts/hint/index.d.ts +4 -0
  36. package/dist/ts/ir/index.d.ts +1 -0
  37. package/dist/ts/markdown/codeCopy.d.ts +1 -0
  38. package/dist/ts/markdown/highlightRender.d.ts +1 -1
  39. package/dist/ts/markdown/mathRender.d.ts +24 -1
  40. package/dist/ts/markdown/outlineRender.d.ts +2 -1
  41. package/dist/ts/markdown/plantumlRender.d.ts +1 -1
  42. package/dist/ts/outline/index.d.ts +2 -0
  43. package/dist/ts/sv/index.d.ts +1 -0
  44. package/dist/ts/tip/index.d.ts +6 -1
  45. package/dist/ts/toolbar/Find.d.ts +5 -0
  46. package/dist/ts/undo/index.d.ts +1 -0
  47. package/dist/ts/upload/linkToImg.d.ts +1 -0
  48. package/dist/ts/util/addScript.d.ts +1 -2
  49. package/dist/ts/util/codeDOM.d.ts +2 -0
  50. package/dist/ts/util/codeSelection.d.ts +1 -0
  51. package/dist/ts/util/compatibility.d.ts +1 -1
  52. package/dist/ts/util/expandHTMLTags.d.ts +1 -0
  53. package/dist/ts/util/fixBrowserBehavior.d.ts +1 -0
  54. package/dist/ts/util/imageDrag.d.ts +1 -0
  55. package/dist/ts/util/notifyConfigChange.d.ts +1 -0
  56. package/dist/ts/util/pairedSymbols.d.ts +5 -0
  57. package/dist/ts/util/pasteIntoTable.d.ts +6 -0
  58. package/dist/ts/util/pasteLink.d.ts +1 -0
  59. package/dist/ts/util/processCode.d.ts +2 -0
  60. package/dist/ts/util/tableWheel.d.ts +1 -0
  61. package/dist/ts/util/taskNavigation.d.ts +1 -0
  62. package/dist/ts/wysiwyg/index.d.ts +1 -0
  63. package/dist/types/index.d.ts +84 -2
  64. package/package.json +3 -2
  65. package/src/assets/less/_find.less +84 -0
  66. package/src/assets/less/_hint.less +6 -0
  67. package/src/assets/less/_reset.less +29 -0
  68. package/src/assets/less/_tooltipped.less +2 -2
  69. package/src/assets/less/_wysiwyg.less +4 -0
  70. package/src/assets/less/index.less +3 -0
  71. package/src/index.ts +49 -6
  72. package/src/ts/constants.ts +4 -0
  73. package/src/ts/find/index.ts +310 -0
  74. package/src/ts/find/text.ts +208 -0
  75. package/src/ts/hint/index.ts +31 -8
  76. package/src/ts/ir/expandMarker.ts +3 -0
  77. package/src/ts/ir/index.ts +16 -3
  78. package/src/ts/ir/process.ts +7 -1
  79. package/src/ts/ir/processKeydown.ts +5 -0
  80. package/src/ts/markdown/chartRender.ts +28 -2
  81. package/src/ts/markdown/codeCopy.ts +41 -0
  82. package/src/ts/markdown/codeRender.ts +19 -8
  83. package/src/ts/markdown/graphvizRender.ts +51 -39
  84. package/src/ts/markdown/highlightRender.ts +25 -12
  85. package/src/ts/markdown/mathRender.ts +26 -38
  86. package/src/ts/markdown/mediaRender.ts +1 -1
  87. package/src/ts/markdown/mermaidRender.ts +4 -3
  88. package/src/ts/markdown/outlineRender.ts +44 -15
  89. package/src/ts/markdown/plantumlRender.ts +24 -5
  90. package/src/ts/markdown/previewRender.ts +5 -1
  91. package/src/ts/markdown/setLute.ts +10 -0
  92. package/src/ts/outline/index.ts +72 -0
  93. package/src/ts/preview/index.ts +1 -1
  94. package/src/ts/sv/index.ts +1 -0
  95. package/src/ts/sv/process.ts +7 -1
  96. package/src/ts/tip/index.ts +25 -9
  97. package/src/ts/toolbar/CodeTheme.ts +2 -0
  98. package/src/ts/toolbar/ContentTheme.ts +2 -0
  99. package/src/ts/toolbar/EditMode.ts +5 -0
  100. package/src/ts/toolbar/Find.ts +12 -0
  101. package/src/ts/toolbar/Headings.ts +4 -6
  102. package/src/ts/toolbar/Outline.ts +2 -0
  103. package/src/ts/toolbar/Record.ts +5 -0
  104. package/src/ts/toolbar/index.ts +5 -1
  105. package/src/ts/toolbar/setToolbar.ts +1 -1
  106. package/src/ts/ui/initUI.ts +10 -1
  107. package/src/ts/ui/setPreviewMode.ts +2 -0
  108. package/src/ts/undo/index.ts +41 -9
  109. package/src/ts/upload/index.ts +1 -1
  110. package/src/ts/upload/linkToImg.ts +91 -0
  111. package/src/ts/util/Options.ts +9 -0
  112. package/src/ts/util/addScript.ts +15 -24
  113. package/src/ts/util/codeDOM.ts +21 -0
  114. package/src/ts/util/codeSelection.ts +37 -0
  115. package/src/ts/util/compatibility.ts +2 -2
  116. package/src/ts/util/editorCommonEvent.ts +48 -5
  117. package/src/ts/util/expandHTMLTags.ts +36 -0
  118. package/src/ts/util/fixBrowserBehavior.ts +61 -61
  119. package/src/ts/util/imageDrag.ts +108 -0
  120. package/src/ts/util/notifyConfigChange.ts +16 -0
  121. package/src/ts/util/pairedSymbols.ts +109 -0
  122. package/src/ts/util/pasteIntoTable.ts +136 -0
  123. package/src/ts/util/pasteLink.ts +51 -0
  124. package/src/ts/util/processCode.ts +46 -9
  125. package/src/ts/util/selection.ts +11 -2
  126. package/src/ts/util/tableWheel.ts +31 -0
  127. package/src/ts/util/taskNavigation.ts +113 -0
  128. package/src/ts/util/toc.ts +9 -3
  129. package/src/ts/wysiwyg/afterRenderEvent.ts +7 -1
  130. package/src/ts/wysiwyg/highlightToolbarWYSIWYG.ts +6 -5
  131. package/src/ts/wysiwyg/index.ts +20 -6
  132. package/src/ts/wysiwyg/processKeydown.ts +5 -0
  133. package/src/ts/wysiwyg/setHeading.ts +4 -0
  134. package/dist/js/graphviz/full.render.js +0 -90
@@ -16,6 +16,10 @@ 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;
20
+ if (options.enableAddUndoStack && vditor.options.undoDelay === 0) {
21
+ vditor.undo.addToUndoStack(vditor);
22
+ }
19
23
  vditor.preview.render(vditor);
20
24
 
21
25
  const text = getMarkdown(vditor);
@@ -40,7 +44,9 @@ export const processAfterRender = (vditor: IVditor, options = {
40
44
 
41
45
  clearTimeout(vditor.sv.processTimeoutId);
42
46
  vditor.sv.processTimeoutId = window.setTimeout(() => {
43
- if (options.enableAddUndoStack && !vditor.sv.composingLock) {
47
+ vditor.sv.processTimeoutId = 0;
48
+ vditor.sv.pendingUndoStack = false;
49
+ if (options.enableAddUndoStack && vditor.options.undoDelay !== 0 && !vditor.sv.composingLock) {
44
50
  vditor.undo.addToUndoStack(vditor);
45
51
  }
46
52
  }, vditor.options.undoDelay);
@@ -1,12 +1,25 @@
1
1
  export class Tip {
2
2
  public element: HTMLElement;
3
+ private timeoutId: number;
4
+ private positionTimeoutId: number;
5
+ private visible = false;
3
6
 
4
- constructor() {
7
+ constructor(private tips?: ICustomTips) {
5
8
  this.element = document.createElement("div");
6
9
  this.element.className = "vditor-tip";
7
10
  }
8
11
 
9
12
  public show(text: string, time: number = 6000) {
13
+ clearTimeout(this.timeoutId);
14
+ clearTimeout(this.positionTimeoutId);
15
+ this.visible = true;
16
+ if (time !== 0) {
17
+ this.timeoutId = window.setTimeout(() => this.hide(), time);
18
+ }
19
+ if (this.tips) {
20
+ this.tips.show(text, time);
21
+ return;
22
+ }
10
23
  this.element.className = "vditor-tip vditor-tip--show";
11
24
  if (time === 0) {
12
25
  this.element.innerHTML = `<div class="vditor-tip__content">${text}
@@ -16,24 +29,27 @@ export class Tip {
16
29
  });
17
30
  } else {
18
31
  this.element.innerHTML = `<div class="vditor-tip__content">${text}</div>`;
19
- setTimeout(() => {
20
- this.hide();
21
- }, time);
22
32
  }
23
33
 
24
34
  // 需在动画结束后才能确定位置
25
- this.element.removeAttribute("style")
26
- setTimeout(() => {
35
+ this.element.removeAttribute("style");
36
+ this.positionTimeoutId = window.setTimeout(() => {
27
37
  const rect = this.element.getBoundingClientRect();
28
38
  if (rect.top < 46) {
29
- this.element.style.position = "fixed"
30
- this.element.style.top = "46px"
39
+ this.element.style.position = "fixed";
40
+ this.element.style.top = "46px";
31
41
  }
32
42
  }, 150);
33
43
  }
34
44
 
35
45
  public hide() {
36
- this.element.className = "vditor-messageElementtip";
46
+ clearTimeout(this.timeoutId);
47
+ clearTimeout(this.positionTimeoutId);
48
+ if (this.visible) {
49
+ this.tips?.hide();
50
+ }
51
+ this.visible = false;
52
+ this.element.className = "vditor-tip";
37
53
  this.element.innerHTML = "";
38
54
  }
39
55
  }
@@ -1,6 +1,7 @@
1
1
  import {Constants} from "../constants";
2
2
  import {setCodeTheme} from "../ui/setCodeTheme";
3
3
  import {getEventName} from "../util/compatibility";
4
+ import {notifyConfigChange} from "../util/notifyConfigChange";
4
5
  import {MenuItem} from "./MenuItem";
5
6
  import {hidePanel, toggleSubMenu} from "./setToolbar";
6
7
 
@@ -25,6 +26,7 @@ export class CodeTheme extends MenuItem {
25
26
  hidePanel(vditor, ["subToolbar"]);
26
27
  vditor.options.preview.hljs.style = event.target.textContent;
27
28
  setCodeTheme(event.target.textContent, vditor.options.cdn);
29
+ notifyConfigChange(vditor);
28
30
  event.preventDefault();
29
31
  event.stopPropagation();
30
32
  }
@@ -1,5 +1,6 @@
1
1
  import {setContentTheme} from "../ui/setContentTheme";
2
2
  import {getEventName} from "../util/compatibility";
3
+ import {notifyConfigChange} from "../util/notifyConfigChange";
3
4
  import {MenuItem} from "./MenuItem";
4
5
  import {hidePanel, toggleSubMenu} from "./setToolbar";
5
6
 
@@ -24,6 +25,7 @@ export class ContentTheme extends MenuItem {
24
25
  hidePanel(vditor, ["subToolbar"]);
25
26
  vditor.options.preview.theme.current = event.target.getAttribute("data-type");
26
27
  setContentTheme(vditor.options.preview.theme.current, vditor.options.preview.theme.path);
28
+ notifyConfigChange(vditor);
27
29
  event.preventDefault();
28
30
  event.stopPropagation();
29
31
  }
@@ -5,6 +5,7 @@ import {mathRender} from "../markdown/mathRender";
5
5
  import {processAfterRender as processSVAfterRender} from "../sv/process";
6
6
  import {setPadding, setTypewriterPosition} from "../ui/initUI";
7
7
  import {getEventName, updateHotkeyTip} from "../util/compatibility";
8
+ import {notifyConfigChange} from "../util/notifyConfigChange";
8
9
  import {highlightToolbar} from "../util/highlightToolbar";
9
10
  import {processCodeRender} from "../util/processCode";
10
11
  import {renderToc} from "../util/toc";
@@ -141,6 +142,10 @@ export const setEditMode = (vditor: IVditor, type: string, event: Event | string
141
142
  }
142
143
 
143
144
  vditor.outline.toggle(vditor, vditor.currentMode !== "sv" && vditor.options.outline.enable, typeof event !== "string");
145
+ vditor.find?.refresh();
146
+ if (typeof event !== "string") {
147
+ notifyConfigChange(vditor);
148
+ }
144
149
  };
145
150
 
146
151
  export class EditMode extends MenuItem {
@@ -0,0 +1,12 @@
1
+ import {getEventName} from "../util/compatibility";
2
+ import {MenuItem} from "./MenuItem";
3
+
4
+ export class Find extends MenuItem {
5
+ constructor(vditor: IVditor, menuItem: IMenuItem) {
6
+ super(vditor, {...menuItem, tip: menuItem.tip || window.VditorI18n.find || "Find"});
7
+ this.element.firstElementChild.addEventListener(getEventName(), event => {
8
+ event.preventDefault();
9
+ vditor.find.open();
10
+ });
11
+ }
12
+ }
@@ -15,12 +15,10 @@ export class Headings extends MenuItem {
15
15
 
16
16
  const panelElement = document.createElement("div");
17
17
  panelElement.className = "vditor-hint vditor-panel--arrow";
18
- panelElement.innerHTML = `<button data-tag="h1" data-value="# ">${window.VditorI18n.heading1} ${updateHotkeyTip("&lt;⌥⌘1>")}</button>
19
- <button data-tag="h2" data-value="## ">${window.VditorI18n.heading2} &lt;${updateHotkeyTip("⌥⌘2")}></button>
20
- <button data-tag="h3" data-value="### ">${window.VditorI18n.heading3} &lt;${updateHotkeyTip("⌥⌘3")}></button>
21
- <button data-tag="h4" data-value="#### ">${window.VditorI18n.heading4} &lt;${updateHotkeyTip("⌥⌘4")}></button>
22
- <button data-tag="h5" data-value="##### ">${window.VditorI18n.heading5} &lt;${updateHotkeyTip("⌥⌘5")}></button>
23
- <button data-tag="h6" data-value="###### ">${window.VditorI18n.heading6} &lt;${updateHotkeyTip("⌥⌘6")}></button>`;
18
+ panelElement.innerHTML = Array.from({length: 6}, (_, index) => {
19
+ const level = index + 1;
20
+ return `<button data-tag="h${level}" data-value="${"#".repeat(level)} ">${window.VditorI18n[`heading${level}`]}<span class="vditor-hint__hotkey"> &lt;${updateHotkeyTip(`⌥⌘${level}`)}></span></button>`;
21
+ }).join("");
24
22
 
25
23
  this.element.appendChild(panelElement);
26
24
 
@@ -1,5 +1,6 @@
1
1
  import {Constants} from "../constants";
2
2
  import {getEventName} from "../util/compatibility";
3
+ import {notifyConfigChange} from "../util/notifyConfigChange";
3
4
  import {MenuItem} from "./MenuItem";
4
5
 
5
6
  export class Outline extends MenuItem {
@@ -16,6 +17,7 @@ export class Outline extends MenuItem {
16
17
  }
17
18
  vditor.options.outline.enable = !this.element.firstElementChild.classList.contains("vditor-menu--current");
18
19
  vditor.outline.toggle(vditor, vditor.options.outline.enable);
20
+ notifyConfigChange(vditor);
19
21
  });
20
22
  }
21
23
  }
@@ -49,6 +49,11 @@ export class Record extends MenuItem {
49
49
  if (mediaRecorder.isRecording) {
50
50
  mediaRecorder.stopRecording();
51
51
  vditor.tip.hide();
52
+ if (vditor.currentMode === "sv") {
53
+ vditor.sv.element.disabled = false;
54
+ } else {
55
+ editorElement.setAttribute("contenteditable", "true");
56
+ }
52
57
  const file: File = new File([mediaRecorder.buildWavFileBlob()],
53
58
  `record${(new Date()).getTime()}.wav`, {type: "video/webm"});
54
59
  uploadFiles(vditor, [file]);
@@ -26,6 +26,7 @@ import {Redo} from "./Redo";
26
26
  import {toggleSubMenu} from "./setToolbar";
27
27
  import {Undo} from "./Undo";
28
28
  import {Upload} from "./Upload";
29
+ import {Find} from "./Find";
29
30
 
30
31
  export class Toolbar {
31
32
  public elements: { [key: string]: HTMLElement };
@@ -44,7 +45,7 @@ export class Toolbar {
44
45
  if (menuItem.toolbar) {
45
46
  const panelElement = document.createElement("div");
46
47
  panelElement.className = "vditor-hint vditor-panel--arrow";
47
- panelElement.addEventListener(getEventName(), (event) => {
48
+ panelElement.addEventListener(getEventName(), () => {
48
49
  panelElement.style.display = "none";
49
50
  });
50
51
  menuItem.toolbar.forEach((subMenuItem: IMenuItem, subI: number) => {
@@ -89,6 +90,9 @@ export class Toolbar {
89
90
  private genItem(vditor: IVditor, menuItem: IMenuItem, index: number) {
90
91
  let menuItemObj;
91
92
  switch (menuItem.name) {
93
+ case "find":
94
+ menuItemObj = new Find(vditor, menuItem);
95
+ break;
92
96
  case "bold":
93
97
  case "italic":
94
98
  case "more":
@@ -85,7 +85,7 @@ export const hidePanel = (vditor: IVditor, panels: string[], exceptElement?: HTM
85
85
  }
86
86
  }
87
87
  if (panels.includes("hint")) {
88
- vditor.hint.element.style.display = "none";
88
+ vditor.hint.hide();
89
89
  }
90
90
  if (vditor.wysiwyg.popover && panels.includes("popover")) {
91
91
  vditor.wysiwyg.popover.style.display = "none";
@@ -2,12 +2,16 @@ import {Constants} from "../constants";
2
2
  import {setEditMode} from "../toolbar/EditMode";
3
3
  import {hidePanel} from "../toolbar/setToolbar";
4
4
  import {accessLocalStorage} from "../util/compatibility";
5
+ import {bindTableWheel} from "../util/tableWheel";
5
6
  import {setContentTheme} from "./setContentTheme";
6
7
  import {setTheme} from "./setTheme";
7
8
 
8
9
  export const initUI = (vditor: IVditor) => {
9
10
  vditor.element.innerHTML = "";
10
11
  vditor.element.classList.add("vditor");
12
+ [vditor.wysiwyg.element, vditor.ir.element].forEach(element => {
13
+ element.classList.toggle("vditor-reset--indent", !!vditor.options.preview.markdown.paragraphBeginningSpace);
14
+ });
11
15
  // 支持 RTL
12
16
  if (vditor.options.rtl) {
13
17
  vditor.element.setAttribute("dir", "rtl")
@@ -29,12 +33,14 @@ export const initUI = (vditor: IVditor) => {
29
33
  }
30
34
 
31
35
  vditor.element.appendChild(vditor.toolbar.element);
36
+ vditor.element.appendChild(vditor.find.element);
32
37
 
33
38
  const contentElement = document.createElement("div");
34
39
  contentElement.className = "vditor-content";
35
40
 
36
41
  if (vditor.options.outline.position === "left") {
37
42
  contentElement.appendChild(vditor.outline.element);
43
+ contentElement.appendChild(vditor.outline.resizeElement);
38
44
  }
39
45
 
40
46
  contentElement.appendChild(vditor.wysiwyg.element.parentElement);
@@ -51,6 +57,7 @@ export const initUI = (vditor: IVditor) => {
51
57
 
52
58
  if (vditor.options.outline.position === "right") {
53
59
  vditor.outline.element.classList.add("vditor-outline--right");
60
+ contentElement.appendChild(vditor.outline.resizeElement);
54
61
  contentElement.appendChild(vditor.outline.element);
55
62
  }
56
63
 
@@ -67,6 +74,8 @@ export const initUI = (vditor: IVditor) => {
67
74
  contentElement.appendChild(vditor.tip.element);
68
75
 
69
76
  vditor.element.appendChild(contentElement);
77
+ vditor.outline.bindResize(vditor);
78
+ bindTableWheel(vditor.element);
70
79
 
71
80
  contentElement.addEventListener("click", () => {
72
81
  hidePanel(vditor, ["subToolbar"]);
@@ -85,7 +94,7 @@ export const initUI = (vditor: IVditor) => {
85
94
  if (navigator.userAgent.indexOf("iPhone") > -1 && typeof window.visualViewport !== "undefined") {
86
95
  // https://github.com/Vanessa219/vditor/issues/379
87
96
  let pendingUpdate = false;
88
- const viewportHandler = (event: Event) => {
97
+ const viewportHandler = () => {
89
98
  if (pendingUpdate) {
90
99
  return;
91
100
  }
@@ -1,5 +1,6 @@
1
1
  import {removeCurrentToolbar} from "../toolbar/setToolbar";
2
2
  import {setCurrentToolbar} from "../toolbar/setToolbar";
3
+ import {notifyConfigChange} from "../util/notifyConfigChange";
3
4
 
4
5
  export const setPreviewMode = (mode: "both" | "editor", vditor: IVditor) => {
5
6
  if (vditor.options.preview.mode === mode) {
@@ -28,4 +29,5 @@ export const setPreviewMode = (mode: "both" | "editor", vditor: IVditor) => {
28
29
  if (vditor.devtools) {
29
30
  vditor.devtools.renderEchart(vditor);
30
31
  }
32
+ notifyConfigChange(vditor);
31
33
  };
@@ -4,9 +4,12 @@ import {isFirefox, isSafari} from "../util/compatibility";
4
4
  import {scrollCenter} from "../util/editorCommonEvent";
5
5
  import {execAfterRender} from "../util/fixBrowserBehavior";
6
6
  import {highlightToolbar} from "../util/highlightToolbar";
7
- import {processCodeRender} from "../util/processCode";
7
+ import {highlightEditableCode, processCodeRender} from "../util/processCode";
8
+ import {normalizeCodeDOM} from "../util/codeDOM";
8
9
  import {setRangeByWbr, setSelectionFocus} from "../util/selection";
9
10
  import {renderToc} from "../util/toc";
11
+ import {codeRender} from "../markdown/codeRender";
12
+ import {highlightRender} from "../markdown/highlightRender";
10
13
 
11
14
  interface IUndo {
12
15
  hasUndo: boolean;
@@ -24,7 +27,7 @@ class Undo {
24
27
 
25
28
  constructor() {
26
29
  this.resetStack();
27
- // @ts-ignore
30
+ // @ts-expect-error CommonJS 导出在运行时是可构造函数。
28
31
  this.dmp = new DiffMatchPatch();
29
32
  }
30
33
 
@@ -52,10 +55,11 @@ class Undo {
52
55
  }
53
56
 
54
57
  public undo(vditor: IVditor) {
55
- if (vditor.currentMode === "sv" ? vditor.sv.element.disabled :
56
- vditor[vditor.currentMode].element.getAttribute("contenteditable") === "false") {
58
+ if (vditor[vditor.currentMode].composingLock || (vditor.currentMode === "sv" ? vditor.sv.element.disabled :
59
+ vditor[vditor.currentMode].element.getAttribute("contenteditable") === "false")) {
57
60
  return;
58
61
  }
62
+ this.flushPendingUndoStack(vditor);
59
63
  if (this[vditor.currentMode].undoStack.length < 2) {
60
64
  return;
61
65
  }
@@ -71,10 +75,11 @@ class Undo {
71
75
  }
72
76
 
73
77
  public redo(vditor: IVditor) {
74
- if (vditor.currentMode === "sv" ? vditor.sv.element.disabled :
75
- vditor[vditor.currentMode].element.getAttribute("contenteditable") === "false") {
78
+ if (vditor[vditor.currentMode].composingLock || (vditor.currentMode === "sv" ? vditor.sv.element.disabled :
79
+ vditor[vditor.currentMode].element.getAttribute("contenteditable") === "false")) {
76
80
  return;
77
81
  }
82
+ this.flushPendingUndoStack(vditor);
78
83
  const state = this[vditor.currentMode].redoStack.pop();
79
84
  if (!state) {
80
85
  return;
@@ -83,6 +88,14 @@ class Undo {
83
88
  this.renderDiff(state, vditor, true);
84
89
  }
85
90
 
91
+ private flushPendingUndoStack(vditor: IVditor) {
92
+ if (vditor[vditor.currentMode].pendingUndoStack) {
93
+ // 输入通知、计数和缓存共用该定时器,记录快照后仍需执行回调。
94
+ vditor[vditor.currentMode].pendingUndoStack = false;
95
+ this.addToUndoStack(vditor);
96
+ }
97
+ }
98
+
86
99
  public recordFirstPosition(vditor: IVditor, event: KeyboardEvent) {
87
100
  if (vditor.currentMode !== "sv" && getSelection().rangeCount === 0) {
88
101
  return;
@@ -111,8 +124,14 @@ class Undo {
111
124
  }
112
125
 
113
126
  public addToUndoStack(vditor: IVditor) {
114
- // afterRenderEvent.ts 已经 debounce
127
+ if (vditor[vditor.currentMode].composingLock) {
128
+ return;
129
+ }
115
130
  const text = this.addCaret(vditor, true);
131
+ if (this[vditor.currentMode].undoStack.length > 0 &&
132
+ text.replace("<wbr>", "") === this[vditor.currentMode].lastText.replace("<wbr>", "")) {
133
+ return;
134
+ }
116
135
  const diff = this.dmp.diff_main(text, this[vditor.currentMode].lastText, true);
117
136
  const patchList = this.dmp.patch_make(text, this[vditor.currentMode].lastText, diff);
118
137
  if (patchList.length === 0 && this[vditor.currentMode].undoStack.length > 0) {
@@ -174,9 +193,12 @@ class Undo {
174
193
  });
175
194
  if (!vditor[vditor.currentMode].element.querySelector("wbr")) {
176
195
  // Safari 第一次输入没有光标,需手动定位到结尾
177
- const range = getSelection().getRangeAt(0);
178
- range.setEndBefore(vditor[vditor.currentMode].element);
196
+ const element = vditor[vditor.currentMode].element;
197
+ const range = element.ownerDocument.createRange();
198
+ range.selectNodeContents(element);
179
199
  range.collapse(false);
200
+ element.focus();
201
+ setSelectionFocus(range);
180
202
  } else {
181
203
  setRangeByWbr(
182
204
  vditor[vditor.currentMode].element, vditor[vditor.currentMode].element.ownerDocument.createRange());
@@ -199,6 +221,15 @@ class Undo {
199
221
  .forEach((item: HTMLElement) => {
200
222
  processCodeRender(item, vditor);
201
223
  });
224
+ vditor[vditor.currentMode].element.querySelectorAll(".vditor-code__preview")
225
+ .forEach((item: HTMLElement) => {
226
+ highlightEditableCode(item, vditor);
227
+ codeRender(item, vditor.options.preview.hljs);
228
+ // 快照可能早于异步高亮完成,普通预览需补齐其渲染结果。
229
+ if (!item.querySelector("code.hljs")) {
230
+ highlightRender(vditor.options.preview.hljs, item, vditor.options.cdn);
231
+ }
232
+ });
202
233
 
203
234
  if (this[vditor.currentMode].undoStack.length > 1) {
204
235
  enableToolbar(vditor.toolbar.elements, ["undo"]);
@@ -252,6 +283,7 @@ class Undo {
252
283
  }
253
284
  // 移除数学公式、echart 渲染 https://github.com/Vanessa219/vditor/issues/1738
254
285
  const cloneElement = vditor[vditor.currentMode].element.cloneNode(true) as HTMLElement;
286
+ normalizeCodeDOM(cloneElement);
255
287
  cloneElement.querySelectorAll(`.vditor-${vditor.currentMode}__preview[data-render='1']`)
256
288
  .forEach((item: HTMLElement) => {
257
289
  if (!item.firstElementChild) {
@@ -119,7 +119,7 @@ const genUploadedLabel = (responseText: string, vditor: IVditor) => {
119
119
  let type = key.substr(lastIndex);
120
120
  const filename = vditor.options.upload.filename(key.substr(0, lastIndex)) + type;
121
121
  type = type.toLowerCase();
122
- if (type.indexOf(".wav") === 0 || type.indexOf(".mp3") === 0 || type.indexOf(".ogg") === 0) {
122
+ if (type.indexOf(".wav") === 0 || type.indexOf(".mp3") === 0 || type.indexOf(".ogg") === 0 || type === ".aac") {
123
123
  if (vditor.currentMode === "wysiwyg") {
124
124
  succFileText += `<div class="vditor-wysiwyg__block" data-type="html-block"
125
125
  data-block="0"><pre><code>&lt;audio controls="controls" src="${path}"&gt;&lt;/audio&gt;</code></pre><pre class="vditor-wysiwyg__preview" data-render="1"><audio controls="controls" src="${path}"></audio></pre></div>\n`;
@@ -0,0 +1,91 @@
1
+ import {setHeaders} from "./setHeaders";
2
+
3
+ const getDisplayURLs = (vditor: IVditor, urls: string[]) => {
4
+ if (!vditor.options.preview.markdown.linkBase && !vditor.options.preview.markdown.linkPrefix) {
5
+ return urls;
6
+ }
7
+ const lute = Lute.New();
8
+ const image = document.createElement("img");
9
+ const markdown = urls.map(url => {
10
+ image.setAttribute("src", url);
11
+ return lute.HTML2Md(image.outerHTML);
12
+ });
13
+ lute.SetLinkBase(vditor.options.preview.markdown.linkBase);
14
+ lute.SetLinkPrefix(vditor.options.preview.markdown.linkPrefix);
15
+ const content = document.createElement("div");
16
+ return markdown.map((value, index) => {
17
+ content.innerHTML = lute.Md2HTML(value);
18
+ return content.querySelector("img")?.getAttribute("src") || urls[index];
19
+ });
20
+ };
21
+
22
+ const replaceImageURL = (vditor: IVditor, original: string, url: string, afterRender: () => void) => {
23
+ if (!original || !url) {
24
+ return;
25
+ }
26
+ if (vditor.currentMode === "sv") {
27
+ const element = vditor.sv.element;
28
+ const start = element.selectionStart;
29
+ const end = element.selectionEnd;
30
+ element.value = element.value.split(original).join(url);
31
+ element.setSelectionRange(start, end);
32
+ } else {
33
+ const [originalDisplay, display] = getDisplayURLs(vditor, [original, url]);
34
+ vditor[vditor.currentMode].element.querySelectorAll("img").forEach(img => {
35
+ if (img.getAttribute("src") !== original && img.getAttribute("src") !== originalDisplay) {
36
+ return;
37
+ }
38
+ img.setAttribute("src", display);
39
+ if (vditor.currentMode === "ir") {
40
+ const marker = img.closest('[data-type="img"]')?.querySelector(".vditor-ir__marker--link");
41
+ if (marker) {
42
+ marker.textContent = url;
43
+ }
44
+ }
45
+ });
46
+ }
47
+ afterRender();
48
+ };
49
+
50
+ export const uploadLinkToImg = (vditor: IVditor, src: string, afterRender: () => void) => {
51
+ const upload = vditor.options.upload;
52
+ if (upload.linkToImgHandler) {
53
+ Promise.resolve().then(() => upload.linkToImgHandler(src)).then(url => {
54
+ replaceImageURL(vditor, src, url, afterRender);
55
+ }).catch(error => vditor.tip.show(error instanceof Error ? error.message : String(error)));
56
+ return;
57
+ }
58
+ const xhr = new XMLHttpRequest();
59
+ xhr.open("POST", upload.linkToImgUrl);
60
+ if (upload.token) {
61
+ xhr.setRequestHeader("X-Upload-Token", upload.token);
62
+ }
63
+ if (upload.withCredentials) {
64
+ xhr.withCredentials = true;
65
+ }
66
+ setHeaders(vditor, xhr);
67
+ xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
68
+ xhr.onreadystatechange = () => {
69
+ if (xhr.readyState !== XMLHttpRequest.DONE) {
70
+ return;
71
+ }
72
+ if (xhr.status === 200) {
73
+ let responseText = xhr.responseText;
74
+ if (upload.linkToImgFormat) {
75
+ responseText = upload.linkToImgFormat(responseText);
76
+ }
77
+ const response = JSON.parse(responseText);
78
+ if (response.code !== 0) {
79
+ vditor.tip.show(response.msg);
80
+ return;
81
+ }
82
+ replaceImageURL(vditor, response.data.originalURL, response.data.url, afterRender);
83
+ } else {
84
+ vditor.tip.show(xhr.responseText);
85
+ }
86
+ upload.linkToImgCallback?.(xhr.responseText);
87
+ };
88
+ const extraData = Object.fromEntries(Object.entries(upload.extraData || {})
89
+ .filter(([, value]) => typeof value === "string"));
90
+ xhr.send(JSON.stringify({...extraData, url: src}));
91
+ };
@@ -50,6 +50,7 @@ export class Options {
50
50
  position: "left",
51
51
  },
52
52
  placeholder: "",
53
+ plantuml: Constants.PLANTUML_OPTIONS,
53
54
  preview: {
54
55
  actions: ["desktop", "tablet", "mobile", "mp-wechat", "zhihu"],
55
56
  delay: 1000,
@@ -67,6 +68,7 @@ export class Options {
67
68
  },
68
69
  link: {
69
70
  isOpen: true,
71
+ ctrlClick: false,
70
72
  },
71
73
  image: {
72
74
  isPreview: true,
@@ -109,6 +111,7 @@ export class Options {
109
111
  {
110
112
  name: "more",
111
113
  toolbar: [
114
+ "find",
112
115
  "both",
113
116
  "code-theme",
114
117
  "content-theme",
@@ -188,6 +191,12 @@ export class Options {
188
191
 
189
192
  private mergeToolbar(toolbar: Array<string | IMenuItem>) {
190
193
  const toolbarItem = [
194
+ {
195
+ name: "find",
196
+ hotkey: "⌘F",
197
+ icon: '<svg><use xlink:href="#vditor-icon-search"></use></svg>',
198
+ tipPosition: "ne",
199
+ },
191
200
  {
192
201
  icon: '<svg><use xlink:href="#vditor-icon-export"></use></svg>',
193
202
  name: "export",
@@ -1,35 +1,23 @@
1
- export const addScriptSync = (path: string, id: string) => {
2
- if (document.getElementById(id)) {
3
- return false;
4
- }
5
- const xhrObj = new XMLHttpRequest();
6
- xhrObj.open("GET", path, false);
7
- xhrObj.setRequestHeader("Accept",
8
- "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01");
9
- xhrObj.send("");
10
- const scriptElement = document.createElement("script");
11
- scriptElement.type = "text/javascript";
12
- scriptElement.text = xhrObj.responseText;
13
- scriptElement.id = id;
14
- document.head.appendChild(scriptElement);
15
- };
1
+ const loadingScripts = new Map<string, Promise<boolean>>();
16
2
 
17
3
  export const addScript = (path: string, id: string) => {
18
- return new Promise((resolve, reject) => {
19
- if (document.getElementById(id)) {
20
- // 脚本加载后再次调用直接返回
21
- resolve(true);
22
- return false;
23
- }
4
+ if (document.getElementById(id)) {
5
+ return Promise.resolve(true);
6
+ }
7
+ if (loadingScripts.has(id)) {
8
+ return loadingScripts.get(id);
9
+ }
10
+ const promise = new Promise<boolean>((resolve, reject) => {
24
11
  const scriptElement = document.createElement("script");
25
12
  scriptElement.src = path;
26
13
  scriptElement.async = true;
27
- // 循环调用时 Chrome 不会重复请求 js
28
- document.head.appendChild(scriptElement);
29
14
  scriptElement.onerror = (event) => {
15
+ scriptElement.remove();
16
+ loadingScripts.delete(id);
30
17
  reject(event);
31
- }
18
+ };
32
19
  scriptElement.onload = () => {
20
+ loadingScripts.delete(id);
33
21
  if (document.getElementById(id)) {
34
22
  // 循环调用需清除 DOM 中的 script 标签
35
23
  scriptElement.remove();
@@ -39,5 +27,8 @@ export const addScript = (path: string, id: string) => {
39
27
  scriptElement.id = id;
40
28
  resolve(true);
41
29
  };
30
+ document.head.appendChild(scriptElement);
42
31
  });
32
+ loadingScripts.set(id, promise);
33
+ return promise;
43
34
  };
@@ -0,0 +1,21 @@
1
+ export const normalizeCodeDOM = (element: HTMLElement) => {
2
+ element.querySelectorAll("code[data-code-highlight]").forEach(code => {
3
+ code.removeAttribute("data-code-highlight");
4
+ code.classList.remove("hljs");
5
+ code.querySelectorAll("span").forEach(span => {
6
+ if (!span.classList.contains("vditor-wbr")) {
7
+ span.replaceWith(...Array.from(span.childNodes));
8
+ }
9
+ });
10
+ });
11
+ };
12
+
13
+ export const normalizeCodeHTML = (html: string) => {
14
+ if (!html.includes("data-code-highlight")) {
15
+ return html;
16
+ }
17
+ const element = document.createElement("div");
18
+ element.innerHTML = html;
19
+ normalizeCodeDOM(element);
20
+ return element.innerHTML;
21
+ };