59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { c as _c } from "react/compiler-runtime";
|
|
import React, { createContext, useMemo, useSyncExternalStore } from 'react';
|
|
import { getTerminalFocused, getTerminalFocusState, subscribeTerminalFocus, type TerminalFocusState } from '../terminal-focus-state.js';
|
|
export type { TerminalFocusState };
|
|
export type TerminalFocusContextProps = {
|
|
readonly isTerminalFocused: boolean;
|
|
readonly terminalFocusState: TerminalFocusState;
|
|
};
|
|
|
|
/**
|
|
* 终端焦点上下文 - 提供终端焦点状态给子组件。
|
|
*/
|
|
const TerminalFocusContext = createContext<TerminalFocusContextProps>({
|
|
isTerminalFocused: true,
|
|
terminalFocusState: 'unknown'
|
|
});
|
|
|
|
// eslint-disable-next-line custom-rules/no-top-level-side-effects
|
|
TerminalFocusContext.displayName = 'TerminalFocusContext';
|
|
|
|
/**
|
|
* 单独的组件,这样 App.tsx 不会在焦点变化时重新渲染。
|
|
* children 是一个稳定的 prop 引用,所以它们也不会重新渲染——
|
|
* 只有使用该上下文的组件会重新渲染。
|
|
*/
|
|
export function TerminalFocusProvider(t0) {
|
|
const $ = _c(6);
|
|
const {
|
|
children
|
|
} = t0;
|
|
const isTerminalFocused = useSyncExternalStore(subscribeTerminalFocus, getTerminalFocused);
|
|
const terminalFocusState = useSyncExternalStore(subscribeTerminalFocus, getTerminalFocusState);
|
|
let t1;
|
|
if ($[0] !== isTerminalFocused || $[1] !== terminalFocusState) {
|
|
t1 = {
|
|
isTerminalFocused,
|
|
terminalFocusState
|
|
};
|
|
$[0] = isTerminalFocused;
|
|
$[1] = terminalFocusState;
|
|
$[2] = t1;
|
|
} else {
|
|
t1 = $[2];
|
|
}
|
|
const value = t1;
|
|
let t2;
|
|
if ($[3] !== children || $[4] !== value) {
|
|
t2 = <TerminalFocusContext.Provider value={value}>{children}</TerminalFocusContext.Provider>;
|
|
$[3] = children;
|
|
$[4] = value;
|
|
$[5] = t2;
|
|
} else {
|
|
t2 = $[5];
|
|
}
|
|
return t2;
|
|
}
|
|
|
|
export default TerminalFocusContext;
|