[3019] | 1 | /*
|
---|
| 2 | Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
|
---|
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * @fileOverview The default editing block plugin, which holds the editing area
|
---|
| 8 | * and source view.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | (function()
|
---|
| 12 | {
|
---|
| 13 | var getMode = function( editor, mode )
|
---|
| 14 | {
|
---|
| 15 | return editor._.modes && editor._.modes[ mode || editor.mode ];
|
---|
| 16 | };
|
---|
| 17 |
|
---|
| 18 | // This is a semaphore used to avoid recursive calls between
|
---|
| 19 | // the following data handling functions.
|
---|
| 20 | var isHandlingData;
|
---|
| 21 |
|
---|
| 22 | CKEDITOR.plugins.add( 'editingblock',
|
---|
| 23 | {
|
---|
| 24 | init : function( editor )
|
---|
| 25 | {
|
---|
| 26 | if ( !editor.config.editingBlock )
|
---|
| 27 | return;
|
---|
| 28 |
|
---|
| 29 | editor.on( 'themeSpace', function( event )
|
---|
| 30 | {
|
---|
| 31 | if ( event.data.space == 'contents' )
|
---|
| 32 | event.data.html += '<br>';
|
---|
| 33 | });
|
---|
| 34 |
|
---|
| 35 | editor.on( 'themeLoaded', function()
|
---|
| 36 | {
|
---|
| 37 | editor.fireOnce( 'editingBlockReady' );
|
---|
| 38 | });
|
---|
| 39 |
|
---|
| 40 | editor.on( 'uiReady', function()
|
---|
| 41 | {
|
---|
| 42 | editor.setMode( editor.config.startupMode );
|
---|
| 43 | });
|
---|
| 44 |
|
---|
| 45 | editor.on( 'afterSetData', function()
|
---|
| 46 | {
|
---|
| 47 | if ( !isHandlingData )
|
---|
| 48 | {
|
---|
| 49 | function setData()
|
---|
| 50 | {
|
---|
| 51 | isHandlingData = true;
|
---|
| 52 | getMode( editor ).loadData( editor.getData() );
|
---|
| 53 | isHandlingData = false;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | if ( editor.mode )
|
---|
| 57 | setData();
|
---|
| 58 | else
|
---|
| 59 | {
|
---|
| 60 | editor.on( 'mode', function()
|
---|
| 61 | {
|
---|
| 62 | setData();
|
---|
| 63 | editor.removeListener( 'mode', arguments.callee );
|
---|
| 64 | });
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | });
|
---|
| 68 |
|
---|
| 69 | editor.on( 'beforeGetData', function()
|
---|
| 70 | {
|
---|
| 71 | if ( !isHandlingData && editor.mode )
|
---|
| 72 | {
|
---|
| 73 | isHandlingData = true;
|
---|
| 74 | editor.setData( getMode( editor ).getData() );
|
---|
| 75 | isHandlingData = false;
|
---|
| 76 | }
|
---|
| 77 | });
|
---|
| 78 |
|
---|
| 79 | editor.on( 'getSnapshot', function( event )
|
---|
| 80 | {
|
---|
| 81 | if ( editor.mode )
|
---|
| 82 | event.data = getMode( editor ).getSnapshotData();
|
---|
| 83 | });
|
---|
| 84 |
|
---|
| 85 | editor.on( 'loadSnapshot', function( event )
|
---|
| 86 | {
|
---|
| 87 | if ( editor.mode )
|
---|
| 88 | getMode( editor ).loadSnapshotData( event.data );
|
---|
| 89 | });
|
---|
| 90 |
|
---|
| 91 | // For the first "mode" call, we'll also fire the "instanceReady"
|
---|
| 92 | // event.
|
---|
| 93 | editor.on( 'mode', function( event )
|
---|
| 94 | {
|
---|
| 95 | // Do that once only.
|
---|
| 96 | event.removeListener();
|
---|
| 97 |
|
---|
| 98 | // Grab editor focus if the editor container is focused. (#3104)
|
---|
| 99 | var focusGrabber = editor.container;
|
---|
| 100 |
|
---|
| 101 | // Safari 3 can't handle tabindex in all elements, so we do
|
---|
| 102 | // a trick to make it move the focus to the editor on TAB.
|
---|
| 103 | if ( CKEDITOR.env.webkit && CKEDITOR.env.version < 528 )
|
---|
| 104 | {
|
---|
| 105 | var tabIndex = editor.config.tabIndex || editor.element.getAttribute( 'tabindex' ) || 0;
|
---|
| 106 | focusGrabber = focusGrabber.append( CKEDITOR.dom.element.createFromHtml(
|
---|
| 107 | '<input' +
|
---|
| 108 | ' tabindex="' + tabIndex + '"' +
|
---|
| 109 | ' style="position:absolute; left:-10000">' ) );
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | focusGrabber.on( 'focus', function()
|
---|
| 113 | {
|
---|
| 114 | editor.focus();
|
---|
| 115 | });
|
---|
| 116 |
|
---|
| 117 | if ( editor.config.startupFocus )
|
---|
| 118 | editor.focus();
|
---|
| 119 |
|
---|
| 120 | // Fire instanceReady for both the editor and CKEDITOR, but
|
---|
| 121 | // defer this until the whole execution has completed
|
---|
| 122 | // to guarantee the editor is fully responsible.
|
---|
| 123 | setTimeout( function(){
|
---|
| 124 | editor.fireOnce( 'instanceReady' );
|
---|
| 125 | CKEDITOR.fire( 'instanceReady', null, editor );
|
---|
| 126 | } );
|
---|
| 127 | });
|
---|
| 128 | }
|
---|
| 129 | });
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * The current editing mode. An editing mode is basically a viewport for
|
---|
| 133 | * editing or content viewing. By default the possible values for this
|
---|
| 134 | * property are "wysiwyg" and "source".
|
---|
| 135 | * @type String
|
---|
| 136 | * @example
|
---|
| 137 | * alert( CKEDITOR.instances.editor1.mode ); // "wysiwyg" (e.g.)
|
---|
| 138 | */
|
---|
| 139 | CKEDITOR.editor.prototype.mode = '';
|
---|
| 140 |
|
---|
| 141 | /**
|
---|
| 142 | * Registers an editing mode. This function is to be used mainly by plugins.
|
---|
| 143 | * @param {String} mode The mode name.
|
---|
| 144 | * @param {Object} modeEditor The mode editor definition.
|
---|
| 145 | * @example
|
---|
| 146 | */
|
---|
| 147 | CKEDITOR.editor.prototype.addMode = function( mode, modeEditor )
|
---|
| 148 | {
|
---|
| 149 | modeEditor.name = mode;
|
---|
| 150 | ( this._.modes || ( this._.modes = {} ) )[ mode ] = modeEditor;
|
---|
| 151 | };
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
| 154 | * Sets the current editing mode in this editor instance.
|
---|
| 155 | * @param {String} mode A registered mode name.
|
---|
| 156 | * @example
|
---|
| 157 | * // Switch to "source" view.
|
---|
| 158 | * CKEDITOR.instances.editor1.setMode( 'source' );
|
---|
| 159 | */
|
---|
| 160 | CKEDITOR.editor.prototype.setMode = function( mode )
|
---|
| 161 | {
|
---|
| 162 | var data,
|
---|
| 163 | holderElement = this.getThemeSpace( 'contents' ),
|
---|
| 164 | isDirty = this.checkDirty();
|
---|
| 165 |
|
---|
| 166 | // Unload the previous mode.
|
---|
| 167 | if ( this.mode )
|
---|
| 168 | {
|
---|
| 169 | if ( mode == this.mode )
|
---|
| 170 | return;
|
---|
| 171 |
|
---|
| 172 | this.fire( 'beforeModeUnload' );
|
---|
| 173 |
|
---|
| 174 | var currentMode = getMode( this );
|
---|
| 175 | data = currentMode.getData();
|
---|
| 176 | currentMode.unload( holderElement );
|
---|
| 177 | this.mode = '';
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | holderElement.setHtml( '' );
|
---|
| 181 |
|
---|
| 182 | // Load required mode.
|
---|
| 183 | var modeEditor = getMode( this, mode );
|
---|
| 184 | if ( !modeEditor )
|
---|
| 185 | throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';
|
---|
| 186 |
|
---|
| 187 | if ( !isDirty )
|
---|
| 188 | {
|
---|
| 189 | this.on( 'mode', function()
|
---|
| 190 | {
|
---|
| 191 | this.resetDirty();
|
---|
| 192 | this.removeListener( 'mode', arguments.callee );
|
---|
| 193 | });
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | modeEditor.load( holderElement, ( typeof data ) != 'string' ? this.getData() : data);
|
---|
| 197 | };
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
| 200 | * Moves the selection focus to the editing are space in the editor.
|
---|
| 201 | */
|
---|
| 202 | CKEDITOR.editor.prototype.focus = function()
|
---|
| 203 | {
|
---|
| 204 | var mode = getMode( this );
|
---|
| 205 | if ( mode )
|
---|
| 206 | mode.focus();
|
---|
| 207 | };
|
---|
| 208 | })();
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
| 211 | * The mode to load at the editor startup. It depends on the plugins
|
---|
| 212 | * loaded. By default, the "wysiwyg" and "source" modes are available.
|
---|
| 213 | * @type String
|
---|
| 214 | * @default 'wysiwyg'
|
---|
| 215 | * @example
|
---|
| 216 | * config.startupMode = 'source';
|
---|
| 217 | */
|
---|
| 218 | CKEDITOR.config.startupMode = 'wysiwyg';
|
---|
| 219 |
|
---|
| 220 | /**
|
---|
| 221 | * Sets whether the editor should have the focus when the page loads.
|
---|
| 222 | * @type Boolean
|
---|
| 223 | * @default false
|
---|
| 224 | * @example
|
---|
| 225 | * config.startupFocus = true;
|
---|
| 226 | */
|
---|
| 227 | CKEDITOR.config.startupFocus = false;
|
---|
| 228 |
|
---|
| 229 | /**
|
---|
| 230 | * Whether to render or not the editing block area in the editor interface.
|
---|
| 231 | * @type Boolean
|
---|
| 232 | * @default true
|
---|
| 233 | * @example
|
---|
| 234 | * config.editingBlock = false;
|
---|
| 235 | */
|
---|
| 236 | CKEDITOR.config.editingBlock = true;
|
---|