2023-05-29 12:31:21 +02:00
|
|
|
const React = require('react');
|
|
|
|
|
2024-03-26 13:35:15 +02:00
|
|
|
import { FunctionComponent, ReactElement } from 'react';
|
2023-05-29 12:31:21 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import Folder, { FolderEntityWithChildren } from '@joplin/lib/models/Folder';
|
2024-03-09 13:15:13 +02:00
|
|
|
import { themeStyle } from './global-style';
|
2023-05-29 12:31:21 +02:00
|
|
|
import Dropdown, { DropdownListItem, OnValueChangedListener } from './Dropdown';
|
|
|
|
import { FolderEntity } from '@joplin/lib/services/database/types';
|
|
|
|
|
|
|
|
interface FolderPickerProps {
|
|
|
|
disabled?: boolean;
|
|
|
|
selectedFolderId?: string;
|
|
|
|
onValueChange?: OnValueChangedListener;
|
|
|
|
mustSelect?: boolean;
|
|
|
|
folders: FolderEntity[];
|
|
|
|
placeholder?: string;
|
|
|
|
darkText?: boolean;
|
2023-07-18 15:46:11 +02:00
|
|
|
themeId?: number;
|
2024-03-26 13:35:15 +02:00
|
|
|
coverableChildrenRight?: ReactElement|ReactElement[];
|
2023-05-29 12:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const FolderPicker: FunctionComponent<FolderPickerProps> = ({
|
|
|
|
disabled,
|
|
|
|
selectedFolderId,
|
|
|
|
onValueChange,
|
|
|
|
mustSelect,
|
|
|
|
folders,
|
|
|
|
placeholder,
|
|
|
|
darkText,
|
2024-03-26 13:35:15 +02:00
|
|
|
coverableChildrenRight,
|
2023-05-29 12:31:21 +02:00
|
|
|
themeId,
|
|
|
|
}) => {
|
|
|
|
const theme = themeStyle(themeId);
|
|
|
|
|
|
|
|
const addFolderChildren = (
|
2023-08-22 12:58:53 +02:00
|
|
|
folders: FolderEntityWithChildren[], pickerItems: DropdownListItem[], indent: number,
|
2023-05-29 12:31:21 +02:00
|
|
|
) => {
|
|
|
|
folders.sort((a, b) => {
|
|
|
|
const aTitle = a && a.title ? a.title : '';
|
|
|
|
const bTitle = b && b.title ? b.title : '';
|
|
|
|
return aTitle.toLowerCase() < bTitle.toLowerCase() ? -1 : +1;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let i = 0; i < folders.length; i++) {
|
|
|
|
const f = folders[i];
|
|
|
|
const icon = Folder.unserializeIcon(f.icon);
|
|
|
|
const iconString = icon ? `${icon.emoji} ` : '';
|
2024-02-09 01:17:17 +02:00
|
|
|
pickerItems.push({ label: `${iconString + Folder.displayTitle(f)}`, depth: indent, value: f.id });
|
2023-05-29 12:31:21 +02:00
|
|
|
pickerItems = addFolderChildren(f.children, pickerItems, indent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pickerItems;
|
|
|
|
};
|
|
|
|
|
|
|
|
const titlePickerItems = (mustSelect: boolean) => {
|
|
|
|
const folderList = folders.filter(f => f.id !== Folder.conflictFolderId());
|
|
|
|
let output = [];
|
|
|
|
if (mustSelect) output.push({ label: placeholder || _('Move to notebook...'), value: '' });
|
|
|
|
const folderTree = Folder.buildTree(folderList);
|
|
|
|
output = addFolderChildren(folderTree, output, 0);
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dropdown
|
|
|
|
items={titlePickerItems(!!mustSelect)}
|
|
|
|
disabled={disabled}
|
|
|
|
labelTransform="trim"
|
|
|
|
selectedValue={selectedFolderId || ''}
|
2024-03-26 13:35:15 +02:00
|
|
|
coverableChildrenRight={coverableChildrenRight}
|
2023-05-29 12:31:21 +02:00
|
|
|
itemListStyle={{
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
}}
|
|
|
|
headerStyle={{
|
|
|
|
color: darkText ? theme.colorFaded : theme.colorBright2,
|
|
|
|
fontSize: theme.fontSize,
|
|
|
|
opacity: disabled ? theme.disabledOpacity : 1,
|
|
|
|
}}
|
|
|
|
itemStyle={{
|
|
|
|
color: theme.color,
|
|
|
|
fontSize: theme.fontSize,
|
|
|
|
}}
|
|
|
|
onValueChange={(folderId) => {
|
|
|
|
if (onValueChange) {
|
|
|
|
onValueChange(folderId);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FolderPicker;
|