Skip to content

Commit 6c06e7a

Browse files
committed
Add escape key functionality to quit editing a spark
1 parent 9af51ab commit 6c06e7a

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/common/components/TextInput/TextInput.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import "./TextInput.css";
44
import { parseSpark } from "../../../scripts/utils/stringUtils";
55
import { isUserSelectingTag } from "./TagList/TagList";
66
import { isUserSelectingExtension } from "./SparkExtensionList/SparkExtensionList";
7-
import { useEffect } from "react";
87

98
export type TextInputProps = {
109
onSubmit?: (plainText: string, html: string) => void;
@@ -16,10 +15,9 @@ export type TextInputProps = {
1615
style?: keyof typeof styleMap;
1716
placeholder?: string;
1817
content?: string;
18+
onEscape?: () => void;
1919
};
2020

21-
let editor: Editor | null = null;
22-
2321
const styleMap = {
2422
spark: "p-4 min-h-full block w-full bg-white border border-blue-300 rounded-lg text-sm focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-900 dark:border-neutral-700 dark:text-neutral-400 dark:placeholder-neutral-500 dark:focus:ring-neutral-600",
2523
search: "px-4 py-2 block w-full bg-transparent hover:bg-white focus:bg-white border-b border-b-stone-200 rounded",
@@ -38,8 +36,9 @@ export const TextInput = (props: TextInputProps) => {
3836
placeholder,
3937
enableTags,
4038
enableExtension,
39+
onEscape,
4140
} = props;
42-
editor = useEditor({
41+
const editor = useEditor({
4342
content,
4443
extensions: getExtensions({
4544
parentWindow: parentWindow ?? window,
@@ -58,13 +57,16 @@ export const TextInput = (props: TextInputProps) => {
5857
if (!onSubmit) {
5958
return false;
6059
}
61-
if (event.key !== "Enter" || event.shiftKey) {
62-
return false;
63-
}
6460
if (isUserSelectingTag || isUserSelectingExtension) {
6561
// user currently has the selection open and might have pressed enter to select an item
6662
return false;
6763
}
64+
if (event.key !== "Enter" || event.shiftKey) {
65+
if (event.key === "Escape") {
66+
onEscape?.();
67+
}
68+
return false;
69+
}
6870
const html = editor?.getHTML().trim() ?? "";
6971
const plainText = editor?.getText().trim() ?? "";
7072
if (html === "") {

src/container/SparkList/SparkItem/SparkItem.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export const SparkItem = (props: Props) => {
3232
setIsEditing(false);
3333
};
3434

35+
const handleEscape = () => {
36+
setIsEditing(false);
37+
};
38+
3539
const handleTrashClicked = async () => {
3640
setIsDeleting(true);
3741
};
@@ -50,6 +54,7 @@ export const SparkItem = (props: Props) => {
5054
<TextInput
5155
onSubmit={handleSubmit}
5256
content={spark.originalHtml}
57+
onEscape={handleEscape}
5358
/>
5459
</div>
5560
) : (

0 commit comments

Comments
 (0)