plugin.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 getKeyboardSpaces = function (editor) {
  13. var spaces = editor.getParam('nonbreaking_force_tab', 0);
  14. if (typeof spaces === 'boolean') {
  15. return spaces === true ? 3 : 0;
  16. } else {
  17. return spaces;
  18. }
  19. };
  20. var wrapNbsps = function (editor) {
  21. return editor.getParam('nonbreaking_wrap', true, 'boolean');
  22. };
  23. var Settings = {
  24. getKeyboardSpaces: getKeyboardSpaces,
  25. wrapNbsps: wrapNbsps
  26. };
  27. var stringRepeat = function (string, repeats) {
  28. var str = '';
  29. for (var index = 0; index < repeats; index++) {
  30. str += string;
  31. }
  32. return str;
  33. };
  34. var isVisualCharsEnabled = function (editor) {
  35. return editor.plugins.visualchars ? editor.plugins.visualchars.isEnabled() : false;
  36. };
  37. var insertNbsp = function (editor, times) {
  38. var classes = function () {
  39. return isVisualCharsEnabled(editor) ? 'mce-nbsp-wrap mce-nbsp' : 'mce-nbsp-wrap';
  40. };
  41. var nbspSpan = function () {
  42. return '<span class="' + classes() + '" contenteditable="false">' + stringRepeat('&nbsp;', times) + '</span>';
  43. };
  44. var shouldWrap = Settings.wrapNbsps(editor);
  45. var html = shouldWrap || editor.plugins.visualchars ? nbspSpan() : stringRepeat('&nbsp;', times);
  46. editor.undoManager.transact(function () {
  47. return editor.insertContent(html);
  48. });
  49. };
  50. var Actions = { insertNbsp: insertNbsp };
  51. var register = function (editor) {
  52. editor.addCommand('mceNonBreaking', function () {
  53. Actions.insertNbsp(editor, 1);
  54. });
  55. };
  56. var Commands = { register: register };
  57. var global$1 = tinymce.util.Tools.resolve('tinymce.util.VK');
  58. var setup = function (editor) {
  59. var spaces = Settings.getKeyboardSpaces(editor);
  60. if (spaces > 0) {
  61. editor.on('keydown', function (e) {
  62. if (e.keyCode === global$1.TAB && !e.isDefaultPrevented()) {
  63. if (e.shiftKey) {
  64. return;
  65. }
  66. e.preventDefault();
  67. e.stopImmediatePropagation();
  68. Actions.insertNbsp(editor, spaces);
  69. }
  70. });
  71. }
  72. };
  73. var Keyboard = { setup: setup };
  74. var register$1 = function (editor) {
  75. editor.ui.registry.addButton('nonbreaking', {
  76. icon: 'non-breaking',
  77. tooltip: 'Nonbreaking space',
  78. onAction: function () {
  79. return editor.execCommand('mceNonBreaking');
  80. }
  81. });
  82. editor.ui.registry.addMenuItem('nonbreaking', {
  83. icon: 'non-breaking',
  84. text: 'Nonbreaking space',
  85. onAction: function () {
  86. return editor.execCommand('mceNonBreaking');
  87. }
  88. });
  89. };
  90. var Buttons = { register: register$1 };
  91. function Plugin () {
  92. global.add('nonbreaking', function (editor) {
  93. Commands.register(editor);
  94. Buttons.register(editor);
  95. Keyboard.setup(editor);
  96. });
  97. }
  98. Plugin();
  99. }());