mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-06 08:50:50 +08:00
refactor(component): clean up code block component
clean up codeblock component ts types remove commented code remove unused vars better css names
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
import { FC, useState, useMemo, useRef, useEffect } from 'react';
|
import { FC, useState, useRef, useEffect } from 'react';
|
||||||
import { StyleWithAtRules } from 'style9';
|
import { StyleWithAtRules } from 'style9';
|
||||||
|
|
||||||
import { CreateView } from '@toeverything/framework/virgo';
|
import { CreateView } from '@toeverything/framework/virgo';
|
||||||
import CodeMirror from './CodeMirror';
|
import CodeMirror, { ReactCodeMirrorRef } from './CodeMirror';
|
||||||
import { styled } from '@toeverything/components/ui';
|
import { styled } from '@toeverything/components/ui';
|
||||||
import DeleteSweepOutlinedIcon from '@mui/icons-material/DeleteSweepOutlined';
|
|
||||||
|
|
||||||
import { javascript } from '@codemirror/lang-javascript';
|
import { javascript } from '@codemirror/lang-javascript';
|
||||||
import { html } from '@codemirror/lang-html';
|
import { html } from '@codemirror/lang-html';
|
||||||
@@ -46,7 +45,6 @@ import { dockerFile } from '@codemirror/legacy-modes/mode/dockerfile';
|
|||||||
import { julia } from '@codemirror/legacy-modes/mode/julia';
|
import { julia } from '@codemirror/legacy-modes/mode/julia';
|
||||||
import { r } from '@codemirror/legacy-modes/mode/r';
|
import { r } from '@codemirror/legacy-modes/mode/r';
|
||||||
import { Extension } from '@codemirror/state';
|
import { Extension } from '@codemirror/state';
|
||||||
// import { Select } from '../../components/select';
|
|
||||||
import { Option, Select } from '@toeverything/components/ui';
|
import { Option, Select } from '@toeverything/components/ui';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -102,8 +100,6 @@ const langs: Record<string, any> = {
|
|||||||
julia: () => StreamLanguage.define(julia),
|
julia: () => StreamLanguage.define(julia),
|
||||||
dockerfile: () => StreamLanguage.define(dockerFile),
|
dockerfile: () => StreamLanguage.define(dockerFile),
|
||||||
r: () => StreamLanguage.define(r),
|
r: () => StreamLanguage.define(r),
|
||||||
// clike: () => StreamLanguage.define(clike),
|
|
||||||
// clike: () => clike({ }),
|
|
||||||
};
|
};
|
||||||
const DEFAULT_LANG = 'javascript';
|
const DEFAULT_LANG = 'javascript';
|
||||||
const CodeBlock = styled('div')(({ theme }) => ({
|
const CodeBlock = styled('div')(({ theme }) => ({
|
||||||
@@ -120,7 +116,7 @@ const CodeBlock = styled('div')(({ theme }) => ({
|
|||||||
flexWrap: 'wrap',
|
flexWrap: 'wrap',
|
||||||
justifyContent: 'space-between',
|
justifyContent: 'space-between',
|
||||||
},
|
},
|
||||||
'.delete-block': {
|
'.copy-block': {
|
||||||
padding: '6px 10px',
|
padding: '6px 10px',
|
||||||
backgroundColor: '#fff',
|
backgroundColor: '#fff',
|
||||||
borderRadius: theme.affine.shape.borderRadius,
|
borderRadius: theme.affine.shape.borderRadius,
|
||||||
@@ -134,22 +130,21 @@ export const CodeView: FC<CreateCodeView> = ({ block, editor }) => {
|
|||||||
const initValue: string = block.getProperty('text')?.value?.[0]?.text;
|
const initValue: string = block.getProperty('text')?.value?.[0]?.text;
|
||||||
const langType: string = block.getProperty('lang');
|
const langType: string = block.getProperty('lang');
|
||||||
const [extensions, setExtensions] = useState<Extension[]>();
|
const [extensions, setExtensions] = useState<Extension[]>();
|
||||||
const codeMirror = useRef();
|
const codeMirror = useRef<ReactCodeMirrorRef>();
|
||||||
useOnSelect(block.id, (is_select: boolean) => {
|
useOnSelect(block.id, (_is_select: boolean) => {
|
||||||
if (codeMirror.current) {
|
if (codeMirror.current) {
|
||||||
//@ts-ignore
|
|
||||||
codeMirror?.current?.view?.focus();
|
codeMirror?.current?.view?.focus();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const onChange = (value: any, codeEditor: any) => {
|
const onChange = (value: string) => {
|
||||||
block.setProperty('text', {
|
block.setProperty('text', {
|
||||||
value: [{ text: value }],
|
value: [{ text: value }],
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
function handleLangChange(lang: string) {
|
const handleLangChange = (lang: string) => {
|
||||||
block.setProperty('lang', lang);
|
block.setProperty('lang', lang);
|
||||||
setExtensions([langs[lang]()]);
|
setExtensions([langs[lang]()]);
|
||||||
}
|
};
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
handleLangChange(langType ? langType : DEFAULT_LANG);
|
handleLangChange(langType ? langType : DEFAULT_LANG);
|
||||||
}, []);
|
}, []);
|
||||||
@@ -172,19 +167,12 @@ export const CodeView: FC<CreateCodeView> = ({ block, editor }) => {
|
|||||||
>
|
>
|
||||||
<div className="operation">
|
<div className="operation">
|
||||||
<div className="select">
|
<div className="select">
|
||||||
{/* <Select
|
|
||||||
label="Lang"
|
|
||||||
options={Object.keys(langs)}
|
|
||||||
value={mode}
|
|
||||||
onChange={evn => handleLangChange(evn.target.value)}
|
|
||||||
/> */}
|
|
||||||
<Select
|
<Select
|
||||||
width={128}
|
width={128}
|
||||||
placeholder="Search for a field type"
|
placeholder="Search for a field type"
|
||||||
value={langType || DEFAULT_LANG}
|
value={langType || DEFAULT_LANG}
|
||||||
listboxStyle={{ maxHeight: '400px' }}
|
listboxStyle={{ maxHeight: '400px' }}
|
||||||
onChange={(selectedValue: string) => {
|
onChange={(selectedValue: string) => {
|
||||||
// setSelectedOption(selectedValue);
|
|
||||||
handleLangChange(selectedValue);
|
handleLangChange(selectedValue);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -198,17 +186,8 @@ export const CodeView: FC<CreateCodeView> = ({ block, editor }) => {
|
|||||||
</Select>
|
</Select>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<div className="delete-block" onClick={copyCode}>
|
<div className="copy-block" onClick={copyCode}>
|
||||||
Copy
|
Copy
|
||||||
{/* <DeleteSweepOutlinedIcon
|
|
||||||
className="delete-icon"
|
|
||||||
fontSize="small"
|
|
||||||
sx={{
|
|
||||||
color: 'rgba(0,0,0,.5)',
|
|
||||||
cursor: 'pointer',
|
|
||||||
'&:hover': { color: 'rgba(0,0,0,.9)' },
|
|
||||||
}}
|
|
||||||
/> */}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user