一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
46 行
1.5 KiB
TypeScript
46 行
1.5 KiB
TypeScript
import { focus } from '@joplin/lib/utils/focusHandler';
|
|
import Logger from '@joplin/utils/Logger';
|
|
import { AccessibilityInfo, findNodeHandle, Platform, UIManager, View } from 'react-native';
|
|
|
|
const logger = Logger.create('focusView');
|
|
|
|
interface Options {
|
|
// Web only
|
|
scrollIntoView?: boolean;
|
|
}
|
|
|
|
const focusView = (source: string, view: View|HTMLElement, { scrollIntoView = true }: Options = {}) => {
|
|
const autoFocus = () => {
|
|
if (Platform.OS === 'web') {
|
|
if (!scrollIntoView && view instanceof HTMLElement) {
|
|
// eslint-disable-next-line no-restricted-properties -- already in a call to focus()
|
|
view.focus({ preventScroll: true });
|
|
} else {
|
|
// react-native-web defines UIManager.focus for setting the keyboard focus. However,
|
|
// this property is not available in standard react-native. As such, access it using type
|
|
// narrowing:
|
|
// eslint-disable-next-line no-restricted-properties
|
|
if (!('focus' in UIManager) || typeof UIManager.focus !== 'function') {
|
|
throw new Error('Failed to focus sidebar. UIManager.focus is not a function.');
|
|
}
|
|
|
|
// eslint-disable-next-line no-restricted-properties -- already in a call to focus()
|
|
UIManager.focus(view);
|
|
}
|
|
} else {
|
|
const handle = findNodeHandle(view as View);
|
|
if (handle !== null) {
|
|
AccessibilityInfo.setAccessibilityFocus(handle);
|
|
} else {
|
|
logger.warn('Couldn\'t find a view to focus.');
|
|
}
|
|
}
|
|
};
|
|
|
|
focus(`focusView:${source}`, {
|
|
focus: autoFocus,
|
|
});
|
|
};
|
|
|
|
export default focusView;
|