$.fn.textSelection = function ( command, options ) {
var fn,
+ alternateFn,
context,
- hasWikiEditorSurface, // The alt edit surface needs to implement the WikiEditor API
+ hasWikiEditor,
needSave,
retval;
} else {
while ( selText.charAt( selText.length - 1 ) === ' ' ) {
// Exclude ending space char
- selText = selText.substring( 0, selText.length - 1 );
+ selText = selText.slice( 0, -1 );
post += ' ';
}
while ( selText.charAt( 0 ) === ' ' ) {
// Exclude prepending space char
- selText = selText.substring( 1, selText.length );
+ selText = selText.slice( 1 );
pre = ' ' + pre;
}
}
post += '\n';
}
}
- this.value = this.value.substring( 0, startPos ) + insertText +
- this.value.substring( endPos, this.value.length );
+ this.value = this.value.slice( 0, startPos ) + insertText +
+ this.value.slice( endPos );
// Setting this.value scrolls the textarea to the top, restore the scroll position
this.scrollTop = scrollTop;
if ( window.opera ) {
}
};
+ alternateFn = $( this ).data( 'jquery.textSelection' );
+
// Apply defaults
switch ( command ) {
//case 'getContents': // no params
force: false // Force a scroll even if the caret position is already visible
}, options );
break;
+ case 'register':
+ if ( alternateFn ) {
+ throw new Error( 'Another textSelection API was already registered' );
+ }
+ $( this ).data( 'jquery.textSelection', options );
+ // No need to update alternateFn as this command only stores the options.
+ // A command that uses it will set it again.
+ return;
+ case 'unregister':
+ $( this ).removeData( 'jquery.textSelection' );
+ return;
}
context = $( this ).data( 'wikiEditor-context' );
- hasWikiEditorSurface = ( context !== undefined );
+ hasWikiEditor = ( context !== undefined && context.$iframe !== undefined );
// IE selection restore voodoo
needSave = false;
- if ( hasWikiEditorSurface && context.savedSelection !== null ) {
+ if ( hasWikiEditor && context.savedSelection !== null ) {
context.fn.restoreSelection();
needSave = true;
}
- retval = ( hasWikiEditorSurface && context.fn[command] !== undefined ? context.fn : fn )[command].call( this, options );
- if ( hasWikiEditorSurface && needSave ) {
+ retval = ( alternateFn && alternateFn[command] || fn[command] ).call( this, options );
+ if ( hasWikiEditor && needSave ) {
context.fn.saveSelection();
}