1 /** |
1 /** |
2 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $ |
2 * $Id: editor_plugin_src.js 372 2007-11-11 18:38:50Z spocke $ |
3 * |
3 * |
4 * @author Moxiecode |
4 * @author Moxiecode |
5 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved. |
5 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved. |
6 */ |
6 */ |
7 |
7 |
8 tinyMCE.importPluginLanguagePack('searchreplace'); |
8 (function() { |
|
9 tinymce.create('tinymce.plugins.SearchReplacePlugin', { |
|
10 init : function(ed, url) { |
|
11 function open(m) { |
|
12 ed.windowManager.open({ |
|
13 file : url + '/searchreplace.htm', |
|
14 width : 400 + ed.getLang('searchreplace.delta_width', 0), |
|
15 height : 160 + ed.getLang('searchreplace.delta_height', 0), |
|
16 inline : 1 |
|
17 }, { |
|
18 mode : m, |
|
19 search_string : ed.selection.getContent({format : 'text'}), |
|
20 plugin_url : url |
|
21 }); |
|
22 }; |
9 |
23 |
10 var TinyMCE_SearchReplacePlugin = { |
24 // Register commands |
11 getInfo : function() { |
25 ed.addCommand('mceSearch', function() { |
12 return { |
26 open('search'); |
13 longname : 'Search/Replace', |
27 }); |
14 author : 'Moxiecode Systems AB', |
|
15 authorurl : 'http://tinymce.moxiecode.com', |
|
16 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/searchreplace', |
|
17 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion |
|
18 }; |
|
19 }, |
|
20 |
28 |
21 initInstance : function (inst) { |
29 ed.addCommand('mceReplace', function() { |
22 inst.addShortcut('ctrl', 'f', 'lang_searchreplace_search_desc', 'mceSearch', true); |
30 open('replace'); |
23 // No CTRL+R for "replace" because browsers will reload page instead of executing plugin |
31 }); |
24 }, |
|
25 |
32 |
26 getControlHTML : function (cn) { |
33 // Register buttons |
27 switch (cn) { |
34 ed.addButton('search', {title : 'searchreplace.search_desc', cmd : 'mceSearch'}); |
28 case "search" : |
35 ed.addButton('replace', {title : 'searchreplace.replace_desc', cmd : 'mceReplace'}); |
29 return tinyMCE.getButtonHTML(cn, 'lang_searchreplace_search_desc', '{$pluginurl}/images/search.gif','mceSearch', true); |
|
30 |
36 |
31 case "replace" : |
37 ed.addShortcut('ctrl+f', 'searchreplace.search_desc', 'mceSearch'); |
32 return tinyMCE.getButtonHTML(cn, 'lang_searchreplace_replace_desc', '{$pluginurl}/images/replace.gif', 'mceSearchReplace', true); |
38 }, |
|
39 |
|
40 getInfo : function() { |
|
41 return { |
|
42 longname : 'Search/Replace', |
|
43 author : 'Moxiecode Systems AB', |
|
44 authorurl : 'http://tinymce.moxiecode.com', |
|
45 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/searchreplace', |
|
46 version : tinymce.majorVersion + "." + tinymce.minorVersion |
|
47 }; |
33 } |
48 } |
|
49 }); |
34 |
50 |
35 return ""; |
51 // Register plugin |
36 }, |
52 tinymce.PluginManager.add('searchreplace', tinymce.plugins.SearchReplacePlugin); |
37 |
53 })(); |
38 execCommand : function (editor_id, element, command, user_interface, value) { |
|
39 var inst = tinyMCE.getInstanceById(editor_id), selectedText = inst.selection.getSelectedText(), rng; |
|
40 |
|
41 function defValue(key, default_value) { |
|
42 value[key] = typeof(value[key]) == "undefined" ? default_value : value[key]; |
|
43 } |
|
44 |
|
45 function replaceSel(search_str, str, back) { |
|
46 if (!inst.selection.isCollapsed()) { |
|
47 if (tinyMCE.isRealIE) |
|
48 inst.selection.getRng().duplicate().pasteHTML(str); // Needs to be duplicated due to selection bug in IE |
|
49 else |
|
50 inst.execCommand('mceInsertContent', false, str); |
|
51 } |
|
52 } |
|
53 |
|
54 if (!value) |
|
55 value = []; |
|
56 |
|
57 defValue("editor_id", editor_id); |
|
58 defValue("searchstring", selectedText); |
|
59 defValue("replacestring", null); |
|
60 defValue("replacemode", "none"); |
|
61 defValue("casesensitive", false); |
|
62 defValue("backwards", false); |
|
63 defValue("wrap", false); |
|
64 defValue("wholeword", false); |
|
65 defValue("inline", "yes"); |
|
66 defValue("resizable", "no"); |
|
67 |
|
68 switch (command) { |
|
69 case "mceSearch" : |
|
70 if (user_interface) { |
|
71 var template = new Array(); |
|
72 |
|
73 template['file'] = '../../plugins/searchreplace/searchreplace.htm'; |
|
74 template['width'] = 380; |
|
75 template['height'] = 155 + (tinyMCE.isNS7 ? 20 : 0) + (tinyMCE.isMSIE ? 15 : 0); |
|
76 template['width'] += tinyMCE.getLang('lang_searchreplace_delta_width', 0); |
|
77 template['height'] += tinyMCE.getLang('lang_searchreplace_delta_height', 0); |
|
78 |
|
79 inst.selection.collapse(true); |
|
80 |
|
81 tinyMCE.openWindow(template, value); |
|
82 } else { |
|
83 var win = tinyMCE.getInstanceById(editor_id).contentWindow; |
|
84 var doc = tinyMCE.getInstanceById(editor_id).contentWindow.document; |
|
85 var body = tinyMCE.getInstanceById(editor_id).contentWindow.document.body; |
|
86 var awin = value.win, found; |
|
87 |
|
88 if (body.innerHTML == "") { |
|
89 awin.alert(tinyMCE.getLang('lang_searchreplace_notfound')); |
|
90 return true; |
|
91 } |
|
92 |
|
93 if (value['replacemode'] == "current") { |
|
94 replaceSel(value['string'], value['replacestring'], value['backwards']); |
|
95 value['replacemode'] = "none"; |
|
96 //tinyMCE.execInstanceCommand(editor_id, 'mceSearch', user_interface, value); |
|
97 //return true; |
|
98 } |
|
99 |
|
100 inst.selection.collapse(value['backwards']); |
|
101 |
|
102 if (tinyMCE.isMSIE) { |
|
103 var rng = inst.selection.getRng(); |
|
104 var flags = 0; |
|
105 if (value['wholeword']) |
|
106 flags = flags | 2; |
|
107 |
|
108 if (value['casesensitive']) |
|
109 flags = flags | 4; |
|
110 |
|
111 if (!rng.findText) { |
|
112 awin.alert('This operation is currently not supported by this browser.'); |
|
113 return true; |
|
114 } |
|
115 |
|
116 if (value['replacemode'] == "all") { |
|
117 found = false; |
|
118 |
|
119 while (rng.findText(value['string'], value['backwards'] ? -1 : 1, flags)) { |
|
120 found = true; |
|
121 rng.scrollIntoView(); |
|
122 rng.select(); |
|
123 replaceSel(value['string'], value['replacestring'], value['backwards']); |
|
124 } |
|
125 |
|
126 if (found) |
|
127 awin.alert(tinyMCE.getLang('lang_searchreplace_allreplaced')); |
|
128 else |
|
129 awin.alert(tinyMCE.getLang('lang_searchreplace_notfound')); |
|
130 |
|
131 return true; |
|
132 } |
|
133 |
|
134 if (rng.findText(value['string'], value['backwards'] ? -1 : 1, flags)) { |
|
135 rng.scrollIntoView(); |
|
136 rng.select(); |
|
137 } else |
|
138 awin.alert(tinyMCE.getLang('lang_searchreplace_notfound')); |
|
139 } else { |
|
140 if (value['replacemode'] == "all") { |
|
141 found = false; |
|
142 |
|
143 while (win.find(value['string'], value['casesensitive'], value['backwards'], value['wrap'], value['wholeword'], false, false)) { |
|
144 found = true; |
|
145 replaceSel(value['string'], value['replacestring'], value['backwards']); |
|
146 } |
|
147 |
|
148 if (found) |
|
149 awin.alert(tinyMCE.getLang('lang_searchreplace_allreplaced')); |
|
150 else |
|
151 awin.alert(tinyMCE.getLang('lang_searchreplace_notfound')); |
|
152 |
|
153 return true; |
|
154 } |
|
155 |
|
156 if (!win.find(value['string'], value['casesensitive'], value['backwards'], value['wrap'], value['wholeword'], false, false)) |
|
157 awin.alert(tinyMCE.getLang('lang_searchreplace_notfound')); |
|
158 } |
|
159 } |
|
160 |
|
161 return true; |
|
162 |
|
163 case "mceSearchReplace" : |
|
164 value['replacestring'] = ""; |
|
165 tinyMCE.execInstanceCommand(editor_id, 'mceSearch', user_interface, value, false); |
|
166 return true; |
|
167 } |
|
168 |
|
169 return false; |
|
170 } |
|
171 }; |
|
172 |
|
173 tinyMCE.addPlugin("searchreplace", TinyMCE_SearchReplacePlugin); |
|