plugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 (domGlobals) {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  13. var global$2 = tinymce.util.Tools.resolve('tinymce.EditorManager');
  14. var global$3 = tinymce.util.Tools.resolve('tinymce.Env');
  15. var global$4 = tinymce.util.Tools.resolve('tinymce.util.Delay');
  16. var global$5 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  17. var global$6 = tinymce.util.Tools.resolve('tinymce.util.VK');
  18. var getTabFocusElements = function (editor) {
  19. return editor.getParam('tabfocus_elements', ':prev,:next');
  20. };
  21. var getTabFocus = function (editor) {
  22. return editor.getParam('tab_focus', getTabFocusElements(editor));
  23. };
  24. var Settings = { getTabFocus: getTabFocus };
  25. var DOM = global$1.DOM;
  26. var tabCancel = function (e) {
  27. if (e.keyCode === global$6.TAB && !e.ctrlKey && !e.altKey && !e.metaKey) {
  28. e.preventDefault();
  29. }
  30. };
  31. var setup = function (editor) {
  32. function tabHandler(e) {
  33. var x, el, v, i;
  34. if (e.keyCode !== global$6.TAB || e.ctrlKey || e.altKey || e.metaKey || e.isDefaultPrevented()) {
  35. return;
  36. }
  37. function find(direction) {
  38. el = DOM.select(':input:enabled,*[tabindex]:not(iframe)');
  39. function canSelectRecursive(e) {
  40. return e.nodeName === 'BODY' || e.type !== 'hidden' && e.style.display !== 'none' && e.style.visibility !== 'hidden' && canSelectRecursive(e.parentNode);
  41. }
  42. function canSelect(el) {
  43. return /INPUT|TEXTAREA|BUTTON/.test(el.tagName) && global$2.get(e.id) && el.tabIndex !== -1 && canSelectRecursive(el);
  44. }
  45. global$5.each(el, function (e, i) {
  46. if (e.id === editor.id) {
  47. x = i;
  48. return false;
  49. }
  50. });
  51. if (direction > 0) {
  52. for (i = x + 1; i < el.length; i++) {
  53. if (canSelect(el[i])) {
  54. return el[i];
  55. }
  56. }
  57. } else {
  58. for (i = x - 1; i >= 0; i--) {
  59. if (canSelect(el[i])) {
  60. return el[i];
  61. }
  62. }
  63. }
  64. return null;
  65. }
  66. v = global$5.explode(Settings.getTabFocus(editor));
  67. if (v.length === 1) {
  68. v[1] = v[0];
  69. v[0] = ':prev';
  70. }
  71. if (e.shiftKey) {
  72. if (v[0] === ':prev') {
  73. el = find(-1);
  74. } else {
  75. el = DOM.get(v[0]);
  76. }
  77. } else {
  78. if (v[1] === ':next') {
  79. el = find(1);
  80. } else {
  81. el = DOM.get(v[1]);
  82. }
  83. }
  84. if (el) {
  85. var focusEditor = global$2.get(el.id || el.name);
  86. if (el.id && focusEditor) {
  87. focusEditor.focus();
  88. } else {
  89. global$4.setTimeout(function () {
  90. if (!global$3.webkit) {
  91. domGlobals.window.focus();
  92. }
  93. el.focus();
  94. }, 10);
  95. }
  96. e.preventDefault();
  97. }
  98. }
  99. editor.on('init', function () {
  100. if (editor.inline) {
  101. DOM.setAttrib(editor.getBody(), 'tabIndex', null);
  102. }
  103. editor.on('keyup', tabCancel);
  104. if (global$3.gecko) {
  105. editor.on('keypress keydown', tabHandler);
  106. } else {
  107. editor.on('keydown', tabHandler);
  108. }
  109. });
  110. };
  111. var Keyboard = { setup: setup };
  112. function Plugin () {
  113. global.add('tabfocus', function (editor) {
  114. Keyboard.setup(editor);
  115. });
  116. }
  117. Plugin();
  118. }(window));