Skip to content

Commit

Permalink
🐛 fix Vanessa219#220
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 authored and stevapple committed Apr 8, 2020
1 parent e0f4c25 commit 2b08490
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

### v3.0.0 / 未发布

* [220](https://github.com/Vanessa219/vditor/issues/220) 软换行前进行删除,将会变为 p `修复缺陷`
* [221](https://github.com/Vanessa219/vditor/issues/221) 输入复选框时出现乱码 `修复缺陷`
* [222](https://github.com/Vanessa219/vditor/issues/222) The cursor does not enter when added in the middle of the list. `修复缺陷`
* 文档更新
Expand Down
4 changes: 2 additions & 2 deletions src/ts/markdown/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const loadLuteJs = (vditor: IVditor | string) => {
// cdn = vditor.options.cdn;
// }
// addScript(`${cdn}/dist/js/lute/lute.min.js`, "vditorLuteScript");
// addScript(`/src/js/lute/lute.min.js`, "vditorLuteScript");
addScript(`http://192.168.2.248:9090/lute.min.js?${new Date().getTime()}`, "vditorLuteScript");
addScript(`/src/js/lute/lute.min.js`, "vditorLuteScript");
// addScript(`http://192.168.2.248:9090/lute.min.js?${new Date().getTime()}`, "vditorLuteScript");

if (vditor && typeof vditor === "object" && !vditor.lute) {
vditor.lute = Lute.New();
Expand Down
89 changes: 53 additions & 36 deletions src/ts/wysiwyg/processKeydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,48 +64,65 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {

// md 处理
const pElement = hasClosestByMatchTag(startContainer, "P");
if (pElement && !isCtrl(event) && !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);
setRangeByWbr(vditor.wysiwyg.element, range);
afterRenderEvent(vditor);
scrollCenter(vditor.wysiwyg.element);
event.preventDefault();
return true;
}
if (pElement) {
if (!isCtrl(event) && !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);
setRangeByWbr(vditor.wysiwyg.element, range);
afterRenderEvent(vditor);
scrollCenter(vditor.wysiwyg.element);
event.preventDefault();
return true;
}

// hr 渲染
if (isHrMD(pElement.innerHTML)) {
// 软换行后 hr 前有内容
let pInnerHTML = "";
const innerHTMLList = pElement.innerHTML.trimRight().split("\n");
if (innerHTMLList.length > 1) {
innerHTMLList.pop();
pInnerHTML = `<p data-block="0">${innerHTMLList.join("\n")}</p>`;
}

// hr 渲染
if (isHrMD(pElement.innerHTML)) {
// 软换行后 hr 前有内容
let pInnerHTML = "";
const innerHTMLList = pElement.innerHTML.trimRight().split("\n");
if (innerHTMLList.length > 1) {
innerHTMLList.pop();
pInnerHTML = `<p data-block="0">${innerHTMLList.join("\n")}</p>`;
pElement.insertAdjacentHTML("afterend",
`${pInnerHTML}<hr data-block="0"><p data-block="0">\n<wbr></p>`);
pElement.remove();
setRangeByWbr(vditor.wysiwyg.element, range);
afterRenderEvent(vditor);
scrollCenter(vditor.wysiwyg.element);
event.preventDefault();
return true;
}

pElement.insertAdjacentHTML("afterend", pInnerHTML + '<hr data-block="0"><p data-block="0">\n<wbr></p>');
pElement.remove();
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 + '<p data-block="0">\n<wbr></p>');
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 + '<p data-block="0">\n<wbr></p>');
// 软换行会被切割 https://github.com/Vanessa219/vditor/issues/220
if (pElement.previousElementSibling && event.key === "Backspace" && !isCtrl(event) && !event.altKey &&
!event.shiftKey && pElement.textContent.trimRight().split("\n").length > 1 &&
getSelectPosition(pElement, range).start === 0) {
const lastElement = getLastNode(pElement.previousElementSibling) as HTMLElement
if (!lastElement.textContent.endsWith('\n')) {
lastElement.textContent = lastElement.textContent + '\n'
}
lastElement.parentElement.insertAdjacentHTML("beforeend", `<wbr>${pElement.innerHTML}`);
pElement.remove();
setRangeByWbr(vditor.wysiwyg.element, range);
afterRenderEvent(vditor);
scrollCenter(vditor.wysiwyg.element);
event.preventDefault();
return true;
}
}

Expand Down

0 comments on commit 2b08490

Please sign in to comment.