Skip to content

Commit

Permalink
🐛 fix #367 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed May 7, 2020
1 parent 3ac6b09 commit d20bbcb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
19 changes: 2 additions & 17 deletions src/ts/toolbar/InsertAfter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {Constants} from "../constants";
import {highlightToolbar as highlightToolbarIR} from "../ir/highlightToolbar";
import {getEventName} from "../util/compatibility";
import {execAfterRender} from "../util/fixBrowserBehavior";
import {hasClosestBlock} from "../util/hasClosest";
import {getEditorRange, setRangeByWbr} from "../util/selection";
import {highlightToolbar} from "../wysiwyg/highlightToolbar";
import {insertEmptyBlock} from "../util/fixBrowserBehavior";
import {MenuItem} from "./MenuItem";

export class InsertAfter extends MenuItem {
Expand All @@ -16,18 +12,7 @@ export class InsertAfter extends MenuItem {
vditor.currentMode === "sv") {
return;
}
const range = getEditorRange(vditor[vditor.currentMode].element);
const blockElement = hasClosestBlock(range.startContainer);
if (blockElement) {
blockElement.insertAdjacentHTML("afterend", `<p data-block="0">${Constants.ZWSP}<wbr>\n</p>`);
setRangeByWbr(vditor[vditor.currentMode].element, range);
if (vditor.currentMode === "ir") {
highlightToolbarIR(vditor);
} else {
highlightToolbar(vditor);
}
execAfterRender(vditor);
}
insertEmptyBlock(vditor, "afterend");
});
}
}
19 changes: 2 additions & 17 deletions src/ts/toolbar/InsertBefore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {Constants} from "../constants";
import {highlightToolbar as highlightToolbarIR} from "../ir/highlightToolbar";
import {getEventName} from "../util/compatibility";
import {execAfterRender} from "../util/fixBrowserBehavior";
import {hasClosestBlock} from "../util/hasClosest";
import {getEditorRange, setRangeByWbr} from "../util/selection";
import {highlightToolbar} from "../wysiwyg/highlightToolbar";
import {insertEmptyBlock} from "../util/fixBrowserBehavior";
import {MenuItem} from "./MenuItem";

export class InsertBefore extends MenuItem {
Expand All @@ -16,18 +12,7 @@ export class InsertBefore extends MenuItem {
vditor.currentMode === "sv") {
return;
}
const range = getEditorRange(vditor[vditor.currentMode].element);
const blockElement = hasClosestBlock(range.startContainer);
if (blockElement) {
blockElement.insertAdjacentHTML("beforebegin", `<p data-block="0">${Constants.ZWSP}<wbr>\n</p>`);
setRangeByWbr(vditor[vditor.currentMode].element, range);
if (vditor.currentMode === "ir") {
highlightToolbarIR(vditor);
} else {
highlightToolbar(vditor);
}
execAfterRender(vditor);
}
insertEmptyBlock(vditor, "beforebegin");
});
}
}
32 changes: 30 additions & 2 deletions src/ts/util/fixBrowserBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
import {getLastNode} from "./hasClosest";
import {hasClosestByHeadings} from "./hasClosestByHEadings";
import {matchHotKey} from "./hotKey";
import {getSelectPosition, insertHTML, setRangeByWbr, setSelectionByPosition} from "./selection";
import {getEditorRange, getSelectPosition, insertHTML, setRangeByWbr, setSelectionByPosition} from "./selection";
import {highlightToolbar as highlightToolbarIR} from "../ir/highlightToolbar";

// https://github.com/Vanessa219/vditor/issues/361
export const fixCJKPosition = (range: Range, key: string) => {
Expand All @@ -33,6 +34,21 @@ export const fixCJKPosition = (range: Range, key: string) => {
}
};

export const insertEmptyBlock = (vditor: IVditor, position: InsertPosition) => {
const range = getEditorRange(vditor[vditor.currentMode].element);
const blockElement = hasClosestBlock(range.startContainer);
if (blockElement) {
blockElement.insertAdjacentHTML(position, `<p data-block="0">${Constants.ZWSP}<wbr>\n</p>`);
setRangeByWbr(vditor[vditor.currentMode].element, range);
if (vditor.currentMode === "ir") {
highlightToolbarIR(vditor);
} else {
highlightToolbar(vditor);
}
execAfterRender(vditor);
}
}

export const isFirstCell = (cellElement: HTMLElement) => {
const tableElement = hasClosestByMatchTag(cellElement, "TABLE") as HTMLTableElement;
if (tableElement && tableElement.rows[0].cells[0].isSameNode(cellElement)) {
Expand Down Expand Up @@ -591,9 +607,16 @@ export const fixTable = (vditor: IVditor, event: KeyboardEvent, range: Range) =>
return true;
}

const tableElement = cellElement.parentElement.parentElement.parentElement as HTMLTableElement;
if (event.key === "ArrowUp") {
event.preventDefault();
if (cellElement.tagName === "TH") {
if (tableElement.previousElementSibling) {
range.selectNodeContents(tableElement.previousElementSibling)
range.collapse(false);
} else {
insertEmptyBlock(vditor, "beforebegin");
}
return true;
}

Expand All @@ -618,6 +641,12 @@ export const fixTable = (vditor: IVditor, event: KeyboardEvent, range: Range) =>
event.preventDefault();
const trElement = cellElement.parentElement as HTMLTableRowElement;
if (!trElement.nextElementSibling && cellElement.tagName === "TD") {
if (tableElement.nextElementSibling) {
range.selectNodeContents(tableElement.nextElementSibling)
range.collapse(true);
} else {
insertEmptyBlock(vditor, "afterend");
}
return true;
}

Expand Down Expand Up @@ -691,7 +720,6 @@ export const fixTable = (vditor: IVditor, event: KeyboardEvent, range: Range) =>
}

// 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);
Expand Down

0 comments on commit d20bbcb

Please sign in to comment.