utils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. function mapTags(tag) {
  2. let ret = ''
  3. switch (tag) {
  4. case 'A':
  5. ret = 'Link'
  6. break
  7. case 'BUTTON':
  8. ret = 'Button'
  9. break
  10. case 'IMG':
  11. ret = 'Image'
  12. break
  13. case 'INPUT':
  14. ret = 'Textbox'
  15. break
  16. case 'TEXTAREA':
  17. ret = 'Textbox'
  18. break
  19. default:
  20. ret = ''
  21. // ret = 'Text'
  22. break
  23. }
  24. return ret
  25. }
  26. function extractTextForFocus(e) {
  27. let meaningfulNode = e.target
  28. // 如果天然能focus,但没有被加上tabindex属性,比如focus到了第三方组件内部的可focus元素,直接返回。
  29. if (
  30. ['A', 'AREA', 'BUTTON', 'INPUT', 'SELECT', 'IFRAME'].includes(meaningfulNode.tagName) &&
  31. !meaningfulNode.getAttribute('tabindex')
  32. ) {
  33. return
  34. }
  35. while (!meaningfulNode.getAttribute || !meaningfulNode.getAttribute('tabindex')) {
  36. meaningfulNode = meaningfulNode.parentNode
  37. if (!meaningfulNode) {
  38. return
  39. }
  40. }
  41. // 约定:tabindex属性值为-1的元素只用于在点击有data-aria-xxx-area attribute的区域包裹元素的子孙元素时,避免focus到区域包裹元素。
  42. if (meaningfulNode.getAttribute('tabindex') === '-1') {
  43. return
  44. }
  45. let elemType = ''
  46. const ariaLabel = meaningfulNode.getAttribute('aria-label')
  47. if (ariaLabel !== null) {
  48. elemType = ariaLabel
  49. } else {
  50. elemType = mapTags(meaningfulNode.tagName)
  51. }
  52. let elemDisc = ''
  53. const ariaDescription = meaningfulNode.getAttribute('aria-description')
  54. if (ariaDescription !== null) {
  55. elemDisc = ariaDescription
  56. } else {
  57. elemDisc = meaningfulNode.innerText
  58. }
  59. return {
  60. elemType,
  61. elemDisc,
  62. ariaNode: meaningfulNode,
  63. }
  64. }
  65. let lastMeaningfulNode = null
  66. function extractTextForMouseOver(e) {
  67. let meaningfulNode = e.target
  68. while (!meaningfulNode.getAttribute || !meaningfulNode.getAttribute('tabindex')) {
  69. meaningfulNode = meaningfulNode.parentNode
  70. if (!meaningfulNode) {
  71. return
  72. }
  73. }
  74. if (meaningfulNode.getAttribute('tabindex') === '-1') {
  75. return
  76. }
  77. // mouseover事件冒泡到有data-aria-xxx-area attribute的区域包裹元素时,不应该提取该区域包裹元素的无障碍辅助信息。
  78. if (
  79. meaningfulNode.getAttribute('data-aria-navigation-area') !== null ||
  80. meaningfulNode.getAttribute('data-aria-viewport-area') !== null ||
  81. meaningfulNode.getAttribute('data-aria-interaction-area') !== null
  82. ) {
  83. return
  84. }
  85. // 如果只是在需要朗读的子元素之间进进出出,第一次需要朗读,以后就不需要朗读了。
  86. let relatedMeaningfulNode = e.relatedTarget
  87. while (relatedMeaningfulNode && (!relatedMeaningfulNode.getAttribute || !relatedMeaningfulNode.getAttribute('tabindex'))) {
  88. relatedMeaningfulNode = relatedMeaningfulNode.parentNode
  89. }
  90. if (relatedMeaningfulNode === meaningfulNode && lastMeaningfulNode === meaningfulNode) {
  91. return
  92. }
  93. lastMeaningfulNode = meaningfulNode
  94. let elemType = ''
  95. const ariaLabel = meaningfulNode.getAttribute('aria-label')
  96. if (ariaLabel !== null) {
  97. elemType = ariaLabel
  98. } else {
  99. elemType = mapTags(meaningfulNode.tagName)
  100. }
  101. let elemDisc = ''
  102. const ariaDescription = meaningfulNode.getAttribute('aria-description')
  103. if (ariaDescription !== null) {
  104. elemDisc = ariaDescription
  105. } else {
  106. elemDisc = meaningfulNode.innerText
  107. }
  108. return {
  109. elemType,
  110. elemDisc,
  111. ariaNode: meaningfulNode,
  112. }
  113. }
  114. function isObject(p) {
  115. return Object.prototype.toString.call(p) === '[object Object]'
  116. }
  117. // 判断两个对象内容是否相同
  118. function isSameObject(object1, object2) {
  119. const keys1 = Object.keys(object1)
  120. const keys2 = Object.keys(object2)
  121. if (keys1.length !== keys2.length) {
  122. return false
  123. }
  124. for (let index = 0; index < keys1.length; index++) {
  125. const val1 = object1[keys1[index]]
  126. const val2 = object2[keys2[index]]
  127. const areObjects = isObject(val1) && isObject(val2)
  128. if (
  129. (areObjects && !isSameObject(val1, val2)) ||
  130. (!areObjects && (val1 !== val2))
  131. ) {
  132. return false
  133. }
  134. }
  135. return true
  136. }
  137. function getAndFocusNextNodeWithCustomAttribute(attriName) {
  138. const startNode = (document.activeElement || document.body)
  139. const treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT)
  140. treeWalker.currentNode = startNode
  141. let targetNode = null
  142. // eslint-disable-next-line
  143. while(true) {
  144. const nextNode = treeWalker.nextNode()
  145. if (!nextNode) {
  146. console.log('往下没找到')
  147. break
  148. }
  149. if (nextNode.dataset[attriName] !== undefined) {
  150. console.log('往下找到了')
  151. targetNode = nextNode
  152. break
  153. }
  154. }
  155. if (!targetNode && (startNode !== document.body)) {
  156. treeWalker.currentNode = document.body
  157. // eslint-disable-next-line
  158. while(true) {
  159. const nextNode = treeWalker.nextNode()
  160. if (!nextNode) {
  161. console.log('往上也没找到')
  162. break
  163. }
  164. if (nextNode.dataset[attriName] !== undefined) {
  165. console.log('往上找到了')
  166. targetNode = nextNode
  167. break
  168. }
  169. }
  170. }
  171. if (targetNode) {
  172. // 如果重复选中某一区域,需要重新触发focus,所以先blur一下
  173. if (document.activeElement === targetNode) {
  174. targetNode.blur()
  175. }
  176. targetNode.focus()
  177. // 如果无法focus,就强行focus
  178. if (document.activeElement !== targetNode) {
  179. targetNode.setAttribute('tabindex', '0')
  180. targetNode.focus()
  181. }
  182. }
  183. return targetNode
  184. }
  185. function __focusNextFocusableNode(treeWalker) {
  186. // eslint-disable-next-line
  187. while(true) {
  188. const nextNode = treeWalker.nextNode()
  189. if (!nextNode) {
  190. return false
  191. }
  192. if (nextNode.focus) {
  193. nextNode.focus()
  194. if (document.activeElement === nextNode && nextNode.tabIndex !== -1) {
  195. return true
  196. }
  197. }
  198. }
  199. }
  200. function iterateOnFocusableNode(startNode, focusedNodeHandler) {
  201. const treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT)
  202. treeWalker.currentNode = startNode
  203. treeWalker.currentNode.focus()
  204. if (document.activeElement === treeWalker.currentNode) {
  205. // console.log('起始节点可以focus')
  206. } else {
  207. // console.log('起始节点不可以focus,focus到下一节点。')
  208. const ret = __focusNextFocusableNode(treeWalker)
  209. if (!ret) {
  210. return
  211. }
  212. }
  213. const iterator = () => {
  214. focusedNodeHandler(treeWalker.currentNode).then(() => {
  215. const result = __focusNextFocusableNode(treeWalker)
  216. if (result) {
  217. // console.log('遍历到下一个节点!')
  218. iterator()
  219. } else {
  220. // console.log('遍历结束!')
  221. }
  222. }).catch((e) => {
  223. // console.log('遍历中止!', e)
  224. })
  225. }
  226. iterator()
  227. }
  228. /**
  229. * 返回一个自带消抖效果的函数,用res表示。
  230. *
  231. * fn: 需要被消抖的函数
  232. * delay: 消抖时长
  233. * isImmediateCall: 是否在一组操作中的第一次调用时立即执行fn
  234. * isRememberLastCall:是否在一组中最后一次调用后等delay时长再执行fn
  235. */
  236. function debounce(fn, delay, isImmediateCall = false, isRememberLastCall = true) {
  237. console.assert(isImmediateCall || isRememberLastCall, 'isImmediateCall 和 isRememberLastCall 至少应有一个是true,否则没有意义!')
  238. let timer = null
  239. // 上次调用的时刻
  240. let lastCallTime = 0
  241. if (isImmediateCall && !isRememberLastCall) {
  242. return function (...args) {
  243. const currentTime = Date.now()
  244. if (currentTime - lastCallTime >= delay) {
  245. fn.apply(this, args)
  246. }
  247. lastCallTime = currentTime
  248. }
  249. } else if (!isImmediateCall && isRememberLastCall) {
  250. return function (...args) {
  251. if (timer) {
  252. clearTimeout(timer)
  253. }
  254. timer = setTimeout(() => {
  255. fn.apply(this, args)
  256. }, delay)
  257. }
  258. } else if (isImmediateCall && isRememberLastCall) {
  259. return function (...args) {
  260. const currentTime = Date.now()
  261. if (currentTime - lastCallTime >= delay) { // 一组操作中的第一次
  262. fn.apply(this, args)
  263. lastCallTime = currentTime
  264. return
  265. } else { // 一组中的后续调用
  266. if (timer) { // 在此之前存在中间调用
  267. lastCallTime = currentTime
  268. clearTimeout(timer)
  269. }
  270. timer = setTimeout(() => {
  271. fn.apply(this, args)
  272. lastCallTime = 0
  273. timer = null
  274. }, delay)
  275. }
  276. }
  277. } else {
  278. console.error('不应该执行到这里!')
  279. }
  280. }
  281. class DebounceScheduler {
  282. constructor(fn, delay, context, isImmediateCall = false) {
  283. this.job = fn
  284. this.delay = delay
  285. this.context = context
  286. this.timer = null
  287. this.lastCallTime = 0
  288. this.isImmediateCall = isImmediateCall
  289. }
  290. planToDo(...args) {
  291. if (!this.isImmediateCall) {
  292. if (this.timer) {
  293. clearTimeout(this.timer)
  294. this.timer = null
  295. }
  296. this.timer = setTimeout(() => {
  297. this.job.apply(this.context, args)
  298. }, this.delay)
  299. } else {
  300. const currentTime = Date.now()
  301. if (currentTime - this.lastCallTime >= this.delay) { // 一组操作中的第一次
  302. this.job.apply(this.context, args)
  303. this.lastCallTime = currentTime
  304. return
  305. } else { // 一组中的后续调用
  306. if (this.timer) { // 在此之前存在中间调用
  307. this.lastCallTime = currentTime
  308. clearTimeout(this.timer)
  309. this.timer = null
  310. }
  311. this.timer = setTimeout(() => {
  312. this.job.apply(this.context, args)
  313. this.lastCallTime = 0
  314. this.timer = null
  315. }, this.delay)
  316. }
  317. }
  318. }
  319. cancel() {
  320. if (this.timer) {
  321. clearTimeout(this.timer)
  322. this.timer = null
  323. }
  324. }
  325. }
  326. export default {
  327. mapTags,
  328. extractTextForFocus,
  329. extractTextForMouseOver,
  330. isSameObject,
  331. getAndFocusNextNodeWithCustomAttribute,
  332. iterateOnFocusableNode,
  333. debounce,
  334. DebounceScheduler,
  335. }