plugin.js 2.9 KB

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