plugin.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * plugin.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /*global tinymce:true */
  11. tinymce.PluginManager.add('preview', function(editor) {
  12. var settings = editor.settings, sandbox = !tinymce.Env.ie;
  13. editor.addCommand('mcePreview', function() {
  14. editor.windowManager.open({
  15. title: 'Preview',
  16. width : parseInt(editor.getParam("plugin_preview_width", "650"), 10),
  17. height : parseInt(editor.getParam("plugin_preview_height", "500"), 10),
  18. html: '<iframe src="javascript:\'\'" frameborder="0"' + (sandbox ? ' sandbox="allow-scripts"' : '') + '></iframe>',
  19. buttons: {
  20. text: 'Close',
  21. onclick: function() {
  22. this.parent().parent().close();
  23. }
  24. },
  25. onPostRender: function() {
  26. var previewHtml, headHtml = '';
  27. if (editor.settings.document_base_url != editor.documentBaseUrl) {
  28. headHtml += '<base href="' + editor.documentBaseURI.getURI() + '">';
  29. }
  30. tinymce.each(editor.contentCSS, function(url) {
  31. headHtml += '<link type="text/css" rel="stylesheet" href="' + editor.documentBaseURI.toAbsolute(url) + '">';
  32. });
  33. var bodyId = settings.body_id || 'tinymce';
  34. if (bodyId.indexOf('=') != -1) {
  35. bodyId = editor.getParam('body_id', '', 'hash');
  36. bodyId = bodyId[editor.id] || bodyId;
  37. }
  38. var bodyClass = settings.body_class || '';
  39. if (bodyClass.indexOf('=') != -1) {
  40. bodyClass = editor.getParam('body_class', '', 'hash');
  41. bodyClass = bodyClass[editor.id] || '';
  42. }
  43. var dirAttr = editor.settings.directionality ? ' dir="' + editor.settings.directionality + '"' : '';
  44. previewHtml = (
  45. '<!DOCTYPE html>' +
  46. '<html>' +
  47. '<head>' +
  48. headHtml +
  49. '</head>' +
  50. '<body id="' + bodyId + '" class="mce-content-body ' + bodyClass + '"' + dirAttr + '>' +
  51. editor.getContent() +
  52. '</body>' +
  53. '</html>'
  54. );
  55. if (!sandbox) {
  56. // IE 6-11 doesn't support data uris on iframes
  57. // so I guess they will have to be less secure since we can't sandbox on those
  58. // TODO: Use sandbox if future versions of IE supports iframes with data: uris.
  59. var doc = this.getEl('body').firstChild.contentWindow.document;
  60. doc.open();
  61. doc.write(previewHtml);
  62. doc.close();
  63. } else {
  64. this.getEl('body').firstChild.src = 'data:text/html;charset=utf-8,' + encodeURIComponent(previewHtml);
  65. }
  66. }
  67. });
  68. });
  69. editor.addButton('preview', {
  70. title : 'Preview',
  71. cmd : 'mcePreview'
  72. });
  73. editor.addMenuItem('preview', {
  74. text : 'Preview',
  75. cmd : 'mcePreview',
  76. context: 'view'
  77. });
  78. });