1 | /*
|
---|
2 | Copyright (c) 2003-2010, 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 | if ( editor.config.startupFocus )
|
---|
99 | editor.focus();
|
---|
100 |
|
---|
101 | // Fire instanceReady for both the editor and CKEDITOR, but
|
---|
102 | // defer this until the whole execution has completed
|
---|
103 | // to guarantee the editor is fully responsible.
|
---|
104 | setTimeout( function(){
|
---|
105 | editor.fireOnce( 'instanceReady' );
|
---|
106 | CKEDITOR.fire( 'instanceReady', null, editor );
|
---|
107 | } );
|
---|
108 | });
|
---|
109 | }
|
---|
110 | });
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * The current editing mode. An editing mode is basically a viewport for
|
---|
114 | * editing or content viewing. By default the possible values for this
|
---|
115 | * property are "wysiwyg" and "source".
|
---|
116 | * @type String
|
---|
117 | * @example
|
---|
118 | * alert( CKEDITOR.instances.editor1.mode ); // "wysiwyg" (e.g.)
|
---|
119 | */
|
---|
120 | CKEDITOR.editor.prototype.mode = '';
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Registers an editing mode. This function is to be used mainly by plugins.
|
---|
124 | * @param {String} mode The mode name.
|
---|
125 | * @param {Object} modeEditor The mode editor definition.
|
---|
126 | * @example
|
---|
127 | */
|
---|
128 | CKEDITOR.editor.prototype.addMode = function( mode, modeEditor )
|
---|
129 | {
|
---|
130 | modeEditor.name = mode;
|
---|
131 | ( this._.modes || ( this._.modes = {} ) )[ mode ] = modeEditor;
|
---|
132 | };
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Sets the current editing mode in this editor instance.
|
---|
136 | * @param {String} mode A registered mode name.
|
---|
137 | * @example
|
---|
138 | * // Switch to "source" view.
|
---|
139 | * CKEDITOR.instances.editor1.setMode( 'source' );
|
---|
140 | */
|
---|
141 | CKEDITOR.editor.prototype.setMode = function( mode )
|
---|
142 | {
|
---|
143 | var data,
|
---|
144 | holderElement = this.getThemeSpace( 'contents' ),
|
---|
145 | isDirty = this.checkDirty();
|
---|
146 |
|
---|
147 | // Unload the previous mode.
|
---|
148 | if ( this.mode )
|
---|
149 | {
|
---|
150 | if ( mode == this.mode )
|
---|
151 | return;
|
---|
152 |
|
---|
153 | this.fire( 'beforeModeUnload' );
|
---|
154 |
|
---|
155 | var currentMode = getMode( this );
|
---|
156 | data = currentMode.getData();
|
---|
157 | currentMode.unload( holderElement );
|
---|
158 | this.mode = '';
|
---|
159 | }
|
---|
160 |
|
---|
161 | holderElement.setHtml( '' );
|
---|
162 |
|
---|
163 | // Load required mode.
|
---|
164 | var modeEditor = getMode( this, mode );
|
---|
165 | if ( !modeEditor )
|
---|
166 | throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';
|
---|
167 |
|
---|
168 | if ( !isDirty )
|
---|
169 | {
|
---|
170 | this.on( 'mode', function()
|
---|
171 | {
|
---|
172 | this.resetDirty();
|
---|
173 | this.removeListener( 'mode', arguments.callee );
|
---|
174 | });
|
---|
175 | }
|
---|
176 |
|
---|
177 | modeEditor.load( holderElement, ( typeof data ) != 'string' ? this.getData() : data);
|
---|
178 | };
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Moves the selection focus to the editing are space in the editor.
|
---|
182 | */
|
---|
183 | CKEDITOR.editor.prototype.focus = function()
|
---|
184 | {
|
---|
185 | var mode = getMode( this );
|
---|
186 | if ( mode )
|
---|
187 | mode.focus();
|
---|
188 | };
|
---|
189 | })();
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * The mode to load at the editor startup. It depends on the plugins
|
---|
193 | * loaded. By default, the "wysiwyg" and "source" modes are available.
|
---|
194 | * @type String
|
---|
195 | * @default 'wysiwyg'
|
---|
196 | * @example
|
---|
197 | * config.startupMode = 'source';
|
---|
198 | */
|
---|
199 | CKEDITOR.config.startupMode = 'wysiwyg';
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Sets whether the editor should have the focus when the page loads.
|
---|
203 | * @type Boolean
|
---|
204 | * @default false
|
---|
205 | * @example
|
---|
206 | * config.startupFocus = true;
|
---|
207 | */
|
---|
208 | CKEDITOR.config.startupFocus = false;
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Whether to render or not the editing block area in the editor interface.
|
---|
212 | * @type Boolean
|
---|
213 | * @default true
|
---|
214 | * @example
|
---|
215 | * config.editingBlock = false;
|
---|
216 | */
|
---|
217 | CKEDITOR.config.editingBlock = true;
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Fired when a CKEDITOR instance is created, fully initialized and ready for interaction.
|
---|
221 | * @name CKEDITOR#instanceReady
|
---|
222 | * @event
|
---|
223 | * @param {CKEDITOR.editor} editor The editor instance that has been created.
|
---|
224 | */
|
---|