PE-71 close (#13)

* fixed expand issues

* removed expand

* can close the file, and min 5 lines are

* removed older file

* lag selection took some time :(

* ready for PR

* changed prompt:lang to lang
This commit is contained in:
anandamarsh 2023-09-06 16:36:49 -07:00 committed by GitHub
parent dff146db66
commit 52a7e14df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -35,7 +35,6 @@ class SourceCodeRenderer extends React.Component<
this.editorRef = React.createRef();
this.state = {
code: null,
language: "",
languages: [],
selectedLanguage: "",
isSave: false,
@ -60,23 +59,42 @@ class SourceCodeRenderer extends React.Component<
});
}
handleEditorDidMount = (editor, monaco) => {
// we try to grab the filename from with filePath (coming from lineState["prompt:file"]) or cmdstr
const strForFilePath = this.filePath || this.props.cmdstr;
const extension = strForFilePath.match(/(?:[^\\\/:*?"<>|\r\n]+\.)([a-zA-Z0-9]+)\b/)?.[1] || "";
const detectedLanguage = monaco.languages
.getLanguages()
.find((lang) => lang.extensions?.includes("." + extension));
setInitialLanguage = (editor) => {
const { screenId, lineId } = this.props.context;
// set all languages
const languages = monaco.languages.getLanguages().map((lang) => lang.id);
this.setState({ languages });
// detect the current language from previous settings
let detectedLanguage = this.props.lineState["lang"];
// if not found, we try to grab the filename from with filePath (coming from lineState["prompt:file"]) or cmdstr
if (!detectedLanguage) {
const strForFilePath = this.filePath || this.props.cmdstr;
const extension = strForFilePath.match(/(?:[^\\\/:*?"<>|\r\n]+\.)([a-zA-Z0-9]+)\b/)?.[1] || "";
const detectedLanguageObj = monaco.languages
.getLanguages()
.find((lang) => lang.extensions?.includes("." + extension));
if (detectedLanguageObj) {
detectedLanguage = detectedLanguageObj.id;
GlobalCommandRunner.setLineState(
screenId,
lineId,
{ ...this.props.lineState, lang: detectedLanguage },
false
);
}
}
if (detectedLanguage) {
this.editorRef.current = editor;
const model = editor.getModel();
if (model) {
monaco.editor.setModelLanguage(model, detectedLanguage.id);
this.setState({ selectedLanguage: detectedLanguage.id, language: detectedLanguage.id });
monaco.editor.setModelLanguage(model, detectedLanguage);
this.setState({ selectedLanguage: detectedLanguage });
}
}
};
handleEditorDidMount = (editor, monaco) => {
this.setInitialLanguage(editor);
this.setEditorHeight();
editor.onKeyDown((e) => {
if (e.code === "KeyS" && (e.ctrlKey || e.metaKey) && this.state.isSave) {
@ -91,13 +109,22 @@ class SourceCodeRenderer extends React.Component<
};
handleLanguageChange = (event) => {
const { screenId, lineId } = this.props.context;
const selectedLanguage = event.target.value;
this.setState({ selectedLanguage });
if (this.editorRef.current) {
const model = this.editorRef.current.getModel();
if (model) {
monaco.editor.setModelLanguage(model, selectedLanguage);
this.setState({ language: selectedLanguage });
GlobalCommandRunner.setLineState(
screenId,
lineId,
{
...this.props.lineState,
lang: selectedLanguage,
},
false
);
}
}
};
@ -166,7 +193,7 @@ class SourceCodeRenderer extends React.Component<
render() {
const { opts, exitcode, readOnly } = this.props;
const { lang, code, isSave, isClosed } = this.state;
const { language, code, isSave, isClosed } = this.state;
if (code == null)
return <div className="renderer-container code-renderer" style={{ height: this.props.savedHeight }} />;
@ -191,7 +218,7 @@ class SourceCodeRenderer extends React.Component<
<Editor
theme="hc-black"
height={this.state.editorHeight}
defaultLanguage={lang}
defaultLanguage={language}
defaultValue={code}
onMount={this.handleEditorDidMount}
options={{