issue-469: resolving error with setCaret (#570)

* issue-469: resolving error with setCaret

* dump version

* update package.json

* expand input types
This commit is contained in:
Murod Khaydarov 2018-12-19 15:46:07 +03:00 committed by GitHub
parent 71a8a99937
commit 41ae41cda7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 16 deletions

12
dist/codex-editor.js vendored

File diff suppressed because one or more lines are too long

View file

@ -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",

View file

@ -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
@ -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
*

View file

@ -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;

View file

@ -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
*