-
Notifications
You must be signed in to change notification settings - Fork 37
/
ReactTerminal_output.stories.js
69 lines (60 loc) · 1.59 KB
/
ReactTerminal_output.stories.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React from 'react';
import { storiesOf } from '@storybook/react';
import ReactTerminal from 'ReactTerminal';
import ReactOutputRenderers from 'output';
import {
CommandMapping, OutputFactory, EmulatorState,
defaultCommandMapping
} from 'javascript-terminal';
const PAPER_TYPE = 'paper';
const paperStyles = {
backgroundColor: 'white',
color: 'black',
fontFamily: 'sans-serif',
padding: '1em',
margin: '1em 0',
borderRadius: '0.2em'
};
const PaperOutput = ({ content }) => (
<div style={paperStyles}>
<h1>{content.title}</h1>
{content.body}
</div>
);
const createPaperRecord = (title, body) => {
return new OutputFactory.OutputRecord({
type: PAPER_TYPE,
content: {
title,
body
}
});
};
storiesOf('ReactTerminal - OutputRenderer', module)
.add('with new command and renderer', () => {
// Add a print command that outputs a PaperRecord
const customState = EmulatorState.create({
'commandMapping': CommandMapping.create({
...defaultCommandMapping,
'print': {
'function': (state, opts) => {
const userInput = opts.join(' ');
return {
output: createPaperRecord('A custom renderer', userInput)
};
},
'optDef': {}
}
})
});
// Add rendering support for the PaperRecord using PaperOutput as the renderer
return (
<ReactTerminal
inputStr='print hello world!'
outputRenderers={{
...ReactOutputRenderers,
[PAPER_TYPE]: PaperOutput
}}
emulatorState={customState}/>
);
});