Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue/4599-add cursor position information down inside the editor area. #4626

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
47 changes: 31 additions & 16 deletions frontend/src/routes/_oh.app._index/code-editor-component.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Editor, Monaco } from "@monaco-editor/react";
import React from "react";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { VscCode } from "react-icons/vsc";
import { type editor } from "monaco-editor";
Expand All @@ -22,6 +22,8 @@ function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {
saveFileContent: saveNewFileContent,
} = useFiles();

const [cursorPosition, setCursorPosition] = useState({ line: 1, column: 1 });

const handleEditorDidMount = React.useCallback(
(editor: editor.IStandaloneCodeEditor, monaco: Monaco): void => {
monaco.editor.defineTheme("my-theme", {
Expand All @@ -34,6 +36,10 @@ function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {
});

monaco.editor.setTheme("my-theme");

editor.onDidChangeCursorPosition((e) => {
setCursorPosition({ line: e.position.lineNumber, column: e.position.column });
});
},
[],
);
Expand Down Expand Up @@ -62,7 +68,7 @@ function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {
return () => {
document.removeEventListener("keydown", handleSave);
};
}, [saveNewFileContent]);
}, [saveNewFileContent, selectedPath]);

if (!selectedPath) {
return (
Expand All @@ -77,20 +83,29 @@ function CodeEditorCompoonent({ isReadOnly }: CodeEditorCompoonentProps) {
}

return (
<Editor
data-testid="code-editor"
height="100%"
path={selectedPath ?? undefined}
defaultValue=""
value={
selectedPath
? modifiedFiles[selectedPath] || files[selectedPath]
: undefined
}
onMount={handleEditorDidMount}
onChange={handleEditorChange}
options={{ readOnly: isReadOnly }}
/>
<div className="flex grow flex-col h-full w-full">
{/* Ensure that the editor takes up the maximum amount of space in the parent container */}
<div className="flex-grow min-h-0">
<Editor
data-testid="code-editor"
height="100%"
path={selectedPath ?? undefined}
defaultValue=""
value={
selectedPath
? modifiedFiles[selectedPath] || files[selectedPath]
: undefined
}
onMount={handleEditorDidMount}
onChange={handleEditorChange}
options={{ readOnly: isReadOnly }}
/>
</div>
{/* cursor position information */}
<div className="p-2 text-neutral-500 flex-shrink-0">
Row: {cursorPosition.line}, Column: {cursorPosition.column}
</div>
</div>
);
}

Expand Down