| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
ve.ui.Sequence = function VeUiSequence( name, commandName, data, strip, config ) { |
| 31 |
this.name = name; |
| 32 |
this.commandName = commandName; |
| 33 |
this.data = data; |
| 34 |
this.strip = strip || 0; |
| 35 |
if ( typeof config === 'object' ) { |
| 36 |
|
| 37 |
this.setSelection = !!config.setSelection; |
| 38 |
this.delayed = !!config.delayed; |
| 39 |
this.checkOnPaste = !!config.checkOnPaste; |
| 40 |
this.checkOnDelete = !!config.checkOnDelete; |
| 41 |
} else { |
| 42 |
|
| 43 |
this.setSelection = !!arguments[ 4 ]; |
| 44 |
this.delayed = !!arguments[ 5 ]; |
| 45 |
this.checkOnPaste = !!arguments[ 6 ]; |
| 46 |
this.checkOnDelete = !!arguments[ 7 ]; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
OO.initClass( ve.ui.Sequence ); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
ve.ui.Sequence.prototype.match = function ( data, offset, plaintext ) { |
| 65 |
let i, j = offset - 1; |
| 66 |
|
| 67 |
if ( this.data instanceof RegExp ) { |
| 68 |
i = plaintext.search( this.data ); |
| 69 |
return ( i < 0 ) ? null : |
| 70 |
new ve.Range( offset - plaintext.length + i, offset ); |
| 71 |
} |
| 72 |
for ( i = this.data.length - 1; i >= 0; i--, j-- ) { |
| 73 |
if ( typeof this.data[ i ] === 'string' ) { |
| 74 |
if ( this.data[ i ] !== data.getCharacterData( j ) ) { |
| 75 |
return null; |
| 76 |
} |
| 77 |
} else if ( !ve.compare( this.data[ i ], data.getData( j ), true ) ) { |
| 78 |
return null; |
| 79 |
} |
| 80 |
} |
| 81 |
return new ve.Range( offset - this.data.length, offset ); |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ve.ui.Sequence.prototype.execute = function ( surface, range ) { |
| 92 |
const surfaceModel = surface.getModel(); |
| 93 |
|
| 94 |
if ( surface.getCommands().indexOf( this.getCommandName() ) === -1 ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
const command = surface.commandRegistry.lookup( this.getCommandName() ); |
| 99 |
|
| 100 |
if ( !command ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
let stripFragment; |
| 105 |
if ( this.strip ) { |
| 106 |
const stripRange = surfaceModel.getSelection().getRange(); |
| 107 |
stripFragment = surfaceModel.getLinearFragment( |
| 108 |
|
| 109 |
new ve.Range( stripRange.end, stripRange.end - this.strip ), true, true |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
surfaceModel.breakpoint(); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
const originalSelectionFragment = surfaceModel.getFragment(); |
| 118 |
if ( this.setSelection ) { |
| 119 |
surfaceModel.setLinearSelection( range ); |
| 120 |
} |
| 121 |
|
| 122 |
let args; |
| 123 |
|
| 124 |
|
| 125 |
if ( command.getAction() === 'window' && command.getMethod() === 'open' ) { |
| 126 |
args = ve.copy( command.args ); |
| 127 |
args[ 1 ] = args[ 1 ] || {}; |
| 128 |
args[ 1 ].strippedSequence = !!this.strip; |
| 129 |
} |
| 130 |
|
| 131 |
if ( stripFragment ) { |
| 132 |
|
| 133 |
|
| 134 |
stripFragment.removeContent(); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
const executed = command.execute( surface, args, 'sequence' ); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if ( !executed || this.delayed ) { |
| 144 |
originalSelectionFragment.select(); |
| 145 |
} |
| 146 |
|
| 147 |
if ( stripFragment && !executed ) { |
| 148 |
surfaceModel.undo(); |
| 149 |
|
| 150 |
surfaceModel.truncateUndoStack(); |
| 151 |
surfaceModel.emit( 'history' ); |
| 152 |
} |
| 153 |
|
| 154 |
return executed; |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
ve.ui.Sequence.prototype.getName = function () { |
| 163 |
return this.name; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
ve.ui.Sequence.prototype.getCommandName = function () { |
| 172 |
return this.commandName; |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
…) |
| 186 |
|
| 187 |
|
| 188 |
ve.ui.Sequence.prototype.getMessage = function ( explode ) { |
| 189 |
let data; |
| 190 |
if ( typeof this.data === 'string' ) { |
| 191 |
data = this.data.split( '' ); |
| 192 |
} else if ( this.data instanceof RegExp ) { |
| 193 |
data = [ this.data.toString() ]; |
| 194 |
} else { |
| 195 |
data = this.data.filter( ( key ) => !ve.isPlainObject( key ) ); |
| 196 |
} |
| 197 |
return explode ? data : data.join( '' ); |
| 198 |
}; |