From 4cdba95ba4aa4f22ad7f442ba0bfc573d89ea87e Mon Sep 17 00:00:00 2001 From: Kenichi Kobayashi Date: Mon, 20 Sep 2021 06:53:14 +0900 Subject: [PATCH] remove string constants and internal wrapping functions --- .../commands/notesSortOrderSwitch.ts | 24 ++++----- .../commands/notesSortOrderToggleReverse.ts | 11 ++-- .../MainScreen/commands/perFolderSortOrder.ts | 52 +++++-------------- .../gui/NoteListControls/NoteListControls.tsx | 18 +++---- 4 files changed, 35 insertions(+), 70 deletions(-) diff --git a/packages/app-desktop/gui/MainScreen/commands/notesSortOrderSwitch.ts b/packages/app-desktop/gui/MainScreen/commands/notesSortOrderSwitch.ts index 9c258eabe48..23b92bef923 100644 --- a/packages/app-desktop/gui/MainScreen/commands/notesSortOrderSwitch.ts +++ b/packages/app-desktop/gui/MainScreen/commands/notesSortOrderSwitch.ts @@ -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; }; @@ -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'), }; @@ -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') { @@ -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 }); } } }, diff --git a/packages/app-desktop/gui/MainScreen/commands/notesSortOrderToggleReverse.ts b/packages/app-desktop/gui/MainScreen/commands/notesSortOrderToggleReverse.ts index c9eaeb220c7..a1e34d8c4d2 100644 --- a/packages/app-desktop/gui/MainScreen/commands/notesSortOrderToggleReverse.ts +++ b/packages/app-desktop/gui/MainScreen/commands/notesSortOrderToggleReverse.ts @@ -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); }, }; }; diff --git a/packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.ts b/packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.ts index 15393c3790f..c1a10980894 100644 --- a/packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.ts +++ b/packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.ts @@ -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 } = { @@ -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'), }; @@ -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; @@ -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); @@ -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'; @@ -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; @@ -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 }); } } @@ -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)) { @@ -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 }); } } diff --git a/packages/app-desktop/gui/NoteListControls/NoteListControls.tsx b/packages/app-desktop/gui/NoteListControls/NoteListControls.tsx index 9f0d00a09ce..1eb3d27908c 100644 --- a/packages/app-desktop/gui/NoteListControls/NoteListControls.tsx +++ b/packages/app-desktop/gui/NoteListControls/NoteListControls.tsx @@ -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; @@ -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', @@ -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() { @@ -125,7 +123,7 @@ export default function NoteListControls(props: Props) { {soButtonsVisible &&