From f049e0b49be9b783051a7dc05e4ef88da8cfe406 Mon Sep 17 00:00:00 2001 From: Derk-Jan Hartman Date: Mon, 28 Jul 2014 22:28:29 +0200 Subject: [PATCH] jquery.textSelection: Add ability to register custom implementation At times we replace the textarea with a different implementation, for instance in the former iframe code of WikiEditor and now in CodeEditor. Obviously only one implementation of this API can be in control at a time, but in order to disable/enable which one exactly is currently determined by the existence of function in the core WikiEditor context. This is a remnant of when this code was still in WikiEditor. I added two commands, "register" and "unregister", that take an object that has alternative function implementations for one or more commands. Bug: 29328 Change-Id: I14492572f7eb9bbd1af68872dbfef5159126f107 --- resources/src/jquery/jquery.textSelection.js | 24 ++++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/resources/src/jquery/jquery.textSelection.js b/resources/src/jquery/jquery.textSelection.js index 8d440fdcee..632769ba54 100644 --- a/resources/src/jquery/jquery.textSelection.js +++ b/resources/src/jquery/jquery.textSelection.js @@ -24,8 +24,9 @@ $.fn.textSelection = function ( command, options ) { var fn, + alternateFn, context, - hasWikiEditorSurface, // The alt edit surface needs to implement the WikiEditor API + hasWikiEditor, needSave, retval; @@ -507,6 +508,8 @@ } }; + alternateFn = $( this ).data( 'jquery.textSelection' ); + // Apply defaults switch ( command ) { //case 'getContents': // no params @@ -550,19 +553,30 @@ 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 && context.$iframe !== 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(); } -- 2.20.1