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

fix/fix-editor-issues #78

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@hookform/resolvers": "^3.3.2",
"@lit/react": "^1.0.2",
"@material/web": "^1.0.1",
"codemirror": "^6.0.1",
"firebase": "^10.7.1",
"overlayscrollbars": "^2.4.5",
"overlayscrollbars-react": "^0.5.3",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Editor/ui/EditorField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const EditorField = ({ onChange, value = '', isJson, isReadOnly }: EditorFieldPr
prettifyEditors(false);
});
const extensions = [
keymap.of(defaultKeymap),
codemirrorLanguage,
keymap.of(defaultKeymap),
oneDark,
EditorView.lineWrapping,
onUpdate,
Expand Down
2 changes: 1 addition & 1 deletion src/components/RequestEditor/RequestEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const RequestEditor: FC<HTMLAttributes<HTMLElement>> = ({ children, ...props })
onChange={(val: string) => {
if (!prettify) setEditorState(val);
}}
isJson={false}
isJson
isReadOnly={false}
/>
{children}
Expand Down
16 changes: 12 additions & 4 deletions src/components/RequestEditor/lib/formatRequest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-continue */
/* eslint-disable class-methods-use-this */
class RequestFormatter {
insideBrackets: boolean;
Expand Down Expand Up @@ -40,10 +41,11 @@ class RequestFormatter {
if (comments) {
this.comments = comments.join('\n');
}
return str.replace(/\/\/.*/g, ''); // Remove lines starting with //
return str.replace(/\/\/.*/g, '');
}

preFormatString(input: string) {
if (input.length === 0) return '';
let result = input;
result = this.removeComments(result);
result = this.removeSpacesAroundSymbols(result);
Expand All @@ -54,29 +56,35 @@ class RequestFormatter {

formatRequest(input: string) {
const text = this.preFormatString(input);
if (text.length === 0) {
return text;
}

for (let i = 0; i < text.length; i += 1) {
const char = text[i];

if (char === ' ') continue;
if (char === '{') {
this.insideBrackets = true;
this.outputText += `${char}${' '.repeat((this.indentationLevel + 1) * this.indentationSpaces)}`;
this.outputText += ` ${char}`;
this.indentationLevel += 1;
this.outputText.trim();
} else if (char === '}') {
this.insideBrackets = false;
this.indentationLevel -= 1;
this.outputText += `\n${' '.repeat(this.indentationLevel * this.indentationSpaces)}${char}`;
} else if (char === ':' && text[i + 1] !== ' ') {
this.outputText += `${char} `;
} else if (this.isLetter(char) && this.insideBrackets && (text[i - 1] === ' ' || text[i - 1] === '{')) {
this.outputText.trim();
this.outputText += `\n${' '.repeat(this.indentationLevel * this.indentationSpaces)}${char}`;
} else {
this.outputText += char;
}
}

// Insert comments before the formatted query
return `${this.comments}\n\n${this.outputText}`;
if (this.comments.length > 0) return `${this.comments}\n\n${this.outputText}`;
return this.outputText;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/styles/codemirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
background-color: transparent !important;
margin-right: 20px;
}

.cm-activeLine {
background-color: transparent;
}