diff options
author | 2021-07-01 12:31:07 +0200 | |
---|---|---|
committer | 2021-07-01 12:31:07 +0200 | |
commit | 8595c9a22830b1baacc49bb694d70389b2539eba (patch) | |
tree | cb496cd34661a0d3ec4c9b5a333e948f24e437f4 /language-web/src | |
parent | Convert to React app (diff) | |
download | refinery-8595c9a22830b1baacc49bb694d70389b2539eba.tar.gz refinery-8595c9a22830b1baacc49bb694d70389b2539eba.tar.zst refinery-8595c9a22830b1baacc49bb694d70389b2539eba.zip |
Add material-ui and mobx integration
Diffstat (limited to 'language-web/src')
-rw-r--r-- | language-web/src/main/css/index.scss | 64 | ||||
-rw-r--r-- | language-web/src/main/js/App.jsx | 73 | ||||
-rw-r--r-- | language-web/src/main/js/Navbar.jsx | 37 | ||||
-rw-r--r-- | language-web/src/main/js/RootStore.jsx | 30 | ||||
-rw-r--r-- | language-web/src/main/js/components/XtextCodeMirror.jsx | 32 | ||||
-rw-r--r-- | language-web/src/main/js/editor/Editor.jsx | 50 | ||||
-rw-r--r-- | language-web/src/main/js/editor/EditorButtons.jsx | 27 | ||||
-rw-r--r-- | language-web/src/main/js/editor/EditorStore.jsx | 75 | ||||
-rw-r--r-- | language-web/src/main/js/index.jsx | 21 |
9 files changed, 311 insertions, 98 deletions
diff --git a/language-web/src/main/css/index.scss b/language-web/src/main/css/index.scss index e788bbd1..62109a2f 100644 --- a/language-web/src/main/css/index.scss +++ b/language-web/src/main/css/index.scss | |||
@@ -3,70 +3,6 @@ | |||
3 | 3 | ||
4 | @import 'xtext/xtext-codemirror'; | 4 | @import 'xtext/xtext-codemirror'; |
5 | 5 | ||
6 | body { | ||
7 | width: 100%; | ||
8 | height: 100%; | ||
9 | overflow: hidden; | ||
10 | font: 16px Helvetica,sans-serif; | ||
11 | } | ||
12 | |||
13 | a { | ||
14 | color: #22a; | ||
15 | text-decoration: none; | ||
16 | } | ||
17 | |||
18 | a:hover { | ||
19 | text-decoration: underline; | ||
20 | } | ||
21 | |||
22 | .container { | ||
23 | display: block; | ||
24 | position: absolute; | ||
25 | top: 0; | ||
26 | bottom: 0; | ||
27 | left: 0; | ||
28 | right: 0; | ||
29 | margin: 24px 24px 36px 24px; | ||
30 | } | ||
31 | |||
32 | .header { | ||
33 | display: block; | ||
34 | position: absolute; | ||
35 | top: 0; | ||
36 | left: 0; | ||
37 | right: 0; | ||
38 | padding: 16px; | ||
39 | background: #dbdbdb; | ||
40 | border-radius: 16px; | ||
41 | color: #383838; | ||
42 | } | ||
43 | |||
44 | .header h1 { | ||
45 | font-size: 36px; | ||
46 | margin: 0; | ||
47 | } | ||
48 | |||
49 | .content { | ||
50 | display: block; | ||
51 | position: absolute; | ||
52 | top: 108px; | ||
53 | bottom: 0; | ||
54 | left: 0; | ||
55 | width: 100%; | ||
56 | box-shadow: 0 0 8px rgba(80, 10, 98, .25), 0 10px 24px rgba(80, 10, 98, .35); | ||
57 | border-radius: 16px; | ||
58 | } | ||
59 | |||
60 | .xtext-editor { | ||
61 | display: block; | ||
62 | position: absolute; | ||
63 | top: 16px; | ||
64 | bottom: 16px; | ||
65 | left: 16px; | ||
66 | right: 16px; | ||
67 | border: 1px solid #999; | ||
68 | } | ||
69 | |||
70 | .problem-class { | 6 | .problem-class { |
71 | color: #005032; | 7 | color: #005032; |
72 | } | 8 | } |
diff --git a/language-web/src/main/js/App.jsx b/language-web/src/main/js/App.jsx new file mode 100644 index 00000000..61ded943 --- /dev/null +++ b/language-web/src/main/js/App.jsx | |||
@@ -0,0 +1,73 @@ | |||
1 | import React from 'react'; | ||
2 | import { makeStyles } from '@material-ui/core/styles'; | ||
3 | import AppBar from '@material-ui/core/AppBar'; | ||
4 | import Box from '@material-ui/core/Box'; | ||
5 | import Fab from '@material-ui/core/Fab'; | ||
6 | import IconButton from '@material-ui/core/IconButton'; | ||
7 | import Toolbar from '@material-ui/core/Toolbar'; | ||
8 | import Typography from '@material-ui/core/Typography'; | ||
9 | import MenuIcon from '@material-ui/icons/Menu'; | ||
10 | import PlayArrowIcon from '@material-ui/icons/PlayArrow'; | ||
11 | |||
12 | import Editor from './editor/Editor'; | ||
13 | import EditorButtons from './editor/EditorButtons'; | ||
14 | |||
15 | const useStyles = makeStyles(theme => ({ | ||
16 | menuButton: { | ||
17 | marginRight: theme.spacing(2), | ||
18 | }, | ||
19 | title: { | ||
20 | flexGrow: 1, | ||
21 | }, | ||
22 | fab: { | ||
23 | position: 'fixed', | ||
24 | right: theme.spacing(3), | ||
25 | bottom: theme.spacing(3), | ||
26 | zIndex: 1000, | ||
27 | }, | ||
28 | extendedIcon: { | ||
29 | marginRight: theme.spacing(1), | ||
30 | } | ||
31 | })); | ||
32 | |||
33 | export default () => { | ||
34 | const classes = useStyles(); | ||
35 | |||
36 | return ( | ||
37 | <> | ||
38 | <AppBar | ||
39 | position='static' | ||
40 | > | ||
41 | <Toolbar> | ||
42 | <IconButton | ||
43 | edge='start' | ||
44 | className={classes.menuButton} | ||
45 | color='inherit' | ||
46 | aria-label='menu' | ||
47 | > | ||
48 | <MenuIcon /> | ||
49 | </IconButton> | ||
50 | <Typography | ||
51 | variant='h6' | ||
52 | component='h1' | ||
53 | className={classes.title} | ||
54 | > | ||
55 | GraphSolver | ||
56 | </Typography> | ||
57 | </Toolbar> | ||
58 | </AppBar> | ||
59 | <Box> | ||
60 | <EditorButtons/> | ||
61 | </Box> | ||
62 | <Editor/> | ||
63 | <Fab | ||
64 | variant='extended' | ||
65 | color='secondary' | ||
66 | className={classes.fab} | ||
67 | > | ||
68 | <PlayArrowIcon className={classes.extendedIcon}/> | ||
69 | Generate | ||
70 | </Fab> | ||
71 | </> | ||
72 | ); | ||
73 | }; | ||
diff --git a/language-web/src/main/js/Navbar.jsx b/language-web/src/main/js/Navbar.jsx new file mode 100644 index 00000000..cf1bc54f --- /dev/null +++ b/language-web/src/main/js/Navbar.jsx | |||
@@ -0,0 +1,37 @@ | |||
1 | import React from 'react'; | ||
2 | import { Button, Navbar, Nav } from 'react-bootstrap'; | ||
3 | import { FaGithub, FaPlayCircle } from 'react-icons/fa'; | ||
4 | |||
5 | export default () => ( | ||
6 | <Navbar | ||
7 | variant='secondary' | ||
8 | className='px-2' | ||
9 | > | ||
10 | <Navbar.Brand>GraphSolver</Navbar.Brand> | ||
11 | <Navbar.Toggle aria-controls='basic-navbar-nav'/> | ||
12 | <Navbar.Collapse id='basic-navbar-nav'> | ||
13 | <Nav className="me-auto"> | ||
14 | <Nav.Link | ||
15 | href="https://github.com/viatra/VIATRA-Generator#readme" | ||
16 | > | ||
17 | About | ||
18 | </Nav.Link> | ||
19 | <Nav.Link | ||
20 | href="https://github.com/viatra/VIATRA-Generator/wiki" | ||
21 | > | ||
22 | Getting started | ||
23 | </Nav.Link> | ||
24 | <Nav.Link | ||
25 | href="https://github.com/viatra/VIATRA-Generator" | ||
26 | > | ||
27 | <FaGithub/> Github | ||
28 | </Nav.Link> | ||
29 | </Nav> | ||
30 | <Button | ||
31 | variant='success' | ||
32 | > | ||
33 | <FaPlayCircle/> Generate | ||
34 | </Button> | ||
35 | </Navbar.Collapse> | ||
36 | </Navbar> | ||
37 | ); | ||
diff --git a/language-web/src/main/js/RootStore.jsx b/language-web/src/main/js/RootStore.jsx new file mode 100644 index 00000000..1ee2823d --- /dev/null +++ b/language-web/src/main/js/RootStore.jsx | |||
@@ -0,0 +1,30 @@ | |||
1 | import { makeAutoObservable } from 'mobx'; | ||
2 | import React, { createContext, useContext } from 'react'; | ||
3 | |||
4 | import EditorStore from './editor/EditorStore'; | ||
5 | |||
6 | export default class RootStore { | ||
7 | editorStore; | ||
8 | |||
9 | constructor() { | ||
10 | this.editorStore = new EditorStore(); | ||
11 | makeAutoObservable(this); | ||
12 | } | ||
13 | } | ||
14 | |||
15 | const StoreContext = createContext(undefined); | ||
16 | |||
17 | export const RootStoreProvider = ({ children, rootStore }) => ( | ||
18 | <StoreContext.Provider value={rootStore}> | ||
19 | {children} | ||
20 | </StoreContext.Provider> | ||
21 | ); | ||
22 | |||
23 | /** @returns {RootStore} */ | ||
24 | export const useRootStore = () => { | ||
25 | const rootStore = useContext(StoreContext); | ||
26 | if (!rootStore) { | ||
27 | throw new Error('useRootStore must be used within RootStoreProvider'); | ||
28 | } | ||
29 | return rootStore; | ||
30 | }; | ||
diff --git a/language-web/src/main/js/components/XtextCodeMirror.jsx b/language-web/src/main/js/components/XtextCodeMirror.jsx deleted file mode 100644 index 75a20daa..00000000 --- a/language-web/src/main/js/components/XtextCodeMirror.jsx +++ /dev/null | |||
@@ -1,32 +0,0 @@ | |||
1 | import React, { useCallback, useState } from 'react'; | ||
2 | import { Controlled as CodeMirror } from 'react-codemirror2'; | ||
3 | import 'mode-problem'; | ||
4 | import { createServices, removeServices } from 'xtext/xtext-codemirror'; | ||
5 | |||
6 | export default function XtextCodeMirror({ initialValue }) { | ||
7 | const [value, setValue] = useState(initialValue); | ||
8 | |||
9 | const codeMirrorOptions = { | ||
10 | mode: 'xtext/problem', | ||
11 | indentUnit: 2, | ||
12 | }; | ||
13 | |||
14 | const xtextOptions = { | ||
15 | xtextLang: 'problem', | ||
16 | enableFormattingAction: true, | ||
17 | }; | ||
18 | |||
19 | const onBeforeChange = useCallback((_editor, _data, newValue) => { | ||
20 | setValue(newValue); | ||
21 | }, [setValue]); | ||
22 | |||
23 | return ( | ||
24 | <CodeMirror | ||
25 | value={value} | ||
26 | options={codeMirrorOptions} | ||
27 | editorDidMount={editor => createServices(editor, xtextOptions)} | ||
28 | editorWillUnmount={removeServices} | ||
29 | onBeforeChange={onBeforeChange} | ||
30 | /> | ||
31 | ); | ||
32 | }; | ||
diff --git a/language-web/src/main/js/editor/Editor.jsx b/language-web/src/main/js/editor/Editor.jsx new file mode 100644 index 00000000..c4b2e183 --- /dev/null +++ b/language-web/src/main/js/editor/Editor.jsx | |||
@@ -0,0 +1,50 @@ | |||
1 | import { observer } from 'mobx-react-lite'; | ||
2 | import 'mode-problem'; | ||
3 | import React, { useCallback } from 'react'; | ||
4 | import { Controlled as CodeMirror } from 'react-codemirror2'; | ||
5 | import { createServices, removeServices } from 'xtext/xtext-codemirror'; | ||
6 | |||
7 | import { useRootStore } from '../RootStore'; | ||
8 | |||
9 | export default observer(() => { | ||
10 | const editorStore = useRootStore().editorStore; | ||
11 | |||
12 | const codeMirrorOptions = { | ||
13 | mode: 'xtext/problem', | ||
14 | indentUnit: 2, | ||
15 | }; | ||
16 | |||
17 | const xtextOptions = { | ||
18 | xtextLang: 'problem', | ||
19 | enableFormattingAction: true, | ||
20 | } | ||
21 | |||
22 | const editorDidMount = useCallback((editor) => { | ||
23 | createServices(editor, xtextOptions); | ||
24 | editorStore.updateEditor(editor); | ||
25 | }, [editorStore]); | ||
26 | |||
27 | const editorWillUnmount = useCallback((editor) => { | ||
28 | editorStore.editor = null; | ||
29 | removeServices(editor); | ||
30 | }, [editorStore]); | ||
31 | |||
32 | const onBeforeChange = useCallback((_editor, _data, value) => { | ||
33 | editorStore.updateValue(value); | ||
34 | }, [editorStore]); | ||
35 | |||
36 | const onChange = useCallback((_editor, _data, _value) => { | ||
37 | editorStore.reportChanged(); | ||
38 | }, [editorStore]); | ||
39 | |||
40 | return ( | ||
41 | <CodeMirror | ||
42 | value={editorStore.value} | ||
43 | options={codeMirrorOptions} | ||
44 | editorDidMount={editorDidMount} | ||
45 | editorWillUnmount={editorWillUnmount} | ||
46 | onBeforeChange={onBeforeChange} | ||
47 | onChange={onChange} | ||
48 | /> | ||
49 | ); | ||
50 | }); | ||
diff --git a/language-web/src/main/js/editor/EditorButtons.jsx b/language-web/src/main/js/editor/EditorButtons.jsx new file mode 100644 index 00000000..b9f0d076 --- /dev/null +++ b/language-web/src/main/js/editor/EditorButtons.jsx | |||
@@ -0,0 +1,27 @@ | |||
1 | import { observer } from 'mobx-react-lite'; | ||
2 | import React from 'react'; | ||
3 | import IconButton from '@material-ui/core/IconButton'; | ||
4 | import RedoIcon from '@material-ui/icons/Redo'; | ||
5 | import UndoIcon from '@material-ui/icons/Undo'; | ||
6 | |||
7 | import { useRootStore } from '../RootStore'; | ||
8 | |||
9 | export default observer(() => { | ||
10 | const editorStore = useRootStore().editorStore; | ||
11 | return ( | ||
12 | <> | ||
13 | <IconButton | ||
14 | disabled={!editorStore.canUndo} | ||
15 | onClick={() => editorStore.undo()} | ||
16 | > | ||
17 | <UndoIcon fontSize='small'/> | ||
18 | </IconButton> | ||
19 | <IconButton | ||
20 | disabled={!editorStore.canRedo} | ||
21 | onClick={() => editorStore.redo()} | ||
22 | > | ||
23 | <RedoIcon fontSize='small'/> | ||
24 | </IconButton> | ||
25 | </> | ||
26 | ); | ||
27 | }); | ||
diff --git a/language-web/src/main/js/editor/EditorStore.jsx b/language-web/src/main/js/editor/EditorStore.jsx new file mode 100644 index 00000000..9c286c28 --- /dev/null +++ b/language-web/src/main/js/editor/EditorStore.jsx | |||
@@ -0,0 +1,75 @@ | |||
1 | import CodeMirror from 'codemirror'; | ||
2 | import { createAtom, makeAutoObservable, observable } from 'mobx'; | ||
3 | |||
4 | export default class EditorStore { | ||
5 | atom; | ||
6 | /** @type {CodeMirror} */ | ||
7 | editor = null; | ||
8 | /** @type {string} */ | ||
9 | value = ''; | ||
10 | |||
11 | constructor() { | ||
12 | this.atom = createAtom('EditorStore'); | ||
13 | makeAutoObservable(this, { | ||
14 | atom: false, | ||
15 | editor: observable.ref, | ||
16 | }); | ||
17 | } | ||
18 | |||
19 | /** | ||
20 | * Attaches a new CodeMirror instance. | ||
21 | * | ||
22 | * The store will node subscribe to any CodeMirror events. Instead, | ||
23 | * the editor component should subscribe to them and relay them to the store. | ||
24 | * | ||
25 | * @param {CodeMirror} newEditor The new CodeMirror instance | ||
26 | */ | ||
27 | updateEditor(newEditor) { | ||
28 | this.editor = newEditor; | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Updates the contents of the editor. | ||
33 | * | ||
34 | * @param {string} newValue The new contents of the editor | ||
35 | */ | ||
36 | updateValue(newValue) { | ||
37 | this.value = newValue; | ||
38 | } | ||
39 | |||
40 | reportChanged() { | ||
41 | this.atom.reportChanged(); | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * @returns {boolean} `true` if there is history to undo | ||
46 | */ | ||
47 | get canUndo() { | ||
48 | this.atom.reportObserved(); | ||
49 | if (!this.editor) { | ||
50 | return false; | ||
51 | } | ||
52 | const { undo: undoSize } = this.editor.historySize(); | ||
53 | return undoSize > 0; | ||
54 | } | ||
55 | |||
56 | undo() { | ||
57 | this.editor.undo(); | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * @returns {boolean} `true` if there is history to redo | ||
62 | */ | ||
63 | get canRedo() { | ||
64 | this.atom.reportObserved(); | ||
65 | if (!this.editor) { | ||
66 | return false; | ||
67 | } | ||
68 | const { redo: redoSize } = this.editor.historySize(); | ||
69 | return redoSize > 0; | ||
70 | } | ||
71 | |||
72 | redo() { | ||
73 | this.editor.redo(); | ||
74 | } | ||
75 | } | ||
diff --git a/language-web/src/main/js/index.jsx b/language-web/src/main/js/index.jsx index 48c0baeb..17e6aef2 100644 --- a/language-web/src/main/js/index.jsx +++ b/language-web/src/main/js/index.jsx | |||
@@ -1,7 +1,11 @@ | |||
1 | import React from 'react'; | 1 | import React from 'react'; |
2 | import { render } from 'react-dom'; | 2 | import { render } from 'react-dom'; |
3 | import { createMuiTheme } from '@material-ui/core/styles'; | ||
4 | import { ThemeProvider } from '@material-ui/styles'; | ||
3 | 5 | ||
4 | import XtextCodeMirror from "./components/XtextCodeMirror"; | 6 | import App from './App'; |
7 | import CssBaseline from '@material-ui/core/CssBaseline'; | ||
8 | import RootStore, { RootStoreProvider } from './RootStore'; | ||
5 | 9 | ||
6 | import '../css/index.scss'; | 10 | import '../css/index.scss'; |
7 | 11 | ||
@@ -38,6 +42,19 @@ age(bob, bobAge). | |||
38 | 42 | ||
39 | scope Family = 1, Person += 5..10. | 43 | scope Family = 1, Person += 5..10. |
40 | `; | 44 | `; |
41 | const app = <XtextCodeMirror initialValue={initialValue}/>; | 45 | |
46 | const rootStore = new RootStore(); | ||
47 | rootStore.editorStore.updateValue(initialValue); | ||
48 | |||
49 | const theme = createMuiTheme(); | ||
50 | |||
51 | const app = ( | ||
52 | <ThemeProvider theme={theme}> | ||
53 | <CssBaseline/> | ||
54 | <RootStoreProvider rootStore={rootStore}> | ||
55 | <App/> | ||
56 | </RootStoreProvider> | ||
57 | </ThemeProvider> | ||
58 | ) | ||
42 | 59 | ||
43 | render(app, document.getElementById('app')); | 60 | render(app, document.getElementById('app')); |