Skip to content

Commit

Permalink
#162 fixed (#164)
Browse files Browse the repository at this point in the history
Co-authored-by: = <rahulkumar@metafic.co>
  • Loading branch information
RahulkumarRV and rahulkumarmetafic committed Jul 14, 2023
1 parent 5d310de commit 5b27ba9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
11 changes: 11 additions & 0 deletions app/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useJsonDoc } from "~/hooks/useJsonDoc";
import { getEditorSetup } from "~/utilities/codeMirrorSetup";
import { darkTheme, lightTheme } from "~/utilities/codeMirrorTheme";
import { useTheme } from "./ThemeProvider";
import { useHotkeys } from "react-hotkeys-hook";

export type CodeEditorProps = {
content: string;
Expand Down Expand Up @@ -98,6 +99,16 @@ export function CodeEditor(opts: CodeEditorProps) {

const { minimal } = useJsonDoc();

useHotkeys(
"ctrl+a,meta+a,command+a",
(e) => {
e.preventDefault();
view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } });
},
[view, state]
);


return (
<div>
<div
Expand Down
12 changes: 11 additions & 1 deletion app/components/CodeViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useRef, useEffect } from "react";
import { getViewerSetup } from "~/utilities/codeMirrorSetup";
import { darkTheme, lightTheme } from "~/utilities/codeMirrorTheme";
import { useTheme } from "./ThemeProvider";
import { useHotkeys } from "react-hotkeys-hook";

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

const [theme] = useTheme();

const { setContainer } = useCodeMirror({
const { setContainer, view, state } = useCodeMirror({
container: editor.current,
extensions,
value: code,
Expand All @@ -33,6 +34,15 @@ export function CodeViewer({ code, lang }: { code: string; lang?: "json" }) {
}
}, [editor.current]);

useHotkeys(
"ctrl+a,meta+a,command+a",
(e) => {
e.preventDefault();
view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } });
},
[view, state]
);

return (
<div>
<div ref={editor} />
Expand Down
14 changes: 12 additions & 2 deletions app/components/JsonPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { getPreviewSetup } from "~/utilities/codeMirrorSetup";
import { lightTheme, darkTheme } from "~/utilities/codeMirrorTheme";
import { CopyTextButton } from "./CopyTextButton";
import { useTheme } from "./ThemeProvider";
import {usePreferences} from '~/components/PreferencesProvider'
import {usePreferences} from '~/components/PreferencesProvider';
import { useHotkeys } from "react-hotkeys-hook";

export type JsonPreviewProps = {
json: unknown;
Expand Down Expand Up @@ -56,7 +57,7 @@ export function JsonPreview({ json, highlightPath }: JsonPreviewProps) {

const [theme] = useTheme();

const { setContainer, view } = useCodeMirror({
const { setContainer, view, state } = useCodeMirror({
container: editor.current,
extensions,
value: jsonMapped.json,
Expand Down Expand Up @@ -92,6 +93,15 @@ export function JsonPreview({ json, highlightPath }: JsonPreviewProps) {
view.dispatch(transactionSpec);
}, [view, highlighting, jsonMapped, highlightPath]);

useHotkeys(
"ctrl+a,meta+a,command+a",
(e) => {
e.preventDefault();
view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } });
},
[view, state]
);

const [hovering, setHovering] = useState(false);

return (
Expand Down

0 comments on commit 5b27ba9

Please sign in to comment.