plugin.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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$1 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global = tinymce.util.Tools.resolve('tinymce.Env');
  13. var getSeparatorHtml = function (editor) {
  14. return editor.getParam('pagebreak_separator', '<!-- pagebreak -->');
  15. };
  16. var shouldSplitBlock = function (editor) {
  17. return editor.getParam('pagebreak_split_block', false);
  18. };
  19. var pageBreakClass = 'mce-pagebreak';
  20. var getPlaceholderHtml = function (shouldSplitBlock) {
  21. var html = '<img src="' + global.transparentSrc + '" class="' + pageBreakClass + '" data-mce-resize="false" data-mce-placeholder />';
  22. return shouldSplitBlock ? '<p>' + html + '</p>' : html;
  23. };
  24. var setup$1 = function (editor) {
  25. var separatorHtml = getSeparatorHtml(editor);
  26. var shouldSplitBlock$1 = function () {
  27. return shouldSplitBlock(editor);
  28. };
  29. var pageBreakSeparatorRegExp = new RegExp(separatorHtml.replace(/[\?\.\*\[\]\(\)\{\}\+\^\$\:]/g, function (a) {
  30. return '\\' + a;
  31. }), 'gi');
  32. editor.on('BeforeSetContent', function (e) {
  33. e.content = e.content.replace(pageBreakSeparatorRegExp, getPlaceholderHtml(shouldSplitBlock$1()));
  34. });
  35. editor.on('PreInit', function () {
  36. editor.serializer.addNodeFilter('img', function (nodes) {
  37. var i = nodes.length, node, className;
  38. while (i--) {
  39. node = nodes[i];
  40. className = node.attr('class');
  41. if (className && className.indexOf(pageBreakClass) !== -1) {
  42. var parentNode = node.parent;
  43. if (editor.schema.getBlockElements()[parentNode.name] && shouldSplitBlock$1()) {
  44. parentNode.type = 3;
  45. parentNode.value = separatorHtml;
  46. parentNode.raw = true;
  47. node.remove();
  48. continue;
  49. }
  50. node.type = 3;
  51. node.value = separatorHtml;
  52. node.raw = true;
  53. }
  54. }
  55. });
  56. });
  57. };
  58. var register$1 = function (editor) {
  59. editor.addCommand('mcePageBreak', function () {
  60. editor.insertContent(getPlaceholderHtml(shouldSplitBlock(editor)));
  61. });
  62. };
  63. var setup = function (editor) {
  64. editor.on('ResolveName', function (e) {
  65. if (e.target.nodeName === 'IMG' && editor.dom.hasClass(e.target, pageBreakClass)) {
  66. e.name = 'pagebreak';
  67. }
  68. });
  69. };
  70. var register = function (editor) {
  71. var onAction = function () {
  72. return editor.execCommand('mcePageBreak');
  73. };
  74. editor.ui.registry.addButton('pagebreak', {
  75. icon: 'page-break',
  76. tooltip: 'Page break',
  77. onAction: onAction
  78. });
  79. editor.ui.registry.addMenuItem('pagebreak', {
  80. text: 'Page break',
  81. icon: 'page-break',
  82. onAction: onAction
  83. });
  84. };
  85. function Plugin () {
  86. global$1.add('pagebreak', function (editor) {
  87. register$1(editor);
  88. register(editor);
  89. setup$1(editor);
  90. setup(editor);
  91. });
  92. }
  93. Plugin();
  94. }());