-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
176 lines (158 loc) · 4.97 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
/**
* 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 vdom = require('virtual-dom')
, h = vdom.h
, match = require('mime-match')
module.exports = setup
module.exports.consumes = ['ui', 'api']
module.exports.provides = ['import']
const IMPORTED = 'IMPORT_IMPORTED'
const IMPORTING = 'IMPORT_IMPORTING'
const TOGGLE_IMPORT_DROPDOWN = 'IMPORT_TOGGLE_DROPDOWN'
function setup(plugin, imports, register) {
var ui = imports.ui
, api = imports.api
ui.reduxReducerMap['import'] = reducer
function reducer(state, action) {
if(!state) {
return {
importTypes: ui.config['importexport:importTypes']
, showImportDropdown: false
, importing: false
, importError: false
}
}
if(TOGGLE_IMPORT_DROPDOWN === action.type) {
return {...state
, showImportDropdown: !state.showImportDropdown
, importError: false
}
}
if(IMPORTING == action.type) {
return {...state, importing: action.payload, importError: false}
}
if(IMPORTED === action.type && action.error) {
return {...state, importing: false, importError: action.error}
}
if(IMPORTED == action.type) {
return {...state
, importing: false
, importError: false
, showImportDropdown: false
}
}
return state
}
var importProvider = {
action_import: function*(files) {
var file = files[0]
, state = ui.store.getState()
, importTypes = state['import'].importTypes[state.editor.document.attributes.type]
, documentId = ui.store.getState().editor.document.id
try {
if(file.type && !importTypes.filter(match(file.type)).length) {
throw new Error('File type not supported')
}
yield importProvider.action_importing(file.name)
yield api.action_document_import(documentId, file)
yield {type: IMPORTED}
}catch(e) {
console.error(e)
yield {type: IMPORTED, error: e.message}
}
}
, action_toggleImportDropdown: function() {
return {type: TOGGLE_IMPORT_DROPDOWN}
}
, action_importing: function(filename) {
return {type: IMPORTING, payload:filename}
}
, renderImport
, renderImportDropdown
}
ui.onRenderNavbarRight((store, children) => {
var state = store.getState()
if(!state.editor.editor) return
if(state.editor.document && state['import'].importTypes[state.editor.document.attributes.type]) {
children.unshift(renderImport(store))
}
})
function renderImport(store) {
var document = store.getState().editor.document
var state = store.getState()['import']
return h('li.dropdown'+(state.showImportDropdown? '.open' : ''), [
h('a.dropdown-toggle', {
href: 'javascript:void(0)'
, 'ev-click': evt => store.dispatch(importProvider.action_toggleImportDropdown())
, id: 'importMenu'
, attributes: {
'data-toggle': 'dropdown'
, 'aria-haspopup': 'true'
, 'aria-expanded': state.showImportDropdown? 'true' : 'false'
}
, title: ui._('plugin-import/import')()
}
, [
h('i.glyphicon.glyphicon-import')
, h('span.sr-only', ui._('plugin-import/import')())
, h('span.caret')
]
)
, h('ul.dropdown-menu'
, { attributes: {'aria-labelledby':'importMenu'}
}
, renderImportDropdown(store)
)
])
}
function renderImportDropdown(store) {
var state = store.getState()['import']
var children = [h('li.dropdown-header', ui._('plugin-import/import')())]
if(!window.File || !window.FileReader || !window.FileList || !window.Blob) {
children.push(h('li'
, h('a', ui._('plugin-import/import-browser-not-supported')())
))
return children
}
if(state.importing) {
children.push(h('li'
, h('a', ui._('plugin-import/importing')({file:state.importing}))
))
return children
}
children.push(
h('li', h('a', [
h('input', {
type: 'file'
, 'ev-change': evt => {
store.dispatch(importProvider.action_import(evt.currentTarget.files))
}
})
]))
)
if(state.importError) {
children.push(
h('li', h('div.alert.alert-danger', [
h('strong', 'Error!')
, ' '+state.importError
]))
)
}
return children
}
register(null, {'import': importProvider})
}