md-editor-v3 5.7.1 → 5.8.1

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