| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
g |
| 6 |
|
| 7 |
|
| 8 |
Lv2) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
( function () { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
function supportsInsertText() { |
| 30 |
return $( this ).data( 'jquery.textSelection' ) === undefined && |
| 31 |
typeof document.execCommand === 'function' && |
| 32 |
typeof document.queryCommandSupported === 'function' && |
| 33 |
document.queryCommandSupported( 'insertText' ); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
function execInsertText( field, content, fallback ) { |
| 45 |
let inserted = false; |
| 46 |
|
| 47 |
if ( |
| 48 |
supportsInsertText() && |
| 49 |
!( |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
$.client.profile().layout === 'webkit' && |
| 54 |
content.split( '\n' ).length > 100 |
| 55 |
) |
| 56 |
) { |
| 57 |
field.focus(); |
| 58 |
try { |
| 59 |
if ( |
| 60 |
|
| 61 |
|
| 62 |
document.activeElement === field && |
| 63 |
|
| 64 |
document.execCommand( 'insertText', false, content ) |
| 65 |
) { |
| 66 |
inserted = true; |
| 67 |
} |
| 68 |
} catch ( e ) {} |
| 69 |
} |
| 70 |
|
| 71 |
if ( !inserted ) { |
| 72 |
fallback.call( field, content ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
const fn = { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
getContents: function () { |
| 84 |
return this.val(); |
| 85 |
}, |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
setContents: function ( content ) { |
| 96 |
return this.each( function () { |
| 97 |
const scrollTop = this.scrollTop; |
| 98 |
this.select(); |
| 99 |
execInsertText( this, content, function () { |
| 100 |
$( this ).val( content ); |
| 101 |
} ); |
| 102 |
|
| 103 |
this.scrollTop = scrollTop; |
| 104 |
} ); |
| 105 |
}, |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
getSelection: function () { |
| 114 |
const el = this.get( 0 ); |
| 115 |
|
| 116 |
let val; |
| 117 |
if ( !el ) { |
| 118 |
val = ''; |
| 119 |
} else { |
| 120 |
val = el.value.slice( el.selectionStart, el.selectionEnd ); |
| 121 |
} |
| 122 |
|
| 123 |
return val; |
| 124 |
}, |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
replaceSelection: function ( value ) { |
| 135 |
return this.each( function () { |
| 136 |
execInsertText( this, value, function () { |
| 137 |
const allText = $( this ).textSelection( 'getContents' ); |
| 138 |
const currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } ); |
| 139 |
const startPos = currSelection[ 0 ]; |
| 140 |
const endPos = currSelection[ 1 ]; |
| 141 |
|
| 142 |
$( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + value + |
| 143 |
allText.slice( endPos ) ); |
| 144 |
$( this ).textSelection( 'setSelection', { |
| 145 |
start: startPos, |
| 146 |
end: startPos + value.length |
| 147 |
} ); |
| 148 |
} ); |
| 149 |
} ); |
| 150 |
}, |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
encapsulateSelection: function ( options ) { |
| 176 |
return this.each( function () { |
| 177 |
let selText, isSample, |
| 178 |
pre = options.pre, |
| 179 |
post = options.post; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
function checkSelectedText() { |
| 187 |
if ( !selText ) { |
| 188 |
selText = options.peri; |
| 189 |
isSample = true; |
| 190 |
} else if ( options.replace ) { |
| 191 |
selText = options.peri; |
| 192 |
} else { |
| 193 |
while ( selText.charAt( selText.length - 1 ) === ' ' ) { |
| 194 |
|
| 195 |
selText = selText.slice( 0, -1 ); |
| 196 |
post += ' '; |
| 197 |
} |
| 198 |
while ( selText.charAt( 0 ) === ' ' ) { |
| 199 |
|
| 200 |
selText = selText.slice( 1 ); |
| 201 |
pre = ' ' + pre; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
function doSplitLines( text, preText, postText ) { |
| 218 |
const selTextArr = text.split( '\n' ); |
| 219 |
let insText = ''; |
| 220 |
for ( let i = 0; i < selTextArr.length; i++ ) { |
| 221 |
insText += preText + selTextArr[ i ] + postText; |
| 222 |
if ( i !== selTextArr.length - 1 ) { |
| 223 |
insText += '\n'; |
| 224 |
} |
| 225 |
} |
| 226 |
return insText; |
| 227 |
} |
| 228 |
|
| 229 |
isSample = false; |
| 230 |
$( this ).trigger( 'focus' ); |
| 231 |
if ( options.selectionStart !== undefined ) { |
| 232 |
$( this ).textSelection( 'setSelection', { start: options.selectionStart, end: options.selectionEnd } ); |
| 233 |
} |
| 234 |
|
| 235 |
selText = $( this ).textSelection( 'getSelection' ); |
| 236 |
const allText = $( this ).textSelection( 'getContents' ); |
| 237 |
const currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } ); |
| 238 |
let startPos = currSelection[ 0 ]; |
| 239 |
const endPos = currSelection[ 1 ]; |
| 240 |
checkSelectedText(); |
| 241 |
let combiningCharSelectionBug = false; |
| 242 |
if ( |
| 243 |
options.selectionStart !== undefined && |
| 244 |
endPos - startPos !== options.selectionEnd - options.selectionStart |
| 245 |
) { |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
startPos = options.selectionStart; |
| 250 |
combiningCharSelectionBug = true; |
| 251 |
|
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
let insertText = pre + selText + post; |
| 256 |
if ( options.splitlines ) { |
| 257 |
insertText = doSplitLines( selText, pre, post ); |
| 258 |
} |
| 259 |
if ( options.ownline ) { |
| 260 |
if ( startPos !== 0 && allText.charAt( startPos - 1 ) !== '\n' && allText.charAt( startPos - 1 ) !== '\r' ) { |
| 261 |
insertText = '\n' + insertText; |
| 262 |
pre += '\n'; |
| 263 |
} |
| 264 |
if ( allText.charAt( endPos ) !== '\n' && allText.charAt( endPos ) !== '\r' ) { |
| 265 |
insertText += '\n'; |
| 266 |
post += '\n'; |
| 267 |
} |
| 268 |
} |
| 269 |
if ( combiningCharSelectionBug ) { |
| 270 |
$( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + insertText + |
| 271 |
allText.slice( endPos ) ); |
| 272 |
} else { |
| 273 |
$( this ).textSelection( 'replaceSelection', insertText ); |
| 274 |
} |
| 275 |
if ( isSample && options.selectPeri && ( !options.splitlines || ( options.splitlines && selText.indexOf( '\n' ) === -1 ) ) ) { |
| 276 |
$( this ).textSelection( 'setSelection', { |
| 277 |
start: startPos + pre.length, |
| 278 |
end: startPos + pre.length + selText.length |
| 279 |
} ); |
| 280 |
} else { |
| 281 |
$( this ).textSelection( 'setSelection', { |
| 282 |
start: startPos + insertText.length |
| 283 |
} ); |
| 284 |
} |
| 285 |
$( this ).trigger( 'encapsulateSelection', [ options.pre, options.peri, options.post, options.ownline, |
| 286 |
options.replace, options.splitlines ] ); |
| 287 |
} ); |
| 288 |
}, |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
getCaretPosition: function ( options ) { |
| 301 |
function getCaret( e ) { |
| 302 |
let caretPos = 0, |
| 303 |
endPos = 0; |
| 304 |
if ( e ) { |
| 305 |
caretPos = e.selectionStart; |
| 306 |
endPos = e.selectionEnd; |
| 307 |
} |
| 308 |
return options.startAndEnd ? [ caretPos, endPos ] : caretPos; |
| 309 |
} |
| 310 |
return getCaret( this.get( 0 ) ); |
| 311 |
}, |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
setSelection: function ( options ) { |
| 324 |
return this.each( function () { |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if ( options.start > this.selectionEnd ) { |
| 329 |
this.selectionEnd = options.end; |
| 330 |
this.selectionStart = options.start; |
| 331 |
} else { |
| 332 |
this.selectionStart = options.start; |
| 333 |
this.selectionEnd = options.end; |
| 334 |
} |
| 335 |
} ); |
| 336 |
}, |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
scrollToCaretPosition: function ( options ) { |
| 350 |
return this.each( function () { |
| 351 |
const clientHeight = this.clientHeight, |
| 352 |
origValue = this.value, |
| 353 |
origSelectionStart = this.selectionStart, |
| 354 |
origSelectionEnd = this.selectionEnd, |
| 355 |
origScrollTop = this.scrollTop; |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
this.value = this.value.slice( 0, this.selectionEnd ); |
| 361 |
this.scrollTop = this.scrollHeight; |
| 362 |
|
| 363 |
|
| 364 |
let calcScrollTop = this.scrollTop; |
| 365 |
this.value = origValue; |
| 366 |
this.selectionStart = origSelectionStart; |
| 367 |
this.selectionEnd = origSelectionEnd; |
| 368 |
|
| 369 |
if ( !options.force ) { |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if ( calcScrollTop < origScrollTop && origScrollTop - calcScrollTop < clientHeight ) { |
| 374 |
calcScrollTop = origScrollTop; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
this.scrollTop = calcScrollTop; |
| 379 |
|
| 380 |
$( this ).trigger( 'scrollToPosition' ); |
| 381 |
} ); |
| 382 |
} |
| 383 |
}; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
$.fn.textSelection = function ( command, commandOptions ) { |
| 429 |
const alternateFn = $( this ).data( 'jquery.textSelection' ); |
| 430 |
|
| 431 |
|
| 432 |
for ( const key in commandOptions ) { |
| 433 |
if ( commandOptions[ key ] === undefined ) { |
| 434 |
delete commandOptions[ key ]; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
switch ( command ) { |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
case 'encapsulateSelection': |
| 445 |
commandOptions = Object.assign( { |
| 446 |
pre: '', |
| 447 |
peri: '', |
| 448 |
post: '', |
| 449 |
ownline: false, |
| 450 |
replace: false, |
| 451 |
selectPeri: true, |
| 452 |
splitlines: false, |
| 453 |
selectionStart: undefined, |
| 454 |
selectionEnd: undefined |
| 455 |
}, commandOptions ); |
| 456 |
break; |
| 457 |
case 'getCaretPosition': |
| 458 |
commandOptions = Object.assign( { |
| 459 |
startAndEnd: false |
| 460 |
}, commandOptions ); |
| 461 |
break; |
| 462 |
case 'setSelection': |
| 463 |
commandOptions = Object.assign( { |
| 464 |
start: undefined, |
| 465 |
end: undefined |
| 466 |
}, commandOptions ); |
| 467 |
if ( commandOptions.end === undefined ) { |
| 468 |
commandOptions.end = commandOptions.start; |
| 469 |
} |
| 470 |
break; |
| 471 |
case 'scrollToCaretPosition': |
| 472 |
commandOptions = Object.assign( { |
| 473 |
force: false |
| 474 |
}, commandOptions ); |
| 475 |
break; |
| 476 |
case 'register': |
| 477 |
if ( alternateFn ) { |
| 478 |
throw new Error( 'Another textSelection API was already registered' ); |
| 479 |
} |
| 480 |
$( this ).data( 'jquery.textSelection', commandOptions ); |
| 481 |
|
| 482 |
|
| 483 |
return; |
| 484 |
case 'unregister': |
| 485 |
$( this ).removeData( 'jquery.textSelection' ); |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
const retval = ( alternateFn && alternateFn[ command ] || fn[ command ] ).call( this, commandOptions ); |
| 490 |
|
| 491 |
return retval; |
| 492 |
}; |
| 493 |
}() ); |