-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathDebugEditor.js
256 lines (228 loc) · 8.56 KB
/
DebugEditor.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React from 'react/addons'
import EditorActions from '../flux/EditorActions'
import EditorStore from '../flux/EditorStore'
import { BASE_CHAR } from '../core/RichText'
import { lineContainingChar } from '../core/EditorCommon'
import { logInGroup } from '../core/utils'
const T = React.PropTypes
export default React.createClass({
propTypes: {
editorState: T.object,
replica: T.object,
searchLinesWithSelection: T.func,
setRenderOptimizations: T.func
},
getInitialState() {
return {
autotypeText: ''
}
},
componentWillReceiveProps(nextProps) {
this.edState = nextProps.editorState
this.replica = nextProps.replica
},
shouldComponentUpdate(nextProps, nextState) {
return this.state.autotypeSeconds !== nextState.autotypeSeconds
|| this.state.autotypeMinutes !== nextState.autotypeMinutes
|| this.state.autotypeText !== nextState.autotypeText
},
_dumpState() {
console.debug('Current state contents (Use React Devtools 0.14+ for real-time state view/edit):')
console.dir(this.edState)
EditorActions.focusInput()
},
_dumpReplica() {
let text = this.replica.getTextRange(BASE_CHAR)
console.debug('Current replica text: [' + text.map(c => c.char).join('') + ']')
console.debug('BASE_CHAR:')
console.dir(this.replica.getCharAt(0))
console.debug('Current replica contents:')
console.dir(text)
EditorActions.focusInput()
},
_dumpPosition() {
if(this.edState.position) {
console.debug('Current position:', this.edState.position, 'positionEolStart:', this.edState.positionEolStart)
} else {
console.debug('No active position')
}
EditorActions.focusInput()
},
_dumpCurrentLine() {
logInGroup('Line debug', () => {
if(this.edState.lines) {
let printLine = l => console.debug(l.toString())
let currentLine = lineContainingChar(this.edState.lines, this.edState.position, this.edState.positionEolStart)
if(!currentLine) {
console.log(null)
} else {
if (currentLine.index > 0) {
logInGroup('Before', () => {
printLine(this.edState.lines[currentLine.index - 1])
})
}
logInGroup('Current', () => {
console.debug('index', currentLine.index, 'endOfLine', currentLine.endOfLine)
printLine(currentLine.line)
})
if (currentLine.index < this.edState.lines.length - 1) {
logInGroup('After', () => {
printLine(this.edState.lines[currentLine.index + 1])
})
}
}
} else {
console.debug('No lines')
}
})
EditorActions.focusInput()
},
_dumpLines() {
if(this.edState.lines) {
console.debug('Lines as Objects:', this.edState.lines)
this._dumpLinesFormatted('Lines', this.edState.lines)
} else {
console.debug('No lines')
}
EditorActions.focusInput()
},
_dumpSelection() {
if(this.edState.selectionActive) {
console.debug('Current selection contents (rich chunks): [' + JSON.stringify(EditorStore.getSelectionRich()) + ']')
console.debug('Current selection contents (plain): [' + EditorStore.getSelectionText() + ']')
console.debug('Current selection contents (html): [' + EditorStore.getSelectionHtml() + ']')
console.debug('Left=', this.edState.selectionLeftChar)
console.debug('Right=', this.edState.selectionRightChar)
console.debug('Anchor=', this.edState.selectionAnchorChar)
console.debug('Chars=', EditorStore.getSelection())
} else {
console.debug('No active selection')
}
EditorActions.focusInput()
},
_dumpLinesWithSelection() {
let linesWithSelection = this.props.searchLinesWithSelection()
if(linesWithSelection) {
let lines = this.edState.lines.slice(linesWithSelection.left, linesWithSelection.right + 1)
console.debug('Lines with selection as Objects:', lines)
this._dumpLinesFormatted('Lines with selection', lines)
} else {
console.debug('No selected lines')
}
EditorActions.focusInput()
},
_dumpLinesFormatted(logText, lines) {
logInGroup(logText, () => {
for(let i = 0; i < lines.length; i++) {
logInGroup(`Index ${i}`, () => { // eslint-disable-line no-loop-func
console.debug(lines[i].toString())
})
}
})
},
_forceFlow() {
EditorActions.replicaUpdated()
EditorActions.focusInput()
},
_forceRender() {
this.forceUpdate(() => console.debug('Render done.'))
EditorActions.focusInput()
},
_togglePositionEolStart() {
// state should only be set from the store, but for debugging this is fine
this.setState(previousState => {
let previous = previousState.positionEolStart
console.debug('Toggling positionEolStart from ' + previous + ' to ' + !previous)
return { positionEolStart: !previous }
})
EditorActions.focusInput()
},
_testError() {
let err = new Error('A test error from DebugEditor')
EditorActions.registerEditorError(err)
},
_renderOptimizationsEnable() {
this.props.setRenderOptimizations(true)
},
_renderOptimizationsDisable() {
this.props.setRenderOptimizations(false)
},
_scheduleAutotype() {
function at(minutes, seconds, cb) {
(function loop() {
let now = new Date()
if (now.getMinutes() === minutes) {
if(now.getSeconds() >= seconds) {
cb()
} else {
let delay = 1000 - (new Date() % 1000)
setTimeout(loop, delay)
}
} else {
let delay = 60000 - (new Date() % 60000)
setTimeout(loop, delay)
}
})()
}
let text = this.state.autotypeText.split('')
if(text.length === 0 || !this.state.autotypeMinutes || !this.state.autotypeSeconds) {
console.debug('No autotype, invalid input.')
return
}
console.debug(`Scheduling autotype of chars [${text}] @ ${this.state.autotypeMinutes} minutes, ${this.state.autotypeSeconds} seconds.`)
at(this.state.autotypeMinutes, this.state.autotypeSeconds, () => {
// if there is any debug logging it will be grouped
logInGroup(`Autotyping text ${text}`, () => {
// auto-type one char at a time for the hardest concurrency test possible
for(let i = 0; i < text.length; i++) {
EditorActions.insertChars(text[i])
}
})
})
EditorActions.focusInput()
},
_onChangeAutotypeText(e) {
this.setState({autotypeText: e.target.value})
},
_onChangeAutotypeMinutes(e) {
let value = parseInt(e.target.value)
if(!Number.isNaN(value)) {
this.setState({autotypeMinutes: parseInt(e.target.value)})
}
},
_onChangeAutotypeSeconds(e) {
let value = parseInt(e.target.value)
if(!Number.isNaN(value)) {
this.setState({autotypeSeconds: parseInt(e.target.value)})
}
},
render() {
return (
<div style={{position: 'relative', zIndex: 100, paddingTop: 30}}>
<p>Debugging tools:</p>
<span>Dump: </span>
<button onClick={this._dumpState}>State</button>
<button onClick={this._dumpReplica}>Replica</button>
<button onClick={this._dumpPosition}>Position</button>
<button onClick={this._dumpCurrentLine}>Line</button>
<button onClick={this._dumpLines}>All Lines</button>
<button onClick={this._dumpSelection}>Selection</button>
<button onClick={this._dumpLinesWithSelection}>Lines with Selection</button><br/>
<span>Force: </span>
<button onClick={this._forceRender}>Render</button>
<button onClick={this._forceFlow}>Flow</button><br/>
<span>Action: </span>
<button onClick={this._togglePositionEolStart}>Toggle Position EOL Start</button>
<button onClick={this._testError}>Raise an Error</button><br/>
<span>Autotype: </span>
<input type='text' value={this.state.autotypeText} size='30' onChange={this._onChangeAutotypeText}/><span> @ </span>
<input type='text' value={this.state.autotypeMinutes} size='3' onChange={this._onChangeAutotypeMinutes}/><span>:</span>
<input type='text' value={this.state.autotypeSeconds} size='3' onChange={this._onChangeAutotypeSeconds}/><span> (min:sec) </span>
<button onClick={this._scheduleAutotype}>Schedule It!</button><br/>
<span>Render Optimizations: </span>
<button onClick={this._renderOptimizationsEnable}>Enable</button>
<button onClick={this._renderOptimizationsDisable}>Disable</button><br/>
</div>
)
}
})