plugin.js 3.9 KB

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