| 1 | /*global tinymce:true */ |
| 2 | |
| 3 | tinymce.PluginManager.add('code', function(editor) { |
| 4 | function showDialog() { |
| 5 | if (editor.selection.isCollapsed()) { |
| 6 | editor.windowManager.open({ |
| 7 | title: "Source code", |
| 8 | body: { |
| 9 | type: 'textbox', |
| 10 | name: 'code', |
| 11 | multiline: true, |
| 12 | minWidth: editor.getParam("code_dialog_width", 600), |
| 13 | minHeight: editor.getParam("code_dialog_height", Math.min(tinymce.DOM.getViewPort().h - 200, 500)), |
| 14 | value: "", |
| 15 | spellcheck: false, |
| 16 | style: 'direction: ltr; text-align: left' |
| 17 | }, |
| 18 | onSubmit: function(e) { |
| 19 | // We get a lovely "Wrong document" error in IE 11 if we |
| 20 | // don't move the focus to the editor before creating an undo |
| 21 | // transation since it tries to make a bookmark for the current selection |
| 22 | editor.focus(); |
| 23 | |
| 24 | editor.undoManager.transact(function() { |
| 25 | var obj = document.createElement('pre'); |
| 26 | if (typeof obj.textContent != 'undefined') { |
| 27 | obj.textContent = e.data.code; |
| 28 | } else { |
| 29 | obj.innerText = e.data.code; |
| 30 | } |
| 31 | editor.insertContent('<pre>' + escapeHtml(e.data.code) + '</pre>'); |
| 32 | }); |
| 33 | |
| 34 | editor.selection.setCursorLocation(); |
| 35 | editor.nodeChanged(); |
| 36 | } |
| 37 | }); |
| 38 | } else { |
| 39 | editor.undoManager.transact(function() { |
| 40 | tinymce.activeEditor.selection.setNode(tinymce.activeEditor.dom.create( |
| 41 | 'code', |
| 42 | {}, |
| 43 | editor.selection.getContent() |
| 44 | )); |
| 45 | }); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function escapeHtml(str) { |
| 50 | var div = document.createElement('div'); |
| 51 | div.appendChild(document.createTextNode(str)); |
| 52 | return div.innerHTML; |
| 53 | }; |
| 54 | |
| 55 | editor.addCommand("mceCodeEditor", showDialog); |
| 56 | |
| 57 | editor.addButton('code', { |
| 58 | icon: 'code', |
| 59 | tooltip: 'Source code', |
| 60 | onclick: showDialog |
| 61 | }); |
| 62 | |
| 63 | editor.addMenuItem('code', { |
| 64 | icon: 'code', |
| 65 | text: 'Source code', |
| 66 | context: 'tools', |
| 67 | onclick: showDialog |
| 68 | }); |
| 69 | }); |