vditor 4.0.2-alpha.11 → 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.
@@ -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