Skip to content

Commit 109235d

Browse files
committed
kit useExtension support extensions directly;
adds support to `useExtension`, for using extension or extension array directly, without setup function
1 parent 2ca9dda commit 109235d

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/demo/src/components/CustomCodeMirror.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Box from '@mui/material/Box'
22
import { useCodeMirror } from '@ui-schema/kit-codemirror'
3-
import React, { useCallback } from 'react'
3+
import React, { useCallback, useMemo } from 'react'
44
import {
55
lineNumbers, highlightActiveLineGutter, highlightSpecialChars,
66
drawSelection, dropCursor,
@@ -30,9 +30,6 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps & MuiCodeMirror
3030
...props
3131
},
3232
) => {
33-
const theme = useEditorTheme(typeof onChange === 'undefined', dense, variant)
34-
const highlightStyle = useHighlightStyle()
35-
3633
const extensionsAll: Extension[] = React.useMemo(() => [
3734
lineNumbers(),
3835
EditorView.lineWrapping,
@@ -77,23 +74,23 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps & MuiCodeMirror
7774
})
7875

7976
useExtension(
80-
useCallback(
81-
() => syntaxHighlighting(highlightStyle || defaultHighlightStyle, {fallback: true}),
82-
[highlightStyle],
83-
),
77+
useMemo(() => {
78+
return Prec.lowest(EditorView.editorAttributes.of({class: classNameContent || ''}))
79+
}, [classNameContent]),
8480
editorRef,
8581
)
82+
83+
const highlightStyle = useHighlightStyle()
8684
useExtension(
8785
useCallback(
88-
() => theme,
89-
[theme],
86+
() => syntaxHighlighting(highlightStyle || defaultHighlightStyle, {fallback: true}),
87+
[highlightStyle],
9088
),
9189
editorRef,
9290
)
91+
9392
useExtension(
94-
useCallback(() => {
95-
return Prec.lowest(EditorView.editorAttributes.of({class: classNameContent || ''}))
96-
}, [classNameContent]),
93+
useEditorTheme(typeof onChange === 'undefined', dense, variant),
9794
editorRef,
9895
)
9996

packages/kit-codemirror/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ui-schema/kit-codemirror",
3-
"version": "1.0.0-alpha.0",
3+
"version": "1.0.0-alpha.1",
44
"description": "CodeMirror v6 as React Component, with hooks and stuff - but only the necessities.",
55
"homepage": "https://ui-schema.bemit.codes/docs/kit-codemirror/kit-codemirror",
66
"author": {

packages/kit-codemirror/src/useExtension/useExtension.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { EditorView } from '@codemirror/view'
33
import { useLayoutEffect, useRef } from 'react'
44
import type { RefObject } from 'react'
55

6+
type SetupExtension = (() => Extension) | Extension
7+
68
export const useExtension = (
7-
setupExtension: () => Extension,
9+
setupExtension: SetupExtension,
810
editorRef: RefObject<EditorView | null>,
911
) => {
10-
const configuredRef = useRef<{ view: EditorView, cb: Function } | null>(null)
12+
const configuredRef = useRef<{ view: EditorView, cb: SetupExtension } | null>(null)
1113
const compartmentRef = useRef<Compartment>(new Compartment())
1214

1315
useLayoutEffect(() => {
@@ -25,10 +27,12 @@ export const useExtension = (
2527
// exists, reconfigure
2628
// new, append to config
2729

30+
const extension = typeof setupExtension === 'function' ? setupExtension() : setupExtension
31+
2832
editorRef.current.dispatch({
2933
effects: isInitialSetup
30-
? StateEffect.appendConfig.of(compartment.of(setupExtension()))
31-
: compartment.reconfigure(setupExtension()),
34+
? StateEffect.appendConfig.of(compartment.of(extension))
35+
: compartment.reconfigure(extension),
3236
})
3337
configuredRef.current = {view: editorRef.current, cb: setupExtension}
3438
} else {

0 commit comments

Comments
 (0)