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.
Files changed (96) hide show
  1. package/README.md +69 -5
  2. package/dist/index.css +35 -29
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +6484 -5444
  5. package/dist/index.min.js +1 -1
  6. package/dist/index.mjs +17599 -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/lute/lute.min.js +3 -3
  13. package/dist/method.d.ts +2 -2
  14. package/dist/method.js +2017 -1903
  15. package/dist/method.min.js +1 -1
  16. package/dist/method.mjs +3243 -0
  17. package/dist/ts/constants.d.ts +2 -0
  18. package/dist/ts/ir/index.d.ts +1 -0
  19. package/dist/ts/markdown/mathRender.d.ts +24 -1
  20. package/dist/ts/markdown/outlineRender.d.ts +2 -1
  21. package/dist/ts/markdown/plantumlRender.d.ts +1 -1
  22. package/dist/ts/outline/index.d.ts +2 -0
  23. package/dist/ts/sv/index.d.ts +1 -0
  24. package/dist/ts/tip/index.d.ts +6 -1
  25. package/dist/ts/undo/index.d.ts +1 -0
  26. package/dist/ts/upload/linkToImg.d.ts +1 -0
  27. package/dist/ts/util/addScript.d.ts +1 -2
  28. package/dist/ts/util/compatibility.d.ts +1 -1
  29. package/dist/ts/util/expandHTMLTags.d.ts +1 -0
  30. package/dist/ts/util/imageDrag.d.ts +1 -0
  31. package/dist/ts/util/notifyConfigChange.d.ts +1 -0
  32. package/dist/ts/util/pairedSymbols.d.ts +5 -0
  33. package/dist/ts/util/pasteIntoTable.d.ts +6 -0
  34. package/dist/ts/util/pasteLink.d.ts +1 -0
  35. package/dist/ts/util/processCode.d.ts +1 -0
  36. package/dist/ts/util/tableWheel.d.ts +1 -0
  37. package/dist/ts/util/taskNavigation.d.ts +1 -0
  38. package/dist/ts/wysiwyg/index.d.ts +1 -0
  39. package/dist/types/index.d.ts +56 -1
  40. package/package.json +3 -2
  41. package/src/assets/less/_hint.less +6 -0
  42. package/src/assets/less/_reset.less +29 -0
  43. package/src/assets/less/_tooltipped.less +2 -2
  44. package/src/assets/less/_wysiwyg.less +4 -0
  45. package/src/index.ts +28 -6
  46. package/src/ts/constants.ts +4 -0
  47. package/src/ts/ir/expandMarker.ts +3 -0
  48. package/src/ts/ir/index.ts +11 -1
  49. package/src/ts/ir/process.ts +3 -0
  50. package/src/ts/markdown/chartRender.ts +28 -2
  51. package/src/ts/markdown/graphvizRender.ts +51 -39
  52. package/src/ts/markdown/highlightRender.ts +6 -2
  53. package/src/ts/markdown/mathRender.ts +26 -38
  54. package/src/ts/markdown/mediaRender.ts +1 -1
  55. package/src/ts/markdown/mermaidRender.ts +4 -3
  56. package/src/ts/markdown/outlineRender.ts +44 -15
  57. package/src/ts/markdown/plantumlRender.ts +24 -5
  58. package/src/ts/markdown/previewRender.ts +5 -1
  59. package/src/ts/markdown/setLute.ts +1 -0
  60. package/src/ts/outline/index.ts +72 -0
  61. package/src/ts/preview/index.ts +1 -1
  62. package/src/ts/sv/index.ts +1 -0
  63. package/src/ts/sv/process.ts +3 -0
  64. package/src/ts/tip/index.ts +25 -9
  65. package/src/ts/toolbar/CodeTheme.ts +2 -0
  66. package/src/ts/toolbar/ContentTheme.ts +2 -0
  67. package/src/ts/toolbar/EditMode.ts +4 -0
  68. package/src/ts/toolbar/Headings.ts +4 -6
  69. package/src/ts/toolbar/Outline.ts +2 -0
  70. package/src/ts/toolbar/Record.ts +5 -0
  71. package/src/ts/ui/initUI.ts +9 -1
  72. package/src/ts/ui/setPreviewMode.ts +2 -0
  73. package/src/ts/undo/index.ts +14 -4
  74. package/src/ts/upload/index.ts +1 -1
  75. package/src/ts/upload/linkToImg.ts +91 -0
  76. package/src/ts/util/Options.ts +2 -0
  77. package/src/ts/util/addScript.ts +15 -24
  78. package/src/ts/util/compatibility.ts +2 -2
  79. package/src/ts/util/editorCommonEvent.ts +47 -4
  80. package/src/ts/util/expandHTMLTags.ts +36 -0
  81. package/src/ts/util/fixBrowserBehavior.ts +29 -61
  82. package/src/ts/util/imageDrag.ts +108 -0
  83. package/src/ts/util/notifyConfigChange.ts +16 -0
  84. package/src/ts/util/pairedSymbols.ts +109 -0
  85. package/src/ts/util/pasteIntoTable.ts +136 -0
  86. package/src/ts/util/pasteLink.ts +51 -0
  87. package/src/ts/util/processCode.ts +33 -8
  88. package/src/ts/util/selection.ts +11 -2
  89. package/src/ts/util/tableWheel.ts +31 -0
  90. package/src/ts/util/taskNavigation.ts +113 -0
  91. package/src/ts/util/toc.ts +9 -3
  92. package/src/ts/wysiwyg/afterRenderEvent.ts +3 -0
  93. package/src/ts/wysiwyg/highlightToolbarWYSIWYG.ts +5 -4
  94. package/src/ts/wysiwyg/index.ts +17 -6
  95. package/src/ts/wysiwyg/setHeading.ts +4 -0
  96. package/dist/js/graphviz/full.render.js +0 -90
@@ -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,9 @@ 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
+ if (typeof event !== "string") {
146
+ notifyConfigChange(vditor);
147
+ }
144
148
  };
145
149
 
146
150
  export class EditMode extends MenuItem {
@@ -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]);
@@ -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")
@@ -35,6 +39,7 @@ export const initUI = (vditor: IVditor) => {
35
39
 
36
40
  if (vditor.options.outline.position === "left") {
37
41
  contentElement.appendChild(vditor.outline.element);
42
+ contentElement.appendChild(vditor.outline.resizeElement);
38
43
  }
39
44
 
40
45
  contentElement.appendChild(vditor.wysiwyg.element.parentElement);
@@ -51,6 +56,7 @@ export const initUI = (vditor: IVditor) => {
51
56
 
52
57
  if (vditor.options.outline.position === "right") {
53
58
  vditor.outline.element.classList.add("vditor-outline--right");
59
+ contentElement.appendChild(vditor.outline.resizeElement);
54
60
  contentElement.appendChild(vditor.outline.element);
55
61
  }
56
62
 
@@ -67,6 +73,8 @@ export const initUI = (vditor: IVditor) => {
67
73
  contentElement.appendChild(vditor.tip.element);
68
74
 
69
75
  vditor.element.appendChild(contentElement);
76
+ vditor.outline.bindResize(vditor);
77
+ bindTableWheel(vditor.element);
70
78
 
71
79
  contentElement.addEventListener("click", () => {
72
80
  hidePanel(vditor, ["subToolbar"]);
@@ -85,7 +93,7 @@ export const initUI = (vditor: IVditor) => {
85
93
  if (navigator.userAgent.indexOf("iPhone") > -1 && typeof window.visualViewport !== "undefined") {
86
94
  // https://github.com/Vanessa219/vditor/issues/379
87
95
  let pendingUpdate = false;
88
- const viewportHandler = (event: Event) => {
96
+ const viewportHandler = () => {
89
97
  if (pendingUpdate) {
90
98
  return;
91
99
  }
@@ -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
  };
@@ -55,10 +55,11 @@ class Undo {
55
55
  }
56
56
 
57
57
  public undo(vditor: IVditor) {
58
- if (vditor.currentMode === "sv" ? vditor.sv.element.disabled :
59
- 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")) {
60
60
  return;
61
61
  }
62
+ this.flushPendingUndoStack(vditor);
62
63
  if (this[vditor.currentMode].undoStack.length < 2) {
63
64
  return;
64
65
  }
@@ -74,10 +75,11 @@ class Undo {
74
75
  }
75
76
 
76
77
  public redo(vditor: IVditor) {
77
- if (vditor.currentMode === "sv" ? vditor.sv.element.disabled :
78
- 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")) {
79
80
  return;
80
81
  }
82
+ this.flushPendingUndoStack(vditor);
81
83
  const state = this[vditor.currentMode].redoStack.pop();
82
84
  if (!state) {
83
85
  return;
@@ -86,6 +88,14 @@ class Undo {
86
88
  this.renderDiff(state, vditor, true);
87
89
  }
88
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
+
89
99
  public recordFirstPosition(vditor: IVditor, event: KeyboardEvent) {
90
100
  if (vditor.currentMode !== "sv" && getSelection().rangeCount === 0) {
91
101
  return;
@@ -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,
@@ -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
  };
@@ -9,7 +9,7 @@ export const isFirefox = () => {
9
9
  export const accessLocalStorage = () => {
10
10
  try {
11
11
  return typeof localStorage !== "undefined";
12
- } catch (e) {
12
+ } catch {
13
13
  return false;
14
14
  }
15
15
  };
@@ -24,7 +24,7 @@ export const getEventName = () => {
24
24
  };
25
25
 
26
26
  // 区别 mac 上的 ctrl 和 meta
27
- export const isCtrl = (event: KeyboardEvent) => {
27
+ export const isCtrl = (event: Pick<KeyboardEvent, "ctrlKey" | "metaKey">) => {
28
28
  if (navigator.platform.toUpperCase().indexOf("MAC") >= 0) {
29
29
  // mac
30
30
  if (event.metaKey && !event.ctrlKey) {
@@ -15,7 +15,10 @@ import {execAfterRender, paste} from "./fixBrowserBehavior";
15
15
  import {getSelectText} from "./getSelectText";
16
16
  import {hasClosestByAttribute, hasClosestByMatchTag} from "./hasClosest";
17
17
  import {matchHotKey} from "./hotKey";
18
- import {getCursorPosition, getEditorRange} from "./selection";
18
+ import {getCursorPosition, getEditorRange, setSelectionFocus} from "./selection";
19
+ import {deleteEmptyPair, wrapSelection} from "./pairedSymbols";
20
+ import {bindTaskNavigation} from "./taskNavigation";
21
+ import {bindImageDrag} from "./imageDrag";
19
22
 
20
23
  export const focusEvent = (vditor: IVditor, editorElement: HTMLElement) => {
21
24
  editorElement.addEventListener("focus", () => {
@@ -61,12 +64,16 @@ export const blurEvent = (vditor: IVditor, editorElement: HTMLElement) => {
61
64
  export const dropEvent = (vditor: IVditor, editorElement: HTMLElement, pasteCode = (code: string) => {
62
65
  document.execCommand("insertHTML", false, code);
63
66
  }) => {
67
+ const dropImage = bindImageDrag(vditor, editorElement);
64
68
  editorElement.addEventListener("dragstart", (event) => {
65
69
  // 选中编辑器中的文字进行拖拽
66
70
  event.dataTransfer.setData(Constants.DROP_EDITOR, Constants.DROP_EDITOR);
67
71
  });
68
72
  editorElement.addEventListener("drop",
69
- (event: ClipboardEvent & { dataTransfer?: DataTransfer, target: HTMLElement }) => {
73
+ (event: DragEvent & { target: HTMLElement }) => {
74
+ if (dropImage(event)) {
75
+ return;
76
+ }
70
77
  if (event.dataTransfer.getData(Constants.DROP_EDITOR)) {
71
78
  // 编辑器内选中文字拖拽
72
79
  execAfterRender(vditor);
@@ -122,6 +129,33 @@ export const scrollCenter = (vditor: IVditor) => {
122
129
  };
123
130
 
124
131
  export const hotkeyEvent = (vditor: IVditor, editorElement: HTMLElement) => {
132
+ bindTaskNavigation(vditor, editorElement);
133
+ editorElement.addEventListener("beforeinput", (event: InputEvent) => {
134
+ if (!event.cancelable || event.defaultPrevented || event.isComposing) {
135
+ return;
136
+ }
137
+ if (event.inputType === "historyUndo" || event.inputType === "historyRedo") {
138
+ if (vditor[vditor.currentMode].composingLock) {
139
+ return;
140
+ }
141
+ event.preventDefault();
142
+ if (event.inputType === "historyUndo") {
143
+ vditor.undo.undo(vditor);
144
+ } else {
145
+ vditor.undo.redo(vditor);
146
+ }
147
+ return;
148
+ }
149
+ if ((event.inputType === "insertText" && wrapSelection(vditor, event.data)) ||
150
+ (["deleteContentBackward", "deleteContentForward"].includes(event.inputType) && deleteEmptyPair(vditor))) {
151
+ event.preventDefault();
152
+ }
153
+ });
154
+ editorElement.addEventListener("keyup", (event: KeyboardEvent) => {
155
+ if (!event.isComposing && /^(Arrow(Up|Down|Left|Right)|Home|End|PageUp|PageDown)$/.test(event.key)) {
156
+ scrollCenter(vditor);
157
+ }
158
+ });
125
159
  editorElement.addEventListener("keydown", (event: KeyboardEvent & { target: HTMLElement }) => {
126
160
  if (!event.isComposing && vditor.options.keydown) {
127
161
  vditor.options.keydown(event);
@@ -152,6 +186,15 @@ export const hotkeyEvent = (vditor: IVditor, editorElement: HTMLElement) => {
152
186
  }
153
187
  }
154
188
 
189
+ if (!event.isComposing && !event.defaultPrevented && vditor.currentMode !== "sv" && matchHotKey("⌘A", event)) {
190
+ // 目录等不可编辑节点也属于正文,全选需覆盖整个编辑区。
191
+ const range = editorElement.ownerDocument.createRange();
192
+ range.selectNodeContents(editorElement);
193
+ setSelectionFocus(range);
194
+ event.preventDefault();
195
+ return;
196
+ }
197
+
155
198
  if (vditor.options.ctrlEnter && matchHotKey("⌘Enter", event)) {
156
199
  vditor.options.ctrlEnter(getMarkdown(vditor));
157
200
  event.preventDefault();
@@ -159,14 +202,14 @@ export const hotkeyEvent = (vditor: IVditor, editorElement: HTMLElement) => {
159
202
  }
160
203
 
161
204
  // undo
162
- if (matchHotKey("⌘Z", event) && !vditor.toolbar.elements.undo) {
205
+ if (!event.isComposing && matchHotKey("⌘Z", event)) {
163
206
  vditor.undo.undo(vditor);
164
207
  event.preventDefault();
165
208
  return;
166
209
  }
167
210
 
168
211
  // redo
169
- if (matchHotKey("⌘Y", event) && !vditor.toolbar.elements.redo) {
212
+ if (!event.isComposing && matchHotKey("⌘Y", event)) {
170
213
  vditor.undo.redo(vditor);
171
214
  event.preventDefault();
172
215
  return;
@@ -0,0 +1,36 @@
1
+ const voidTags = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta",
2
+ "param", "source", "track", "wbr"]);
3
+
4
+ export const expandHTMLTags = (root: HTMLElement, range: Range, className: string) => {
5
+ const stacks = new Map<Element, Array<{name: string; element: Element}>>();
6
+ root.querySelectorAll('[data-type="html-inline"]').forEach(element => {
7
+ const tag = element.textContent.replace(/[\u200b\ufeff]/g, "").trim();
8
+ const match = /^<(\/?)([a-z][\w:-]*)(?:\s[^]*)?>$/i.exec(tag);
9
+ if (!match || /\/\s*>$/.test(tag)) {
10
+ return;
11
+ }
12
+ const name = match[2].toLowerCase();
13
+ if (voidTags.has(name)) {
14
+ return;
15
+ }
16
+ const block = element.closest('[data-block="0"]') || root;
17
+ const stack = stacks.get(block) || [];
18
+ stacks.set(block, stack);
19
+ if (!match[1]) {
20
+ stack.push({name, element});
21
+ return;
22
+ }
23
+ if (stack[stack.length - 1]?.name !== name) {
24
+ return;
25
+ }
26
+ const opening = stack.pop().element;
27
+ const contents = document.createRange();
28
+ contents.setStartBefore(opening);
29
+ contents.setEndAfter(element);
30
+ if (contents.isPointInRange(range.startContainer, range.startOffset) &&
31
+ contents.isPointInRange(range.endContainer, range.endOffset)) {
32
+ opening.classList.add(className);
33
+ element.classList.add(className);
34
+ }
35
+ });
36
+ };