From 422a4732f39a80aa17219199a681cd6043c345ac Mon Sep 17 00:00:00 2001 From: Henry Heino Date: Thu, 19 Dec 2024 09:05:33 -0800 Subject: [PATCH] Mobile: Fixes #11539: Fix missing "Insert Time" button --- .eslintignore | 1 + .gitignore | 1 + .../utils/allToolbarCommandNamesFromState.ts | 2 ++ .../components/screens/Note/Note.tsx | 19 +++++++++++------- .../components/screens/Note/commands/index.ts | 2 ++ .../screens/Note/commands/insertDateTime.ts | 20 +++++++++++++++++++ 6 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 packages/app-mobile/components/screens/Note/commands/insertDateTime.ts diff --git a/.eslintignore b/.eslintignore index 9714669e5..712648c71 100644 --- a/.eslintignore +++ b/.eslintignore @@ -754,6 +754,7 @@ packages/app-mobile/components/screens/Note/Note.js packages/app-mobile/components/screens/Note/commands/attachFile.js packages/app-mobile/components/screens/Note/commands/hideKeyboard.js packages/app-mobile/components/screens/Note/commands/index.js +packages/app-mobile/components/screens/Note/commands/insertDateTime.js packages/app-mobile/components/screens/Note/commands/setTags.js packages/app-mobile/components/screens/Note/commands/toggleVisiblePanes.js packages/app-mobile/components/screens/Note/types.js diff --git a/.gitignore b/.gitignore index 18154eb43..9edbe5ba4 100644 --- a/.gitignore +++ b/.gitignore @@ -730,6 +730,7 @@ packages/app-mobile/components/screens/Note/Note.js packages/app-mobile/components/screens/Note/commands/attachFile.js packages/app-mobile/components/screens/Note/commands/hideKeyboard.js packages/app-mobile/components/screens/Note/commands/index.js +packages/app-mobile/components/screens/Note/commands/insertDateTime.js packages/app-mobile/components/screens/Note/commands/setTags.js packages/app-mobile/components/screens/Note/commands/toggleVisiblePanes.js packages/app-mobile/components/screens/Note/types.js diff --git a/packages/app-mobile/components/EditorToolbar/utils/allToolbarCommandNamesFromState.ts b/packages/app-mobile/components/EditorToolbar/utils/allToolbarCommandNamesFromState.ts index dd0478895..e6b0d8806 100644 --- a/packages/app-mobile/components/EditorToolbar/utils/allToolbarCommandNamesFromState.ts +++ b/packages/app-mobile/components/EditorToolbar/utils/allToolbarCommandNamesFromState.ts @@ -23,6 +23,8 @@ const builtInCommandNames = [ EditorCommandType.IndentLess, EditorCommandType.IndentMore, '-', + 'insertDateTime', + '-', EditorCommandType.EditLink, 'setTags', EditorCommandType.ToggleSearch, diff --git a/packages/app-mobile/components/screens/Note/Note.tsx b/packages/app-mobile/components/screens/Note/Note.tsx index 3fc6ebcdc..9044e0f78 100644 --- a/packages/app-mobile/components/screens/Note/Note.tsx +++ b/packages/app-mobile/components/screens/Note/Note.tsx @@ -69,6 +69,10 @@ const emptyArray: any[] = []; const logger = Logger.create('screens/Note'); +interface InsertTextOptions { + newLine?: boolean; +} + interface Props extends BaseProps { provisionalNoteIds: string[]; dispatch: Dispatch; @@ -721,20 +725,21 @@ class NoteScreenComponent extends BaseScreenComponent imp return await saveOriginalImage(); } - private async insertText(text: string) { + private async insertText(text: string, { newLine = false }: InsertTextOptions = {}) { const newNote = { ...this.state.note }; + const separator = newLine ? '\n' : ''; if (this.state.mode === 'edit') { let newText = ''; if (this.selection) { - newText = `\n${text}\n`; + newText = `${separator}${text}${separator}`; const prefix = newNote.body.substring(0, this.selection.start); const suffix = newNote.body.substring(this.selection.end); newNote.body = `${prefix}${newText}${suffix}`; } else { - newText = `\n${text}`; - newNote.body = `${newNote.body}\n${newText}`; + newText = `${separator}${separator}${text}`; + newNote.body = `${newNote.body}${newText}`; } if (this.useEditorBeta()) { @@ -747,7 +752,7 @@ class NoteScreenComponent extends BaseScreenComponent imp } } } else { - newNote.body += `\n${text}`; + newNote.body += `${separator}${text}`; } this.setState({ note: newNote }); @@ -830,7 +835,7 @@ class NoteScreenComponent extends BaseScreenComponent imp resource = await Resource.save(resource, { isNew: true }); const resourceTag = Resource.markupTag(resource); - const newNote = await this.insertText(resourceTag); + const newNote = await this.insertText(resourceTag, { newLine: true }); void this.refreshResource(resource, newNote.body); @@ -850,7 +855,7 @@ class NoteScreenComponent extends BaseScreenComponent imp private cameraView_onInsertBarcode = (data: string) => { this.setState({ showCamera: false }); - void this.insertText(data); + void this.insertText(data, { newLine: true }); }; private cameraView_onCancel() { diff --git a/packages/app-mobile/components/screens/Note/commands/index.ts b/packages/app-mobile/components/screens/Note/commands/index.ts index 201282ce4..409da0dc1 100644 --- a/packages/app-mobile/components/screens/Note/commands/index.ts +++ b/packages/app-mobile/components/screens/Note/commands/index.ts @@ -1,12 +1,14 @@ // AUTO-GENERATED using `gulp buildScriptIndexes` import * as attachFile from './attachFile'; import * as hideKeyboard from './hideKeyboard'; +import * as insertDateTime from './insertDateTime'; import * as setTags from './setTags'; import * as toggleVisiblePanes from './toggleVisiblePanes'; const index: any[] = [ attachFile, hideKeyboard, + insertDateTime, setTags, toggleVisiblePanes, ]; diff --git a/packages/app-mobile/components/screens/Note/commands/insertDateTime.ts b/packages/app-mobile/components/screens/Note/commands/insertDateTime.ts new file mode 100644 index 000000000..fa85daa8b --- /dev/null +++ b/packages/app-mobile/components/screens/Note/commands/insertDateTime.ts @@ -0,0 +1,20 @@ +import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService'; +import { _ } from '@joplin/lib/locale'; +import { CommandRuntimeProps } from '../types'; +import time from '@joplin/lib/time'; + +export const declaration: CommandDeclaration = { + name: 'insertDateTime', + label: () => _('Insert time'), + iconName: 'material calendar-plus', +}; + +export const runtime = (props: CommandRuntimeProps): CommandRuntime => { + return { + execute: async (_context: CommandContext) => { + props.insertText(time.formatDateToLocal(new Date())); + }, + + enabledCondition: '!noteIsReadOnly', + }; +};