| 1 |
import { DIRECTION } from './utils.js'; |
| 2 |
|
| 3 |
/** Provides details of changing input */ |
| 4 |
class ActionDetails { |
| 5 |
/** Current input value */ |
| 6 |
|
| 7 |
/** Current cursor position */ |
| 8 |
|
| 9 |
/** Old input value */ |
| 10 |
|
| 11 |
/** Old selection */ |
| 12 |
|
| 13 |
constructor(opts) { |
| 14 |
Object.assign(this, opts); |
| 15 |
|
| 16 |
// double check if left part was changed (autofilling, other non-standard input triggers) |
| 17 |
while (this.value.slice(0, this.startChangePos) !== this.oldValue.slice(0, this.startChangePos)) { |
| 18 |
--this.oldSelection.start; |
| 19 |
} |
| 20 |
if (this.insertedCount) { |
| 21 |
// double check right part |
| 22 |
while (this.value.slice(this.cursorPos) !== this.oldValue.slice(this.oldSelection.end)) { |
| 23 |
if (this.value.length - this.cursorPos < this.oldValue.length - this.oldSelection.end) ++this.oldSelection.end;else ++this.cursorPos; |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
/** Start changing position */ |
| 29 |
get startChangePos() { |
| 30 |
return Math.min(this.cursorPos, this.oldSelection.start); |
| 31 |
} |
| 32 |
|
| 33 |
/** Inserted symbols count */ |
| 34 |
get insertedCount() { |
| 35 |
return this.cursorPos - this.startChangePos; |
| 36 |
} |
| 37 |
|
| 38 |
/** Inserted symbols */ |
| 39 |
get inserted() { |
| 40 |
return this.value.substr(this.startChangePos, this.insertedCount); |
| 41 |
} |
| 42 |
|
| 43 |
/** Removed symbols count */ |
| 44 |
get removedCount() { |
| 45 |
// Math.max for opposite operation |
| 46 |
return Math.max(this.oldSelection.end - this.startChangePos || |
| 47 |
// for Delete |
| 48 |
this.oldValue.length - this.value.length, 0); |
| 49 |
} |
| 50 |
|
| 51 |
/** Removed symbols */ |
| 52 |
get removed() { |
| 53 |
return this.oldValue.substr(this.startChangePos, this.removedCount); |
| 54 |
} |
| 55 |
|
| 56 |
/** Unchanged head symbols */ |
| 57 |
get head() { |
| 58 |
return this.value.substring(0, this.startChangePos); |
| 59 |
} |
| 60 |
|
| 61 |
/** Unchanged tail symbols */ |
| 62 |
get tail() { |
| 63 |
return this.value.substring(this.startChangePos + this.insertedCount); |
| 64 |
} |
| 65 |
|
| 66 |
/** Remove direction */ |
| 67 |
get removeDirection() { |
| 68 |
if (!this.removedCount || this.insertedCount) return DIRECTION.NONE; |
| 69 |
|
| 70 |
// align right if delete at right |
| 71 |
return (this.oldSelection.end === this.cursorPos || this.oldSelection.start === this.cursorPos) && |
| 72 |
// if not range removed (event with backspace) |
| 73 |
this.oldSelection.end === this.oldSelection.start ? DIRECTION.RIGHT : DIRECTION.LEFT; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
export { ActionDetails as default }; |
| 78 |
|