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.
@@ -3,9 +3,9 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  import { defineComponent, createVNode, inject, ref, onMounted, reactive, computed, watch, toRef, nextTick, Fragment, cloneVNode, shallowRef, onBeforeUnmount } from "vue";
5
5
  import { p as prefix, d as configOption, a as allToolbar, M as MinInputBoxWidth, b as allFooter } from "./chunks/config.mjs";
6
- import { draggingScroll, linkTo, throttle } from "@vavt/util";
6
+ import { draggingScroll, linkTo, throttle, createSmoothScroll } from "@vavt/util";
7
7
  import { a as Dropdown, D as DropdownToolbar } from "./chunks/index2.mjs";
8
- import { b as bus, C as CHANGE_FULL_SCREEN, E as ERROR_CATCHER, U as UPLOAD_IMAGE, O as OPEN_MODALS, R as REPLACE, a as CHANGE_CATALOG_VISIBLE, c as ON_SAVE, d as CTRL_SHIFT_Z, e as CTRL_Z, f as EVENT_LISTENER, T as TASK_STATE_CHANGED } from "./chunks/event-name.mjs";
8
+ import { b as bus, C as CHANGE_FULL_SCREEN, E as ERROR_CATCHER, U as UPLOAD_IMAGE, O as OPEN_MODALS, R as REPLACE, a as CHANGE_CATALOG_VISIBLE, c as ON_SAVE, d as CTRL_SHIFT_Z, e as CTRL_Z, f as EVENT_LISTENER, T as TASK_STATE_CHANGED, S as SEND_EDITOR_VIEW, G as GET_EDITOR_VIEW } from "./chunks/event-name.mjs";
9
9
  import { a as appendHandler } from "./chunks/dom.mjs";
10
10
  import { C as CDN_IDS, c as contentProps, a as ContentPreview, e as editorProps, b as editorEmits, u as useProvide, d as useConfig, f as useCatalog, g as useOnSave, h as useExpansion, i as useErrorCatcher, j as useExpose, M as MdPreview } from "./chunks/index.mjs";
11
11
  import { M as MdModal, I as Icon } from "./chunks/index4.mjs";
@@ -1411,268 +1411,260 @@ const useAutoScroll = (props2, html, codeMirrorUt) => {
1411
1411
  onMounted(rebindEvent);
1412
1412
  };
1413
1413
  const directive2flag = async (direct, codeMirrorUt, params) => {
1414
- var _a;
1415
- let targetValue = "";
1416
- let deviationStart = 0;
1417
- let deviationEnd = 0;
1418
- let select = true;
1419
- let replaceAll = false;
1420
- const selectedText = codeMirrorUt.getSelectedText();
1421
- const mermaidTemplate = configOption.editorConfig.mermaidTemplate;
1422
- if (/^h[1-6]{1}$/.test(direct)) {
1423
- const pix = direct.replace(/^h(\d)/, (_, num) => {
1424
- return new Array(Number(num)).fill("#", 0, num).join("");
1425
- });
1426
- targetValue = `${pix} ${selectedText}`;
1427
- deviationStart = pix.length + 1;
1414
+ if (/^h[1-6]$/.test(direct)) {
1415
+ return handleHeading(direct, codeMirrorUt);
1428
1416
  } else if (direct === "prettier") {
1429
- const prettier = window.prettier || configOption.editorExtensions.prettier.prettierInstance;
1430
- const prettierPlugins = [
1431
- ((_a = window.prettierPlugins) == null ? void 0 : _a.markdown) || configOption.editorExtensions.prettier.parserMarkdownInstance
1432
- ];
1433
- if (!prettier || prettierPlugins[0] === void 0) {
1434
- bus.emit(params.editorId, ERROR_CATCHER, {
1435
- name: "prettier",
1436
- message: "prettier is undefined"
1437
- });
1438
- targetValue = codeMirrorUt.getValue();
1439
- } else {
1440
- targetValue = await prettier.format(codeMirrorUt.getValue(), {
1441
- parser: "markdown",
1442
- plugins: prettierPlugins
1443
- });
1417
+ return await handlePrettier(codeMirrorUt, params);
1418
+ }
1419
+ switch (direct) {
1420
+ case "bold":
1421
+ case "underline":
1422
+ case "italic":
1423
+ case "strikeThrough":
1424
+ case "sub":
1425
+ case "sup":
1426
+ case "codeRow":
1427
+ case "katexInline":
1428
+ case "katexBlock": {
1429
+ return wrapText(direct, codeMirrorUt);
1430
+ }
1431
+ case "quote":
1432
+ case "orderedList":
1433
+ case "unorderedList":
1434
+ case "task": {
1435
+ return handleMultiLine(direct, codeMirrorUt);
1436
+ }
1437
+ case "code": {
1438
+ return handleCodeBlock(params, codeMirrorUt);
1439
+ }
1440
+ case "table": {
1441
+ return handleTable(params);
1442
+ }
1443
+ case "link": {
1444
+ return { text: `[${params.desc}](${params.url})`, options: { select: false } };
1445
+ }
1446
+ case "image": {
1447
+ return handleImage(params);
1448
+ }
1449
+ case "flow":
1450
+ case "sequence":
1451
+ case "gantt":
1452
+ case "class":
1453
+ case "state":
1454
+ case "pie":
1455
+ case "relationship":
1456
+ case "journey": {
1457
+ return handleMermaid(direct);
1458
+ }
1459
+ case "universal": {
1460
+ return handleUniversal(codeMirrorUt.getSelectedText(), params);
1461
+ }
1462
+ default: {
1463
+ return { text: "", options: {} };
1444
1464
  }
1445
- select = false;
1446
- replaceAll = true;
1447
- } else {
1448
- switch (direct) {
1449
- case "bold": {
1450
- targetValue = `**${selectedText}**`;
1451
- deviationStart = 2;
1452
- deviationEnd = -2;
1453
- break;
1454
- }
1455
- case "underline": {
1456
- targetValue = `<u>${selectedText}</u>`;
1457
- deviationStart = 3;
1458
- deviationEnd = -4;
1459
- break;
1460
- }
1461
- case "italic": {
1462
- targetValue = `*${selectedText}*`;
1463
- deviationStart = 1;
1464
- deviationEnd = -1;
1465
- break;
1466
- }
1467
- case "strikeThrough": {
1468
- targetValue = `~~${selectedText}~~`;
1469
- deviationStart = 2;
1470
- deviationEnd = -2;
1471
- break;
1472
- }
1473
- case "sub": {
1474
- targetValue = `~${selectedText}~`;
1475
- deviationStart = 1;
1476
- deviationEnd = -1;
1477
- break;
1478
- }
1479
- case "sup": {
1480
- targetValue = `^${selectedText}^`;
1481
- deviationStart = 1;
1482
- deviationEnd = -1;
1483
- break;
1484
- }
1485
- case "codeRow": {
1486
- targetValue = "`" + selectedText + "`";
1487
- deviationStart = 1;
1488
- deviationEnd = -1;
1489
- break;
1490
- }
1491
- case "quote": {
1492
- targetValue = `> ${selectedText}`;
1493
- deviationStart = 2;
1494
- break;
1495
- }
1496
- case "orderedList": {
1497
- targetValue = `1. ${selectedText}`;
1498
- deviationStart = 3;
1499
- break;
1500
- }
1501
- case "unorderedList": {
1502
- targetValue = `- ${selectedText}`;
1503
- deviationStart = 2;
1504
- break;
1505
- }
1506
- case "task": {
1507
- targetValue = `- [ ] ${selectedText}`;
1508
- deviationStart = 6;
1509
- break;
1510
- }
1511
- case "code": {
1512
- const text = params.text || selectedText || "";
1513
- const mode = params.mode || "language";
1514
- targetValue = `\`\`\`${mode}
1515
- ${text}
1516
- \`\`\`
1517
- `;
1518
- deviationStart = 3;
1519
- deviationEnd = 3 + mode.length - targetValue.length;
1520
- break;
1521
- }
1522
- case "table": {
1523
- targetValue = "|";
1524
- const { selectedShape = { x: 1, y: 1 } } = params;
1525
- const { x, y } = selectedShape;
1526
- for (let i = 0; i <= y; i++) {
1527
- targetValue += " col |";
1528
- }
1529
- targetValue += "\n|";
1530
- for (let i = 0; i <= y; i++) {
1531
- targetValue += " - |";
1532
- }
1533
- for (let row = 0; row <= x; row++) {
1534
- targetValue += "\n|";
1535
- for (let col = 0; col <= y; col++) {
1536
- targetValue += " content |";
1537
- }
1538
- }
1539
- deviationStart = 2;
1540
- deviationEnd = 5 - targetValue.length;
1541
- break;
1542
- }
1543
- case "link": {
1544
- const { desc, url } = params;
1545
- targetValue = `[${desc}](${url})`;
1546
- select = false;
1547
- break;
1548
- }
1549
- case "image": {
1550
- const { desc, url, urls } = params;
1551
- if (urls instanceof Array) {
1552
- targetValue = urls.reduce((pVal, _url) => {
1553
- const {
1554
- url: url2 = "",
1555
- alt = "",
1556
- title = ""
1557
- } = typeof _url === "object" ? _url : { url: _url };
1558
- return pVal + `![${alt}](${url2}${title ? " '" + title + "'" : ""})
1559
- `;
1560
- }, "");
1561
- } else {
1562
- targetValue = `![${desc}](${url})
1563
- `;
1564
- }
1565
- select = false;
1566
- break;
1567
- }
1568
- case "flow": {
1569
- targetValue = `\`\`\`mermaid
1570
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.flow) || "flowchart TD \n Start --> Stop"}
1571
- \`\`\`
1572
- `;
1573
- deviationStart = 11;
1574
- deviationEnd = -5;
1575
- break;
1576
- }
1577
- case "sequence": {
1578
- targetValue = `\`\`\`mermaid
1579
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.sequence) || "sequenceDiagram\n A->>B: hello!\n B-->>A: hi!\n A-)B: bye!"}
1580
- \`\`\`
1581
- `;
1582
- deviationStart = 11;
1583
- deviationEnd = -5;
1584
- break;
1585
- }
1586
- case "gantt": {
1587
- targetValue = `\`\`\`mermaid
1588
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.gantt) || "gantt\ntitle A Gantt Diagram\ndateFormat YYYY-MM-DD\nsection Section\nA task :a1, 2014-01-01, 30d\nAnother task :after a1, 20d"}
1589
- \`\`\`
1590
- `;
1591
- deviationStart = 11;
1592
- deviationEnd = -5;
1593
- break;
1594
- }
1595
- case "class": {
1596
- targetValue = `\`\`\`mermaid
1597
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.class) || "classDiagram\n class Animal\n Vehicle <|-- Car"}
1598
- \`\`\`
1599
- `;
1600
- deviationStart = 11;
1601
- deviationEnd = -5;
1602
- break;
1603
- }
1604
- case "state": {
1605
- targetValue = `\`\`\`mermaid
1606
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.state) || "stateDiagram-v2\n s1 --> s2"}
1607
- \`\`\`
1608
- `;
1609
- deviationStart = 11;
1610
- deviationEnd = -5;
1611
- break;
1612
- }
1613
- case "pie": {
1614
- targetValue = `\`\`\`mermaid
1615
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.pie) || 'pie title Pets adopted by volunteers\n "Dogs" : 386\n "Cats" : 85\n "Rats" : 15'}
1465
+ }
1466
+ };
1467
+ const handleHeading = (direct, codeMirrorUt) => {
1468
+ const level = direct.slice(1);
1469
+ const prefix2 = "#".repeat(Number(level));
1470
+ const [text, replaceStart, replaceEnd] = getSelectedInfo(codeMirrorUt, {
1471
+ wholeLine: true
1472
+ });
1473
+ return {
1474
+ text: `${prefix2} ${text}`,
1475
+ options: { deviationStart: prefix2.length + 1, replaceStart, replaceEnd }
1476
+ };
1477
+ };
1478
+ const handlePrettier = async (codeMirrorUt, params) => {
1479
+ var _a, _b, _c;
1480
+ const prettier = window.prettier || ((_a = configOption.editorExtensions.prettier) == null ? void 0 : _a.prettierInstance);
1481
+ const prettierPlugins = [
1482
+ ((_b = window.prettierPlugins) == null ? void 0 : _b.markdown) || ((_c = configOption.editorExtensions.prettier) == null ? void 0 : _c.parserMarkdownInstance)
1483
+ ];
1484
+ if (!prettier || !prettierPlugins[0]) {
1485
+ bus.emit(params.editorId, ERROR_CATCHER, {
1486
+ name: "prettier",
1487
+ message: "prettier is undefined"
1488
+ });
1489
+ return {
1490
+ text: codeMirrorUt.getValue(),
1491
+ options: { select: false, replaceAll: true }
1492
+ };
1493
+ }
1494
+ return {
1495
+ text: await prettier.format(codeMirrorUt.getValue(), {
1496
+ parser: "markdown",
1497
+ plugins: prettierPlugins
1498
+ }),
1499
+ options: { select: false, replaceAll: true }
1500
+ };
1501
+ };
1502
+ const wrappers = {
1503
+ bold: ["**", "**", 2, -2],
1504
+ underline: ["<u>", "</u>", 3, -4],
1505
+ italic: ["*", "*", 1, -1],
1506
+ strikeThrough: ["~~", "~~", 2, -2],
1507
+ sub: ["~", "~", 1, -1],
1508
+ sup: ["^", "^", 1, -1],
1509
+ codeRow: ["`", "`", 1, -1],
1510
+ katexInline: ["$", "$", 1, -1],
1511
+ katexBlock: ["\n$$\n", "\n$$\n", 4, -4]
1512
+ };
1513
+ const wrapText = (type, codeMirrorUt) => {
1514
+ const text = codeMirrorUt.getSelectedText();
1515
+ const [prefix2, subfix, deviationStart, deviationEnd] = wrappers[type];
1516
+ return {
1517
+ text: `${prefix2}${text}${subfix}`,
1518
+ options: {
1519
+ deviationStart,
1520
+ deviationEnd
1521
+ // replaceStart, replaceEnd
1522
+ }
1523
+ };
1524
+ };
1525
+ const keys = {
1526
+ quote: "> ",
1527
+ unorderedList: "- ",
1528
+ orderedList: 1,
1529
+ task: "- [ ] "
1530
+ };
1531
+ const handleMultiLine = (type, codeMirrorUt) => {
1532
+ const [text, replaceStart, replaceEnd] = getSelectedInfo(codeMirrorUt, {
1533
+ wholeLine: true
1534
+ });
1535
+ const lines = text.split("\n");
1536
+ const key = keys[type];
1537
+ const formattedLines = type === "orderedList" ? lines.map((line, i) => `${key + i}. ${line}`) : lines.map((line) => `${key}${line}`);
1538
+ const firstLinePrefix = type === "orderedList" ? "1. " : key.toString();
1539
+ const deviationStart = lines.length === 1 ? firstLinePrefix.length : 0;
1540
+ return {
1541
+ text: formattedLines.join("\n"),
1542
+ options: {
1543
+ deviationStart,
1544
+ replaceStart,
1545
+ replaceEnd
1546
+ }
1547
+ };
1548
+ };
1549
+ const handleCodeBlock = (params, codeMirrorUt) => {
1550
+ const [text, replaceStart, replaceEnd] = getSelectedInfo(codeMirrorUt);
1551
+ const mode = params.mode || "language";
1552
+ const _text = `
1553
+ \`\`\`${mode}
1554
+ ${params.text || text || ""}
1616
1555
  \`\`\`
1617
1556
  `;
1618
- deviationStart = 11;
1619
- deviationEnd = -5;
1620
- break;
1621
- }
1622
- case "relationship": {
1623
- targetValue = `\`\`\`mermaid
1624
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.relationship) || "erDiagram\n CAR ||--o{ NAMED-DRIVER : allows\n PERSON ||--o{ NAMED-DRIVER : is"}
1557
+ return {
1558
+ text: _text,
1559
+ options: {
1560
+ deviationStart: 4,
1561
+ deviationEnd: 4 + mode.length - _text.length,
1562
+ replaceStart,
1563
+ replaceEnd
1564
+ }
1565
+ };
1566
+ };
1567
+ const handleMermaid = (type) => {
1568
+ const mermaidTemplates = {
1569
+ flow: "flowchart TD \n Start --> Stop",
1570
+ sequence: "sequenceDiagram\n A->>B: hello!\n B-->>A: hi!",
1571
+ gantt: "gantt\ntitle Gantt Chart\ndateFormat YYYY-MM-DD",
1572
+ class: "classDiagram\n class Animal",
1573
+ state: "stateDiagram-v2\n s1 --> s2",
1574
+ pie: 'pie\n "Dogs" : 386\n "Cats" : 85\n "Rats" : 15',
1575
+ relationship: "erDiagram\n CAR ||--o{ NAMED-DRIVER : allows",
1576
+ journey: "journey\n title My Journey",
1577
+ ...configOption.editorConfig.mermaidTemplate
1578
+ };
1579
+ return {
1580
+ text: `
1581
+ \`\`\`mermaid
1582
+ ${mermaidTemplates[type]}
1625
1583
  \`\`\`
1584
+ `,
1585
+ options: { deviationStart: 12, deviationEnd: -5 }
1586
+ };
1587
+ };
1588
+ const handleImage = (params) => {
1589
+ const { desc, url, urls } = params;
1590
+ let text = "";
1591
+ if (urls instanceof Array) {
1592
+ text = urls.reduce((pVal, _url) => {
1593
+ const {
1594
+ url: url2 = "",
1595
+ alt = "",
1596
+ title = ""
1597
+ } = typeof _url === "object" ? _url : { url: _url };
1598
+ return pVal + `![${alt}](${url2}${title ? " '" + title + "'" : ""})
1626
1599
  `;
1627
- deviationStart = 11;
1628
- deviationEnd = -5;
1629
- break;
1630
- }
1631
- case "journey": {
1632
- targetValue = `\`\`\`mermaid
1633
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.journey) || "journey\n title My working day\n section Go to work\n Make tea: 5: Me\n Go upstairs: 3: Me\n Do work: 1: Me, Cat\n section Go home\n Go downstairs: 5: Me\n Sit down: 5: Me"}
1634
- \`\`\`
1600
+ }, "");
1601
+ } else {
1602
+ text = `![${desc}](${url})
1635
1603
  `;
1636
- deviationStart = 11;
1637
- deviationEnd = -5;
1638
- break;
1639
- }
1640
- case "katexInline": {
1641
- targetValue = "$$";
1642
- deviationStart = 1;
1643
- deviationEnd = -1;
1644
- break;
1645
- }
1646
- case "katexBlock": {
1647
- targetValue = "$$\n\n$$\n";
1648
- deviationStart = 3;
1649
- deviationEnd = -4;
1650
- break;
1651
- }
1652
- case "universal": {
1653
- const { generate } = params;
1654
- const insertOptions = generate(selectedText);
1655
- targetValue = insertOptions.targetValue;
1656
- select = insertOptions.select ?? true;
1657
- deviationStart = insertOptions.deviationStart || 0;
1658
- deviationEnd = insertOptions.deviationEnd || 0;
1659
- }
1604
+ }
1605
+ return { text, options: { select: false } };
1606
+ };
1607
+ const handleTable = (params) => {
1608
+ const { selectedShape = { x: 1, y: 1 } } = params;
1609
+ const { x, y } = selectedShape;
1610
+ let text = "\n| Column";
1611
+ for (let i = 0; i <= y; i++) {
1612
+ text += ` |`;
1613
+ }
1614
+ text += "\n|";
1615
+ for (let i = 0; i <= y; i++) {
1616
+ text += " - |";
1617
+ }
1618
+ for (let row = 0; row <= x; row++) {
1619
+ text += "\n|";
1620
+ for (let col = 0; col <= y; col++) {
1621
+ text += " |";
1660
1622
  }
1661
1623
  }
1624
+ text += "\n";
1662
1625
  return {
1663
- text: targetValue,
1626
+ text,
1664
1627
  options: {
1665
- // 是否选中
1666
- select,
1667
- // 选中时,开始位置的偏移量
1668
- deviationStart,
1669
- // 结束的偏移量
1670
- deviationEnd,
1671
- // 是否整个替换
1672
- replaceAll
1628
+ deviationStart: 3,
1629
+ deviationEnd: 10 - text.length
1673
1630
  }
1674
1631
  };
1675
1632
  };
1633
+ const handleUniversal = (selectedText, params) => {
1634
+ const { generate } = params;
1635
+ const insertOptions = generate(selectedText);
1636
+ return {
1637
+ text: insertOptions.targetValue,
1638
+ options: {
1639
+ select: insertOptions.select ?? true,
1640
+ deviationStart: insertOptions.deviationStart || 0,
1641
+ deviationEnd: insertOptions.deviationEnd || 0
1642
+ }
1643
+ };
1644
+ };
1645
+ const getSelectedInfo = (codeMirrorUt, options = {
1646
+ wholeLine: false
1647
+ }) => {
1648
+ const state = codeMirrorUt.view.state;
1649
+ const selection2 = state.selection.main;
1650
+ if (selection2.empty) {
1651
+ const startLine = state.doc.lineAt(selection2.from);
1652
+ return [state.doc.lineAt(selection2.from).text, startLine.from, startLine.to];
1653
+ } else if (options.wholeLine) {
1654
+ const startLine = state.doc.lineAt(selection2.from);
1655
+ const endLine = state.doc.lineAt(selection2.to);
1656
+ return [
1657
+ state.doc.sliceString(startLine.from, endLine.to),
1658
+ startLine.from,
1659
+ endLine.to
1660
+ ];
1661
+ }
1662
+ return [
1663
+ state.doc.sliceString(selection2.from, selection2.to),
1664
+ selection2.from,
1665
+ selection2.to
1666
+ ];
1667
+ };
1676
1668
  const chalky$1 = "#e5c07b", coral$1 = "var(--md-color)", cyan$1 = "#56b6c2", invalid$1 = "#ffffff", ivory$1 = "var(--md-color)", stone$1 = "#e5c07b", malibu$1 = "#e5c07b", sage$1 = "var(--md-color)", whiskey$1 = "#d19a66", violet$1 = "#c678dd", darkBackground$1 = "#21252b", highlightBackground$1 = "#2c313a", background$1 = "var(--md-bk-color)", tooltipBackground$1 = "var(--md-bk-color)", selection$1 = "#ceedfa33", cursor$1 = "#528bff";
1677
1669
  const oneDarkTheme = EditorView.theme(
1678
1670
  {
@@ -2080,16 +2072,22 @@ class CodeMirrorUt {
2080
2072
  * @param text 待替换内容
2081
2073
  * @param options 替换后是否选中
2082
2074
  */
2083
- replaceSelectedText(text, options = {
2084
- // 是否选中
2085
- select: true,
2086
- // 选中时,开始位置的偏移量
2087
- deviationStart: 0,
2088
- // 结束的偏移量
2089
- deviationEnd: 0,
2090
- // 直接替换所有文本
2091
- replaceAll: false
2092
- }, editorId) {
2075
+ replaceSelectedText(text, _options, editorId) {
2076
+ const options = {
2077
+ // 是否选中
2078
+ select: true,
2079
+ // 选中时,开始位置的偏移量
2080
+ deviationStart: 0,
2081
+ // 结束的偏移量
2082
+ deviationEnd: 0,
2083
+ // 直接替换所有文本
2084
+ replaceAll: false,
2085
+ // 替换旧文本的开始位置
2086
+ replaceStart: -1,
2087
+ // 替换旧文本的结束位置
2088
+ replaceEnd: -1,
2089
+ ..._options
2090
+ };
2093
2091
  try {
2094
2092
  if (options.replaceAll) {
2095
2093
  this.setValue(text);
@@ -2102,13 +2100,22 @@ class CodeMirrorUt {
2102
2100
  throw new Error("The input text is too long");
2103
2101
  }
2104
2102
  const { from } = this.view.state.selection.main;
2105
- this.view.dispatch(this.view.state.replaceSelection(text));
2103
+ if (options.replaceStart !== -1) {
2104
+ this.view.dispatch({
2105
+ changes: {
2106
+ from: options.replaceStart,
2107
+ to: options.replaceEnd,
2108
+ insert: text
2109
+ }
2110
+ });
2111
+ } else {
2112
+ this.view.dispatch(this.view.state.replaceSelection(text));
2113
+ }
2106
2114
  if (options.select) {
2107
- const to = from + text.length + options.deviationEnd;
2108
2115
  this.view.dispatch({
2109
2116
  selection: {
2110
- anchor: from + options.deviationStart,
2111
- head: to
2117
+ anchor: options.replaceStart === -1 ? from + options.deviationStart : options.replaceStart + options.deviationStart,
2118
+ head: options.replaceStart === -1 ? from + text.length + options.deviationEnd : options.replaceStart + text.length + options.deviationEnd
2112
2119
  }
2113
2120
  });
2114
2121
  }
@@ -2651,6 +2658,13 @@ const useCodeMirror = (props2) => {
2651
2658
  );
2652
2659
  }
2653
2660
  });
2661
+ bus.on(editorId, {
2662
+ name: SEND_EDITOR_VIEW,
2663
+ callback() {
2664
+ bus.emit(editorId, GET_EDITOR_VIEW, view);
2665
+ }
2666
+ });
2667
+ bus.emit(editorId, GET_EDITOR_VIEW, view);
2654
2668
  });
2655
2669
  watch(
2656
2670
  theme,
@@ -2816,6 +2830,7 @@ const useResize = (props2, contentRef, resizeRef) => {
2816
2830
  );
2817
2831
  return { inputWrapperStyle, resizeOperateStyle, showPreviewWrapper };
2818
2832
  };
2833
+ const smoothScroll = createSmoothScroll();
2819
2834
  const Content = /* @__PURE__ */ defineComponent({
2820
2835
  name: "MDEditorContent",
2821
2836
  props: contentProps,
@@ -2862,7 +2877,7 @@ const Content = /* @__PURE__ */ defineComponent({
2862
2877
  "class": `${prefix}-resize-operate`,
2863
2878
  "style": resizeOperateStyle,
2864
2879
  "ref": resizeRef
2865
- }, null), showPreviewWrapper.value && createVNode(ContentPreview, {
2880
+ }, null), createVNode(ContentPreview, {
2866
2881
  "modelValue": props2.modelValue,
2867
2882
  "onChange": props2.onChange,
2868
2883
  "setting": props2.setting,
@@ -2887,7 +2902,21 @@ const Content = /* @__PURE__ */ defineComponent({
2887
2902
  "editorId": editorId,
2888
2903
  "mdHeadingId": props2.mdHeadingId,
2889
2904
  "key": "internal-catalog",
2890
- "scrollElementOffsetTop": 2
2905
+ "scrollElementOffsetTop": 2,
2906
+ "syncWith": !props2.setting.preview ? "editor" : "preview",
2907
+ "onClick": (e, toc) => {
2908
+ var _a, _b;
2909
+ if (!props2.setting.preview && toc.line !== void 0) {
2910
+ e.preventDefault();
2911
+ const view = (_a = codeMirrorUt.value) == null ? void 0 : _a.view;
2912
+ if (view) {
2913
+ const line = view.state.doc.line(toc.line + 1);
2914
+ const top = (_b = view.lineBlockAt(line.from)) == null ? void 0 : _b.top;
2915
+ const scroller = view.scrollDOM;
2916
+ smoothScroll(scroller, top);
2917
+ }
2918
+ }
2919
+ }
2891
2920
  }, null)]);
2892
2921
  };
2893
2922
  }
@@ -69,15 +69,19 @@ const PUSH_CATALOG = "pushCatalog";
69
69
  const RERENDER = "rerender";
70
70
  const EVENT_LISTENER = "eventListener";
71
71
  const TASK_STATE_CHANGED = "taskStateChanged";
72
+ const SEND_EDITOR_VIEW = "sendEditorView";
73
+ const GET_EDITOR_VIEW = "getEditorView";
72
74
  export {
73
75
  BUILD_FINISHED as B,
74
76
  CHANGE_FULL_SCREEN as C,
75
77
  ERROR_CATCHER as E,
76
78
  FULL_SCREEN_CHANGED as F,
79
+ GET_EDITOR_VIEW as G,
77
80
  HTML_PREVIEW_CHANGED as H,
78
81
  OPEN_MODALS as O,
79
82
  PUSH_CATALOG as P,
80
83
  REPLACE as R,
84
+ SEND_EDITOR_VIEW as S,
81
85
  TASK_STATE_CHANGED as T,
82
86
  UPLOAD_IMAGE as U,
83
87
  CHANGE_CATALOG_VISIBLE as a,
@@ -584,7 +584,8 @@ const HeadingPlugin = (md, options) => {
584
584
  const level = token.markup.length;
585
585
  options.headsRef.value.push({
586
586
  text,
587
- level
587
+ level,
588
+ line: token.map[0]
588
589
  });
589
590
  if (token.map && token.level === 0) {
590
591
  token.attrSet(