plugin.js 3.8 KB

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