diff --git a/appinfo/info.xml b/appinfo/info.xml index 835cb8a0e..b4aebb05f 100755 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -28,7 +28,7 @@ https://github.com/nextcloud/officeonline/raw/main/screenshot.png - + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 3e04d975a..0611f0fed 100755 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -46,8 +46,12 @@ use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Files\Template\ITemplateManager; +use OCP\Files\Template\TemplateFileCreator; use OCP\IPreview; use Psr\Log\LoggerInterface; +use OCP\IL10N; +use OCP\IConfig; class Application extends App implements IBootstrap { public const APP_ID = 'officeonline'; @@ -68,15 +72,17 @@ public function boot(IBootContext $context): void { if (!$this->isEnabled()) { return; } + $this->registerProvider(); $this->updateCSP(); + $this->registerNewFileCreators($context); } public function isEnabled(): bool { $currentUser = \OC::$server->getUserSession()->getUser(); if ($currentUser !== null) { /** @var PermissionManager $permissionManager */ - $permissionManager = \OCP\Server::get(PermissionManager::class); + $permissionManager = \OC::$server->query(PermissionManager::class); if (!$permissionManager->isEnabledForUser($currentUser)) { return false; } @@ -162,7 +168,7 @@ public function updateCSP() { } } } catch (\Throwable $e) { - \OCP\Server::get(LoggerInterface::class)->warning('Failed to gather federation hosts for CSP', [ + \OC::$server->query(LoggerInterface::class)->warning('Failed to gather federation hosts for CSP', [ 'exception' => $e, 'app' => 'officeonline' ]); @@ -184,4 +190,52 @@ private function domainOnly(string $url): string { $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; return "$scheme$host$port"; } + + private function registerNewFileCreators($context) { + $context->injectFn(function (ITemplateManager $templateManager, IL10N $l10n, IConfig $config) { + if (!$this->isEnabled()) { + return; + } + $ooxml = $config->getAppValue(self::APP_ID, 'doc_format', '') === 'ooxml'; + $templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml) { + $odtType = new TemplateFileCreator('richdocuments', $l10n->t('New document'), ($ooxml ? '.docx' : '.odt')); + if ($ooxml) { + $odtType->addMimetype('application/msword'); + $odtType->addMimetype('application/vnd.openxmlformats-officedocument.wordprocessingml.document'); + } else { + $odtType->addMimetype('application/vnd.oasis.opendocument.text'); + $odtType->addMimetype('application/vnd.oasis.opendocument.text-template'); + } + $odtType->setIconClass('icon-filetype-document'); + $odtType->setRatio(21 / 29.7); + return $odtType; + }); + $templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml) { + $odsType = new TemplateFileCreator('richdocuments', $l10n->t('New spreadsheet'), ($ooxml ? '.xlsx' : '.ods')); + if ($ooxml) { + $odsType->addMimetype('application/vnd.ms-excel'); + $odsType->addMimetype('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); + } else { + $odsType->addMimetype('application/vnd.oasis.opendocument.spreadsheet'); + $odsType->addMimetype('application/vnd.oasis.opendocument.spreadsheet-template'); + } + $odsType->setIconClass('icon-filetype-spreadsheet'); + $odsType->setRatio(16 / 9); + return $odsType; + }); + $templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml) { + $odpType = new TemplateFileCreator('richdocuments', $l10n->t('New presentation'), ($ooxml ? '.pptx' : '.odp')); + if ($ooxml) { + $odpType->addMimetype('application/vnd.ms-powerpoint'); + $odpType->addMimetype('application/vnd.openxmlformats-officedocument.presentationml.presentation'); + } else { + $odpType->addMimetype('application/vnd.oasis.opendocument.presentation'); + $odpType->addMimetype('application/vnd.oasis.opendocument.presentation-template'); + } + $odpType->setIconClass('icon-filetype-presentation'); + $odpType->setRatio(16 / 9); + return $odpType; + }); + }); + } } diff --git a/src/files.js b/src/files.js index 18a0c8426..f822a15f9 100644 --- a/src/files.js +++ b/src/files.js @@ -1,12 +1,9 @@ -import Types from './helpers/types' -import axios from '@nextcloud/axios' import { getCapabilities } from '@nextcloud/capabilities' import './viewer.js' import Vue from 'vue' import Office from './view/Office' import './css/icons.css' -import { getCurrentDirectory } from './helpers/index.js' // eslint-disable-next-line __webpack_nonce__ = btoa(window.OC.requestToken) @@ -19,77 +16,6 @@ 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 = 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 @@ -103,6 +29,4 @@ document.addEventListener('DOMContentLoaded', () => { render: h => h(Office, { props: { fileName: document.getElementById('filename').value } }), }).$mount('#imgframe') } - // new file menu - OC.Plugins.register('OCA.Files.NewFileMenu', NewFilePlugin) })