Skip to content

Commit

Permalink
dependency to 'app' is removed from perFolderSortOrder.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ken1kob committed Sep 20, 2021
1 parent e9dcc3b commit 5d0b0e3
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import CommandService, { CommandContext, CommandDeclaration, CommandRuntime } from '@joplin/lib/services/CommandService';
import Setting from '@joplin/lib/models/Setting';
import { State } from '@joplin/lib/reducer';
import app from '../../../app';
import eventManager from '@joplin/lib/eventManager';
import { notesSortOrderFieldArray } from './notesSortOrderSwitch';
import { _ } from '@joplin/lib/locale';

let previousFolderId: string = null;
const folderState = { notesParentType: '', selectedFolderId: '' };
let ownSortOrders: { [key: string]: any } = null;
const sharedSortOrder: { [key: string]: any } = {
field: 'user_updated_time',
Expand All @@ -17,10 +16,6 @@ const sharedSortOrder: { [key: string]: any } = {
order: false,
};

export function selectedFolderHasOwnSortOrder() {
return hasOwnSortOrder(getSelectedFolderId());
}

export function hasOwnSortOrder(folderId: string) {
return folderId && ownSortOrders && ownSortOrders.hasOwnProperty(folderId + SUFFIX_FIELD);
}
Expand All @@ -33,8 +28,8 @@ export const declaration: CommandDeclaration = {
export const runtime = (): CommandRuntime => {
loadOwnSortOrders();
loadSharedSortOrder();
eventManager.appStateOn('notesParentType', onFolderSelectionMayChange);
eventManager.appStateOn('selectedFolderId', onFolderSelectionMayChange);
eventManager.appStateOn('notesParentType', onFolderSelectionMayChange.bind(null, 'notesParentType'));
eventManager.appStateOn('selectedFolderId', onFolderSelectionMayChange.bind(null, 'selectedFolderId'));

return {
enabledCondition: 'oneFolderSelected',
Expand Down Expand Up @@ -75,8 +70,10 @@ export const runtime = (): CommandRuntime => {
};
};

function onFolderSelectionMayChange() {
const selectedId = getSelectedFolderId();
function onFolderSelectionMayChange(cause: string, event: any) {
if (cause !== 'notesParentType' && cause !== 'selectedFolderId') return;
folderState[cause] = event.value;
const selectedId = getSelectedFolderId(folderState);
if (previousFolderId === null) previousFolderId = selectedId;
if (previousFolderId === selectedId) return;
const field = Setting.value('notes.sortOrder.field');
Expand Down Expand Up @@ -106,10 +103,9 @@ function onFolderSelectionMayChange() {
const SUFFIX_FIELD = '$field';
const SUFFIX_REVERSE = '$reverse';

function getSelectedFolderId(state?: State): string {
const s = state ? state : app().store().getState();
if (s.notesParentType === 'Folder') {
return s.selectedFolderId;
function getSelectedFolderId(state: { notesParentType: string; selectedFolderId: string }): string {
if (state.notesParentType === 'Folder') {
return state.selectedFolderId;
} else {
return '';
}
Expand Down

0 comments on commit 5d0b0e3

Please sign in to comment.