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 "sourcearea" plugin. It registers the "source" editing
|
---|
8 | * mode, which displays the raw data being edited in the editor.
|
---|
9 | */
|
---|
10 |
|
---|
11 | CKEDITOR.plugins.add( 'sourcearea',
|
---|
12 | {
|
---|
13 | requires : [ 'editingblock' ],
|
---|
14 |
|
---|
15 | init : function( editor )
|
---|
16 | {
|
---|
17 | var sourcearea = CKEDITOR.plugins.sourcearea;
|
---|
18 |
|
---|
19 | editor.on( 'editingBlockReady', function()
|
---|
20 | {
|
---|
21 | var textarea,
|
---|
22 | onResize;
|
---|
23 |
|
---|
24 | editor.addMode( 'source',
|
---|
25 | {
|
---|
26 | load : function( holderElement, data )
|
---|
27 | {
|
---|
28 | if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
|
---|
29 | holderElement.setStyle( 'position', 'relative' );
|
---|
30 |
|
---|
31 | // Create the source area <textarea>.
|
---|
32 | editor.textarea = textarea = new CKEDITOR.dom.element( 'textarea' );
|
---|
33 | textarea.setAttributes(
|
---|
34 | {
|
---|
35 | dir : 'ltr',
|
---|
36 | tabIndex : -1
|
---|
37 | });
|
---|
38 | textarea.addClass( 'cke_source' );
|
---|
39 | textarea.addClass( 'cke_enable_context_menu' );
|
---|
40 |
|
---|
41 | var styles =
|
---|
42 | {
|
---|
43 | // IE7 has overflow the <textarea> from wrapping table cell.
|
---|
44 | width : CKEDITOR.env.ie7Compat ? '99%' : '100%',
|
---|
45 | height : '100%',
|
---|
46 | resize : 'none',
|
---|
47 | outline : 'none',
|
---|
48 | 'text-align' : 'left'
|
---|
49 | };
|
---|
50 |
|
---|
51 | // The textarea height/width='100%' doesn't
|
---|
52 | // constraint to the 'td' in IE strick mode
|
---|
53 | if ( CKEDITOR.env.ie )
|
---|
54 | {
|
---|
55 | if ( !CKEDITOR.env.ie8Compat )
|
---|
56 | {
|
---|
57 | onResize = function()
|
---|
58 | {
|
---|
59 | // Holder rectange size is stretched by textarea,
|
---|
60 | // so hide it just for a moment.
|
---|
61 | textarea.hide();
|
---|
62 | textarea.setStyle( 'height', holderElement.$.clientHeight + 'px' );
|
---|
63 | // When we have proper holder size, show textarea again.
|
---|
64 | textarea.show();
|
---|
65 | };
|
---|
66 | editor.on( 'resize', onResize );
|
---|
67 | editor.on( 'afterCommandExec', function( event )
|
---|
68 | {
|
---|
69 | if ( event.data.name == 'toolbarCollapse' )
|
---|
70 | onResize();
|
---|
71 | });
|
---|
72 | styles.height = holderElement.$.clientHeight + 'px';
|
---|
73 | }
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | // By some yet unknown reason, we must stop the
|
---|
78 | // mousedown propagation for the textarea,
|
---|
79 | // otherwise it's not possible to place the caret
|
---|
80 | // inside of it (non IE).
|
---|
81 | textarea.on( 'mousedown', function( evt )
|
---|
82 | {
|
---|
83 | evt.data.stopPropagation();
|
---|
84 | } );
|
---|
85 | }
|
---|
86 |
|
---|
87 | // Reset the holder element and append the
|
---|
88 | // <textarea> to it.
|
---|
89 | holderElement.setHtml( '' );
|
---|
90 | holderElement.append( textarea );
|
---|
91 | textarea.setStyles( styles );
|
---|
92 |
|
---|
93 | textarea.on( 'blur', function()
|
---|
94 | {
|
---|
95 | editor.focusManager.blur();
|
---|
96 | });
|
---|
97 |
|
---|
98 | textarea.on( 'focus', function()
|
---|
99 | {
|
---|
100 | editor.focusManager.focus();
|
---|
101 | });
|
---|
102 |
|
---|
103 | // The editor data "may be dirty" after this point.
|
---|
104 | editor.mayBeDirty = true;
|
---|
105 |
|
---|
106 | // Set the <textarea> value.
|
---|
107 | this.loadData( data );
|
---|
108 |
|
---|
109 | var keystrokeHandler = editor.keystrokeHandler;
|
---|
110 | if ( keystrokeHandler )
|
---|
111 | keystrokeHandler.attach( textarea );
|
---|
112 |
|
---|
113 | setTimeout( function()
|
---|
114 | {
|
---|
115 | editor.mode = 'source';
|
---|
116 | editor.fire( 'mode' );
|
---|
117 | },
|
---|
118 | ( CKEDITOR.env.gecko || CKEDITOR.env.webkit ) ? 100 : 0 );
|
---|
119 | },
|
---|
120 |
|
---|
121 | loadData : function( data )
|
---|
122 | {
|
---|
123 | textarea.setValue( data );
|
---|
124 | editor.fire( 'dataReady' );
|
---|
125 | },
|
---|
126 |
|
---|
127 | getData : function()
|
---|
128 | {
|
---|
129 | return textarea.getValue();
|
---|
130 | },
|
---|
131 |
|
---|
132 | getSnapshotData : function()
|
---|
133 | {
|
---|
134 | return textarea.getValue();
|
---|
135 | },
|
---|
136 |
|
---|
137 | unload : function( holderElement )
|
---|
138 | {
|
---|
139 | editor.textarea = textarea = null;
|
---|
140 |
|
---|
141 | if ( onResize )
|
---|
142 | editor.removeListener( 'resize', onResize );
|
---|
143 |
|
---|
144 | if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )
|
---|
145 | holderElement.removeStyle( 'position' );
|
---|
146 | },
|
---|
147 |
|
---|
148 | focus : function()
|
---|
149 | {
|
---|
150 | textarea.focus();
|
---|
151 | }
|
---|
152 | });
|
---|
153 | });
|
---|
154 |
|
---|
155 | editor.addCommand( 'source', sourcearea.commands.source );
|
---|
156 |
|
---|
157 | if ( editor.ui.addButton )
|
---|
158 | {
|
---|
159 | editor.ui.addButton( 'Source',
|
---|
160 | {
|
---|
161 | label : editor.lang.source,
|
---|
162 | command : 'source'
|
---|
163 | });
|
---|
164 | }
|
---|
165 |
|
---|
166 | editor.on( 'mode', function()
|
---|
167 | {
|
---|
168 | editor.getCommand( 'source' ).setState(
|
---|
169 | editor.mode == 'source' ?
|
---|
170 | CKEDITOR.TRISTATE_ON :
|
---|
171 | CKEDITOR.TRISTATE_OFF );
|
---|
172 | });
|
---|
173 | }
|
---|
174 | });
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Holds the definition of commands an UI elements included with the sourcearea
|
---|
178 | * plugin.
|
---|
179 | * @example
|
---|
180 | */
|
---|
181 | CKEDITOR.plugins.sourcearea =
|
---|
182 | {
|
---|
183 | commands :
|
---|
184 | {
|
---|
185 | source :
|
---|
186 | {
|
---|
187 | modes : { wysiwyg:1, source:1 },
|
---|
188 |
|
---|
189 | exec : function( editor )
|
---|
190 | {
|
---|
191 | if ( editor.mode == 'wysiwyg' )
|
---|
192 | editor.fire( 'saveSnapshot' );
|
---|
193 | editor.getCommand( 'source' ).setState( CKEDITOR.TRISTATE_DISABLED );
|
---|
194 | editor.setMode( editor.mode == 'source' ? 'wysiwyg' : 'source' );
|
---|
195 | },
|
---|
196 |
|
---|
197 | canUndo : false
|
---|
198 | }
|
---|
199 | }
|
---|
200 | };
|
---|