feat(CodeEditor): add a prop to enable the readOnly extension (#10516)

* feat(CodeEditor): add a prop to enable the readOnly extension

* feat: enable keymap for TextFilePreview
This commit is contained in:
one 2025-10-04 23:24:50 +08:00 committed by GitHub
parent 78eacccf6e
commit 2048f210e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 3 deletions

View File

@ -75,10 +75,15 @@ export interface CodeEditorProps {
/** CSS class name appended to the default `code-editor` class. */ /** CSS class name appended to the default `code-editor` class. */
className?: string className?: string
/** /**
* Whether the editor is editable. * Whether the editor view is editable.
* @default true * @default true
*/ */
editable?: boolean editable?: boolean
/**
* Set the editor state to read only but keep some user interactions, e.g., keymaps.
* @default false
*/
readOnly?: boolean
/** /**
* Whether the editor is expanded. * Whether the editor is expanded.
* If true, the height and maxHeight props are ignored. * If true, the height and maxHeight props are ignored.
@ -114,6 +119,7 @@ const CodeEditor = ({
style, style,
className, className,
editable = true, editable = true,
readOnly = false,
expanded = true, expanded = true,
wrapped = true wrapped = true
}: CodeEditorProps) => { }: CodeEditorProps) => {
@ -189,6 +195,7 @@ const CodeEditor = ({
maxHeight={expanded ? undefined : maxHeight} maxHeight={expanded ? undefined : maxHeight}
minHeight={minHeight} minHeight={minHeight}
editable={editable} editable={editable}
readOnly={readOnly}
// @ts-ignore 强制使用,见 react-codemirror 的 Example.tsx // @ts-ignore 强制使用,见 react-codemirror 的 Example.tsx
theme={activeCmTheme} theme={activeCmTheme}
extensions={customExtensions} extensions={customExtensions}

View File

@ -1,4 +1,3 @@
import { EditorState } from '@codemirror/state'
import { Modal } from 'antd' import { Modal } from 'antd'
import { useState } from 'react' import { useState } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
@ -56,12 +55,15 @@ const PopupContainer: React.FC<Props> = ({ text, title, extension, resolve }) =>
footer={null}> footer={null}>
{extension !== undefined ? ( {extension !== undefined ? (
<Editor <Editor
readOnly={true}
expanded={false} expanded={false}
height="100%" height="100%"
style={{ height: '100%' }} style={{ height: '100%' }}
value={text} value={text}
language={extension} language={extension}
extensions={[EditorState.readOnly.of(true)]} options={{
keymap: true
}}
/> />
) : ( ) : (
<Text>{text}</Text> <Text>{text}</Text>