Skip to content

Commit d7575fe

Browse files
committed
Add sharing support, optimize code structur
1 parent def8c98 commit d7575fe

9 files changed

Lines changed: 112 additions & 36 deletions

File tree

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
<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>
1919
<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>
2020
<dependencies>
21-
<nextcloud min-version="28" max-version="30"/>
21+
<nextcloud min-version="28" max-version="31"/>
2222
</dependencies>
2323
</info>

lib/AppInfo/Application.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
use OCP\AppFramework\Bootstrap\IBootstrap;
1010
use OCP\AppFramework\Bootstrap\IRegistrationContext;
1111
use OCP\AppFramework\Http\ContentSecurityPolicy;
12+
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
1213
use OCP\EventDispatcher\IEventDispatcher;
1314
use OCP\Files\IMimeTypeDetector;
1415
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
1516
use OCA\Viewer\Event\LoadViewer;
1617
use OCA\Files_MindMap\Listener\LoadAdditionalListener;
1718
use OCA\Files_MindMap\Listener\LoadViewerListener;
19+
use OCA\Files_MindMap\Listener\LoadPublicViewerListener;
20+
1821

1922

2023
class Application extends App implements IBootstrap {
@@ -37,6 +40,7 @@ public function registerProvider() {
3740

3841
public function register(IRegistrationContext $context): void {
3942
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
43+
$context->registerEventListener(BeforeTemplateRenderedEvent::class, LoadPublicViewerListener::class);
4044
$context->registerEventListener(LoadViewer::class, LoadViewerListener::class);
4145
}
4246

lib/Listener/LoadAdditionalListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function handle(Event $event): void {
2020

2121
public static function additionalScripts() {
2222
Util::addStyle(Application::APPNAME, 'style');
23-
Util::addScript(Application::APPNAME, 'files_mindmap-mindmap');
23+
// Util::addScript(Application::APPNAME, 'files_mindmap-mindmap');
2424
}
2525
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OCA\Files_MindMap\Listener;
6+
7+
use OCA\Files_MindMap\AppInfo\Application;
8+
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
9+
use OCP\AppFramework\Http\TemplateResponse;
10+
use OCP\EventDispatcher\Event;
11+
use OCP\EventDispatcher\IEventListener;
12+
use OCP\Util;
13+
14+
/**
15+
* @template-implements IEventListener<BeforeTemplateRenderedEvent>
16+
*/
17+
class LoadPublicViewerListener implements IEventListener {
18+
public function handle(Event $event): void {
19+
if (!$event instanceof BeforeTemplateRenderedEvent) {
20+
return;
21+
}
22+
// Make sure we are on a public page rendering
23+
if ($event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_PUBLIC) {
24+
return;
25+
}
26+
Util::addScript(Application::APPNAME, 'files_mindmap-public');
27+
}
28+
}

src/logger.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { getLoggerBuilder } from '@nextcloud/logger'
2+
3+
4+
const logger = getLoggerBuilder()
5+
.setApp('Files_MindMap')
6+
.detectUser()
7+
.build()
8+
9+
export default logger

src/mindmap.js

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { emit } from '@nextcloud/event-bus'
1717
import axios from '@nextcloud/axios'
1818
import { getCurrentUser } from '@nextcloud/auth'
1919
import { dirname } from '@nextcloud/paths'
20+
import { isPublicShare } from '@nextcloud/sharing/public'
2021

2122

2223
import util from './util'
@@ -71,6 +72,18 @@ var FilesMindMap = {
7172
OC.Notification.hide(id, t);
7273
},
7374

75+
/**
76+
* Determine if this page is public mindmap share page
77+
* @returns {boolean}
78+
*/
79+
isMindmapPublic: function() {
80+
if (!isPublicShare()) {
81+
return false;
82+
}
83+
84+
return this.isSupportedMime($('#mimetype').val());
85+
},
86+
7487
save: function(data, success, fail) {
7588
var url = '';
7689
var path = this._file.dir + '/' + this._file.name;
@@ -270,36 +283,4 @@ var FilesMindMap = {
270283
},
271284
};
272285

273-
// TODO: move to @nextcloud/files
274-
// function getUniqueName(name, names) {
275-
// let newName = name
276-
// let i = 1
277-
// while (names.includes(newName)) {
278-
// const ext = extname(name)
279-
// newName = `${basename(name, ext)} (${i++})${ext}`
280-
// }
281-
// return newName
282-
// }
283-
284-
OCA.FilesMindMap = FilesMindMap;
285-
286-
287-
console.debug('files_mindmaps start.');
288-
289-
// register mime types
290-
FilesMindMap.init();
291-
292-
console.debug('files_mindmaps registerNewFileMenuPlugin.');
293-
// Declare the plugin and its attachments
294-
OCA.FilesMindMap.registerNewFileMenuPlugin();
295-
console.debug('files_mindmaps registerFileActions.');
296-
OCA.FilesMindMap.registerFileActions();
297-
298-
if ($('#isPublic').val() && OCA.FilesMindMap.isSupportedMime($('#mimetype').val())) {
299-
var sharingToken = $('#sharingToken').val();
300-
var downloadUrl = OC.generateUrl('/s/{token}/download', {token: sharingToken});
301-
var viewer = OCA.FilesMindMap;
302-
viewer.show(downloadUrl, false);
303-
}
304-
305-
console.log('files_mindmaps loaded.');
286+
export default FilesMindMap;

src/mindmapviewer.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import MindMap from './views/MindMap.vue'
2+
import FilesMindMap from './mindmap'
3+
4+
OCA.FilesMindMap = FilesMindMap;
5+
6+
FilesMindMap.init();
7+
FilesMindMap.registerNewFileMenuPlugin();
8+
FilesMindMap.registerFileActions();
9+
210

311
const supportedMimes = OCA.FilesMindMap.getSupportedMimetypes();
412

@@ -11,4 +19,4 @@ if (OCA.Viewer) {
1119
theme: 'default',
1220
canCompare: true,
1321
});
14-
}
22+
}

src/public.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import FilesMindMap from './mindmap'
2+
import logger from './logger'
3+
4+
import { getSharingToken, isPublicShare } from '@nextcloud/sharing/public'
5+
6+
if (isPublicShare()) {
7+
OCA.FilesMindMap = FilesMindMap;
8+
FilesMindMap.init();
9+
10+
if (FilesMindMap.isMindmapPublic()) {
11+
window.addEventListener('DOMContentLoaded', function() {
12+
var sharingToken = getSharingToken();
13+
var downloadUrl = OC.generateUrl('/s/{token}/download', {token: sharingToken});
14+
var viewer = OCA.FilesMindMap;
15+
16+
17+
const contentElmt = document.getElementById('files-public-content');
18+
const footerElmt = document.querySelector('body > footer') || document.querySelector('#app-content > footer');
19+
if (contentElmt) {
20+
if (OCA.Viewer) {
21+
contentElmt.innerHTML = '';
22+
OCA.Viewer.setRootElement('#files-public-content')
23+
OCA.Viewer.open({ path: '/' });
24+
25+
footerElmt.style.display = 'none';
26+
27+
// This is an ugly implementation, need to remove the top margin after viewer creates the iframe
28+
setTimeout(() => {
29+
const frameElmt = document.querySelector('#viewer > iframe');
30+
if (frameElmt) {
31+
frameElmt.style.marginTop = '0';
32+
}
33+
}, 1000);
34+
35+
} else {
36+
logger.error('Viewer is not available, cannot preview mindmap');
37+
}
38+
}
39+
40+
});
41+
}
42+
43+
44+
console.log('files_mindmap public.js loaded');
45+
}

vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare const __dirname: string
77

88
export default createAppConfig({
99
mindmap: join(__dirname, 'src', 'mindmap.js'),
10+
public: join(__dirname, 'src', 'public.js'),
1011
mindmapviewer: join(__dirname, 'src', 'mindmapviewer.js'),
1112
}, {
1213
inlineCSS: { relativeCSSInjection: true },

0 commit comments

Comments
 (0)