md-editor-v3 5.2.1 → 5.2.2

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.
@@ -1412,268 +1412,260 @@ const useAutoScroll = (props2, html, codeMirrorUt) => {
1412
1412
  vue.onMounted(rebindEvent);
1413
1413
  };
1414
1414
  const directive2flag = async (direct, codeMirrorUt, params) => {
1415
- var _a;
1416
- let targetValue = "";
1417
- let deviationStart = 0;
1418
- let deviationEnd = 0;
1419
- let select = true;
1420
- let replaceAll = false;
1421
- const selectedText = codeMirrorUt.getSelectedText();
1422
- const mermaidTemplate = config.configOption.editorConfig.mermaidTemplate;
1423
- if (/^h[1-6]{1}$/.test(direct)) {
1424
- const pix = direct.replace(/^h(\d)/, (_, num) => {
1425
- return new Array(Number(num)).fill("#", 0, num).join("");
1426
- });
1427
- targetValue = `${pix} ${selectedText}`;
1428
- deviationStart = pix.length + 1;
1415
+ if (/^h[1-6]$/.test(direct)) {
1416
+ return handleHeading(direct, codeMirrorUt);
1429
1417
  } else if (direct === "prettier") {
1430
- const prettier = window.prettier || config.configOption.editorExtensions.prettier.prettierInstance;
1431
- const prettierPlugins = [
1432
- ((_a = window.prettierPlugins) == null ? void 0 : _a.markdown) || config.configOption.editorExtensions.prettier.parserMarkdownInstance
1433
- ];
1434
- if (!prettier || prettierPlugins[0] === void 0) {
1435
- eventName.bus.emit(params.editorId, eventName.ERROR_CATCHER, {
1436
- name: "prettier",
1437
- message: "prettier is undefined"
1438
- });
1439
- targetValue = codeMirrorUt.getValue();
1440
- } else {
1441
- targetValue = await prettier.format(codeMirrorUt.getValue(), {
1442
- parser: "markdown",
1443
- plugins: prettierPlugins
1444
- });
1418
+ return await handlePrettier(codeMirrorUt, params);
1419
+ }
1420
+ switch (direct) {
1421
+ case "bold":
1422
+ case "underline":
1423
+ case "italic":
1424
+ case "strikeThrough":
1425
+ case "sub":
1426
+ case "sup":
1427
+ case "codeRow":
1428
+ case "katexInline":
1429
+ case "katexBlock": {
1430
+ return wrapText(direct, codeMirrorUt);
1431
+ }
1432
+ case "quote":
1433
+ case "orderedList":
1434
+ case "unorderedList":
1435
+ case "task": {
1436
+ return handleMultiLine(direct, codeMirrorUt);
1437
+ }
1438
+ case "code": {
1439
+ return handleCodeBlock(params, codeMirrorUt);
1440
+ }
1441
+ case "table": {
1442
+ return handleTable(params);
1443
+ }
1444
+ case "link": {
1445
+ return { text: `[${params.desc}](${params.url})`, options: { select: false } };
1446
+ }
1447
+ case "image": {
1448
+ return handleImage(params);
1449
+ }
1450
+ case "flow":
1451
+ case "sequence":
1452
+ case "gantt":
1453
+ case "class":
1454
+ case "state":
1455
+ case "pie":
1456
+ case "relationship":
1457
+ case "journey": {
1458
+ return handleMermaid(direct);
1459
+ }
1460
+ case "universal": {
1461
+ return handleUniversal(codeMirrorUt.getSelectedText(), params);
1462
+ }
1463
+ default: {
1464
+ return { text: "", options: {} };
1445
1465
  }
1446
- select = false;
1447
- replaceAll = true;
1448
- } else {
1449
- switch (direct) {
1450
- case "bold": {
1451
- targetValue = `**${selectedText}**`;
1452
- deviationStart = 2;
1453
- deviationEnd = -2;
1454
- break;
1455
- }
1456
- case "underline": {
1457
- targetValue = `<u>${selectedText}</u>`;
1458
- deviationStart = 3;
1459
- deviationEnd = -4;
1460
- break;
1461
- }
1462
- case "italic": {
1463
- targetValue = `*${selectedText}*`;
1464
- deviationStart = 1;
1465
- deviationEnd = -1;
1466
- break;
1467
- }
1468
- case "strikeThrough": {
1469
- targetValue = `~~${selectedText}~~`;
1470
- deviationStart = 2;
1471
- deviationEnd = -2;
1472
- break;
1473
- }
1474
- case "sub": {
1475
- targetValue = `~${selectedText}~`;
1476
- deviationStart = 1;
1477
- deviationEnd = -1;
1478
- break;
1479
- }
1480
- case "sup": {
1481
- targetValue = `^${selectedText}^`;
1482
- deviationStart = 1;
1483
- deviationEnd = -1;
1484
- break;
1485
- }
1486
- case "codeRow": {
1487
- targetValue = "`" + selectedText + "`";
1488
- deviationStart = 1;
1489
- deviationEnd = -1;
1490
- break;
1491
- }
1492
- case "quote": {
1493
- targetValue = `> ${selectedText}`;
1494
- deviationStart = 2;
1495
- break;
1496
- }
1497
- case "orderedList": {
1498
- targetValue = `1. ${selectedText}`;
1499
- deviationStart = 3;
1500
- break;
1501
- }
1502
- case "unorderedList": {
1503
- targetValue = `- ${selectedText}`;
1504
- deviationStart = 2;
1505
- break;
1506
- }
1507
- case "task": {
1508
- targetValue = `- [ ] ${selectedText}`;
1509
- deviationStart = 6;
1510
- break;
1511
- }
1512
- case "code": {
1513
- const text = params.text || selectedText || "";
1514
- const mode = params.mode || "language";
1515
- targetValue = `\`\`\`${mode}
1516
- ${text}
1517
- \`\`\`
1518
- `;
1519
- deviationStart = 3;
1520
- deviationEnd = 3 + mode.length - targetValue.length;
1521
- break;
1522
- }
1523
- case "table": {
1524
- targetValue = "|";
1525
- const { selectedShape = { x: 1, y: 1 } } = params;
1526
- const { x, y } = selectedShape;
1527
- for (let i = 0; i <= y; i++) {
1528
- targetValue += " col |";
1529
- }
1530
- targetValue += "\n|";
1531
- for (let i = 0; i <= y; i++) {
1532
- targetValue += " - |";
1533
- }
1534
- for (let row = 0; row <= x; row++) {
1535
- targetValue += "\n|";
1536
- for (let col = 0; col <= y; col++) {
1537
- targetValue += " content |";
1538
- }
1539
- }
1540
- deviationStart = 2;
1541
- deviationEnd = 5 - targetValue.length;
1542
- break;
1543
- }
1544
- case "link": {
1545
- const { desc, url } = params;
1546
- targetValue = `[${desc}](${url})`;
1547
- select = false;
1548
- break;
1549
- }
1550
- case "image": {
1551
- const { desc, url, urls } = params;
1552
- if (urls instanceof Array) {
1553
- targetValue = urls.reduce((pVal, _url) => {
1554
- const {
1555
- url: url2 = "",
1556
- alt = "",
1557
- title = ""
1558
- } = typeof _url === "object" ? _url : { url: _url };
1559
- return pVal + `![${alt}](${url2}${title ? " '" + title + "'" : ""})
1560
- `;
1561
- }, "");
1562
- } else {
1563
- targetValue = `![${desc}](${url})
1564
- `;
1565
- }
1566
- select = false;
1567
- break;
1568
- }
1569
- case "flow": {
1570
- targetValue = `\`\`\`mermaid
1571
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.flow) || "flowchart TD \n Start --> Stop"}
1572
- \`\`\`
1573
- `;
1574
- deviationStart = 11;
1575
- deviationEnd = -5;
1576
- break;
1577
- }
1578
- case "sequence": {
1579
- targetValue = `\`\`\`mermaid
1580
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.sequence) || "sequenceDiagram\n A->>B: hello!\n B-->>A: hi!\n A-)B: bye!"}
1581
- \`\`\`
1582
- `;
1583
- deviationStart = 11;
1584
- deviationEnd = -5;
1585
- break;
1586
- }
1587
- case "gantt": {
1588
- targetValue = `\`\`\`mermaid
1589
- ${(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"}
1590
- \`\`\`
1591
- `;
1592
- deviationStart = 11;
1593
- deviationEnd = -5;
1594
- break;
1595
- }
1596
- case "class": {
1597
- targetValue = `\`\`\`mermaid
1598
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.class) || "classDiagram\n class Animal\n Vehicle <|-- Car"}
1599
- \`\`\`
1600
- `;
1601
- deviationStart = 11;
1602
- deviationEnd = -5;
1603
- break;
1604
- }
1605
- case "state": {
1606
- targetValue = `\`\`\`mermaid
1607
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.state) || "stateDiagram-v2\n s1 --> s2"}
1608
- \`\`\`
1609
- `;
1610
- deviationStart = 11;
1611
- deviationEnd = -5;
1612
- break;
1613
- }
1614
- case "pie": {
1615
- targetValue = `\`\`\`mermaid
1616
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.pie) || 'pie title Pets adopted by volunteers\n "Dogs" : 386\n "Cats" : 85\n "Rats" : 15'}
1466
+ }
1467
+ };
1468
+ const handleHeading = (direct, codeMirrorUt) => {
1469
+ const level = direct.slice(1);
1470
+ const prefix = "#".repeat(Number(level));
1471
+ const [text, replaceStart, replaceEnd] = getSelectedInfo(codeMirrorUt, {
1472
+ wholeLine: true
1473
+ });
1474
+ return {
1475
+ text: `${prefix} ${text}`,
1476
+ options: { deviationStart: prefix.length + 1, replaceStart, replaceEnd }
1477
+ };
1478
+ };
1479
+ const handlePrettier = async (codeMirrorUt, params) => {
1480
+ var _a, _b, _c;
1481
+ const prettier = window.prettier || ((_a = config.configOption.editorExtensions.prettier) == null ? void 0 : _a.prettierInstance);
1482
+ const prettierPlugins = [
1483
+ ((_b = window.prettierPlugins) == null ? void 0 : _b.markdown) || ((_c = config.configOption.editorExtensions.prettier) == null ? void 0 : _c.parserMarkdownInstance)
1484
+ ];
1485
+ if (!prettier || !prettierPlugins[0]) {
1486
+ eventName.bus.emit(params.editorId, eventName.ERROR_CATCHER, {
1487
+ name: "prettier",
1488
+ message: "prettier is undefined"
1489
+ });
1490
+ return {
1491
+ text: codeMirrorUt.getValue(),
1492
+ options: { select: false, replaceAll: true }
1493
+ };
1494
+ }
1495
+ return {
1496
+ text: await prettier.format(codeMirrorUt.getValue(), {
1497
+ parser: "markdown",
1498
+ plugins: prettierPlugins
1499
+ }),
1500
+ options: { select: false, replaceAll: true }
1501
+ };
1502
+ };
1503
+ const wrappers = {
1504
+ bold: ["**", "**", 2, -2],
1505
+ underline: ["<u>", "</u>", 3, -4],
1506
+ italic: ["*", "*", 1, -1],
1507
+ strikeThrough: ["~~", "~~", 2, -2],
1508
+ sub: ["~", "~", 1, -1],
1509
+ sup: ["^", "^", 1, -1],
1510
+ codeRow: ["`", "`", 1, -1],
1511
+ katexInline: ["$", "$", 1, -1],
1512
+ katexBlock: ["\n$$\n", "\n$$\n", 4, -4]
1513
+ };
1514
+ const wrapText = (type, codeMirrorUt) => {
1515
+ const text = codeMirrorUt.getSelectedText();
1516
+ const [prefix, subfix, deviationStart, deviationEnd] = wrappers[type];
1517
+ return {
1518
+ text: `${prefix}${text}${subfix}`,
1519
+ options: {
1520
+ deviationStart,
1521
+ deviationEnd
1522
+ // replaceStart, replaceEnd
1523
+ }
1524
+ };
1525
+ };
1526
+ const keys = {
1527
+ quote: "> ",
1528
+ unorderedList: "- ",
1529
+ orderedList: 1,
1530
+ task: "- [ ] "
1531
+ };
1532
+ const handleMultiLine = (type, codeMirrorUt) => {
1533
+ const [text, replaceStart, replaceEnd] = getSelectedInfo(codeMirrorUt, {
1534
+ wholeLine: true
1535
+ });
1536
+ const lines = text.split("\n");
1537
+ const key = keys[type];
1538
+ const formattedLines = type === "orderedList" ? lines.map((line, i) => `${key + i}. ${line}`) : lines.map((line) => `${key}${line}`);
1539
+ const firstLinePrefix = type === "orderedList" ? "1. " : key.toString();
1540
+ const deviationStart = lines.length === 1 ? firstLinePrefix.length : 0;
1541
+ return {
1542
+ text: formattedLines.join("\n"),
1543
+ options: {
1544
+ deviationStart,
1545
+ replaceStart,
1546
+ replaceEnd
1547
+ }
1548
+ };
1549
+ };
1550
+ const handleCodeBlock = (params, codeMirrorUt) => {
1551
+ const [text, replaceStart, replaceEnd] = getSelectedInfo(codeMirrorUt);
1552
+ const mode = params.mode || "language";
1553
+ const _text = `
1554
+ \`\`\`${mode}
1555
+ ${params.text || text || ""}
1617
1556
  \`\`\`
1618
1557
  `;
1619
- deviationStart = 11;
1620
- deviationEnd = -5;
1621
- break;
1622
- }
1623
- case "relationship": {
1624
- targetValue = `\`\`\`mermaid
1625
- ${(mermaidTemplate == null ? void 0 : mermaidTemplate.relationship) || "erDiagram\n CAR ||--o{ NAMED-DRIVER : allows\n PERSON ||--o{ NAMED-DRIVER : is"}
1558
+ return {
1559
+ text: _text,
1560
+ options: {
1561
+ deviationStart: 4,
1562
+ deviationEnd: 4 + mode.length - _text.length,
1563
+ replaceStart,
1564
+ replaceEnd
1565
+ }
1566
+ };
1567
+ };
1568
+ const handleMermaid = (type) => {
1569
+ const mermaidTemplates = {
1570
+ flow: "flowchart TD \n Start --> Stop",
1571
+ sequence: "sequenceDiagram\n A->>B: hello!\n B-->>A: hi!",
1572
+ gantt: "gantt\ntitle Gantt Chart\ndateFormat YYYY-MM-DD",
1573
+ class: "classDiagram\n class Animal",
1574
+ state: "stateDiagram-v2\n s1 --> s2",
1575
+ pie: 'pie\n "Dogs" : 386\n "Cats" : 85\n "Rats" : 15',
1576
+ relationship: "erDiagram\n CAR ||--o{ NAMED-DRIVER : allows",
1577
+ journey: "journey\n title My Journey",
1578
+ ...config.configOption.editorConfig.mermaidTemplate
1579
+ };
1580
+ return {
1581
+ text: `
1582
+ \`\`\`mermaid
1583
+ ${mermaidTemplates[type]}
1626
1584
  \`\`\`
1585
+ `,
1586
+ options: { deviationStart: 12, deviationEnd: -5 }
1587
+ };
1588
+ };
1589
+ const handleImage = (params) => {
1590
+ const { desc, url, urls } = params;
1591
+ let text = "";
1592
+ if (urls instanceof Array) {
1593
+ text = urls.reduce((pVal, _url) => {
1594
+ const {
1595
+ url: url2 = "",
1596
+ alt = "",
1597
+ title = ""
1598
+ } = typeof _url === "object" ? _url : { url: _url };
1599
+ return pVal + `![${alt}](${url2}${title ? " '" + title + "'" : ""})
1627
1600
  `;
1628
- deviationStart = 11;
1629
- deviationEnd = -5;
1630
- break;
1631
- }
1632
- case "journey": {
1633
- targetValue = `\`\`\`mermaid
1634
- ${(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"}
1635
- \`\`\`
1601
+ }, "");
1602
+ } else {
1603
+ text = `![${desc}](${url})
1636
1604
  `;
1637
- deviationStart = 11;
1638
- deviationEnd = -5;
1639
- break;
1640
- }
1641
- case "katexInline": {
1642
- targetValue = "$$";
1643
- deviationStart = 1;
1644
- deviationEnd = -1;
1645
- break;
1646
- }
1647
- case "katexBlock": {
1648
- targetValue = "$$\n\n$$\n";
1649
- deviationStart = 3;
1650
- deviationEnd = -4;
1651
- break;
1652
- }
1653
- case "universal": {
1654
- const { generate } = params;
1655
- const insertOptions = generate(selectedText);
1656
- targetValue = insertOptions.targetValue;
1657
- select = insertOptions.select ?? true;
1658
- deviationStart = insertOptions.deviationStart || 0;
1659
- deviationEnd = insertOptions.deviationEnd || 0;
1660
- }
1605
+ }
1606
+ return { text, options: { select: false } };
1607
+ };
1608
+ const handleTable = (params) => {
1609
+ const { selectedShape = { x: 1, y: 1 } } = params;
1610
+ const { x, y } = selectedShape;
1611
+ let text = "\n| Column";
1612
+ for (let i = 0; i <= y; i++) {
1613
+ text += ` |`;
1614
+ }
1615
+ text += "\n|";
1616
+ for (let i = 0; i <= y; i++) {
1617
+ text += " - |";
1618
+ }
1619
+ for (let row = 0; row <= x; row++) {
1620
+ text += "\n|";
1621
+ for (let col = 0; col <= y; col++) {
1622
+ text += " |";
1661
1623
  }
1662
1624
  }
1625
+ text += "\n";
1663
1626
  return {
1664
- text: targetValue,
1627
+ text,
1665
1628
  options: {
1666
- // 是否选中
1667
- select,
1668
- // 选中时,开始位置的偏移量
1669
- deviationStart,
1670
- // 结束的偏移量
1671
- deviationEnd,
1672
- // 是否整个替换
1673
- replaceAll
1629
+ deviationStart: 3,
1630
+ deviationEnd: 10 - text.length
1631
+ }
1632
+ };
1633
+ };
1634
+ const handleUniversal = (selectedText, params) => {
1635
+ const { generate } = params;
1636
+ const insertOptions = generate(selectedText);
1637
+ return {
1638
+ text: insertOptions.targetValue,
1639
+ options: {
1640
+ select: insertOptions.select ?? true,
1641
+ deviationStart: insertOptions.deviationStart || 0,
1642
+ deviationEnd: insertOptions.deviationEnd || 0
1674
1643
  }
1675
1644
  };
1676
1645
  };
1646
+ const getSelectedInfo = (codeMirrorUt, options = {
1647
+ wholeLine: false
1648
+ }) => {
1649
+ const state2 = codeMirrorUt.view.state;
1650
+ const selection2 = state2.selection.main;
1651
+ if (selection2.empty) {
1652
+ const startLine = state2.doc.lineAt(selection2.from);
1653
+ return [state2.doc.lineAt(selection2.from).text, startLine.from, startLine.to];
1654
+ } else if (options.wholeLine) {
1655
+ const startLine = state2.doc.lineAt(selection2.from);
1656
+ const endLine = state2.doc.lineAt(selection2.to);
1657
+ return [
1658
+ state2.doc.sliceString(startLine.from, endLine.to),
1659
+ startLine.from,
1660
+ endLine.to
1661
+ ];
1662
+ }
1663
+ return [
1664
+ state2.doc.sliceString(selection2.from, selection2.to),
1665
+ selection2.from,
1666
+ selection2.to
1667
+ ];
1668
+ };
1677
1669
  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";
1678
1670
  const oneDarkTheme = view.EditorView.theme(
1679
1671
  {
@@ -2081,16 +2073,22 @@ class CodeMirrorUt {
2081
2073
  * @param text 待替换内容
2082
2074
  * @param options 替换后是否选中
2083
2075
  */
2084
- replaceSelectedText(text, options = {
2085
- // 是否选中
2086
- select: true,
2087
- // 选中时,开始位置的偏移量
2088
- deviationStart: 0,
2089
- // 结束的偏移量
2090
- deviationEnd: 0,
2091
- // 直接替换所有文本
2092
- replaceAll: false
2093
- }, editorId) {
2076
+ replaceSelectedText(text, _options, editorId) {
2077
+ const options = {
2078
+ // 是否选中
2079
+ select: true,
2080
+ // 选中时,开始位置的偏移量
2081
+ deviationStart: 0,
2082
+ // 结束的偏移量
2083
+ deviationEnd: 0,
2084
+ // 直接替换所有文本
2085
+ replaceAll: false,
2086
+ // 替换旧文本的开始位置
2087
+ replaceStart: -1,
2088
+ // 替换旧文本的结束位置
2089
+ replaceEnd: -1,
2090
+ ..._options
2091
+ };
2094
2092
  try {
2095
2093
  if (options.replaceAll) {
2096
2094
  this.setValue(text);
@@ -2103,13 +2101,22 @@ class CodeMirrorUt {
2103
2101
  throw new Error("The input text is too long");
2104
2102
  }
2105
2103
  const { from } = this.view.state.selection.main;
2106
- this.view.dispatch(this.view.state.replaceSelection(text));
2104
+ if (options.replaceStart !== -1) {
2105
+ this.view.dispatch({
2106
+ changes: {
2107
+ from: options.replaceStart,
2108
+ to: options.replaceEnd,
2109
+ insert: text
2110
+ }
2111
+ });
2112
+ } else {
2113
+ this.view.dispatch(this.view.state.replaceSelection(text));
2114
+ }
2107
2115
  if (options.select) {
2108
- const to = from + text.length + options.deviationEnd;
2109
2116
  this.view.dispatch({
2110
2117
  selection: {
2111
- anchor: from + options.deviationStart,
2112
- head: to
2118
+ anchor: options.replaceStart === -1 ? from + options.deviationStart : options.replaceStart + options.deviationStart,
2119
+ head: options.replaceStart === -1 ? from + text.length + options.deviationEnd : options.replaceStart + text.length + options.deviationEnd
2113
2120
  }
2114
2121
  });
2115
2122
  }