Skip to content

Commit

Permalink
Fix last bugs in backspace plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Feb 7, 2018
1 parent a8dd266 commit 891b17c
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 295 deletions.
1 change: 1 addition & 0 deletions src/modules/Dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ export class Dialog extends Component{
* @param {string} msg Message
* @param {string|function} [title] Title or callback
* @param {function} [callback] callback
* @param {string} [className]
* @example
* ```javascript
* Jodit.Alert("File was uploaded");
Expand Down
38 changes: 34 additions & 4 deletions src/modules/Dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ export class Dom {

return false;
}

static findInline = (node: Node | null, toLeft: boolean, root: Node): Node | null => {
let prevElement: Node | null = node,
nextElement: Node | null = null;

do {
if (prevElement) {
nextElement = toLeft ? prevElement.previousSibling : prevElement.nextSibling;
if (!nextElement && prevElement.parentNode && prevElement.parentNode !== root && Dom.isInlineBlock(prevElement.parentNode)) {
prevElement = prevElement.parentNode;
} else {
break;
}
} else {
break;
}
} while(!nextElement);

while (nextElement && Dom.isInlineBlock(nextElement) && (!toLeft ? nextElement.firstChild : nextElement.lastChild)) {
nextElement = !toLeft ? nextElement.firstChild : nextElement.lastChild;
}

return nextElement;
};

/**
* Find next/prev node what `condition(next) === true`
*
Expand Down Expand Up @@ -345,15 +370,15 @@ export class Dom {
}

if (node.nodeType === Node.TEXT_NODE) {
return !node.nodeValue || trim(node.nodeValue).length === 0;
return node.nodeValue === null || trim(node.nodeValue).length === 0;
}

return Dom.each(<HTMLElement>node, (elm: Node | null): false | void => {
if (
(
elm && elm.nodeType === Node.TEXT_NODE &&
(
elm.nodeValue &&
elm.nodeValue !== null &&
trim(elm.nodeValue).length !== 0
)
) ||
Expand Down Expand Up @@ -455,9 +480,13 @@ export class Dom {
*/
static moveContent(from: Node, to: Node, inStart: boolean = false) {
const fragment: DocumentFragment = from.ownerDocument.createDocumentFragment();

[].slice.call(from.childNodes).forEach((node: Node) => {
fragment.appendChild(node);
if (node.nodeType !== Node.TEXT_NODE || node.nodeValue !== consts.INVISIBLE_SPACE) {
fragment.appendChild(node);
}
});

if (!inStart || !to.firstChild) {
to.appendChild(fragment);
} else {
Expand Down Expand Up @@ -495,6 +524,7 @@ export class Dom {
}
child = child.parentNode;
}

return false;
};

Expand All @@ -506,7 +536,7 @@ export class Dom {
* @return {boolean}
*/
static isOrContains = (root: Node, child: Node): boolean => {
return root === child || Dom.contains(root, child);
return child && root && (root === child || Dom.contains(root, child));
};

/*static apply = (options, addPropertyCallback, editor: Jodit) => {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,11 @@ export const normalizeNode = (node: Node | null) => {
}

if (node.nodeType === Node.TEXT_NODE && node.nodeValue !== null && node.parentNode) {
while (node.nextSibling && node.nextSibling.nodeType == 3) {
while (node.nextSibling && node.nextSibling.nodeType === Node.TEXT_NODE) {
if (node.nextSibling.nodeValue !== null) {
node.nodeValue += node.nextSibling.nodeValue;
}

node.nodeValue = node.nodeValue.replace(consts.INVISIBLE_SPACE_REG_EXP, '');
node.parentNode.removeChild(node.nextSibling);
}
} else {
Expand Down
23 changes: 10 additions & 13 deletions src/modules/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class Select extends Component{
*
* @return false|Node The element under the cursor or false if undefined or not in editor
*/
current(): false|Node {
current(): false | Node {
if (this.jodit.getRealMode() === consts.MODE_WYSIWYG && this.jodit.editorWindow.getSelection !== undefined) {
const sel: Selection = this.jodit.editorWindow.getSelection();
if (sel.rangeCount > 0) {
Expand Down Expand Up @@ -532,8 +532,7 @@ export class Select extends Component{
throw new Error('Node element must be in editor');
}

const sel: Selection = this.jodit.editorWindow.getSelection(),
range: Range = this.jodit.editorDocument.createRange(),
const range: Range = this.jodit.editorDocument.createRange(),
fakeNode: Text = this.jodit.editorDocument.createTextNode(consts.INVISIBLE_SPACE);


Expand All @@ -543,7 +542,7 @@ export class Select extends Component{
range.insertNode(fakeNode);
range.selectNode(fakeNode);
} else {
range.setStart(node, node.nodeValue ? node.nodeValue.length : 0);
range.setStart(node, node.nodeValue !== null ? node.nodeValue.length : 0);
}

range.collapse(false);
Expand All @@ -564,6 +563,7 @@ export class Select extends Component{
cursorInTheEdge (start: boolean = false, parentBlock: HTMLElement|Function|false = false, inverse: boolean = false): boolean|null {
const sel: Selection = this.jodit.editorWindow.getSelection(),
isNoEmptyNode = (elm: Node | null) => (elm && !Dom.isEmptyTextNode(elm));

let
container: HTMLElement = <HTMLElement>parentBlock;

Expand Down Expand Up @@ -729,16 +729,15 @@ export class Select extends Component{
throw new Error('Node element must be in editor');
}

const sel: Selection = this.jodit.editorWindow.getSelection(),
range: Range = this.jodit.editorDocument.createRange(),
const range: Range = this.jodit.editorDocument.createRange(),
fakeNode: Text = this.jodit.editorDocument.createTextNode(consts.INVISIBLE_SPACE);

if (node.nodeType !== Node.TEXT_NODE) {
range.setStartBefore(node);
range.insertNode(fakeNode);
range.selectNode(fakeNode);
} else {
range.setStart(node, node.nodeValue ? node.nodeValue.length : 0);
range.setStart(node, node.nodeValue !== null ? node.nodeValue.length : 0);
}

range.collapse(true);
Expand All @@ -761,18 +760,17 @@ export class Select extends Component{
throw new Error('Node element must be in editor');
}

const sel: Selection = this.jodit.editorWindow.getSelection(),
range: Range = this.jodit.editorDocument.createRange();
const range: Range = this.jodit.editorDocument.createRange();

let start: Node|null = node,
let start: Node | null = node,
last: Node = node;

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

if (last === node && node.nodeType !== Node.TEXT_NODE) {
Expand Down Expand Up @@ -809,8 +807,7 @@ export class Select extends Component{
throw new Error('Node element must be in editor');
}

const sel: Selection = this.jodit.editorWindow.getSelection(),
range: Range = this.jodit.editorDocument.createRange();
const range: Range = this.jodit.editorDocument.createRange();

range[inward ? 'selectNodeContents' : 'selectNode'](node);

Expand Down
Loading

0 comments on commit 891b17c

Please sign in to comment.