如何设置 mui-rte CodeBlock 的样式

问题描述 投票:0回答:5

默认情况下,CodeBlock 的样式为白色背景和黑色。这对于“浅”调色板效果很好,但对于“深色”调色板则无法读取,因为使用“深色”调色板时,背景保持白色,而颜色也变成白色。我可以应用基于调色板的主题,但无法弄清楚如何设置 CodeBlock 的样式。我想做如下的事情:

const darkTheme = createMuiTheme()

Object.assign(darkTheme, {
   overrides: {
      CodeBlock: {
         root: {
            backgroundColor: '#37474F',
            color: '#fff',
         },
      },
   ....
 })
  ....    

    const MyEditor = (props: IProps) => {
       return (
                <MuiThemeProvider theme={getTheme(props.brightness)}>
                   <MUIRichTextEditor defaultValue="" label="Type something here..." onSave={save} inlineToolbar={true} />
                </MuiThemeProvider>)
reactjs material-ui rich-text-editor
5个回答
2
投票

我认为杰克的回答是正确的。似乎没有一种方法可以专门设置“.CodeBlock”的样式。我用 mui-rte 开了一张票。我能够使用可读的“.CodeBlock”编写一个看起来相当漂亮的暗模式编辑器,如下所示:

const darkTheme = createMuiTheme()

Object.assign(darkTheme, {
   overrides: {
      MuiIconButton: {
         root: {
            color: '#fff',
         },
      },
      MUIRichTextEditor: {
         root: {
            '& pre': {
               color: '#212121',
            },
         },
         editor: {
            padding: '20px',
            height: '200px',
            maxHeight: '200px',
            overflow: 'auto',
         },
         placeHolder: {
            paddingLeft: 20,
            width: 'inherit',
            position: 'static',
         },
         anchorLink: {
            color: '#FFEB3B',
            textDecoration: 'underline',
         },
      },
   },
})

interface IProps {
   brightness: string
}

const MyEditor = (props: IProps) => {
   return (
      <div>
         <form onSubmit={handleSubmit} style={{ overflow: 'auto' }}>
            <TextField>ff</TextField>
            <MuiThemeProvider theme={getTheme(props.brightness)}>
               <MUIRichTextEditor defaultValue="" label="Type something here..." onSave={save} inlineToolbar={true} />
            </MuiThemeProvider>
            <br />
            <Button variant="contained" color="primary" type="submit">
               submit
            </Button>
         </form>
      </div>
   )
}

export default MyEditor


0
投票

根据docs,您可以使用

inlineStyle
来设置背景颜色。

示例:

import MUIRichTextEditor from 'mui-rte'
import InvertColorsIcon from '@material-ui/icons/InvertColors'

<MUIRichTextEditor 
    controls={["my-style"]}
    customControls={[
        {
            name: "my-style",
            icon: <InvertColorsIcon />,
            type: "inline",
            inlineStyle: {
                backgroundColor: "black",
                color: "white"
            }
        }
    ]}
/>

根据文档的另一个选项:

import { createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles'
import MUIRichTextEditor from 'mui-rte'

const defaultTheme = createMuiTheme()

Object.assign(defaultTheme, {
    overrides: {
        MUIRichTextEditor: {
            root: {
                marginTop: 20,
                width: "80%"
            },
            editor: {
                borderBottom: "1px solid gray" 
            }
        }
    }
})

<MuiThemeProvider theme={defaultTheme}>
    <MUIRichTextEditor 
        label="Type something here..."
    />
</MuiThemeProvider>

0
投票

此位允许您进行更多自定义操作,例如自动调整代码块大小、调整行高,例如:

overrides: {
  MUIRichTextEditor: {
    root: {
      '& div[class^="CodeBlock"]': {
        'background-color': theme.palette.background.paper,
        color: theme.palette.text.secondary,
        'display': 'inline-block',
        'line-height': 0
      }
    }, ...

但是,Jake 的代码也可能像下面这样工作:

import MUIRichTextEditor from 'mui-rte';
import CodeIcon from '@mui/icons-material/Code';

<MUIRichTextEditor 
  controls={["my-codeblock"]}
  customControls={[
    {
      name: "my-codeblock",
      icon: <CodeIcon />,
      type: "inline",
      inlineStyle: {
        bgcolor: theme.palette.background.paper,
        color: theme.palette.text.secondary,
        'display': 'inline-block',
        'line-height': 0
        }
      }
  ]}
/>

0
投票

如果编辑器被禁用,我想更改文本的颜色。 这对我有用:

    const useStyles = makeStyles((theme: Theme) =>
    createStyles({
        editorReadonly: {
            color: MBTheme.palette.grey[500],
        },
    })
    );

function TaskFormFields(props: EditTaskProps) {
    const translate = useTranslation();
    const intl = useIntl();

    const classes = useStyles();
           .
           .
           .


 return (
            <MUIRichTextEditor
                classes={{ editorReadOnly: classes.editorReadonly }}
                ref={rteRef as unknown as React.Ref<TMUIRichTextEditorRef>}
                readOnly={props.readonly}
                defaultValue={task.content ? ensureRichtText(task.content ?? '') : ''}
                label={props.readonly ? '' : translate('task_content')}
                onBlur={() => rteRef.current?.save()}
                onSave={(data: string) =>
                    task.content !== data && props.onChange && props.onChange({ ...task, content: data })
                }
            />)

梅5, 反应 17, 打字稿


0
投票

我知道这对这个主题没有直接贡献,但我想我还是要问。

你们中有人实现了自定义颜色选择器来允许对某些文本进行着色吗?

我在堆栈、码头或 GitHub 示例上找不到任何内容。

提前感谢您的反馈。

垫子

© www.soinside.com 2019 - 2024. All rights reserved.