vditor 4.0.2-alpha.0 → 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 +7338 -5352
  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 +4 -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 +124 -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
@@ -51,6 +51,10 @@
51
51
  padding-right: 0 !important;
52
52
  }
53
53
 
54
+ &__kbd-marker:not(.vditor-wysiwyg__kbd-marker--expand) {
55
+ display: none;
56
+ }
57
+
54
58
  &__block {
55
59
  // Source visibility is serialized both by Lute and by CSSStyleDeclaration.
56
60
  // Keep the preview laid out for asynchronous wrapped line-number measurements.
@@ -14,6 +14,8 @@
14
14
  @max-width: 520px;
15
15
 
16
16
  .vditor {
17
+ --find-background-color: #f6c644;
18
+ --find-text-color: #24292e;
17
19
  --border-color: #d1d5da;
18
20
  --second-color: rgba(88, 96, 105, 0.36);
19
21
 
@@ -93,6 +95,7 @@
93
95
 
94
96
  @import "_tooltipped";
95
97
  @import "_panel";
98
+ @import "_find";
96
99
  @import "_toolbar";
97
100
  @import "_content";
98
101
  @import "_hint";
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ import {input as irInput} from "./ts/ir/input";
8
8
  import {processAfterRender} from "./ts/ir/process";
9
9
  import {getHTML} from "./ts/markdown/getHTML";
10
10
  import {getMarkdown} from "./ts/markdown/getMarkdown";
11
+ import {getOutlineHeadings} from "./ts/markdown/outlineRender";
11
12
  import {setLute} from "./ts/markdown/setLute";
12
13
  import {Outline} from "./ts/outline/index";
13
14
  import {Preview} from "./ts/preview/index";
@@ -25,17 +26,20 @@ import {setPreviewMode} from "./ts/ui/setPreviewMode";
25
26
  import {setTheme} from "./ts/ui/setTheme";
26
27
  import {Undo} from "./ts/undo/index";
27
28
  import {Upload} from "./ts/upload/index";
28
- import {addScript, addScriptSync} from "./ts/util/addScript";
29
+ import {addScript} from "./ts/util/addScript";
29
30
  import {getSelectText} from "./ts/util/getSelectText";
30
31
  import {Options} from "./ts/util/Options";
31
32
  import {processCodeRender} from "./ts/util/processCode";
33
+ import {notifyConfigChange} from "./ts/util/notifyConfigChange";
32
34
  import {getCursorPosition, getEditorRange, insertHTML} from "./ts/util/selection";
33
35
  import {afterRenderEvent} from "./ts/wysiwyg/afterRenderEvent";
34
36
  import {WYSIWYG} from "./ts/wysiwyg/index";
35
37
  import {input} from "./ts/wysiwyg/input";
36
38
  import {renderDomByMd} from "./ts/wysiwyg/renderDomByMd";
39
+ import {renderToc} from "./ts/util/toc";
37
40
  import {execAfterRender, insertEmptyBlock} from "./ts/util/fixBrowserBehavior";
38
41
  import {accessLocalStorage} from "./ts/util/compatibility";
42
+ import {Find} from "./ts/find/index";
39
43
 
40
44
  class Vditor extends VditorMethod {
41
45
  public readonly version: string;
@@ -88,8 +92,8 @@ class Vditor extends VditorMethod {
88
92
  });
89
93
  addScript(`${mergedOptions.cdn}/dist/js/i18n/${mergedOptions.lang}.js`, i18nScriptID).then(() => {
90
94
  this.init(id as HTMLElement, mergedOptions);
91
- }).catch(error => {
92
- this.showErrorTip(`GET ${mergedOptions.cdn}/dist/js/i18n/${mergedOptions.lang}.js net::ERR_ABORTED 404 (Not Found)`);
95
+ }, () => {
96
+ this.showErrorTip(`Failed to load ${mergedOptions.cdn}/dist/js/i18n/${mergedOptions.lang}.js`);
93
97
  });
94
98
  }
95
99
  } else {
@@ -125,6 +129,7 @@ class Vditor extends VditorMethod {
125
129
  this.vditor.options.preview.hljs.style = codeTheme;
126
130
  setCodeTheme(codeTheme, this.vditor.options.cdn);
127
131
  }
132
+ notifyConfigChange(this.vditor);
128
133
  }
129
134
 
130
135
  /** 获取 Markdown 内容 */
@@ -132,6 +137,34 @@ class Vditor extends VditorMethod {
132
137
  return getMarkdown(this.vditor);
133
138
  }
134
139
 
140
+ public openFind() {
141
+ this.vditor.find.open();
142
+ }
143
+
144
+ public closeFind() {
145
+ this.vditor.find.close();
146
+ }
147
+
148
+ public find(query: string, options?: IFindOptions): IFindResult {
149
+ return this.vditor.find.find(query, options);
150
+ }
151
+
152
+ public replace(query: string, replacement: string, options?: IFindOptions): number {
153
+ return this.vditor.find.replace(query, replacement, options);
154
+ }
155
+
156
+ public getOutline(): IOutlineItem[] {
157
+ const content = document.createElement("div");
158
+ content.innerHTML = getHTML(this.vditor);
159
+ const rendered = getOutlineHeadings(content);
160
+ const source = this.vditor.currentMode === "sv" ? content : this.vditor[this.vditor.currentMode].element;
161
+ return getOutlineHeadings(source, true).map((heading, index) => ({
162
+ text: (rendered[index] || heading).textContent.replace(/\u200b/g, ""),
163
+ level: Number(heading.tagName.substring(1)),
164
+ id: heading.id,
165
+ }));
166
+ }
167
+
135
168
  /** 获取编辑器当前编辑模式 */
136
169
  public getCurrentMode() {
137
170
  return this.vditor.currentMode;
@@ -344,6 +377,7 @@ class Vditor extends VditorMethod {
344
377
 
345
378
  /** 设置编辑器内容 */
346
379
  public setValue(markdown: string, clearStack = false) {
380
+ this.vditor.find.close();
347
381
  if (this.vditor.currentMode === "sv") {
348
382
  this.vditor.sv.element.value = markdown;
349
383
  processSVAfterRender(this.vditor, {
@@ -371,7 +405,11 @@ class Vditor extends VditorMethod {
371
405
  });
372
406
  }
373
407
 
374
- this.vditor.outline.render(this.vditor);
408
+ if (this.vditor.currentMode === "sv") {
409
+ this.vditor.outline.render(this.vditor);
410
+ } else {
411
+ renderToc(this.vditor);
412
+ }
375
413
 
376
414
  if (!markdown) {
377
415
  hidePanel(this.vditor, ["emoji", "headings", "submenu", "hint"]);
@@ -398,6 +436,9 @@ class Vditor extends VditorMethod {
398
436
 
399
437
  /** 销毁编辑器 */
400
438
  public destroy() {
439
+ this.vditor.find.destroy();
440
+ this.vditor.hint.destroy();
441
+ this.vditor.tip.hide();
401
442
  this.vditor.element.innerHTML = this.vditor.originalInnerHTML;
402
443
  this.vditor.element.classList.remove("vditor");
403
444
  this.vditor.element.removeAttribute("style");
@@ -528,7 +569,7 @@ class Vditor extends VditorMethod {
528
569
  options: mergedOptions,
529
570
  originalInnerHTML: id.innerHTML,
530
571
  outline: new Outline(window.VditorI18n.outline),
531
- tip: new Tip(),
572
+ tip: new Tip(mergedOptions.tips),
532
573
  };
533
574
 
534
575
  this.vditor.sv = new Editor(this.vditor);
@@ -536,6 +577,7 @@ class Vditor extends VditorMethod {
536
577
  this.vditor.wysiwyg = new WYSIWYG(this.vditor);
537
578
  this.vditor.ir = new IR(this.vditor);
538
579
  this.vditor.toolbar = new Toolbar(this.vditor);
580
+ this.vditor.find = new Find(this.vditor);
539
581
 
540
582
  if (mergedOptions.resize.enable) {
541
583
  this.vditor.resize = new Resize(this.vditor);
@@ -566,6 +608,7 @@ class Vditor extends VditorMethod {
566
608
  fixTermTypo: this.vditor.options.preview.markdown.fixTermTypo,
567
609
  footnotes: this.vditor.options.preview.markdown.footnotes,
568
610
  headingAnchor: false,
611
+ imgPathAllowSpace: this.vditor.options.preview.markdown.imgPathAllowSpace,
569
612
  inlineMathDigit: this.vditor.options.preview.math.inlineDigit,
570
613
  linkBase: this.vditor.options.preview.markdown.linkBase,
571
614
  linkPrefix: this.vditor.options.preview.markdown.linkPrefix,
@@ -590,7 +633,7 @@ class Vditor extends VditorMethod {
590
633
  }
591
634
  if (mergedOptions.icon) {
592
635
  // 防止初始化 2 个编辑器时加载 2 次
593
- addScriptSync(`${mergedOptions.cdn}/dist/js/icons/${mergedOptions.icon}.js`, "vditorIconScript");
636
+ addScript(`${mergedOptions.cdn}/dist/js/icons/${mergedOptions.icon}.js`, "vditorIconScript");
594
637
  }
595
638
  });
596
639
  }
@@ -57,6 +57,7 @@ export abstract class Constants {
57
57
  codeBlockPreview: true,
58
58
  fixTermTypo: false,
59
59
  footnotes: true,
60
+ imgPathAllowSpace: false,
60
61
  imageCaption: false,
61
62
  linkBase: "",
62
63
  linkPrefix: "",
@@ -80,6 +81,9 @@ export abstract class Constants {
80
81
  inlineDigit: false,
81
82
  macros: {},
82
83
  };
84
+ public static readonly PLANTUML_OPTIONS: IPlantumlOptions = {
85
+ server: "https://www.plantuml.com/plantuml/svg/",
86
+ };
83
87
  public static readonly THEME_OPTIONS = {
84
88
  current: "light",
85
89
  list: {
@@ -0,0 +1,310 @@
1
+ import {getMarkdown} from "../markdown/getMarkdown";
2
+ import {isCtrl} from "../util/compatibility";
3
+ import {execAfterRender} from "../util/fixBrowserBehavior";
4
+ import {normalizeCodeDOM} from "../util/codeDOM";
5
+ import {processCodeRender} from "../util/processCode";
6
+ import {renderToc} from "../util/toc";
7
+ import {renderDomByMd} from "../wysiwyg/renderDomByMd";
8
+ import {countMatches, displayRange, findText, replaceText} from "./text";
9
+
10
+ let instanceCount = 0;
11
+
12
+ export class Find {
13
+ public element: HTMLElement;
14
+ private query: HTMLInputElement;
15
+ private replacement: HTMLInputElement;
16
+ private sensitive: HTMLInputElement;
17
+ private count: HTMLElement;
18
+ private replaceRow: HTMLElement;
19
+ private replaceButtons: HTMLButtonElement[] = [];
20
+ private navigationButtons: HTMLButtonElement[] = [];
21
+ private index = 0;
22
+ private total = 0;
23
+ private composing = false;
24
+ private timer = 0;
25
+ private observer: MutationObserver;
26
+ private events = new AbortController();
27
+ private highlightName = `vditor-find-${++instanceCount}`;
28
+ private highlightStyle: HTMLStyleElement;
29
+ private sourceHighlight: HTMLElement;
30
+ private sourceContent: HTMLElement;
31
+ private sourceMatch: IFindResult["match"];
32
+ private resizeObserver: ResizeObserver;
33
+
34
+ constructor(private vditor: IVditor) {
35
+ const labels = {
36
+ find: "Find", replace: "Replace", replaceAll: "Replace all", matchCase: "Match case",
37
+ previousMatch: "Previous", nextMatch: "Next", close: "Close",
38
+ };
39
+ const label = (key: keyof typeof labels) => vditor.options.i18n?.[key] || window.VditorI18n[key] || labels[key];
40
+ this.element = document.createElement("section");
41
+ this.element.className = "vditor-find";
42
+ this.element.hidden = true;
43
+ this.element.setAttribute("aria-label", label("find"));
44
+ const row = document.createElement("div");
45
+ row.className = "vditor-find__row";
46
+ this.element.appendChild(row);
47
+ this.query = this.input(label("find"));
48
+ this.query.type = "search";
49
+ row.appendChild(this.query);
50
+ this.count = document.createElement("output");
51
+ this.count.setAttribute("aria-live", "polite");
52
+ row.appendChild(this.count);
53
+ this.navigationButtons = [this.button(label("previousMatch"), () => this.move(-1)),
54
+ this.button(label("nextMatch"), () => this.move(1))];
55
+ row.append(...this.navigationButtons);
56
+ const sensitive = document.createElement("label");
57
+ this.sensitive = document.createElement("input");
58
+ this.sensitive.type = "checkbox";
59
+ sensitive.append(this.sensitive, document.createTextNode(label("matchCase")));
60
+ row.appendChild(sensitive);
61
+ const toggle = this.button(label("replace"), () => {
62
+ this.replaceRow.hidden = !this.replaceRow.hidden;
63
+ toggle.setAttribute("aria-expanded", String(!this.replaceRow.hidden));
64
+ if (!this.replaceRow.hidden) this.replacement.focus();
65
+ });
66
+ toggle.setAttribute("aria-expanded", "false");
67
+ row.appendChild(toggle);
68
+ row.appendChild(this.button(label("close"), () => this.close(true)));
69
+ this.replaceRow = document.createElement("div");
70
+ this.replaceRow.className = "vditor-find__row";
71
+ this.replaceRow.hidden = true;
72
+ this.replacement = this.input(label("replace"));
73
+ this.replaceRow.appendChild(this.replacement);
74
+ this.replaceButtons = [this.button(label("replace"), () => this.replaceCurrent(false)),
75
+ this.button(label("replaceAll"), () => this.replaceCurrent(true))];
76
+ this.replaceRow.append(...this.replaceButtons);
77
+ this.element.append(this.replaceRow);
78
+ const signal = this.events.signal;
79
+ this.query.addEventListener("input", () => {
80
+ this.index = 0;
81
+ this.schedule();
82
+ }, {signal});
83
+ this.sensitive.addEventListener("change", () => {
84
+ this.index = 0;
85
+ this.refresh();
86
+ }, {signal});
87
+ this.element.addEventListener("compositionstart", () => {this.composing = true;}, {signal});
88
+ this.element.addEventListener("compositionend", () => {
89
+ this.composing = false;
90
+ this.schedule();
91
+ }, {signal});
92
+ vditor.element.addEventListener("keydown", event => {
93
+ if (event.isComposing || this.composing || vditor[vditor.currentMode].composingLock) return;
94
+ if (isCtrl(event) && !event.altKey && !event.shiftKey && event.key.toLowerCase() === "f") {
95
+ event.preventDefault();
96
+ event.stopPropagation();
97
+ this.open();
98
+ } else if (!this.element.hidden && event.key === "Escape") {
99
+ event.preventDefault();
100
+ event.stopPropagation();
101
+ this.close(true);
102
+ } else if (this.element.contains(event.target as Node) && event.key === "Enter") {
103
+ event.preventDefault();
104
+ event.stopPropagation();
105
+ if (event.target === this.replacement) this.replaceCurrent(false);
106
+ else this.move(event.shiftKey ? -1 : 1);
107
+ }
108
+ }, {capture: true, signal});
109
+ this.observer = new MutationObserver(() => this.schedule());
110
+ for (const mode of ["sv", "ir", "wysiwyg"] as const) {
111
+ this.observer.observe(vditor[mode].element, {childList: true, characterData: true, subtree: true,
112
+ attributes: true, attributeFilter: ["contenteditable", "disabled"]});
113
+ }
114
+ vditor.element.addEventListener("input", event => {
115
+ if (!this.element.contains(event.target as Node)) this.schedule();
116
+ }, {signal});
117
+ vditor.sv.element.addEventListener("scroll", () => this.scrollSourceHighlight(), {signal});
118
+ this.resizeObserver = new ResizeObserver(() => {
119
+ if (this.sourceMatch) this.highlightSource(this.sourceMatch, false);
120
+ });
121
+ this.resizeObserver.observe(vditor.sv.element);
122
+ this.highlightStyle = document.createElement("style");
123
+ this.highlightStyle.textContent = `::highlight(${this.highlightName}) {background-color: var(--find-background-color); color: var(--find-text-color);}`;
124
+ document.head.appendChild(this.highlightStyle);
125
+ }
126
+
127
+ public open() {
128
+ this.element.hidden = false;
129
+ this.refresh();
130
+ this.query.focus();
131
+ this.query.select();
132
+ }
133
+
134
+ public close(focus = false) {
135
+ this.element.hidden = true;
136
+ window.clearTimeout(this.timer);
137
+ this.clearHighlight();
138
+ if (focus) this.vditor[this.vditor.currentMode].element.focus();
139
+ }
140
+
141
+ public refresh() {
142
+ if (this.element.hidden || this.composing || this.vditor[this.vditor.currentMode].composingLock) return;
143
+ window.clearTimeout(this.timer);
144
+ const result = this.find(this.query.value, {caseSensitive: this.sensitive.checked, index: this.index});
145
+ this.index = Math.max(0, result.index);
146
+ this.total = result.total;
147
+ this.count.textContent = `${result.index + 1}/${result.total}`;
148
+ this.navigationButtons.forEach(button => {button.disabled = !result.total;});
149
+ this.replaceButtons.forEach(button => {button.disabled = !result.total || !this.editable();});
150
+ }
151
+
152
+ public find(query: string, options: IFindOptions = {}): IFindResult {
153
+ this.clearHighlight();
154
+ const result = findText(this.vditor[this.vditor.currentMode].element, query,
155
+ !!options.caseSensitive, options.index || 0);
156
+ if (result.match && !this.vditor[this.vditor.currentMode].composingLock) {
157
+ if (this.vditor.currentMode === "sv") {
158
+ const element = this.vditor.sv.element;
159
+ element.setSelectionRange(result.match.start, result.match.end);
160
+ this.highlightSource(result.match);
161
+ } else {
162
+ const range = displayRange(result.match.range);
163
+ const css = window.CSS as typeof CSS & {highlights?: Map<string, unknown>};
164
+ const HighlightClass = (window as unknown as {Highlight?: new(range: Range) => unknown}).Highlight;
165
+ if (css?.highlights && HighlightClass) css.highlights.set(this.highlightName, new HighlightClass(range));
166
+ const rect = range.getBoundingClientRect();
167
+ const element = this.vditor[this.vditor.currentMode].element;
168
+ const viewport = element.getBoundingClientRect();
169
+ if (rect.top < viewport.top || rect.bottom > viewport.bottom) {
170
+ element.scrollTop += rect.top - viewport.top - element.clientHeight / 2;
171
+ }
172
+ }
173
+ }
174
+ return result;
175
+ }
176
+
177
+ public replace(query: string, replacement: string, options: IFindOptions = {}): number {
178
+ if (!this.editable() || this.composing || !query) return 0;
179
+ const vditor = this.vditor;
180
+ const surface = vditor[vditor.currentMode].element;
181
+ const clone = surface.cloneNode(true) as HTMLElement;
182
+ const count = replaceText(clone, query, replacement, !!options.caseSensitive, options.index);
183
+ if (!count) return 0;
184
+ if (vditor.currentMode !== "sv") normalizeCodeDOM(clone);
185
+ const markdown = vditor.currentMode === "sv" ? (clone as HTMLTextAreaElement).value :
186
+ vditor.currentMode === "ir" ? vditor.lute.VditorIRDOM2Md(clone.innerHTML) : vditor.lute.VditorDOM2Md(clone.innerHTML);
187
+ if (markdown === getMarkdown(vditor)) return 0;
188
+ if (vditor.options.beforeReplace?.(markdown) === false) return 0;
189
+ vditor.undo.addToUndoStack(vditor);
190
+ this.clearHighlight();
191
+ const target = vditor[vditor.currentMode].element;
192
+ const renderOptions = {enableAddUndoStack: false, enableHint: false, enableInput: true};
193
+ if (vditor.currentMode === "sv") {
194
+ vditor.sv.element.value = markdown;
195
+ vditor.outline.render(vditor);
196
+ } else if (vditor.currentMode === "wysiwyg") {
197
+ renderDomByMd(vditor, markdown, renderOptions);
198
+ } else {
199
+ target.innerHTML = vditor.lute.Md2VditorIRDOM(markdown);
200
+ target.querySelectorAll(".vditor-ir__preview[data-render='2']").forEach((item: HTMLElement) => {
201
+ processCodeRender(item, vditor);
202
+ });
203
+ }
204
+ renderToc(vditor);
205
+ // 替换独立记录历史,输入通知继续使用各模式的渲染流程。
206
+ if (vditor.currentMode !== "wysiwyg") execAfterRender(vditor, renderOptions);
207
+ vditor.undo.addToUndoStack(vditor);
208
+ this.schedule();
209
+ return count;
210
+ }
211
+
212
+ public destroy() {
213
+ this.close();
214
+ this.events.abort();
215
+ this.observer.disconnect();
216
+ this.resizeObserver.disconnect();
217
+ this.highlightStyle.remove();
218
+ this.element.remove();
219
+ }
220
+
221
+ private editable() {
222
+ const vditor = this.vditor;
223
+ const surface = vditor[vditor.currentMode].element;
224
+ return !vditor[vditor.currentMode].composingLock && !vditor.element.closest("[inert]") &&
225
+ (vditor.currentMode === "sv" ? !vditor.sv.element.disabled : surface.getAttribute("contenteditable") !== "false");
226
+ }
227
+
228
+ private replaceCurrent(all: boolean) {
229
+ const query = this.query.value;
230
+ const replacement = this.replacement.value;
231
+ const count = this.replace(query, replacement, {caseSensitive: this.sensitive.checked,
232
+ index: all ? undefined : this.index});
233
+ if (count && !all) this.index += countMatches(replacement, query, this.sensitive.checked);
234
+ this.refresh();
235
+ }
236
+
237
+ private move(direction: number) {
238
+ this.index = this.total ? (this.index + direction + this.total) % this.total : 0;
239
+ this.refresh();
240
+ }
241
+
242
+ private schedule() {
243
+ if (this.element.hidden) return;
244
+ window.clearTimeout(this.timer);
245
+ this.timer = window.setTimeout(() => this.refresh(), 120);
246
+ }
247
+
248
+ private clearHighlight() {
249
+ (window.CSS as typeof CSS & {highlights?: Map<string, unknown>})?.highlights?.delete(this.highlightName);
250
+ this.sourceHighlight?.remove();
251
+ this.sourceHighlight = undefined;
252
+ this.sourceContent = undefined;
253
+ this.sourceMatch = undefined;
254
+ }
255
+
256
+ private input(label: string) {
257
+ const input = document.createElement("input");
258
+ input.className = "vditor-input";
259
+ input.placeholder = label;
260
+ input.setAttribute("aria-label", label);
261
+ return input;
262
+ }
263
+
264
+ private button(label: string, action: () => void) {
265
+ const button = document.createElement("button");
266
+ button.type = "button";
267
+ button.className = "vditor-button";
268
+ button.textContent = label;
269
+ button.addEventListener("click", action, {signal: this.events.signal});
270
+ return button;
271
+ }
272
+
273
+ private highlightSource(match: IFindResult["match"], reveal = true) {
274
+ const element = this.vditor.sv.element;
275
+ this.sourceMatch = match;
276
+ if (!this.sourceHighlight) {
277
+ this.sourceHighlight = document.createElement("div");
278
+ this.sourceHighlight.className = "vditor-find__source";
279
+ this.sourceHighlight.setAttribute("aria-hidden", "true");
280
+ this.sourceContent = document.createElement("div");
281
+ this.sourceHighlight.appendChild(this.sourceContent);
282
+ element.parentElement.appendChild(this.sourceHighlight);
283
+ }
284
+ const mirror = this.sourceContent;
285
+ const computed = getComputedStyle(element);
286
+ for (const property of ["font-family", "font-size", "font-style", "font-weight", "font-variant", "line-height",
287
+ "letter-spacing", "tab-size", "padding", "word-break", "overflow-wrap", "text-indent", "text-align", "direction"]) {
288
+ mirror.style.setProperty(property, computed.getPropertyValue(property));
289
+ }
290
+ Object.assign(this.sourceHighlight.style, {left: `${element.offsetLeft + element.clientLeft}px`,
291
+ top: `${element.offsetTop + element.clientTop}px`, width: `${element.clientWidth}px`, height: `${element.clientHeight}px`});
292
+ Object.assign(mirror.style, {whiteSpace: element.wrap === "off" ? "pre" : "pre-wrap", width: `${element.clientWidth}px`});
293
+ const marker = document.createElement("mark");
294
+ marker.textContent = element.value.slice(match.start, match.end);
295
+ const lineEnd = element.value.indexOf("\n", match.end);
296
+ mirror.replaceChildren(document.createTextNode(element.value.slice(0, match.start)), marker,
297
+ document.createTextNode(element.value.slice(match.end, lineEnd === -1 ? undefined : lineEnd)));
298
+ const top = marker.getBoundingClientRect().top - mirror.getBoundingClientRect().top;
299
+ if (reveal && (top < element.scrollTop || top > element.scrollTop + element.clientHeight)) {
300
+ element.scrollTop = Math.max(0, top - element.clientHeight / 2);
301
+ }
302
+ this.scrollSourceHighlight();
303
+ }
304
+
305
+ private scrollSourceHighlight() {
306
+ if (!this.sourceContent) return;
307
+ const element = this.vditor.sv.element;
308
+ this.sourceContent.style.transform = `translate(${-element.scrollLeft}px, ${-element.scrollTop}px)`;
309
+ }
310
+ }