1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/app-desktop/gui/NoteListControls/NoteListControls.tsx

168 lines
4.5 KiB
TypeScript
Raw Normal View History

import { AppState } from '../../app.reducer';
2020-09-15 15:01:07 +02:00
import * as React from 'react';
import { useEffect, useRef } from 'react';
import SearchBar from '../SearchBar/SearchBar';
import Button, { ButtonLevel, ButtonSize, buttonSizePx } from '../Button/Button';
import CommandService from '@joplin/lib/services/CommandService';
2020-09-15 15:01:07 +02:00
import { runtime as focusSearchRuntime } from './commands/focusSearch';
const { connect } = require('react-redux');
2020-09-15 15:01:07 +02:00
const styled = require('styled-components').default;
interface Props {
showNewNoteButtons: boolean;
sortOrderButtonsVisible: boolean;
sortOrderField: string;
sortOrderReverse: boolean;
notesParentType: string;
height: number;
}
2020-09-15 15:01:07 +02:00
const StyledRoot = styled.div`
box-sizing: border-box;
height: ${(props: any) => props.height}px;
2020-09-15 15:01:07 +02:00
display: flex;
flex-direction: row;
padding: ${(props: any) => props.theme.mainPadding}px;
background-color: ${(props: any) => props.theme.backgroundColor3};
2020-09-15 15:01:07 +02:00
`;
const StyledButton = styled(Button)`
margin-left: 8px;
width: 26px;
height: 26px;
min-width: 26px;
min-height: 26px;
2020-09-15 15:01:07 +02:00
`;
const StyledPairButtonL = styled(Button)`
margin-left: 8px;
border-radius: 3px 0 0 3px;
min-width: ${(props: any) => buttonSizePx(props)}px;
max-width: ${(props: any) => buttonSizePx(props)}px;
`;
const StyledPairButtonR = styled(Button)`
min-width: 8px;
margin-left: 0px;
border-radius: 0 3px 3px 0;
border-width: 1px 1px 1px 0;
width: auto;
`;
const ButtonContainer = styled.div`
display: flex;
flex-direction: row;
`;
function NoteListControls(props: Props) {
2020-09-15 15:01:07 +02:00
const searchBarRef = useRef(null);
useEffect(function() {
CommandService.instance().registerRuntime('focusSearch', focusSearchRuntime(searchBarRef));
return function() {
CommandService.instance().unregisterRuntime('focusSearch');
};
}, []);
function onNewTodoButtonClick() {
2020-11-25 16:40:25 +02:00
void CommandService.instance().execute('newTodo');
2020-09-15 15:01:07 +02:00
}
function onNewNoteButtonClick() {
2020-11-25 16:40:25 +02:00
void CommandService.instance().execute('newNote');
2020-09-15 15:01:07 +02:00
}
function onSortOrderFieldButtonClick() {
void CommandService.instance().execute('toggleNotesSortOrderField');
}
function onSortOrderReverseButtonClick() {
void CommandService.instance().execute('toggleNotesSortOrderReverse');
}
function sortOrderFieldIcon() {
const field = props.sortOrderField;
const iconMap: any = {
user_updated_time: 'far fa-calendar-alt',
user_created_time: 'far fa-calendar-plus',
title: 'fas fa-font',
order: 'fas fa-wrench',
};
return `${iconMap[field] || iconMap['title']} ${field}`;
}
function sortOrderReverseIcon() {
return props.sortOrderReverse ? 'fas fa-long-arrow-alt-up' : 'fas fa-long-arrow-alt-down';
}
function showsSortOrderButtons() {
let visible = props.sortOrderButtonsVisible;
if (props.notesParentType === 'Search') visible = false;
return visible;
}
function renderNewNoteButtons() {
if (!props.showNewNoteButtons) return null;
return (
<ButtonContainer>
{showsSortOrderButtons() &&
<StyledPairButtonL
className="sort-order-field-button"
tooltip={CommandService.instance().label('toggleNotesSortOrderField')}
iconName={sortOrderFieldIcon()}
level={ButtonLevel.Secondary}
size={ButtonSize.Small}
onClick={onSortOrderFieldButtonClick}
/>
}
{showsSortOrderButtons() &&
<StyledPairButtonR
className="sort-order-reverse-button"
tooltip={CommandService.instance().label('toggleNotesSortOrderReverse')}
iconName={sortOrderReverseIcon()}
level={ButtonLevel.Secondary}
size={ButtonSize.Small}
onClick={onSortOrderReverseButtonClick}
/>
}
<StyledButton
className="new-todo-button"
tooltip={CommandService.instance().label('newTodo')}
iconName="far fa-check-square"
level={ButtonLevel.Primary}
size={ButtonSize.Small}
onClick={onNewTodoButtonClick}
/>
<StyledButton
className="new-note-button"
tooltip={CommandService.instance().label('newNote')}
iconName="icon-note"
level={ButtonLevel.Primary}
size={ButtonSize.Small}
onClick={onNewNoteButtonClick}
/>
</ButtonContainer>
);
}
2020-09-15 15:01:07 +02:00
return (
<StyledRoot height={props.height}>
2020-09-15 15:01:07 +02:00
<SearchBar inputRef={searchBarRef}/>
{renderNewNoteButtons()}
2020-09-15 15:01:07 +02:00
</StyledRoot>
);
}
const mapStateToProps = (state: AppState) => {
return {
sortOrderButtonsVisible: state.settings['notes.sortOrder.buttonsVisible'],
sortOrderField: state.settings['notes.sortOrder.field'],
sortOrderReverse: state.settings['notes.sortOrder.reverse'],
notesParentType: state.notesParentType,
};
};
export default connect(mapStateToProps)(NoteListControls);