Skip to content

Commit

Permalink
further down the codemirror rabbit hole
Browse files Browse the repository at this point in the history
  • Loading branch information
rgbkrk committed Jan 14, 2024
1 parent 75e5fda commit bd7a618
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 47 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-slot": "^1.0.2",
"@tauri-apps/api": "^1.5.2",
"@uiw/react-codemirror": "^4.21.21",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"codemirror": "^6.0.1",
Expand Down
59 changes: 59 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/components/Cell.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Input } from "@/components/ui/input";

Check failure on line 1 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

'Input' is declared but its value is never read.

Check failure on line 1 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-20.04)

'Input' is declared but its value is never read.

Check failure on line 1 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

'Input' is declared but its value is never read.
import { Button } from "@/components/ui/button";

import { useCell } from "../hooks/useCell";
import { useCell } from "@/hooks/useCell";
import { Editor } from "@/components//Editor";

const Cell = ({ cellID }: { cellID: string }) => {
const { content, executeCell, updateCell, cellState, executionCount } = useCell(cellID);

Check failure on line 8 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

'content' is declared but its value is never read.

Check failure on line 8 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

'updateCell' is declared but its value is never read.

Check failure on line 8 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-20.04)

'content' is declared but its value is never read.

Check failure on line 8 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-20.04)

'updateCell' is declared but its value is never read.

Check failure on line 8 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

'content' is declared but its value is never read.

Check failure on line 8 in src/components/Cell.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

'updateCell' is declared but its value is never read.
Expand All @@ -27,19 +28,25 @@ const Cell = ({ cellID }: { cellID: string }) => {
variant="ghost"
className="font-mono mr-2 text-sm group w-14"
onClick={executeCell}
// onKeyPress={(event) => {
// if (event.key === 'Enter') {
// executeCell();
// }
// }}
>
[
<div className="group-hover:block hidden">{actionIcon}</div>
<span className="group-hover:hidden">{executionCount}</span>
]
</Button>
<Input
<Editor cellID={cellID} />
{/* <Input
type="text"
placeholder="write code..."
className="mr-2 font-mono text-sm"
value={content}
onChange={(e) => updateCell(e.target.value)}
/>
/> */}
</div>
);
};
Expand Down
75 changes: 31 additions & 44 deletions src/components/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
import { useRef, useEffect, useState } from 'react';

import { basicSetup } from 'codemirror';

import { EditorState} from '@codemirror/state';
import { EditorView, keymap } from '@codemirror/view';
import { defaultKeymap, indentWithTab } from '@codemirror/commands';
import { javascript } from '@codemirror/lang-javascript';

import {lightTheme} from "@/codemirror-themes"

export const Editor = (/*{ setEditorState }*/) => {
const editor = useRef<HTMLDivElement | null>(null);
const [code, setCode] = useState('');

const onUpdate = EditorView.updateListener.of((v) => {
setCode(v.state.doc.toString());
});

useEffect(() => {
const state = EditorState.create({
doc: code,
extensions: [
basicSetup,
keymap.of([...defaultKeymap, indentWithTab]),
lightTheme,
javascript(),
onUpdate,
],
});

if(editor.current === null) {
return;
}

const view = new EditorView({ state, parent: editor.current });

return () => {
view.destroy();
};
}, [editor.current]);

return <div ref={editor}></div>;
};
import CodeMirror from "@uiw/react-codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { lightTheme } from "@/codemirror-themes";
import { useCell } from "@/hooks/useCell";

import { cn } from "@/lib/utils"


export const Editor = ({ cellID, className }: { cellID: string, className?: string }) => {
const { content, updateCell } = useCell(cellID);

const bigclasses = cn(
"flex w-full h-full border border-input bg-transparent text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
className
)

return (
<CodeMirror
value={content}
width="100%"
height="100%"
// Revert to CodeMirror's default for Accessibility -- not indenting with tab.
// We can bring it back if we put escape in place
indentWithTab={false}
extensions={[javascript({ jsx: true })]}
onChange={updateCell}
className={bigclasses}
theme={lightTheme}
/>
);
};

0 comments on commit bd7a618

Please sign in to comment.