vditor 3.9.9 → 3.10.2

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/src/index.ts CHANGED
@@ -266,7 +266,7 @@ class Vditor extends VditorMethod {
266
266
  inputEvent(this.vditor);
267
267
  }
268
268
  } else if (this.vditor.currentMode === "wysiwyg") {
269
- this.vditor.wysiwyg.preventInput = true;
269
+ // 由于 https://github.com/Vanessa219/vditor/issues/1566 不能使用 this.vditor.wysiwyg.preventInput = true;
270
270
  if (render) {
271
271
  input(this.vditor, getSelection().getRangeAt(0));
272
272
  }
@@ -20,6 +20,7 @@ import {expandMarker} from "./expandMarker";
20
20
  import {highlightToolbarIR} from "./highlightToolbarIR";
21
21
  import {input} from "./input";
22
22
  import {processAfterRender, processHint} from "./process";
23
+ import {hidePanel} from "../toolbar/setToolbar";
23
24
 
24
25
  class IR {
25
26
  public range: Range;
@@ -74,6 +75,10 @@ class IR {
74
75
  });
75
76
  });
76
77
 
78
+ this.element.addEventListener("scroll", () => {
79
+ hidePanel(vditor, ["hint"]);
80
+ });
81
+
77
82
  this.element.addEventListener("compositionstart", (event: InputEvent) => {
78
83
  this.composingLock = true;
79
84
  });
@@ -1,7 +1,7 @@
1
1
  import {code160to32} from "../util/code160to32";
2
2
  import {Constants} from "../constants";
3
3
 
4
- export const codeRender = (element: HTMLElement) => {
4
+ export const codeRender = (element: HTMLElement, option?: IHljs) => {
5
5
  Array.from<HTMLElement>(element.querySelectorAll("pre > code")).filter((e, index) => {
6
6
  if (e.parentElement.classList.contains("vditor-wysiwyg__pre") ||
7
7
  e.parentElement.classList.contains("vditor-ir__marker--pre")) {
@@ -49,7 +49,9 @@ onclick="this.previousElementSibling.select();document.execCommand('copy');this.
49
49
  const textarea = document.createElement("textarea");
50
50
  textarea.value = code160to32(codeText);
51
51
  divElement.insertAdjacentElement("afterbegin", textarea);
52
-
52
+ if (option && option.renderMenu) {
53
+ option.renderMenu(e, divElement);
54
+ }
53
55
  e.before(divElement);
54
56
  e.style.maxHeight = (window.outerHeight - 40) + "px";
55
57
  // https://github.com/Vanessa219/vditor/issues/1356
@@ -1,3 +1,5 @@
1
+ import {getSearch} from "../util/function";
2
+
1
3
  const videoRender = (element: HTMLElement, url: string) => {
2
4
  element.insertAdjacentHTML("afterend", `<video controls="controls" src="${url}"></video>`);
3
5
  element.remove();
@@ -46,10 +48,35 @@ const iframeRender = (element: HTMLElement, url: string) => {
46
48
  `<iframe class="iframe__video"
47
49
  src="https://www.dailymotion.com/embed/video/${dailymotionMatch[2]}"></iframe>`);
48
50
  element.remove();
49
- } else if (bilibiliMatch && bilibiliMatch[1]) {
51
+ } else if (url.indexOf("bilibili.com") > -1 && (url.indexOf("bvid=") > -1 || (bilibiliMatch && bilibiliMatch[1]))) {
52
+ const params: IObject = {
53
+ bvid: getSearch("bvid", url) || (bilibiliMatch && bilibiliMatch[1]),
54
+ page: "1",
55
+ high_quality: "1",
56
+ as_wide: "1",
57
+ allowfullscreen: "true",
58
+ autoplay: "0"
59
+ };
60
+ new URL(url.startsWith("http") ? url : "https:" + url).search.split("&").forEach((item, index) => {
61
+ if (!item) {
62
+ return;
63
+ }
64
+ if (index === 0) {
65
+ item = item.substr(1);
66
+ }
67
+ const keyValue = item.split("=");
68
+ params[keyValue[0]] = keyValue[1];
69
+ });
70
+ let src = "https://player.bilibili.com/player.html?";
71
+ const keys = Object.keys(params);
72
+ keys.forEach((key, index) => {
73
+ src += `${key}=${params[key]}`;
74
+ if (index < keys.length - 1) {
75
+ src += "&";
76
+ }
77
+ });
50
78
  element.insertAdjacentHTML("afterend",
51
- `<iframe class="iframe__video"
52
- src="//player.bilibili.com/player.html?bvid=${bilibiliMatch[1]}"></iframe>`);
79
+ `<iframe class="iframe__video" src="${src}"></iframe>`);
53
80
  element.remove();
54
81
  } else if (tedMatch && tedMatch[1]) {
55
82
  element.insertAdjacentHTML("afterend",
@@ -1,10 +1,11 @@
1
1
  import {Constants} from "../constants";
2
2
  import {addScript} from "../util/addScript";
3
3
  import {mermaidRenderAdapter} from "./adapterRender";
4
+ import {genUUID} from "../util/function";
4
5
 
5
6
  declare const mermaid: {
6
7
  initialize(options: any): void,
7
- init(options: any, element: Element): void,
8
+ render(id: string, text: string): { svg: string }
8
9
  };
9
10
 
10
11
  export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme: string) => {
@@ -26,7 +27,8 @@ export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme:
26
27
  useMaxWidth: true,
27
28
  diagramMarginX: 8,
28
29
  diagramMarginY: 8,
29
- boxMargin: 8
30
+ boxMargin: 8,
31
+ showSequenceNumbers: true // Mermaid 时序图增加序号 https://github.com/siyuan-note/siyuan/pull/6992 https://mermaid.js.org/syntax/sequenceDiagram.html#sequencenumbers
30
32
  },
31
33
  gantt: {
32
34
  leftPadding: 75,
@@ -37,12 +39,21 @@ export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme:
37
39
  config.theme = "dark";
38
40
  }
39
41
  mermaid.initialize(config);
40
- mermaidElements.forEach((item) => {
42
+ mermaidElements.forEach(async (item) => {
41
43
  const code = mermaidRenderAdapter.getCode(item);
42
44
  if (item.getAttribute("data-processed") === "true" || code.trim() === "") {
43
45
  return;
44
46
  }
45
- mermaid.init(undefined, item);
47
+ const id = "mermaid"+genUUID()
48
+ try {
49
+ const mermaidData = await mermaid.render(id, item.textContent);
50
+ item.innerHTML = mermaidData.svg;
51
+ } catch (e) {
52
+ const errorElement = document.querySelector("#" + id);
53
+ item.innerHTML = `${errorElement.outerHTML}<br>
54
+ <div style="text-align: left"><small>${e.message.replace(/\n/, "<br>")}</small></div>`;
55
+ errorElement.parentElement.remove();
56
+ }
46
57
  item.setAttribute("data-processed", "true");
47
58
  });
48
59
  });
@@ -22,7 +22,7 @@ export const plantumlRender = (element: (HTMLElement | Document) = document, cdn
22
22
  return;
23
23
  }
24
24
  try {
25
- e.innerHTML = `<img src="http://www.plantuml.com/plantuml/svg/~1${plantumlEncoder.encode(text)}">`;
25
+ e.innerHTML = `<object type="image/svg+xml" data="http://www.plantuml.com/plantuml/svg/~1${plantumlEncoder.encode(text)}"/>`;
26
26
  } catch (error) {
27
27
  e.className = "vditor-reset--error";
28
28
  e.innerHTML = `plantuml render error: <br>${error}`;
@@ -119,7 +119,7 @@ export const previewRender = async (previewElement: HTMLDivElement, markdown: st
119
119
  if (mergedOptions.anchor === 1) {
120
120
  previewElement.classList.add("vditor-reset--anchor");
121
121
  }
122
- codeRender(previewElement);
122
+ codeRender(previewElement, mergedOptions.hljs);
123
123
  highlightRender(mergedOptions.hljs, previewElement, mergedOptions.cdn);
124
124
  mathRender(previewElement, {
125
125
  cdn: mergedOptions.cdn,
@@ -220,7 +220,7 @@ export class Preview {
220
220
  if (cmtFocusElement) {
221
221
  cmtFocusElement.classList.remove("vditor-comment--focus");
222
222
  }
223
- codeRender(vditor.preview.previewElement);
223
+ codeRender(vditor.preview.previewElement, vditor.options.preview.hljs);
224
224
  highlightRender(vditor.options.preview.hljs, vditor.preview.previewElement,
225
225
  vditor.options.cdn);
226
226
  mermaidRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.theme);
@@ -0,0 +1,11 @@
1
+ export const genUUID = () => ([1e7].toString() + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
2
+ (parseInt(c, 10) ^ (window.crypto.getRandomValues(new Uint32Array(1))[0] & (15 >> (parseInt(c, 10) / 4)))).toString(16)
3
+ );
4
+
5
+ export const getSearch = (key: string, link = window.location.search) => {
6
+ const params = link.substring(link.indexOf("?"));
7
+ const hashIndex = params.indexOf("#");
8
+ // REF https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams
9
+ const urlSearchParams = new URLSearchParams(params.substring(0, hashIndex >= 0 ? hashIndex : undefined));
10
+ return urlSearchParams.get(key);
11
+ };
@@ -90,7 +90,7 @@ export const processCodeRender = (previewPanel: HTMLElement, vditor: IVditor) =>
90
90
  })
91
91
  if (!cRender) {
92
92
  highlightRender(Object.assign({}, vditor.options.preview.hljs), previewPanel, vditor.options.cdn);
93
- codeRender(previewPanel);
93
+ codeRender(previewPanel, vditor.options.preview.hljs);
94
94
  }
95
95
  }
96
96
 
@@ -395,6 +395,10 @@ class WYSIWYG {
395
395
  }
396
396
  return;
397
397
  }
398
+ // https://github.com/Vanessa219/vditor/issues/1565
399
+ if (event.inputType === "insertParagraph" && this.element.innerHTML === '<p><br></p><p><br></p>') {
400
+ blockElement.previousElementSibling.remove();
401
+ }
398
402
 
399
403
  input(vditor, range, event);
400
404
  });
@@ -424,11 +428,12 @@ class WYSIWYG {
424
428
  }
425
429
 
426
430
  // 打开链接
427
- if (event.target.tagName === "A") {
431
+ const a = hasClosestByMatchTag(event.target, "A");
432
+ if (a) {
428
433
  if (vditor.options.link.click) {
429
- vditor.options.link.click(event.target);
434
+ vditor.options.link.click(a);
430
435
  } else if (vditor.options.link.isOpen) {
431
- window.open(event.target.getAttribute("href"));
436
+ window.open(a.getAttribute("href"));
432
437
  }
433
438
  event.preventDefault();
434
439
  return;