Skip to content

Commit

Permalink
🐛 fix #243
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Mar 27, 2020
1 parent 022b055 commit 88e0af9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@

* [241](https://github.com/Vanessa219/vditor/issues/241) When editing the heading, not converted using the cursor key `修复缺陷`
* [242](https://github.com/Vanessa219/vditor/issues/242) combined lists "+" and "-" `修复缺陷`
* [248](https://github.com/Vanessa219/vditor/issues/248) enter after heading `修复缺陷`
* [243](https://github.com/Vanessa219/vditor/issues/243) can not delete the table with "backspace" `修复缺陷`
* [246](https://github.com/Vanessa219/vditor/issues/246) 报错index.min.js:27 `修复缺陷`
* [248](https://github.com/Vanessa219/vditor/issues/248) enter after heading `修复缺陷`

### v3.0.5 / 2020-03-26

Expand Down
17 changes: 16 additions & 1 deletion src/ts/ir/processKeydown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import {Constants} from "../constants";
import {isCtrl} from "../util/compatibility";
import {scrollCenter} from "../util/editorCommenEvent";
import {fixBlockquote, fixCodeBlock, fixList, fixMarkdown, fixTab, fixTable, fixTask} from "../util/fixBrowserBehavior";
import {
fixBlockquote,
fixCodeBlock,
fixDelete,
fixList,
fixMarkdown,
fixTab,
fixTable,
fixTask,
} from "../util/fixBrowserBehavior";
import {hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "../util/hasClosest";
import {getSelectPosition, setRangeByWbr} from "../util/selection";

Expand Down Expand Up @@ -132,6 +141,12 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
return true;
}

if (event.key === "Backspace" && !isCtrl(event) && !event.shiftKey && !event.altKey && range.toString() === "") {
if (fixDelete(range, event)) {
return true;
}
}

if (event.key === "Enter") {
scrollCenter(vditor.ir.element);
}
Expand Down
57 changes: 38 additions & 19 deletions src/ts/util/fixBrowserBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const goPreviousCell = (cellElement: HTMLElement, range: Range, isSelected = tru
range.collapse(false);
}
}
return previousElement
};

export const listToggle = (vditor: IVditor, range: Range, type: string, cancel = true) => {
Expand Down Expand Up @@ -455,14 +456,6 @@ export const fixTable = (vditor: IVditor, event: KeyboardEvent, range: Range) =>
return true;
}

// Backspace:光标移动到前一个 cell
if (!isCtrl(event) && !event.shiftKey && !event.altKey && event.key === "Backspace"
&& range.startOffset === 0 && range.toString() === "") {
goPreviousCell(cellElement, range, false);
event.preventDefault();
return true;
}

// tab:光标移向下一个 cell
if (event.key === "Tab") {
if (event.shiftKey) {
Expand Down Expand Up @@ -532,8 +525,31 @@ export const fixTable = (vditor: IVditor, event: KeyboardEvent, range: Range) =>
return true;
}

// 后方新添加一列
// focus row input, only wysiwyg
if (vditor.currentMode === "wysiwyg" &&
!isCtrl(event) && event.key === "Enter" && !event.shiftKey && event.altKey) {
const inputElement = (vditor.wysiwyg.popover.querySelector(".vditor-input") as HTMLInputElement);
inputElement.focus();
inputElement.select();
event.preventDefault();
return true;
}

// Backspace:光标移动到前一个 cell
const tableElement = cellElement.parentElement.parentElement.parentElement as HTMLTableElement;
if (!isCtrl(event) && !event.shiftKey && !event.altKey && event.key === "Backspace"
&& range.startOffset === 0 && range.toString() === "") {
const previousCellElement = goPreviousCell(cellElement, range, false);
if (!previousCellElement && tableElement) {
tableElement.outerHTML = `<p data-block="0"><wbr>${tableElement.textContent}</p>`
setRangeByWbr(vditor[vditor.currentMode].element, range);
execAfterRender(vditor);
}
event.preventDefault();
return true;
}

// 后方新添加一列
if (matchHotKey("⌘-⇧-=", event)) {
let index = 0;
let previousElement = cellElement.previousElementSibling;
Expand Down Expand Up @@ -627,16 +643,6 @@ export const fixTable = (vditor: IVditor, event: KeyboardEvent, range: Range) =>
}
}
}

// focus row input
if (vditor.currentMode === "wysiwyg" &&
!isCtrl(event) && event.key === "Enter" && !event.shiftKey && event.altKey) {
const inputElement = (vditor.wysiwyg.popover.querySelector(".vditor-input") as HTMLInputElement);
inputElement.focus();
inputElement.select();
event.preventDefault();
return true;
}
}
return false;
};
Expand Down Expand Up @@ -858,3 +864,16 @@ export const fixTask = (vditor: IVditor, range: Range, event: KeyboardEvent) =>
}
return false;
};

export const fixDelete = (range: Range, event: KeyboardEvent) => {
const offsetChildNode = range.startContainer.childNodes[range.startOffset] as HTMLElement;
if (range.startContainer.nodeType !== 3 && offsetChildNode && range.startOffset > 0 &&
(offsetChildNode.tagName === "TABLE" || offsetChildNode.tagName === "HR")) {
// 光标位于 table/hr 前,table/hr 前有内容
range.selectNodeContents(offsetChildNode.previousElementSibling);
range.collapse(false);
event.preventDefault();
return true;
}
return false;
};
11 changes: 2 additions & 9 deletions src/ts/wysiwyg/processKeydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {isCtrl} from "../util/compatibility";
import {scrollCenter} from "../util/editorCommenEvent";
import {
fixBlockquote,
fixCodeBlock,
fixCodeBlock, fixDelete,
fixList,
fixMarkdown,
fixTab,
Expand Down Expand Up @@ -263,16 +263,9 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {

// 删除
if (event.key === "Backspace" && !isCtrl(event) && !event.shiftKey && !event.altKey && range.toString() === "") {
const offsetChildNode = startContainer.childNodes[range.startOffset] as HTMLElement;
if (startContainer.nodeType !== 3 && offsetChildNode && range.startOffset > 0 &&
(offsetChildNode.tagName === "TABLE" || offsetChildNode.tagName === "HR")) {
// 光标位于 table/hr 前,table/hr 前有内容
range.selectNodeContents(offsetChildNode.previousElementSibling);
range.collapse(false);
event.preventDefault();
if (fixDelete(range, event)) {
return true;
}

if (blockElement) {
if (blockElement.previousElementSibling
&& blockElement.previousElementSibling.classList.contains("vditor-wysiwyg__block")
Expand Down

0 comments on commit 88e0af9

Please sign in to comment.