Skip to content

Commit 8d0e3dd

Browse files
committed
Merge branch 'develop' into main
2 parents b4fa211 + 141729b commit 8d0e3dd

File tree

26 files changed

+849
-470
lines changed

26 files changed

+849
-470
lines changed

package-lock.json

Lines changed: 0 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/demo/package-lock.json

Lines changed: 171 additions & 184 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/demo/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"@codemirror/language": "^6.1.0",
2828
"@ui-schema/dictionary": "~0.0.10",
2929
"@ui-schema/ds-material": "~0.4.0-alpha.5",
30-
"@ui-schema/kit-codemirror": "~0.0.1",
31-
"@ui-schema/material-code": "~0.4.0-beta.0",
30+
"@ui-schema/kit-codemirror": "~0.1.0",
31+
"@ui-schema/material-code": "~0.4.0",
3232
"@ui-schema/ui-schema": "~0.4.1",
3333
"immutable": "^4.0.0",
3434
"react": "^17.0",

packages/demo/public/index.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@
1010
<!--<link rel="manifest" href="<%= htmlWebpackPlugin.options.PUBLIC_URL %>/manifest.json"/>-->
1111
<title>React App</title>
1212
<style>
13-
html,
14-
body {
13+
body, html {
14+
height: 100%;
1515
margin: 0;
16-
box-sizing: border-box;
16+
padding: 0;
17+
}
18+
19+
#root {
20+
overflow: auto;
21+
height: 100%;
22+
display: flex;
23+
flex-direction: column
24+
}
25+
26+
body {
27+
display: flex;
28+
flex-direction: column
1729
}
1830

1931
*, ::before, ::after {

packages/demo/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { customWidgets } from './components/UISchema'
1212
const customThemes = customTheme('#05aeca')
1313

1414
export const App: React.ComponentType<{}> = () => {
15-
const [themeId] = React.useState<'dark' | 'light'>('dark')
15+
const [themeId, setThemeId] = React.useState<'dark' | 'light'>('dark')
1616

1717
const [theme, setTheme] = React.useState(customThemes[themeId])
1818
React.useEffect(() => {
@@ -26,7 +26,7 @@ export const App: React.ComponentType<{}> = () => {
2626
<BrowserRouter>
2727
<React.Suspense fallback={<CircularProgress/>}>
2828
<UIMetaProvider t={browserT} widgets={customWidgets}>
29-
<Layout/>
29+
<Layout setTheme={setThemeId}/>
3030
</UIMetaProvider>
3131
</React.Suspense>
3232
</BrowserRouter>

packages/demo/src/components/CustomCodeMirror.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,36 @@ import { history, defaultKeymap, historyKeymap, indentWithTab } from '@codemirro
1111
import { highlightSelectionMatches, searchKeymap } from '@codemirror/search'
1212
import { closeBrackets, autocompletion, closeBracketsKeymap, completionKeymap } from '@codemirror/autocomplete'
1313
import { lintKeymap } from '@codemirror/lint'
14-
import { Compartment, EditorState } from '@codemirror/state'
14+
import { Compartment, EditorState, Extension } from '@codemirror/state'
1515
import { useEditorTheme } from '@ui-schema/material-code/useEditorTheme'
1616
import { useHighlightStyle } from '@ui-schema/material-code/useHighlightStyle'
1717
import { CodeMirrorComponentProps, CodeMirror, CodeMirrorProps } from '@ui-schema/kit-codemirror/CodeMirror'
18+
import { useExtension } from '@ui-schema/kit-codemirror/useExtension'
19+
import { MuiCodeMirrorStyleProps } from '@ui-schema/material-code'
1820

19-
export const CustomCodeMirror: React.FC<CodeMirrorComponentProps> = (
21+
export const CustomCodeMirror: React.FC<CodeMirrorComponentProps & MuiCodeMirrorStyleProps> = (
2022
{
2123
// values we want to override in this component
22-
value, extensions,
24+
value, extensions, effects,
25+
dense, variant,
2326
// everything else is just passed down
2427
...props
2528
},
2629
) => {
2730
const {onChange} = props
28-
const theme = useEditorTheme(typeof onChange === 'undefined')
31+
const theme = useEditorTheme(typeof onChange === 'undefined', dense, variant)
2932
const highlightStyle = useHighlightStyle()
33+
const {init: initHighlightExt, effects: effectsHighlightExt} = useExtension(
34+
() => syntaxHighlighting(highlightStyle || defaultHighlightStyle, {fallback: true}),
35+
[highlightStyle],
36+
)
37+
const {init: initThemeExt, effects: effectsThemeExt} = useExtension(
38+
() => theme,
39+
[theme],
40+
)
41+
const effectsRef = React.useRef<((editor: EditorView) => void)[]>(effects || [])
3042

31-
const extensionsAll = React.useMemo(() => [
43+
const extensionsAll: Extension[] = React.useMemo(() => [
3244
lineNumbers(),
3345
EditorView.lineWrapping,
3446
highlightActiveLineGutter(),
@@ -38,16 +50,15 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps> = (
3850
drawSelection(),
3951
dropCursor(),
4052
EditorState.allowMultipleSelections.of(true),
53+
new Compartment().of(EditorState.tabSize.of(4)),
4154
indentOnInput(),
42-
syntaxHighlighting(highlightStyle || defaultHighlightStyle, {fallback: true}),
4355
bracketMatching(),
4456
closeBrackets(),
4557
autocompletion(),
4658
rectangularSelection(),
4759
// crosshairCursor(),
4860
highlightActiveLine(),
4961
highlightSelectionMatches(),
50-
new Compartment().of(EditorState.tabSize.of(4)),
5162
keymap.of([
5263
...closeBracketsKeymap,
5364
...defaultKeymap,
@@ -58,9 +69,24 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps> = (
5869
...lintKeymap,
5970
indentWithTab,
6071
]),
61-
theme,
72+
initHighlightExt(),
73+
initThemeExt(),
6274
...(extensions || []),
63-
], [highlightStyle, extensions, theme])
75+
], [extensions, initHighlightExt, initThemeExt])
76+
77+
// attach parent plugin effects first
78+
React.useMemo(() => {
79+
if(!effects) return
80+
effectsRef.current.push(...effects)
81+
}, [effects])
82+
83+
// attach each plugin effect separately (thus only the one which changes get reconfigured)
84+
React.useMemo(() => {
85+
effectsRef.current.push(...effectsHighlightExt)
86+
}, [effectsHighlightExt])
87+
React.useMemo(() => {
88+
effectsRef.current.push(...effectsThemeExt)
89+
}, [effectsThemeExt])
6490

6591
const onViewLifecycle: CodeMirrorProps['onViewLifecycle'] = React.useCallback((view) => {
6692
console.log('on-view-lifecycle', view)
@@ -70,6 +96,7 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps> = (
7096
value={value || ''}
7197
extensions={extensionsAll}
7298
onViewLifecycle={onViewLifecycle}
99+
effects={effectsRef.current.splice(0, effectsRef.current.length)}
73100
{...props}
74101
// className={className}
75102
/>

packages/demo/src/components/Layout.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@ import { PageHome } from '../pages/PageHome'
44
import { PageDemoWidget } from '../pages/PageDemoWidget'
55
import { PageDemoLangSelectable } from '../pages/PageDemoLangSelectable'
66
import { PageDemoComponent } from '../pages/PageDemoComponent'
7+
import { Button } from '@mui/material'
8+
import { PageDemoComponentMui } from '../pages/PageDemoComponentMui'
79

8-
export const Layout: React.ComponentType<{}> = () => {
10+
export const Layout: React.ComponentType<{ setTheme: React.Dispatch<React.SetStateAction<'dark' | 'light'>> }> = ({setTheme}) => {
911
const scrollWrapper = React.useRef<HTMLDivElement | null>(null)
1012

1113
return <div
1214
ref={scrollWrapper}
1315
style={{
1416
display: 'flex',
1517
flexDirection: 'column',
18+
flexGrow: 1,
1619
maxHeight: '100%',
1720
position: 'relative',
18-
color: '#ffffff',
1921
overflow: 'auto',
2022
padding: '0 12px',
2123
}}
2224
>
2325
<Routes>
2426
<Route path={'/'} element={<PageHome/>}/>
2527
<Route path={'/component'} element={<PageDemoComponent/>}/>
28+
<Route path={'/component-mui'} element={<PageDemoComponentMui/>}/>
2629
<Route path={'/widget'} element={<PageDemoWidget/>}/>
2730
<Route path={'/selectable'} element={<PageDemoLangSelectable/>}/>
2831
</Routes>
32+
<Button size={'small'} onClick={() => setTheme(t => t === 'dark' ? 'light' : 'dark')}>
33+
switch theme
34+
</Button>
2935
</div>
3036
}

packages/demo/src/components/Nav.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export const Nav: React.FC<{}> = () => {
1111
return <Box style={{display: 'flex', flexDirection: 'column', flexShrink: 0}}>
1212
<MuiList>
1313
<ListItemButton onClick={() => navigate('/component')} selected={'/component' === location.pathname}>
14-
<ListItemText primary={'Component'}/>
14+
<ListItemText primary={'Component Plain'}/>
15+
</ListItemButton>
16+
<ListItemButton onClick={() => navigate('/component-mui')} selected={'/component-mui' === location.pathname}>
17+
<ListItemText primary={'Component MUI'}/>
1518
</ListItemButton>
1619
<ListItemButton onClick={() => navigate('/widget')} selected={'/widget' === location.pathname}>
1720
<ListItemText primary={'Widget'}/>

packages/demo/src/pages/PageDemoComponent.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Box from '@mui/material/Box'
44
import Link from '@mui/material/Link'
55
import Typography from '@mui/material/Typography'
66
import { Nav } from '../components/Nav'
7-
import { CodeMirrorOnChange } from '@ui-schema/kit-codemirror'
7+
import { CodeMirrorOnChange } from '@ui-schema/kit-codemirror/useCodeMirror'
88
import { CodeMirrorComponentProps, CodeMirror, CodeMirrorProps } from '@ui-schema/kit-codemirror/CodeMirror'
99
import {
1010
lineNumbers, highlightActiveLineGutter, highlightSpecialChars,
@@ -110,10 +110,11 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps> = (
110110
const DemoComponent = () => {
111111
const [value, setValue] = React.useState('')
112112

113-
const onChange: CodeMirrorOnChange = React.useCallback((_editor, nextValue, prevValue) => {
114-
if(nextValue !== prevValue) {
115-
setValue(nextValue)
113+
const onChange: CodeMirrorOnChange = React.useCallback((editor, newValue) => {
114+
if(!editor.docChanged || typeof newValue !== 'string') {
115+
return
116116
}
117+
setValue(newValue)
117118
}, [setValue])
118119

119120
return <React.Fragment>
@@ -128,10 +129,11 @@ const DemoComponentReadOnly = () => {
128129
const [readOnly, setReadOnly] = React.useState(false)
129130
const [value, setValue] = React.useState('')
130131

131-
const onChange: CodeMirrorOnChange = React.useCallback((_editor, nextValue, prevValue) => {
132-
if(nextValue !== prevValue) {
133-
setValue(nextValue)
132+
const onChange: CodeMirrorOnChange = React.useCallback((editor, newValue) => {
133+
if(!editor.docChanged || typeof newValue !== 'string') {
134+
return
134135
}
136+
setValue(newValue)
135137
}, [setValue])
136138

137139
return <React.Fragment>

0 commit comments

Comments
 (0)