feat: Add patterns to insert current date and time into doc (#3309)

* feat: Add patterns to insert current date and time into doc

* Add commands to title input too

* lint: Remove console.log
This commit is contained in:
Tom Moor
2022-03-31 19:51:55 -07:00
committed by GitHub
parent 4c0cd3d893
commit c66aca063e
6 changed files with 165 additions and 17 deletions

View File

@@ -4,8 +4,13 @@ import styled from "styled-components";
import breakpoint from "styled-components-breakpoint";
import { MAX_TITLE_LENGTH } from "@shared/constants";
import { light } from "@shared/theme";
import {
getCurrentDateAsString,
getCurrentDateTimeAsString,
getCurrentTimeAsString,
} from "@shared/utils/date";
import Document from "~/models/Document";
import ContentEditable from "~/components/ContentEditable";
import ContentEditable, { RefHandle } from "~/components/ContentEditable";
import Star, { AnimatedStar } from "~/components/Star";
import useEmojiWidth from "~/hooks/useEmojiWidth";
import usePolicy from "~/hooks/usePolicy";
@@ -42,7 +47,7 @@ const EditableTitle = React.forwardRef(
starrable,
placeholder,
}: Props,
ref: React.RefObject<HTMLSpanElement>
ref: React.RefObject<RefHandle>
) => {
const can = usePolicy(document.id);
const normalizedTitle =
@@ -92,6 +97,24 @@ const EditableTitle = React.forwardRef(
[onGoToNextInput, onSave]
);
const handleChange = React.useCallback(
(text: string) => {
if (/\/date\s$/.test(text)) {
onChange(getCurrentDateAsString());
ref.current?.focusAtEnd();
} else if (/\/time$/.test(text)) {
onChange(getCurrentTimeAsString());
ref.current?.focusAtEnd();
} else if (/\/datetime$/.test(text)) {
onChange(getCurrentDateTimeAsString());
ref.current?.focusAtEnd();
} else {
onChange(text);
}
},
[ref, onChange]
);
const emojiWidth = useEmojiWidth(document.emoji, {
fontSize,
lineHeight,
@@ -100,7 +123,7 @@ const EditableTitle = React.forwardRef(
return (
<Title
onClick={handleClick}
onChange={onChange}
onChange={handleChange}
onKeyDown={handleKeyDown}
placeholder={placeholder}
value={normalizedTitle}