Skip to content

Commit

Permalink
Fixed selection module
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Jan 27, 2020
1 parent 1986ae0 commit 075c2a8
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 185 deletions.
2 changes: 1 addition & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ Config.prototype.controls = {

if (
current &&
current.nodeType !== Node.TEXT_NODE &&
!Dom.isText(current) &&
(current.tagName === 'IMG' || $$('img', current).length)
) {
sourceImage =
Expand Down
77 changes: 55 additions & 22 deletions src/modules/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,7 @@ export class Select {
if (selection.collapsed || !end) {
const previousNode: Node | null = start.previousSibling;

if (
previousNode &&
previousNode.nodeType === Node.TEXT_NODE
) {
if (Dom.isText(previousNode)) {
range.setStart(
previousNode,
previousNode.nodeValue
Expand Down Expand Up @@ -373,14 +370,14 @@ export class Select {
this.win.focus();
this.area.focus();

const sel = this.sel,
range = this.createRange();
const sel: WindowSelection = this.sel,
range = sel?.rangeCount ? sel?.getRangeAt(0) : null;

if (sel && (!sel.rangeCount || !this.current())) {
if (!range || !Dom.isOrContains(this.area, range.startContainer)) {
const range = this.createRange();
range.setStart(this.area, 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
this.selectRange(range);
}

if (!this.jodit.editorIsActive) {
Expand Down Expand Up @@ -516,7 +513,9 @@ export class Select {
) {
this.errorNode(node);

this.focus();
if (!this.isFocused() && this.jodit.isEditorMode()) {
this.focus();
}

const sel = this.sel;

Expand All @@ -528,8 +527,20 @@ export class Select {
const range = sel.getRangeAt(0);

if (Dom.isOrContains(this.area, range.commonAncestorContainer)) {
range.deleteContents();
range.insertNode(node);
if (
/^(BR|HR|IMG|VIDEO)$/i.test(
range.startContainer.nodeName
) &&
range.collapsed
) {
range.startContainer.parentNode?.insertBefore(
node,
range.startContainer
);
} else {
range.deleteContents();
range.insertNode(node);
}
} else {
this.area.appendChild(node);
}
Expand Down Expand Up @@ -612,8 +623,7 @@ export class Select {
lastEditorElement = this.area.lastChild;

while (
lastEditorElement &&
lastEditorElement.nodeType === Node.TEXT_NODE &&
Dom.isText(lastEditorElement) &&
lastEditorElement.previousSibling &&
lastEditorElement.nodeValue &&
/^\s*$/.test(lastEditorElement.nodeValue)
Expand All @@ -625,7 +635,7 @@ export class Select {
if (
lastEditorElement &&
lastChild === lastEditorElement &&
lastChild.nodeType === Node.ELEMENT_NODE
Dom.isElement(lastChild)
) {
this.area.appendChild(this.jodit.create.inside.element('br'));
}
Expand Down Expand Up @@ -809,7 +819,7 @@ export class Select {
const range = this.createRange();
let fakeNode: Text | false = false;

if (node.nodeType !== Node.TEXT_NODE) {
if (!Dom.isText(node)) {
fakeNode = this.jodit.create.inside.text(consts.INVISIBLE_SPACE);
range.setStartAfter(node);
range.insertNode(fakeNode);
Expand Down Expand Up @@ -932,8 +942,8 @@ export class Select {
const range = this.createRange();
let fakeNode: Text | false = false;

if (node.nodeType !== Node.TEXT_NODE) {
fakeNode = this.doc.createTextNode(consts.INVISIBLE_SPACE);
if (!Dom.isText(node)) {
fakeNode = this.jodit.create.inside.text(consts.INVISIBLE_SPACE);
range.setStartBefore(node);
range.collapse(true);
range.insertNode(fakeNode);
Expand Down Expand Up @@ -977,15 +987,15 @@ export class Select {
last: Node = node;

do {
if (start.nodeType === Node.TEXT_NODE) {
if (Dom.isText(start)) {
break;
}
last = start;
start = inStart ? start.firstChild : start.lastChild;
} while (start);

if (!start) {
const fakeNode: Text = this.doc.createTextNode(
const fakeNode = this.jodit.create.inside.text(
consts.INVISIBLE_SPACE
);
if (!/^(img|br|input)$/i.test(last.nodeName)) {
Expand Down Expand Up @@ -1406,19 +1416,42 @@ export class Select {
const br = this.jodit.create.inside.element('br');
range.insertNode(br);

const clearBR = (
start: Node,
getNext: (node: Node) => Node | null
) => {
let next = getNext(start);

while (next) {
const nextSib = getNext(next);

if (
next &&
(next.nodeName === 'BR' || Dom.isEmptyTextNode(next))
) {
Dom.safeRemove(next);
} else {
break;
}

next = nextSib;
}
};

clearBR(br, (n: Node) => n.nextSibling);
clearBR(br, (n: Node) => n.previousSibling);

if (cursorOnTheRight) {
leftRange.setEndBefore(br);
range.setEndBefore(br);
} else {
leftRange.setEndAfter(br);
range.setEndAfter(br);
}

} else {
leftRange.setEnd(range.startContainer, range.startOffset);
}


const fragment = leftRange.extractContents();

if (currentBox.parentNode) {
Expand Down
27 changes: 11 additions & 16 deletions src/modules/Snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,8 @@ export class Snapshot extends Component<IJodit> {
for (j = 0; j < elms.length; j += 1) {
if (
last &&
(!(
elms[j].nodeType === Node.TEXT_NODE &&
elms[j].textContent === ''
) &&
!(
last.nodeType === Node.TEXT_NODE &&
elms[j].nodeType === Node.TEXT_NODE
))
!(Dom.isText(elms[j]) && elms[j].textContent === '') &&
!(Dom.isText(last) && Dom.isText(elms[j]))
) {
count += 1;
}
Expand All @@ -74,11 +68,11 @@ export class Snapshot extends Component<IJodit> {
* @return {number}
*/
private static strokeOffset(elm: Node | null, offset: number): number {
while (elm && elm.nodeType === Node.TEXT_NODE) {
while (Dom.isText(elm)) {
elm = elm.previousSibling;

if (
elm &&
elm.nodeType === Node.TEXT_NODE &&
Dom.isText(elm) &&
elm.textContent !== null
) {
offset += elm.textContent.length;
Expand Down Expand Up @@ -153,13 +147,11 @@ export class Snapshot extends Component<IJodit> {
const sel = this.jodit.selection.sel;

if (sel && sel.rangeCount) {
const
range = sel.getRangeAt(0),
const range = sel.getRangeAt(0),
startContainer = this.calcHierarchyLadder(range.startContainer),
endContainer = this.calcHierarchyLadder(range.endContainer);

let
startOffset = Snapshot.strokeOffset(
let startOffset = Snapshot.strokeOffset(
range.startContainer,
range.startOffset
),
Expand Down Expand Up @@ -219,7 +211,10 @@ export class Snapshot extends Component<IJodit> {
this.jodit.selection.selectRange(range);
}
} catch (__ignore) {
this.jodit.editor.lastChild && this.jodit.selection.setCursorAfter(this.jodit.editor.lastChild);
this.jodit.editor.lastChild &&
this.jodit.selection.setCursorAfter(
this.jodit.editor.lastChild
);

if (process.env.NODE_ENV !== 'production') {
console.warn('Broken snapshot', __ignore);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/Widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export namespace Widget {

if (
elm &&
elm.nodeType !== Node.TEXT_NODE &&
!Dom.isText(elm) &&
(elm.tagName === 'IMG' || $$('img', elm).length)
) {
currentImage = elm.tagName === 'IMG' ? elm : $$('img', elm)[0];
Expand All @@ -482,7 +482,7 @@ export namespace Widget {

if (
elm &&
elm.nodeType !== Node.TEXT_NODE &&
!Dom.isText(elm) &&
elm.nodeName === 'A'
) {
val(form, 'input[name=url]', elm.getAttribute('href') || '');
Expand Down
2 changes: 1 addition & 1 deletion src/modules/toolbar/joditToolbarCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class JoditToolbarCollection extends ToolbarCollection<IJodit> {
Dom.up(
elm,
(node: Node | null): boolean | void => {
if (node && node.nodeType !== Node.TEXT_NODE) {
if (node && !Dom.isText(node)) {
return this.checkActiveStatus(
css,
node as HTMLElement
Expand Down
9 changes: 4 additions & 5 deletions src/plugins/backspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function backspace(editor: IJodit) {
: nextElement.firstChild;
}

if (nextElement && nextElement.nodeType === Node.TEXT_NODE) {
if (Dom.isText(nextElement)) {
box.node = nextElement;
return removeChar(box, toLeft, range);
}
Expand Down Expand Up @@ -176,8 +176,7 @@ export function backspace(editor: IJodit) {
}

while (
node &&
node.nodeType === Node.TEXT_NODE &&
Dom.isText(node) &&
node.nodeValue &&
node.nodeValue.match(/^[\n\r]+$/)
) {
Expand All @@ -200,7 +199,7 @@ export function backspace(editor: IJodit) {
return true;
}

if (node.nodeType === Node.TEXT_NODE && !Dom.isEmptyTextNode(node)) {
if (Dom.isText(node) && !Dom.isEmptyTextNode(node)) {
return false;
}

Expand Down Expand Up @@ -357,7 +356,7 @@ export function backspace(editor: IJodit) {
editor.selection.insertNode(marker, false, false);

if (
tmpNode.nodeType === Node.TEXT_NODE &&
Dom.isText(tmpNode) &&
tmpNode.nodeValue === consts.INVISIBLE_SPACE
) {
Dom.safeRemove(tmpNode);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/clipboard/copyformat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Config.prototype.controls.copyformat = {
(Dom.up(
current,
(elm: Node | null) =>
elm && elm.nodeType !== Node.TEXT_NODE,
elm && !Dom.isText(elm),
editor.editor
) as HTMLElement) || editor.editor;

Expand Down
25 changes: 18 additions & 7 deletions src/plugins/enter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class enter extends Plugin {
let currentBox = this.getBlockWrapper(current);

if (!currentBox) {
this.wrapText(current)
this.wrapText(current);
}
};

Expand All @@ -132,7 +132,7 @@ export class enter extends Plugin {

if (!current || current === editor.editor) {
current = editor.create.inside.text(INVISIBLE_SPACE);
sel.insertNode(current)
sel.insertNode(current);
sel.select(current);
}

Expand All @@ -150,7 +150,7 @@ export class enter extends Plugin {
currentBox = this.wrapText(current);
}

if (!currentBox) {
if (!currentBox || currentBox === current) {
insertParagraph(editor, false, isLi ? 'li' : defaultTag);
return false;
}
Expand All @@ -164,11 +164,16 @@ export class enter extends Plugin {
return false;
}

const isDefault = currentBox.tagName.toLowerCase() === this.defaultTag;
const canSplit =
currentBox.tagName.toLowerCase() === this.defaultTag || isLi;

const cursorOnTheRight = sel.cursorOnTheRight(currentBox);
const cursorOnTheLeft = sel.cursorOnTheLeft(currentBox);

if ((!isDefault || Dom.isEmpty(currentBox)) && (cursorOnTheRight || cursorOnTheLeft)) {
if (
(!canSplit || Dom.isEmpty(currentBox)) &&
(cursorOnTheRight || cursorOnTheLeft)
) {
let fake: Text | false = false;

if (cursorOnTheRight) {
Expand All @@ -189,7 +194,10 @@ export class enter extends Plugin {
sel.splitSelection(currentBox);
}

private getBlockWrapper(current: Node | null, tagReg = consts.IS_BLOCK): HTMLElement | false {
private getBlockWrapper(
current: Node | null,
tagReg = consts.IS_BLOCK
): HTMLElement | false {
let node = current;
const root = this.jodit.editor;

Expand All @@ -203,7 +211,10 @@ export class enter extends Plugin {
return node as HTMLLIElement;
}

return this.getBlockWrapper(node.parentNode, /^li$/i) || node as HTMLElement;
return (
this.getBlockWrapper(node.parentNode, /^li$/i) ||
(node as HTMLElement)
);
}

node = node.parentNode;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class placeholder extends Plugin {

const first = root.firstChild;

if (MAY_BE_REMOVED_WITH_KEY.test(first.nodeName)) {
if (MAY_BE_REMOVED_WITH_KEY.test(first.nodeName) || /^(TABLE)$/i.test(first.nodeName)) {
return false;
}

Expand Down
Loading

0 comments on commit 075c2a8

Please sign in to comment.