diff --git a/appinfo/info.xml b/appinfo/info.xml index cfbafdd..39f1e20 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -18,6 +18,6 @@ https://raw.githubusercontent.com/ACTom/files_mindmap/master/screenshots/2.png https://raw.githubusercontent.com/ACTom/files_mindmap/master/screenshots/3.png - + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 361b3e9..1d1d5f6 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -9,12 +9,15 @@ use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Http\ContentSecurityPolicy; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\IMimeTypeDetector; use OCP\Security\CSP\AddContentSecurityPolicyEvent; use OCA\Viewer\Event\LoadViewer; use OCA\Files_MindMap\Listener\LoadAdditionalListener; use OCA\Files_MindMap\Listener\LoadViewerListener; +use OCA\Files_MindMap\Listener\LoadPublicViewerListener; + class Application extends App implements IBootstrap { @@ -37,6 +40,7 @@ public function registerProvider() { public function register(IRegistrationContext $context): void { $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class); + $context->registerEventListener(BeforeTemplateRenderedEvent::class, LoadPublicViewerListener::class); $context->registerEventListener(LoadViewer::class, LoadViewerListener::class); } diff --git a/lib/Listener/LoadAdditionalListener.php b/lib/Listener/LoadAdditionalListener.php index 39b583e..8badfc5 100644 --- a/lib/Listener/LoadAdditionalListener.php +++ b/lib/Listener/LoadAdditionalListener.php @@ -20,6 +20,6 @@ public function handle(Event $event): void { public static function additionalScripts() { Util::addStyle(Application::APPNAME, 'style'); - Util::addScript(Application::APPNAME, 'files_mindmap-mindmap'); +// Util::addScript(Application::APPNAME, 'files_mindmap-mindmap'); } } diff --git a/lib/Listener/LoadPublicViewerListener.php b/lib/Listener/LoadPublicViewerListener.php new file mode 100644 index 0000000..39d1976 --- /dev/null +++ b/lib/Listener/LoadPublicViewerListener.php @@ -0,0 +1,28 @@ + + */ +class LoadPublicViewerListener implements IEventListener { + public function handle(Event $event): void { + if (!$event instanceof BeforeTemplateRenderedEvent) { + return; + } + // Make sure we are on a public page rendering + if ($event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_PUBLIC) { + return; + } + Util::addScript(Application::APPNAME, 'files_mindmap-public'); + } +} \ No newline at end of file diff --git a/src/logger.js b/src/logger.js new file mode 100644 index 0000000..16315ad --- /dev/null +++ b/src/logger.js @@ -0,0 +1,9 @@ +import { getLoggerBuilder } from '@nextcloud/logger' + + +const logger = getLoggerBuilder() + .setApp('Files_MindMap') + .detectUser() + .build() + +export default logger \ No newline at end of file diff --git a/src/mindmap.js b/src/mindmap.js index 423d64b..5b8a53c 100644 --- a/src/mindmap.js +++ b/src/mindmap.js @@ -17,6 +17,7 @@ import { emit } from '@nextcloud/event-bus' import axios from '@nextcloud/axios' import { getCurrentUser } from '@nextcloud/auth' import { dirname } from '@nextcloud/paths' +import { isPublicShare } from '@nextcloud/sharing/public' import util from './util' @@ -71,6 +72,18 @@ var FilesMindMap = { OC.Notification.hide(id, t); }, + /** + * Determine if this page is public mindmap share page + * @returns {boolean} + */ + isMindmapPublic: function() { + if (!isPublicShare()) { + return false; + } + + return this.isSupportedMime($('#mimetype').val()); + }, + save: function(data, success, fail) { var url = ''; var path = this._file.dir + '/' + this._file.name; @@ -270,36 +283,4 @@ var FilesMindMap = { }, }; -// TODO: move to @nextcloud/files -// function getUniqueName(name, names) { -// let newName = name -// let i = 1 -// while (names.includes(newName)) { -// const ext = extname(name) -// newName = `${basename(name, ext)} (${i++})${ext}` -// } -// return newName -// } - -OCA.FilesMindMap = FilesMindMap; - - -console.debug('files_mindmaps start.'); - -// register mime types -FilesMindMap.init(); - -console.debug('files_mindmaps registerNewFileMenuPlugin.'); -// Declare the plugin and its attachments -OCA.FilesMindMap.registerNewFileMenuPlugin(); -console.debug('files_mindmaps registerFileActions.'); -OCA.FilesMindMap.registerFileActions(); - -if ($('#isPublic').val() && OCA.FilesMindMap.isSupportedMime($('#mimetype').val())) { - var sharingToken = $('#sharingToken').val(); - var downloadUrl = OC.generateUrl('/s/{token}/download', {token: sharingToken}); - var viewer = OCA.FilesMindMap; - viewer.show(downloadUrl, false); -} - -console.log('files_mindmaps loaded.'); +export default FilesMindMap; \ No newline at end of file diff --git a/src/mindmapviewer.js b/src/mindmapviewer.js index c3f389b..0ca7e22 100644 --- a/src/mindmapviewer.js +++ b/src/mindmapviewer.js @@ -1,4 +1,12 @@ import MindMap from './views/MindMap.vue' +import FilesMindMap from './mindmap' + +OCA.FilesMindMap = FilesMindMap; + +FilesMindMap.init(); +FilesMindMap.registerNewFileMenuPlugin(); +FilesMindMap.registerFileActions(); + const supportedMimes = OCA.FilesMindMap.getSupportedMimetypes(); @@ -11,4 +19,4 @@ if (OCA.Viewer) { theme: 'default', canCompare: true, }); -} \ No newline at end of file +} diff --git a/src/public.js b/src/public.js new file mode 100644 index 0000000..3a3de6f --- /dev/null +++ b/src/public.js @@ -0,0 +1,45 @@ +import FilesMindMap from './mindmap' +import logger from './logger' + +import { getSharingToken, isPublicShare } from '@nextcloud/sharing/public' + +if (isPublicShare()) { + OCA.FilesMindMap = FilesMindMap; + FilesMindMap.init(); + + if (FilesMindMap.isMindmapPublic()) { + window.addEventListener('DOMContentLoaded', function() { + var sharingToken = getSharingToken(); + var downloadUrl = OC.generateUrl('/s/{token}/download', {token: sharingToken}); + var viewer = OCA.FilesMindMap; + + + const contentElmt = document.getElementById('files-public-content'); + const footerElmt = document.querySelector('body > footer') || document.querySelector('#app-content > footer'); + if (contentElmt) { + if (OCA.Viewer) { + contentElmt.innerHTML = ''; + OCA.Viewer.setRootElement('#files-public-content') + OCA.Viewer.open({ path: '/' }); + + footerElmt.style.display = 'none'; + + // This is an ugly implementation, need to remove the top margin after viewer creates the iframe + setTimeout(() => { + const frameElmt = document.querySelector('#viewer > iframe'); + if (frameElmt) { + frameElmt.style.marginTop = '0'; + } + }, 1000); + + } else { + logger.error('Viewer is not available, cannot preview mindmap'); + } + } + + }); + } + + + console.log('files_mindmap public.js loaded'); +} \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index c6f199e..ff77836 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,6 +7,7 @@ declare const __dirname: string export default createAppConfig({ mindmap: join(__dirname, 'src', 'mindmap.js'), + public: join(__dirname, 'src', 'public.js'), mindmapviewer: join(__dirname, 'src', 'mindmapviewer.js'), }, { inlineCSS: { relativeCSSInjection: true },