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

Insert today's date (shortcut: ctrl+;) #1929

Merged
merged 5 commits into from
Oct 2, 2024
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 quadratic-client/src/app/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export enum Action {
SaveInlineEditorMoveLeft = 'save_inline_editor_move_left',
RemoveInsertedCells = 'remove_inserted_cells',
TriggerCell = 'trigger_cell',
InsertToday = 'insert_today',
InsertColumnLeft = 'insert_column_left',
InsertColumnRight = 'insert_column_right',
InsertRowAbove = 'insert_row_above',
Expand Down
21 changes: 20 additions & 1 deletion quadratic-client/src/app/actions/insertActionsSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { pixiAppSettings } from '@/app/gridGL/pixiApp/PixiAppSettings';
import { insertCellRef } from '@/app/ui/menus/CodeEditor/insertCellRef';
import { SNIPPET_JS_API, SNIPPET_JS_CHART } from '@/app/ui/menus/CodeEditor/snippetsJS';
import { SNIPPET_PY_API, SNIPPET_PY_CHART } from '@/app/ui/menus/CodeEditor/snippetsPY';
import { ArrowDropDownCircleIcon, CheckBoxIcon, DataValidationsIcon, SheetIcon } from '@/shared/components/Icons';
import {
ArrowDropDownCircleIcon,
CheckBoxIcon,
DataValidationsIcon,
SheetIcon,
FormatDateTimeIcon,
} from '@/shared/components/Icons';
import { quadraticCore } from '../web-workers/quadraticCore/quadraticCore';

type InsertActionSpec = Pick<
ActionSpecRecord,
Expand All @@ -22,6 +29,7 @@ type InsertActionSpec = Pick<
| Action.ToggleDataValidation
| Action.InsertCellReference
| Action.RemoveInsertedCells
| Action.InsertToday
>;

export const insertActionsSpec: InsertActionSpec = {
Expand Down Expand Up @@ -232,4 +240,15 @@ export const insertActionsSpec: InsertActionSpec = {
// TODO(ayush): add this when refactoring shortcuts to use action specs
},
},
[Action.InsertToday]: {
label: "Insert today's date",
Icon: FormatDateTimeIcon,
run: () => {
const sheet = sheets.sheet;
const cursor = sheet.cursor;
const today = new Date();
const formattedDate = `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a date utilities file for stuff like this.

quadraticCore.setCellValue(sheet.id, cursor.cursorPosition.x, cursor.cursorPosition.y, formattedDate);
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { pixiApp } from '@/app/gridGL/pixiApp/PixiApp';
import { pixiAppSettings } from '@/app/gridGL/pixiApp/PixiAppSettings';
import { matchShortcut } from '@/app/helpers/keyboardShortcuts.js';
import { quadraticCore } from '@/app/web-workers/quadraticCore/quadraticCore';
import { inlineEditorEvents } from './inlineEditorEvents';

class InlineEditorKeyboard {
escapeBackspacePressed = false;
Expand Down Expand Up @@ -321,7 +322,13 @@ class InlineEditorKeyboard {
language: pixiAppSettings.codeEditorState.codeCell.language,
},
});
} else if (matchShortcut(Action.InsertToday, e)) {
const today = new Date();
// todo: this should be based on locale (maybe?)
const formattedDate = `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`;
inlineEditorEvents.emit('replaceText', formattedDate, false);
}

// Fallback for all other keys (used to end cursorIsMoving and return
// control to the formula box)
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Action } from '@/app/actions/actions';
import { EditorInteractionState } from '@/app/atoms/editorInteractionStateAtom';
import { sheets } from '@/app/grid/controller/Sheets';
import { inlineEditorHandler } from '@/app/gridGL/HTMLGrid/inlineEditor/inlineEditorHandler';
import { keyboardCell } from '@/app/gridGL/interaction/keyboard/keyboardCell';
import { keyboardClipboard } from '@/app/gridGL/interaction/keyboard/keyboardClipboard';
Expand All @@ -11,6 +14,13 @@ import { keyboardUndoRedo } from '@/app/gridGL/interaction/keyboard/keyboardUndo
import { keyboardViewport } from '@/app/gridGL/interaction/keyboard/keyboardViewport';
import { pixiAppSettings } from '@/app/gridGL/pixiApp/PixiAppSettings';
import { Size } from '@/app/gridGL/types/size';
import { matchShortcut } from '@/app/helpers/keyboardShortcuts';
import { quadraticCore } from '@/app/web-workers/quadraticCore/quadraticCore';

export interface IProps {
editorInteractionState: EditorInteractionState;
setEditorInteractionState: React.Dispatch<React.SetStateAction<EditorInteractionState>>;
}

export const pixiKeyboardCanvasProps: { headerSize: Size } = { headerSize: { width: 0, height: 0 } };

Expand All @@ -36,6 +46,16 @@ export const useKeyboard = (): {
return;
}

// todo: we need to reorganize this so we can handle shortcuts in keyboardCell when ctrl or meta is pressed
// insert today's date if the inline editor is not open
if (matchShortcut(Action.InsertToday, event)) {
const sheet = sheets.sheet;
const cursor = sheet.cursor;
const today = new Date();
const formattedDate = `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here. I've seen this repeat several times, ripe to be refactored into a reusable function.

quadraticCore.setCellValue(sheet.id, cursor.cursorPosition.x, cursor.cursorPosition.y, formattedDate);
}

// Prevent these commands if "command" key is being pressed
if (event.metaKey || event.ctrlKey) {
return;
Expand Down
7 changes: 7 additions & 0 deletions quadratic-client/src/app/keyboard/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,11 @@ export const defaultShortcuts: ActionShortcut = {
mac: [[Keys.Space]],
windows: [[Keys.Space]],
},
[Action.InsertToday]: {
mac: [
[MacModifiers.Ctrl, Keys.Semicolon],
[MacModifiers.Cmd, Keys.Semicolon],
],
windows: [[WindowsModifiers.Ctrl, Keys.Semicolon]],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const data: CommandGroup = {
Action.ShowGoToMenu,
Action.FindInCurrentSheet,
Action.FindInAllSheets,
Action.InsertToday,
{
label: 'Copy selection as PNG',
isAvailable: () => fullClipboardSupport(),
Expand Down
Loading