Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface NavigationItem {
count?: number;
}

const navigation: NavigationItem[] = [
const navigation: readonly NavigationItem[] = [
{ name: 'Dashboard', icon: HomeIcon, href: '/' },
{ name: 'Team', icon: UsersIcon, href: '/team', count: 3 },
{ name: 'Projects', icon: FolderIcon, href: '/projects', count: 4 },
Expand All @@ -23,7 +23,7 @@ export default function Navigation() {
const navigate = useNavigate();
const { pathname } = useLocation();

const shortcuts: KbsDefinition[] = [
const shortcuts: readonly KbsDefinition[] = [
{
shortcut: { key: 'PageUp', shift: true },
handler() {
Expand Down
2 changes: 1 addition & 1 deletion src/app/useCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function useCounter(
) {
const { allowC = true, maxFrequency } = options;
const [counter, setCounter] = useState(0);
const shortcuts: KbsDefinition[] = [
const shortcuts: readonly KbsDefinition[] = [
{
shortcut: allowC ? ['i', 'c'] : ['i'],
handler() {
Expand Down
2 changes: 1 addition & 1 deletion src/component/hooks/use_kbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useLastTriggerRef,
} from '../utils/get_key_down_handler.ts';

export function useKbs(shortcuts: KbsDefinition[]) {
export function useKbs(shortcuts: readonly KbsDefinition[]) {
const lastTrigger = useLastTriggerRef();
const combinedShortcuts = useMemo(
() => combineShortcuts(cleanShortcuts([shortcuts])),
Expand Down
2 changes: 1 addition & 1 deletion src/component/hooks/use_kbs_global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect } from 'react';
import { useKbsDispatch } from '../kbs_context.ts';
import type { KbsDefinition } from '../types.ts';

export function useKbsGlobal(shortcuts: KbsDefinition[]) {
export function useKbsGlobal(shortcuts: readonly KbsDefinition[]) {
const kbsDispatch = useKbsDispatch();
useEffect(() => {
kbsDispatch({ type: 'INIT', shortcuts });
Expand Down
2 changes: 1 addition & 1 deletion src/component/hooks/use_kbs_global_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useContext } from 'react';
import { kbsContext } from '../kbs_context.ts';
import type { KbsShortcut } from '../types.ts';

export function useKbsGlobalList(): KbsShortcut[] {
export function useKbsGlobalList(): readonly KbsShortcut[] {
return useContext(kbsContext).cleanedShortcuts.map(
({ handler, ...rest }) => rest,
);
Expand Down
8 changes: 4 additions & 4 deletions src/component/kbs_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { cleanShortcuts } from './utils/clean_shortcuts.ts';
import { combineShortcuts } from './utils/combine_shortcuts.ts';

interface KbsState {
inputShortcuts: KbsDefinition[][];
cleanedShortcuts: KbsInternalShortcut[];
inputShortcuts: ReadonlyArray<readonly KbsDefinition[]>;
cleanedShortcuts: readonly KbsInternalShortcut[];
combinedShortcuts: Record<string, KbsInternalShortcut>;
disableCount: number;
}

type KbsAction =
| { type: 'INIT'; shortcuts: KbsDefinition[] }
| { type: 'CLEANUP'; shortcuts: KbsDefinition[] }
| { type: 'INIT'; shortcuts: readonly KbsDefinition[] }
| { type: 'CLEANUP'; shortcuts: readonly KbsDefinition[] }
| { type: 'DISABLE_GLOBAL' }
| { type: 'ENABLE_GLOBAL' };

Expand Down
7 changes: 5 additions & 2 deletions src/component/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export interface KbsDefinition {
/**
* The definition of key(s) that will trigger the shortcut.
*/
shortcut: string | KbsKeyDefinition | Array<string | KbsKeyDefinition>;
shortcut:
| string
| KbsKeyDefinition
| ReadonlyArray<string | KbsKeyDefinition>;
/**
* The handler function to call when the shortcut is triggered.
*/
Expand All @@ -48,7 +51,7 @@ export interface KbsDefinition {

export interface KbsShortcut {
shortcut: KbsKeyDefinition;
aliases: KbsKeyDefinition[];
aliases: readonly KbsKeyDefinition[];
meta?: KbsMetadata;
}

Expand Down
16 changes: 11 additions & 5 deletions src/component/utils/clean_shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const defaultModifiers: Modifiers = {
};

export function cleanShortcuts(
inputs: KbsDefinition[][],
): KbsInternalShortcut[] {
inputs: ReadonlyArray<readonly KbsDefinition[]>,
): readonly KbsInternalShortcut[] {
const result: KbsInternalShortcut[] = [];
for (const input of inputs) {
for (const definition of input) {
Expand All @@ -32,11 +32,17 @@ export function cleanShortcuts(
}

function shortcutToObjects(
shortcut: string | KbsKeyDefinition | Array<string | KbsKeyDefinition>,
): KbsKeyDefinition[] {
shortcut:
| string
| KbsKeyDefinition
| ReadonlyArray<string | KbsKeyDefinition>,
): readonly KbsKeyDefinition[] {
if (typeof shortcut === 'string') {
return [{ ...defaultModifiers, key: shortcut.toLowerCase() }];
} else if (Array.isArray(shortcut)) {
} else if (
// Cannot use `Array.isArray` because it does not narrow `ReadonlyArray` (<https://github.com/microsoft/TypeScript/issues/17002>)
'map' in shortcut
) {
return shortcut.map((shortcut) => {
if (typeof shortcut === 'string') {
return { ...defaultModifiers, key: shortcut.toLowerCase() };
Expand Down
2 changes: 1 addition & 1 deletion src/component/utils/combine_shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { KbsInternalShortcut } from '../types.ts';
import { shortcutToKeys } from './make_key.ts';

export function combineShortcuts(
shortcuts: KbsInternalShortcut[],
shortcuts: readonly KbsInternalShortcut[],
): Record<string, KbsInternalShortcut> {
const result: Record<string, KbsInternalShortcut> = {};
for (const shortcut of shortcuts) {
Expand Down
4 changes: 3 additions & 1 deletion src/component/utils/make_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export interface Modifiers {
alt?: boolean;
}

export function shortcutToKeys(shortcut: KbsInternalShortcut): string[] {
export function shortcutToKeys(
shortcut: KbsInternalShortcut,
): readonly string[] {
return [
...shortcutObjectToKey(shortcut.shortcut),
...shortcut.aliases.flatMap(shortcutObjectToKey),
Expand Down
Loading