Skip to content

Commit

Permalink
feat(snippet): set the current language as the default when editing
Browse files Browse the repository at this point in the history
  • Loading branch information
tericcabrel committed Nov 1, 2023
1 parent 3be5818 commit 18db8b6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
1 change: 0 additions & 1 deletion apps/web/src/styles/code-editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

.code-editor-container textarea {
outline: 0;
caret-color: white;
font-family: 'Inter', monospace !important;
font-size: 14px;
padding-left: 50px !important;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ const CreateSnippetContainer = ({ closeModal, folderId, open }: Props) => {

const formMethods = useForm<SnippetFormValues>({
defaultValues: {
code: `import fs from "fs";
import path from "path";
const content= fs.readFileSync(path.resolve(__dirname, 'file.json'), { encoding: "utf-8" });
console.log(content);`,
code: `// Write your code here...\n`,
codeHighlight: CODE_HIGHLIGHT_OPTIONS[0],
codeHighlighted: '',
isPrivate: true,
Expand All @@ -45,7 +40,7 @@ console.log(content);`,
const handleCloseModal = () => {
closeModal();
formMethods.reset({
code: '',
code: '// Write your code here...\n',
codeHighlight: CODE_HIGHLIGHT_OPTIONS[0],
codeHighlighted: '',
description: '',
Expand All @@ -58,8 +53,6 @@ console.log(content);`,
};

const submitCreateSnippet = async (values: SnippetFormValues) => {
console.log('Values => ', values);

await createSnippet({
input: {
content: values.code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FormProvider, useForm } from 'react-hook-form';
import Button from '../../../../forms/button';
import { useCodeHighlighter } from '../../../../hooks';
import { useUpdateSnippet } from '../../../../services/snippets/update-snippet';
import { SelectOption } from '../../../../typings/components';
import { SnippetItem } from '../../../../typings/queries';
import { CODE_HIGHLIGHT_OPTIONS, THEME_OPTIONS } from '../../../../utils/constants';
import { extractLanguageFromName, lineHighlightToString } from '../../../../utils/snippets';
Expand All @@ -22,11 +23,16 @@ const selectCodeHighlightOptionValue = (theme: string) => {
return themeOption ?? THEME_OPTIONS[0];
};

const selectLanguageOptionValue = (options: SelectOption[], language: string) => {
return options.find((option) => option.id === language);
};

const ViewSnippet = ({ snippet }: Props) => {
const { highlighter } = useCodeHighlighter();
const { toastError, toastSuccess } = useToast();

const { isLoading, updateSnippet } = useUpdateSnippet(snippet.folderId);
const languageOptions = generateSnippetLanguageOptions();

const formMethods = useForm<SnippetFormValues>({
defaultValues: {
Expand All @@ -35,6 +41,7 @@ const ViewSnippet = ({ snippet }: Props) => {
codeHighlighted: snippet.contentHighlighted,
description: snippet.description ?? undefined,
isPrivate: snippet.isPrivate,
language: selectLanguageOptionValue(languageOptions, snippet.language),
lineHighlight: snippet.lineHighLight,
name: snippet.name,
theme: selectCodeHighlightOptionValue(snippet.theme),
Expand All @@ -43,8 +50,6 @@ const ViewSnippet = ({ snippet }: Props) => {
});

const submitUpdateSnippet = async (values: SnippetFormValues) => {
console.log('Values => ', values);

await updateSnippet({
id: snippet.id,
input: {
Expand All @@ -71,7 +76,7 @@ const ViewSnippet = ({ snippet }: Props) => {
<FormProvider {...formMethods}>
<SnippetTextEditor
highlighter={highlighter}
languageOptions={generateSnippetLanguageOptions()}
languageOptions={languageOptions}
codeHighlightOptions={CODE_HIGHLIGHT_OPTIONS}
themeOptions={THEME_OPTIONS}
/>
Expand Down

0 comments on commit 18db8b6

Please sign in to comment.