plugin.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Cell = function (initial) {
  12. var value = initial;
  13. var get = function () {
  14. return value;
  15. };
  16. var set = function (v) {
  17. value = v;
  18. };
  19. return {
  20. get: get,
  21. set: set
  22. };
  23. };
  24. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  25. var fireVisualBlocks = function (editor, state) {
  26. editor.fire('VisualBlocks', { state: state });
  27. };
  28. var toggleVisualBlocks = function (editor, pluginUrl, enabledState) {
  29. var dom = editor.dom;
  30. dom.toggleClass(editor.getBody(), 'mce-visualblocks');
  31. enabledState.set(!enabledState.get());
  32. fireVisualBlocks(editor, enabledState.get());
  33. };
  34. var register = function (editor, pluginUrl, enabledState) {
  35. editor.addCommand('mceVisualBlocks', function () {
  36. toggleVisualBlocks(editor, pluginUrl, enabledState);
  37. });
  38. };
  39. var isEnabledByDefault = function (editor) {
  40. return editor.getParam('visualblocks_default_state', false, 'boolean');
  41. };
  42. var setup = function (editor, pluginUrl, enabledState) {
  43. editor.on('PreviewFormats AfterPreviewFormats', function (e) {
  44. if (enabledState.get()) {
  45. editor.dom.toggleClass(editor.getBody(), 'mce-visualblocks', e.type === 'afterpreviewformats');
  46. }
  47. });
  48. editor.on('init', function () {
  49. if (isEnabledByDefault(editor)) {
  50. toggleVisualBlocks(editor, pluginUrl, enabledState);
  51. }
  52. });
  53. editor.on('remove', function () {
  54. editor.dom.removeClass(editor.getBody(), 'mce-visualblocks');
  55. });
  56. };
  57. var toggleActiveState = function (editor, enabledState) {
  58. return function (api) {
  59. api.setActive(enabledState.get());
  60. var editorEventCallback = function (e) {
  61. return api.setActive(e.state);
  62. };
  63. editor.on('VisualBlocks', editorEventCallback);
  64. return function () {
  65. return editor.off('VisualBlocks', editorEventCallback);
  66. };
  67. };
  68. };
  69. var register$1 = function (editor, enabledState) {
  70. editor.ui.registry.addToggleButton('visualblocks', {
  71. icon: 'visualblocks',
  72. tooltip: 'Show blocks',
  73. onAction: function () {
  74. return editor.execCommand('mceVisualBlocks');
  75. },
  76. onSetup: toggleActiveState(editor, enabledState)
  77. });
  78. editor.ui.registry.addToggleMenuItem('visualblocks', {
  79. text: 'Show blocks',
  80. icon: 'visualblocks',
  81. onAction: function () {
  82. return editor.execCommand('mceVisualBlocks');
  83. },
  84. onSetup: toggleActiveState(editor, enabledState)
  85. });
  86. };
  87. function Plugin () {
  88. global.add('visualblocks', function (editor, pluginUrl) {
  89. var enabledState = Cell(false);
  90. register(editor, pluginUrl, enabledState);
  91. register$1(editor, enabledState);
  92. setup(editor, pluginUrl, enabledState);
  93. });
  94. }
  95. Plugin();
  96. }());