plugin.js 3.8 KB

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