plugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.3.0 (2020-05-21)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  13. var getContentStyle = function (editor) {
  14. return editor.getParam('content_style', '');
  15. };
  16. var shouldUseContentCssCors = function (editor) {
  17. return editor.getParam('content_css_cors', false, 'boolean');
  18. };
  19. var global$2 = tinymce.util.Tools.resolve('tinymce.Env');
  20. var getPreviewHtml = function (editor) {
  21. var headHtml = '';
  22. var encode = editor.dom.encode;
  23. var contentStyle = getContentStyle(editor);
  24. headHtml += '<base href="' + encode(editor.documentBaseURI.getURI()) + '">';
  25. if (contentStyle) {
  26. headHtml += '<style type="text/css">' + contentStyle + '</style>';
  27. }
  28. var cors = shouldUseContentCssCors(editor) ? ' crossorigin="anonymous"' : '';
  29. global$1.each(editor.contentCSS, function (url) {
  30. headHtml += '<link type="text/css" rel="stylesheet" href="' + encode(editor.documentBaseURI.toAbsolute(url)) + '"' + cors + '>';
  31. });
  32. var bodyId = editor.settings.body_id || 'tinymce';
  33. if (bodyId.indexOf('=') !== -1) {
  34. bodyId = editor.getParam('body_id', '', 'hash');
  35. bodyId = bodyId[editor.id] || bodyId;
  36. }
  37. var bodyClass = editor.settings.body_class || '';
  38. if (bodyClass.indexOf('=') !== -1) {
  39. bodyClass = editor.getParam('body_class', '', 'hash');
  40. bodyClass = bodyClass[editor.id] || '';
  41. }
  42. var isMetaKeyPressed = global$2.mac ? 'e.metaKey' : 'e.ctrlKey && !e.altKey';
  43. var preventClicksOnLinksScript = '<script>' + 'document.addEventListener && document.addEventListener("click", function(e) {' + 'for (var elm = e.target; elm; elm = elm.parentNode) {' + 'if (elm.nodeName === "A" && !(' + isMetaKeyPressed + ')) {' + 'e.preventDefault();' + '}' + '}' + '}, false);' + '</script> ';
  44. var directionality = editor.getBody().dir;
  45. var dirAttr = directionality ? ' dir="' + encode(directionality) + '"' : '';
  46. var previewHtml = '<!DOCTYPE html>' + '<html>' + '<head>' + headHtml + '</head>' + '<body id="' + encode(bodyId) + '" class="mce-content-body ' + encode(bodyClass) + '"' + dirAttr + '>' + editor.getContent() + preventClicksOnLinksScript + '</body>' + '</html>';
  47. return previewHtml;
  48. };
  49. var open = function (editor) {
  50. var content = getPreviewHtml(editor);
  51. var dataApi = editor.windowManager.open({
  52. title: 'Preview',
  53. size: 'large',
  54. body: {
  55. type: 'panel',
  56. items: [{
  57. name: 'preview',
  58. type: 'iframe',
  59. sandboxed: true
  60. }]
  61. },
  62. buttons: [{
  63. type: 'cancel',
  64. name: 'close',
  65. text: 'Close',
  66. primary: true
  67. }],
  68. initialData: { preview: content }
  69. });
  70. dataApi.focus('close');
  71. };
  72. var register = function (editor) {
  73. editor.addCommand('mcePreview', function () {
  74. open(editor);
  75. });
  76. };
  77. var register$1 = function (editor) {
  78. editor.ui.registry.addButton('preview', {
  79. icon: 'preview',
  80. tooltip: 'Preview',
  81. onAction: function () {
  82. return editor.execCommand('mcePreview');
  83. }
  84. });
  85. editor.ui.registry.addMenuItem('preview', {
  86. icon: 'preview',
  87. text: 'Preview',
  88. onAction: function () {
  89. return editor.execCommand('mcePreview');
  90. }
  91. });
  92. };
  93. function Plugin () {
  94. global.add('preview', function (editor) {
  95. register(editor);
  96. register$1(editor);
  97. });
  98. }
  99. Plugin();
  100. }());