md-editor-v3 4.19.0-5 → 4.19.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,284 +1,570 @@
1
1
  "use strict";
2
2
  const vue = require("vue");
3
3
  const config = require("./config.cjs");
4
- const vueTsx = require("./vue-tsx.cjs");
5
- const dom = require("./dom.cjs");
6
- const index = require("./index4.cjs");
7
- const Icon$1 = /* @__PURE__ */ vue.defineComponent({
8
- props: {
9
- name: {
10
- type: String,
11
- default: ""
4
+ const index = require("./index5.cjs");
5
+ const eventName = require("./event-name.cjs");
6
+ const util = require("@vavt/util");
7
+ const DATA_LINE_SELECTOR = `.${config.prefix}-preview > [data-line]`;
8
+ const getComputedStyleNum = (ele, key) => {
9
+ return +getComputedStyle(ele).getPropertyValue(key).replace("px", "");
10
+ };
11
+ const scrollAutoWithScale = (pEle, cEle) => {
12
+ const addEvent = util.debounce(() => {
13
+ pEle.removeEventListener("scroll", scrollHandler);
14
+ pEle.addEventListener("scroll", scrollHandler);
15
+ cEle.removeEventListener("scroll", scrollHandler);
16
+ cEle.addEventListener("scroll", scrollHandler);
17
+ }, 50);
18
+ const scrollHandler = (e) => {
19
+ const pHeight = pEle.clientHeight;
20
+ const cHeight = cEle.clientHeight;
21
+ const pScrollHeight = pEle.scrollHeight;
22
+ const cScrollHeight = cEle.scrollHeight;
23
+ const scale = (pScrollHeight - pHeight) / (cScrollHeight - cHeight);
24
+ if (e.target === pEle) {
25
+ cEle.removeEventListener("scroll", scrollHandler);
26
+ cEle.scrollTo({
27
+ top: pEle.scrollTop / scale
28
+ // behavior: 'smooth'
29
+ });
30
+ addEvent();
31
+ } else {
32
+ pEle.removeEventListener("scroll", scrollHandler);
33
+ pEle.scrollTo({
34
+ top: cEle.scrollTop * scale
35
+ // behavior: 'smooth'
36
+ });
37
+ addEvent();
38
+ }
39
+ };
40
+ return [
41
+ () => {
42
+ addEvent().finally(() => {
43
+ pEle.dispatchEvent(new Event("scroll"));
44
+ });
45
+ },
46
+ () => {
47
+ pEle.removeEventListener("scroll", scrollHandler);
48
+ cEle.removeEventListener("scroll", scrollHandler);
49
+ }
50
+ ];
51
+ };
52
+ const scrollAuto = (pEle, cEle, codeMirrorUt) => {
53
+ const { view } = codeMirrorUt;
54
+ const smoothScroll = util.createSmoothScroll();
55
+ const getTopByLine = (line) => {
56
+ return view.lineBlockAt(view.state.doc.line(line + 1).from).top;
57
+ };
58
+ const getBottomByLine = (line) => {
59
+ return view.lineBlockAt(view.state.doc.line(line + 1).from).bottom;
60
+ };
61
+ let blockMap = [];
62
+ let elesHasLineNumber = [];
63
+ let startLines = [];
64
+ const buildMap = () => {
65
+ blockMap = [];
66
+ elesHasLineNumber = Array.from(
67
+ cEle.querySelectorAll(DATA_LINE_SELECTOR)
68
+ );
69
+ startLines = elesHasLineNumber.map((item) => Number(item.dataset.line));
70
+ const tempStartLines = [...startLines];
71
+ const { lines } = view.state.doc;
72
+ let start = tempStartLines.shift() || 0;
73
+ let end = tempStartLines.shift() || lines;
74
+ for (let i = 0; i < lines; i++) {
75
+ if (i === end) {
76
+ start = i;
77
+ end = tempStartLines.shift() || lines;
78
+ }
79
+ blockMap.push({
80
+ start,
81
+ end: end - 1
82
+ });
83
+ }
84
+ };
85
+ const getLineNumber = (pMaxScrollLength, cMaxScrollLength) => {
86
+ let lineNumer = 1;
87
+ for (let i = elesHasLineNumber.length - 1; i - 1 >= 0; i--) {
88
+ const curr = elesHasLineNumber[i];
89
+ const sibling = elesHasLineNumber[i - 1];
90
+ if (curr.offsetTop + curr.offsetHeight > cMaxScrollLength && sibling.offsetTop < cMaxScrollLength) {
91
+ lineNumer = Number(sibling.dataset.line);
92
+ break;
93
+ }
94
+ }
95
+ for (let i = blockMap.length - 1; i >= 0; i--) {
96
+ const itemBottom = getBottomByLine(blockMap[i].end);
97
+ const itemTop = getTopByLine(blockMap[i].start);
98
+ if (itemBottom > pMaxScrollLength && itemTop <= pMaxScrollLength) {
99
+ lineNumer = lineNumer < blockMap[i].start ? lineNumer : blockMap[i].start;
100
+ break;
101
+ }
102
+ }
103
+ return lineNumer;
104
+ };
105
+ let pLock = 0;
106
+ let cLock = 0;
107
+ const pEleHandler = () => {
108
+ var _a, _b, _c;
109
+ if (cLock !== 0) {
110
+ return false;
111
+ }
112
+ pLock++;
113
+ const { scrollDOM, contentHeight } = view;
114
+ let cElePaddingTop = getComputedStyleNum(cEle, "padding-top");
115
+ const blockInfo = view.lineBlockAtHeight(scrollDOM.scrollTop);
116
+ const { number: currLine } = view.state.doc.lineAt(blockInfo.from);
117
+ const blockData = blockMap[currLine - 1];
118
+ if (!blockData) {
119
+ return false;
120
+ }
121
+ let scale = 1;
122
+ const startEle = cEle.querySelector(`[data-line="${blockData.start}"]`) || ((_a = cEle.firstElementChild) == null ? void 0 : _a.firstElementChild);
123
+ const endEle = cEle.querySelector(`[data-line="${blockData.end + 1}"]`) || ((_b = cEle.lastElementChild) == null ? void 0 : _b.lastElementChild);
124
+ const pMaxScrollLength = scrollDOM.scrollHeight - scrollDOM.clientHeight;
125
+ const cMaxScrollLength = cEle.scrollHeight - cEle.clientHeight;
126
+ let startTop = getTopByLine(blockData.start);
127
+ let endBottom = getBottomByLine(blockData.end);
128
+ let startEleOffetTop = startEle.offsetTop;
129
+ let blockHeight = endEle.offsetTop - startEleOffetTop;
130
+ if (startTop === 0) {
131
+ startEleOffetTop = 0;
132
+ if (startEle === endEle) {
133
+ cElePaddingTop = 0;
134
+ endBottom = contentHeight - scrollDOM.offsetHeight;
135
+ blockHeight = cMaxScrollLength;
136
+ } else {
137
+ blockHeight = endEle.offsetTop;
138
+ }
139
+ }
140
+ scale = (scrollDOM.scrollTop - startTop) / (endBottom - startTop);
141
+ const endElePos = endEle == ((_c = cEle.lastElementChild) == null ? void 0 : _c.lastElementChild) ? endEle.offsetTop + endEle.clientHeight : endEle.offsetTop;
142
+ if (endBottom >= pMaxScrollLength || endElePos > cMaxScrollLength) {
143
+ const lineNumer = getLineNumber(pMaxScrollLength, cMaxScrollLength);
144
+ startTop = getTopByLine(lineNumer);
145
+ scale = (scrollDOM.scrollTop - startTop) / (pMaxScrollLength - startTop);
146
+ const _startEle = cEle.querySelector(`[data-line="${lineNumer}"]`);
147
+ if (startTop > 0 && _startEle) {
148
+ startEleOffetTop = _startEle.offsetTop;
149
+ }
150
+ blockHeight = cMaxScrollLength - startEleOffetTop + getComputedStyleNum(cEle, "padding-top");
151
+ }
152
+ const scrollToTop = startEleOffetTop - cElePaddingTop + blockHeight * scale;
153
+ smoothScroll(cEle, scrollToTop, () => {
154
+ pLock--;
155
+ });
156
+ };
157
+ const cEleHandler = () => {
158
+ var _a, _b, _c, _d, _e, _f;
159
+ if (pLock !== 0) {
160
+ return;
161
+ }
162
+ cLock++;
163
+ const { scrollDOM } = view;
164
+ const cScrollTop = cEle.scrollTop;
165
+ const cScrollHeight = cEle.scrollHeight;
166
+ const pMaxScrollLength = scrollDOM.scrollHeight - scrollDOM.clientHeight;
167
+ const cMaxScrollLength = cEle.scrollHeight - cEle.clientHeight;
168
+ let realEleStart = (_a = cEle.firstElementChild) == null ? void 0 : _a.firstElementChild;
169
+ let realEleEnd = (_b = cEle.firstElementChild) == null ? void 0 : _b.lastElementChild;
170
+ if (startLines.length > 0) {
171
+ let virtualLine = Math.ceil(
172
+ startLines[startLines.length - 1] * (cScrollTop / cScrollHeight)
173
+ );
174
+ let startLineIndex = startLines.findLastIndex((value) => value <= virtualLine);
175
+ startLineIndex = startLineIndex === -1 ? 0 : startLineIndex;
176
+ virtualLine = startLines[startLineIndex];
177
+ for (let i = startLineIndex; i >= 0 && i < startLines.length; ) {
178
+ const currentElementTop = elesHasLineNumber[i].offsetTop;
179
+ if (currentElementTop > cScrollTop) {
180
+ if (i - 1 >= 0) {
181
+ i--;
182
+ continue;
183
+ }
184
+ virtualLine = -1;
185
+ startLineIndex = i;
186
+ break;
187
+ } else {
188
+ if (i + 1 < startLines.length && elesHasLineNumber[i + 1].offsetTop < cScrollTop) {
189
+ i++;
190
+ continue;
191
+ }
192
+ virtualLine = startLines[i];
193
+ startLineIndex = i;
194
+ break;
195
+ }
196
+ }
197
+ switch (startLineIndex) {
198
+ case -1: {
199
+ realEleStart = (_c = cEle.firstElementChild) == null ? void 0 : _c.firstElementChild;
200
+ realEleEnd = elesHasLineNumber[startLineIndex];
201
+ break;
202
+ }
203
+ case startLines.length - 1: {
204
+ realEleStart = elesHasLineNumber[startLineIndex];
205
+ realEleEnd = (_d = cEle.firstElementChild) == null ? void 0 : _d.lastElementChild;
206
+ break;
207
+ }
208
+ default: {
209
+ realEleStart = elesHasLineNumber[startLineIndex];
210
+ realEleEnd = elesHasLineNumber[startLineIndex + 1 === elesHasLineNumber.length ? startLineIndex : startLineIndex + 1];
211
+ }
212
+ }
213
+ }
214
+ let eleStartOffsetTop = realEleStart === ((_e = cEle.firstElementChild) == null ? void 0 : _e.firstElementChild) ? 0 : realEleStart.offsetTop - getComputedStyleNum(realEleStart, "margin-top");
215
+ let eleEndOffsetTop = realEleEnd.offsetTop;
216
+ let scale = 0;
217
+ const { start, end } = blockMap[Number(realEleStart.dataset.line || 0)];
218
+ let firstLineScrollTop = getTopByLine(start);
219
+ const endLineScrollTop = getTopByLine(
220
+ end + 1 === view.state.doc.lines ? end : end + 1
221
+ );
222
+ let blockHeight = 0;
223
+ if (endLineScrollTop > pMaxScrollLength || realEleEnd.offsetTop + realEleEnd.offsetHeight > cMaxScrollLength) {
224
+ const lineNumer = getLineNumber(pMaxScrollLength, cMaxScrollLength);
225
+ const _startEle = cEle.querySelector(`[data-line="${lineNumer}"]`);
226
+ eleStartOffsetTop = _startEle ? _startEle.offsetTop - getComputedStyleNum(_startEle, "margin-top") : eleStartOffsetTop;
227
+ firstLineScrollTop = getTopByLine(lineNumer);
228
+ scale = (cScrollTop - eleStartOffsetTop) / (cMaxScrollLength - eleStartOffsetTop);
229
+ blockHeight = pMaxScrollLength - firstLineScrollTop;
230
+ } else if (realEleStart === ((_f = cEle.firstElementChild) == null ? void 0 : _f.firstElementChild)) {
231
+ if (realEleStart === realEleEnd) {
232
+ eleEndOffsetTop = realEleEnd.offsetTop + realEleEnd.offsetHeight + +getComputedStyle(realEleEnd).marginBottom.replace("px", "");
233
+ blockHeight = endLineScrollTop;
234
+ } else {
235
+ blockHeight = endLineScrollTop;
236
+ }
237
+ scale = Math.max(cScrollTop / eleEndOffsetTop, 0);
238
+ } else {
239
+ scale = Math.max(
240
+ (cScrollTop - eleStartOffsetTop) / (eleEndOffsetTop - eleStartOffsetTop),
241
+ 0
242
+ );
243
+ blockHeight = endLineScrollTop - firstLineScrollTop;
244
+ }
245
+ smoothScroll(pEle, firstLineScrollTop + blockHeight * scale, () => {
246
+ cLock--;
247
+ });
248
+ };
249
+ const scrollHandler = (e) => {
250
+ var _a;
251
+ const { scrollDOM, contentHeight } = view;
252
+ const scrollDomHeight = scrollDOM.clientHeight;
253
+ if (contentHeight <= scrollDomHeight || cEle.firstElementChild.clientHeight <= cEle.clientHeight) {
254
+ return false;
255
+ }
256
+ if (view.state.doc.lines <= ((_a = blockMap[blockMap.length - 1]) == null ? void 0 : _a.end)) {
257
+ return false;
12
258
  }
259
+ if (e.target === pEle) {
260
+ pEleHandler();
261
+ } else {
262
+ cEleHandler();
263
+ }
264
+ };
265
+ return [
266
+ () => {
267
+ buildMap();
268
+ pEle.addEventListener("scroll", scrollHandler);
269
+ cEle.addEventListener("scroll", scrollHandler);
270
+ pEle.dispatchEvent(new Event("scroll"));
271
+ },
272
+ () => {
273
+ pEle.removeEventListener("scroll", scrollHandler);
274
+ cEle.removeEventListener("scroll", scrollHandler);
275
+ }
276
+ ];
277
+ };
278
+ const props$1 = {
279
+ tocItem: {
280
+ type: Object,
281
+ default: () => ({})
13
282
  },
14
- setup(props2) {
15
- return () => {
16
- return config.configOption.iconfontType === "svg" ? vue.createVNode("svg", {
17
- "class": `${config.prefix}-icon`,
18
- "aria-hidden": "true"
19
- }, [vue.createVNode("use", {
20
- "xlink:href": `#${config.prefix}-icon-${props2.name}`
21
- }, null)]) : vue.createVNode("i", {
22
- "class": `${config.prefix}-iconfont ${config.prefix}-icon-${props2.name}`
23
- }, null);
24
- };
25
- }
26
- });
27
- const Icon = /* @__PURE__ */ vue.defineComponent({
28
- props: {
29
- name: {
30
- type: String,
31
- default: ""
283
+ mdHeadingId: {
284
+ type: Function,
285
+ default: () => {
32
286
  }
33
287
  },
288
+ onClick: {
289
+ type: Function,
290
+ default: () => {
291
+ }
292
+ },
293
+ scrollElementOffsetTop: {
294
+ type: Number,
295
+ default: 0
296
+ }
297
+ };
298
+ const CatalogLink = /* @__PURE__ */ vue.defineComponent({
299
+ props: props$1,
34
300
  setup(props2) {
35
- const customIcon = vue.inject("customIcon");
301
+ const scrollElementRef = vue.inject("scrollElementRef");
302
+ const rootNodeRef = vue.inject("roorNodeRef");
36
303
  return () => {
37
- const item = customIcon.value[props2.name];
38
- if (typeof item === "object") {
39
- return typeof item.component === "object" ? vue.h(item.component, item.props) : vue.createVNode("span", {
40
- "innerHTML": item.component
41
- }, null);
42
- }
43
- return vue.createVNode(Icon$1, {
44
- "name": props2.name
45
- }, null);
304
+ const {
305
+ tocItem,
306
+ mdHeadingId,
307
+ onClick,
308
+ scrollElementOffsetTop
309
+ } = props2;
310
+ return vue.createVNode("div", {
311
+ "class": [`${config.prefix}-catalog-link`, tocItem.active && `${config.prefix}-catalog-active`],
312
+ "onClick": (e) => {
313
+ onClick(e, tocItem);
314
+ e.stopPropagation();
315
+ const id = mdHeadingId(tocItem.text, tocItem.level, tocItem.index);
316
+ const targetHeadEle = rootNodeRef.value.getElementById(id);
317
+ const scrollContainer = scrollElementRef.value;
318
+ if (targetHeadEle && scrollContainer) {
319
+ let par = targetHeadEle.offsetParent;
320
+ let offsetTop = targetHeadEle.offsetTop;
321
+ if (scrollContainer.contains(par)) {
322
+ while (par && scrollContainer != par) {
323
+ offsetTop += par == null ? void 0 : par.offsetTop;
324
+ par = par == null ? void 0 : par.offsetParent;
325
+ }
326
+ }
327
+ const pel = targetHeadEle.previousElementSibling;
328
+ let currMarginTop = 0;
329
+ if (!pel) {
330
+ currMarginTop = getComputedStyleNum(targetHeadEle, "margin-top");
331
+ }
332
+ scrollContainer == null ? void 0 : scrollContainer.scrollTo({
333
+ top: offsetTop - scrollElementOffsetTop - currMarginTop,
334
+ behavior: "smooth"
335
+ });
336
+ }
337
+ }
338
+ }, [vue.createVNode("span", {
339
+ "title": tocItem.text
340
+ }, [tocItem.text]), vue.createVNode("div", {
341
+ "class": `${config.prefix}-catalog-wrapper`
342
+ }, [tocItem.children && tocItem.children.map((item) => vue.createVNode(CatalogLink, {
343
+ "mdHeadingId": mdHeadingId,
344
+ "key": `${tocItem.text}-link-${item.level}-${item.text}`,
345
+ "tocItem": item,
346
+ "onClick": onClick,
347
+ "scrollElementOffsetTop": scrollElementOffsetTop
348
+ }, null))])]);
46
349
  };
47
350
  }
48
351
  });
352
+ const CatalogLink$1 = CatalogLink;
49
353
  const props = {
50
- title: {
354
+ /**
355
+ * 编辑器的Id,务必与需要绑定的编辑器Id相同
356
+ */
357
+ editorId: {
358
+ type: String
359
+ },
360
+ class: {
51
361
  type: String,
52
362
  default: ""
53
363
  },
54
- visible: {
55
- type: Boolean,
56
- default: false
364
+ mdHeadingId: {
365
+ type: Function,
366
+ default: (text) => text
57
367
  },
58
- width: {
59
- type: String,
60
- default: "auto"
368
+ /**
369
+ * 指定滚动的容器,选择器需带上对应的符号,默认预览框
370
+ * 元素必须定位!!!!!!
371
+ *
372
+ * 默认:#md-editor-preview-wrapper
373
+ */
374
+ scrollElement: {
375
+ type: [String, Object]
61
376
  },
62
- height: {
377
+ theme: {
63
378
  type: String,
64
- default: "auto"
379
+ default: "light"
65
380
  },
66
- onClose: {
67
- type: Function
381
+ /**
382
+ * 高亮标题相对滚动容器顶部偏移量,即距离该值时,高亮当前目录菜单项
383
+ *
384
+ * 默认:20px
385
+ */
386
+ offsetTop: {
387
+ type: Number,
388
+ default: 20
68
389
  },
69
- showAdjust: {
70
- type: Boolean,
71
- default: false
72
- },
73
- isFullscreen: {
74
- type: Boolean,
75
- default: false
390
+ /**
391
+ * 滚动区域的固定顶部高度
392
+ *
393
+ * 默认:0
394
+ */
395
+ scrollElementOffsetTop: {
396
+ type: Number,
397
+ default: 0
76
398
  },
77
- onAdjust: {
78
- type: Function,
79
- default: () => {
80
- }
81
- },
82
- class: {
83
- type: String
399
+ onClick: {
400
+ type: Function
84
401
  },
85
- style: {
86
- type: [Object, String],
87
- default: () => ({})
402
+ onActive: {
403
+ type: Function
88
404
  },
89
- showMask: {
405
+ /**
406
+ * 滚动容器是否在web component中,默认不在
407
+ *
408
+ * 在其中的话通过document查询不到
409
+ */
410
+ isScrollElementInShadow: {
90
411
  type: Boolean,
91
- default: true
412
+ default: false
92
413
  }
93
414
  };
94
- const MdModal = /* @__PURE__ */ vue.defineComponent({
95
- name: "MdModal",
415
+ const MdCatalog = /* @__PURE__ */ vue.defineComponent({
416
+ name: "MdCatalog",
96
417
  props,
97
- emits: ["onClose"],
418
+ emits: ["onClick", "onActive"],
98
419
  setup(props2, ctx) {
99
- const themeRef = vue.inject("theme");
100
- const rootRef = vue.inject("rootRef");
101
- const modalVisible = vue.ref(props2.visible);
102
- const modalClass = vue.ref([`${config.prefix}-modal`]);
103
- const modalRef = vue.ref();
104
- const modalHeaderRef = vue.ref();
105
- const bodyRef = vue.ref();
106
- const containerRef = vue.shallowRef();
107
- let keyMoveClear = () => {
108
- };
420
+ const editorId = props2.editorId;
421
+ const defaultScrollElement = `#${editorId}-preview-wrapper`;
109
422
  const state = vue.reactive({
110
- maskStyle: {
111
- zIndex: -1
112
- },
113
- modalStyle: {
114
- zIndex: -1
115
- },
116
- initPos: {
117
- left: "0px",
118
- top: "0px"
119
- },
120
- historyPos: {
121
- left: "0px",
122
- top: "0px"
123
- }
423
+ list: [],
424
+ show: false,
425
+ scrollElement: props2.scrollElement || defaultScrollElement
124
426
  });
125
- const innerSize = vue.computed(() => {
126
- if (props2.isFullscreen) {
127
- return {
128
- width: "100%",
129
- height: "100%"
130
- };
131
- } else {
132
- return {
133
- width: props2.width,
134
- height: props2.height
427
+ const activeItem = vue.shallowRef();
428
+ const catalogRef = vue.ref();
429
+ const scrollElementRef = vue.ref();
430
+ const rootNodeRef = vue.ref();
431
+ vue.provide("scrollElementRef", scrollElementRef);
432
+ vue.provide("roorNodeRef", rootNodeRef);
433
+ const catalogs = vue.computed(() => {
434
+ const tocItems = [];
435
+ state.list.forEach((listItem, index2) => {
436
+ const {
437
+ text,
438
+ level
439
+ } = listItem;
440
+ const item = {
441
+ level,
442
+ text,
443
+ index: index2 + 1,
444
+ active: activeItem.value === listItem
135
445
  };
136
- }
446
+ if (tocItems.length === 0) {
447
+ tocItems.push(item);
448
+ } else {
449
+ let lastItem = tocItems[tocItems.length - 1];
450
+ if (item.level > lastItem.level) {
451
+ for (let i = lastItem.level + 1; i <= 6; i++) {
452
+ const {
453
+ children
454
+ } = lastItem;
455
+ if (!children) {
456
+ lastItem.children = [item];
457
+ break;
458
+ }
459
+ lastItem = children[children.length - 1];
460
+ if (item.level <= lastItem.level) {
461
+ children.push(item);
462
+ break;
463
+ }
464
+ }
465
+ } else {
466
+ tocItems.push(item);
467
+ }
468
+ }
469
+ });
470
+ return tocItems;
137
471
  });
138
- vue.watch(() => props2.isFullscreen, (nVal) => {
139
- if (nVal) {
140
- keyMoveClear();
141
- } else {
142
- vue.nextTick(() => {
143
- keyMoveClear = dom.keyMove(modalHeaderRef.value, (left, top) => {
144
- state.initPos.left = left + "px";
145
- state.initPos.top = top + "px";
146
- });
147
- });
472
+ const getScrollElement = () => {
473
+ var _a;
474
+ if (state.scrollElement instanceof HTMLElement) {
475
+ return state.scrollElement;
148
476
  }
149
- });
150
- vue.watch(() => props2.visible, (nVal) => {
151
- if (nVal) {
152
- state.maskStyle.zIndex = config.configOption.editorConfig.zIndex + index.getZIndexIncrement();
153
- state.modalStyle.zIndex = config.configOption.editorConfig.zIndex + index.getZIndexIncrement();
154
- modalClass.value.push("zoom-in");
155
- modalVisible.value = nVal;
156
- vue.nextTick(() => {
157
- const halfWidth = modalRef.value.offsetWidth / 2;
158
- const halfHeight = modalRef.value.offsetHeight / 2;
159
- const halfClientWidth = document.documentElement.clientWidth / 2;
160
- const halfClientHeight = document.documentElement.clientHeight / 2;
161
- state.initPos.left = halfClientWidth - halfWidth + "px";
162
- state.initPos.top = halfClientHeight - halfHeight + "px";
163
- if (!props2.isFullscreen) {
164
- keyMoveClear = dom.keyMove(modalHeaderRef.value, (left, top) => {
165
- state.initPos.left = left + "px";
166
- state.initPos.top = top + "px";
167
- });
168
- }
169
- });
170
- setTimeout(() => {
171
- modalClass.value = modalClass.value.filter((item) => item !== "zoom-in");
172
- }, 140);
173
- } else {
174
- modalClass.value.push("zoom-out");
175
- keyMoveClear();
176
- setTimeout(() => {
177
- modalClass.value = modalClass.value.filter((item) => item !== "zoom-out");
178
- modalVisible.value = nVal;
179
- }, 130);
477
+ let scrollRoot = document;
478
+ if (state.scrollElement === defaultScrollElement || props2.isScrollElementInShadow) {
479
+ scrollRoot = (_a = catalogRef.value) == null ? void 0 : _a.getRootNode();
180
480
  }
181
- });
182
- const internalStyle = vue.computed(() => ({
183
- display: modalVisible.value ? "block" : "none"
184
- }));
185
- const combinedStyle = vue.computed(() => {
186
- if (typeof props2.style === "string") {
187
- return [props2.style, internalStyle.value].join("; ");
188
- } else if (props2.style instanceof Object) {
189
- return {
190
- ...internalStyle.value,
191
- ...props2.style
192
- };
481
+ return scrollRoot.querySelector(state.scrollElement);
482
+ };
483
+ const findActiveHeading = (list) => {
484
+ if (list.length === 0) {
485
+ state.list = [];
486
+ return false;
487
+ }
488
+ const {
489
+ activeHead
490
+ } = list.reduce((activeData, link, index$1) => {
491
+ var _a;
492
+ const linkEle = (_a = rootNodeRef.value) == null ? void 0 : _a.getElementById(props2.mdHeadingId(link.text, link.level, index$1 + 1));
493
+ if (linkEle instanceof HTMLElement) {
494
+ const relativeTop = index.getRelativeTop(linkEle, scrollElementRef.value);
495
+ if (relativeTop < props2.offsetTop && relativeTop > activeData.minTop) {
496
+ return {
497
+ activeHead: link,
498
+ minTop: relativeTop
499
+ };
500
+ }
501
+ }
502
+ return activeData;
503
+ }, {
504
+ activeHead: list[0],
505
+ minTop: Number.MIN_SAFE_INTEGER
506
+ });
507
+ activeItem.value = activeHead;
508
+ state.list = list;
509
+ };
510
+ const scrollHandler = () => {
511
+ findActiveHeading(state.list);
512
+ };
513
+ vue.watch(() => activeItem.value, (nVal) => {
514
+ const activeHeading = nVal ? {
515
+ ...nVal
516
+ } : void 0;
517
+ if (props2.onActive) {
518
+ props2.onActive(activeHeading);
193
519
  } else {
194
- return internalStyle.value;
520
+ ctx.emit("onActive", activeHeading);
195
521
  }
196
522
  });
197
523
  vue.onMounted(() => {
524
+ rootNodeRef.value = catalogRef.value.getRootNode();
525
+ let scrollElement = getScrollElement();
526
+ scrollElementRef.value = scrollElement;
527
+ eventName.bus.on(editorId, {
528
+ name: eventName.CATALOG_CHANGED,
529
+ callback: (_list) => {
530
+ scrollElement = getScrollElement();
531
+ scrollElementRef.value = scrollElement;
532
+ scrollElement == null ? void 0 : scrollElement.removeEventListener("scroll", scrollHandler);
533
+ findActiveHeading(_list);
534
+ scrollElement == null ? void 0 : scrollElement.addEventListener("scroll", scrollHandler);
535
+ }
536
+ });
537
+ eventName.bus.emit(editorId, eventName.PUSH_CATALOG);
538
+ scrollElement == null ? void 0 : scrollElement.addEventListener("scroll", scrollHandler);
539
+ });
540
+ vue.onBeforeUnmount(() => {
198
541
  var _a;
199
- const rootNode = (_a = rootRef.value) == null ? void 0 : _a.getRootNode();
200
- bodyRef.value = rootNode instanceof Document ? document.body : rootNode;
542
+ (_a = scrollElementRef.value) == null ? void 0 : _a.removeEventListener("scroll", scrollHandler);
201
543
  });
202
- return () => {
203
- const slotDefault = vueTsx.getSlot({
204
- ctx
205
- });
206
- const slotTitle = vueTsx.getSlot({
207
- props: props2,
208
- ctx
209
- }, "title");
210
- return bodyRef.value ? vue.createVNode(vue.Teleport, {
211
- "to": bodyRef.value
212
- }, {
213
- default: () => [vue.createVNode("div", {
214
- "ref": containerRef,
215
- "class": `${config.prefix}-modal-container`,
216
- "data-theme": themeRef.value
217
- }, [vue.createVNode("div", {
218
- "class": props2.class,
219
- "style": combinedStyle.value
220
- }, [props2.showMask && vue.createVNode("div", {
221
- "class": `${config.prefix}-modal-mask`,
222
- "style": state.maskStyle,
223
- "onClick": () => {
224
- if (props2.onClose) {
225
- props2.onClose();
226
- } else {
227
- ctx.emit("onClose");
228
- }
229
- }
230
- }, null), vue.createVNode("div", {
231
- "class": modalClass.value,
232
- "style": {
233
- ...state.modalStyle,
234
- ...state.initPos,
235
- ...innerSize.value
236
- },
237
- "ref": modalRef
238
- }, [vue.createVNode("div", {
239
- "class": `${config.prefix}-modal-header`,
240
- "ref": modalHeaderRef
241
- }, [slotTitle || ""]), vue.createVNode("div", {
242
- "class": `${config.prefix}-modal-body`
243
- }, [slotDefault]), vue.createVNode("div", {
244
- "class": `${config.prefix}-modal-func`
245
- }, [props2.showAdjust && vue.createVNode("div", {
246
- "class": `${config.prefix}-modal-adjust`,
247
- "onClick": (e) => {
248
- e.stopPropagation();
249
- if (!props2.isFullscreen) {
250
- state.historyPos = state.initPos;
251
- state.initPos = {
252
- left: "0",
253
- top: "0"
254
- };
255
- } else {
256
- state.initPos = state.historyPos;
257
- }
258
- props2.onAdjust(!props2.isFullscreen);
544
+ return () => vue.createVNode("div", {
545
+ "class": [`${config.prefix}-catalog${props2.theme === "dark" ? "-dark" : ""}`, `${props2.class}`],
546
+ "ref": catalogRef
547
+ }, [catalogs.value.map((item) => {
548
+ return vue.createVNode(CatalogLink$1, {
549
+ "mdHeadingId": props2.mdHeadingId,
550
+ "tocItem": item,
551
+ "key": `link-${item.level}-${item.text}`,
552
+ "onClick": (e, t) => {
553
+ if (props2.onClick) {
554
+ props2.onClick(e, t);
555
+ } else {
556
+ ctx.emit("onClick", e, t);
259
557
  }
260
- }, [vue.createVNode(Icon, {
261
- "name": props2.isFullscreen ? "suoxiao" : "fangda"
262
- }, null)]), vue.createVNode("div", {
263
- "class": `${config.prefix}-modal-close`,
264
- "onClick": (e) => {
265
- e.stopPropagation();
266
- if (props2.onClose) {
267
- props2.onClose();
268
- } else {
269
- ctx.emit("onClose");
270
- }
271
- }
272
- }, [vue.createVNode(Icon, {
273
- "name": "close"
274
- }, null)])])])])])]
275
- }) : "";
276
- };
558
+ },
559
+ "scrollElementOffsetTop": props2.scrollElementOffsetTop
560
+ }, null);
561
+ })]);
277
562
  }
278
563
  });
279
- MdModal.install = (app) => {
280
- app.component(MdModal.name, MdModal);
564
+ MdCatalog.install = (app) => {
565
+ app.component(MdCatalog.name, MdCatalog);
281
566
  return app;
282
567
  };
283
- exports.Icon = Icon;
284
- exports.MdModal = MdModal;
568
+ exports.MdCatalog = MdCatalog;
569
+ exports.scrollAuto = scrollAuto;
570
+ exports.scrollAutoWithScale = scrollAutoWithScale;