Skip to content

Commit

Permalink
Notebook -> Folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ken1kob committed Sep 19, 2021
1 parent 91de06d commit c723ebe
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ packages/app-desktop/gui/MainScreen/commands/openNote.js.map
packages/app-desktop/gui/MainScreen/commands/openTag.d.ts
packages/app-desktop/gui/MainScreen/commands/openTag.js
packages/app-desktop/gui/MainScreen/commands/openTag.js.map
packages/app-desktop/gui/MainScreen/commands/perNotebookSortOrder.d.ts
packages/app-desktop/gui/MainScreen/commands/perNotebookSortOrder.js
packages/app-desktop/gui/MainScreen/commands/perNotebookSortOrder.js.map
packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.d.ts
packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.js
packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.js.map
packages/app-desktop/gui/MainScreen/commands/print.d.ts
packages/app-desktop/gui/MainScreen/commands/print.js
packages/app-desktop/gui/MainScreen/commands/print.js.map
Expand Down
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ packages/app-desktop/gui/MainScreen/commands/openNote.js.map
packages/app-desktop/gui/MainScreen/commands/openTag.d.ts
packages/app-desktop/gui/MainScreen/commands/openTag.js
packages/app-desktop/gui/MainScreen/commands/openTag.js.map
packages/app-desktop/gui/MainScreen/commands/perNotebookSortOrder.d.ts
packages/app-desktop/gui/MainScreen/commands/perNotebookSortOrder.js
packages/app-desktop/gui/MainScreen/commands/perNotebookSortOrder.js.map
packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.d.ts
packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.js
packages/app-desktop/gui/MainScreen/commands/perFolderSortOrder.js.map
packages/app-desktop/gui/MainScreen/commands/print.d.ts
packages/app-desktop/gui/MainScreen/commands/print.js
packages/app-desktop/gui/MainScreen/commands/print.js.map
Expand Down
4 changes: 2 additions & 2 deletions packages/app-desktop/gui/MainScreen/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as notesSortOrderToggleReverse from './notesSortOrderToggleReverse';
import * as openFolder from './openFolder';
import * as openNote from './openNote';
import * as openTag from './openTag';
import * as perNotebookSortOrder from './perNotebookSortOrder';
import * as perFolderSortOrder from './perFolderSortOrder';
import * as print from './print';
import * as renameFolder from './renameFolder';
import * as renameTag from './renameTag';
Expand Down Expand Up @@ -49,7 +49,7 @@ const index:any[] = [
openFolder,
openNote,
openTag,
perNotebookSortOrder,
perFolderSortOrder,
print,
renameFolder,
renameTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
import { _ } from '@joplin/lib/locale';


export const PER_NOTEBOOK_SORT_ORDER = 'perNotebookSortOrder';
export const SETTING_PER_NOTEBOOK_SORT_ORDER_ENABLED = 'notes.perNotebookSortOrderEnabled';
export const PER_FOLDER_SORT_ORDER = 'perFolderSortOrder';
export const SETTING_PER_FOLDER_SORT_ORDER_ENABLED = 'notes.perFolderSortOrderEnabled';

let previousNotebookId: string = null;
let previousFolderId: string = null;
let ownSortOrders: { [key: string]: any } = null;
const sharedSortOrder: { [key: string]: any } = {
field: 'user_updated_time',
Expand All @@ -23,33 +23,33 @@ const sharedSortOrder: { [key: string]: any } = {
order: false,
};

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

export function hasOwnSortOrder(notebookId: string) {
return notebookId && ownSortOrders && ownSortOrders.hasOwnProperty(notebookId + SUFFIX_FIELD);
export function hasOwnSortOrder(folderId: string) {
return folderId && ownSortOrders && ownSortOrders.hasOwnProperty(folderId + SUFFIX_FIELD);
}

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

export const runtime = (): CommandRuntime => {
loadOwnSortOrders();
loadSharedSortOrder();
eventManager.appStateOn('notesParentType', onNotebookSelectionMayChange);
eventManager.appStateOn('selectedFolderId', onNotebookSelectionMayChange);
eventManager.appStateOn('notesParentType', onFolderSelectionMayChange);
eventManager.appStateOn('selectedFolderId', onFolderSelectionMayChange);

return {
enabledCondition: 'oneFolderSelected',

execute: async (context: CommandContext, notebookId?: string, own?: boolean) => {
let targetId = notebookId;
const selectedId = getSelectedNotebookId(context.state);
execute: async (context: CommandContext, folderId?: string, own?: boolean) => {
let targetId = folderId;
const selectedId = getSelectedFolderId(context.state);
if (!targetId) {
targetId = selectedId; // default: selected notebook
targetId = selectedId; // default: selected folder
if (!targetId) return;
}
const targetOwn = hasOwnSortOrder(targetId);
Expand Down Expand Up @@ -81,40 +81,40 @@ export const runtime = (): CommandRuntime => {
};
};

function onNotebookSelectionMayChange() {
const selectedId = getSelectedNotebookId();
if (previousNotebookId === null) previousNotebookId = selectedId;
if (previousNotebookId === selectedId) return;
function onFolderSelectionMayChange() {
const selectedId = getSelectedFolderId();
if (previousFolderId === null) previousFolderId = selectedId;
if (previousFolderId === selectedId) return;
const field = getCurrentField();
const reverse = getCurrentReverse();
const previousNotebookHasOwnSortOrder = hasOwnSortOrder(previousNotebookId);
if (previousNotebookHasOwnSortOrder) {
setOwnSortOrder(previousNotebookId, field, reverse);
const previousFolderHasOwnSortOrder = hasOwnSortOrder(previousFolderId);
if (previousFolderHasOwnSortOrder) {
setOwnSortOrder(previousFolderId, field, reverse);
} else {
setSharedSortOrder(field, reverse);
}
previousNotebookId = selectedId;
previousFolderId = selectedId;
let next;
if (hasOwnSortOrder(selectedId)) {
next = getOwnSortOrder(selectedId);
} else if (previousNotebookHasOwnSortOrder) {
} else if (previousFolderHasOwnSortOrder) {
next = sharedSortOrder;
} else {
return;
}
if (perNotebookSortOrderEnabled()) {
if (perFolderSortOrderEnabled()) {
if (next.field != field || next.reverse != reverse) {
void CommandService.instance().execute(NOTES_SORT_ORDER_SWITCH, next.field, next.reverse);
}
}
}

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

function getSelectedNotebookId(state?: State): string {
function getSelectedFolderId(state?: State): string {
const s = state ? state : app().store().getState();
if (s.notesParentType === 'Folder') {
return s.selectedFolderId;
Expand All @@ -135,40 +135,40 @@ function perFieldReversalEnabled(): boolean {
return Setting.value(SETTING_PER_FIELD_REVERSAL_ENABLED);
}

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

function getOwnSortOrder(notebookId: string) {
const field = ownSortOrders[notebookId + SUFFIX_FIELD] as string;
const reverse = ownSortOrders[notebookId + SUFFIX_REVERSE] as boolean;
function getOwnSortOrder(folderId: string) {
const field = ownSortOrders[folderId + SUFFIX_FIELD] as string;
const reverse = ownSortOrders[folderId + SUFFIX_REVERSE] as boolean;
return { field, reverse };
}

function setOwnSortOrder(notebookId: string, field: string, reverse: boolean) {
const old = getOwnSortOrder(notebookId);
function setOwnSortOrder(folderId: string, field: string, reverse: boolean) {
const old = getOwnSortOrder(folderId);
let dirty = false;
if (old.field !== field) {
ownSortOrders[notebookId + SUFFIX_FIELD] = field;
ownSortOrders[folderId + SUFFIX_FIELD] = field;
dirty = true;
}
if (old.reverse !== reverse) {
ownSortOrders[notebookId + SUFFIX_REVERSE] = reverse;
ownSortOrders[folderId + SUFFIX_REVERSE] = reverse;
dirty = true;
}
if (dirty) {
Setting.setValue(OWN_SORT_ORDERS, { ...ownSortOrders });
}
}

function deleteOwnSortOrder(notebookId: string) {
function deleteOwnSortOrder(folderId: string) {
let dirty = false;
if (ownSortOrders.hasOwnProperty(notebookId + SUFFIX_FIELD)) {
delete ownSortOrders[notebookId + SUFFIX_FIELD];
if (ownSortOrders.hasOwnProperty(folderId + SUFFIX_FIELD)) {
delete ownSortOrders[folderId + SUFFIX_FIELD];
dirty = true;
}
if (ownSortOrders.hasOwnProperty(notebookId + SUFFIX_REVERSE)) {
delete ownSortOrders[notebookId + SUFFIX_REVERSE];
if (ownSortOrders.hasOwnProperty(folderId + SUFFIX_REVERSE)) {
delete ownSortOrders[folderId + SUFFIX_REVERSE];
dirty = true;
}
if (dirty) {
Expand Down
6 changes: 3 additions & 3 deletions packages/app-desktop/gui/Sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Logger from '@joplin/lib/Logger';
import { FolderEntity } from '@joplin/lib/services/database/types';
import stateToWhenClauseContext from '../../services/commands/stateToWhenClauseContext';
import { store } from '@joplin/lib/reducer';
import { hasOwnSortOrder } from '../MainScreen/commands/perNotebookSortOrder';
import { hasOwnSortOrder } from '../MainScreen/commands/perFolderSortOrder';
const { connect } = require('react-redux');
const shared = require('@joplin/lib/components/shared/side-menu-shared.js');
const { themeStyle } = require('@joplin/lib/theme');
Expand Down Expand Up @@ -325,9 +325,9 @@ class SidebarComponent extends React.Component<Props, State> {
submenu: exportMenu,
})
);
if (Setting.value('notes.perNotebookSortOrderEnabled')) {
if (Setting.value('notes.perFolderSortOrderEnabled')) {
menu.append(new MenuItem({
...menuUtils.commandToStatefulMenuItem('perNotebookSortOrder', itemId),
...menuUtils.commandToStatefulMenuItem('perFolderSortOrder', itemId),
type: 'checkbox',
checked: hasOwnSortOrder(itemId),
}));
Expand Down
2 changes: 1 addition & 1 deletion packages/app-desktop/gui/menuCommandNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function() {
'notesSortOrderSwitch',
'notesSortOrderToggleReverse',
'openProfileDirectory',
'perNotebookSortOrder',
'perFolderSortOrder',
'print',
'setTags',
'showLocalSearch',
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/models/Setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ class Setting extends BaseModel {
public: false,
appTypes: [AppType.Cli, AppType.Desktop],
},
'notes.perNotebookSortOrderEnabled': {
'notes.perFolderSortOrderEnabled': {
value: true,
type: SettingItemType.Bool,
storage: SettingStorage.File,
Expand All @@ -809,7 +809,7 @@ class Setting extends BaseModel {
label: () => _('Enable per-notebook sort order'),
appTypes: [AppType.Cli, AppType.Desktop],
},
'notes.perNotebookSortOrders': {
'notes.perFolderSortOrders': {
value: {},
type: SettingItemType.Object,
storage: SettingStorage.File,
Expand Down

0 comments on commit c723ebe

Please sign in to comment.