plugin.js 3.6 KB

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