-
Notifications
You must be signed in to change notification settings - Fork 7
/
editable.js
179 lines (161 loc) · 5.16 KB
/
editable.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
require('prosemirror-menu/style/menu.css')
require('prosemirror-view/style/prosemirror.css')
require('./editable.css')
require('./editable-menu.css')
require('./editable-feature-flags.css')
import React, {createElement as el} from 'react'
import {EditorState, Plugin, NodeSelection} from 'prosemirror-state'
import {history as pluginHistory} from 'prosemirror-history'
// import {dropCursor as pluginDropCursor} from 'prosemirror-dropcursor'
import {MenuBarEditorView} from 'prosemirror-menu'
import {edMenuPlugin, edMenuEmptyPlugin, patchMenuWithFeatureFlags} from '../menu/ed-menu'
import GridToDoc from '../convert/grid-to-doc'
import EdSchema from '../schema/ed-schema'
import {MediaNodeView} from '../schema/media'
import {edInputRules, edBaseKeymap, edKeymap} from '../inputrules/ed-input-rules'
import {posToIndex} from '../util/pm'
import {isDropFileEvent} from '../util/drop'
import PluginShareUrl from '../plugins/share-url'
// import PluginContentHints from '../plugins/content-hints'
import PluginPlaceholder from '../plugins/placeholder'
import PluginFixedMenuHack from '../plugins/fixed-menu-hack'
import PluginCommandsInterface from '../plugins/commands-interface'
import {PluginStoreRef} from '../plugins/store-ref'
class Editable extends React.Component {
constructor (props) {
super(props)
this.boundOnDrop = this.onDrop.bind(this)
}
setState () {
throw new Error('Can not setState of Editable')
}
render () {
return el('div', {
className: 'Editable',
},
el('div', {className: 'Editable-Mirror', ref: 'mirror'}),
el('div', {className: 'Editable-Plugins', ref: 'plugins'})
)
}
componentDidMount () {
const {mirror, plugins} = this.refs
const { initialContent
, menuBar
, onChange
, onCommandsChanged
, widgetPath
, coverPrefs } = this.props
const {store, imgfloConfig, featureFlags} = this.context
let edPlugins = [
pluginHistory(),
edInputRules,
edKeymap,
edBaseKeymap,
]
let edPluginClasses = [
PluginStoreRef,
PluginShareUrl,
PluginPlaceholder,
]
patchMenuWithFeatureFlags(featureFlags)
if (menuBar) {
edPlugins.push(edMenuPlugin)
edPluginClasses.push(PluginFixedMenuHack)
} else {
edPlugins.push(edMenuEmptyPlugin)
}
if (onCommandsChanged) {
edPluginClasses.push(PluginCommandsInterface)
}
const pluginProps = {
ed: store,
editableView: this,
elMirror: mirror,
elPlugins: plugins,
widgetPath,
coverPrefs,
}
edPluginClasses.forEach(function (plugin) {
// FIXME least knowledge per plugin
plugin.edStuff = pluginProps
const p = new Plugin(plugin)
edPlugins.push(p)
})
const state = EditorState.create({
schema: EdSchema,
doc: GridToDoc(initialContent),
plugins: edPlugins,
ed: store,
})
let view
const applyTransaction = (transaction) => {
view.updateState(view.editor.state.apply(transaction))
if (transaction.steps.length) {
onChange('EDITABLE_CHANGE', this.pm)
}
}
// PM setup
let pmOptions =
{ state,
autoInput: true,
spellcheck: true,
dispatchTransaction: applyTransaction,
handleClickOn: function (_view, _pos, node) { return node.type.name === 'media' },
nodeViews: {
media: (node, view, getPos) => {
return new MediaNodeView(node, view, getPos, store, imgfloConfig, coverPrefs, widgetPath, featureFlags)
},
},
editable: function (state) { return true },
attributes: { class: 'ProseMirror-content' },
handleDOMEvents: {
drop: this.boundOnDrop,
},
// Don't type over node selection
handleTextInput: function (view, from, to, text) {
if (view.state.selection instanceof NodeSelection) {
return true
}
},
}
view = this.pm = new MenuBarEditorView(mirror, pmOptions)
this.pm.ed = store
onChange('EDITABLE_INITIALIZE', this)
}
componentWillUnmount () {
this.pm.editor.destroy()
}
onDrop (editor, event) {
if (!isDropFileEvent(event)) return
const {onDropFiles} = this.props
if (!onDropFiles) return
const {pos} = this.pm.editor.posAtCoords({left: event.clientX, top: event.clientY})
if (pos == null) return
const index = posToIndex(editor.state.doc, pos)
if (index == null) return
event.preventDefault()
event.stopPropagation()
onDropFiles(index, event.dataTransfer.files)
}
}
Editable.contextTypes = {
store: React.PropTypes.object,
imgfloConfig: React.PropTypes.object,
featureFlags: React.PropTypes.object,
}
Editable.propTypes = {
initialContent: React.PropTypes.array.isRequired,
menuBar: React.PropTypes.bool,
onChange: React.PropTypes.func.isRequired,
onShareFile: React.PropTypes.func,
onShareUrl: React.PropTypes.func,
onDropFiles: React.PropTypes.func,
onEditableInit: React.PropTypes.func,
onCommandsChanged: React.PropTypes.func,
widgetPath: React.PropTypes.string,
coverPrefs: React.PropTypes.object,
}
Editable.defaultProps = {
coverPrefs: {},
}
export default React.createFactory(Editable)