plugin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.9.1 (2021-08-27)
  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$1 = 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. };
  54. var toggleActiveState = function (editor, enabledState) {
  55. return function (api) {
  56. api.setActive(enabledState.get());
  57. var editorEventCallback = function (e) {
  58. return api.setActive(e.state);
  59. };
  60. editor.on('VisualBlocks', editorEventCallback);
  61. return function () {
  62. return editor.off('VisualBlocks', editorEventCallback);
  63. };
  64. };
  65. };
  66. var register = function (editor, enabledState) {
  67. var onAction = function () {
  68. return editor.execCommand('mceVisualBlocks');
  69. };
  70. editor.ui.registry.addToggleButton('visualblocks', {
  71. icon: 'visualblocks',
  72. tooltip: 'Show blocks',
  73. onAction: onAction,
  74. onSetup: toggleActiveState(editor, enabledState)
  75. });
  76. editor.ui.registry.addToggleMenuItem('visualblocks', {
  77. text: 'Show blocks',
  78. icon: 'visualblocks',
  79. onAction: onAction,
  80. onSetup: toggleActiveState(editor, enabledState)
  81. });
  82. };
  83. function Plugin () {
  84. global.add('visualblocks', function (editor, pluginUrl) {
  85. var enabledState = Cell(false);
  86. register$1(editor, pluginUrl, enabledState);
  87. register(editor, enabledState);
  88. setup(editor, pluginUrl, enabledState);
  89. });
  90. }
  91. Plugin();
  92. }());