From 31e14040dc63042996fc737357d1b2b0ca563323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 16 Apr 2024 10:12:06 +0200 Subject: [PATCH] fix: Load public share link file creation again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/AppInfo/Application.php | 8 ++-- src/document.js | 2 +- src/files.js | 75 +++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index f0d2ceabf..18ed9e60c 100755 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -27,11 +27,10 @@ use OC\Files\Type\Detection; use OC\Security\CSP\ContentSecurityPolicy; use OCA\Federation\TrustedServers; -use OCA\Files\Event\LoadAdditionalScriptsEvent; -use OCA\Files_Sharing\Listener\LoadAdditionalListener; +use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; use OCA\Officeonline\Capabilities; use OCA\Officeonline\Hooks\WopiLockHooks; -use OCA\Officeonline\Listener\FilesScriptListener; +use OCA\Officeonline\Listener\SharingLoadAdditionalScriptsListener; use OCA\Officeonline\Listener\LoadViewerListener; use OCA\Officeonline\Middleware\WOPIMiddleware; use OCA\Officeonline\PermissionManager; @@ -63,8 +62,7 @@ public function __construct(array $urlParams = []) { public function register(IRegistrationContext $context): void { $context->registerCapability(Capabilities::class); $context->registerMiddleWare(WOPIMiddleware::class); - $context->registerEventListener(LoadAdditionalScriptsEvent::class, FilesScriptListener::class); - $context->registerEventListener(LoadAdditionalListener::class, FilesScriptListener::class); + $context->registerEventListener(BeforeTemplateRenderedEvent::class, SharingLoadAdditionalScriptsListener::class); $context->registerEventListener(LoadViewer::class, LoadViewerListener::class); } diff --git a/src/document.js b/src/document.js index e8d275927..3b7774b45 100644 --- a/src/document.js +++ b/src/document.js @@ -283,7 +283,7 @@ const documentsMain = { }, 45000) }) - $('#loleafletframe').load(function() { + $('#loleafletframe').on('load', function() { const ViewerToLool = [ 'Action_FollowUser', 'Host_VersionRestore', diff --git a/src/files.js b/src/files.js index f822a15f9..bcd964ccb 100644 --- a/src/files.js +++ b/src/files.js @@ -1,3 +1,5 @@ +import Types from './helpers/types' +import axios from '@nextcloud/axios' import { getCapabilities } from '@nextcloud/capabilities' import './viewer.js' import Vue from 'vue' @@ -16,6 +18,77 @@ Vue.prototype.n = window.n Vue.prototype.OC = window.OC Vue.prototype.OCA = window.OCA +const NewFilePlugin = { + attach: function(newFileMenu) { + const self = this + const document = Types.getFileType('document') + const spreadsheet = Types.getFileType('spreadsheet') + const presentation = Types.getFileType('presentation') + + newFileMenu.addMenuEntry({ + id: 'add-' + document.extension, + displayName: t('officeonline', 'New Document'), + templateName: t('officeonline', 'New Document') + '.' + document.extension, + iconClass: 'icon-filetype-document', + fileType: 'x-office-document', + actionHandler: function(filename) { + self._createDocument(document.mime, filename) + }, + }) + + newFileMenu.addMenuEntry({ + id: 'add-' + spreadsheet.extension, + displayName: t('officeonline', 'New Spreadsheet'), + templateName: t('officeonline', 'New Spreadsheet') + '.' + spreadsheet.extension, + iconClass: 'icon-filetype-spreadsheet', + fileType: 'x-office-spreadsheet', + actionHandler: function(filename) { + self._createDocument(spreadsheet.mime, filename) + }, + }) + + newFileMenu.addMenuEntry({ + id: 'add-' + presentation.extension, + displayName: t('officeonline', 'New Presentation'), + templateName: t('officeonline', 'New Presentation') + '.' + presentation.extension, + iconClass: 'icon-filetype-presentation', + fileType: 'x-office-presentation', + actionHandler: function(filename) { + self._createDocument(presentation.mime, filename) + }, + }) + }, + + _createDocument: function(mimetype, filename) { + const dir = document.getElementById('dir') ? document.getElementById('dir').value : window.FileList.getCurrentDirectory() + try { + OCA.Files.Files.isFileNameValid(filename) + } catch (e) { + window.OC.dialogs.alert(e, t('core', 'Could not create file')) + return + } + filename = FileList.getUniqueName(filename) + const path = dir + '/' + filename + + const isPublic = document.getElementById('isPublic') ? document.getElementById('isPublic').value === '1' : false + if (isPublic) { + return window.FileList.createFile(filename).then(function() { + OCA.Viewer.open({ path }) + }) + } + + axios.post(OC.generateUrl('apps/officeonline/ajax/documents/create'), { mimetype, filename, dir }).then(({ data }) => { + console.debug(data) + if (data && data.status === 'success') { + window.FileList.add(data.data, { animate: true, scrollTo: true }) + window.OCA.Viewer.open({ path }) + } else { + window.OC.dialogs.alert(data.data.message, t('core', 'Could not create file')) + } + }) + }, +} + document.addEventListener('DOMContentLoaded', () => { // PUBLIC SHARE LINK HANDLING const isPublic = document.getElementById('isPublic') ? document.getElementById('isPublic').value === '1' : false @@ -29,4 +102,6 @@ document.addEventListener('DOMContentLoaded', () => { render: h => h(Office, { props: { fileName: document.getElementById('filename').value } }), }).$mount('#imgframe') } + + OC.Plugins.register('OCA.Files.NewFileMenu', NewFilePlugin) })