const React = require('react'); const { connect } = require('react-redux'); const { Header } = require('./Header.min.js'); const { SideBar } = require('./SideBar.min.js'); const { NoteList } = require('./NoteList.min.js'); const { NoteText } = require('./NoteText.min.js'); const NoteText2 = require('./NoteText2.js').default; const { stateUtils } = require('lib/reducer.js'); const { PromptDialog } = require('./PromptDialog.min.js'); const NoteContentPropertiesDialog = require('./NoteContentPropertiesDialog.js').default; const NotePropertiesDialog = require('./NotePropertiesDialog.min.js'); const ShareNoteDialog = require('./ShareNoteDialog.js').default; const Setting = require('lib/models/Setting.js'); const BaseModel = require('lib/BaseModel.js'); const Tag = require('lib/models/Tag.js'); const Note = require('lib/models/Note.js'); const { uuid } = require('lib/uuid.js'); const Folder = require('lib/models/Folder.js'); const { themeStyle } = require('../theme.js'); const { _ } = require('lib/locale.js'); const { bridge } = require('electron').remote.require('./bridge'); const eventManager = require('../eventManager'); const VerticalResizer = require('./VerticalResizer.min'); const PluginManager = require('lib/services/PluginManager'); const TemplateUtils = require('lib/TemplateUtils'); const EncryptionService = require('lib/services/EncryptionService'); const ipcRenderer = require('electron').ipcRenderer; class MainScreenComponent extends React.Component { constructor() { super(); this.state = { promptOptions: null, modalLayer: { visible: false, message: '', }, notePropertiesDialogOptions: {}, noteContentPropertiesDialogOptions: {}, shareNoteDialogOptions: {}, }; this.setupAppCloseHandling(); this.notePropertiesDialog_close = this.notePropertiesDialog_close.bind(this); this.noteContentPropertiesDialog_close = this.noteContentPropertiesDialog_close.bind(this); this.shareNoteDialog_close = this.shareNoteDialog_close.bind(this); this.sidebar_onDrag = this.sidebar_onDrag.bind(this); this.noteList_onDrag = this.noteList_onDrag.bind(this); } setupAppCloseHandling() { this.waitForNotesSavedIID_ = null; // This event is dispached from the main process when the app is about // to close. The renderer process must respond with the "appCloseReply" // and tell the main process whether the app can really be closed or not. // For example, it cannot be closed right away if a note is being saved. // If a note is being saved, we wait till it is saved and then call // "appCloseReply" again. ipcRenderer.on('appClose', () => { if (this.waitForNotesSavedIID_) clearInterval(this.waitForNotesSavedIID_); this.waitForNotesSavedIID_ = null; ipcRenderer.send('asynchronous-message', 'appCloseReply', { canClose: !this.props.hasNotesBeingSaved, }); if (this.props.hasNotesBeingSaved) { this.waitForNotesSavedIID_ = setInterval(() => { if (!this.props.hasNotesBeingSaved) { clearInterval(this.waitForNotesSavedIID_); this.waitForNotesSavedIID_ = null; ipcRenderer.send('asynchronous-message', 'appCloseReply', { canClose: true, }); } }, 50); } }); } sidebar_onDrag(event) { Setting.setValue('style.sidebar.width', this.props.sidebarWidth + event.deltaX); } noteList_onDrag(event) { Setting.setValue('style.noteList.width', Setting.value('style.noteList.width') + event.deltaX); } notePropertiesDialog_close() { this.setState({ notePropertiesDialogOptions: {} }); } noteContentPropertiesDialog_close() { this.setState({ noteContentPropertiesDialogOptions: {} }); } shareNoteDialog_close() { this.setState({ shareNoteDialogOptions: {} }); } UNSAFE_componentWillReceiveProps(newProps) { // Execute a command if any, and if we haven't already executed it if (newProps.windowCommand && newProps.windowCommand !== this.props.windowCommand) { this.doCommand(newProps.windowCommand); } } toggleVisiblePanes() { this.props.dispatch({ type: 'NOTE_VISIBLE_PANES_TOGGLE', }); } toggleSidebar() { this.props.dispatch({ type: 'SIDEBAR_VISIBILITY_TOGGLE', }); } toggleNoteList() { this.props.dispatch({ type: 'NOTELIST_VISIBILITY_TOGGLE', }); } async doCommand(command) { if (!command) return; const createNewNote = async (template, isTodo) => { const folderId = Setting.value('activeFolderId'); if (!folderId) return; const body = template ? TemplateUtils.render(template) : ''; const newNote = await Note.save({ parent_id: folderId, is_todo: isTodo ? 1 : 0, body: body, }, { provisional: true }); this.props.dispatch({ type: 'NOTE_SELECT', id: newNote.id, }); }; let commandProcessed = true; if (command.name === 'newNote') { if (!this.props.folders.length) { bridge().showErrorMessageBox(_('Please create a notebook first.')); } else { await createNewNote(null, false); } } else if (command.name === 'newTodo') { if (!this.props.folders.length) { bridge().showErrorMessageBox(_('Please create a notebook first')); } else { await createNewNote(null, true); } } else if (command.name === 'newNotebook' || (command.name === 'newSubNotebook' && command.activeFolderId)) { this.setState({ promptOptions: { label: _('Notebook title:'), onClose: async answer => { if (answer) { let folder = null; try { folder = await Folder.save({ title: answer }, { userSideValidation: true }); if (command.name === 'newSubNotebook') folder = await Folder.moveToFolder(folder.id, command.activeFolderId); } catch (error) { bridge().showErrorMessageBox(error.message); } if (folder) { this.props.dispatch({ type: 'FOLDER_SELECT', id: folder.id, historyAction: 'goto', }); } } this.setState({ promptOptions: null }); }, }, }); } else if (command.name === 'setTags') { const tags = await Tag.commonTagsByNoteIds(command.noteIds); const startTags = tags .map(a => { return { value: a.id, label: a.title }; }) .sort((a, b) => { // sensitivity accent will treat accented characters as differemt // but treats caps as equal return a.label.localeCompare(b.label, undefined, { sensitivity: 'accent' }); }); const allTags = await Tag.allWithNotes(); const tagSuggestions = allTags.map(a => { return { value: a.id, label: a.title }; }) .sort((a, b) => { // sensitivity accent will treat accented characters as differemt // but treats caps as equal return a.label.localeCompare(b.label, undefined, { sensitivity: 'accent' }); }); this.setState({ promptOptions: { label: _('Add or remove tags:'), inputType: 'tags', value: startTags, autocomplete: tagSuggestions, onClose: async answer => { if (answer !== null) { const endTagTitles = answer.map(a => { return a.label.trim(); }); if (command.noteIds.length === 1) { await Tag.setNoteTagsByTitles(command.noteIds[0], endTagTitles); } else { const startTagTitles = startTags.map(a => { return a.label.trim(); }); const addTags = endTagTitles.filter(value => !startTagTitles.includes(value)); const delTags = startTagTitles.filter(value => !endTagTitles.includes(value)); // apply the tag additions and deletions to each selected note for (let i = 0; i < command.noteIds.length; i++) { const tags = await Tag.tagsByNoteId(command.noteIds[i]); let tagTitles = tags.map(a => { return a.title; }); tagTitles = tagTitles.concat(addTags); tagTitles = tagTitles.filter(value => !delTags.includes(value)); await Tag.setNoteTagsByTitles(command.noteIds[i], tagTitles); } } } this.setState({ promptOptions: null }); }, }, }); } else if (command.name === 'moveToFolder') { const startFolders = []; const maxDepth = 15; const addOptions = (folders, depth) => { for (let i = 0; i < folders.length; i++) { const folder = folders[i]; startFolders.push({ key: folder.id, value: folder.id, label: folder.title, indentDepth: depth }); if (folder.children) addOptions(folder.children, (depth + 1) < maxDepth ? depth + 1 : maxDepth); } }; addOptions(await Folder.allAsTree(), 0); this.setState({ promptOptions: { label: _('Move to notebook:'), inputType: 'dropdown', value: '', autocomplete: startFolders, onClose: async answer => { if (answer != null) { for (let i = 0; i < command.noteIds.length; i++) { await Note.moveToFolder(command.noteIds[i], answer.value); } } this.setState({ promptOptions: null }); }, }, }); } else if (command.name === 'renameFolder') { const folder = await Folder.load(command.id); if (folder) { this.setState({ promptOptions: { label: _('Rename notebook:'), value: folder.title, onClose: async answer => { if (answer !== null) { try { folder.title = answer; await Folder.save(folder, { fields: ['title'], userSideValidation: true }); } catch (error) { bridge().showErrorMessageBox(error.message); } } this.setState({ promptOptions: null }); }, }, }); } } else if (command.name === 'renameTag') { const tag = await Tag.load(command.id); if (tag) { this.setState({ promptOptions: { label: _('Rename tag:'), value: tag.title, onClose: async answer => { if (answer !== null) { try { tag.title = answer; await Tag.save(tag, { fields: ['title'], userSideValidation: true }); } catch (error) { bridge().showErrorMessageBox(error.message); } } this.setState({ promptOptions: null }); }, }, }); } } else if (command.name === 'search') { if (!this.searchId_) this.searchId_ = uuid.create(); this.props.dispatch({ type: 'SEARCH_UPDATE', search: { id: this.searchId_, title: command.query, query_pattern: command.query, query_folder_id: null, type_: BaseModel.TYPE_SEARCH, }, }); if (command.query) { this.props.dispatch({ type: 'SEARCH_SELECT', id: this.searchId_, }); } else { const note = await Note.load(this.props.selectedNoteId); if (note) { this.props.dispatch({ type: 'FOLDER_AND_NOTE_SELECT', folderId: note.parent_id, noteId: note.id, }); } } } else if (command.name === 'commandNoteProperties') { this.setState({ notePropertiesDialogOptions: { noteId: command.noteId, visible: true, onRevisionLinkClick: command.onRevisionLinkClick, }, }); } else if (command.name === 'commandContentProperties') { this.setState({ noteContentPropertiesDialogOptions: { visible: true, text: command.text, lines: command.lines, }, }); } else if (command.name === 'commandShareNoteDialog') { this.setState({ shareNoteDialogOptions: { noteIds: command.noteIds, visible: true, }, }); } else if (command.name === 'toggleVisiblePanes') { this.toggleVisiblePanes(); } else if (command.name === 'toggleSidebar') { this.toggleSidebar(); } else if (command.name === 'toggleNoteList') { this.toggleNoteList(); } else if (command.name === 'showModalMessage') { this.setState({ modalLayer: { visible: true, message: