1
- import React , { useRef , useEffect } from ' react'
1
+ import React , { useRef , useEffect } from " react" ;
2
2
import CodeMirror from "react-codemirror" ;
3
- import { Remarkable } from ' remarkable' ;
3
+ import { Remarkable } from " remarkable" ;
4
4
5
5
require ( "codemirror/lib/codemirror.css" ) ;
6
6
require ( "codemirror/mode/javascript/javascript" ) ;
7
7
require ( "codemirror/theme/dracula.css" ) ;
8
8
9
- export default function Cell ( { cell, dispatch, currentCell, setCurrentCell, cellId} ) {
9
+ export default function Cell ( {
10
+ cell,
11
+ dispatch,
12
+ currentCell,
13
+ setCurrentCell,
14
+ cellId,
15
+ } ) {
10
16
const refCode = useRef ( null ) ;
11
- const refOutput = useRef ( '' ) ;
17
+ const refOutput = useRef ( "" ) ;
12
18
13
- const getCode = ( ) => {
14
- if ( cell . type === "code" ) {
19
+ const getCode = ( ) => {
20
+ if ( cell . type === "code" ) {
15
21
const input = refCode . current . getCodeMirror ( ) . getValue ( ) ;
16
22
try {
17
- const output = ( "global" , eval ) ( input ) || ''
18
- const cellstate = { ...cell , input : input , output :output }
19
- dispatch ( { type :"CHANGE_CELL" , payload :cellstate } ) ;
20
- } catch ( error ) {
21
- const cellstate = { ...cell , input : input , output :error }
22
- dispatch ( { type :"CHANGE_CELL" , payload :cellstate } ) ;
23
+ // eslint-disable-next-line no-eval
24
+ const output = ( "global" , eval ) ( input ) || "" ;
25
+ const cellstate = { ...cell , input, output } ;
26
+ dispatch ( { type : "CHANGE_CELL" , payload : cellstate } ) ;
27
+ } catch ( error ) {
28
+ const cellstate = { ...cell , input, output : error } ;
29
+ dispatch ( { type : "CHANGE_CELL" , payload : cellstate } ) ;
23
30
}
24
-
25
-
26
- } else {
27
- const cellstate = { ...cell , input : refCode . current . value }
31
+ } else {
32
+ const cellstate = { ...cell , input : refCode . current . value } ;
28
33
showOutput ( ) ;
29
- dispatch ( { type :"CHANGE_CELL" , payload :cellstate } ) ;
34
+ dispatch ( { type : "CHANGE_CELL" , payload : cellstate } ) ;
30
35
}
31
- }
36
+ } ;
32
37
33
- const setId = ( ) => {
34
- const id = currentCell ? currentCell : parseInt ( cell . id . split ( "_" ) [ 1 ] ) ;
38
+ const setId = ( ) => {
39
+ const id = currentCell || parseInt ( cell . id . split ( "_" ) [ 1 ] ) ;
35
40
setCurrentCell ( id ) ;
36
- }
41
+ } ;
37
42
38
- useEffect ( ( ) => {
39
- if ( cell . type === "text" ) {
43
+ useEffect ( ( ) => {
44
+ if ( cell . type === "text" ) {
40
45
refCode . current . value = cell . input ;
41
46
const md = new Remarkable ( ) ;
42
47
refOutput . current . innerHTML = md . render ( cell . input ) ;
43
- refCode . current . style . display = cell . input ? "none" : "block" ;
44
- } else
45
- {
48
+ refCode . current . style . display = cell . input ? "none" : "block" ;
49
+ } else {
46
50
refCode . current . getCodeMirror ( ) . setValue ( cell . input ) ;
47
51
refOutput . current . innerHTML = cell . output ;
48
52
}
49
53
setId ( ) ;
50
- } , [ cell ] )
51
-
52
- const upCell = ( type ) => {
54
+ // eslint-disable-next-line react-hooks/exhaustive-deps
55
+ } , [ cell ] ) ;
53
56
54
- const id = cellId - 1 ;
55
- newCell ( id , type ) ;
56
- }
57
+ const upCell = ( type ) => {
58
+ const id = cellId - 1 ;
59
+ newCell ( id , type ) ;
60
+ } ;
57
61
58
- const downCell = ( type ) => {
62
+ const downCell = ( type ) => {
59
63
const id = cellId ;
60
- newCell ( id , type ) ;
61
-
62
- }
63
-
64
- const newCell = ( id , type ) => {
64
+ newCell ( id , type ) ;
65
+ } ;
65
66
67
+ const newCell = ( id , type ) => {
66
68
const newCell = {
67
- id : " cell_" + ( currentCell + 1 ) ,
69
+ id : ` cell_${ currentCell + 1 } ` ,
68
70
input : "" ,
69
- }
71
+ } ;
70
72
71
- if ( type === "text" ) {
72
- newCell [ " type" ] = "text"
73
- } else {
74
- newCell [ " output" ] = ""
75
- newCell [ " type" ] = "code"
73
+ if ( type === "text" ) {
74
+ newCell . type = "text" ;
75
+ } else {
76
+ newCell . output = "" ;
77
+ newCell . type = "code" ;
76
78
}
77
- setCurrentCell ( currentCell + 1 ) ;
78
- dispatch ( { type :"ADD_CELL" , payload :newCell , currentId : id } ) ;
79
- }
79
+ setCurrentCell ( currentCell + 1 ) ;
80
+ dispatch ( { type : "ADD_CELL" , payload : newCell , currentId : id } ) ;
81
+ } ;
80
82
81
- const disableOutput = ( ) => {
82
- if ( cell . type === "text" ) {
83
+ const disableOutput = ( ) => {
84
+ if ( cell . type === "text" ) {
83
85
refOutput . current . style . display = "none" ;
84
86
refCode . current . style . display = "block" ;
85
87
}
86
- }
88
+ } ;
87
89
88
- const showOutput = ( ) => {
89
- if ( cell . type === "text" ) {
90
+ const showOutput = ( ) => {
91
+ if ( cell . type === "text" ) {
90
92
refOutput . current . style . display = "block" ;
91
93
refCode . current . style . display = "none" ;
92
94
}
93
- }
95
+ } ;
94
96
95
- const deleteCell = ( ) => {
96
- dispatch ( { type :"DELETE_CELL" , payload :cell . id } ) ;
97
- }
97
+ const deleteCell = ( ) => {
98
+ dispatch ( { type : "DELETE_CELL" , payload : cell . id } ) ;
99
+ } ;
98
100
99
101
return (
100
102
< >
101
- < button onClick = { ( ) => { getCode ( ) } } > Run</ button >
102
- < button onClick = { ( ) => { upCell ( "code" ) } } > Up cell</ button >
103
- < button onClick = { ( ) => { downCell ( "code" ) } } > down cell</ button >
104
- < button onClick = { ( ) => { upCell ( "text" ) } } > Text up</ button >
105
- < button onClick = { ( ) => { downCell ( "text" ) } } > Text down</ button >
106
- < button onClick = { ( ) => { deleteCell ( ) } } > Delete Cell</ button >
107
- { cell . type === "code" ? < CodeMirror value = { cell . input } ref = { refCode }
108
- options = { {
109
- tabSize : 2 ,
110
- theme : "dracula" ,
111
- lineNumbers : true ,
112
- mode : "javascript"
103
+ < button
104
+ onClick = { ( ) => {
105
+ getCode ( ) ;
106
+ } }
107
+ >
108
+ Run
109
+ </ button >
110
+ < button
111
+ onClick = { ( ) => {
112
+ upCell ( "code" ) ;
113
+ } }
114
+ >
115
+ Up cell
116
+ </ button >
117
+ < button
118
+ onClick = { ( ) => {
119
+ downCell ( "code" ) ;
120
+ } }
121
+ >
122
+ down cell
123
+ </ button >
124
+ < button
125
+ onClick = { ( ) => {
126
+ upCell ( "text" ) ;
113
127
} }
114
- /> : < TextCell refText = { refCode } /> }
115
- < div ref = { refOutput } onClick = { ( ) => { disableOutput ( ) } } > </ div >
116
- </ >
117
- )
128
+ >
129
+ Text up
130
+ </ button >
131
+ < button
132
+ onClick = { ( ) => {
133
+ downCell ( "text" ) ;
134
+ } }
135
+ >
136
+ Text down
137
+ </ button >
138
+ < button
139
+ onClick = { ( ) => {
140
+ deleteCell ( ) ;
141
+ } }
142
+ >
143
+ Delete Cell
144
+ </ button >
145
+ { cell . type === "code" ? (
146
+ < CodeMirror
147
+ value = { cell . input }
148
+ ref = { refCode }
149
+ options = { {
150
+ tabSize : 2 ,
151
+ theme : "dracula" ,
152
+ lineNumbers : true ,
153
+ mode : "javascript" ,
154
+ } }
155
+ />
156
+ ) : (
157
+ < TextCell refText = { refCode } />
158
+ ) }
159
+ < div
160
+ ref = { refOutput }
161
+ onClick = { ( ) => {
162
+ disableOutput ( ) ;
163
+ } }
164
+ > </ div >
165
+ </ >
166
+ ) ;
118
167
}
119
168
120
- function TextCell ( { refText} ) {
121
-
122
- return (
123
- < textarea ref = { refText } > </ textarea >
124
- )
125
- }
169
+ function TextCell ( { refText } ) {
170
+ return < textarea ref = { refText } > </ textarea > ;
171
+ }
0 commit comments