md-editor-v3 5.2.1 → 5.3.0-0

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.
@@ -1,7 +1,7 @@
1
1
  import { defineComponent, inject, ref, watch, nextTick, createVNode, reactive, shallowRef, provide, computed, onMounted, onBeforeUnmount, Fragment } from "vue";
2
2
  import { p as prefix } from "./config.mjs";
3
3
  import { a as getRelativeTop } from "./index5.mjs";
4
- import { b as bus, g as CATALOG_CHANGED, P as PUSH_CATALOG } from "./event-name.mjs";
4
+ import { b as bus, g as CATALOG_CHANGED, G as GET_EDITOR_VIEW, P as PUSH_CATALOG, S as SEND_EDITOR_VIEW } from "./event-name.mjs";
5
5
  import { debounce, createSmoothScroll } from "@vavt/util";
6
6
  const DATA_LINE_SELECTOR = `.${prefix}-preview > [data-line]`;
7
7
  const getComputedStyleNum = (ele, key) => {
@@ -329,8 +329,11 @@ const CatalogLink = /* @__PURE__ */ defineComponent({
329
329
  "ref": currRef,
330
330
  "class": [`${prefix}-catalog-link`, tocItem.active && `${prefix}-catalog-active`],
331
331
  "onClick": (e) => {
332
- onClick(e, tocItem);
333
332
  e.stopPropagation();
333
+ onClick(e, tocItem);
334
+ if (e.defaultPrevented) {
335
+ return;
336
+ }
334
337
  const id = mdHeadingId(tocItem.text, tocItem.level, tocItem.index);
335
338
  const targetHeadEle = rootNodeRef.value.getElementById(id);
336
339
  const scrollContainer = scrollElementRef.value;
@@ -433,6 +436,15 @@ const props = {
433
436
  isScrollElementInShadow: {
434
437
  type: Boolean,
435
438
  default: false
439
+ },
440
+ /**
441
+ * 设置与哪个区域同步,默认与内容区域同步
442
+ *
443
+ * >= v5.3.0
444
+ */
445
+ syncWith: {
446
+ type: String,
447
+ default: "preview"
436
448
  }
437
449
  };
438
450
  const MdCatalog = /* @__PURE__ */ defineComponent({
@@ -452,6 +464,7 @@ const MdCatalog = /* @__PURE__ */ defineComponent({
452
464
  const scrollElementRef = ref();
453
465
  const scrollContainerRef = ref();
454
466
  const rootNodeRef = ref();
467
+ const editorViewRef = shallowRef();
455
468
  const indicatorStyles = ref({});
456
469
  provide("scrollElementRef", scrollElementRef);
457
470
  provide("roorNodeRef", rootNodeRef);
@@ -460,11 +473,13 @@ const MdCatalog = /* @__PURE__ */ defineComponent({
460
473
  state.list.forEach((listItem, index) => {
461
474
  const {
462
475
  text,
463
- level
476
+ level,
477
+ line
464
478
  } = listItem;
465
479
  const item = {
466
480
  level,
467
481
  text,
482
+ line,
468
483
  index: index + 1,
469
484
  active: activeItem.value === listItem
470
485
  };
@@ -515,15 +530,25 @@ const MdCatalog = /* @__PURE__ */ defineComponent({
515
530
  activeHead
516
531
  } = list.reduce((activeData, link, index) => {
517
532
  var _a;
518
- const linkEle = (_a = rootNodeRef.value) == null ? void 0 : _a.getElementById(props2.mdHeadingId(link.text, link.level, index + 1));
519
- if (linkEle instanceof HTMLElement) {
520
- const relativeTop = getRelativeTop(linkEle, scrollElementRef.value);
521
- if (relativeTop < props2.offsetTop && relativeTop > activeData.minTop) {
522
- return {
523
- activeHead: link,
524
- minTop: relativeTop
525
- };
533
+ let relativeTop = 0;
534
+ if (props2.syncWith === "preview") {
535
+ const linkEle = (_a = rootNodeRef.value) == null ? void 0 : _a.getElementById(props2.mdHeadingId(link.text, link.level, index + 1));
536
+ if (linkEle instanceof HTMLElement) {
537
+ relativeTop = getRelativeTop(linkEle, scrollElementRef.value);
526
538
  }
539
+ } else {
540
+ const view = editorViewRef.value;
541
+ if (view) {
542
+ const top = view.lineBlockAt(view.state.doc.line(link.line + 1).from).top;
543
+ const scrollTop = view.scrollDOM.scrollTop;
544
+ relativeTop = top - scrollTop;
545
+ }
546
+ }
547
+ if (relativeTop < props2.offsetTop && relativeTop > activeData.minTop) {
548
+ return {
549
+ activeHead: link,
550
+ minTop: relativeTop
551
+ };
527
552
  }
528
553
  return activeData;
529
554
  }, {
@@ -545,25 +570,41 @@ const MdCatalog = /* @__PURE__ */ defineComponent({
545
570
  findActiveHeading(state.list);
546
571
  };
547
572
  const catalogChangedHandler = (_list) => {
548
- var _a, _b;
549
- const scrollElement = getScrollElement();
550
- scrollElementRef.value = scrollElement;
551
- scrollContainerRef.value = scrollElement === document.documentElement ? document : scrollElement;
573
+ var _a, _b, _c;
552
574
  (_a = scrollContainerRef.value) == null ? void 0 : _a.removeEventListener("scroll", scrollHandler);
575
+ if (props2.syncWith === "editor") {
576
+ scrollContainerRef.value = (_b = editorViewRef.value) == null ? void 0 : _b.scrollDOM;
577
+ } else {
578
+ const scrollElement = getScrollElement();
579
+ scrollElementRef.value = scrollElement;
580
+ scrollContainerRef.value = scrollElement === document.documentElement ? document : scrollElement;
581
+ }
553
582
  findActiveHeading(_list);
554
- (_b = scrollContainerRef.value) == null ? void 0 : _b.addEventListener("scroll", scrollHandler);
583
+ (_c = scrollContainerRef.value) == null ? void 0 : _c.addEventListener("scroll", scrollHandler);
584
+ };
585
+ const getEditorView = (view) => {
586
+ editorViewRef.value = view;
555
587
  };
588
+ watch(() => props2.syncWith, () => {
589
+ catalogChangedHandler(state.list);
590
+ });
556
591
  onMounted(() => {
557
592
  rootNodeRef.value = catalogRef.value.getRootNode();
558
593
  bus.on(editorId, {
559
594
  name: CATALOG_CHANGED,
560
595
  callback: catalogChangedHandler
561
596
  });
597
+ bus.on(editorId, {
598
+ name: GET_EDITOR_VIEW,
599
+ callback: getEditorView
600
+ });
562
601
  bus.emit(editorId, PUSH_CATALOG);
602
+ bus.emit(editorId, SEND_EDITOR_VIEW);
563
603
  });
564
604
  onBeforeUnmount(() => {
565
605
  var _a;
566
606
  bus.remove(editorId, CATALOG_CHANGED, catalogChangedHandler);
607
+ bus.remove(editorId, GET_EDITOR_VIEW, getEditorView);
567
608
  (_a = scrollContainerRef.value) == null ? void 0 : _a.removeEventListener("scroll", scrollHandler);
568
609
  });
569
610
  return () => createVNode("div", {
@@ -583,9 +624,8 @@ const MdCatalog = /* @__PURE__ */ defineComponent({
583
624
  "onClick": (e, t) => {
584
625
  if (props2.onClick) {
585
626
  props2.onClick(e, t);
586
- } else {
587
- ctx.emit("onClick", e, t);
588
627
  }
628
+ ctx.emit("onClick", e, t);
589
629
  },
590
630
  "scrollElementOffsetTop": props2.scrollElementOffsetTop
591
631
  }, null);
@@ -10,6 +10,7 @@ declare const MdCatalog: import("vue").DefineComponent<{
10
10
  readonly offsetTop: number;
11
11
  readonly scrollElementOffsetTop: number;
12
12
  readonly isScrollElementInShadow: boolean;
13
+ readonly syncWith: "preview" | "editor";
13
14
  readonly onClick: ((e: MouseEvent, t: TocItem) => void) | undefined;
14
15
  readonly editorId: string | undefined;
15
16
  readonly onActive: ((heading: HeadList | undefined, activeElement: HTMLDivElement) => void) | undefined;
@@ -21,6 +22,7 @@ declare const MdCatalog: import("vue").DefineComponent<{
21
22
  readonly offsetTop: number;
22
23
  readonly scrollElementOffsetTop: number;
23
24
  readonly isScrollElementInShadow: boolean;
25
+ readonly syncWith: "preview" | "editor";
24
26
  readonly onClick: ((e: MouseEvent, t: TocItem) => void) | undefined;
25
27
  readonly editorId: string | undefined;
26
28
  readonly onActive: ((heading: HeadList | undefined, activeElement: HTMLDivElement) => void) | undefined;
@@ -39,5 +41,6 @@ declare const MdCatalog: import("vue").DefineComponent<{
39
41
  scrollElementOffsetTop: number;
40
42
  scrollElement: string | HTMLElement;
41
43
  isScrollElementInShadow: boolean;
44
+ syncWith: "preview" | "editor";
42
45
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
43
46
  export default MdCatalog;
@@ -32,12 +32,7 @@ export default class CodeMirrorUt {
32
32
  * @param text 待替换内容
33
33
  * @param options 替换后是否选中
34
34
  */
35
- replaceSelectedText(text: string, options: {
36
- select: boolean;
37
- deviationStart: number;
38
- deviationEnd: number;
39
- replaceAll: boolean;
40
- } | undefined, editorId: string): void;
35
+ replaceSelectedText(text: string, _options: any, editorId: string): void;
41
36
  constructor(view: EditorView);
42
37
  /**
43
38
  * 设置tabSize
@@ -20,3 +20,5 @@ export declare const PUSH_CATALOG = "pushCatalog";
20
20
  export declare const RERENDER = "rerender";
21
21
  export declare const EVENT_LISTENER = "eventListener";
22
22
  export declare const TASK_STATE_CHANGED = "taskStateChanged";
23
+ export declare const SEND_EDITOR_VIEW = "sendEditorView";
24
+ export declare const GET_EDITOR_VIEW = "getEditorView";
@@ -127,6 +127,7 @@ export interface HeadList {
127
127
  text: string;
128
128
  level: 1 | 2 | 3 | 4 | 5 | 6;
129
129
  active?: boolean;
130
+ line: number;
130
131
  }
131
132
  export type MdHeadingId = (text: string, level: number, index: number) => string;
132
133
  export interface MermaidTemplate {
@@ -10,10 +10,31 @@ export type ToolDirective = 'bold' | 'underline' | 'italic' | 'strikeThrough' |
10
10
  */
11
11
  export declare const directive2flag: (direct: ToolDirective, codeMirrorUt: CodeMirrorUt, params?: any) => Promise<{
12
12
  text: string;
13
+ options: {
14
+ deviationStart: number;
15
+ replaceStart: number;
16
+ replaceEnd: number;
17
+ };
18
+ } | {
19
+ text: any;
13
20
  options: {
14
21
  select: boolean;
22
+ replaceAll: boolean;
23
+ };
24
+ } | {
25
+ text: string;
26
+ options: {
15
27
  deviationStart: number;
16
28
  deviationEnd: number;
17
- replaceAll: boolean;
29
+ };
30
+ } | {
31
+ text: string;
32
+ options: {
33
+ select: boolean;
34
+ };
35
+ } | {
36
+ text: string;
37
+ options: {
38
+ select?: undefined;
18
39
  };
19
40
  }>;