|
@@ -1,6 +1,7 @@
|
|
|
import { Debounce } from "@imengyu/imengyu-utils";
|
|
|
import { onBeforeUnmount, onMounted } from "vue";
|
|
|
import { useRouter } from "vue-router";
|
|
|
+import SpatialNavigation from 'spatial-navigation-js';
|
|
|
|
|
|
/**
|
|
|
* 安卓电视焦点控制帮助类
|
|
@@ -11,35 +12,19 @@ export function useTvFocusImprovement(fromApp = false) {
|
|
|
|
|
|
const router = useRouter()
|
|
|
if (fromApp) {
|
|
|
- router.afterEach( (to, from) => {
|
|
|
- setTimeout(() => {
|
|
|
- focusFirstElement();
|
|
|
- }, 1000);
|
|
|
+ router.afterEach((to, from) => {
|
|
|
+ setTimeout(initSpatialNavigation, 500);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- function focusFirstElement() {
|
|
|
- const focusableElements = getFocusableElement();
|
|
|
- if (focusableElements.length) {
|
|
|
- for (const element of focusableElements) {
|
|
|
- if (element.classList.contains('active')) {//优先激活选中条目
|
|
|
- element.focus();
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- focusableElements[0].focus();
|
|
|
- }
|
|
|
- }
|
|
|
- function getFocusableElement() {
|
|
|
- return document.querySelectorAll<
|
|
|
- | HTMLButtonElement
|
|
|
- | HTMLAnchorElement
|
|
|
- | HTMLInputElement
|
|
|
- | HTMLSelectElement
|
|
|
- | HTMLTextAreaElement
|
|
|
- >(
|
|
|
- 'a[href], button, input, select, textarea, [tabindex]:not([tabindex="-1"])'
|
|
|
- );
|
|
|
+ function initSpatialNavigation() {
|
|
|
+ SpatialNavigation.add({
|
|
|
+ selector: '.focusable, a[href], button, input, select, textarea, [tabindex]:not([tabindex="-1"])'
|
|
|
+ });
|
|
|
+ // Make the *currently existing* navigable elements focusable.
|
|
|
+ SpatialNavigation.makeFocusable();
|
|
|
+ // Focus the first navigable element.
|
|
|
+ SpatialNavigation.focus();
|
|
|
}
|
|
|
|
|
|
const backDebounce = new Debounce(1000, () => router.back());
|
|
@@ -74,17 +59,10 @@ export function useTvFocusImprovement(fromApp = false) {
|
|
|
|
|
|
onMounted(() => {
|
|
|
window.addEventListener('keyup', handleKeyUp);
|
|
|
- setTimeout(() => {
|
|
|
- focusFirstElement();
|
|
|
- }, 2000);
|
|
|
+ setTimeout(initSpatialNavigation, 2000);
|
|
|
})
|
|
|
onBeforeUnmount(() => {
|
|
|
window.removeEventListener('keyup', handleKeyUp);
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- return {
|
|
|
- focusFirstElement,
|
|
|
- getFocusableElement,
|
|
|
- }
|
|
|
}
|