第 3 步
返回富文本编辑器的基本原理与实践
1 if (!YAHOO.realazy)
2 YAHOO.realazy = {};
3
4 if (!YAHOO.realazy.RTE)
5 YAHOO.realazy.RTE = {};
6
7 (function(){
8 var $D = YAHOO.util.Dom,
9 $E = YAHOO.util.Event;
10
11 var ua = YAHOO.env.ua,
12 isIE = ua.ie,
13 isGecko = ua.gecko,
14 isOpera = ua.opera,
15 isWebkit = ua.webkit;
16
17
22 YAHOO.realazy.RTE = function(elTextarea){
23 if ('string' == typeof elTextarea) elTextarea = $D.get(elTextarea);
24 this.textarea = elTextarea;
25 this.toolbarItems = {};
26 }
27
28
32 YAHOO.realazy.RTE.htmlContent = ['<!DOCTYPE HTML PUBLIC "-/','/W3C/','/DTD HTML 4.01/','/EN" "http:/','/www.w3.org/TR/html4/strict.dtd"><html><head><title>编辑页面</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>{CONTENT}</body></html>'].join('');
33
34
35 YAHOO.realazy.RTE.prototype = {
36
37
40 render: function(){
41 42 this._createIframeAndHideTextarea();
43 44 this._turnOnDesignMode();
45 },
46
50 _createIframeAndHideTextarea: function(){
51 var iframe = document.createElement('iframe');
52 $D.addClass(iframe, 'rte-iframe');
53 this.textarea.style.display = 'none';
54 $D.insertBefore(iframe, this.textarea);
55 this.iframe = iframe;
56 },
57
61 _getWin: function(){
62 return this.iframe.contentWindow;
63 },
64
65
69 _getDoc: function(){
70 return this._getWin().document;
71 },
72
73
77 _turnOnDesignMode: function(){
78 try {
79 this._getDoc().open();
80 this._getDoc().write(YAHOO.realazy.RTE.htmlContent.replace(/{CONTENT}/, this.textarea.value));
81 this._getDoc().close();
82 this._getDoc().designMode = 'on';
83 } catch(ex) {
84 setTimeout(arguments.callee, 0);
85 return;
86 }
87 }
88 }
89 })();