]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WysiwygEdit/htmlarea3.php
refactor WysiwygEdit
[SourceForge/phpwiki.git] / lib / WysiwygEdit / htmlarea3.php
1 <?php
2 /**
3  * requires installation into themes/default/htmlarea3/
4  * MSIE => 5.5,  Mozilla >= 1.3
5  *
6  * @package WysiwygEdit
7  * @author Reini Urban
8  */
9
10 require_once("lib/WysiwygEdit.php");
11
12 class WysiwygEdit_htmlarea3 extends WysiwygEdit {
13
14     function Head($name='edit[content]') {
15         global $WikiTheme;
16         $WikiTheme->addMoreAttr('body'," onload='initEditor()'");
17         //Todo: language selection from available lang/*.js files
18         return new RawXml('
19 <script type="text/javascript" src="'.DATA_PATH.'/themes/default/htmlarea3/htmlarea.js"></script>
20 <script type="text/javascript" src="'.DATA_PATH.'/themes/default/htmlarea3/lang/en.js"></script>
21 <script type="text/javascript" src="'.DATA_PATH.'/themes/default/htmlarea3/dialog.js"></script> 
22 <style type="text/css">
23 @import url('.DATA_PATH.'/themes/default/htmlarea3/htmlarea.css);
24 </style>
25 <script type="text/javascript">
26 _editor_url = "'.DATA_PATH.'/themes/default/htmlarea3/";
27 var editor = null;
28 function initEditor() {
29   editor = new HTMLArea("'.$name.'");
30
31   // comment the following two lines to see how customization works
32   editor.generate();
33   return false;
34   
35   // BEGIN: code that adds custom buttons
36   var cfg = editor.config; // this is the default configuration
37   function clickHandler(editor, buttonId) {
38     switch (buttonId) {
39       case "my-toc":
40         editor.insertHTML("<?plugin CreateToc ?>");
41         break;
42       case "my-date":
43         editor.insertHTML((new Date()).toString());
44         break;
45       case "my-bold-em":
46         editor.execCommand("bold");
47         editor.execCommand("italic");
48         break;
49       case "my-hilite":
50         editor.surroundHTML("<span class=\"hilite\">", "</span>");
51         break;
52     }
53   };
54   cfg.registerButton("my-toc",  "Insert TOC", _editor_url+"ed_custom.gif", false, clickHandler);
55   cfg.registerButton("my-date", "Insert date/time", _editor_url+"ed_custom.gif", false, clickHandler);
56   cfg.registerButton("my-bold-em", "Toggle bold/italic", _editor_url+"ed_custom.gif", false, clickHandler);
57   cfg.registerButton("my-hilite", "Hilite selection", _editor_url+"ed_custom.gif", false, clickHandler);
58   
59   cfg.registerButton("my-sample", "Class: sample", _editor_url+"ed_custom.gif", false,
60     function(editor) {
61       if (HTMLArea.is_ie) {
62         editor.insertHTML("<span class=\"sample\">&nbsp;&nbsp;</span>");
63         var r = editor._doc.selection.createRange();
64         r.move("character", -2);
65         r.moveEnd("character", 2);
66         r.select();
67       } else { // Gecko/W3C compliant
68         var n = editor._doc.createElement("span");
69         n.className = "sample";
70         editor.insertNodeAtSelection(n);
71         var sel = editor._iframe.contentWindow.getSelection();
72         sel.removeAllRanges();
73         var r = editor._doc.createRange();
74         r.setStart(n, 0);
75         r.setEnd(n, 0);
76         sel.addRange(r);
77       }
78     }
79   );
80   
81   //cfg.pageStyle = "body { background-color: #efd; } .hilite { background-color: yellow; } "+
82   //                ".sample { color: green; font-family: monospace; }";
83   // add the new button to the toolbar
84   //cfg.toolbar.push(["linebreak", "my-toc", "my-date", "my-bold-em", "my-hilite", "my-sample"]); 
85   // END: code that adds custom buttons
86
87   editor.generate();
88 }
89 function insertHTML() {
90   var html = prompt("Enter some HTML code here");
91   if (html) {
92     editor.insertHTML(html);
93   }
94 }
95 function highlight() {
96   editor.surroundHTML(\'<span style="background-color: yellow">\', \'</span>\');
97 }
98 </script>
99  ');
100     }
101
102     function Textarea($textarea,$wikitext,$name='edit[content]') {
103         $out = HTML($textarea,HTML::div(array("id"=>"editareawiki",'style'=>'display:none'),$wikitext),"\n");
104         //TODO: maybe some more custom links
105         return $out;
106     }
107 }
108
109 // re-use these classes for the regexp's.
110 // just output strings instead of XmlObjects
111 class Markup_html_br extends Markup_linebreak {
112     function markup ($match) {
113         return $match;
114     }
115 }
116
117 class Markup_html_simple_tag extends Markup_html_emphasis {
118     function markup ($match, $body) {
119         $tag = substr($match, 1, -1);
120         switch ($tag) {
121         case 'b':
122         case 'strong':
123             return "*".$body."*";
124         case 'big': return "<big>".$body."</big>";
125         case 'i':
126         case 'em':
127             return "_".$body."_";
128         }
129     }
130 }
131
132 //'<SPAN style="FONT-WEIGHT: bold">text</SPAN>' => '*text*'
133 class Markup_html_bold extends BalancedMarkup
134 {
135     var $_start_regexp = "<(?:span|SPAN) style=\"FONT-WEIGHT: bold\">";
136
137     function getEndRegexp ($match) {
138         return "<\\/" . substr($match, 1);
139     }
140     function markup ($match, $body) {
141         //Todo: convert style formatting to simplier nested <b><i> tags
142         return "*".$body."*";
143     }
144 }
145
146 class HtmlTransformer extends InlineTransformer
147 {
148     function HtmlTransformer () {
149         $this->InlineTransformer(array('escape',
150                                        'html_br','html_bold','html_simple_tag',
151                                        /*
152                                        'html_a','html_span','html_div',
153                                        'html_table','html_hr','html_pre',
154                                        'html_blockquote',
155                                        'html_indent','html_ol','html_li','html_ul','html_img',
156                                        */));
157     }
158 }
159
160 /*
161  $Log: not supported by cvs2svn $
162
163 */
164
165 // Local Variables:
166 // mode: php
167 // tab-width: 8
168 // c-basic-offset: 4
169 // c-hanging-comment-ender-p: nil
170 // indent-tabs-mode: nil
171 // End:
172 ?>