2023-08-21 17:01:20 +02:00
|
|
|
import * as React from 'react';
|
2024-10-11 23:07:56 +02:00
|
|
|
import { useMemo, useRef, useEffect, useCallback } from 'react';
|
2023-08-21 17:01:20 +02:00
|
|
|
import { AppState } from '../../app.reducer';
|
|
|
|
import BaseModel, { ModelType } from '@joplin/lib/BaseModel';
|
2023-09-18 18:40:36 +02:00
|
|
|
import { Props } from './utils/types';
|
|
|
|
import { ItemFlow } from '@joplin/lib/services/plugins/api/noteListType';
|
2023-08-21 17:01:20 +02:00
|
|
|
import { itemIsReadOnlySync, ItemSlice } from '@joplin/lib/models/utils/readOnly';
|
|
|
|
import { FolderEntity } from '@joplin/lib/services/database/types';
|
|
|
|
import ItemChange from '@joplin/lib/models/ItemChange';
|
|
|
|
import { Size } from '@joplin/utils/types';
|
|
|
|
import NoteListItem from '../NoteListItem/NoteListItem';
|
|
|
|
import useItemCss from './utils/useItemCss';
|
|
|
|
import useOnContextMenu from '../NoteListItem/utils/useOnContextMenu';
|
|
|
|
import useVisibleRange from './utils/useVisibleRange';
|
|
|
|
import useScroll from './utils/useScroll';
|
|
|
|
import useFocusNote from './utils/useFocusNote';
|
|
|
|
import useOnNoteClick from './utils/useOnNoteClick';
|
|
|
|
import useMoveNote from './utils/useMoveNote';
|
|
|
|
import useOnKeyDown from './utils/useOnKeyDown';
|
|
|
|
import * as focusElementNoteList from './commands/focusElementNoteList';
|
|
|
|
import CommandService from '@joplin/lib/services/CommandService';
|
|
|
|
import useDragAndDrop from './utils/useDragAndDrop';
|
2024-03-02 16:25:27 +02:00
|
|
|
import { itemIsInTrash } from '@joplin/lib/services/trash';
|
2024-04-25 14:34:32 +02:00
|
|
|
import getEmptyFolderMessage from '@joplin/lib/components/shared/NoteList/getEmptyFolderMessage';
|
2024-03-02 16:25:27 +02:00
|
|
|
import Folder from '@joplin/lib/models/Folder';
|
2024-08-31 17:05:01 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import useActiveDescendantId from './utils/useActiveDescendantId';
|
|
|
|
import getNoteElementIdFromJoplinId from '../NoteListItem/utils/getNoteElementIdFromJoplinId';
|
|
|
|
import useFocusVisible from './utils/useFocusVisible';
|
2024-11-08 17:32:05 +02:00
|
|
|
import { stateUtils } from '@joplin/lib/reducer';
|
|
|
|
import { connect } from 'react-redux';
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
const commands = {
|
|
|
|
focusElementNoteList,
|
|
|
|
};
|
|
|
|
|
|
|
|
const NoteList = (props: Props) => {
|
2024-08-31 17:05:01 +02:00
|
|
|
const listRef = useRef<HTMLDivElement>(null);
|
2023-08-21 17:01:20 +02:00
|
|
|
const itemRefs = useRef<Record<string, HTMLDivElement>>({});
|
2023-09-18 18:40:36 +02:00
|
|
|
const listRenderer = props.listRenderer;
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
const itemSize: Size = useMemo(() => {
|
|
|
|
return {
|
|
|
|
width: listRenderer.itemSize.width ? listRenderer.itemSize.width : props.size.width,
|
|
|
|
height: listRenderer.itemSize.height,
|
|
|
|
};
|
|
|
|
}, [listRenderer.itemSize, props.size.width]);
|
|
|
|
|
|
|
|
const itemsPerLine = useMemo(() => {
|
|
|
|
if (listRenderer.flow === ItemFlow.TopToBottom) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return Math.max(1, Math.floor(props.size.width / itemSize.width));
|
|
|
|
}
|
|
|
|
}, [listRenderer.flow, props.size.width, itemSize.width]);
|
|
|
|
|
|
|
|
const { scrollTop, onScroll, makeItemIndexVisible } = useScroll(
|
|
|
|
itemsPerLine,
|
|
|
|
props.notes.length,
|
|
|
|
itemSize,
|
|
|
|
props.size,
|
2023-08-22 12:58:53 +02:00
|
|
|
listRef,
|
2023-08-21 17:01:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const [startNoteIndex, endNoteIndex, startLineIndex, endLineIndex, totalLineCount, visibleItemCount] = useVisibleRange(
|
|
|
|
itemsPerLine,
|
|
|
|
scrollTop,
|
|
|
|
props.size,
|
|
|
|
itemSize,
|
2023-08-22 12:58:53 +02:00
|
|
|
props.notes.length,
|
2023-08-21 17:01:20 +02:00
|
|
|
);
|
|
|
|
|
2024-08-31 17:05:01 +02:00
|
|
|
const { activeNoteId, setActiveNoteId } = useActiveDescendantId(props.selectedFolderId, props.selectedNoteIds);
|
|
|
|
const focusNote = useFocusNote(listRef, props.notes, makeItemIndexVisible, setActiveNoteId);
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
const moveNote = useMoveNote(
|
|
|
|
props.notesParentType,
|
|
|
|
props.noteSortOrder,
|
|
|
|
props.selectedNoteIds,
|
|
|
|
props.selectedFolderId,
|
|
|
|
props.uncompletedTodosOnTop,
|
|
|
|
props.showCompletedTodos,
|
2023-08-22 12:58:53 +02:00
|
|
|
props.notes,
|
2024-03-02 16:25:27 +02:00
|
|
|
props.selectedFolderInTrash,
|
2024-03-26 13:45:15 +02:00
|
|
|
makeItemIndexVisible,
|
|
|
|
focusNote,
|
|
|
|
props.dispatch,
|
2023-08-21 17:01:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const noteItemStyle = useMemo(() => {
|
|
|
|
return {
|
|
|
|
width: 'auto',
|
|
|
|
height: itemSize.height,
|
|
|
|
};
|
|
|
|
}, [itemSize.height]);
|
|
|
|
|
|
|
|
const noteListStyle = useMemo(() => {
|
|
|
|
return {
|
|
|
|
width: props.size.width,
|
|
|
|
height: props.size.height,
|
|
|
|
};
|
|
|
|
}, [props.size]);
|
|
|
|
|
|
|
|
const onNoteClick = useOnNoteClick(props.dispatch, focusNote);
|
|
|
|
|
|
|
|
const onKeyDown = useOnKeyDown(
|
2024-08-31 17:05:01 +02:00
|
|
|
activeNoteId,
|
2023-08-21 17:01:20 +02:00
|
|
|
props.selectedNoteIds,
|
|
|
|
moveNote,
|
|
|
|
makeItemIndexVisible,
|
|
|
|
focusNote,
|
|
|
|
props.notes,
|
|
|
|
props.dispatch,
|
|
|
|
visibleItemCount,
|
|
|
|
props.notes.length,
|
|
|
|
listRenderer.flow,
|
2023-08-22 12:58:53 +02:00
|
|
|
itemsPerLine,
|
2023-08-21 17:01:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
useItemCss(listRenderer.itemCss);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
CommandService.instance().registerRuntime(commands.focusElementNoteList.declaration.name, commands.focusElementNoteList.runtime(focusNote));
|
|
|
|
return () => {
|
|
|
|
CommandService.instance().unregisterRuntime(commands.focusElementNoteList.declaration.name);
|
|
|
|
};
|
|
|
|
}, [focusNote]);
|
|
|
|
|
|
|
|
const onItemContextMenu = useOnContextMenu(
|
|
|
|
props.selectedNoteIds,
|
|
|
|
props.selectedFolderId,
|
|
|
|
props.notes,
|
|
|
|
props.dispatch,
|
|
|
|
props.watchedNoteFiles,
|
|
|
|
props.plugins,
|
2023-08-22 12:58:53 +02:00
|
|
|
props.customCss,
|
2023-08-21 17:01:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const { onDragStart, onDragOver, onDrop, dragOverTargetNoteIndex } = useDragAndDrop(props.parentFolderIsReadOnly,
|
|
|
|
props.selectedNoteIds,
|
|
|
|
props.selectedFolderId,
|
|
|
|
listRef,
|
|
|
|
scrollTop,
|
|
|
|
itemSize,
|
|
|
|
props.notesParentType,
|
|
|
|
props.noteSortOrder,
|
|
|
|
props.uncompletedTodosOnTop,
|
|
|
|
props.showCompletedTodos,
|
|
|
|
listRenderer.flow,
|
2023-08-22 12:58:53 +02:00
|
|
|
itemsPerLine,
|
2024-03-02 16:25:27 +02:00
|
|
|
props.selectedFolderInTrash,
|
2023-08-21 17:01:20 +02:00
|
|
|
);
|
|
|
|
|
2024-04-01 17:51:47 +02:00
|
|
|
// 2024-04-01: Whatever the below effect is supposed to be doing has been lost in time and even
|
|
|
|
// if it's doing something useful it should be refactored. In my tests, removing it doesn't
|
|
|
|
// affect anything - including scrolling with the keyboard and switching notes so there's a
|
|
|
|
// chance that whatever it's doing is being done more cleanly somewhere else. If a focus
|
|
|
|
// related-bug is found, it should be fixed from scratch, without touching this event, although
|
|
|
|
// it could possibly be used as a reference.
|
|
|
|
//
|
|
|
|
// * * *
|
|
|
|
|
|
|
|
// const previousSelectedNoteIds = usePrevious(props.selectedNoteIds, []);
|
|
|
|
// const previousNoteCount = usePrevious(props.notes.length, 0);
|
|
|
|
// const previousVisible = usePrevious(props.visible, false);
|
|
|
|
|
|
|
|
// useEffect(() => {
|
|
|
|
// if (previousSelectedNoteIds !== props.selectedNoteIds && props.selectedNoteIds.length === 1) {
|
|
|
|
// const id = props.selectedNoteIds[0];
|
|
|
|
// const doRefocus = props.notes.length < previousNoteCount && !props.focusedField;
|
|
|
|
|
|
|
|
// for (let i = 0; i < props.notes.length; i++) {
|
|
|
|
// if (props.notes[i].id === id) {
|
|
|
|
// makeItemIndexVisible(i);
|
|
|
|
// if (doRefocus) {
|
|
|
|
// const ref = itemRefs.current[id];
|
|
|
|
// if (ref) {
|
|
|
|
// focus('NoteList::doRefocus', ref);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }, [makeItemIndexVisible, previousSelectedNoteIds, previousNoteCount, previousVisible, props.selectedNoteIds, props.notes, props.focusedField, props.visible]);
|
2023-08-21 17:01:20 +02:00
|
|
|
|
2024-08-31 17:05:01 +02:00
|
|
|
const { focusVisible, onFocus, onBlur, onKeyUp } = useFocusVisible(listRef, () => {
|
|
|
|
focusNote(activeNoteId);
|
|
|
|
});
|
|
|
|
|
2023-08-21 17:01:20 +02:00
|
|
|
const highlightedWords = useMemo(() => {
|
|
|
|
if (props.notesParentType === 'Search') {
|
|
|
|
const query = BaseModel.byId(props.searches, props.selectedSearchId);
|
|
|
|
if (query) return props.highlightedWords;
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}, [props.notesParentType, props.searches, props.selectedSearchId, props.highlightedWords]);
|
|
|
|
|
|
|
|
const renderEmptyList = () => {
|
|
|
|
if (props.notes.length) return null;
|
2024-04-25 14:34:32 +02:00
|
|
|
return <div className="emptylist">{getEmptyFolderMessage(props.folders, props.selectedFolderId)}</div>;
|
2023-08-21 17:01:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const renderFiller = (key: string, style: React.CSSProperties) => {
|
|
|
|
if (!props.notes.length) return null;
|
|
|
|
if (style.height as number <= 0) return null;
|
|
|
|
return <div key={key} style={style}></div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderNotes = () => {
|
2024-08-31 17:05:01 +02:00
|
|
|
if (!props.notes.length) return [];
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
const output: JSX.Element[] = [];
|
|
|
|
|
|
|
|
for (let i = startNoteIndex; i <= endNoteIndex; i++) {
|
|
|
|
const note = props.notes[i];
|
2024-08-31 17:05:01 +02:00
|
|
|
const isSelected = props.selectedNoteIds.includes(note.id);
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
output.push(
|
|
|
|
<NoteListItem
|
|
|
|
key={note.id}
|
|
|
|
ref={el => itemRefs.current[note.id] = el}
|
|
|
|
index={i}
|
|
|
|
dragIndex={dragOverTargetNoteIndex}
|
|
|
|
noteCount={props.notes.length}
|
|
|
|
itemSize={itemSize}
|
|
|
|
onChange={listRenderer.onChange}
|
|
|
|
onClick={onNoteClick}
|
|
|
|
onContextMenu={onItemContextMenu}
|
|
|
|
onDragStart={onDragStart}
|
|
|
|
onDragOver={onDragOver}
|
|
|
|
style={noteItemStyle}
|
|
|
|
highlightedWords={highlightedWords}
|
|
|
|
isProvisional={props.provisionalNoteIds.includes(note.id)}
|
|
|
|
flow={listRenderer.flow}
|
2023-09-18 18:40:36 +02:00
|
|
|
note={note}
|
2024-08-31 17:05:01 +02:00
|
|
|
tabIndex={-1}
|
|
|
|
focusVisible={focusVisible && activeNoteId === note.id}
|
|
|
|
isSelected={isSelected}
|
2023-09-18 18:40:36 +02:00
|
|
|
isWatched={props.watchedNoteFiles.includes(note.id)}
|
|
|
|
listRenderer={listRenderer}
|
2024-02-20 01:09:34 +02:00
|
|
|
dispatch={props.dispatch}
|
2024-03-02 17:29:18 +02:00
|
|
|
columns={props.columns}
|
2023-08-22 12:58:53 +02:00
|
|
|
/>,
|
2023-08-21 17:01:20 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
|
|
|
const topFillerHeight = startLineIndex * itemSize.height;
|
|
|
|
const bottomFillerHeight = (totalLineCount - endLineIndex - 1) * itemSize.height;
|
|
|
|
|
|
|
|
const fillerBaseStyle = useMemo(() => {
|
|
|
|
// return { width: 'auto', border: '1px solid red', backgroundColor: 'green' };
|
|
|
|
return { width: 'auto' };
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const topFillerStyle = useMemo(() => {
|
|
|
|
return { ...fillerBaseStyle, height: topFillerHeight };
|
|
|
|
}, [fillerBaseStyle, topFillerHeight]);
|
|
|
|
|
|
|
|
const bottomFillerStyle = useMemo(() => {
|
|
|
|
return { ...fillerBaseStyle, height: bottomFillerHeight };
|
|
|
|
}, [fillerBaseStyle, bottomFillerHeight]);
|
|
|
|
|
|
|
|
const notesStyle = useMemo(() => {
|
|
|
|
const output: React.CSSProperties = {};
|
|
|
|
|
|
|
|
if (listRenderer.flow === ItemFlow.LeftToRight) {
|
|
|
|
output.flexFlow = 'row wrap';
|
|
|
|
} else {
|
|
|
|
output.flexDirection = 'column';
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}, [listRenderer.flow]);
|
|
|
|
|
2024-10-11 23:07:56 +02:00
|
|
|
const onContainerContextMenu = useCallback((event: React.MouseEvent) => {
|
|
|
|
const isFromKeyboard = event.button === -1;
|
|
|
|
if (event.isDefaultPrevented() || !isFromKeyboard) return;
|
|
|
|
onItemContextMenu({ itemId: activeNoteId });
|
|
|
|
}, [onItemContextMenu, activeNoteId]);
|
|
|
|
|
2024-11-09 14:50:06 +02:00
|
|
|
const hasNotes = !!props.notes.length;
|
2023-08-21 17:01:20 +02:00
|
|
|
return (
|
|
|
|
<div
|
2024-11-09 14:50:06 +02:00
|
|
|
role={hasNotes ? 'listbox' : 'group'}
|
|
|
|
aria-label={hasNotes ? _('Notes') : null}
|
|
|
|
aria-activedescendant={activeNoteId ? getNoteElementIdFromJoplinId(activeNoteId) : undefined}
|
|
|
|
aria-multiselectable={hasNotes ? true : undefined}
|
2024-08-31 17:05:01 +02:00
|
|
|
tabIndex={0}
|
|
|
|
|
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
|
|
|
|
2023-08-21 17:01:20 +02:00
|
|
|
className="note-list"
|
|
|
|
style={noteListStyle}
|
|
|
|
ref={listRef}
|
|
|
|
onScroll={onScroll}
|
|
|
|
onKeyDown={onKeyDown}
|
2024-08-31 17:05:01 +02:00
|
|
|
onKeyUp={onKeyUp}
|
2023-08-21 17:01:20 +02:00
|
|
|
onDrop={onDrop}
|
2024-10-11 23:07:56 +02:00
|
|
|
onContextMenu={onContainerContextMenu}
|
2023-08-21 17:01:20 +02:00
|
|
|
>
|
|
|
|
{renderEmptyList()}
|
|
|
|
{renderFiller('top', topFillerStyle)}
|
2024-08-31 17:05:01 +02:00
|
|
|
<div className='notes' role='presentation' style={notesStyle}>
|
2023-08-21 17:01:20 +02:00
|
|
|
{renderNotes()}
|
|
|
|
</div>
|
|
|
|
{renderFiller('bottom', bottomFillerStyle)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
interface ConnectProps {
|
|
|
|
windowId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state: AppState, ownProps: ConnectProps) => {
|
2024-03-02 16:25:27 +02:00
|
|
|
const selectedFolder: FolderEntity = state.notesParentType === 'Folder' ? Folder.byId(state.folders, state.selectedFolderId) : null;
|
2023-08-21 17:01:20 +02:00
|
|
|
const userId = state.settings['sync.userId'];
|
2024-11-08 17:32:05 +02:00
|
|
|
const windowState = stateUtils.windowStateById(state, ownProps.windowId);
|
2023-08-21 17:01:20 +02:00
|
|
|
|
|
|
|
return {
|
2024-11-08 17:32:05 +02:00
|
|
|
notes: windowState.notes,
|
2023-08-21 17:01:20 +02:00
|
|
|
folders: state.folders,
|
2024-11-08 17:32:05 +02:00
|
|
|
selectedNoteIds: windowState.selectedNoteIds,
|
|
|
|
selectedFolderId: windowState.selectedFolderId,
|
2023-08-21 17:01:20 +02:00
|
|
|
themeId: state.settings.theme,
|
|
|
|
notesParentType: state.notesParentType,
|
|
|
|
searches: state.searches,
|
2024-11-08 17:32:05 +02:00
|
|
|
selectedSearchId: windowState.selectedSearchId,
|
2023-08-21 17:01:20 +02:00
|
|
|
watchedNoteFiles: state.watchedNoteFiles,
|
|
|
|
provisionalNoteIds: state.provisionalNoteIds,
|
|
|
|
isInsertingNotes: state.isInsertingNotes,
|
|
|
|
noteSortOrder: state.settings['notes.sortOrder.field'],
|
|
|
|
uncompletedTodosOnTop: state.settings.uncompletedTodosOnTop,
|
|
|
|
showCompletedTodos: state.settings.showCompletedTodos,
|
|
|
|
highlightedWords: state.highlightedWords,
|
|
|
|
plugins: state.pluginService.plugins,
|
2024-11-08 17:32:05 +02:00
|
|
|
customCss: state.customViewerCss,
|
2023-08-21 17:01:20 +02:00
|
|
|
focusedField: state.focusedField,
|
|
|
|
parentFolderIsReadOnly: state.notesParentType === 'Folder' && selectedFolder ? itemIsReadOnlySync(ModelType.Folder, ItemChange.SOURCE_UNSPECIFIED, selectedFolder as ItemSlice, userId, state.shareService) : false,
|
2024-03-02 16:25:27 +02:00
|
|
|
selectedFolderInTrash: itemIsInTrash(selectedFolder),
|
2023-08-21 17:01:20 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(NoteList);
|