Skip to content

Commit

Permalink
Merge pull request #3649 from nextcloud/backport/3645/stable29
Browse files Browse the repository at this point in the history
[stable29] File action to open PDFs with richdocuments when files_pdfviewer is enabled
  • Loading branch information
juliushaertl committed May 2, 2024
2 parents 88a8d9d + 52c0948 commit b7b8092
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
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 @@ -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',
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 @@ -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;
Expand All @@ -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;
Expand All @@ -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);
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)
11 changes: 9 additions & 2 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 @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b7b8092

Please sign in to comment.