1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
* 全屏组件
*/
(function(){
//状态缓存
var STATUS_CACHE = {},
//状态值列表
STATUS_LIST = [ 'width', 'height', 'position', 'top', 'left', 'margin', 'padding', 'overflowX', 'overflowY' ],
CONTENT_AREA_STATUS = {},
//页面状态
DOCUMENT_STATUS = {},
DOCUMENT_ELEMENT_STATUS = {},
FULLSCREENS = {};
UM.registerUI('fullscreen', function( name ){
var me = this,
$button = $.eduibutton({
'icon': 'fullscreen',
'title': (me.options.labelMap && me.options.labelMap[name]) || me.getLang("labelMap." + name),
'click': function(){
//切换
me.execCommand( name );
UM.setTopEditor(me);
}
});
me.addListener( "selectionchange", function () {
var state = this.queryCommandState( name );
$button.edui().disabled( state == -1 ).active( state == 1 );
} );
//切换至全屏
me.addListener('ready', function(){
me.options.fullscreen && Fullscreen.getInstance( me ).toggle();
});
return $button;
});
UM.commands[ 'fullscreen' ] = {
execCommand: function (cmdName) {
Fullscreen.getInstance( this ).toggle();
},
queryCommandState: function (cmdName) {
return this._edui_fullscreen_status;
},
notNeedUndo: 1
};
function Fullscreen( editor ) {
var me = this;
if( !editor ) {
throw new Error('invalid params, notfound editor');
}
me.editor = editor;
//记录初始化的全屏组件
FULLSCREENS[ editor.uid ] = this;
editor.addListener('destroy', function(){
delete FULLSCREENS[ editor.uid ];
me.editor = null;
});
}
Fullscreen.prototype = {
/**
* 全屏状态切换
*/
toggle: function(){
var editor = this.editor,
//当前编辑器的缩放状态
_edui_fullscreen_status = this.isFullState();
editor.fireEvent('beforefullscreenchange', !_edui_fullscreen_status );
//更新状态
this.update( !_edui_fullscreen_status );
!_edui_fullscreen_status ? this.enlarge() : this.revert();
editor.fireEvent('afterfullscreenchange', !_edui_fullscreen_status );
if(editor.body.contentEditable === 'true'){
editor.fireEvent( 'fullscreenchanged', !_edui_fullscreen_status );
}
editor.fireEvent( 'selectionchange' );
},
/**
* 执行放大
*/
enlarge: function(){
this.saveSataus();
this.setDocumentStatus();
this.resize();
},
/**
* 全屏还原
*/
revert: function(){
//还原CSS表达式
var options = this.editor.options,
height = /%$/.test(options.initialFrameHeight) ? '100%' : (options.initialFrameHeight - this.getStyleValue("padding-top")- this.getStyleValue("padding-bottom") - this.getStyleValue('border-width'));
$.IE6 && this.getEditorHolder().style.setExpression('height', 'this.scrollHeight <= ' + height + ' ? "' + height + 'px" : "auto"');
//还原容器状态
this.revertContainerStatus();
this.revertContentAreaStatus();
this.revertDocumentStatus();
},
/**
* 更新状态
* @param isFull 当前状态是否是全屏状态
*/
update: function( isFull ) {
this.editor._edui_fullscreen_status = isFull;
},
/**
* 调整当前编辑器的大小, 如果当前编辑器不处于全屏状态, 则不做调整
*/
resize: function(){
var $win = null,
height = 0,
width = 0,
borderWidth = 0,
paddingWidth = 0,
editor = this.editor,
me = this,
bound = null,
editorBody = null;
if( !this.isFullState() ) {
return;
}
$win = $( window );
width = $win.width();
height = $win.height();
editorBody = this.getEditorHolder();
//文本编辑区border宽度
borderWidth = parseInt( domUtils.getComputedStyle( editorBody, 'border-width' ), 10 ) || 0;
//容器border宽度
borderWidth += parseInt( domUtils.getComputedStyle( editor.container, 'border-width' ), 10 ) || 0;
//容器padding
paddingWidth += parseInt( domUtils.getComputedStyle( editorBody, 'padding-left' ), 10 ) + parseInt( domUtils.getComputedStyle( editorBody, 'padding-right' ), 10 ) || 0;
//干掉css表达式
$.IE6 && editorBody.style.setExpression( 'height', null );
bound = this.getBound();
$( editor.container ).css( {
width: width + 'px',
height: height + 'px',
position: !$.IE6 ? 'fixed' : 'absolute',
top: bound.top,
left: bound.left,
margin: 0,
padding: 0,
overflowX: 'hidden',
overflowY: 'hidden'
} );
$( editorBody ).css({
width: width - 2*borderWidth - paddingWidth + 'px',
height: height - 2*borderWidth - ( editor.options.withoutToolbar ? 0 : $( '.edui-toolbar', editor.container ).outerHeight() ) - $( '.edui-bottombar', editor.container).outerHeight() + 'px',
overflowX: 'hidden',
overflowY: 'auto'
});
},
/**
* 保存状态
*/
saveSataus: function(){
var styles = this.editor.container.style,
tmp = null,
cache = {};
for( var i= 0, len = STATUS_LIST.length; i<len; i++ ) {
tmp = STATUS_LIST[ i ];
cache[ tmp ] = styles[ tmp ];
}
STATUS_CACHE[ this.editor.uid ] = cache;
this.saveContentAreaStatus();
this.saveDocumentStatus();
},
saveContentAreaStatus: function(){
var $holder = $(this.getEditorHolder());
CONTENT_AREA_STATUS[ this.editor.uid ] = {
width: $holder.css("width"),
overflowX: $holder.css("overflowX"),
overflowY: $holder.css("overflowY"),
height: $holder.css("height")
};
},
/**
* 保存与指定editor相关的页面的状态
*/
saveDocumentStatus: function(){
var $doc = $( this.getEditorDocumentBody() );
DOCUMENT_STATUS[ this.editor.uid ] = {
overflowX: $doc.css( 'overflowX' ),
overflowY: $doc.css( 'overflowY' )
};
DOCUMENT_ELEMENT_STATUS[ this.editor.uid ] = {
overflowX: $( this.getEditorDocumentElement() ).css( 'overflowX'),
overflowY: $( this.getEditorDocumentElement() ).css( 'overflowY' )
};
},
/**
* 恢复容器状态
*/
revertContainerStatus: function(){
$( this.editor.container ).css( this.getEditorStatus() );
},
/**
* 恢复编辑区状态
*/
revertContentAreaStatus: function(){
var holder = this.getEditorHolder(),
state = this.getContentAreaStatus();
if ( this.supportMin() ) {
delete state.height;
holder.style.height = null;
}
$( holder ).css( state );
},
/**
* 恢复页面状态
*/
revertDocumentStatus: function() {
var status = this.getDocumentStatus();
$( this.getEditorDocumentBody() ).css( 'overflowX', status.body.overflowX );
$( this.getEditorDocumentElement() ).css( 'overflowY', status.html.overflowY );
},
setDocumentStatus: function(){
$(this.getEditorDocumentBody()).css( {
overflowX: 'hidden',
overflowY: 'hidden'
} );
$(this.getEditorDocumentElement()).css( {
overflowX: 'hidden',
overflowY: 'hidden'
} );
},
/**
* 检测当前编辑器是否处于全屏状态全屏状态
* @returns {boolean} 是否处于全屏状态
*/
isFullState: function(){
return !!this.editor._edui_fullscreen_status;
},
/**
* 获取编辑器状态
*/
getEditorStatus: function(){
return STATUS_CACHE[ this.editor.uid ];
},
getContentAreaStatus: function(){
return CONTENT_AREA_STATUS[ this.editor.uid ];
},
getEditorDocumentElement: function(){
return this.editor.container.ownerDocument.documentElement;
},
getEditorDocumentBody: function(){
return this.editor.container.ownerDocument.body;
},
/**
* 获取编辑区包裹对象
*/
getEditorHolder: function(){
return this.editor.body;
},
/**
* 获取编辑器状态
* @returns {*}
*/
getDocumentStatus: function(){
return {
'body': DOCUMENT_STATUS[ this.editor.uid ],
'html': DOCUMENT_ELEMENT_STATUS[ this.editor.uid ]
};
},
supportMin: function () {
var node = null;
if ( !this._support ) {
node = document.createElement("div");
this._support = "minWidth" in node.style;
node = null;
}
return this._support;
},
getBound: function () {
var tags = {
html: true,
body: true
},
result = {
top: 0,
left: 0
},
offsetParent = null;
if ( !$.IE6 ) {
return result;
}
offsetParent = this.editor.container.offsetParent;
if( offsetParent && !tags[ offsetParent.nodeName.toLowerCase() ] ) {
tags = offsetParent.getBoundingClientRect();
result.top = -tags.top;
result.left = -tags.left;
}
return result;
},
getStyleValue: function (attr) {
return parseInt(domUtils.getComputedStyle( this.getEditorHolder() ,attr));
}
};
$.extend( Fullscreen, {
/**
* 监听resize
*/
listen: function(){
var timer = null;
if( Fullscreen._hasFullscreenListener ) {
return;
}
Fullscreen._hasFullscreenListener = true;
$( window ).on( 'resize', function(){
if( timer !== null ) {
window.clearTimeout( timer );
timer = null;
}
timer = window.setTimeout(function(){
for( var key in FULLSCREENS ) {
FULLSCREENS[ key ].resize();
}
timer = null;
}, 50);
} );
},
getInstance: function ( editor ) {
if ( !FULLSCREENS[editor.uid ] ) {
new Fullscreen( editor );
}
return FULLSCREENS[editor.uid ];
}
});
//开始监听
Fullscreen.listen();
})();