plugin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.10.0 (2021-10-11)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global$2 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  13. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  14. var getContentStyle = function (editor) {
  15. return editor.getParam('content_style', '', 'string');
  16. };
  17. var shouldUseContentCssCors = function (editor) {
  18. return editor.getParam('content_css_cors', false, 'boolean');
  19. };
  20. var getBodyClassByHash = function (editor) {
  21. var bodyClass = editor.getParam('body_class', '', 'hash');
  22. return bodyClass[editor.id] || '';
  23. };
  24. var getBodyClass = function (editor) {
  25. var bodyClass = editor.getParam('body_class', '', 'string');
  26. if (bodyClass.indexOf('=') === -1) {
  27. return bodyClass;
  28. } else {
  29. return getBodyClassByHash(editor);
  30. }
  31. };
  32. var getBodyIdByHash = function (editor) {
  33. var bodyId = editor.getParam('body_id', '', 'hash');
  34. return bodyId[editor.id] || bodyId;
  35. };
  36. var getBodyId = function (editor) {
  37. var bodyId = editor.getParam('body_id', 'tinymce', 'string');
  38. if (bodyId.indexOf('=') === -1) {
  39. return bodyId;
  40. } else {
  41. return getBodyIdByHash(editor);
  42. }
  43. };
  44. var getPreviewHtml = function (editor) {
  45. var headHtml = '';
  46. var encode = editor.dom.encode;
  47. var contentStyle = getContentStyle(editor);
  48. headHtml += '<base href="' + encode(editor.documentBaseURI.getURI()) + '">';
  49. var cors = shouldUseContentCssCors(editor) ? ' crossorigin="anonymous"' : '';
  50. global.each(editor.contentCSS, function (url) {
  51. headHtml += '<link type="text/css" rel="stylesheet" href="' + encode(editor.documentBaseURI.toAbsolute(url)) + '"' + cors + '>';
  52. });
  53. if (contentStyle) {
  54. headHtml += '<style type="text/css">' + contentStyle + '</style>';
  55. }
  56. var bodyId = getBodyId(editor);
  57. var bodyClass = getBodyClass(editor);
  58. var isMetaKeyPressed = global$1.mac ? 'e.metaKey' : 'e.ctrlKey && !e.altKey';
  59. 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> ';
  60. var directionality = editor.getBody().dir;
  61. var dirAttr = directionality ? ' dir="' + encode(directionality) + '"' : '';
  62. var previewHtml = '<!DOCTYPE html>' + '<html>' + '<head>' + headHtml + '</head>' + '<body id="' + encode(bodyId) + '" class="mce-content-body ' + encode(bodyClass) + '"' + dirAttr + '>' + editor.getContent() + preventClicksOnLinksScript + '</body>' + '</html>';
  63. return previewHtml;
  64. };
  65. var open = function (editor) {
  66. var content = getPreviewHtml(editor);
  67. var dataApi = editor.windowManager.open({
  68. title: 'Preview',
  69. size: 'large',
  70. body: {
  71. type: 'panel',
  72. items: [{
  73. name: 'preview',
  74. type: 'iframe',
  75. sandboxed: true
  76. }]
  77. },
  78. buttons: [{
  79. type: 'cancel',
  80. name: 'close',
  81. text: 'Close',
  82. primary: true
  83. }],
  84. initialData: { preview: content }
  85. });
  86. dataApi.focus('close');
  87. };
  88. var register$1 = function (editor) {
  89. editor.addCommand('mcePreview', function () {
  90. open(editor);
  91. });
  92. };
  93. var register = function (editor) {
  94. var onAction = function () {
  95. return editor.execCommand('mcePreview');
  96. };
  97. editor.ui.registry.addButton('preview', {
  98. icon: 'preview',
  99. tooltip: 'Preview',
  100. onAction: onAction
  101. });
  102. editor.ui.registry.addMenuItem('preview', {
  103. icon: 'preview',
  104. text: 'Preview',
  105. onAction: onAction
  106. });
  107. };
  108. function Plugin () {
  109. global$2.add('preview', function (editor) {
  110. register$1(editor);
  111. register(editor);
  112. });
  113. }
  114. Plugin();
  115. }());