| 1 |
(function () { |
| 2 |
tinymce.create('tinymce.plugins.wpforo_pre_button', { |
| 3 |
init: function (ed, url) { |
| 4 |
function showDialog() { |
| 5 |
ed.windowManager.open({ |
| 6 |
title: "Code", |
| 7 |
body: { |
| 8 |
type: 'textbox', |
| 9 |
value: ed.selection.getContent({format: 'text'}), |
| 10 |
name: 'preformatted', |
| 11 |
multiline: true, |
| 12 |
minWidth: ed.getParam("code_dialog_width", 600), |
| 13 |
minHeight: ed.getParam("code_dialog_height", Math.min(tinymce.DOM.getViewPort().h - 200, 500)), |
| 14 |
spellcheck: false, |
| 15 |
style: 'direction: ltr; text-align: left' |
| 16 |
}, |
| 17 |
onSubmit: function (e) { |
| 18 |
ed.focus(); |
| 19 |
ed.undoManager.transact(function () { |
| 20 |
ed.insertContent('<pre contenteditable="false">' + escapeHtml(e.data.preformatted) + '</pre>'); |
| 21 |
}); |
| 22 |
ed.selection.setCursorLocation(); |
| 23 |
ed.nodeChanged(); |
| 24 |
} |
| 25 |
}); |
| 26 |
} |
| 27 |
function escapeHtml(str) { |
| 28 |
var div = document.createElement('div'); |
| 29 |
div.appendChild(document.createTextNode(str)); |
| 30 |
return div.innerHTML; |
| 31 |
} |
| 32 |
ed.addButton('pre', { |
| 33 |
title: 'Code', |
| 34 |
icon: 'code', |
| 35 |
onclick: showDialog |
| 36 |
}); |
| 37 |
|
| 38 |
ed.addShortcut('ctrl+0','Format Code', showDialog); |
| 39 |
|
| 40 |
ed.on('dblclick', function(e){ |
| 41 |
if( e.target.tagName === 'PRE' && 'false' === e.target.getAttribute('contenteditable') ){ |
| 42 |
showDialog(); |
| 43 |
} |
| 44 |
}); |
| 45 |
|
| 46 |
ed.on('Dirty ExecCommand KeyPress SetContent', function(e) { |
| 47 |
ed.dom.select('pre').forEach(function(pre){ |
| 48 |
pre.setAttribute('contenteditable', 'false'); |
| 49 |
}); |
| 50 |
}); |
| 51 |
|
| 52 |
} |
| 53 |
}); |
| 54 |
tinymce.PluginManager.add('wpforo_pre_button', tinymce.plugins.wpforo_pre_button); |
| 55 |
})(); |