From 52c0948c016c0f153bc43b25a14a237fee194902 Mon Sep 17 00:00:00 2001 From: Elizabeth Danzberger Date: Mon, 29 Apr 2024 19:07:30 -0400 Subject: [PATCH] fix: file action for opening pdf files with richdocuments Signed-off-by: Elizabeth Danzberger --- composer/composer/autoload_classmap.php | 1 + composer/composer/autoload_static.php | 1 + img/app.svg | 2 +- lib/AppInfo/Application.php | 3 ++ lib/Listener/LoadAdditionalListener.php | 24 +++++++++++++++ src/file-actions.js | 40 +++++++++++++++++++++++++ webpack.js | 11 +++++-- 7 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 lib/Listener/LoadAdditionalListener.php create mode 100644 src/file-actions.js diff --git a/composer/composer/autoload_classmap.php b/composer/composer/autoload_classmap.php index ee7b00ed0f..0a6461598b 100644 --- a/composer/composer/autoload_classmap.php +++ b/composer/composer/autoload_classmap.php @@ -44,6 +44,7 @@ 'OCA\\Richdocuments\\Listener\\BeforeFetchPreviewListener' => $baseDir . '/../lib/Listener/BeforeFetchPreviewListener.php', 'OCA\\Richdocuments\\Listener\\BeforeTemplateRenderedListener' => $baseDir . '/../lib/Listener/BeforeTemplateRenderedListener.php', 'OCA\\Richdocuments\\Listener\\FileCreatedFromTemplateListener' => $baseDir . '/../lib/Listener/FileCreatedFromTemplateListener.php', + 'OCA\\Richdocuments\\Listener\\LoadAdditionalListener' => $baseDir . '/../lib/Listener/LoadAdditionalListener.php', 'OCA\\Richdocuments\\Listener\\LoadViewerListener' => $baseDir . '/../lib/Listener/LoadViewerListener.php', 'OCA\\Richdocuments\\Listener\\ReferenceListener' => $baseDir . '/../lib/Listener/ReferenceListener.php', 'OCA\\Richdocuments\\Listener\\ShareLinkListener' => $baseDir . '/../lib/Listener/ShareLinkListener.php', diff --git a/composer/composer/autoload_static.php b/composer/composer/autoload_static.php index aab9ca95c7..ad5166b283 100644 --- a/composer/composer/autoload_static.php +++ b/composer/composer/autoload_static.php @@ -59,6 +59,7 @@ class ComposerStaticInitRichdocuments 'OCA\\Richdocuments\\Listener\\BeforeFetchPreviewListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeFetchPreviewListener.php', 'OCA\\Richdocuments\\Listener\\BeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeTemplateRenderedListener.php', 'OCA\\Richdocuments\\Listener\\FileCreatedFromTemplateListener' => __DIR__ . '/..' . '/../lib/Listener/FileCreatedFromTemplateListener.php', + 'OCA\\Richdocuments\\Listener\\LoadAdditionalListener' => __DIR__ . '/..' . '/../lib/Listener/LoadAdditionalListener.php', 'OCA\\Richdocuments\\Listener\\LoadViewerListener' => __DIR__ . '/..' . '/../lib/Listener/LoadViewerListener.php', 'OCA\\Richdocuments\\Listener\\ReferenceListener' => __DIR__ . '/..' . '/../lib/Listener/ReferenceListener.php', 'OCA\\Richdocuments\\Listener\\ShareLinkListener' => __DIR__ . '/..' . '/../lib/Listener/ShareLinkListener.php', diff --git a/img/app.svg b/img/app.svg index 7e7da10361..858839619b 100644 --- a/img/app.svg +++ b/img/app.svg @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 084edf8bf2..572c4e992c 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -33,6 +33,7 @@ use OCA\Richdocuments\Listener\BeforeFetchPreviewListener; use OCA\Richdocuments\Listener\BeforeTemplateRenderedListener; use OCA\Richdocuments\Listener\FileCreatedFromTemplateListener; +use OCA\Richdocuments\Listener\LoadAdditionalListener; use OCA\Richdocuments\Listener\LoadViewerListener; use OCA\Richdocuments\Listener\ReferenceListener; use OCA\Richdocuments\Listener\ShareLinkListener; @@ -56,6 +57,7 @@ use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\Collaboration\Reference\RenderReferenceEvent; +use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent; use OCP\Files\Template\FileCreatedFromTemplateEvent; use OCP\Files\Template\ITemplateManager; use OCP\Files\Template\TemplateFileCreator; @@ -81,6 +83,7 @@ public function register(IRegistrationContext $context): void { $context->registerEventListener(FileCreatedFromTemplateEvent::class, FileCreatedFromTemplateListener::class); $context->registerEventListener(AddContentSecurityPolicyEvent::class, AddContentSecurityPolicyListener::class); $context->registerEventListener(AddFeaturePolicyEvent::class, AddFeaturePolicyListener::class); + $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class); $context->registerEventListener(LoadViewer::class, LoadViewerListener::class); $context->registerEventListener(ShareLinkAccessedEvent::class, ShareLinkListener::class); $context->registerEventListener(BeforePreviewFetchedEvent::class, BeforeFetchPreviewListener::class); diff --git a/lib/Listener/LoadAdditionalListener.php b/lib/Listener/LoadAdditionalListener.php new file mode 100644 index 0000000000..3ed0f56b10 --- /dev/null +++ b/lib/Listener/LoadAdditionalListener.php @@ -0,0 +1,24 @@ + */ +class LoadAdditionalListener implements IEventListener { + public function handle(Event $event): void { + // If not a LoadAdditionalScriptsEvent, we should do nothing + if (!($event instanceof LoadAdditionalScriptsEvent)) { + return; + } + + // If we can add an init script, we add the file-actions script + if (method_exists(Util::class, 'addInitScript')) { + Util::addInitScript(Application::APPNAME, 'richdocuments-fileActions'); + } + } +} diff --git a/src/file-actions.js b/src/file-actions.js new file mode 100644 index 0000000000..19e4033cfa --- /dev/null +++ b/src/file-actions.js @@ -0,0 +1,40 @@ +import { registerFileAction, FileAction } from '@nextcloud/files' +import { getCapabilities } from '@nextcloud/capabilities' +import { translate as t } from '@nextcloud/l10n' + +// eslint-disable-next-line import/no-unresolved +import appIcon from '../img/app.svg?raw' + +const richdocuments = getCapabilities().richdocuments + +const openPdf = new FileAction({ + id: 'office-open-pdf', + + iconSvgInline: () => { + // Make sure the icon is the correct color + return appIcon.replaceAll(/#(fff|0{6})/g, 'currentColor') + }, + + displayName: () => { + return t('richdocuments', + 'Edit with {productName}', + { productName: richdocuments.productName }) + }, + + enabled: () => { + // Only enable the file action when files_pdfviewer is enabled + const optionalMimetypes = richdocuments.mimetypesNoDefaultOpen + return optionalMimetypes.includes('application/pdf') + }, + + exec: (file) => { + // If no viewer API, we can't open the document + if (!OCA.Viewer) { + return + } + + OCA.Viewer.openWith('richdocuments', { path: file.path }) + }, +}) + +registerFileAction(openPdf) diff --git a/webpack.js b/webpack.js index dd5a0102e6..d33a405197 100644 --- a/webpack.js +++ b/webpack.js @@ -7,6 +7,7 @@ const BabelLoaderExcludeNodeModulesExcept = require('babel-loader-exclude-node-m webpackConfig.entry = { viewer: path.join(__dirname, 'src', 'viewer.js'), files: path.join(__dirname, 'src', 'files.js'), + fileActions: path.join(__dirname, 'src', 'file-actions.js'), document: path.join(__dirname, 'src', 'document.js'), admin: path.join(__dirname, 'src', 'admin.js'), personal: path.join(__dirname, 'src', 'personal.js'), @@ -17,7 +18,7 @@ webpackConfig.entry = { webpackRules.RULE_JS.test = /\.m?js$/ webpackRules.RULE_JS.exclude = BabelLoaderExcludeNodeModulesExcept([ '@nextcloud/dialogs', - '@nextcloud/event-bus' + '@nextcloud/event-bus', ]) // Replaces rules array @@ -27,7 +28,13 @@ webpackConfig.module.rules = Object.values(webpackRules) webpackConfig.module.rules.push({ test: /\.tsx?$/, use: ['babel-loader', 'ts-loader'], - exclude: /node_modules/ + exclude: /node_modules/, +}) + +// Raw files rule +webpackConfig.module.rules.push({ + resourceQuery: /raw/, + type: 'asset/source', }) // Add typescript extension resolver