文件
mynode/packages/app-desktop/gui/Sidebar/hooks/useOnItemClick.ts
chen 301194963b
一些检查失败了
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
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

66 行
2.0 KiB
TypeScript

import { MouseEvent } from 'react';
import { ModelType } from '@joplin/lib/BaseModel';
import { RefObject, useCallback } from 'react';
import { Dispatch } from 'redux';
import { ListItem, ListItemType } from '../types';
import shim from '@joplin/lib/shim';
export interface ItemClickEvent {
id: string;
type: ModelType;
event: MouseEvent;
}
interface Props {
itemsRef: RefObject<ListItem[]>;
selectedIndexesRef: RefObject<number[]>;
dispatch: Dispatch;
}
const listItemToId = (item: ListItem) => {
if (item.kind === ListItemType.Tag) return item.tag.id;
if (item.kind === ListItemType.Folder) return item.folder.id;
return null;
};
const useOnItemClick = ({ dispatch, selectedIndexesRef, itemsRef }: Props) => {
return useCallback(({ id, type, event }: ItemClickEvent) => {
const action = type === ModelType.Folder ? 'FOLDER_SELECT' : 'TAG_SELECT';
const selectedIndexes = selectedIndexesRef.current;
const findItemIndex = () => itemsRef.current.findIndex(item => listItemToId(item) === id);
if (event.shiftKey && selectedIndexes.length > 0) {
const index = findItemIndex();
if (index === -1) throw new Error(`No item found with ID: ${id}`);
const lastAddedIndex = selectedIndexes[selectedIndexes.length - 1];
const indexStart = Math.min(index, lastAddedIndex);
const indexStop = Math.max(index, lastAddedIndex);
const itemIds = itemsRef.current.slice(indexStart, indexStop + 1)
.map(listItemToId)
.filter(id => !!id);
dispatch({
type: `${action}_ADD`,
ids: itemIds,
});
} else if (shim.isMac() ? event.metaKey : event.ctrlKey) {
const index = findItemIndex();
// Don't allow unselecting all items: Keep at least one item selected
const canDeselect = selectedIndexes.length > 1;
const actionType = canDeselect && selectedIndexes.includes(index) ? 'REMOVE' : 'ADD';
dispatch({
type: `${action}_${actionType}`,
id: id,
});
} else {
dispatch({
type: action,
id: id,
});
}
}, [dispatch, selectedIndexesRef, itemsRef]);
};
export default useOnItemClick;