| 1 | /*global tinymce:true */ |
| 2 | |
| 3 | tinymce.PluginManager.add('wpcode', 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: 'wpcode', |
| 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 | editor.insertContent('<pre>' + escapeHtml(e.data.wpcode) + '</pre>'); |
| 26 | }); |
| 27 | |
| 28 | editor.selection.setCursorLocation(); |
| 29 | editor.nodeChanged(); |
| 30 | } |
| 31 | }); |
| 32 | } else { |
| 33 | editor.undoManager.transact(function() { |
| 34 | tinymce.activeEditor.selection.setNode(tinymce.activeEditor.dom.create( |
| 35 | 'code', |
| 36 | {}, |
| 37 | editor.selection.getContent() |
| 38 | )); |
| 39 | }); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | function escapeHtml(str) { |
| 44 | var div = document.createElement('div'); |
| 45 | div.appendChild(document.createTextNode(str)); |
| 46 | return div.innerHTML; |
| 47 | }; |
| 48 | |
| 49 | editor.addCommand("mceCodeEditor", showDialog); |
| 50 | |
| 51 | editor.addButton('wpcode', { |
| 52 | icon: 'code', |
| 53 | tooltip: 'Source code', |
| 54 | onclick: showDialog |
| 55 | }); |
| 56 | |
| 57 | editor.addMenuItem('wpcode', { |
| 58 | icon: 'code', |
| 59 | text: 'Source code', |
| 60 | context: 'tools', |
| 61 | onclick: showDialog |
| 62 | }); |
| 63 | }); |
| 64 | /*global tinymce:true */ |
| 65 | |
| 66 | tinymce.PluginManager.add('wpcode', function(editor) { |
| 67 | function showDialog() { |
| 68 | if (editor.selection.isCollapsed()) { |
| 69 | editor.windowManager.open({ |
| 70 | title: "Source code", |
| 71 | body: { |
| 72 | type: 'textbox', |
| 73 | name: 'wpcode', |
| 74 | multiline: true, |
| 75 | minWidth: editor.getParam("code_dialog_width", 600), |
| 76 | minHeight: editor.getParam("code_dialog_height", Math.min(tinymce.DOM.getViewPort().h - 200, 500)), |
| 77 | value: "", |
| 78 | spellcheck: false, |
| 79 | style: 'direction: ltr; text-align: left' |
| 80 | }, |
| 81 | onSubmit: function(e) { |
| 82 | // We get a lovely "Wrong document" error in IE 11 if we |
| 83 | // don't move the focus to the editor before creating an undo |
| 84 | // transation since it tries to make a bookmark for the current selection |
| 85 | editor.focus(); |
| 86 | |
| 87 | editor.undoManager.transact(function() { |
| 88 | editor.insertContent('<pre>' + escapeHtml(e.data.wpcode) + '</pre>'); |
| 89 | }); |
| 90 | |
| 91 | editor.selection.setCursorLocation(); |
| 92 | editor.nodeChanged(); |
| 93 | } |
| 94 | }); |
| 95 | } else { |
| 96 | editor.undoManager.transact(function() { |
| 97 | tinymce.activeEditor.selection.setNode(tinymce.activeEditor.dom.create( |
| 98 | 'code', |
| 99 | {}, |
| 100 | editor.selection.getContent() |
| 101 | )); |
| 102 | }); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | function escapeHtml(str) { |
| 107 | var div = document.createElement('div'); |
| 108 | div.appendChild(document.createTextNode(str)); |
| 109 | return div.innerHTML; |
| 110 | }; |
| 111 | |
| 112 | editor.addCommand("mceCodeEditor", showDialog); |
| 113 | |
| 114 | editor.addButton('wpcode', { |
| 115 | icon: 'code', |
| 116 | tooltip: 'Source code', |
| 117 | onclick: showDialog |
| 118 | }); |
| 119 | |
| 120 | editor.addMenuItem('wpcode', { |
| 121 | icon: 'code', |
| 122 | text: 'Source code', |
| 123 | context: 'tools', |
| 124 | onclick: showDialog |
| 125 | }); |
| 126 | }); |