Skip to content

Commit 674f386

Browse files
committed
feat: Add editable props.
1 parent 6e82e5d commit 674f386

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export interface ReactCodeMirrorProps
2727
* @default true
2828
*/
2929
basicSetup?: boolean;
30+
/**
31+
* This disables editing of the editor content by the user.
32+
* @default true
33+
*/
34+
editable?: boolean;
3035
/**
3136
* Whether to optional basicSetup by default
3237
* @default true
@@ -69,6 +74,7 @@ const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProp
6974
maxWidth,
7075
basicSetup,
7176
indentWithTab,
77+
editable,
7278
...other
7379
} = props;
7480
const editor = useRef<HTMLDivElement>(null);
@@ -85,6 +91,7 @@ const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProp
8591
maxWidth,
8692
basicSetup,
8793
indentWithTab,
94+
editable,
8895
selection,
8996
onChange,
9097
onUpdate,

src/useCodeMirror.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { basicSetup as defaultBasicSetup } from '@codemirror/basic-setup';
33
import { EditorState, StateEffect } from '@codemirror/state';
44
import { indentWithTab as defaultIndentWithTab } from '@codemirror/commands';
55
import { EditorView, keymap, ViewUpdate } from '@codemirror/view';
6-
import { ReactCodeMirrorProps } from './';
76
import { oneDarkTheme } from '@codemirror/theme-one-dark';
7+
import { ReactCodeMirrorProps } from './';
88
import { defaultLightThemeOption } from './theme/light';
99

1010
export interface UseCodeMirror extends ReactCodeMirrorProps {
@@ -26,6 +26,7 @@ export function useCodeMirror(props: UseCodeMirror) {
2626
width = '',
2727
minWidth = '',
2828
maxWidth = '',
29+
editable = true,
2930
indentWithTab = true,
3031
basicSetup = true,
3132
} = props;
@@ -56,7 +57,13 @@ export function useCodeMirror(props: UseCodeMirror) {
5657
if (basicSetup) {
5758
getExtensions.unshift(defaultBasicSetup);
5859
}
60+
5961
theme === 'light' ? getExtensions.push(defaultLightThemeOption) : getExtensions.push(oneDarkTheme);
62+
63+
if (editable === false) {
64+
getExtensions.push(EditorView.editable.of(false));
65+
}
66+
6067
if (onUpdate && typeof onUpdate === 'function') {
6168
getExtensions.push(EditorView.updateListener.of(onUpdate));
6269
}
@@ -103,7 +110,7 @@ export function useCodeMirror(props: UseCodeMirror) {
103110
view.dispatch({ effects: StateEffect.reconfigure.of(getExtensions) });
104111
}
105112
// eslint-disable-next-line react-hooks/exhaustive-deps
106-
}, [theme, extensions, height, minHeight, maxHeight, width, minWidth, maxWidth]);
113+
}, [theme, extensions, height, minHeight, maxHeight, width, minWidth, maxWidth, editable, indentWithTab, basicSetup]);
107114

108115
useEffect(() => {
109116
if (autoFocus && view) {

website/App.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ let count = 0;
127127
export default function App() {
128128
const [mode, setMode] = useState('javascript');
129129
const [autofocus, setAutofocus] = useState(false);
130+
const [editable, setEditable] = useState(true);
130131
const [theme, setTheme] = useState<ReactCodeMirrorProps['theme']>('light');
131132
const [code, setCode] = useState('');
132133
const [extensions, setExtensions] = useState<Extension[]>();
@@ -185,6 +186,7 @@ export default function App() {
185186
value={code}
186187
height={height}
187188
theme={theme}
189+
editable={editable}
188190
extensions={extensions}
189191
autoFocus={autofocus}
190192
className={styles.codemirror}
@@ -224,6 +226,10 @@ export default function App() {
224226
<input type="checkbox" checked={autofocus} onChange={(evn) => setAutofocus(evn.target.checked)} />
225227
autoFocus
226228
</label>
229+
<label>
230+
<input type="checkbox" checked={editable} onChange={(evn) => setEditable(evn.target.checked)} />
231+
editable
232+
</label>
227233
</div>
228234
<MarkdownPreview className={styles.markdown} source={DocumentStr.replace(/([\s\S]*)<!--dividing-->/, '')} />
229235
<div className={styles.footer}>

0 commit comments

Comments
 (0)