2020-11-13 19:09:28 +02:00
|
|
|
import { State, stateUtils } from '../../reducer';
|
2021-01-22 19:41:11 +02:00
|
|
|
import BaseModel from '../../BaseModel';
|
|
|
|
import Folder from '../../models/Folder';
|
|
|
|
import MarkupToHtml from '@joplin/renderer/MarkupToHtml';
|
2021-05-13 18:57:37 +02:00
|
|
|
import { isRootSharedFolder, isSharedFolderOwner } from '../share/reducer';
|
|
|
|
import { FolderEntity, NoteEntity } from '../database/types';
|
|
|
|
|
|
|
|
export interface WhenClauseContextOptions {
|
|
|
|
commandFolderId?: string;
|
|
|
|
commandNoteId?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface WhenClauseContext {
|
|
|
|
notesAreBeingSaved: boolean;
|
|
|
|
syncStarted: boolean;
|
|
|
|
inConflictFolder: boolean;
|
|
|
|
oneNoteSelected: boolean;
|
|
|
|
someNotesSelected: boolean;
|
|
|
|
multipleNotesSelected: boolean;
|
|
|
|
noNotesSelected: boolean;
|
|
|
|
historyhasBackwardNotes: boolean;
|
|
|
|
historyhasForwardNotes: boolean;
|
|
|
|
oneFolderSelected: boolean;
|
|
|
|
noteIsTodo: boolean;
|
|
|
|
noteTodoCompleted: boolean;
|
|
|
|
noteIsMarkdown: boolean;
|
|
|
|
noteIsHtml: boolean;
|
2021-10-13 19:02:54 +02:00
|
|
|
folderIsShareRootAndNotOwnedByUser: boolean;
|
2021-05-13 18:57:37 +02:00
|
|
|
folderIsShareRootAndOwnedByUser: boolean;
|
|
|
|
folderIsShared: boolean;
|
2021-10-13 19:02:54 +02:00
|
|
|
folderIsShareRoot: boolean;
|
2021-05-16 15:21:55 +02:00
|
|
|
joplinServerConnected: boolean;
|
2022-04-11 17:49:32 +02:00
|
|
|
hasMultiProfiles: boolean;
|
2021-05-13 18:57:37 +02:00
|
|
|
}
|
2020-10-18 22:52:10 +02:00
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
export default function stateToWhenClauseContext(state: State, options: WhenClauseContextOptions = null): WhenClauseContext {
|
|
|
|
options = {
|
|
|
|
commandFolderId: '',
|
|
|
|
commandNoteId: '',
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectedNoteId = state.selectedNoteIds.length === 1 ? state.selectedNoteIds[0] : null;
|
|
|
|
const selectedNote: NoteEntity = selectedNoteId ? BaseModel.byId(state.notes, selectedNoteId) : null;
|
|
|
|
|
|
|
|
// const commandNoteId = options.commandNoteId || selectedNoteId;
|
|
|
|
// const commandNote:NoteEntity = commandNoteId ? BaseModel.byId(state.notes, commandNoteId) : null;
|
|
|
|
|
2021-05-16 15:21:55 +02:00
|
|
|
const commandFolderId = options.commandFolderId || state.selectedFolderId;
|
2021-05-13 18:57:37 +02:00
|
|
|
const commandFolder: FolderEntity = commandFolderId ? BaseModel.byId(state.folders, commandFolderId) : null;
|
2020-10-18 22:52:10 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
// Application state
|
|
|
|
notesAreBeingSaved: stateUtils.hasNotesBeingSaved(state),
|
|
|
|
syncStarted: state.syncStarted,
|
|
|
|
|
|
|
|
// Current location
|
|
|
|
inConflictFolder: state.selectedFolderId === Folder.conflictFolderId(),
|
|
|
|
|
|
|
|
// Note selection
|
2021-05-13 18:57:37 +02:00
|
|
|
oneNoteSelected: !!selectedNote,
|
2020-10-18 22:52:10 +02:00
|
|
|
someNotesSelected: state.selectedNoteIds.length > 0,
|
|
|
|
multipleNotesSelected: state.selectedNoteIds.length > 1,
|
|
|
|
noNotesSelected: !state.selectedNoteIds.length,
|
|
|
|
|
|
|
|
// Note history
|
|
|
|
historyhasBackwardNotes: state.backwardHistoryNotes.length > 0,
|
|
|
|
historyhasForwardNotes: state.forwardHistoryNotes.length > 0,
|
|
|
|
|
|
|
|
// Folder selection
|
|
|
|
oneFolderSelected: !!state.selectedFolderId,
|
|
|
|
|
|
|
|
// Current note properties
|
2021-05-13 18:57:37 +02:00
|
|
|
noteIsTodo: selectedNote ? !!selectedNote.is_todo : false,
|
|
|
|
noteTodoCompleted: selectedNote ? !!selectedNote.todo_completed : false,
|
|
|
|
noteIsMarkdown: selectedNote ? selectedNote.markup_language === MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN : false,
|
|
|
|
noteIsHtml: selectedNote ? selectedNote.markup_language === MarkupToHtml.MARKUP_LANGUAGE_HTML : false,
|
|
|
|
|
|
|
|
// Current context folder
|
2021-10-13 19:02:54 +02:00
|
|
|
folderIsShareRoot: commandFolder ? isRootSharedFolder(commandFolder) : false,
|
|
|
|
folderIsShareRootAndNotOwnedByUser: commandFolder ? isRootSharedFolder(commandFolder) && !isSharedFolderOwner(state, commandFolder.id) : false,
|
2021-05-13 18:57:37 +02:00
|
|
|
folderIsShareRootAndOwnedByUser: commandFolder ? isRootSharedFolder(commandFolder) && isSharedFolderOwner(state, commandFolder.id) : false,
|
|
|
|
folderIsShared: commandFolder ? !!commandFolder.share_id : false,
|
2021-05-16 15:21:55 +02:00
|
|
|
|
2021-06-06 19:14:12 +02:00
|
|
|
joplinServerConnected: [9, 10].includes(state.settings['sync.target']),
|
2022-04-11 17:49:32 +02:00
|
|
|
|
|
|
|
hasMultiProfiles: state.profileConfig && state.profileConfig.profiles.length > 1,
|
2020-10-18 22:52:10 +02:00
|
|
|
};
|
|
|
|
}
|