| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
ve.SelectionState = function VeSelectionState( selection ) { |
| 25 |
this.anchorNode = selection.anchorNode; |
| 26 |
this.anchorOffset = selection.anchorOffset; |
| 27 |
this.focusNode = selection.focusNode; |
| 28 |
this.focusOffset = selection.focusOffset; |
| 29 |
|
| 30 |
this.isCollapsed = selection.isCollapsed; |
| 31 |
if ( this.isCollapsed === undefined ) { |
| 32 |
|
| 33 |
this.isCollapsed = this.anchorNode === this.focusNode && |
| 34 |
this.anchorOffset === this.focusOffset; |
| 35 |
} |
| 36 |
this.isBackwards = selection.isBackwards; |
| 37 |
if ( this.isBackwards === undefined ) { |
| 38 |
|
| 39 |
this.isBackwards = ve.compareDocumentOrder( |
| 40 |
this.focusNode, |
| 41 |
this.focusOffset, |
| 42 |
this.anchorNode, |
| 43 |
this.anchorOffset |
| 44 |
) < 0; |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
OO.initClass( ve.SelectionState ); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
ve.SelectionState.static.newNullSelection = function () { |
| 60 |
return new ve.SelectionState( { |
| 61 |
focusNode: null, |
| 62 |
focusOffset: 0, |
| 63 |
anchorNode: null, |
| 64 |
anchorOffset: 0 |
| 65 |
} ); |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
ve.SelectionState.prototype.flip = function () { |
| 76 |
if ( this.isCollapsed ) { |
| 77 |
return this; |
| 78 |
} |
| 79 |
return new ve.SelectionState( { |
| 80 |
anchorNode: this.focusNode, |
| 81 |
anchorOffset: this.focusOffset, |
| 82 |
focusNode: this.anchorNode, |
| 83 |
focusOffset: this.anchorOffset, |
| 84 |
isCollapsed: false, |
| 85 |
isBackwards: !this.isBackwards |
| 86 |
} ); |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
ve.SelectionState.prototype.equalsSelection = function ( other ) { |
| 96 |
return this.anchorNode === other.anchorNode && |
| 97 |
this.anchorOffset === other.anchorOffset && |
| 98 |
this.focusNode === other.focusNode && |
| 99 |
this.focusOffset === other.focusOffset; |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
ve.SelectionState.prototype.getNativeRange = function ( doc ) { |
| 111 |
if ( this.anchorNode === null ) { |
| 112 |
return null; |
| 113 |
} |
| 114 |
const range = doc.createRange(); |
| 115 |
try { |
| 116 |
if ( this.isBackwards ) { |
| 117 |
range.setStart( this.focusNode, this.focusOffset ); |
| 118 |
range.setEnd( this.anchorNode, this.anchorOffset ); |
| 119 |
} else { |
| 120 |
range.setStart( this.anchorNode, this.anchorOffset ); |
| 121 |
if ( !this.isCollapsed ) { |
| 122 |
range.setEnd( this.focusNode, this.focusOffset ); |
| 123 |
} |
| 124 |
} |
| 125 |
} catch ( e ) { |
| 126 |
|
| 127 |
return null; |
| 128 |
} |
| 129 |
return range; |
| 130 |
}; |