|
| 1 | +import React from 'react' |
| 2 | +import Container from '@mui/material/Container' |
| 3 | +import Box from '@mui/material/Box' |
| 4 | +import Link from '@mui/material/Link' |
| 5 | +import Typography from '@mui/material/Typography' |
| 6 | +import { Nav } from '../components/Nav' |
| 7 | +import { CodeMirrorOnChange } from '@ui-schema/kit-codemirror/useCodeMirror' |
| 8 | +import { CodeMirrorComponentProps, CodeMirror, CodeMirrorProps } from '@ui-schema/kit-codemirror/CodeMirror' |
| 9 | +import { |
| 10 | + lineNumbers, highlightActiveLineGutter, highlightSpecialChars, |
| 11 | + drawSelection, dropCursor, |
| 12 | + rectangularSelection, highlightActiveLine, keymap, EditorView, |
| 13 | + // crosshairCursor, |
| 14 | +} from '@codemirror/view' |
| 15 | +import { foldGutter, indentOnInput, syntaxHighlighting, defaultHighlightStyle, bracketMatching, foldKeymap } from '@codemirror/language' |
| 16 | +import { history, defaultKeymap, historyKeymap, indentWithTab } from '@codemirror/commands' |
| 17 | +import { highlightSelectionMatches, searchKeymap } from '@codemirror/search' |
| 18 | +import { closeBrackets, autocompletion, closeBracketsKeymap, completionKeymap } from '@codemirror/autocomplete' |
| 19 | +import { lintKeymap } from '@codemirror/lint' |
| 20 | +import { Compartment, EditorState, Extension } from '@codemirror/state' |
| 21 | +import { json } from '@codemirror/lang-json' |
| 22 | +import { javascript } from '@codemirror/lang-javascript' |
| 23 | +import { html } from '@codemirror/lang-html' |
| 24 | +import { css } from '@codemirror/lang-css' |
| 25 | +import { MuiCodeMirrorStyleProps, useEditorTheme, useHighlightStyle } from '@ui-schema/material-code' |
| 26 | +import { useExtension } from '@ui-schema/kit-codemirror' |
| 27 | + |
| 28 | +export const PageDemoComponentMui: React.ComponentType = () => { |
| 29 | + return <> |
| 30 | + <Container maxWidth={'md'} fixed style={{display: 'flex', flexGrow: 1, overflow: 'auto'}}> |
| 31 | + <Nav/> |
| 32 | + <Box mx={2} py={1} style={{display: 'flex', flexDirection: 'column', overflow: 'auto', flexGrow: 1}}> |
| 33 | + <Box mb={2}> |
| 34 | + <Typography variant={'h1'} gutterBottom>Component</Typography> |
| 35 | + <Typography variant={'body1'}>Plain React components demo, no UI-Schema widgets, using Material-UI styling with <code>@mui</code>.</Typography> |
| 36 | + <Typography variant={'body1'}>Page filling style, where the scrollable area is the CodeMirror Editor, use <code>variant: &qout;embed&qout;</code> to deactivate outlined/borders-radius.</Typography> |
| 37 | + <Typography variant={'body1'}> |
| 38 | + <Link |
| 39 | + href={'https://github.com/ui-schema/react-codemirror/blob/main/packages/demo/src/pages/PageDemoComponentMui.tsx'} |
| 40 | + target={'_blank'} rel={'noopener noreferrer'} |
| 41 | + >demo source</Link> |
| 42 | + </Typography> |
| 43 | + </Box> |
| 44 | + <Box |
| 45 | + mb={2} |
| 46 | + style={{ |
| 47 | + display: 'flex', |
| 48 | + flexDirection: 'column', |
| 49 | + overflow: 'auto', |
| 50 | + flexGrow: 1, |
| 51 | + }} |
| 52 | + > |
| 53 | + <Typography variant={'h2'} gutterBottom>Content Editor</Typography> |
| 54 | + <DemoComponent/> |
| 55 | + </Box> |
| 56 | + </Box> |
| 57 | + </Container> |
| 58 | + </> |
| 59 | +} |
| 60 | + |
| 61 | +export const CustomCodeMirror: React.FC<CodeMirrorComponentProps & MuiCodeMirrorStyleProps> = ( |
| 62 | + { |
| 63 | + value, |
| 64 | + onChange, |
| 65 | + // MUI style props |
| 66 | + dense, variant, |
| 67 | + // make this a reusable `CodeMirror` component |
| 68 | + // otherwise use `Pick<CodeMirrorComponentProps, 'value' | 'onChange'>` as props |
| 69 | + extensions, |
| 70 | + effects, |
| 71 | + ...props |
| 72 | + }, |
| 73 | +) => { |
| 74 | + const [format] = React.useState('html') |
| 75 | + |
| 76 | + // using a "direct plugin integration with reconfigure support" |
| 77 | + const theme = useEditorTheme(typeof onChange === 'undefined', dense, variant) |
| 78 | + const themeRef = React.useRef<Extension>(theme) |
| 79 | + const themeCompartment = React.useRef<Compartment>(new Compartment()) |
| 80 | + |
| 81 | + // using the `useExtension` hook to help with compartment plugins: |
| 82 | + const highlightStyle = useHighlightStyle() |
| 83 | + const {init: initHighlightExt, effects: effectsHighlightExt} = useExtension( |
| 84 | + () => syntaxHighlighting(highlightStyle || defaultHighlightStyle, {fallback: true}), |
| 85 | + [highlightStyle], |
| 86 | + ) |
| 87 | + |
| 88 | + const effectsRef = React.useRef<((editor: EditorView) => void)[]>(effects || []) |
| 89 | + |
| 90 | + const extensionsAll = React.useMemo(() => [ |
| 91 | + lineNumbers(), |
| 92 | + highlightActiveLineGutter(), |
| 93 | + highlightSpecialChars(), |
| 94 | + history(), |
| 95 | + foldGutter(), |
| 96 | + drawSelection(), |
| 97 | + dropCursor(), |
| 98 | + EditorState.allowMultipleSelections.of(true), |
| 99 | + indentOnInput(), |
| 100 | + syntaxHighlighting(defaultHighlightStyle, {fallback: true}), |
| 101 | + bracketMatching(), |
| 102 | + closeBrackets(), |
| 103 | + autocompletion(), |
| 104 | + rectangularSelection(), |
| 105 | + // crosshairCursor(), |
| 106 | + highlightActiveLine(), |
| 107 | + highlightSelectionMatches(), |
| 108 | + new Compartment().of(EditorState.tabSize.of(4)), |
| 109 | + keymap.of([ |
| 110 | + ...closeBracketsKeymap, |
| 111 | + ...defaultKeymap, |
| 112 | + ...searchKeymap, |
| 113 | + ...historyKeymap, |
| 114 | + ...foldKeymap, |
| 115 | + ...completionKeymap, |
| 116 | + ...lintKeymap, |
| 117 | + indentWithTab, |
| 118 | + ]), |
| 119 | + themeCompartment.current.of(themeRef.current),// only initial theme here to not re-create extensions |
| 120 | + initHighlightExt(), |
| 121 | + ...(format === 'json' ? [json()] : []), |
| 122 | + ...(format === 'javascript' ? [javascript()] : []), |
| 123 | + ...(format === 'html' ? [html()] : []), |
| 124 | + ...(format === 'css' ? [css()] : []), |
| 125 | + ...(extensions || []), |
| 126 | + ], [extensions, format, initHighlightExt]) |
| 127 | + |
| 128 | + // attach parent plugin effects first |
| 129 | + React.useMemo(() => { |
| 130 | + if(!effects) return |
| 131 | + effectsRef.current.push(...effects) |
| 132 | + }, [effects]) |
| 133 | + |
| 134 | + // attach each plugin effect separately (thus only the one which changes get reconfigured) |
| 135 | + React.useMemo(() => { |
| 136 | + // without `useExtension` you get direct access to the current `editor` inside of the effect |
| 137 | + // to otherwise access `editor`, you can't use the component `CodeMirror` but must use the hook `useCodeMirror` |
| 138 | + effectsRef.current.push( |
| 139 | + function updateTheme(editor) { |
| 140 | + editor.dispatch({ |
| 141 | + effects: themeCompartment.current.reconfigure(theme), |
| 142 | + }) |
| 143 | + }, |
| 144 | + ) |
| 145 | + }, [theme]) |
| 146 | + |
| 147 | + React.useMemo(() => { |
| 148 | + effectsRef.current.push(...effectsHighlightExt) |
| 149 | + }, [effectsHighlightExt]) |
| 150 | + |
| 151 | + const onViewLifecycle: CodeMirrorProps['onViewLifecycle'] = React.useCallback((view) => { |
| 152 | + console.log('on-view-lifecycle', view) |
| 153 | + }, []) |
| 154 | + |
| 155 | + return <CodeMirror |
| 156 | + value={value || ''} |
| 157 | + extensions={extensionsAll} |
| 158 | + onChange={onChange} |
| 159 | + onViewLifecycle={onViewLifecycle} |
| 160 | + effects={effectsRef.current.splice(0, effectsRef.current.length)} |
| 161 | + {...props} |
| 162 | + /> |
| 163 | +} |
| 164 | + |
| 165 | +const initialHtml = `<!DOCTYPE html> |
| 166 | +<html lang="en"> |
| 167 | +<head> |
| 168 | + <meta charset="utf-8"/> |
| 169 | + <title>React App</title> |
| 170 | + <style> |
| 171 | + body, html { |
| 172 | + height: 100%; |
| 173 | + margin: 0; |
| 174 | + padding: 0; |
| 175 | + } |
| 176 | + </style> |
| 177 | +</head> |
| 178 | +<body> |
| 179 | +<h1>Lorem ipsum</h1> |
| 180 | +<p>Dolor sit amet, consectutor adipisci.</p> |
| 181 | +</body> |
| 182 | +</html>` |
| 183 | + |
| 184 | +const DemoComponent = () => { |
| 185 | + const [variant] = React.useState('standard') |
| 186 | + const [value, setValue] = React.useState(initialHtml) |
| 187 | + |
| 188 | + const onChange: CodeMirrorOnChange = React.useCallback((editor, newValue) => { |
| 189 | + if(!editor.docChanged || typeof newValue !== 'string') { |
| 190 | + return |
| 191 | + } |
| 192 | + setValue(newValue) |
| 193 | + }, [setValue]) |
| 194 | + |
| 195 | + return <React.Fragment> |
| 196 | + <CustomCodeMirror |
| 197 | + value={value} |
| 198 | + onChange={onChange} |
| 199 | + style={{ |
| 200 | + display: 'flex', |
| 201 | + flexGrow: 1, |
| 202 | + overflow: 'auto', |
| 203 | + ...(variant === 'standard' ? { |
| 204 | + padding: 2,// otherwise outline won't work (or use some hacky negative margin tricks etc.) |
| 205 | + } : {}), |
| 206 | + }} |
| 207 | + /> |
| 208 | + </React.Fragment> |
| 209 | +} |
0 commit comments