plugin.js 3.3 KB

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