vditor 4.0.2-alpha.10 → 4.0.2-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/method.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Vditor v4.0.2-alpha.10 - A markdown editor written in TypeScript.
2
+ * Vditor v4.0.2-alpha.12 - A markdown editor written in TypeScript.
3
3
  *
4
4
  * MIT License
5
5
  *
@@ -2140,7 +2140,7 @@ var graphvizRender = function (element, cdn) {
2140
2140
  /* harmony export */ Y: () => (/* binding */ Constants)
2141
2141
  /* harmony export */ });
2142
2142
  /* unused harmony export VDITOR_VERSION */
2143
- var _VDITOR_VERSION = (/* unused pure expression or super */ null && ("4.0.2-alpha.10"));
2143
+ var _VDITOR_VERSION = (/* unused pure expression or super */ null && ("4.0.2-alpha.12"));
2144
2144
 
2145
2145
  var Constants = /** @class */ (function () {
2146
2146
  function Constants() {
@@ -2188,7 +2188,7 @@ var Constants = /** @class */ (function () {
2188
2188
  // 别名
2189
2189
  "js", "ts", "html", "toml", "c#", "bat"
2190
2190
  ];
2191
- Constants.CDN = "https://unpkg.com/vditor@".concat("4.0.2-alpha.10");
2191
+ Constants.CDN = "https://unpkg.com/vditor@".concat("4.0.2-alpha.12");
2192
2192
  Constants.MARKDOWN_OPTIONS = {
2193
2193
  autoSpace: false,
2194
2194
  callout: true,
@@ -13,6 +13,7 @@ export declare class Find {
13
13
  private total;
14
14
  private composing;
15
15
  private timer;
16
+ private revealPending;
16
17
  private observer;
17
18
  private events;
18
19
  private highlightName;
@@ -24,8 +25,9 @@ export declare class Find {
24
25
  constructor(vditor: IVditor);
25
26
  open(): void;
26
27
  close(focus?: boolean): void;
27
- refresh(): void;
28
+ refresh(reveal?: boolean): void;
28
29
  find(query: string, options?: IFindOptions): IFindResult;
30
+ private findMatch;
29
31
  replace(query: string, replacement: string, options?: IFindOptions): number;
30
32
  destroy(): void;
31
33
  private editable;
@@ -1,5 +1,5 @@
1
1
  export declare const fixGSKeyBackspace: (event: KeyboardEvent, vditor: IVditor, startContainer: Node) => boolean;
2
- export declare const backspaceEmptyHeading: (event: KeyboardEvent, vditor: IVditor, range: Range) => boolean;
2
+ export declare const deleteEmptyHeading: (event: KeyboardEvent, vditor: IVditor, range: Range) => boolean;
3
3
  export declare const fixCJKPosition: (range: Range, vditor: IVditor, event: KeyboardEvent) => void;
4
4
  export declare const fixCursorDownInlineMath: (range: Range, key: string) => void;
5
5
  export declare const insertEmptyBlock: (vditor: IVditor, position: InsertPosition) => void;
@@ -1 +1,2 @@
1
+ export declare const pasteUnfinishedIRLinkDestination: (vditor: IVditor, text: string) => boolean;
1
2
  export declare const getPastedLink: (vditor: IVditor, text: string) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vditor",
3
- "version": "4.0.2-alpha.10",
3
+ "version": "4.0.2-alpha.12",
4
4
  "description": "♏ 易于使用的 Markdown 编辑器,为适配不同的应用场景而生",
5
5
  "author": "Vanessa <v@b3log.org> (http://vanessa.b3log.org)",
6
6
  "homepage": "https://b3log.org/vditor",
@@ -22,6 +22,7 @@ export class Find {
22
22
  private total = 0;
23
23
  private composing = false;
24
24
  private timer = 0;
25
+ private revealPending = false;
25
26
  private observer: MutationObserver;
26
27
  private events = new AbortController();
27
28
  private highlightName = `vditor-find-${++instanceCount}`;
@@ -78,16 +79,16 @@ export class Find {
78
79
  const signal = this.events.signal;
79
80
  this.query.addEventListener("input", () => {
80
81
  this.index = 0;
81
- this.schedule();
82
+ this.schedule(true);
82
83
  }, {signal});
83
84
  this.sensitive.addEventListener("change", () => {
84
85
  this.index = 0;
85
86
  this.refresh();
86
87
  }, {signal});
87
88
  this.element.addEventListener("compositionstart", () => {this.composing = true;}, {signal});
88
- this.element.addEventListener("compositionend", () => {
89
+ this.element.addEventListener("compositionend", event => {
89
90
  this.composing = false;
90
- this.schedule();
91
+ this.schedule(event.target === this.query);
91
92
  }, {signal});
92
93
  vditor.element.addEventListener("keydown", event => {
93
94
  if (event.isComposing || this.composing || vditor[vditor.currentMode].composingLock) return;
@@ -134,14 +135,16 @@ export class Find {
134
135
  public close(focus = false) {
135
136
  this.element.hidden = true;
136
137
  window.clearTimeout(this.timer);
138
+ this.revealPending = false;
137
139
  this.clearHighlight();
138
140
  if (focus) this.vditor[this.vditor.currentMode].element.focus();
139
141
  }
140
142
 
141
- public refresh() {
143
+ public refresh(reveal = true) {
142
144
  if (this.element.hidden || this.composing || this.vditor[this.vditor.currentMode].composingLock) return;
143
145
  window.clearTimeout(this.timer);
144
- const result = this.find(this.query.value, {caseSensitive: this.sensitive.checked, index: this.index});
146
+ this.revealPending = false;
147
+ const result = this.findMatch(this.query.value, {caseSensitive: this.sensitive.checked, index: this.index}, reveal);
145
148
  this.index = Math.max(0, result.index);
146
149
  this.total = result.total;
147
150
  this.count.textContent = `${result.index + 1}/${result.total}`;
@@ -150,24 +153,30 @@ export class Find {
150
153
  }
151
154
 
152
155
  public find(query: string, options: IFindOptions = {}): IFindResult {
156
+ return this.findMatch(query, options, true);
157
+ }
158
+
159
+ private findMatch(query: string, options: IFindOptions, reveal: boolean): IFindResult {
153
160
  this.clearHighlight();
154
161
  const result = findText(this.vditor[this.vditor.currentMode].element, query,
155
162
  !!options.caseSensitive, options.index || 0);
156
163
  if (result.match && !this.vditor[this.vditor.currentMode].composingLock) {
157
164
  if (this.vditor.currentMode === "sv") {
158
165
  const element = this.vditor.sv.element;
159
- element.setSelectionRange(result.match.start, result.match.end);
160
- this.highlightSource(result.match);
166
+ if (reveal) element.setSelectionRange(result.match.start, result.match.end);
167
+ this.highlightSource(result.match, reveal);
161
168
  } else {
162
169
  const range = displayRange(result.match.range);
163
170
  const css = window.CSS as typeof CSS & {highlights?: Map<string, unknown>};
164
171
  const HighlightClass = (window as unknown as {Highlight?: new(range: Range) => unknown}).Highlight;
165
172
  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;
173
+ if (reveal) {
174
+ const rect = range.getBoundingClientRect();
175
+ const element = this.vditor[this.vditor.currentMode].element;
176
+ const viewport = element.getBoundingClientRect();
177
+ if (rect.top < viewport.top || rect.bottom > viewport.bottom) {
178
+ element.scrollTop += rect.top - viewport.top - element.clientHeight / 2;
179
+ }
171
180
  }
172
181
  }
173
182
  }
@@ -239,10 +248,12 @@ export class Find {
239
248
  this.refresh();
240
249
  }
241
250
 
242
- private schedule() {
251
+ private schedule(reveal = false) {
243
252
  if (this.element.hidden) return;
253
+ // 查询输入和正文更新共用定时器,查询框仍有焦点时才定位待显示的命中。
254
+ this.revealPending ||= reveal;
244
255
  window.clearTimeout(this.timer);
245
- this.timer = window.setTimeout(() => this.refresh(), 120);
256
+ this.timer = window.setTimeout(() => this.refresh(this.revealPending && document.activeElement === this.query), 120);
246
257
  }
247
258
 
248
259
  private clearHighlight() {
@@ -270,7 +281,7 @@ export class Find {
270
281
  return button;
271
282
  }
272
283
 
273
- private highlightSource(match: IFindResult["match"], reveal = true) {
284
+ private highlightSource(match: IFindResult["match"], reveal: boolean) {
274
285
  const element = this.vditor.sv.element;
275
286
  this.sourceMatch = match;
276
287
  if (!this.sourceHighlight) {
@@ -2,7 +2,7 @@ import {Constants} from "../constants";
2
2
  import {hidePanel} from "../toolbar/setToolbar";
3
3
  import {isCtrl} from "../util/compatibility";
4
4
  import {
5
- backspaceEmptyHeading,
5
+ deleteEmptyHeading,
6
6
  fixBlockquote, fixCJKPosition,
7
7
  fixCodeBlock, fixCursorDownInlineMath,
8
8
  fixDelete, fixFirefoxArrowUpTable, fixGSKeyBackspace, fixHR,
@@ -41,7 +41,7 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
41
41
  const range = getEditorRange(vditor);
42
42
  const startContainer = range.startContainer;
43
43
 
44
- if (backspaceEmptyHeading(event, vditor, range)) {
44
+ if (deleteEmptyHeading(event, vditor, range)) {
45
45
  return true;
46
46
  }
47
47
 
@@ -21,7 +21,7 @@ import {hasClosestByHeadings} from "./hasClosestByHeadings";
21
21
  import {highlightToolbar} from "./highlightToolbar";
22
22
  import {matchHotKey} from "./hotKey";
23
23
  import {isPlainTextPaste, processCodeRender, processPasteCode} from "./processCode";
24
- import {getPastedLink} from "./pasteLink";
24
+ import {getPastedLink, pasteUnfinishedIRLinkDestination} from "./pasteLink";
25
25
  import {renderToc} from "./toc";
26
26
  import {
27
27
  getEditorRange,
@@ -44,22 +44,35 @@ export const fixGSKeyBackspace = (event: KeyboardEvent, vditor: IVditor, startCo
44
44
  return true;
45
45
  };
46
46
 
47
- export const backspaceEmptyHeading = (event: KeyboardEvent, vditor: IVditor, range: Range) => {
48
- if (event.key !== "Backspace" || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey ||
49
- event.defaultPrevented || !range.collapsed) {
47
+ export const deleteEmptyHeading = (event: KeyboardEvent, vditor: IVditor, range: Range) => {
48
+ if ((event.key !== "Backspace" && (event.key !== "Delete" || range.collapsed)) ||
49
+ event.ctrlKey || event.metaKey || event.shiftKey || event.altKey || event.defaultPrevented) {
50
50
  return false;
51
51
  }
52
52
  const heading = hasClosestByHeadings(range.startContainer);
53
- if (!heading) {
53
+ if (!heading || !heading.contains(range.endContainer)) {
54
54
  return false;
55
55
  }
56
- const firstElement = heading.firstElementChild;
57
- const marker = firstElement?.getAttribute("data-type") === "heading-marker" ? firstElement : null;
58
- const empty = Array.from(heading.childNodes).every((node) => node === marker ||
56
+ const isEmpty = (nodes: NodeListOf<ChildNode>) => Array.from(nodes).every((node) =>
57
+ (node.nodeType === Node.ELEMENT_NODE && (node as Element).getAttribute("data-type") === "heading-marker") ||
59
58
  node.nodeName === "BR" || node.nodeName === "WBR" ||
60
59
  (node.nodeType === Node.TEXT_NODE && node.textContent.split(Constants.ZWSP).join("").trim() === ""));
61
- if (!empty) {
62
- return false;
60
+ if (range.collapsed) {
61
+ if (!isEmpty(heading.childNodes)) {
62
+ return false;
63
+ }
64
+ } else {
65
+ const remainder = range.cloneRange();
66
+ remainder.selectNodeContents(heading);
67
+ remainder.setEnd(range.startContainer, range.startOffset);
68
+ if (!isEmpty(remainder.cloneContents().childNodes)) {
69
+ return false;
70
+ }
71
+ remainder.selectNodeContents(heading);
72
+ remainder.setStart(range.endContainer, range.endOffset);
73
+ if (!isEmpty(remainder.cloneContents().childNodes)) {
74
+ return false;
75
+ }
63
76
  }
64
77
 
65
78
  const paragraph = heading.ownerDocument.createElement("p");
@@ -1662,6 +1675,12 @@ export const paste = async (vditor: IVditor, event: (ClipboardEvent | DragEvent)
1662
1675
  }
1663
1676
  }
1664
1677
  } else if (textPlain.trim() !== "" && files.length === 0) {
1678
+ if (pasteUnfinishedIRLinkDestination(vditor, textPlain)) {
1679
+ // 结束括号输入后由即时渲染重新解析整条链接。
1680
+ vditor.wysiwyg.triggerRemoveComment(vditor);
1681
+ execAfterRender(vditor);
1682
+ return;
1683
+ }
1665
1684
  textPlain = getPastedLink(vditor, textPlain);
1666
1685
  if (pasteIntoTable(vditor, "", textPlain)) {
1667
1686
  // 单元格填充与普通粘贴共用后续规范化和撤销记录。
@@ -1,5 +1,5 @@
1
1
  import {getEditorRange} from "./selection";
2
- import {hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "./hasClosest";
2
+ import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "./hasClosest";
3
3
 
4
4
  interface IMarkdownNode {
5
5
  Type: string;
@@ -15,6 +15,39 @@ const containsPlainText = (node: IMarkdownNode, marker: string): boolean => {
15
15
  !!node.Children?.some(child => containsPlainText(child, marker));
16
16
  };
17
17
 
18
+ // 百分号编码语法字符和实体中的非保留字符,保持查询参数分隔符和 Markdown 往返语义。
19
+ const escapeDestination = (text: string) => {
20
+ const encode = (character: string) => "%" + character.charCodeAt(0).toString(16).toUpperCase();
21
+ return text.replace(/[\\()<>|]/g, encode).replace(
22
+ /&(?:#[xX]?)?([a-zA-Z0-9])(?=[a-zA-Z0-9]*;)/g,
23
+ (match, character: string) => match.slice(0, -1) + encode(character));
24
+ };
25
+
26
+ export const pasteUnfinishedIRLinkDestination = (vditor: IVditor, text: string): boolean => {
27
+ if (vditor.currentMode !== "ir" || !vditor.lute.IsValidLinkDest(text)) {
28
+ return false;
29
+ }
30
+ const range = getEditorRange(vditor);
31
+ const block = hasClosestBlock(range.startContainer);
32
+ if (!block || !block.contains(range.endContainer) ||
33
+ hasClosestByAttribute(range.startContainer, "data-type", "a") ||
34
+ hasClosestByMatchTag(range.startContainer, "CODE")) {
35
+ return false;
36
+ }
37
+ const prefix = range.cloneRange();
38
+ prefix.selectNodeContents(block);
39
+ prefix.setEnd(range.startContainer, range.startOffset);
40
+ if (!/(?:^|[^\\])!?\[(?:\\.|[^[\]\\\r\n])+\]\(\s*$/.test(prefix.toString())) {
41
+ return false;
42
+ }
43
+ const destination = document.createTextNode(escapeDestination(text));
44
+ range.deleteContents();
45
+ range.insertNode(destination);
46
+ range.setStartAfter(destination);
47
+ range.collapse(true);
48
+ return true;
49
+ };
50
+
18
51
  export const getPastedLink = (vditor: IVditor, text: string): string => {
19
52
  if (!vditor.lute.IsValidLinkDest(text)) {
20
53
  return text;
@@ -42,10 +75,6 @@ export const getPastedLink = (vditor: IVditor, text: string): string => {
42
75
  selectedText = range.toString();
43
76
  }
44
77
  const title = (selectedText || text).replace(/[\\`*_[\]!~^=$&<>|:]/g, "\\$&");
45
- const encode = (character: string) => "%" + character.charCodeAt(0).toString(16).toUpperCase();
46
- // 百分号编码语法字符和实体中的非保留字符,保持查询参数分隔符和 Markdown 往返语义。
47
- const destination = text.replace(/[\\()<>|]/g, encode).replace(
48
- /&(?:#[xX]?)?([a-zA-Z0-9])(?=[a-zA-Z0-9]*;)/g,
49
- (match, character: string) => match.slice(0, -1) + encode(character));
78
+ const destination = escapeDestination(text);
50
79
  return `[${title}](${destination})`;
51
80
  };
@@ -2,7 +2,7 @@ import {Constants} from "../constants";
2
2
  import {isCtrl, isFirefox} from "../util/compatibility";
3
3
  import {scrollCenter} from "../util/editorCommonEvent";
4
4
  import {
5
- backspaceEmptyHeading,
5
+ deleteEmptyHeading,
6
6
  fixBlockquote, fixCJKPosition,
7
7
  fixCodeBlock, fixCursorDownInlineMath, fixDelete, fixFirefoxArrowUpTable, fixGSKeyBackspace, fixHR,
8
8
  fixList,
@@ -85,7 +85,7 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
85
85
  const range = getEditorRange(vditor);
86
86
  const startContainer = range.startContainer;
87
87
 
88
- if (backspaceEmptyHeading(event, vditor, range)) {
88
+ if (deleteEmptyHeading(event, vditor, range)) {
89
89
  return true;
90
90
  }
91
91