Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile: Fixes #11539: Fix missing "Insert Time" button #11542

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const builtInCommandNames = [
EditorCommandType.IndentLess,
EditorCommandType.IndentMore,
'-',
'insertDateTime',
'-',
EditorCommandType.EditLink,
'setTags',
EditorCommandType.ToggleSearch,
Expand Down
19 changes: 12 additions & 7 deletions packages/app-mobile/components/screens/Note/Note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -721,20 +725,21 @@ class NoteScreenComponent extends BaseScreenComponent<ComponentProps, State> 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()) {
Expand All @@ -747,7 +752,7 @@ class NoteScreenComponent extends BaseScreenComponent<ComponentProps, State> imp
}
}
} else {
newNote.body += `\n${text}`;
newNote.body += `${separator}${text}`;
}

this.setState({ note: newNote });
Expand Down Expand Up @@ -830,7 +835,7 @@ class NoteScreenComponent extends BaseScreenComponent<ComponentProps, State> 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);

Expand All @@ -850,7 +855,7 @@ class NoteScreenComponent extends BaseScreenComponent<ComponentProps, State> imp

private cameraView_onInsertBarcode = (data: string) => {
this.setState({ showCamera: false });
void this.insertText(data);
void this.insertText(data, { newLine: true });
};

private cameraView_onCancel() {
Expand Down
2 changes: 2 additions & 0 deletions packages/app-mobile/components/screens/Note/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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,
];
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
};
};
Loading