plugin.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. (function () {
  2. var nonbreaking = (function () {
  3. 'use strict';
  4. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  5. var stringRepeat = function (string, repeats) {
  6. var str = '';
  7. for (var index = 0; index < repeats; index++) {
  8. str += string;
  9. }
  10. return str;
  11. };
  12. var isVisualCharsEnabled = function (editor) {
  13. return editor.plugins.visualchars ? editor.plugins.visualchars.isEnabled() : false;
  14. };
  15. var insertNbsp = function (editor, times) {
  16. var nbsp = isVisualCharsEnabled(editor) ? '<span class="mce-nbsp">&nbsp;</span>' : '&nbsp;';
  17. editor.insertContent(stringRepeat(nbsp, times));
  18. editor.dom.setAttrib(editor.dom.select('span.mce-nbsp'), 'data-mce-bogus', '1');
  19. };
  20. var Actions = { insertNbsp: insertNbsp };
  21. var register = function (editor) {
  22. editor.addCommand('mceNonBreaking', function () {
  23. Actions.insertNbsp(editor, 1);
  24. });
  25. };
  26. var Commands = { register: register };
  27. var global$1 = tinymce.util.Tools.resolve('tinymce.util.VK');
  28. var getKeyboardSpaces = function (editor) {
  29. var spaces = editor.getParam('nonbreaking_force_tab', 0);
  30. if (typeof spaces === 'boolean') {
  31. return spaces === true ? 3 : 0;
  32. } else {
  33. return spaces;
  34. }
  35. };
  36. var Settings = { getKeyboardSpaces: getKeyboardSpaces };
  37. var setup = function (editor) {
  38. var spaces = Settings.getKeyboardSpaces(editor);
  39. if (spaces > 0) {
  40. editor.on('keydown', function (e) {
  41. if (e.keyCode === global$1.TAB && !e.isDefaultPrevented()) {
  42. if (e.shiftKey) {
  43. return;
  44. }
  45. e.preventDefault();
  46. e.stopImmediatePropagation();
  47. Actions.insertNbsp(editor, spaces);
  48. }
  49. });
  50. }
  51. };
  52. var Keyboard = { setup: setup };
  53. var register$1 = function (editor) {
  54. editor.addButton('nonbreaking', {
  55. title: 'Nonbreaking space',
  56. cmd: 'mceNonBreaking'
  57. });
  58. editor.addMenuItem('nonbreaking', {
  59. icon: 'nonbreaking',
  60. text: 'Nonbreaking space',
  61. cmd: 'mceNonBreaking',
  62. context: 'insert'
  63. });
  64. };
  65. var Buttons = { register: register$1 };
  66. global.add('nonbreaking', function (editor) {
  67. Commands.register(editor);
  68. Buttons.register(editor);
  69. Keyboard.setup(editor);
  70. });
  71. function Plugin () {
  72. }
  73. return Plugin;
  74. }());
  75. })();