Van 5 years ago
parent
commit
d13e02c1de
3 changed files with 12 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 1 1
      demo/static.html
  3. 10 0
      src/ts/toolbar/Headings.ts

+ 1 - 0
CHANGELOG.md

@@ -40,6 +40,7 @@
 
 ### v2.0.14 / 2020-01-08
 
+* [50](https://github.com/Vanessa219/vditor/issues/50) 标题前的段落结尾为 `\n` 时,标题的选中和取消会关联到 `\n` `修复缺陷`
 * [48](https://github.com/Vanessa219/vditor/issues/48) H6 回车 解析问题 `修复缺陷`
 * [47](https://github.com/Vanessa219/vditor/issues/47) <details> 改进 `功能改进`
 * [46](https://github.com/Vanessa219/vditor/issues/46) add row 添加快捷键 `引入特性`

+ 1 - 1
demo/static.html

@@ -35,7 +35,7 @@
     <meta property="og:image" content="https://cdn.jsdelivr.net/npm/vditor/dist/images/logo.png"/>
     <meta name="twitter:image" content="https://cdn.jsdelivr.net/npm/vditor/dist/images/logo.png"/>
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vditor@latest/dist/index.classic.css"/>
-    <script src="https://cdn.jsdelivr.net/npm/vditor@1.10.10/dist/index.min.js"></script>
+    <script src="https://cdn.jsdelivr.net/npm/vditor@2.0.13/dist/index.min.js"></script>
 </head>
 <body>
 <h2><a href="https://hacpai.com/article/1549638745630?r=Vanessa" target="_blank">Doc</a></h2>

+ 10 - 0
src/ts/toolbar/Headings.ts

@@ -33,6 +33,11 @@ export class Headings extends MenuItem {
                     vditor.wysiwyg.element.querySelector("wbr").remove();
                 }
                 document.execCommand("formatBlock", false, "p");
+                // https://github.com/Vanessa219/vditor/issues/50
+                const range = getSelection().getRangeAt(0);
+                if (!range.collapsed && !range.startContainer.isEqualNode(range.endContainer)) {
+                    range.setStart(range.endContainer, 0);
+                }
                 highlightToolbar(vditor);
             } else {
                 if (headingsPanelElement.style.display === "block") {
@@ -55,6 +60,11 @@ export class Headings extends MenuItem {
             headingsPanelElement.children.item(i).addEventListener(getEventName(), (event: Event) => {
                 if (vditor.currentMode === "wysiwyg") {
                     document.execCommand("formatblock", false, (event.target as HTMLElement).tagName.toLowerCase());
+                    // https://github.com/Vanessa219/vditor/issues/50
+                    const range = getSelection().getRangeAt(0);
+                    if (!range.collapsed && !range.startContainer.isEqualNode(range.endContainer)) {
+                        range.setStart(range.endContainer, 0);
+                    }
                     highlightToolbar(vditor);
                 } else {
                     insertText(vditor, (event.target as HTMLElement).getAttribute("data-value"), "",