GeezerVison is showing github.com — Add layout feature · byplus/algorithm-visualizer@c7ec896
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/backend/controllers/tracers.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,7 +38,7 @@ const trace = lang => (req, res, next) => { | 38 | 38 | }).finally(() => clearTimeout(timer)); | 39 | 39 | }) | 40 | 40 | .then(() => new Promise((resolve, reject) => { | 41 | | - const visualizationPath = path.resolve(tempPath, 'traces.json'); | | 41 | + const visualizationPath = path.resolve(tempPath, 'visualization.json'); | 42 | 42 | res.sendFile(visualizationPath, err => { | 43 | 43 | if (err) return reject(new Error('Visualization Not Found')); | 44 | 44 | resolve(); | |
Collapse file Copy file name to clipboardExpand all lines: src/backend/tracers/js/worker.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,5 +10,5 @@ onmessage = e => { | 10 | 10 | const lines = e.data.split('\n').map((line, i) => line.replace(/(\.\s*delay\s*)\(\s*\)/g, `$1(${i})`)); | 11 | 11 | const code = lines.join('\n'); | 12 | 12 | sandbox(code); | 13 | | - postMessage(AlgorithmVisualizer.Tracer.traces); | | 13 | + postMessage(AlgorithmVisualizer.Commander.commands); | 14 | 14 | }; |
Collapse file Copy file name to clipboardExpand all lines: src/frontend/apis/index.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -69,11 +69,11 @@ const GitHubApi = { | 69 | 69 |
| 70 | 70 | const TracerApi = { | 71 | 71 | md: ({ code }) => Promise.resolve([{ | 72 | | - tracerKey: '0-MarkdownTracer-Markdown', | 73 | | - method: 'construct', | 74 | | - args: ['MarkdownTracer', 'Markdown'], | | 72 | + key: 'markdown', | | 73 | + method: 'MarkdownTracer', | | 74 | + args: ['Markdown'], | 75 | 75 | }, { | 76 | | - tracerKey: '0-MarkdownTracer-Markdown', | | 76 | + key: 'markdown', | 77 | 77 | method: 'set', | 78 | 78 | args: [code], | 79 | 79 | }]), | |
Collapse file Copy file name to clipboardExpand all lines: src/frontend/components/App/index.jsx +23-11Lines changed: 23 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,13 +27,16 @@ class App extends BaseComponent { | 27 | 27 | super(props); | 28 | 28 |
| 29 | 29 | this.state = { | 30 | | - navigatorOpened: true, | | 30 | + workspaceVisibles: [true, true, true], | 31 | 31 | workspaceWeights: [1, 2, 2], | 32 | 32 | }; | 33 | 33 |
| 34 | 34 | this.codeEditorRef = React.createRef(); | 35 | 35 |
| 36 | 36 | this.ignoreHistoryBlock = this.ignoreHistoryBlock.bind(this); | | 37 | + this.handleClickTitleBar = this.handleClickTitleBar.bind(this); | | 38 | + this.loadScratchPapers = this.loadScratchPapers.bind(this); | | 39 | + this.handleChangeWorkspaceWeights = this.handleChangeWorkspaceWeights.bind(this); | 37 | 40 | } | 38 | 41 |
| 39 | 42 | componentDidMount() { | @@ -161,7 +164,7 @@ class App extends BaseComponent { | 161 | 164 | login: undefined, | 162 | 165 | gistId, | 163 | 166 | title: 'Untitled', | 164 | | - files: [CONTRIBUTING_MD, createUserFile('traces.json', JSON.stringify(content))], | | 167 | + files: [CONTRIBUTING_MD, createUserFile('visualization.json', JSON.stringify(content))], | 165 | 168 | }); | 166 | 169 | }); | 167 | 170 | } else if (gistId === 'new') { | @@ -182,7 +185,10 @@ class App extends BaseComponent { | 182 | 185 | return Promise.resolve(); | 183 | 186 | }; | 184 | 187 | fetch() | 185 | | - .then(() => this.selectDefaultTab()) | | 188 | + .then(() => { | | 189 | + this.selectDefaultTab(); | | 190 | + return null; // to suppress unnecessary bluebird warning | | 191 | + }) | 186 | 192 | .catch(error => { | 187 | 193 | this.handleError(error); | 188 | 194 | this.props.history.push('/'); | @@ -204,28 +210,34 @@ class App extends BaseComponent { | 204 | 210 | this.codeEditorRef.current.getWrappedInstance().handleResize(); | 205 | 211 | } | 206 | 212 |
| 207 | | - toggleNavigatorOpened(navigatorOpened = !this.state.navigatorOpened) { | 208 | | - this.setState({ navigatorOpened }); | | 213 | + toggleNavigatorOpened(navigatorOpened = !this.state.workspaceVisibles[0]) { | | 214 | + const workspaceVisibles = [...this.state.workspaceVisibles]; | | 215 | + workspaceVisibles[0] = navigatorOpened; | | 216 | + this.setState({ workspaceVisibles }); | 209 | 217 | } | 210 | 218 |
| 211 | | - render() { | 212 | | - const { navigatorOpened, workspaceWeights } = this.state; | | 219 | + handleClickTitleBar() { | | 220 | + this.toggleNavigatorOpened(); | | 221 | + } | 213 | 222 |
| | 223 | + render() { | | 224 | + const { workspaceVisibles, workspaceWeights } = this.state; | 214 | 225 | const { titles, description, saved } = this.props.current; | | 226 | + | 215 | 227 | const title = `${saved ? '' : '(Unsaved) '}${titles.join(' - ')}`; | | 228 | + const [navigatorOpened] = workspaceVisibles; | 216 | 229 |
| 217 | 230 | return ( | 218 | 231 | <div className={styles.app}> | 219 | 232 | <Helmet> | 220 | 233 | <title>{title}</title> | 221 | 234 | <meta name="description" content={description} /> | 222 | 235 | </Helmet> | 223 | | - <Header className={styles.header} onClickTitleBar={() => this.toggleNavigatorOpened()} | 224 | | - navigatorOpened={navigatorOpened} loadScratchPapers={() => this.loadScratchPapers()} | | 236 | + <Header className={styles.header} onClickTitleBar={this.handleClickTitleBar} | | 237 | + navigatorOpened={navigatorOpened} loadScratchPapers={this.loadScratchPapers} | 225 | 238 | ignoreHistoryBlock={this.ignoreHistoryBlock} /> | 226 | 239 | <ResizableContainer className={styles.workspace} horizontal weights={workspaceWeights} | 227 | | - visibles={[navigatorOpened, true, true]} | 228 | | - onChangeWeights={weights => this.handleChangeWorkspaceWeights(weights)}> | | 240 | + visibles={workspaceVisibles} onChangeWeights={this.handleChangeWorkspaceWeights}> | 229 | 241 | <Navigator /> | 230 | 242 | <VisualizationViewer className={styles.visualization_viewer} /> | 231 | 243 | <TabContainer className={styles.editor_tab_container}> | |
Collapse file Copy file name to clipboardExpand all lines: src/frontend/components/CodeEditor/index.jsx +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,7 +17,7 @@ class CodeEditor extends React.Component { | 17 | 17 | } | 18 | 18 |
| 19 | 19 | handleResize() { | 20 | | - this.aceEditorRef.current.editor.resize(); | | 20 | + this.aceEditorRef.current.getWrappedInstance().resize(); | 21 | 21 | } | 22 | 22 |
| 23 | 23 | render() { | |
Collapse file Copy file name to clipboardExpand all lines: src/frontend/components/FoldableAceEditor/index.jsx +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,7 +11,7 @@ import 'brace/theme/tomorrow_night_eighties'; | 11 | 11 | import 'brace/ext/searchbox'; | 12 | 12 | import { actions } from '/reducers'; | 13 | 13 |
| 14 | | -@connect(({ current }) => ({ current }), actions) | | 14 | +@connect(({ current }) => ({ current }), actions, null, { withRef: true }) | 15 | 15 | class FoldableAceEditor extends AceEditor { | 16 | 16 | componentDidMount() { | 17 | 17 | super.componentDidMount(); | @@ -40,6 +40,10 @@ class FoldableAceEditor extends AceEditor { | 40 | 40 | } | 41 | 41 | } | 42 | 42 | } | | 43 | + | | 44 | + resize() { | | 45 | + this.editor.resize(); | | 46 | + } | 43 | 47 | } | 44 | 48 |
| 45 | 49 | export default FoldableAceEditor; |
Collapse file Copy file name to clipboardExpand all lines: src/frontend/components/Player/index.jsx +11-10Lines changed: 11 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,22 +41,23 @@ class Player extends BaseComponent { | 41 | 41 | } | 42 | 42 | } | 43 | 43 |
| 44 | | - reset(traces = []) { | | 44 | + reset(commands = []) { | 45 | 45 | const chunks = [{ | 46 | | - traces: [], | | 46 | + commands: [], | 47 | 47 | lineNumber: undefined, | 48 | 48 | }]; | 49 | | - while (traces.length) { | 50 | | - const trace = traces.shift(); | 51 | | - if (trace.method === 'delay') { | 52 | | - const [lineNumber] = trace.args; | | 49 | + while (commands.length) { | | 50 | + const command = commands.shift(); | | 51 | + const { key, method, args } = command; | | 52 | + if (key === null && method === 'delay') { | | 53 | + const [lineNumber] = args; | 53 | 54 | chunks[chunks.length - 1].lineNumber = lineNumber; | 54 | 55 | chunks.push({ | 55 | | - traces: [], | | 56 | + commands: [], | 56 | 57 | lineNumber: undefined, | 57 | 58 | }); | 58 | 59 | } else { | 59 | | - chunks[chunks.length - 1].traces.push(trace); | | 60 | + chunks[chunks.length - 1].commands.push(command); | 60 | 61 | } | 61 | 62 | } | 62 | 63 | this.props.setChunks(chunks); | @@ -76,10 +77,10 @@ class Player extends BaseComponent { | 76 | 77 | const ext = extension(file.name); | 77 | 78 | if (ext in TracerApi) { | 78 | 79 | TracerApi[ext]({ code: file.content }, undefined, this.tracerApiSource.token) | 79 | | - .then(traces => { | | 80 | + .then(commands => { | 80 | 81 | this.tracerApiSource = null; | 81 | 82 | this.setState({ building: false }); | 82 | | - this.reset(traces); | | 83 | + this.reset(commands); | 83 | 84 | this.next(); | 84 | 85 | }) | 85 | 86 | .catch(error => { | |
Collapse file Copy file name to clipboardExpand all lines: src/frontend/components/ResizableContainer/index.jsx +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,7 +16,7 @@ class ResizableContainer extends React.Component { | 16 | 16 | let totalWeight = 0; | 17 | 17 | let subtotalWeight = 0; | 18 | 18 | weights.forEach((weight, i) => { | 19 | | - if (!visibles[i]) return; | | 19 | + if (visibles && !visibles[i]) return; | 20 | 20 | totalWeight += weight; | 21 | 21 | if (i < index) subtotalWeight += weight; | 22 | 22 | }); | @@ -34,9 +34,10 @@ class ResizableContainer extends React.Component { | 34 | 34 |
| 35 | 35 | const elements = []; | 36 | 36 | let lastIndex = -1; | 37 | | - const totalWeight = weights.filter((weight, i) => visibles[i]).reduce((sumWeight, weight) => sumWeight + weight, 0); | | 37 | + const totalWeight = weights.filter((weight, i) => !visibles || visibles[i]) | | 38 | + .reduce((sumWeight, weight) => sumWeight + weight, 0); | 38 | 39 | children.forEach((child, i) => { | 39 | | - if (visibles[i]) { | | 40 | + if (!visibles || visibles[i]) { | 40 | 41 | if (~lastIndex) { | 41 | 42 | const prevIndex = lastIndex; | 42 | 43 | elements.push( | |
Collapse file Copy file name to clipboardExpand all lines: src/frontend/components/VisualizationViewer/index.jsx +30-53Lines changed: 30 additions & 53 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import React from 'react'; | 2 | 2 | import { connect } from 'react-redux'; | 3 | | -import { classes } from '/common/util'; | 4 | | -import { BaseComponent, ResizableContainer } from '/components'; | | 3 | +import { BaseComponent } from '/components'; | 5 | 4 | import { actions } from '/reducers'; | 6 | 5 | import styles from './stylesheet.scss'; | 7 | | -import { Array1DData, Array2DData, ChartData, Data, GraphData, LogData, MarkdownData } from '/core/datas'; | | 6 | +import * as TracerClasses from '/core/tracers'; | | 7 | +import * as LayoutClasses from '/core/layouts'; | | 8 | +import { classes } from '/common/util'; | 8 | 9 |
| 9 | 10 | @connect(({ player }) => ({ player }), actions) | 10 | 11 | class VisualizationViewer extends BaseComponent { | 11 | 12 | constructor(props) { | 12 | 13 | super(props); | 13 | 14 |
| 14 | | - this.state = { | 15 | | - dataWeights: {}, | 16 | | - }; | | 15 | + this.reset(); | | 16 | + } | 17 | 17 |
| 18 | | - this.datas = []; | | 18 | + reset() { | | 19 | + this.root = null; | | 20 | + this.objects = {}; | 19 | 21 | } | 20 | 22 |
| 21 | 23 | componentDidMount() { | @@ -36,19 +38,11 @@ class VisualizationViewer extends BaseComponent { | 36 | 38 | if (cursor > oldCursor) { | 37 | 39 | applyingChunks = chunks.slice(oldCursor, cursor); | 38 | 40 | } else { | 39 | | - this.datas = []; | | 41 | + this.reset(); | 40 | 42 | applyingChunks = chunks.slice(0, cursor); | 41 | 43 | } | 42 | 44 | applyingChunks.forEach(chunk => this.applyChunk(chunk)); | 43 | 45 |
| 44 | | - const dataWeights = chunks === oldChunks ? { ...this.state.dataWeights } : {}; | 45 | | - this.datas.forEach(data => { | 46 | | - if (!(data.tracerKey in dataWeights)) { | 47 | | - dataWeights[data.tracerKey] = 1; | 48 | | - } | 49 | | - }); | 50 | | - this.setState({ dataWeights }); | 51 | | - | 52 | 46 | const lastChunk = applyingChunks[applyingChunks.length - 1]; | 53 | 47 | if (lastChunk && lastChunk.lineNumber !== undefined) { | 54 | 48 | this.props.setLineIndicator({ lineNumber: lastChunk.lineNumber, cursor }); | @@ -57,60 +51,43 @@ class VisualizationViewer extends BaseComponent { | 57 | 51 | } | 58 | 52 | } | 59 | 53 |
| 60 | | - addTracer(className, tracerKey, title) { | 61 | | - const DataClass = { | 62 | | - Tracer: Data, | 63 | | - MarkdownTracer: MarkdownData, | 64 | | - LogTracer: LogData, | 65 | | - Array2DTracer: Array2DData, | 66 | | - Array1DTracer: Array1DData, | 67 | | - ChartTracer: ChartData, | 68 | | - GraphTracer: GraphData, | 69 | | - }[className]; | 70 | | - const data = new DataClass(tracerKey, title, this.datas); | 71 | | - this.datas.push(data); | 72 | | - } | 73 | | - | 74 | | - applyTrace(trace) { | 75 | | - const { tracerKey, method, args } = trace; | | 54 | + applyCommand(command) { | | 55 | + const { key, method, args } = command; | 76 | 56 | try { | 77 | | - if (method === 'construct') { | 78 | | - const [className, title] = args; | 79 | | - this.addTracer(className, tracerKey, title); | | 57 | + if (key === null && method === 'setRoot') { | | 58 | + const [root] = args; | | 59 | + this.root = this.objects[root]; | | 60 | + } else if (method === 'destroy') { | | 61 | + delete this.objects[key]; | | 62 | + } else if (method in LayoutClasses) { | | 63 | + const [children] = args; | | 64 | + const LayoutClass = LayoutClasses[method]; | | 65 | + this.objects[key] = new LayoutClass(key, key => this.objects[key], children); | | 66 | + } else if (method in TracerClasses) { | | 67 | + const [title] = args; | | 68 | + const TracerClass = TracerClasses[method]; | | 69 | + this.objects[key] = new TracerClass(key, key => this.objects[key], title); | 80 | 70 | } else { | 81 | | - const data = this.datas.find(data => data.tracerKey === tracerKey); | 82 | | - data[method](...args); | | 71 | + this.objects[key][method](...args); | 83 | 72 | } | 84 | 73 | } catch (error) { | 85 | 74 | this.handleError(error); | 86 | 75 | } | 87 | 76 | } | 88 | 77 |
| 89 | 78 | applyChunk(chunk) { | 90 | | - chunk.traces.forEach(trace => this.applyTrace(trace)); | 91 | | - } | 92 | | - | 93 | | - handleChangeWeights(weights) { | 94 | | - const dataWeights = {}; | 95 | | - weights.forEach((weight, i) => { | 96 | | - dataWeights[this.datas[i].tracerKey] = weight; | 97 | | - }); | 98 | | - this.setState({ dataWeights }); | | 79 | + chunk.commands.forEach(command => this.applyCommand(command)); | 99 | 80 | } | 100 | 81 |
| 101 | 82 | render() { | 102 | 83 | const { className } = this.props; | 103 | | - const { dataWeights } = this.state; | 104 | 84 |
| 105 | 85 | return ( | 106 | | - <ResizableContainer className={classes(styles.visualization_viewer, className)} | 107 | | - weights={this.datas.map(data => dataWeights[data.tracerKey])} | 108 | | - visibles={this.datas.map(() => true)} | 109 | | - onChangeWeights={weights => this.handleChangeWeights(weights)}> | | 86 | + <div className={classes(styles.visualization_viewer, className)}> | 110 | 87 | { | 111 | | - this.datas.map(data => data.render()) | | 88 | + this.root && this.root.render() | 112 | 89 | } | 113 | | - </ResizableContainer> | | 90 | + </div> | 114 | 91 | ); | 115 | 92 | } | 116 | 93 | } | |
Collapse file Copy file name to clipboard +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | @import "~/common/stylesheet/index"; | 2 | 2 |
| 3 | 3 | .visualization_viewer { | 4 | | -} | | 4 | + flex: 1; | | 5 | + display: flex; | | 6 | + flex-direction: column; | | 7 | + align-items: stretch; | | 8 | +} |
|