-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
215 lines (190 loc) · 6.39 KB
/
client.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
/**
* hive.js
* Copyright (C) 2013-2016 Marcel Klehr <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License version 2
* as published by the Mozilla Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Mozilla Public License
* along with this program. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
*/
var bindCodemirror = require('gulf-codemirror')
, vdom = require('virtual-dom')
, h = vdom.h
const SET_MODE = 'EDITORTEXTCODEMIRROR_SET_MODE'
const TOGGLE_LINENUMBERS = 'EDITORTEXTCODEMIRROR_TOGGLE_LINENUMBERS'
const SET_LINENUMBERS = 'EDITORTEXTCODEMIRROR_SET_LINENUMBERS'
module.exports = setup
module.exports.consumes = ['ui', 'editor', 'settings']
module.exports.provides = []
function setup(plugin, imports, register) {
var ui = imports.ui
, editor = imports.editor
, settings = imports.settings
ui.reduxReducerMap.editorTextCodemirror = reducer
function reducer(state, action) {
if(!state) {
return {
mode: null
, lineNumbers: false
, lineSeparator: null
, lineWrapping: true
}
}
if(SET_MODE === action.type) {
return {...state
, mode: action.payload
}
}
if(TOGGLE_LINENUMBERS === action.type) {
return {...state, lineNumbers: !state.lineNumbers}
}
if(SET_LINENUMBERS === action.type) {
return {...state, lineNumbers: action.payload}
}
return state
}
ui.reduxMiddleware.push(function(store) {
return next => action => {
if(SET_MODE === action.type) {
var mode = action.payload
if(mode) {
return ui.requireScript(ui.baseURL+'/static/codemirror/mode/'+mode+'/'+mode+'.js')
.then(() => next(action))
}
}
return next(action)
}
})
editor.registerEditor('CodeMirror', 'text/plain', 'An extensible and performant code editor'
, function(editorEl, onClose) {
return ui.requireScript(ui.baseURL+'/static/build/codemirror.js')
.then(() => {
window.CodeMirror = require('codemirror')
return ui.requireScript(ui.baseURL+'/static/codemirror/mode/meta.js')
})
.then(() => {
// Update state on settings changes
var dispose = settings.onChange(updateFromSettings)
updateFromSettings()
// Update editor on state changes
var dispose2 = ui.store.subscribe(_ => {
var state = ui.store.getState()
if(cm.getOption('mode') != state.editorTextCodemirror.mode) {
cm.setOption('mode', state.editorTextCodemirror.mode)
}
if(cm.getOption('lineNumbers') != state.editorTextCodemirror.lineNumbers) {
cm.setOption('lineNumbers', state.editorTextCodemirror.lineNumbers)
}
})
// Initialize editor
var cmEl
var cm = CodeMirror(function(el) {
editorEl.appendChild(el)
el.style['height'] = '100%'
el.style['visibility'] = 'hidden' // Keep hidden until init
cmEl = el
}, ui.store.getState().editorTextCodemirror)
editorEl.style['height'] = '100%'
onClose(() => {
dispose()
dispose2()
})
var doc = bindCodemirror(cm)
doc.on('editableInitialized', () => {
cmEl.style['visibility'] = 'visible'
})
return Promise.resolve(doc)
})
})
function updateFromSettings() {
ui.store.dispatch(action_setLinenumbers(
null !== settings.getForUserDocument('editorTextCodemirror:lineNumbers')?
settings.getForUserDocument('editorTextCodemirror:lineNumbers')
: settings.getForDocument('editorTextCodemirror:lineNumbers')
))
ui.store.dispatch(action_setMode(
settings.getForUserDocument('editorTextCodemirror:mode')
|| settings.getForDocument('editorTextCodemirror:mode')
))
}
// Document Settings
settings.onRenderDocumentSettings((children) => {
if(ui.store.getState().editor.document.attributes.type !== 'text/plain') return
children.push(renderSettings(ui.store
, settings.getForDocument.bind(settings), settings.action_setForDocument.bind(settings)))
})
settings.onRenderUserDocumentSettings((children) => {
if(ui.store.getState().editor.document.attributes.type !== 'text/plain') return
children.push(renderSettings(ui.store
, settings.getForUserDocument.bind(settings), settings.action_setForUserDocument.bind(settings)))
})
function renderSettings(store, getSetting, actionSave) {
return h('div',[
h('h4', 'Codemirror')
, h('ul.list-group', [
renderlineNumberSetting(getSetting('editorTextCodemirror:lineNumbers')
, evt => {
ui.store.dispatch(actionSave({
'editorTextCodemirror:lineNumbers': evt.currentTarget.checked
}))
}
)
, renderModeSetting(getSetting('editorTextCodemirror:mode')
, evt => {
ui.store.dispatch(actionSave({
'editorTextCodemirror:mode': evt.currentTarget.value
}))
}
)
])
])
}
function renderlineNumberSetting(checked, cb) {
return h('li.list-group-item', [
h('label', [
h('input', {
type: 'checkbox'
, 'ev-change': cb
, checked
})
, ' '+ui._('editor-text-codemirror/show-line-numbers')()
])
])
}
function renderModeSetting(currentMode, cb) {
return h('li.list-group-item', [
h('label', [
ui._('editor-text-codemirror/syntax-highlighting')()+' '
, h('select'
, { 'ev-change': cb, value: currentMode }
, [h('option', {
value: ''
, attributes: !currentMode? {selected: true} : {}
}, ui._('editor-text-codemirror/select-mode')())]
.concat(CodeMirror.modeInfo.map(mode => {
return h('option'
, {value: mode.mode, attributes: currentMode == mode.mode? {selected: true} : {}}
, mode.name)
}))
)
])
])
}
register()
}
function action_setLinenumbers(value) {
return {type: SET_LINENUMBERS, payload: value}
}
function action_toggleLinenumbers() {
return {type: TOGGLE_LINENUMBERS}
}
function action_setMode(mode) {
return {type: SET_MODE, payload: mode}
}