md-editor-v3 5.7.1 → 5.8.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,330 +1,647 @@
1
- import { createVNode, defineComponent, h, inject, ref, shallowRef, reactive, computed, watch, nextTick, onMounted, Teleport } from "vue";
2
- import { p as prefix, g as globalConfig } from "./config.mjs";
3
- import { g as getSlot } from "./vue-tsx.mjs";
4
- import { k as keyMove } from "./dom.mjs";
5
- import { Upload, Trash2, X, SquareSigma, ChartArea, ListTree, CodeXml, Eye, View, Expand, Shrink, Maximize2, Minimize2, SquareCode, Save, Forward, Reply, Table, Image, Link, Code, ListTodo, ListOrdered, List, Quote, Superscript, Subscript, Heading, Strikethrough, Italic, Underline, Bold } from "lucide-vue-next";
6
- import { c as getZIndexIncrement } from "./index5.mjs";
7
- const Github = () => createVNode("svg", {
8
- "xmlns": "http://www.w3.org/2000/svg",
9
- "viewBox": "0 0 24 24",
10
- "fill": "none",
11
- "stroke": "currentColor",
12
- "stroke-width": "2",
13
- "stroke-linecap": "round",
14
- "stroke-linejoin": "round",
15
- "class": "lucide lucide-github-icon"
16
- }, [createVNode("path", {
17
- "d": "M15 22v-4a4.8 4.8 0 0 0-1-3.5c3 0 6-2 6-5.5.08-1.25-.27-2.48-1-3.5.28-1.15.28-2.35 0-3.5 0 0-1 0-3 1.5-2.64-.5-5.36-.5-8 0C6 2 5 2 5 2c-.3 1.15-.3 2.35 0 3.5A5.403 5.403 0 0 0 4 9c0 3.5 3 5.5 6 5.5-.39.49-.68 1.05-.85 1.65-.17.6-.22 1.23-.15 1.85v4"
18
- }, null), createVNode("path", {
19
- "d": "M9 18c-4.51 2-5-2-7-2"
20
- }, null)]);
21
- const iconMaps = {
22
- bold: Bold,
23
- underline: Underline,
24
- italic: Italic,
25
- "strike-through": Strikethrough,
26
- title: Heading,
27
- sub: Subscript,
28
- sup: Superscript,
29
- quote: Quote,
30
- "unordered-list": List,
31
- "ordered-list": ListOrdered,
32
- task: ListTodo,
33
- "code-row": Code,
34
- code: SquareCode,
35
- link: Link,
36
- image: Image,
37
- table: Table,
38
- revoke: Reply,
39
- next: Forward,
40
- save: Save,
41
- prettier: SquareCode,
42
- minimize: Minimize2,
43
- maximize: Maximize2,
44
- "fullscreen-exit": Shrink,
45
- fullscreen: Expand,
46
- "preview-only": View,
47
- preview: Eye,
48
- "preview-html": CodeXml,
49
- catalog: ListTree,
50
- github: Github,
51
- mermaid: ChartArea,
52
- formula: SquareSigma,
53
- close: X,
54
- delete: Trash2,
55
- upload: Upload
1
+ import { defineComponent, inject, ref, watch, onMounted, createVNode, reactive, shallowRef, provide, computed, onBeforeUnmount, Fragment } from "vue";
2
+ import { p as prefix } from "./config.mjs";
3
+ import { a as getRelativeTop } from "./index5.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
+ import { createSmoothScroll, debounce } from "@vavt/util";
6
+ const DATA_LINE_SELECTOR = `.${prefix}-preview > [data-line]`;
7
+ const getComputedStyleNum = (ele, key) => {
8
+ return +getComputedStyle(ele).getPropertyValue(key).replace("px", "");
56
9
  };
57
- const Icon$1 = /* @__PURE__ */ defineComponent({
58
- name: `${prefix}-icon-set`,
59
- props: {
60
- name: {
61
- type: String,
62
- default: ""
10
+ const scrollAutoWithScale = (pEle, cEle) => {
11
+ const addEvent = debounce(() => {
12
+ pEle.removeEventListener("scroll", scrollHandler);
13
+ pEle.addEventListener("scroll", scrollHandler);
14
+ cEle.removeEventListener("scroll", scrollHandler);
15
+ cEle.addEventListener("scroll", scrollHandler);
16
+ }, 50);
17
+ const scrollHandler = (e) => {
18
+ const pHeight = pEle.clientHeight;
19
+ const cHeight = cEle.clientHeight;
20
+ const pScrollHeight = pEle.scrollHeight;
21
+ const cScrollHeight = cEle.scrollHeight;
22
+ const scale = (pScrollHeight - pHeight) / (cScrollHeight - cHeight);
23
+ if (e.target === pEle) {
24
+ cEle.removeEventListener("scroll", scrollHandler);
25
+ cEle.scrollTo({
26
+ top: pEle.scrollTop / scale
27
+ // behavior: 'smooth'
28
+ });
29
+ addEvent();
30
+ } else {
31
+ pEle.removeEventListener("scroll", scrollHandler);
32
+ pEle.scrollTo({
33
+ top: cEle.scrollTop * scale
34
+ // behavior: 'smooth'
35
+ });
36
+ addEvent();
63
37
  }
64
- },
65
- setup(props2) {
66
- return () => {
67
- return h(iconMaps[props2.name], {
68
- class: `${prefix}-icon`
38
+ };
39
+ return [
40
+ () => {
41
+ addEvent().finally(() => {
42
+ pEle.dispatchEvent(new Event("scroll"));
69
43
  });
70
- };
71
- }
72
- });
73
- const Icon = /* @__PURE__ */ defineComponent({
74
- name: `${prefix}-icon`,
75
- props: {
76
- name: {
77
- type: String,
78
- default: ""
44
+ },
45
+ () => {
46
+ pEle.removeEventListener("scroll", scrollHandler);
47
+ cEle.removeEventListener("scroll", scrollHandler);
48
+ }
49
+ ];
50
+ };
51
+ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
52
+ const { view } = codeMirrorUt;
53
+ const smoothScroll = createSmoothScroll();
54
+ const getTopByLine = (line) => {
55
+ return view.lineBlockAt(view.state.doc.line(line + 1).from).top;
56
+ };
57
+ const getBottomByLine = (line) => {
58
+ return view.lineBlockAt(view.state.doc.line(line + 1).from).bottom;
59
+ };
60
+ let blockMap = [];
61
+ let elesHasLineNumber = [];
62
+ let startLines = [];
63
+ const buildMap = () => {
64
+ blockMap = [];
65
+ elesHasLineNumber = Array.from(
66
+ cEle.querySelectorAll(DATA_LINE_SELECTOR)
67
+ );
68
+ startLines = elesHasLineNumber.map((item) => Number(item.dataset.line));
69
+ const tempStartLines = [...startLines];
70
+ const { lines } = view.state.doc;
71
+ let start = tempStartLines.shift() || 0;
72
+ let end = tempStartLines.shift() || lines;
73
+ for (let i = 0; i < lines; i++) {
74
+ if (i === end) {
75
+ start = i;
76
+ end = tempStartLines.shift() || lines;
77
+ }
78
+ blockMap.push({
79
+ start,
80
+ end: end - 1
81
+ });
82
+ }
83
+ };
84
+ const getLineNumber = (pMaxScrollLength, cMaxScrollLength) => {
85
+ let lineNumer = 1;
86
+ for (let i = elesHasLineNumber.length - 1; i - 1 >= 0; i--) {
87
+ const curr = elesHasLineNumber[i];
88
+ const sibling = elesHasLineNumber[i - 1];
89
+ if (curr.offsetTop + curr.offsetHeight > cMaxScrollLength && sibling.offsetTop < cMaxScrollLength) {
90
+ lineNumer = Number(sibling.dataset.line);
91
+ break;
92
+ }
93
+ }
94
+ for (let i = blockMap.length - 1; i >= 0; i--) {
95
+ const itemBottom = getBottomByLine(blockMap[i].end);
96
+ const itemTop = getTopByLine(blockMap[i].start);
97
+ if (itemBottom > pMaxScrollLength && itemTop <= pMaxScrollLength) {
98
+ lineNumer = lineNumer < blockMap[i].start ? lineNumer : blockMap[i].start;
99
+ break;
100
+ }
101
+ }
102
+ return lineNumer;
103
+ };
104
+ let pLock = 0;
105
+ let cLock = 0;
106
+ const pEleHandler = () => {
107
+ var _a, _b, _c;
108
+ if (cLock !== 0) {
109
+ return false;
110
+ }
111
+ pLock++;
112
+ const { scrollDOM, contentHeight } = view;
113
+ let cElePaddingTop = getComputedStyleNum(cEle, "padding-top");
114
+ const blockInfo = view.lineBlockAtHeight(scrollDOM.scrollTop);
115
+ const { number: currLine } = view.state.doc.lineAt(blockInfo.from);
116
+ const blockData = blockMap[currLine - 1];
117
+ if (!blockData) {
118
+ return false;
119
+ }
120
+ let scale = 1;
121
+ const startEle = cEle.querySelector(`[data-line="${blockData.start}"]`) || ((_a = cEle.firstElementChild) == null ? void 0 : _a.firstElementChild);
122
+ const endEle = cEle.querySelector(`[data-line="${blockData.end + 1}"]`) || ((_b = cEle.lastElementChild) == null ? void 0 : _b.lastElementChild);
123
+ const pMaxScrollLength = scrollDOM.scrollHeight - scrollDOM.clientHeight;
124
+ const cMaxScrollLength = cEle.scrollHeight - cEle.clientHeight;
125
+ let startTop = getTopByLine(blockData.start);
126
+ let endBottom = getBottomByLine(blockData.end);
127
+ let startEleOffetTop = startEle.offsetTop;
128
+ let blockHeight = endEle.offsetTop - startEleOffetTop;
129
+ if (startTop === 0) {
130
+ startEleOffetTop = 0;
131
+ if (startEle === endEle) {
132
+ cElePaddingTop = 0;
133
+ endBottom = contentHeight - scrollDOM.offsetHeight;
134
+ blockHeight = cMaxScrollLength;
135
+ } else {
136
+ blockHeight = endEle.offsetTop;
137
+ }
138
+ }
139
+ scale = (scrollDOM.scrollTop - startTop) / (endBottom - startTop);
140
+ const endElePos = endEle == ((_c = cEle.lastElementChild) == null ? void 0 : _c.lastElementChild) ? endEle.offsetTop + endEle.clientHeight : endEle.offsetTop;
141
+ if (endBottom >= pMaxScrollLength || endElePos > cMaxScrollLength) {
142
+ const lineNumer = getLineNumber(pMaxScrollLength, cMaxScrollLength);
143
+ startTop = getTopByLine(lineNumer);
144
+ scale = (scrollDOM.scrollTop - startTop) / (pMaxScrollLength - startTop);
145
+ const _startEle = cEle.querySelector(`[data-line="${lineNumer}"]`);
146
+ if (startTop > 0 && _startEle) {
147
+ startEleOffetTop = _startEle.offsetTop;
148
+ }
149
+ blockHeight = cMaxScrollLength - startEleOffetTop + getComputedStyleNum(cEle, "padding-top");
150
+ }
151
+ const scrollToTop = startEleOffetTop - cElePaddingTop + blockHeight * scale;
152
+ smoothScroll(cEle, scrollToTop, () => {
153
+ pLock--;
154
+ });
155
+ };
156
+ const cEleHandler = () => {
157
+ var _a, _b, _c, _d, _e, _f;
158
+ if (pLock !== 0) {
159
+ return;
160
+ }
161
+ cLock++;
162
+ const { scrollDOM } = view;
163
+ const cScrollTop = cEle.scrollTop;
164
+ const cScrollHeight = cEle.scrollHeight;
165
+ const pMaxScrollLength = scrollDOM.scrollHeight - scrollDOM.clientHeight;
166
+ const cMaxScrollLength = cEle.scrollHeight - cEle.clientHeight;
167
+ let realEleStart = (_a = cEle.firstElementChild) == null ? void 0 : _a.firstElementChild;
168
+ let realEleEnd = (_b = cEle.firstElementChild) == null ? void 0 : _b.lastElementChild;
169
+ if (startLines.length > 0) {
170
+ let virtualLine = Math.ceil(
171
+ startLines[startLines.length - 1] * (cScrollTop / cScrollHeight)
172
+ );
173
+ let startLineIndex = startLines.findLastIndex((value) => value <= virtualLine);
174
+ startLineIndex = startLineIndex === -1 ? 0 : startLineIndex;
175
+ virtualLine = startLines[startLineIndex];
176
+ for (let i = startLineIndex; i >= 0 && i < startLines.length; ) {
177
+ const currentElementTop = elesHasLineNumber[i].offsetTop;
178
+ if (currentElementTop > cScrollTop) {
179
+ if (i - 1 >= 0) {
180
+ i--;
181
+ continue;
182
+ }
183
+ virtualLine = -1;
184
+ startLineIndex = i;
185
+ break;
186
+ } else {
187
+ if (i + 1 < startLines.length && elesHasLineNumber[i + 1].offsetTop < cScrollTop) {
188
+ i++;
189
+ continue;
190
+ }
191
+ virtualLine = startLines[i];
192
+ startLineIndex = i;
193
+ break;
194
+ }
195
+ }
196
+ switch (startLineIndex) {
197
+ case -1: {
198
+ realEleStart = (_c = cEle.firstElementChild) == null ? void 0 : _c.firstElementChild;
199
+ realEleEnd = elesHasLineNumber[startLineIndex];
200
+ break;
201
+ }
202
+ case startLines.length - 1: {
203
+ realEleStart = elesHasLineNumber[startLineIndex];
204
+ realEleEnd = (_d = cEle.firstElementChild) == null ? void 0 : _d.lastElementChild;
205
+ break;
206
+ }
207
+ default: {
208
+ realEleStart = elesHasLineNumber[startLineIndex];
209
+ realEleEnd = elesHasLineNumber[startLineIndex + 1 === elesHasLineNumber.length ? startLineIndex : startLineIndex + 1];
210
+ }
211
+ }
212
+ }
213
+ let eleStartOffsetTop = realEleStart === ((_e = cEle.firstElementChild) == null ? void 0 : _e.firstElementChild) ? 0 : realEleStart.offsetTop - getComputedStyleNum(realEleStart, "margin-top");
214
+ let eleEndOffsetTop = realEleEnd.offsetTop;
215
+ let scale = 0;
216
+ const { start, end } = blockMap[Number(realEleStart.dataset.line || 0)];
217
+ let firstLineScrollTop = getTopByLine(start);
218
+ const endLineScrollTop = getTopByLine(
219
+ end + 1 === view.state.doc.lines ? end : end + 1
220
+ );
221
+ let blockHeight = 0;
222
+ if (endLineScrollTop > pMaxScrollLength || realEleEnd.offsetTop + realEleEnd.offsetHeight > cMaxScrollLength) {
223
+ const lineNumer = getLineNumber(pMaxScrollLength, cMaxScrollLength);
224
+ const _startEle = cEle.querySelector(`[data-line="${lineNumer}"]`);
225
+ eleStartOffsetTop = _startEle ? _startEle.offsetTop - getComputedStyleNum(_startEle, "margin-top") : eleStartOffsetTop;
226
+ firstLineScrollTop = getTopByLine(lineNumer);
227
+ scale = (cScrollTop - eleStartOffsetTop) / (cMaxScrollLength - eleStartOffsetTop);
228
+ blockHeight = pMaxScrollLength - firstLineScrollTop;
229
+ } else if (realEleStart === ((_f = cEle.firstElementChild) == null ? void 0 : _f.firstElementChild)) {
230
+ if (realEleStart === realEleEnd) {
231
+ eleEndOffsetTop = realEleEnd.offsetTop + realEleEnd.offsetHeight + +getComputedStyle(realEleEnd).marginBottom.replace("px", "");
232
+ blockHeight = endLineScrollTop;
233
+ } else {
234
+ blockHeight = endLineScrollTop;
235
+ }
236
+ scale = Math.max(cScrollTop / eleEndOffsetTop, 0);
237
+ } else {
238
+ scale = Math.max(
239
+ (cScrollTop - eleStartOffsetTop) / (eleEndOffsetTop - eleStartOffsetTop),
240
+ 0
241
+ );
242
+ blockHeight = endLineScrollTop - firstLineScrollTop;
243
+ }
244
+ smoothScroll(pEle, firstLineScrollTop + blockHeight * scale, () => {
245
+ cLock--;
246
+ });
247
+ };
248
+ const scrollHandler = (e) => {
249
+ var _a;
250
+ const { scrollDOM, contentHeight } = view;
251
+ const scrollDomHeight = scrollDOM.clientHeight;
252
+ if (contentHeight <= scrollDomHeight || cEle.firstElementChild.clientHeight <= cEle.clientHeight) {
253
+ return false;
254
+ }
255
+ if (view.state.doc.lines <= ((_a = blockMap[blockMap.length - 1]) == null ? void 0 : _a.end)) {
256
+ return false;
257
+ }
258
+ if (e.target === pEle) {
259
+ pEleHandler();
260
+ } else {
261
+ cEleHandler();
262
+ }
263
+ };
264
+ return [
265
+ () => {
266
+ buildMap();
267
+ pEle.addEventListener("scroll", scrollHandler);
268
+ cEle.addEventListener("scroll", scrollHandler);
269
+ pEle.dispatchEvent(new Event("scroll"));
270
+ },
271
+ () => {
272
+ pEle.removeEventListener("scroll", scrollHandler);
273
+ cEle.removeEventListener("scroll", scrollHandler);
274
+ }
275
+ ];
276
+ };
277
+ const props$1 = {
278
+ tocItem: {
279
+ type: Object,
280
+ default: () => ({})
281
+ },
282
+ mdHeadingId: {
283
+ type: Function,
284
+ default: () => {
285
+ }
286
+ },
287
+ onActive: {
288
+ type: Function,
289
+ default: () => {
290
+ }
291
+ },
292
+ onClick: {
293
+ type: Function,
294
+ default: () => {
79
295
  }
80
296
  },
297
+ scrollElementOffsetTop: {
298
+ type: Number,
299
+ default: 0
300
+ }
301
+ };
302
+ const CatalogLink = /* @__PURE__ */ defineComponent({
303
+ props: props$1,
81
304
  setup(props2) {
82
- const customIcon = inject("customIcon");
83
- return () => {
84
- const item = customIcon.value[props2.name];
85
- if (typeof item === "object") {
86
- return typeof item.component === "object" ? h(item.component, item.props) : createVNode("span", {
87
- "innerHTML": item.component
88
- }, null);
305
+ const scrollElementRef = inject("scrollElementRef");
306
+ const rootNodeRef = inject("roorNodeRef");
307
+ const currRef = ref();
308
+ watch(() => props2.tocItem.active, (active) => {
309
+ if (active) {
310
+ props2.onActive(props2.tocItem, currRef.value);
89
311
  }
90
- return createVNode(Icon$1, {
91
- "name": props2.name
92
- }, null);
312
+ });
313
+ onMounted(() => {
314
+ if (props2.tocItem.active) {
315
+ props2.onActive(props2.tocItem, currRef.value);
316
+ }
317
+ });
318
+ return () => {
319
+ const {
320
+ tocItem,
321
+ mdHeadingId,
322
+ onClick,
323
+ scrollElementOffsetTop
324
+ } = props2;
325
+ return createVNode("div", {
326
+ "ref": currRef,
327
+ "class": [`${prefix}-catalog-link`, tocItem.active && `${prefix}-catalog-active`],
328
+ "onClick": (e) => {
329
+ e.stopPropagation();
330
+ onClick(e, tocItem);
331
+ if (e.defaultPrevented) {
332
+ return;
333
+ }
334
+ const id = mdHeadingId(tocItem.text, tocItem.level, tocItem.index);
335
+ const targetHeadEle = rootNodeRef.value.getElementById(id);
336
+ const scrollContainer = scrollElementRef.value;
337
+ if (targetHeadEle && scrollContainer) {
338
+ let par = targetHeadEle.offsetParent;
339
+ let offsetTop = targetHeadEle.offsetTop;
340
+ if (scrollContainer.contains(par)) {
341
+ while (par && scrollContainer != par) {
342
+ offsetTop += par == null ? void 0 : par.offsetTop;
343
+ par = par == null ? void 0 : par.offsetParent;
344
+ }
345
+ }
346
+ const pel = targetHeadEle.previousElementSibling;
347
+ let currMarginTop = 0;
348
+ if (!pel) {
349
+ currMarginTop = getComputedStyleNum(targetHeadEle, "margin-top");
350
+ }
351
+ scrollContainer == null ? void 0 : scrollContainer.scrollTo({
352
+ top: offsetTop - scrollElementOffsetTop - currMarginTop,
353
+ behavior: "smooth"
354
+ });
355
+ }
356
+ }
357
+ }, [createVNode("span", {
358
+ "title": tocItem.text
359
+ }, [tocItem.text]), tocItem.children && tocItem.children.length > 0 && createVNode("div", {
360
+ "class": `${prefix}-catalog-wrapper`
361
+ }, [tocItem.children.map((item) => createVNode(CatalogLink, {
362
+ "mdHeadingId": mdHeadingId,
363
+ "key": `${tocItem.text}-link-${item.level}-${item.text}`,
364
+ "tocItem": item,
365
+ "onActive": props2.onActive,
366
+ "onClick": onClick,
367
+ "scrollElementOffsetTop": scrollElementOffsetTop
368
+ }, null))])]);
93
369
  };
94
370
  }
95
371
  });
96
372
  const props = {
97
- title: {
98
- type: [String, Object],
373
+ /**
374
+ * 编辑器的Id,务必与需要绑定的编辑器Id相同
375
+ */
376
+ editorId: {
377
+ type: String,
378
+ default: void 0
379
+ },
380
+ class: {
381
+ type: String,
99
382
  default: ""
100
383
  },
101
- visible: {
102
- type: Boolean,
103
- default: false
384
+ mdHeadingId: {
385
+ type: Function,
386
+ default: (text) => text
104
387
  },
105
- width: {
106
- type: String,
107
- default: "auto"
388
+ /**
389
+ * 指定滚动的容器,选择器需带上对应的符号,默认预览框
390
+ * 元素必须定位!!!!!!
391
+ *
392
+ * 默认:#md-editor-preview-wrapper
393
+ */
394
+ scrollElement: {
395
+ type: [String, Object],
396
+ default: void 0
108
397
  },
109
- height: {
398
+ theme: {
110
399
  type: String,
111
- default: "auto"
112
- },
113
- onClose: {
114
- type: Function
400
+ default: "light"
115
401
  },
116
- showAdjust: {
117
- type: Boolean,
118
- default: false
402
+ /**
403
+ * 高亮标题相对滚动容器顶部偏移量,即距离该值时,高亮当前目录菜单项
404
+ *
405
+ * 默认:20px
406
+ */
407
+ offsetTop: {
408
+ type: Number,
409
+ default: 20
119
410
  },
120
- isFullscreen: {
121
- type: Boolean,
122
- default: false
411
+ /**
412
+ * 滚动区域的固定顶部高度
413
+ *
414
+ * 默认:0
415
+ */
416
+ scrollElementOffsetTop: {
417
+ type: Number,
418
+ default: 0
123
419
  },
124
- onAdjust: {
420
+ onClick: {
125
421
  type: Function,
126
- default: () => {
127
- }
128
- },
129
- class: {
130
- type: String,
131
422
  default: void 0
132
423
  },
133
- style: {
134
- type: [Object, String],
135
- default: () => ({})
424
+ onActive: {
425
+ type: Function,
426
+ default: void 0
136
427
  },
137
- showMask: {
428
+ /**
429
+ * 滚动容器是否在web component中,默认不在
430
+ *
431
+ * 在其中的话通过document查询不到
432
+ */
433
+ isScrollElementInShadow: {
138
434
  type: Boolean,
139
- default: true
435
+ default: false
436
+ },
437
+ /**
438
+ * 设置与哪个区域同步,默认与内容区域同步
439
+ *
440
+ * >= v5.3.0
441
+ */
442
+ syncWith: {
443
+ type: String,
444
+ default: "preview"
445
+ },
446
+ /**
447
+ * 控制最大显示的目录层级
448
+ */
449
+ catalogMaxDepth: {
450
+ type: Number,
451
+ default: void 0
140
452
  }
141
453
  };
142
- const MdModal = /* @__PURE__ */ defineComponent({
143
- name: "MdModal",
454
+ const MdCatalog = /* @__PURE__ */ defineComponent({
455
+ name: "MdCatalog",
144
456
  props,
145
- emits: ["onClose"],
457
+ emits: ["onClick", "onActive"],
146
458
  setup(props2, ctx) {
147
- const themeRef = inject("theme");
148
- const rootRef = inject("rootRef");
149
- const modalVisible = ref(props2.visible);
150
- const modalClass = ref([`${prefix}-modal`]);
151
- const modalRef = ref();
152
- const modalHeaderRef = ref();
153
- const bodyRef = ref();
154
- const containerRef = shallowRef();
155
- let keyMoveClear = () => {
156
- };
459
+ const editorId = props2.editorId;
460
+ const defaultScrollElement = `#${editorId}-preview-wrapper`;
157
461
  const state = reactive({
158
- maskStyle: {
159
- zIndex: -1
160
- },
161
- modalStyle: {
162
- zIndex: -1
163
- },
164
- initPos: {
165
- left: "0px",
166
- top: "0px"
167
- },
168
- historyPos: {
169
- left: "0px",
170
- top: "0px"
171
- }
462
+ list: [],
463
+ show: false,
464
+ scrollElement: props2.scrollElement || defaultScrollElement
172
465
  });
173
- const innerSize = computed(() => {
174
- if (props2.isFullscreen) {
175
- return {
176
- width: "100%",
177
- height: "100%"
466
+ const activeItem = shallowRef();
467
+ const catalogRef = ref();
468
+ const scrollElementRef = ref();
469
+ const scrollContainerRef = ref();
470
+ const rootNodeRef = ref();
471
+ const editorViewRef = shallowRef();
472
+ const indicatorStyles = ref({});
473
+ provide("scrollElementRef", scrollElementRef);
474
+ provide("roorNodeRef", rootNodeRef);
475
+ const catalogs = computed(() => {
476
+ const tocItems = [];
477
+ state.list.forEach((listItem, index) => {
478
+ if (props2.catalogMaxDepth && listItem.level > props2.catalogMaxDepth) {
479
+ return;
480
+ }
481
+ const {
482
+ text,
483
+ level,
484
+ line
485
+ } = listItem;
486
+ const item = {
487
+ level,
488
+ text,
489
+ line,
490
+ index: index + 1,
491
+ active: activeItem.value === listItem
178
492
  };
179
- } else {
180
- return {
181
- width: props2.width,
182
- height: props2.height
183
- };
184
- }
493
+ if (tocItems.length === 0) {
494
+ tocItems.push(item);
495
+ } else {
496
+ let lastItem = tocItems[tocItems.length - 1];
497
+ if (item.level > lastItem.level) {
498
+ for (let i = lastItem.level + 1; i <= 6; i++) {
499
+ const {
500
+ children
501
+ } = lastItem;
502
+ if (!children) {
503
+ lastItem.children = [item];
504
+ break;
505
+ }
506
+ lastItem = children[children.length - 1];
507
+ if (item.level <= lastItem.level) {
508
+ children.push(item);
509
+ break;
510
+ }
511
+ }
512
+ } else {
513
+ tocItems.push(item);
514
+ }
515
+ }
516
+ });
517
+ return tocItems;
185
518
  });
186
- watch(() => props2.isFullscreen, (nVal) => {
187
- if (nVal) {
188
- keyMoveClear();
189
- } else {
190
- nextTick(() => {
191
- keyMoveClear = keyMove(modalHeaderRef.value, (left, top) => {
192
- state.initPos.left = left + "px";
193
- state.initPos.top = top + "px";
194
- });
195
- });
519
+ const getScrollElement = () => {
520
+ var _a;
521
+ if (state.scrollElement instanceof HTMLElement) {
522
+ return state.scrollElement;
196
523
  }
197
- });
198
- watch(() => props2.visible, (nVal) => {
199
- if (nVal) {
200
- state.maskStyle.zIndex = globalConfig.editorConfig.zIndex + getZIndexIncrement();
201
- state.modalStyle.zIndex = globalConfig.editorConfig.zIndex + getZIndexIncrement();
202
- modalClass.value.push("zoom-in");
203
- modalVisible.value = nVal;
204
- nextTick(() => {
205
- const halfWidth = modalRef.value.offsetWidth / 2;
206
- const halfHeight = modalRef.value.offsetHeight / 2;
207
- const halfClientWidth = document.documentElement.clientWidth / 2;
208
- const halfClientHeight = document.documentElement.clientHeight / 2;
209
- state.initPos.left = halfClientWidth - halfWidth + "px";
210
- state.initPos.top = halfClientHeight - halfHeight + "px";
211
- if (!props2.isFullscreen) {
212
- keyMoveClear = keyMove(modalHeaderRef.value, (left, top) => {
213
- state.initPos.left = left + "px";
214
- state.initPos.top = top + "px";
215
- });
216
- }
217
- });
218
- setTimeout(() => {
219
- modalClass.value = modalClass.value.filter((item) => item !== "zoom-in");
220
- }, 140);
221
- } else {
222
- modalClass.value.push("zoom-out");
223
- keyMoveClear();
224
- setTimeout(() => {
225
- modalClass.value = modalClass.value.filter((item) => item !== "zoom-out");
226
- modalVisible.value = nVal;
227
- }, 130);
524
+ let scrollRoot = document;
525
+ if (state.scrollElement === defaultScrollElement || props2.isScrollElementInShadow) {
526
+ scrollRoot = (_a = catalogRef.value) == null ? void 0 : _a.getRootNode();
228
527
  }
229
- });
230
- const internalStyle = computed(() => ({
231
- display: modalVisible.value ? "block" : "none"
232
- }));
233
- const combinedStyle = computed(() => {
234
- if (typeof props2.style === "string") {
235
- return [props2.style, internalStyle.value].join("; ");
236
- } else if (props2.style instanceof Object) {
237
- return {
238
- ...internalStyle.value,
239
- ...props2.style
240
- };
528
+ return scrollRoot.querySelector(state.scrollElement);
529
+ };
530
+ const findActiveHeading = (list) => {
531
+ if (list.length === 0) {
532
+ activeItem.value = void 0;
533
+ state.list = [];
534
+ return false;
535
+ }
536
+ const {
537
+ activeHead
538
+ } = list.reduce((activeData, link, index) => {
539
+ var _a;
540
+ let relativeTop = 0;
541
+ if (props2.syncWith === "preview") {
542
+ const linkEle = (_a = rootNodeRef.value) == null ? void 0 : _a.getElementById(props2.mdHeadingId(link.text, link.level, index + 1));
543
+ if (linkEle instanceof HTMLElement) {
544
+ relativeTop = getRelativeTop(linkEle, scrollElementRef.value);
545
+ }
546
+ } else {
547
+ const view = editorViewRef.value;
548
+ if (view) {
549
+ const top = view.lineBlockAt(view.state.doc.line(link.line + 1).from).top;
550
+ const scrollTop = view.scrollDOM.scrollTop;
551
+ relativeTop = top - scrollTop;
552
+ }
553
+ }
554
+ if (relativeTop < props2.offsetTop && relativeTop > activeData.minTop) {
555
+ return {
556
+ activeHead: link,
557
+ minTop: relativeTop
558
+ };
559
+ }
560
+ return activeData;
561
+ }, {
562
+ activeHead: list[0],
563
+ minTop: Number.MIN_SAFE_INTEGER
564
+ });
565
+ activeItem.value = activeHead;
566
+ state.list = list;
567
+ };
568
+ const onActive = (tocItem, ele) => {
569
+ var _a;
570
+ indicatorStyles.value.top = ele.offsetTop + getComputedStyleNum(ele, "padding-top") + "px";
571
+ (_a = props2.onActive) == null ? void 0 : _a.call(props2, tocItem, ele);
572
+ ctx.emit("onActive", tocItem, ele);
573
+ };
574
+ const scrollHandler = () => {
575
+ findActiveHeading(state.list);
576
+ };
577
+ const catalogChangedHandler = (_list) => {
578
+ var _a, _b, _c;
579
+ (_a = scrollContainerRef.value) == null ? void 0 : _a.removeEventListener("scroll", scrollHandler);
580
+ if (props2.syncWith === "editor") {
581
+ scrollContainerRef.value = (_b = editorViewRef.value) == null ? void 0 : _b.scrollDOM;
241
582
  } else {
242
- return internalStyle.value;
583
+ const scrollElement = getScrollElement();
584
+ scrollElementRef.value = scrollElement;
585
+ scrollContainerRef.value = scrollElement === document.documentElement ? document : scrollElement;
243
586
  }
587
+ findActiveHeading(_list);
588
+ (_c = scrollContainerRef.value) == null ? void 0 : _c.addEventListener("scroll", scrollHandler);
589
+ };
590
+ const getEditorView = (view) => {
591
+ editorViewRef.value = view;
592
+ };
593
+ watch([() => props2.syncWith, editorViewRef, () => props2.catalogMaxDepth], () => {
594
+ catalogChangedHandler(state.list);
244
595
  });
245
596
  onMounted(() => {
597
+ rootNodeRef.value = catalogRef.value.getRootNode();
598
+ bus.on(editorId, {
599
+ name: CATALOG_CHANGED,
600
+ callback: catalogChangedHandler
601
+ });
602
+ bus.on(editorId, {
603
+ name: GET_EDITOR_VIEW,
604
+ callback: getEditorView
605
+ });
606
+ bus.emit(editorId, PUSH_CATALOG);
607
+ bus.emit(editorId, SEND_EDITOR_VIEW);
608
+ });
609
+ onBeforeUnmount(() => {
246
610
  var _a;
247
- const rootNode = (_a = rootRef.value) == null ? void 0 : _a.getRootNode();
248
- bodyRef.value = rootNode instanceof Document ? document.body : rootNode;
611
+ bus.remove(editorId, CATALOG_CHANGED, catalogChangedHandler);
612
+ bus.remove(editorId, GET_EDITOR_VIEW, getEditorView);
613
+ (_a = scrollContainerRef.value) == null ? void 0 : _a.removeEventListener("scroll", scrollHandler);
249
614
  });
250
- return () => {
251
- const slotDefault = getSlot({
252
- ctx
253
- });
254
- const slotTitle = getSlot({
255
- props: props2,
256
- ctx
257
- }, "title");
258
- return bodyRef.value ? createVNode(Teleport, {
259
- "to": bodyRef.value
260
- }, {
261
- default: () => [createVNode("div", {
262
- "ref": containerRef,
263
- "class": `${prefix}-modal-container`,
264
- "data-theme": themeRef.value
265
- }, [createVNode("div", {
266
- "class": props2.class,
267
- "style": combinedStyle.value
268
- }, [props2.showMask && createVNode("div", {
269
- "class": `${prefix}-modal-mask`,
270
- "style": state.maskStyle,
271
- "onClick": () => {
272
- var _a;
273
- (_a = props2.onClose) == null ? void 0 : _a.call(props2);
274
- ctx.emit("onClose");
275
- }
276
- }, null), createVNode("div", {
277
- "class": modalClass.value,
278
- "style": {
279
- ...state.modalStyle,
280
- ...state.initPos,
281
- ...innerSize.value
282
- },
283
- "ref": modalRef
284
- }, [createVNode("div", {
285
- "class": `${prefix}-modal-header`,
286
- "ref": modalHeaderRef
287
- }, [slotTitle || ""]), createVNode("div", {
288
- "class": `${prefix}-modal-body`
289
- }, [slotDefault]), createVNode("div", {
290
- "class": `${prefix}-modal-func`
291
- }, [props2.showAdjust && createVNode("div", {
292
- "class": `${prefix}-modal-adjust`,
293
- "onClick": (e) => {
294
- e.stopPropagation();
295
- if (!props2.isFullscreen) {
296
- state.historyPos = state.initPos;
297
- state.initPos = {
298
- left: "0",
299
- top: "0"
300
- };
301
- } else {
302
- state.initPos = state.historyPos;
303
- }
304
- props2.onAdjust(!props2.isFullscreen);
305
- }
306
- }, [createVNode(Icon, {
307
- "name": props2.isFullscreen ? "minimize" : "maximize"
308
- }, null)]), createVNode("div", {
309
- "class": `${prefix}-modal-close`,
310
- "onClick": (e) => {
311
- var _a;
312
- e.stopPropagation();
313
- (_a = props2.onClose) == null ? void 0 : _a.call(props2);
314
- ctx.emit("onClose");
315
- }
316
- }, [createVNode(Icon, {
317
- "name": "close"
318
- }, null)])])])])])]
319
- }) : "";
320
- };
615
+ return () => createVNode("div", {
616
+ "class": [`${prefix}-catalog`, props2.theme === "dark" && `${prefix}-catalog-dark`, props2.class || ""],
617
+ "ref": catalogRef
618
+ }, [catalogs.value.length > 0 && createVNode(Fragment, null, [createVNode("div", {
619
+ "class": `${prefix}-catalog-indicator`,
620
+ "style": indicatorStyles.value
621
+ }, null), createVNode("div", {
622
+ "class": `${prefix}-catalog-container`
623
+ }, [catalogs.value.map((item) => {
624
+ return createVNode(CatalogLink, {
625
+ "mdHeadingId": props2.mdHeadingId,
626
+ "tocItem": item,
627
+ "key": `link-${item.level}-${item.text}`,
628
+ "onActive": onActive,
629
+ "onClick": (e, t) => {
630
+ var _a;
631
+ (_a = props2.onClick) == null ? void 0 : _a.call(props2, e, t);
632
+ ctx.emit("onClick", e, t);
633
+ },
634
+ "scrollElementOffsetTop": props2.scrollElementOffsetTop
635
+ }, null);
636
+ })])])]);
321
637
  }
322
638
  });
323
- MdModal.install = (app) => {
324
- app.component(MdModal.name, MdModal);
639
+ MdCatalog.install = (app) => {
640
+ app.component(MdCatalog.name, MdCatalog);
325
641
  return app;
326
642
  };
327
643
  export {
328
- Icon as I,
329
- MdModal as M
644
+ MdCatalog as M,
645
+ scrollAutoWithScale as a,
646
+ scrollAuto as s
330
647
  };