refactor blockcode

This commit is contained in:
Mathew Pareles 2025-01-07 20:39:20 -08:00
parent a53f92432b
commit af244938b8
4 changed files with 20 additions and 16 deletions

View file

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { ReactNode } from "react"
import { VoidCodeEditor } from '../util/inputs.js';
import { VoidCodeEditor, VoidCodeEditorProps } from '../util/inputs.js';
const extensionMap: { [key: string]: string } = {
@ -63,10 +63,9 @@ export function getLanguageFromFileName(fileName: string): string {
return extensionMap[ext] || 'plaintext';
}
export const BlockCode = ({ text, buttonsOnHover, language }: { text: string, buttonsOnHover?: ReactNode, language?: string }) => {
export const BlockCode = ({ buttonsOnHover, ...codeEditorProps }: { buttonsOnHover?: React.ReactNode } & VoidCodeEditorProps) => {
const isSingleLine = !text.includes('\n')
const isSingleLine = !codeEditorProps.initValue.includes('\n')
return (<>
<div className={`relative group w-full overflow-hidden`}>
@ -76,10 +75,7 @@ export const BlockCode = ({ text, buttonsOnHover, language }: { text: string, bu
</div>
)}
<VoidCodeEditor
initValue={text}
language={language}
/>
<VoidCodeEditor {...codeEditorProps} />
</div>
</>
)

View file

@ -76,7 +76,7 @@ const RenderToken = ({ token, nested = false }: { token: Token | string, nested?
if (t.type === "code") {
return <BlockCode
text={t.text}
initValue={t.text}
// language={t.lang} // instead use vscode to detect language
buttonsOnHover={<CodeButtonsOnHover text={t.text} />}
/>

View file

@ -362,7 +362,7 @@ export const SelectedFiles = (
{/* selection text */}
{isThisSelectionOpened &&
<div className='w-full px-1 rounded-sm border-vscode-editor-border'>
<BlockCode text={selection.selectionStr!} language={getLanguageFromFileName(selection.fileURI.path)} />
<BlockCode initValue={selection.selectionStr!} language={getLanguageFromFileName(selection.fileURI.path)} />
</div>
}
</div>

View file

@ -294,9 +294,12 @@ const normalizeIndentation = (code: string): string => {
}
export const VoidCodeEditor = ({ initValue, language }: { initValue: string, language: string | undefined }) => {
export type VoidCodeEditorProps = { initValue: string, language?: string, maxHeight?: number, hideScrollbars?: boolean }
export const VoidCodeEditor = ({ initValue, language, maxHeight, hideScrollbars }: VoidCodeEditorProps) => {
const MAX_HEIGHT = Infinity;
// apply default settings
const MAX_HEIGHT = maxHeight ?? Infinity;
const HIDE_SCROLLBARS = hideScrollbars ?? false;
const divRef = useRef<HTMLDivElement | null>(null)
@ -320,10 +323,15 @@ export const VoidCodeEditor = ({ initValue, language }: { initValue: string, lan
scrollbar: {
alwaysConsumeMouseWheel: false,
// vertical: 'hidden',
// horizontal: 'hidden',
verticalScrollbarSize: 8,
horizontalScrollbarSize: 8,
...HIDE_SCROLLBARS ? {
vertical: 'hidden',
horizontal: 'hidden',
verticalScrollbarSize: 0,
horizontalScrollbarSize: 0,
} : {
verticalScrollbarSize: 8,
horizontalScrollbarSize: 8,
},
},
scrollBeyondLastLine: false,