From eb7f1225bd39a682213d090570e715a52def11eb Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Wed, 24 Jan 2024 13:40:07 +0800 Subject: [PATCH] fix: overflow for source code editor (#2014) * fix: source code should wrap * fix: code scroll * chore: optimize * chore: code opt * chore: code clean --- .../builtins/SourceCode/index.less | 25 +++++ .../builtins/SourceCode/index.tsx | 92 +++++++++++-------- .../slots/SourceCodeEditor/index.less | 26 +++++- .../slots/SourceCodeEditor/index.tsx | 66 +++++++------ 4 files changed, 132 insertions(+), 77 deletions(-) diff --git a/src/client/theme-default/builtins/SourceCode/index.less b/src/client/theme-default/builtins/SourceCode/index.less index 18b9387530..26020477df 100644 --- a/src/client/theme-default/builtins/SourceCode/index.less +++ b/src/client/theme-default/builtins/SourceCode/index.less @@ -25,7 +25,32 @@ border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; + &-scroll-container { + overflow: auto; + width: 100%; + height: 100%; + } + + &-scroll-content { + position: relative; + width: max-content; + height: max-content; + min-width: 100%; + min-height: 100%; + + > pre.prism-code { + width: max-content; + position: relative; + overflow: visible; + } + } + > pre.prism-code { + overflow: auto; + } + + > pre.prism-code, + &-scroll-content > pre.prism-code { margin: 0; padding: 18px 24px; font-size: 14px; diff --git a/src/client/theme-default/builtins/SourceCode/index.tsx b/src/client/theme-default/builtins/SourceCode/index.tsx index ed6b4af4ed..efd907dad5 100644 --- a/src/client/theme-default/builtins/SourceCode/index.tsx +++ b/src/client/theme-default/builtins/SourceCode/index.tsx @@ -27,6 +27,7 @@ interface SourceCodeProps { lang: Language; highlightLines?: number[]; extra?: ReactNode; + textarea?: ReactNode; } const SourceCode: FC = (props) => { @@ -44,6 +45,48 @@ const SourceCode: FC = (props) => { } }, [lang, children]); + const code = ( + + {({ className, style, tokens, getLineProps, getTokenProps }) => ( +
+          {tokens.map((line, i) => (
+            
+ {themeConfig.showLineNum && ( + {i + 1} + )} +
+ {line.map((token, key) => ( + // getTokenProps 返回值包含 key + // eslint-disable-next-line react/jsx-key + + ))} +
+
+ ))} +
+ )} +
+ ); + return (
= (props) => { {isCopied ? : } - - {({ className, style, tokens, getLineProps, getTokenProps }) => ( -
-            {tokens.map((line, i) => (
-              
- {themeConfig.showLineNum && ( - {i + 1} - )} -
- {line.map((token, key) => ( - // getTokenProps 返回值包含 key - // eslint-disable-next-line react/jsx-key - - ))} -
-
- ))} -
- )} -
+ {props.textarea ? ( +
+
+ {code} + {props.textarea} +
+
+ ) : ( + code + )} {props.extra}
); diff --git a/src/client/theme-default/slots/SourceCodeEditor/index.less b/src/client/theme-default/slots/SourceCodeEditor/index.less index 0385bf44a4..6bd7d61ca5 100644 --- a/src/client/theme-default/slots/SourceCodeEditor/index.less +++ b/src/client/theme-default/slots/SourceCodeEditor/index.less @@ -30,11 +30,7 @@ } &:focus { - box-shadow: 0 0 0 1px lighten(@c-primary, 20%) inset; - @{dark-selector} & { - box-shadow: 0 0 0 1px darken(@c-primary-dark, 20%) inset; - // for firefox because its selection color is not translucent when color-scheme is dark &::selection { background-color: fade(@c-primary-dark, 30%); @@ -42,4 +38,26 @@ } } } + + &::after { + content: ''; + position: absolute; + z-index: 0; + top: 0; + left: 0; + width: 100%; + height: 100%; + border-bottom-left-radius: 3px; + border-bottom-right-radius: 3px; + } + + &:focus-within { + &::after { + box-shadow: 0 0 0 1px @c-primary inset; + + @{dark-selector} & { + box-shadow: 0 0 0 1px @c-primary-dark inset; + } + } + } } diff --git a/src/client/theme-default/slots/SourceCodeEditor/index.tsx b/src/client/theme-default/slots/SourceCodeEditor/index.tsx index 4fbb023121..da7af47cbe 100644 --- a/src/client/theme-default/slots/SourceCodeEditor/index.tsx +++ b/src/client/theme-default/slots/SourceCodeEditor/index.tsx @@ -51,45 +51,43 @@ const SourceCodeEditor: FC = (props) => {
-