Skip to content

Commit

Permalink
issue-469: resolving error with setCaret (codex-team#570)
Browse files Browse the repository at this point in the history
* issue-469: resolving error with setCaret

* dump version

* update package.json

* expand input types
  • Loading branch information
khaydarov authored Dec 19, 2018
1 parent 71a8a99 commit 41ae41c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
12 changes: 6 additions & 6 deletions dist/codex-editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codex.editor",
"version": "2.7.13",
"version": "2.7.14",
"description": "CodeX Editor. Native JS, based on API and Open Source",
"main": "dist/codex-editor.js",
"types": "./types/index.d.ts",
Expand Down
36 changes: 36 additions & 0 deletions src/components/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ export default class Dom {
return node && typeof node === 'object' && node.nodeType && node.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
}

/**
* Check if passed element is contenteditable
* @param {HTMLElement} element
* @return {boolean}
*/
public static isContentEditable(element: HTMLElement): boolean {
return element.contentEditable === 'true';
}

/**
* Checks target if it is native input
* @param {Element|String|Node} target - HTML element or string
Expand All @@ -255,6 +264,33 @@ export default class Dom {
return target && target.tagName ? nativeInputs.includes(target.tagName) : false;
}

/**
* Checks if we can set caret
* @param {HTMLElement} target
* @return {boolean}
*/
public static canSetCaret(target: HTMLElement): boolean {
let result = true;
if (Dom.isNativeInput(target)) {
const inputElement = target as HTMLInputElement;
switch (inputElement.type) {
case 'file':
case 'checkbox':
case 'radio':
case 'hidden':
case 'submit':
case 'button':
case 'image':
case 'reset':
result = false;
break;
}
} else {
result = Dom.isContentEditable(target);
}
return result;
}

/**
* Checks node if it is empty
*
Expand Down
4 changes: 4 additions & 0 deletions src/components/modules/caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ export default class Caret extends Module {

/** if found deepest node is native input */
if ($.isNativeInput(element)) {
if (!$.canSetCaret(element)) {
return;
}

element.focus();
(element as HTMLInputElement).selectionStart = (element as HTMLInputElement).selectionEnd = offset;
return;
Expand Down
9 changes: 0 additions & 9 deletions src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,6 @@ export default class Util {
return Promise.resolve(object) === object;
}

/**
* Check if passed element is contenteditable
* @param {HTMLElement} element
* @return {boolean}
*/
public static isContentEditable(element: HTMLElement): boolean {
return element.contentEditable === 'true';
}

/**
* Delays method execution
*
Expand Down

0 comments on commit 41ae41c

Please sign in to comment.