Skip to content

Commit 5b27ba9

Browse files
#162 fixed (#164)
Co-authored-by: = <[email protected]>
1 parent 5d310de commit 5b27ba9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

app/components/CodeEditor.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useJsonDoc } from "~/hooks/useJsonDoc";
1010
import { getEditorSetup } from "~/utilities/codeMirrorSetup";
1111
import { darkTheme, lightTheme } from "~/utilities/codeMirrorTheme";
1212
import { useTheme } from "./ThemeProvider";
13+
import { useHotkeys } from "react-hotkeys-hook";
1314

1415
export type CodeEditorProps = {
1516
content: string;
@@ -98,6 +99,16 @@ export function CodeEditor(opts: CodeEditorProps) {
9899

99100
const { minimal } = useJsonDoc();
100101

102+
useHotkeys(
103+
"ctrl+a,meta+a,command+a",
104+
(e) => {
105+
e.preventDefault();
106+
view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } });
107+
},
108+
[view, state]
109+
);
110+
111+
101112
return (
102113
<div>
103114
<div

app/components/CodeViewer.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useRef, useEffect } from "react";
44
import { getViewerSetup } from "~/utilities/codeMirrorSetup";
55
import { darkTheme, lightTheme } from "~/utilities/codeMirrorTheme";
66
import { useTheme } from "./ThemeProvider";
7+
import { useHotkeys } from "react-hotkeys-hook";
78

89
export function CodeViewer({ code, lang }: { code: string; lang?: "json" }) {
910
const editor = useRef(null);
@@ -16,7 +17,7 @@ export function CodeViewer({ code, lang }: { code: string; lang?: "json" }) {
1617

1718
const [theme] = useTheme();
1819

19-
const { setContainer } = useCodeMirror({
20+
const { setContainer, view, state } = useCodeMirror({
2021
container: editor.current,
2122
extensions,
2223
value: code,
@@ -33,6 +34,15 @@ export function CodeViewer({ code, lang }: { code: string; lang?: "json" }) {
3334
}
3435
}, [editor.current]);
3536

37+
useHotkeys(
38+
"ctrl+a,meta+a,command+a",
39+
(e) => {
40+
e.preventDefault();
41+
view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } });
42+
},
43+
[view, state]
44+
);
45+
3646
return (
3747
<div>
3848
<div ref={editor} />

app/components/JsonPreview.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import { getPreviewSetup } from "~/utilities/codeMirrorSetup";
1515
import { lightTheme, darkTheme } from "~/utilities/codeMirrorTheme";
1616
import { CopyTextButton } from "./CopyTextButton";
1717
import { useTheme } from "./ThemeProvider";
18-
import {usePreferences} from '~/components/PreferencesProvider'
18+
import {usePreferences} from '~/components/PreferencesProvider';
19+
import { useHotkeys } from "react-hotkeys-hook";
1920

2021
export type JsonPreviewProps = {
2122
json: unknown;
@@ -56,7 +57,7 @@ export function JsonPreview({ json, highlightPath }: JsonPreviewProps) {
5657

5758
const [theme] = useTheme();
5859

59-
const { setContainer, view } = useCodeMirror({
60+
const { setContainer, view, state } = useCodeMirror({
6061
container: editor.current,
6162
extensions,
6263
value: jsonMapped.json,
@@ -92,6 +93,15 @@ export function JsonPreview({ json, highlightPath }: JsonPreviewProps) {
9293
view.dispatch(transactionSpec);
9394
}, [view, highlighting, jsonMapped, highlightPath]);
9495

96+
useHotkeys(
97+
"ctrl+a,meta+a,command+a",
98+
(e) => {
99+
e.preventDefault();
100+
view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } });
101+
},
102+
[view, state]
103+
);
104+
95105
const [hovering, setHovering] = useState(false);
96106

97107
return (

0 commit comments

Comments
 (0)