Skip to content

Commit

Permalink
Fixed complexity warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed May 11, 2024
1 parent f16ade8 commit 2a2b2cf
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 156 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
node: true
},
rules: {
complexity: ['warn', 20],
complexity: ['warn', 15],
'no-octal-escape': 0,
'@typescript-eslint/no-unsafe-declaration-merging': 'off',
'mocha/no-skipped-tests': 'error',
Expand Down
76 changes: 76 additions & 0 deletions src/core/selection/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,81 @@
* @module selection
*/

import {
INVISIBLE_SPACE_REG_EXP_END as INV_END,
INVISIBLE_SPACE_REG_EXP_START as INV_START
} from 'jodit/core/constants';
import { Dom } from 'jodit/core/dom';

export * from './move-node-inside-start';
export * from './move-the-node-along-the-edge-outward';

/**
* Check if the cursor is at the edge of the string
* @private
*/
export function cursorInTheEdgeOfString(
container: Node,
offset: number,
start: boolean,
end: boolean
): boolean {
const text = container.nodeValue?.length ? container.nodeValue : '';

if (end && text.replace(INV_END(), '').length > offset) {
return true;
}

const inv = INV_START().exec(text);

return start && ((inv && inv[0].length < offset) || (!inv && offset > 0));
}

export function findCorrectCurrentNode(
node: Node,
range: Range,
rightMode: boolean,
isCollapsed: boolean,
checkChild: boolean,
child: (nd: Node) => Node | null
): { node: Node; rightMode: boolean } {
node = range.startContainer.childNodes[range.startOffset];

if (!node) {
node = range.startContainer.childNodes[range.startOffset - 1];
rightMode = true;
}

if (node && isCollapsed && !Dom.isText(node)) {
// test Current method - Cursor in the left of some SPAN
if (!rightMode && Dom.isText(node.previousSibling)) {
node = node.previousSibling;
} else if (checkChild) {
let current: Node | null = child(node);

while (current) {
if (current && Dom.isText(current)) {
node = current;
break;
}
current = child(current);
}
}
}

if (node && !isCollapsed && !Dom.isText(node)) {
let leftChild: Node | null = node,
rightChild: Node | null = node;

do {
leftChild = leftChild.firstChild;
rightChild = rightChild.lastChild;
} while (leftChild && rightChild && !Dom.isText(leftChild));

if (leftChild === rightChild && leftChild && Dom.isText(leftChild)) {
node = leftChild;
}
}

return { node, rightMode };
}
Loading

0 comments on commit 2a2b2cf

Please sign in to comment.