autoscroll wip

This commit is contained in:
Niels Geens
2024-11-22 17:43:23 +01:00
parent 69f4f8a6fc
commit eb89564aac
5 changed files with 67 additions and 62 deletions

View File

@@ -9,28 +9,54 @@ export const StandardEditor = ({
readOnly = false,
language = 'xml',
validationError = null,
}) => (
<Card className="h-[calc(100vh-8rem)]">
<CardContent className="p-0 h-full">
{validationError && (
<Alert variant="destructive" className="m-4">
<AlertDescription>{validationError}</AlertDescription>
</Alert>
)}
<Editor
height="100%"
language={language}
value={content}
onChange={onChange}
options={{
minimap: { enabled: false },
lineNumbers: 'on',
readOnly,
fontSize: 14,
automaticLayout: true,
wordWrap: 'on',
}}
/>
</CardContent>
</Card>
);
autoScroll = false,
}) => {
const editorRef = React.useRef(null);
const scrollToBottom = () => {
if (editorRef.current && autoScroll) {
const model = editorRef.current.getModel();
const lineCount = model.getLineCount();
editorRef.current.revealPositionInCenter({
lineNumber: lineCount,
column: 1
});
}
};
const handleEditorDidMount = (editor) => {
editorRef.current = editor;
scrollToBottom();
};
React.useEffect(() => {
scrollToBottom();
}, [content, autoScroll]);
return (
<Card className="h-[calc(100vh-8rem)]">
<CardContent className="p-0 h-full">
{validationError && (
<Alert variant="destructive" className="m-4">
<AlertDescription>{validationError}</AlertDescription>
</Alert>
)}
<Editor
height="100%"
language={language}
value={content}
onChange={onChange}
onMount={handleEditorDidMount}
options={{
minimap: { enabled: false },
lineNumbers: 'on',
readOnly,
fontSize: 14,
automaticLayout: true,
wordWrap: 'on',
}}
/>
</CardContent>
</Card>
);
};