| 1 |
import HTMLMaskElement from './html-mask-element.js'; |
| 2 |
import IMask from '../core/holder.js'; |
| 3 |
import './mask-element.js'; |
| 4 |
|
| 5 |
class HTMLContenteditableMaskElement extends HTMLMaskElement { |
| 6 |
/** Returns HTMLElement selection start */ |
| 7 |
get _unsafeSelectionStart() { |
| 8 |
const root = this.rootElement; |
| 9 |
const selection = root.getSelection && root.getSelection(); |
| 10 |
const anchorOffset = selection && selection.anchorOffset; |
| 11 |
const focusOffset = selection && selection.focusOffset; |
| 12 |
if (focusOffset == null || anchorOffset == null || anchorOffset < focusOffset) { |
| 13 |
return anchorOffset; |
| 14 |
} |
| 15 |
return focusOffset; |
| 16 |
} |
| 17 |
|
| 18 |
/** Returns HTMLElement selection end */ |
| 19 |
get _unsafeSelectionEnd() { |
| 20 |
const root = this.rootElement; |
| 21 |
const selection = root.getSelection && root.getSelection(); |
| 22 |
const anchorOffset = selection && selection.anchorOffset; |
| 23 |
const focusOffset = selection && selection.focusOffset; |
| 24 |
if (focusOffset == null || anchorOffset == null || anchorOffset > focusOffset) { |
| 25 |
return anchorOffset; |
| 26 |
} |
| 27 |
return focusOffset; |
| 28 |
} |
| 29 |
|
| 30 |
/** Sets HTMLElement selection */ |
| 31 |
_unsafeSelect(start, end) { |
| 32 |
if (!this.rootElement.createRange) return; |
| 33 |
const range = this.rootElement.createRange(); |
| 34 |
range.setStart(this.input.firstChild || this.input, start); |
| 35 |
range.setEnd(this.input.lastChild || this.input, end); |
| 36 |
const root = this.rootElement; |
| 37 |
const selection = root.getSelection && root.getSelection(); |
| 38 |
if (selection) { |
| 39 |
selection.removeAllRanges(); |
| 40 |
selection.addRange(range); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** HTMLElement value */ |
| 45 |
get value() { |
| 46 |
return this.input.textContent || ''; |
| 47 |
} |
| 48 |
set value(value) { |
| 49 |
this.input.textContent = value; |
| 50 |
} |
| 51 |
} |
| 52 |
IMask.HTMLContenteditableMaskElement = HTMLContenteditableMaskElement; |
| 53 |
|
| 54 |
export { HTMLContenteditableMaskElement as default }; |
| 55 |
|