Skip to content

Commit

Permalink
Add sharing support, optimize code structur
Browse files Browse the repository at this point in the history
  • Loading branch information
ACTom committed Jan 14, 2025
1 parent def8c98 commit d7575fe
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 36 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
<screenshot small-thumbnail="https://raw.githubusercontent.com/ACTom/files_mindmap/master/screenshots/2-small.png">https://raw.githubusercontent.com/ACTom/files_mindmap/master/screenshots/2.png</screenshot>
<screenshot small-thumbnail="https://raw.githubusercontent.com/ACTom/files_mindmap/master/screenshots/3-small.png">https://raw.githubusercontent.com/ACTom/files_mindmap/master/screenshots/3.png</screenshot>
<dependencies>
<nextcloud min-version="28" max-version="30"/>
<nextcloud min-version="28" max-version="31"/>
</dependencies>
</info>
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/LoadAdditionalListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
28 changes: 28 additions & 0 deletions lib/Listener/LoadPublicViewerListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace OCA\Files_MindMap\Listener;

use OCA\Files_MindMap\AppInfo\Application;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

/**
* @template-implements IEventListener<BeforeTemplateRenderedEvent>
*/
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');
}
}
9 changes: 9 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getLoggerBuilder } from '@nextcloud/logger'


const logger = getLoggerBuilder()
.setApp('Files_MindMap')
.detectUser()
.build()

export default logger
47 changes: 14 additions & 33 deletions src/mindmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
10 changes: 9 additions & 1 deletion src/mindmapviewer.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -11,4 +19,4 @@ if (OCA.Viewer) {
theme: 'default',
canCompare: true,
});
}
}
45 changes: 45 additions & 0 deletions src/public.js
Original file line number Diff line number Diff line change
@@ -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');
}
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit d7575fe

Please sign in to comment.