Skip to content

Commit

Permalink
Merge pull request #3650 from nextcloud/backport/3645/stable28
Browse files Browse the repository at this point in the history
[stable28] File action to open PDFs with richdocuments when files_pdfviewer is enabled
  • Loading branch information
juliushaertl authored May 3, 2024
2 parents eb45c8a + a8bbdbd commit 9ad2f01
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'OCA\\Richdocuments\\Listener\\AddFeaturePolicyListener' => $baseDir . '/../lib/Listener/AddFeaturePolicyListener.php',
'OCA\\Richdocuments\\Listener\\BeforeFetchPreviewListener' => $baseDir . '/../lib/Listener/BeforeFetchPreviewListener.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',
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ComposerStaticInitRichdocuments
'OCA\\Richdocuments\\Listener\\AddFeaturePolicyListener' => __DIR__ . '/..' . '/../lib/Listener/AddFeaturePolicyListener.php',
'OCA\\Richdocuments\\Listener\\BeforeFetchPreviewListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeFetchPreviewListener.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',
Expand Down
2 changes: 1 addition & 1 deletion img/app.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCA\Richdocuments\Listener\AddFeaturePolicyListener;
use OCA\Richdocuments\Listener\BeforeFetchPreviewListener;
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;
Expand All @@ -54,6 +55,7 @@
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
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;
Expand All @@ -80,6 +82,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);
Expand Down
24 changes: 24 additions & 0 deletions lib/Listener/LoadAdditionalListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace OCA\Richdocuments\Listener;

use OCA\Richdocuments\AppInfo\Application;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

/** @template-implements IEventListener<LoadAdditionalScriptsEvent> */
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');
}
}
}
40 changes: 40 additions & 0 deletions src/file-actions.js
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -30,6 +31,12 @@ webpackConfig.module.rules.push({
exclude: /node_modules/,
})

// Raw files rule
webpackConfig.module.rules.push({
resourceQuery: /raw/,
type: 'asset/source',
})

// Add typescript extension resolver
webpackConfig.resolve.extensions.push('.tsx')

Expand Down

0 comments on commit 9ad2f01

Please sign in to comment.