-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
246 lines (232 loc) · 5.87 KB
/
app.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
require('./app.css')
import React, {createElement as el} from 'react'
import AddCover from './add-cover'
import AddFold from './add-fold'
import Editable from './editable'
import rebassTheme from './rebass-theme'
import WidgetEdit from './widget-edit'
import Modal from './modal'
import EdStore from '../store/ed-store'
import {edCommands} from '../menu/ed-menu'
import {version as PACKAGE_VERSION} from '../../package.json'
export default class App extends React.Component {
constructor (props) {
super(props)
if (!props.initialContent) {
throw new Error('Missing props.initialContent')
}
if (!props.onChange) {
throw new Error('Missing props.onChange')
}
if (!props.onShareUrl) {
throw new Error('Missing props.onShareUrl')
}
if (!props.onShareFile) {
throw new Error('Missing props.onShareFile')
}
if (!props.onRequestCoverUpload) {
throw new Error('Missing props.onRequestCoverUpload')
}
const {
initialContent,
onChange,
onShareFile,
onShareUrl,
onRequestCoverUpload,
onRequestLink,
onDropFiles,
onDropFileOnBlock,
onCommandsChanged,
} = props
this._store = new EdStore(
{ initialContent,
onChange,
onShareFile,
onShareUrl,
onRequestCoverUpload,
onRequestLink,
onDropFiles,
onDropFileOnBlock,
onCommandsChanged,
}
)
this.routeChange = this._store.routeChange.bind(this._store)
this._store.on('media.block.edit.open', (blockID) => {
// TODO expose prop for native editors?
this.setState({blockToEdit: blockID})
this.blur()
})
this.closeMediaBlockModal = () => {
this.setState({blockToEdit: null})
}
this._store.on('media.block.edit.close', () => {
this.closeMediaBlockModal()
})
this.state = {
blockToEdit: null,
}
}
componentDidMount () {
this.boundOnDragOver = this.onDragOver.bind(this)
window.addEventListener('dragover', this.boundOnDragOver)
this.boundOnDrop = this.onDrop.bind(this)
window.addEventListener('drop', this.boundOnDrop)
if (this.props.onMount) {
this.props.onMount(this)
}
}
componentWillUnmount () {
window.removeEventListener('dragover', this.boundOnDragOver)
window.removeEventListener('drop', this.boundOnDrop)
}
getChildContext () {
const {imgfloConfig, featureFlags} = this.props
return ({
imgfloConfig,
featureFlags,
rebass: rebassTheme,
store: this._store,
})
}
render () {
return el('div',
{className: 'Ed'},
this.renderContent(),
// this.renderHints(),
this.renderModal()
)
}
renderContent () {
const { initialContent
, menuBar
, onShareFile
, onShareUrl
, onCommandsChanged
, onDropFiles
, widgetPath
, coverPrefs } = this.props
return el('div'
, { className: 'Ed-Content',
style:
{ zIndex: 1,
},
}
, el(Editable
, { initialContent,
menuBar,
onChange: this.routeChange,
onShareFile,
onShareUrl,
onCommandsChanged,
onDropFiles,
widgetPath,
coverPrefs,
}
)
)
}
renderHints () {
return el('div'
, { className: 'Ed-Hints',
}
, el(AddCover, {})
, el(AddFold, {})
)
}
renderModal () {
const {blockToEdit} = this.state
if (!blockToEdit) return
const initialBlock = this._store.getBlock(blockToEdit)
if (!initialBlock) return
const {coverPrefs} = this.props
return el(Modal,
{
onClose: this.closeMediaBlockModal,
child: el(WidgetEdit, {
initialBlock,
coverPrefs,
}),
}
)
}
onDragOver (event) {
// Listening to window
event.preventDefault()
}
onDrop (event) {
// Listening to window, for drops not caught by content
event.preventDefault()
}
// Exposed methods
getContent () {
return this._store.getContent()
}
setContent (content) {
this._store.setContent(content)
}
execCommand (commandName, attrs) {
const item = edCommands[commandName]
if (!item) {
throw new Error('commandName not found')
}
const view = this.pm.editor
item.spec.run(view.state, view.dispatch, view, attrs)
}
insertPlaceholders (index, count) {
return this._store.insertPlaceholders(index, count)
}
updatePlaceholder () {
throw new Error('updatePlaceholder is deprecated: use updateProgress')
}
updateProgress (id, metadata) {
this._store.updateProgress(id, metadata)
}
setCoverPreview (id, src) {
this._store.setCoverPreview(id, src)
}
setCover (id, cover) {
this._store.setCover(id, cover)
}
indexOfFold () {
return this._store.indexOfFold()
}
blur () {
this.pm.editor.content.blur()
window.getSelection().removeAllRanges()
}
get pm () {
return this._store.pm
}
get version () {
return PACKAGE_VERSION
}
}
App.childContextTypes = {
imgfloConfig: React.PropTypes.object,
store: React.PropTypes.object,
rebass: React.PropTypes.object,
featureFlags: React.PropTypes.object,
}
App.propTypes = {
initialContent: React.PropTypes.array.isRequired,
onChange: React.PropTypes.func.isRequired,
onShareFile: React.PropTypes.func.isRequired,
onShareUrl: React.PropTypes.func.isRequired,
onDropFiles: React.PropTypes.func,
onCommandsChanged: React.PropTypes.func,
onRequestCoverUpload: React.PropTypes.func.isRequired,
onRequestLink: React.PropTypes.func,
imgfloConfig: React.PropTypes.object,
widgetPath: React.PropTypes.string,
coverPrefs: React.PropTypes.object,
menuBar: React.PropTypes.bool,
onMount: React.PropTypes.func,
onDropFileOnBlock: React.PropTypes.func,
featureFlags: React.PropTypes.object,
}
App.defaultProps = {
widgetPath: './node_modules/',
menuBar: true,
coverPrefs: {},
featureFlags: {},
}