plugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * TinyMCE version 6.8.6 (TBD)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$1 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  7. const isSimpleType = type => value => typeof value === type;
  8. const isBoolean = isSimpleType('boolean');
  9. const isNumber = isSimpleType('number');
  10. const option = name => editor => editor.options.get(name);
  11. const register$2 = editor => {
  12. const registerOption = editor.options.register;
  13. registerOption('nonbreaking_force_tab', {
  14. processor: value => {
  15. if (isBoolean(value)) {
  16. return {
  17. value: value ? 3 : 0,
  18. valid: true
  19. };
  20. } else if (isNumber(value)) {
  21. return {
  22. value,
  23. valid: true
  24. };
  25. } else {
  26. return {
  27. valid: false,
  28. message: 'Must be a boolean or number.'
  29. };
  30. }
  31. },
  32. default: false
  33. });
  34. registerOption('nonbreaking_wrap', {
  35. processor: 'boolean',
  36. default: true
  37. });
  38. };
  39. const getKeyboardSpaces = option('nonbreaking_force_tab');
  40. const wrapNbsps = option('nonbreaking_wrap');
  41. const stringRepeat = (string, repeats) => {
  42. let str = '';
  43. for (let index = 0; index < repeats; index++) {
  44. str += string;
  45. }
  46. return str;
  47. };
  48. const isVisualCharsEnabled = editor => editor.plugins.visualchars ? editor.plugins.visualchars.isEnabled() : false;
  49. const insertNbsp = (editor, times) => {
  50. const classes = () => isVisualCharsEnabled(editor) ? 'mce-nbsp-wrap mce-nbsp' : 'mce-nbsp-wrap';
  51. const nbspSpan = () => `<span class="${ classes() }" contenteditable="false">${ stringRepeat('&nbsp;', times) }</span>`;
  52. const shouldWrap = wrapNbsps(editor);
  53. const html = shouldWrap || editor.plugins.visualchars ? nbspSpan() : stringRepeat('&nbsp;', times);
  54. editor.undoManager.transact(() => editor.insertContent(html));
  55. };
  56. const register$1 = editor => {
  57. editor.addCommand('mceNonBreaking', () => {
  58. insertNbsp(editor, 1);
  59. });
  60. };
  61. var global = tinymce.util.Tools.resolve('tinymce.util.VK');
  62. const setup = editor => {
  63. const spaces = getKeyboardSpaces(editor);
  64. if (spaces > 0) {
  65. editor.on('keydown', e => {
  66. if (e.keyCode === global.TAB && !e.isDefaultPrevented()) {
  67. if (e.shiftKey) {
  68. return;
  69. }
  70. e.preventDefault();
  71. e.stopImmediatePropagation();
  72. insertNbsp(editor, spaces);
  73. }
  74. });
  75. }
  76. };
  77. const onSetupEditable = editor => api => {
  78. const nodeChanged = () => {
  79. api.setEnabled(editor.selection.isEditable());
  80. };
  81. editor.on('NodeChange', nodeChanged);
  82. nodeChanged();
  83. return () => {
  84. editor.off('NodeChange', nodeChanged);
  85. };
  86. };
  87. const register = editor => {
  88. const onAction = () => editor.execCommand('mceNonBreaking');
  89. editor.ui.registry.addButton('nonbreaking', {
  90. icon: 'non-breaking',
  91. tooltip: 'Nonbreaking space',
  92. onAction,
  93. onSetup: onSetupEditable(editor)
  94. });
  95. editor.ui.registry.addMenuItem('nonbreaking', {
  96. icon: 'non-breaking',
  97. text: 'Nonbreaking space',
  98. onAction,
  99. onSetup: onSetupEditable(editor)
  100. });
  101. };
  102. var Plugin = () => {
  103. global$1.add('nonbreaking', editor => {
  104. register$2(editor);
  105. register$1(editor);
  106. register(editor);
  107. setup(editor);
  108. });
  109. };
  110. Plugin();
  111. })();