2020-05-02 17:41:07 +02:00
|
|
|
import * as React from 'react';
|
2020-07-03 23:32:39 +02:00
|
|
|
import { useEffect, useCallback, useState } from 'react';
|
|
|
|
import CommandService from '../../lib/services/CommandService';
|
2020-05-03 19:44:49 +02:00
|
|
|
const { connect } = require('react-redux');
|
2020-06-10 23:08:59 +02:00
|
|
|
const { buildStyle } = require('lib/theme');
|
2020-05-02 17:41:07 +02:00
|
|
|
const Toolbar = require('../Toolbar.min.js');
|
2020-05-03 19:44:49 +02:00
|
|
|
const Folder = require('lib/models/Folder');
|
2020-05-02 17:41:07 +02:00
|
|
|
const { _ } = require('lib/locale');
|
2020-05-03 19:44:49 +02:00
|
|
|
const { substrWithEllipsis } = require('lib/string-utils');
|
2020-05-02 17:41:07 +02:00
|
|
|
|
|
|
|
interface ButtonClickEvent {
|
|
|
|
name: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface NoteToolbarProps {
|
|
|
|
theme: number,
|
|
|
|
style: any,
|
2020-05-03 19:44:49 +02:00
|
|
|
folders: any[],
|
2020-05-02 17:41:07 +02:00
|
|
|
watchedNoteFiles: string[],
|
2020-05-15 13:13:42 +02:00
|
|
|
backwardHistoryNotes: any[],
|
|
|
|
forwardHistoryNotes: any[],
|
2020-05-03 19:44:49 +02:00
|
|
|
notesParentType: string,
|
2020-05-02 17:41:07 +02:00
|
|
|
note: any,
|
|
|
|
dispatch: Function,
|
|
|
|
onButtonClick(event:ButtonClickEvent):void,
|
|
|
|
}
|
|
|
|
|
|
|
|
function styles_(props:NoteToolbarProps) {
|
|
|
|
return buildStyle('NoteToolbar', props.theme, (/* theme:any*/) => {
|
|
|
|
return {
|
|
|
|
root: {
|
|
|
|
...props.style,
|
2020-05-03 19:44:49 +02:00
|
|
|
borderBottom: 'none',
|
2020-05-02 17:41:07 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
function NoteToolbar(props:NoteToolbarProps) {
|
|
|
|
const styles = styles_(props);
|
|
|
|
const [toolbarItems, setToolbarItems] = useState([]);
|
|
|
|
const selectedNoteFolder = Folder.byId(props.folders, props.note.parent_id);
|
2020-07-07 20:16:33 +02:00
|
|
|
const folderId = selectedNoteFolder ? selectedNoteFolder.id : '';
|
|
|
|
const folderTitle = selectedNoteFolder ? selectedNoteFolder.title : '';
|
2020-07-03 23:32:39 +02:00
|
|
|
|
|
|
|
const cmdService = CommandService.instance();
|
|
|
|
|
|
|
|
const updateToolbarItems = useCallback(() => {
|
|
|
|
const output = [];
|
|
|
|
|
|
|
|
output.push(
|
|
|
|
cmdService.commandToToolbarButton('historyBackward')
|
|
|
|
);
|
|
|
|
|
|
|
|
output.push(
|
|
|
|
cmdService.commandToToolbarButton('historyForward')
|
|
|
|
);
|
|
|
|
|
2020-07-07 20:16:33 +02:00
|
|
|
if (folderId && ['Search', 'Tag', 'SmartFilter'].includes(props.notesParentType)) {
|
2020-07-03 23:32:39 +02:00
|
|
|
output.push({
|
2020-07-07 20:16:33 +02:00
|
|
|
title: _('In: %s', substrWithEllipsis(folderTitle, 0, 16)),
|
2020-07-03 23:32:39 +02:00
|
|
|
iconName: 'fa-book',
|
|
|
|
onClick: () => {
|
|
|
|
props.dispatch({
|
|
|
|
type: 'FOLDER_AND_NOTE_SELECT',
|
2020-07-07 20:16:33 +02:00
|
|
|
folderId: folderId,
|
2020-07-03 23:32:39 +02:00
|
|
|
noteId: props.note.id,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-05-03 19:44:49 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
output.push(cmdService.commandToToolbarButton('showNoteProperties'));
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
if (props.watchedNoteFiles.indexOf(props.note.id) >= 0) {
|
|
|
|
output.push(cmdService.commandToToolbarButton('stopExternalEditing'));
|
|
|
|
} else {
|
|
|
|
output.push(cmdService.commandToToolbarButton('startExternalEditing'));
|
|
|
|
}
|
2020-05-03 19:44:49 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
output.push(cmdService.commandToToolbarButton('editAlarm'));
|
2020-05-15 13:13:42 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
output.push(cmdService.commandToToolbarButton('setTags'));
|
2020-05-15 13:13:42 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
setToolbarItems(output);
|
2020-07-07 20:16:33 +02:00
|
|
|
}, [props.note.id, folderId, folderTitle, props.watchedNoteFiles, props.notesParentType]);
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
useEffect(() => {
|
|
|
|
updateToolbarItems();
|
|
|
|
cmdService.on('commandsEnabledStateChange', updateToolbarItems);
|
|
|
|
return () => {
|
|
|
|
cmdService.off('commandsEnabledStateChange', updateToolbarItems);
|
2020-05-02 17:41:07 +02:00
|
|
|
};
|
2020-07-03 23:32:39 +02:00
|
|
|
}, [updateToolbarItems]);
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-05-03 19:44:49 +02:00
|
|
|
return <Toolbar style={styles.root} items={toolbarItems} />;
|
2020-05-02 17:41:07 +02:00
|
|
|
}
|
2020-05-03 19:44:49 +02:00
|
|
|
|
|
|
|
const mapStateToProps = (state:any) => {
|
|
|
|
return {
|
|
|
|
folders: state.folders,
|
|
|
|
watchedNoteFiles: state.watchedNoteFiles,
|
2020-05-15 13:13:42 +02:00
|
|
|
backwardHistoryNotes: state.backwardHistoryNotes,
|
|
|
|
forwardHistoryNotes: state.forwardHistoryNotes,
|
2020-05-03 19:44:49 +02:00
|
|
|
notesParentType: state.notesParentType,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(NoteToolbar);
|