diff options
author | Julius Härtl <jus@bitgrid.net> | 2019-06-24 22:24:08 +0300 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2019-06-24 22:24:18 +0300 |
commit | 072ad2fc88ab09c1bed1552d23c5c0965491ccd5 (patch) | |
tree | 822de2b5f8ab6f6b326b4b0e84035926bc9def68 | |
parent | e38f89c215b9ba1c0ce89ccde552da0538b7b429 (diff) |
Refactor file action fallback
Signed-off-by: Julius Härtl <jus@bitgrid.net>
-rw-r--r-- | src/components/PublicFilesEditor.vue | 7 | ||||
-rw-r--r-- | src/files.js | 46 | ||||
-rw-r--r-- | src/helpers.js | 104 | ||||
-rw-r--r-- | src/public.js | 51 |
4 files changed, 123 insertions, 85 deletions
diff --git a/src/components/PublicFilesEditor.vue b/src/components/PublicFilesEditor.vue index 4a51f6610..6991ad37c 100644 --- a/src/components/PublicFilesEditor.vue +++ b/src/components/PublicFilesEditor.vue @@ -22,7 +22,8 @@ <template> <modal v-if="active" :title="fileName" @close="close"> - <editor-wrapper :relative-path="relativePath" :active="active" :share-token="shareToken" /> + <editor-wrapper :file-id="fileId" :relative-path="relativePath" :active="active" + :share-token="shareToken" /> </modal> </template> @@ -36,6 +37,10 @@ export default { EditorWrapper: () => import('./EditorWrapper') }, props: { + fileId: { + type: Number, + default: null + }, relativePath: { type: String, default: null diff --git a/src/files.js b/src/files.js index 59d0b92b3..8d0fc2f93 100644 --- a/src/files.js +++ b/src/files.js @@ -21,56 +21,15 @@ */ import FilesEditor from './components/FilesEditor' +import { registerFileActionFallback, registerFileCreate } from './helpers' __webpack_nonce__ = btoa(OC.requestToken) // eslint-disable-line __webpack_public_path__ = OC.linkTo('text', 'js/') // eslint-disable-line -const openFileExtensions = [ - 'md', 'markdown' -] - -const newFileMenuPlugin = { - attach: function(menu) { - var fileList = menu.fileList - - // only attach to main file list, public view is not supported yet - if (fileList.id !== 'files') { - return - } - - // register the new menu entry - menu.addMenuEntry({ - id: 'file', - displayName: t('text', 'New text document'), - templateName: t('text', 'New text document.md'), - iconClass: 'icon-filetype-text', - fileType: 'file', - actionHandler: function(name) { - fileList.createFile(name).then(function(status, data) { - const fileExtension = name.split('.').pop() - if (openFileExtensions.indexOf(fileExtension) > -1) { - let fileInfoModel = new OCA.Files.FileInfoModel(data) - OCA.Files.fileActions.triggerAction('view', fileInfoModel, fileList) - } else if (typeof OCA.Files_Texteditor !== 'undefined') { - const dir = fileList.getCurrentDirectory() - OCA.Files_Texteditor._onEditorTrigger( - name, - { - fileList: fileList, - dir: dir - } - ) - } - }) - } - }) - } -} - -OC.Plugins.register('OCA.Files.NewFileMenu', newFileMenuPlugin) document.addEventListener('DOMContentLoaded', () => { if (typeof OCA.Viewer === 'undefined') { console.error('Viewer app is not installed') + registerFileActionFallback() return } OCA.Viewer.registerHandler({ @@ -79,6 +38,7 @@ document.addEventListener('DOMContentLoaded', () => { component: FilesEditor, group: null }) + registerFileCreate() }) OCA.Text = { diff --git a/src/helpers.js b/src/helpers.js index ba17150fa..defbf4e1b 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -27,6 +27,14 @@ import axios from 'axios' import { generateRemoteUrl } from 'nextcloud-server/dist/router' +const openFileExtensions = [ + 'md', 'markdown', 'txt' +] + +const openMimetypes = [ + 'text/markdown', 'text/plain' +] + const documentReady = function(callback) { const fn = () => setTimeout(callback, 0) if (document.attachEvent ? document.readyState === 'complete' : document.readyState !== 'loading') { @@ -90,9 +98,103 @@ const fetchFileInfo = async function(user, path) { }) } +const registerFileCreate = () => { + const newFileMenuPlugin = { + attach: function(menu) { + var 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 document'), + templateName: t('text', 'New text document.md'), + iconClass: 'icon-filetype-text', + fileType: 'file', + actionHandler: function(name) { + fileList.createFile(name).then(function(status, data) { + const fileExtension = name.split('.').pop() + let fileInfoModel = new OCA.Files.FileInfoModel(data) + if (typeof OCA.Viewer !== 'undefined' && openFileExtensions.indexOf(fileExtension) > -1) { + OCA.Files.fileActions.triggerAction('view', fileInfoModel, fileList) + } else if (typeof OCA.Viewer === 'undefined' && openFileExtensions.indexOf(fileExtension) > -1) { + OCA.Files.fileActions.triggerAction('Edit with text', fileInfoModel, fileList) + } else if (typeof OCA.Files_Texteditor !== 'undefined') { + const dir = fileList.getCurrentDirectory() + OCA.Files_Texteditor._onEditorTrigger( + name, + { + fileList: fileList, + dir: dir + } + ) + } + }) + } + }) + } + } + OC.Plugins.register('OCA.Files.NewFileMenu', newFileMenuPlugin) +} + +const registerFileActionFallback = () => { + const sharingToken = document.getElementById('sharingToken') ? document.getElementById('sharingToken').value : null + const dir = document.getElementById('dir').value + + if (!sharingToken || dir !== '') { + const ViewerRoot = document.createElement('div') + ViewerRoot.id = 'text-viewer-fallback' + document.body.appendChild(ViewerRoot) + const registerAction = (mime) => OCA.Files.fileActions.register( + mime, + 'Edit with text', + OC.PERMISSION_UPDATE | OC.PERMISSION_READ, + OC.imagePath('core', 'actions/rename'), + (filename) => { + const file = window.FileList.findFile(filename) + Promise.all([ + import('vue'), + import('./components/PublicFilesEditor') + ]).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: h => h(Editor, { + props: { + fileId: file ? file.id : null, + active: true, + shareToken: sharingToken, + relativePath: path + } + }) + }) + vm.$mount(ViewerRoot) + }) + }, + t('text', 'Edit') + ) + registerAction('text/markdown') + registerAction('text/plain') + OCA.Files.fileActions.setDefault('text/markdown', 'Edit with text') + OCA.Files.fileActions.setDefault('text/plain', 'Edit with text') + } + +} + export { documentReady, endpointUrl, getRandomGuestName, - fetchFileInfo + fetchFileInfo, + openFileExtensions, openMimetypes, + registerFileActionFallback, + registerFileCreate } diff --git a/src/public.js b/src/public.js index 154d5dfbc..f04018068 100644 --- a/src/public.js +++ b/src/public.js @@ -1,58 +1,29 @@ -import { documentReady } from './helpers' -import { newFileMenuPlugin } from './publicPlugins' +import { + documentReady, + registerFileActionFallback, + openMimetypes, + registerFileCreate +} from './helpers' __webpack_nonce__ = btoa(OC.requestToken) // eslint-disable-line __webpack_public_path__ = OC.linkTo('text', 'js/') // eslint-disable-line documentReady(() => { - const sharingToken = document.getElementById('sharingToken').value const dir = document.getElementById('dir').value const mimetype = document.getElementById('mimetype').value + const sharingToken = document.getElementById('sharingToken') ? document.getElementById('sharingToken').value : null - // Load regular viewer integration if (dir !== '') { - const ViewerRoot = document.createElement('div') - ViewerRoot.id = 'viewerpublic' - document.body.appendChild(ViewerRoot) - OC.Plugins.register('OCA.Files.NewFileMenu', newFileMenuPlugin) - OCA.Files.fileActions.register( - 'text/markdown', - 'Edit with text', - OC.PERMISSION_UPDATE | OC.PERMISSION_READ, - OC.imagePath('core', 'actions/rename'), - (filename) => { - Promise.all([ - import('vue'), - import('./components/PublicFilesEditor') - ]).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: h => h(Editor, { - props: { - active: true, - shareToken: sharingToken, - relativePath: path - } - }) - }) - vm.$mount(ViewerRoot) - }) - }, - t('text', 'Edit') - ) - OCA.Files.fileActions.setDefault('text/markdown', 'Edit with text') + registerFileActionFallback() + registerFileCreate() } else { + // single file share const container = document.createElement('div') container.id = 'texteditor' const body = document.getElementById('app-content') body.append(container) - if (mimetype === 'text/markdown') { + if (openMimetypes.indexOf(mimetype) !== -1) { Promise.all([ import('vue'), import('./components/EditorWrapper') |