-
Notifications
You must be signed in to change notification settings - Fork 94
/
files.js
196 lines (179 loc) · 5.6 KB
/
files.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
/*
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { loadState } from '@nextcloud/initial-state'
import { subscribe } from '@nextcloud/event-bus'
import { openMimetypes } from './mime.js'
import { getSharingToken } from './token.js'
import RichWorkspace from '../views/RichWorkspace.vue'
import { imagePath } from '@nextcloud/router'
import store from '../store/index.js'
const FILE_ACTION_IDENTIFIER = 'Edit with text app'
const optimalPath = function(from, to) {
const current = from.split('/')
const target = to.split('/')
current.pop() // ignore filename
while (current[0] === target[0]) {
current.shift()
target.shift()
}
const relativePath = current.fill('..').concat(target)
const absolutePath = to.split('/')
return relativePath.length < absolutePath.length
? relativePath.join('/')
: to
}
const registerFileCreate = () => {
const newFileMenuPlugin = {
attach(menu) {
const fileList = menu.fileList
// only attach to main file list, public view is not supported yet
if (fileList.id !== 'files' && fileList.id !== 'files.public') {
return
}
// register the new menu entry
menu.addMenuEntry({
id: 'file',
displayName: t('text', 'New text file'),
templateName: t('text', 'New text file') + '.' + loadState('text', 'default_file_extension'),
iconClass: 'icon-filetype-text',
fileType: 'file',
actionHandler(name) {
fileList.createFile(name).then(function(status, data) {
const fileInfoModel = new OCA.Files.FileInfoModel(data)
if (typeof OCA.Viewer !== 'undefined') {
OCA.Files.fileActions.triggerAction('view', fileInfoModel, fileList)
} else if (typeof OCA.Viewer === 'undefined') {
OCA.Files.fileActions.triggerAction(FILE_ACTION_IDENTIFIER, fileInfoModel, fileList)
}
})
},
})
},
}
OC.Plugins.register('OCA.Files.NewFileMenu', newFileMenuPlugin)
}
const registerFileActionFallback = () => {
const sharingToken = getSharingToken()
const filesTable = document.querySelector('#preview table.files-filestable')
if (!sharingToken || !filesTable) {
const ViewerRoot = document.createElement('div')
ViewerRoot.id = 'text-viewer-fallback'
document.body.appendChild(ViewerRoot)
const registerAction = (mime) => OCA.Files.fileActions.register(
mime,
FILE_ACTION_IDENTIFIER,
OC.PERMISSION_UPDATE | OC.PERMISSION_READ,
imagePath('core', 'actions/rename'),
(filename) => {
const file = window.FileList.findFile(filename)
Promise.all([
import('vue'),
import(/* webpackChunkName: "files-modal" */'./../components/PublicFilesEditor.vue'),
]).then((imports) => {
const path = window.FileList.getCurrentDirectory() + '/' + filename
const Vue = imports[0].default
Vue.prototype.t = window.t
Vue.prototype.n = window.n
Vue.prototype.OCA = window.OCA
const Editor = imports[1].default
const vm = new Vue({
render: function(h) { // eslint-disable-line
const self = this
return h(Editor, {
props: {
fileId: file ? file.id : null,
active: true,
shareToken: sharingToken,
relativePath: path,
mimeType: file.mimetype,
},
on: {
close: function() { // eslint-disable-line
self.$destroy()
},
},
})
},
})
vm.$mount(ViewerRoot)
})
},
t('text', 'Edit'),
)
for (let i = 0; i < openMimetypes.length; i++) {
registerAction(openMimetypes[i])
OCA.Files.fileActions.setDefault(openMimetypes[i], FILE_ACTION_IDENTIFIER)
}
}
}
const FilesWorkspacePlugin = {
el: null,
attach(fileList) {
if (fileList.id !== 'files' && fileList.id !== 'files.public') {
return
}
this.el = document.createElement('div')
fileList.registerHeader({
id: 'workspace',
el: this.el,
render: this.render.bind(this),
priority: 10,
})
},
render(fileList) {
if (fileList.id !== 'files' && fileList.id !== 'files.public') {
return
}
import('vue').then((module) => {
const Vue = module.default
this.el.id = 'files-workspace-wrapper'
Vue.prototype.t = window.t
Vue.prototype.n = window.n
Vue.prototype.OCA = window.OCA
const View = Vue.extend(RichWorkspace)
const vm = new View({
propsData: {
path: fileList.getCurrentDirectory(),
},
store,
}).$mount(this.el)
subscribe('files:navigation:changed', () => {
// Expose if the default file list is active to the component
// to only render the workspace if the file list is actually visible
vm.active = OCA.Files.App.getCurrentFileList() === fileList
})
fileList.$el.on('urlChanged', data => {
vm.path = data.dir.toString()
})
fileList.$el.on('changeDirectory', data => {
vm.path = data.dir.toString()
})
})
},
}
export {
optimalPath,
registerFileActionFallback,
registerFileCreate,
FilesWorkspacePlugin,
FILE_ACTION_IDENTIFIER,
}