Skip to content

Commit

Permalink
fix #113
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Feb 8, 2020
1 parent 6f33277 commit 84647f0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
### v2.1.11 / 2020-02-07

* [117](https://github.com/Vanessa219/vditor/issues/117) cursor moving problem near inline code `修复缺陷`
* [114](https://github.com/Vanessa219/vditor/issues/114) 有序列表合并问题 `修复缺陷`
* [115](https://github.com/Vanessa219/vditor/issues/115) codeblock difference between ``` and ctrl+u `修复缺陷`
* [114](https://github.com/Vanessa219/vditor/issues/114) 有序列表合并问题 `修复缺陷`
* [113](https://github.com/Vanessa219/vditor/issues/113) 分隔线、标题在换行时处理 `改进功能`
* [112](https://github.com/Vanessa219/vditor/issues/112) 列表项加代码块问题 `修复缺陷`
* [111](https://github.com/Vanessa219/vditor/issues/111) ctrl+b, ctrl+i, ctrl+s not working at starting and combining `修复缺陷`
* [109](https://github.com/Vanessa219/vditor/issues/109) tab key at list `改进功能`
Expand Down
3 changes: 2 additions & 1 deletion src/ts/wysiwyg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {highlightToolbar} from "./highlightToolbar";
import {input} from "./input";
import {insertHTML} from "./insertHTML";
import {processCodeRender, showCode} from "./processCodeRender";
import {isHeadingMD, isHrMD} from "./processMD";

class WYSIWYG {
public element: HTMLPreElement;
Expand Down Expand Up @@ -250,7 +251,7 @@ class WYSIWYG {
}
}

if (startSpace || endSpace) {
if (startSpace || endSpace || isHrMD(blockElement.innerHTML) || isHeadingMD(blockElement.innerHTML)) {
return;
}

Expand Down
28 changes: 25 additions & 3 deletions src/ts/wysiwyg/processKeydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {processKeymap} from "../util/processKeymap";
import {afterRenderEvent} from "./afterRenderEvent";
import {nextIsCode} from "./inlineTag";
import {processCodeRender, showCode} from "./processCodeRender";
import {isHeadingMD, isHrMD} from "./processMD";
import {setHeading} from "./setHeading";
import {setRangeByWbr} from "./setRangeByWbr";

Expand All @@ -32,12 +33,13 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
const range = getSelection().getRangeAt(0);
const startContainer = range.startContainer;

// table 自动完成
// md 处理
const pElement = hasClosestByMatchTag(startContainer, "P");
if (pElement && !event.metaKey && !event.ctrlKey && !event.altKey && event.key === "Enter") {
const pText = String.raw`${pElement.textContent}`.replace(/\\\|/g, "").trim();
const pTextList = pText.split("|");
if (pText.startsWith("|") && pText.endsWith("|") && pTextList.length > 3) {
// table 自动完成
let tableHeaderMD = pTextList.map(() => "---").join("|");
tableHeaderMD = pElement.textContent + tableHeaderMD.substring(3, tableHeaderMD.length - 3) + "\n|<wbr>";
pElement.outerHTML = vditor.lute.SpinVditorDOM(tableHeaderMD);
Expand All @@ -47,6 +49,26 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
event.preventDefault();
return true;
}

if (isHrMD(pElement.innerHTML)) {
// hr 渲染
pElement.innerHTML = "<hr><wbr>";
setRangeByWbr(vditor.wysiwyg.element, range);
afterRenderEvent(vditor);
scrollCenter(vditor.wysiwyg.element);
event.preventDefault();
return true;
}

if (isHeadingMD(pElement.innerHTML)) {
// heading 渲染
pElement.outerHTML = vditor.lute.SpinVditorDOM(pElement.innerHTML + "<wbr>");
setRangeByWbr(vditor.wysiwyg.element, range);
afterRenderEvent(vditor);
scrollCenter(vditor.wysiwyg.element);
event.preventDefault();
return true;
}
}

// table
Expand Down Expand Up @@ -551,7 +573,7 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
return true;
}

// shift+enter:软换行,但 table 自动完成、cell 内换行、block render 换行、li 软换行处理单独写在上面
// shift+enter:软换行,但 md 处理、cell 内换行、block render 换行、li 软换行处理单独写在上面
if (!event.metaKey && !event.ctrlKey && event.shiftKey && !event.altKey && event.key === "Enter") {
range.insertNode(document.createTextNode("\n"));
range.collapse(false);
Expand Down Expand Up @@ -606,7 +628,7 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
}
}

// 除 table 自动完成、cell 内换行、table 添加新行/列、代码块语言切换、block render 换行、跳出/逐层跳出 blockquote、h6 换行、
// 除 md 处理、cell 内换行、table 添加新行/列、代码块语言切换、block render 换行、跳出/逐层跳出 blockquote、h6 换行、
// 任务列表换行、软换行外需在换行时调整文档位置
if (event.key === "Enter") {
scrollCenter(vditor.wysiwyg.element);
Expand Down
19 changes: 19 additions & 0 deletions src/ts/wysiwyg/processMD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const isHrMD = (text: string) => {
// - _ *
if (text.replace(/ |-/g, "") === ""
|| text.replace(/ |_/g, "") === ""
|| text.replace(/ |\*/g, "") === "") {
return true;
}
return false;
};

export const isHeadingMD = (text: string) => {
// - =
text = text.split("\n").pop()
if (text.replace(/-/g, "") === ""
|| text.replace(/=/g, "") === "") {
return true;
}
return false;
};

0 comments on commit 84647f0

Please sign in to comment.