Skip to content

Commit

Permalink
fix: file action for opening pdf files with richdocuments
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed May 2, 2024
1 parent bda8da3 commit a75c2c8
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\\RegisterTemplateFileCreatorListener' => $baseDir . '/../lib/Listener/RegisterTemplateFileCreatorListener.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\\RegisterTemplateFileCreatorListener' => __DIR__ . '/..' . '/../lib/Listener/RegisterTemplateFileCreatorListener.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\RegisterTemplateFileCreatorListener;
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\RegisterTemplateCreatorEvent;
use OCP\Preview\BeforePreviewFetchedEvent;
Expand All @@ -79,6 +81,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 a75c2c8

Please sign in to comment.