2023-02-22 15:13:39 +02:00
|
|
|
import { themeStyle } from '@joplin/lib/theme';
|
2020-11-13 19:09:28 +02:00
|
|
|
import * as React from 'react';
|
2023-03-13 18:26:56 +02:00
|
|
|
import { useMemo, useState } from 'react';
|
2023-08-21 17:01:20 +02:00
|
|
|
import NoteList2 from '../NoteList/NoteList2';
|
2020-11-13 19:09:28 +02:00
|
|
|
import NoteListControls from '../NoteListControls/NoteListControls';
|
|
|
|
import { Size } from '../ResizableLayout/utils/types';
|
|
|
|
import styled from 'styled-components';
|
2023-09-18 18:40:36 +02:00
|
|
|
import { getDefaultListRenderer, getListRendererById } from '@joplin/lib/services/noteList/renderers';
|
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
|
|
|
|
const logger = Logger.create('NoteListWrapper');
|
2020-11-13 19:09:28 +02:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
resizableLayoutEventEmitter: any;
|
|
|
|
size: Size;
|
|
|
|
visible: boolean;
|
|
|
|
themeId: number;
|
2023-09-18 18:40:36 +02:00
|
|
|
listRendererId: string;
|
|
|
|
startupPluginsLoaded: boolean;
|
2020-11-13 19:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const StyledRoot = styled.div`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2020-11-15 21:30:04 +02:00
|
|
|
overflow: hidden;
|
2021-07-06 15:02:22 +02:00
|
|
|
width: 100%;
|
2020-11-13 19:09:28 +02:00
|
|
|
`;
|
|
|
|
|
2023-09-18 18:40:36 +02:00
|
|
|
// If the renderer ID that was saved to settings is already registered, we
|
|
|
|
// return it. If not, we need to wait for all plugins to be loaded, because one
|
|
|
|
// of them will most likely register the renderer we need. If none of them do,
|
|
|
|
// we use a default renderer instead of throwing an error.
|
|
|
|
const useListRenderer = (listRendererId: string, startupPluginsLoaded: boolean) => {
|
|
|
|
const listRenderer = getListRendererById(listRendererId);
|
|
|
|
if (listRenderer) return listRenderer;
|
|
|
|
if (startupPluginsLoaded) {
|
|
|
|
logger.error(`Tried to load renderer "${listRendererId}" but none of the registered renderers match this ID - using default renderer instead`);
|
|
|
|
return getDefaultListRenderer();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2020-11-13 19:09:28 +02:00
|
|
|
export default function NoteListWrapper(props: Props) {
|
2023-02-22 15:13:39 +02:00
|
|
|
const theme = themeStyle(props.themeId);
|
2023-09-17 09:27:42 +02:00
|
|
|
const [controlHeight] = useState(theme.topRowHeight);
|
2023-09-18 18:40:36 +02:00
|
|
|
const listRenderer = useListRenderer(props.listRendererId, props.startupPluginsLoaded);
|
2023-02-22 15:13:39 +02:00
|
|
|
|
2020-11-13 19:09:28 +02:00
|
|
|
const noteListSize = useMemo(() => {
|
|
|
|
return {
|
|
|
|
width: props.size.width,
|
2023-09-17 09:27:42 +02:00
|
|
|
height: props.size.height,
|
2020-11-13 19:09:28 +02:00
|
|
|
};
|
2023-09-17 09:27:42 +02:00
|
|
|
}, [props.size]);
|
2020-11-13 19:09:28 +02:00
|
|
|
|
2023-09-18 18:40:36 +02:00
|
|
|
const renderNoteList = () => {
|
|
|
|
if (!listRenderer) return null;
|
|
|
|
return <NoteList2
|
|
|
|
listRenderer={listRenderer}
|
|
|
|
resizableLayoutEventEmitter={props.resizableLayoutEventEmitter}
|
|
|
|
size={noteListSize}
|
|
|
|
visible={props.visible}
|
|
|
|
/>;
|
|
|
|
};
|
2023-08-21 17:01:20 +02:00
|
|
|
|
2020-11-13 19:09:28 +02:00
|
|
|
return (
|
|
|
|
<StyledRoot>
|
2023-09-17 09:27:42 +02:00
|
|
|
<NoteListControls height={controlHeight} width={noteListSize.width}/>
|
2023-09-18 18:40:36 +02:00
|
|
|
{renderNoteList()}
|
2020-11-13 19:09:28 +02:00
|
|
|
</StyledRoot>
|
|
|
|
);
|
|
|
|
}
|