From c3510bf26b6ba8cd01bfb7fbf61285738a6dfb0b Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 9 Nov 2023 14:58:38 +0000 Subject: [PATCH] Desktop: Fixes #9149: Toolbar icons in view mode are partly not grayed out and can be used --- .eslintignore | 1 + .gitignore | 1 + .../editorCommandDeclarations.test.ts | 64 +++++++++++++++++++ .../NoteEditor/editorCommandDeclarations.ts | 12 +++- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.test.ts diff --git a/.eslintignore b/.eslintignore index 9219b416b..1d99d6b76 100644 --- a/.eslintignore +++ b/.eslintignore @@ -260,6 +260,7 @@ packages/app-desktop/gui/NoteEditor/commands/index.js packages/app-desktop/gui/NoteEditor/commands/pasteAsText.js packages/app-desktop/gui/NoteEditor/commands/showLocalSearch.js packages/app-desktop/gui/NoteEditor/commands/showRevisions.js +packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.test.js packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.js packages/app-desktop/gui/NoteEditor/styles/index.js packages/app-desktop/gui/NoteEditor/utils/clipboardUtils.test.js diff --git a/.gitignore b/.gitignore index a3e7dcd08..fdb5419a6 100644 --- a/.gitignore +++ b/.gitignore @@ -242,6 +242,7 @@ packages/app-desktop/gui/NoteEditor/commands/index.js packages/app-desktop/gui/NoteEditor/commands/pasteAsText.js packages/app-desktop/gui/NoteEditor/commands/showLocalSearch.js packages/app-desktop/gui/NoteEditor/commands/showRevisions.js +packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.test.js packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.js packages/app-desktop/gui/NoteEditor/styles/index.js packages/app-desktop/gui/NoteEditor/utils/clipboardUtils.test.js diff --git a/packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.test.ts b/packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.test.ts new file mode 100644 index 000000000..fce63e781 --- /dev/null +++ b/packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.test.ts @@ -0,0 +1,64 @@ +import WhenClause from '@joplin/lib/services/WhenClause'; +import { enabledCondition } from './editorCommandDeclarations'; + +const baseContext: Record = { + modalDialogVisible: false, + gotoAnythingVisible: false, + markdownEditorPaneVisible: true, + oneNoteSelected: true, + noteIsMarkdown: true, + noteIsReadOnly: false, + richTextEditorVisible: false, +}; + +describe('editorCommandDeclarations', () => { + + test.each([ + [ + {}, + true, + ], + [ + { + markdownEditorPaneVisible: false, + }, + false, + ], + [ + { + noteIsReadOnly: true, + }, + false, + ], + [ + // In the Markdown editor, but only the viewer is visible + { + markdownEditorPaneVisible: false, + richTextEditorVisible: false, + }, + false, + ], + [ + // In the Markdown editor, and the viewer is visible + { + markdownEditorPaneVisible: true, + richTextEditorVisible: false, + }, + true, + ], + [ + // In the RT editor + { + markdownEditorPaneVisible: false, + richTextEditorVisible: true, + }, + true, + ], + ])('should create the enabledCondition', (context: Record, expected: boolean) => { + const condition = enabledCondition('textBold'); + const wc = new WhenClause(condition); + const actual = wc.evaluate({ ...baseContext, ...context }); + expect(actual).toBe(expected); + }); + +}); diff --git a/packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.ts b/packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.ts index a076b5822..89e29d1dd 100644 --- a/packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.ts +++ b/packages/app-desktop/gui/NoteEditor/editorCommandDeclarations.ts @@ -9,7 +9,17 @@ const workWithHtmlNotes = [ export const enabledCondition = (commandName: string) => { const markdownEditorOnly = !Object.keys(joplinCommandToTinyMceCommands).includes(commandName); const noteMustBeMarkdown = !workWithHtmlNotes.includes(commandName); - return `(!modalDialogVisible || gotoAnythingVisible) ${markdownEditorOnly ? '&& markdownEditorPaneVisible' : ''} && oneNoteSelected ${noteMustBeMarkdown ? '&& noteIsMarkdown' : ''} && !noteIsReadOnly`; + + const output = [ + '!modalDialogVisible', + '!gotoAnythingVisible', + markdownEditorOnly ? 'markdownEditorPaneVisible' : '(markdownEditorPaneVisible || richTextEditorVisible)', + 'oneNoteSelected', + noteMustBeMarkdown ? 'noteIsMarkdown' : '', + '!noteIsReadOnly', + ]; + + return output.filter(c => !!c).join(' && '); }; const declarations: CommandDeclaration[] = [