1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ElectronClient/app/gui/NoteList.jsx

337 lines
10 KiB
React
Raw Normal View History

2017-11-04 18:40:34 +02:00
const { ItemList } = require('./ItemList.min.js');
const React = require('react');
const { connect } = require('react-redux');
2017-11-10 22:11:48 +02:00
const { time } = require('lib/time-utils.js');
2017-11-08 19:51:55 +02:00
const { themeStyle } = require('../theme.js');
const BaseModel = require('lib/BaseModel');
2017-11-08 19:51:55 +02:00
const { _ } = require('lib/locale.js');
const { bridge } = require('electron').remote.require('./bridge');
const Menu = bridge().Menu;
const MenuItem = bridge().MenuItem;
const eventManager = require('../eventManager');
const InteropService = require('lib/services/InteropService');
const InteropServiceHelper = require('../InteropServiceHelper.js');
const Search = require('lib/models/Search');
const Mark = require('mark.js/dist/mark.min.js');
2017-11-04 18:40:34 +02:00
class NoteListComponent extends React.Component {
2017-11-09 21:21:10 +02:00
style() {
const theme = themeStyle(this.props.theme);
const itemHeight = 34;
2017-11-09 21:21:10 +02:00
let style = {
root: {
backgroundColor: theme.backgroundColor,
},
listItem: {
height: itemHeight,
boxSizing: 'border-box',
display: 'flex',
2017-11-10 22:11:48 +02:00
alignItems: 'stretch',
2017-11-09 21:21:10 +02:00
backgroundColor: theme.backgroundColor,
borderBottom: '1px solid ' + theme.dividerColor,
},
listItemSelected: {
backgroundColor: theme.selectedColor,
},
2017-11-10 22:11:48 +02:00
listItemTitle: {
fontFamily: theme.fontFamily,
fontSize: theme.fontSize,
textDecoration: 'none',
color: theme.color,
cursor: 'default',
whiteSpace: 'nowrap',
flex: 1,
display: 'flex',
alignItems: 'center',
overflow: 'hidden',
},
listItemTitleCompleted: {
opacity: 0.5,
textDecoration: 'line-through',
},
2017-11-09 21:21:10 +02:00
};
return style;
}
2017-11-08 19:51:55 +02:00
itemContextMenu(event) {
const currentItemId = event.currentTarget.getAttribute('data-id');
if (!currentItemId) return;
let noteIds = [];
if (this.props.selectedNoteIds.indexOf(currentItemId) < 0) {
noteIds = [currentItemId];
} else {
noteIds = this.props.selectedNoteIds;
}
2017-11-22 20:35:31 +02:00
if (!noteIds.length) return;
2017-11-08 19:51:55 +02:00
const notes = noteIds.map((id) => BaseModel.byId(this.props.notes, id));
let hasEncrypted = false;
for (let i = 0; i < notes.length; i++) {
if (!!notes[i].encryption_applied) hasEncrypted = true;
}
const menu = new Menu()
if (!hasEncrypted) {
menu.append(new MenuItem({label: _('Add or remove tags'), enabled: noteIds.length === 1, click: async () => {
this.props.dispatch({
type: 'WINDOW_COMMAND',
name: 'setTags',
noteId: noteIds[0],
});
}}));
menu.append(new MenuItem({label: _('Duplicate'), click: async () => {
for (let i = 0; i < noteIds.length; i++) {
const note = await Note.load(noteIds[i]);
await Note.duplicate(noteIds[i], {
uniqueTitle: _('%s - Copy', note.title),
});
}
}}));
if (noteIds.length <= 1) {
menu.append(new MenuItem({label: _('Switch between note and to-do type'), click: async () => {
for (let i = 0; i < noteIds.length; i++) {
const note = await Note.load(noteIds[i]);
await Note.save(Note.toggleIsTodo(note), { userSideValidation: true });
eventManager.emit('noteTypeToggle', { noteId: note.id });
}
}}));
} else {
const switchNoteType = async (noteIds, type) => {
for (let i = 0; i < noteIds.length; i++) {
const note = await Note.load(noteIds[i]);
const newNote = Note.changeNoteType(note, type);
if (newNote === note) continue;
await Note.save(newNote, { userSideValidation: true });
eventManager.emit('noteTypeToggle', { noteId: note.id });
}
}
menu.append(new MenuItem({label: _('Switch to note type'), click: async () => {
await switchNoteType(noteIds, 'note');
}}));
menu.append(new MenuItem({label: _('Switch to to-do type'), click: async () => {
await switchNoteType(noteIds, 'todo');
}}));
}
menu.append(new MenuItem({label: _('Copy Markdown link'), click: async () => {
const { clipboard } = require('electron');
const links = [];
for (let i = 0; i < noteIds.length; i++) {
const note = await Note.load(noteIds[i]);
links.push(Note.markdownTag(note));
}
clipboard.writeText(links.join(' '));
}}));
2018-03-12 10:30:10 +02:00
const exportMenu = new Menu();
const ioService = new InteropService();
const ioModules = ioService.modules();
for (let i = 0; i < ioModules.length; i++) {
const module = ioModules[i];
if (module.type !== 'exporter') continue;
exportMenu.append(new MenuItem({ label: module.fullLabel() , click: async () => {
2018-03-12 10:30:10 +02:00
await InteropServiceHelper.export(this.props.dispatch.bind(this), module, { sourceNoteIds: noteIds });
}}));
}
exportMenu.append(new MenuItem({ label: 'PDF - ' + _('PDF File') , click: () => {
this.props.dispatch({
type: 'WINDOW_COMMAND',
name: 'exportPdf',
});
}}));
2018-03-12 10:30:10 +02:00
const exportMenuItem = new MenuItem({label: _('Export'), submenu: exportMenu});
menu.append(exportMenuItem);
}
2017-11-12 01:13:14 +02:00
menu.append(new MenuItem({label: _('Delete'), click: async () => {
2017-11-22 20:35:31 +02:00
const ok = bridge().showConfirmMessageBox(noteIds.length > 1 ? _('Delete notes?') : _('Delete note?'));
2017-11-12 01:13:14 +02:00
if (!ok) return;
2017-11-22 20:35:31 +02:00
await Note.batchDelete(noteIds);
2017-11-12 01:13:14 +02:00
}}));
2017-11-08 19:51:55 +02:00
menu.popup(bridge().window());
}
2017-11-10 22:11:48 +02:00
itemRenderer(item, theme, width) {
const onTitleClick = async (event, item) => {
2017-11-22 20:35:31 +02:00
if (event.ctrlKey) {
event.preventDefault();
2017-11-22 20:35:31 +02:00
this.props.dispatch({
type: 'NOTE_SELECT_TOGGLE',
id: item.id,
});
} else if (event.shiftKey) {
event.preventDefault();
2017-11-22 20:35:31 +02:00
this.props.dispatch({
type: 'NOTE_SELECT_EXTEND',
id: item.id,
});
} else {
this.props.dispatch({
type: 'NOTE_SELECT',
id: item.id,
});
}
2017-11-05 01:27:13 +02:00
}
const onDragStart = (event) => {
let noteIds = [];
// Here there is two cases:
// - If multiple notes are selected, we drag the group
// - If only one note is selected, we drag the note that was clicked on (which might be different from the currently selected note)
if (this.props.selectedNoteIds.length >= 2) {
noteIds = this.props.selectedNoteIds;
} else {
const clickedNoteId = event.currentTarget.getAttribute('data-id');
if (clickedNoteId) noteIds.push(clickedNoteId);
}
if (!noteIds.length) return;
event.dataTransfer.setDragImage(new Image(), 1, 1);
event.dataTransfer.clearData();
event.dataTransfer.setData('text/x-jop-note-ids', JSON.stringify(noteIds));
}
2017-11-10 22:11:48 +02:00
const onCheckboxClick = async (event) => {
const checked = event.target.checked;
const newNote = {
id: item.id,
todo_completed: checked ? time.unixMs() : 0,
}
await Note.save(newNote, { userSideValidation: true });
eventManager.emit('todoToggle', { noteId: item.id });
2017-11-10 22:11:48 +02:00
}
2017-11-10 23:04:53 +02:00
const hPadding = 10;
2017-11-10 22:11:48 +02:00
let highlightedWords = [];
if (this.props.notesParentType === 'Search') {
const search = BaseModel.byId(this.props.searches, this.props.selectedSearchId);
highlightedWords = search ? Search.keywords(search.query_pattern) : [];
}
2017-11-10 22:11:48 +02:00
let style = Object.assign({ width: width }, this.style().listItem);
if (this.props.selectedNoteIds.indexOf(item.id) >= 0) {
style = Object.assign(style, this.style().listItemSelected);
}
2017-11-08 19:51:55 +02:00
2017-11-10 22:11:48 +02:00
// Setting marginBottom = 1 because it makes the checkbox looks more centered, at least on Windows
// but don't know how it will look in other OSes.
const checkbox = item.is_todo ?
2017-11-10 23:04:53 +02:00
<div style={{display: 'flex', height: style.height, alignItems: 'center', paddingLeft: hPadding}}>
2017-11-10 22:11:48 +02:00
<input style={{margin:0, marginBottom:1}} type="checkbox" defaultChecked={!!item.todo_completed} onClick={(event) => { onCheckboxClick(event, item) }}/>
</div>
: null;
let listItemTitleStyle = Object.assign({}, this.style().listItemTitle);
2017-11-10 23:04:53 +02:00
listItemTitleStyle.paddingLeft = !checkbox ? hPadding : 4;
if (item.is_todo && !!item.todo_completed) listItemTitleStyle = Object.assign(listItemTitleStyle, this.style().listItemTitleCompleted);
2017-11-10 22:11:48 +02:00
let displayTitle = Note.displayTitle(item);
let titleComp = null;
if (highlightedWords.length) {
const titleElement = document.createElement('span');
titleElement.textContent = displayTitle;
const mark = new Mark(titleElement, {
exclude: ['img'],
acrossElements: true,
});
mark.mark(highlightedWords);
// Note: in this case it is safe to use dangerouslySetInnerHTML because titleElement
// is a span tag that we created and that contains data that's been inserted as plain text
// with `textContent` so it cannot contain any XSS attacks. We use this feature because
// mark.js can only deal with DOM elements.
// https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml
titleComp = <span dangerouslySetInnerHTML={{ __html: titleElement.outerHTML }}></span>
} else {
titleComp = <span>{displayTitle}</span>
}
2017-11-14 20:02:58 +02:00
// Need to include "todo_completed" in key so that checkbox is updated when
// item is changed via sync.
2017-11-14 20:02:58 +02:00
return <div key={item.id + '_' + item.todo_completed} style={style}>
2017-11-10 22:11:48 +02:00
{checkbox}
<a
className="list-item"
onContextMenu={(event) => this.itemContextMenu(event)}
href="#"
draggable={true}
2017-11-10 22:11:48 +02:00
style={listItemTitleStyle}
onClick={(event) => { onTitleClick(event, item) }}
onDragStart={(event) => onDragStart(event) }
data-id={item.id}
2017-11-10 22:11:48 +02:00
>
{titleComp}
2017-11-10 22:11:48 +02:00
</a>
</div>
2017-11-04 18:40:34 +02:00
}
render() {
2017-11-08 19:51:55 +02:00
const theme = themeStyle(this.props.theme);
2017-11-10 19:58:17 +02:00
const style = this.props.style;
let notes = this.props.notes.slice();
2017-11-10 19:58:17 +02:00
if (!notes.length) {
2017-11-10 19:58:17 +02:00
const padding = 10;
const emptyDivStyle = Object.assign({
padding: padding + 'px',
fontSize: theme.fontSize,
color: theme.color,
backgroundColor: theme.backgroundColor,
fontFamily: theme.fontFamily,
}, style);
emptyDivStyle.width = emptyDivStyle.width - padding * 2;
emptyDivStyle.height = emptyDivStyle.height - padding * 2;
return <div style={emptyDivStyle}>{ this.props.folders.length ? _('No notes in here. Create one by clicking on "New note".') : _('There is currently no notebook. Create one by clicking on "New notebook".')}</div>
2017-11-10 19:58:17 +02:00
}
2017-11-08 19:51:55 +02:00
2017-11-04 18:40:34 +02:00
return (
<ItemList
2017-11-13 02:23:12 +02:00
itemHeight={this.style().listItem.height}
2017-11-10 19:58:17 +02:00
style={style}
className={"note-list"}
items={notes}
2017-11-10 22:11:48 +02:00
itemRenderer={ (item) => { return this.itemRenderer(item, theme, style.width) } }
2017-11-05 01:27:13 +02:00
></ItemList>
2017-11-04 18:40:34 +02:00
);
}
}
const mapStateToProps = (state) => {
return {
2017-11-05 01:27:13 +02:00
notes: state.notes,
folders: state.folders,
2017-11-22 20:35:31 +02:00
selectedNoteIds: state.selectedNoteIds,
2017-11-08 19:51:55 +02:00
theme: state.settings.theme,
notesParentType: state.notesParentType,
searches: state.searches,
selectedSearchId: state.selectedSearchId,
2017-11-04 18:40:34 +02:00
};
};
const NoteList = connect(mapStateToProps)(NoteListComponent);
module.exports = { NoteList };