Skip to content

Commit

Permalink
remove string constants and internal wrapping functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ken1kob committed Sep 19, 2021
1 parent c723ebe commit 4cdba95
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ import { CommandContext, CommandDeclaration, CommandRuntime } from '@joplin/lib/
import Setting from '@joplin/lib/models/Setting';
import { _ } from '@joplin/lib/locale';

export const NOTES_SORT_ORDER_SWITCH = 'notesSortOrderSwitch';
export const SETTING_FIELD = 'notes.sortOrder.field';
export const SETTING_REVERSE = 'notes.sortOrder.reverse';
export const SETTING_PER_FIELD_REVERSAL_ENABLED = 'notes.perFieldReversalEnabled';
export const SETTING_PER_FIELD_REVERSE = 'notes.perFieldReverse';

let fields: string[] = null;
let perFieldReverse: { [field: string]: boolean } = null;

export const notesSortOrderFieldArray = (): string[] => {
// The order of the fields is strictly determinate.
if (fields == null) {
fields = Setting.enumOptionValues(SETTING_FIELD).sort().reverse();
fields = Setting.enumOptionValues('notes.sortOrder.field').sort().reverse();
}
return fields;
};
Expand All @@ -30,7 +24,7 @@ export const notesSortOrderNextField = (currentField: string) => {
};

export const declaration: CommandDeclaration = {
name: NOTES_SORT_ORDER_SWITCH,
name: 'notesSortOrderSwitch',
label: () => _('Switch sort order'),
parentLabel: () => _('Notes'),
};
Expand All @@ -52,12 +46,12 @@ export const runtime = (): CommandRuntime => {
nextField = field[0];
nextReverse = field[1];
}
const currentField = Setting.value(SETTING_FIELD);
const currentReverse = Setting.value(SETTING_REVERSE);
const enabled = Setting.value(SETTING_PER_FIELD_REVERSAL_ENABLED);
const currentField = Setting.value('notes.sortOrder.field');
const currentReverse = Setting.value('notes.sortOrder.reverse');
const enabled = Setting.value('notes.perFieldReversalEnabled');
if (enabled) {
if (perFieldReverse === null) {
perFieldReverse = { ...Setting.value(SETTING_PER_FIELD_REVERSE) };
perFieldReverse = { ...Setting.value('notes.perFieldReverse') };
}
}
if (typeof field === 'undefined') {
Expand All @@ -76,17 +70,17 @@ export const runtime = (): CommandRuntime => {
}
}
if (currentField !== nextField) {
Setting.setValue(SETTING_FIELD, nextField);
Setting.setValue('notes.sortOrder.field', nextField);
}
if (currentReverse !== nextReverse) {
Setting.setValue(SETTING_REVERSE, nextReverse);
Setting.setValue('notes.sortOrder.reverse', nextReverse);
}
if (enabled) {
// nextField is sane here.
nextReverse = !!nextReverse;
if (perFieldReverse[nextField] !== nextReverse) {
perFieldReverse[nextField] = nextReverse;
Setting.setValue(SETTING_PER_FIELD_REVERSE, { ...perFieldReverse });
Setting.setValue('notes.perFieldReverse', { ...perFieldReverse });
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import CommandService, { CommandContext, CommandDeclaration, CommandRuntime } from '@joplin/lib/services/CommandService';
import Setting from '@joplin/lib/models/Setting';
import { _ } from '@joplin/lib/locale';
import { NOTES_SORT_ORDER_SWITCH, SETTING_REVERSE } from './notesSortOrderSwitch';

export const NOTES_SORT_ORDER_TOGGLE_REVERSE = 'notesSortOrderToggleReverse';

export const declaration: CommandDeclaration = {
name: NOTES_SORT_ORDER_TOGGLE_REVERSE,
label: () => Setting.settingMetadata(SETTING_REVERSE).label(),
name: 'notesSortOrderToggleReverse',
label: () => Setting.settingMetadata('notes.sortOrder.reverse').label(),
parentLabel: () => _('Notes'),
};

export const runtime = (): CommandRuntime => {
return {
execute: async (_context: CommandContext) => {
const reverse = Setting.value(SETTING_REVERSE);
return CommandService.instance().execute(NOTES_SORT_ORDER_SWITCH, undefined, !reverse);
const reverse = Setting.value('notes.sortOrder.reverse');
return CommandService.instance().execute('notesSortOrderSwitch', undefined, !reverse);
},
};
};
52 changes: 14 additions & 38 deletions packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ 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, NOTES_SORT_ORDER_SWITCH, SETTING_FIELD, SETTING_REVERSE, SETTING_PER_FIELD_REVERSAL_ENABLED,
} from './notesSortOrderSwitch';
import { notesSortOrderFieldArray } from './notesSortOrderSwitch';
import { _ } from '@joplin/lib/locale';


export const PER_FOLDER_SORT_ORDER = 'perFolderSortOrder';
export const SETTING_PER_FOLDER_SORT_ORDER_ENABLED = 'notes.perFolderSortOrderEnabled';

let previousFolderId: string = null;
let ownSortOrders: { [key: string]: any } = null;
const sharedSortOrder: { [key: string]: any } = {
Expand All @@ -32,7 +26,7 @@ export function hasOwnSortOrder(folderId: string) {
}

export const declaration: CommandDeclaration = {
name: PER_FOLDER_SORT_ORDER,
name: 'perFolderSortOrder',
label: () => _('Toggle own sort order'),
};

Expand Down Expand Up @@ -63,11 +57,11 @@ export const runtime = (): CommandRuntime => {
if (newOwn) {
let field: string, reverse: boolean;
if (!hasOwnSortOrder(selectedId)) {
field = getCurrentField();
reverse = getCurrentReverse();
field = Setting.value('notes.sortOrder.field');
reverse = Setting.value('notes.sortOrder.reverse');
} else {
field = sharedSortOrder.field;
if (perFieldReversalEnabled()) {
if (Setting.value('notes.perFieldReversalEnabled')) {
reverse = sharedSortOrder[field];
} else {
reverse = sharedSortOrder.reverse;
Expand All @@ -85,8 +79,8 @@ function onFolderSelectionMayChange() {
const selectedId = getSelectedFolderId();
if (previousFolderId === null) previousFolderId = selectedId;
if (previousFolderId === selectedId) return;
const field = getCurrentField();
const reverse = getCurrentReverse();
const field = Setting.value('notes.sortOrder.field');
const reverse = Setting.value('notes.sortOrder.reverse');
const previousFolderHasOwnSortOrder = hasOwnSortOrder(previousFolderId);
if (previousFolderHasOwnSortOrder) {
setOwnSortOrder(previousFolderId, field, reverse);
Expand All @@ -102,15 +96,13 @@ function onFolderSelectionMayChange() {
} else {
return;
}
if (perFolderSortOrderEnabled()) {
if (Setting.value('notes.perFolderSortOrderEnabled')) {
if (next.field != field || next.reverse != reverse) {
void CommandService.instance().execute(NOTES_SORT_ORDER_SWITCH, next.field, next.reverse);
void CommandService.instance().execute('notesSortOrderSwitch', next.field, next.reverse);
}
}
}

const OWN_SORT_ORDERS = 'notes.perFolderSortOrders';
const SHARED_SORT_ORDER = 'notes.sharedSortOrder';
const SUFFIX_FIELD = '$field';
const SUFFIX_REVERSE = '$reverse';

Expand All @@ -123,22 +115,6 @@ function getSelectedFolderId(state?: State): string {
}
}

function getCurrentField(): string {
return Setting.value(SETTING_FIELD);
}

function getCurrentReverse(): boolean {
return Setting.value(SETTING_REVERSE);
}

function perFieldReversalEnabled(): boolean {
return Setting.value(SETTING_PER_FIELD_REVERSAL_ENABLED);
}

function perFolderSortOrderEnabled(): boolean {
return Setting.value(SETTING_PER_FOLDER_SORT_ORDER_ENABLED);
}

function getOwnSortOrder(folderId: string) {
const field = ownSortOrders[folderId + SUFFIX_FIELD] as string;
const reverse = ownSortOrders[folderId + SUFFIX_REVERSE] as boolean;
Expand All @@ -157,7 +133,7 @@ function setOwnSortOrder(folderId: string, field: string, reverse: boolean) {
dirty = true;
}
if (dirty) {
Setting.setValue(OWN_SORT_ORDERS, { ...ownSortOrders });
Setting.setValue('notes.perFolderSortOrders', { ...ownSortOrders });
}
}

Expand All @@ -172,17 +148,17 @@ function deleteOwnSortOrder(folderId: string) {
dirty = true;
}
if (dirty) {
Setting.setValue(OWN_SORT_ORDERS, { ...ownSortOrders });
Setting.setValue('notes.perFolderSortOrders', { ...ownSortOrders });
}
}

function loadOwnSortOrders() {
ownSortOrders = { ...Setting.value(OWN_SORT_ORDERS) };
ownSortOrders = { ...Setting.value('notes.perFolderSortOrders') };
}

function loadSharedSortOrder() {
const validFields = notesSortOrderFieldArray();
const value = Setting.value(SHARED_SORT_ORDER);
const value = Setting.value('notes.sharedSortOrder');
for (const key in sharedSortOrder) {
if (value.hasOwnProperty(key)) {
if (key !== 'field' || validFields.includes(value.field)) {
Expand All @@ -207,6 +183,6 @@ function setSharedSortOrder(field: string, reverse: boolean) {
dirty = true;
}
if (dirty) {
Setting.setValue(SHARED_SORT_ORDER, { ...sharedSortOrder });
Setting.setValue('notes.sharedSortOrder', { ...sharedSortOrder });
}
}
18 changes: 8 additions & 10 deletions packages/app-desktop/gui/NoteListControls/NoteListControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import CommandService from '@joplin/lib/services/CommandService';
import { runtime as focusSearchRuntime } from './commands/focusSearch';
import Setting from '@joplin/lib/models/Setting';
import Note from '@joplin/lib/models/Note';
import { notesSortOrderNextField, SETTING_FIELD, SETTING_REVERSE, NOTES_SORT_ORDER_SWITCH }
from '../MainScreen/commands/notesSortOrderSwitch';
import { NOTES_SORT_ORDER_TOGGLE_REVERSE } from '../MainScreen/commands/notesSortOrderToggleReverse';
import { notesSortOrderNextField } from '../MainScreen/commands/notesSortOrderSwitch';
import app from '../../app';
const styled = require('styled-components').default;

Expand Down Expand Up @@ -70,23 +68,23 @@ export default function NoteListControls(props: Props) {
}

function onSortOrderFieldButtonClick() {
void CommandService.instance().execute(NOTES_SORT_ORDER_SWITCH);
void CommandService.instance().execute('notesSortOrderSwitch');
}

function onSortOrderReverseButtonClick() {
void CommandService.instance().execute(NOTES_SORT_ORDER_TOGGLE_REVERSE);
void CommandService.instance().execute('notesSortOrderToggleReverse');
}

function sortOrderFieldTooltip() {
const term1 = CommandService.instance().label(NOTES_SORT_ORDER_SWITCH);
const field = Setting.value(SETTING_FIELD);
const term1 = CommandService.instance().label('notesSortOrderSwitch');
const field = Setting.value('notes.sortOrder.field');
const term2 = Note.fieldToLabel(field);
const term3 = Note.fieldToLabel(notesSortOrderNextField(field));
return `${term1}:\n ${term2} -> ${term3}`;
}

function sortOrderFieldIcon() {
const field = Setting.value(SETTING_FIELD);
const field = Setting.value('notes.sortOrder.field');
const iconMap: any = {
user_updated_time: 'far fa-calendar-alt',
user_created_time: 'far fa-calendar-plus',
Expand All @@ -97,7 +95,7 @@ export default function NoteListControls(props: Props) {
}

function sortOrderReverseIcon() {
return Setting.value(SETTING_REVERSE) ? 'fas fa-long-arrow-alt-up' : 'fas fa-long-arrow-alt-down';
return Setting.value('notes.sortOrder.reverse') ? 'fas fa-long-arrow-alt-up' : 'fas fa-long-arrow-alt-down';
}

function sortOrderButtonsVisible() {
Expand Down Expand Up @@ -125,7 +123,7 @@ export default function NoteListControls(props: Props) {
{soButtonsVisible &&
<StyledPairButtonR
className="sort-order-reverse-button"
tooltip={CommandService.instance().label(NOTES_SORT_ORDER_TOGGLE_REVERSE)}
tooltip={CommandService.instance().label('notesSortOrderToggleReverse')}
iconName={sortOrderReverseIcon()}
level={ButtonLevel.Secondary}
onClick={onSortOrderReverseButtonClick}
Expand Down

0 comments on commit 4cdba95

Please sign in to comment.