Skip to content

Commit 72d6655

Browse files
committed
Implement file reference wiget
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 1ab6698 commit 72d6655

16 files changed

+483
-4
lines changed

apps/files/composer/composer/autoload_classmap.php

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
'OCA\\Files\\Helper' => $baseDir . '/../lib/Helper.php',
4848
'OCA\\Files\\Listener\\LegacyLoadAdditionalScriptsAdapter' => $baseDir . '/../lib/Listener/LegacyLoadAdditionalScriptsAdapter.php',
4949
'OCA\\Files\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
50+
'OCA\\Files\\Listener\\RenderReferenceEventListener' => $baseDir . '/../lib/Listener/RenderReferenceEventListener.php',
5051
'OCA\\Files\\Migration\\Version11301Date20191205150729' => $baseDir . '/../lib/Migration/Version11301Date20191205150729.php',
5152
'OCA\\Files\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
5253
'OCA\\Files\\Search\\FilesSearchProvider' => $baseDir . '/../lib/Search/FilesSearchProvider.php',

apps/files/composer/composer/autoload_static.php

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class ComposerStaticInitFiles
6262
'OCA\\Files\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
6363
'OCA\\Files\\Listener\\LegacyLoadAdditionalScriptsAdapter' => __DIR__ . '/..' . '/../lib/Listener/LegacyLoadAdditionalScriptsAdapter.php',
6464
'OCA\\Files\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
65+
'OCA\\Files\\Listener\\RenderReferenceEventListener' => __DIR__ . '/..' . '/../lib/Listener/RenderReferenceEventListener.php',
6566
'OCA\\Files\\Migration\\Version11301Date20191205150729' => __DIR__ . '/..' . '/../lib/Migration/Version11301Date20191205150729.php',
6667
'OCA\\Files\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
6768
'OCA\\Files\\Search\\FilesSearchProvider' => __DIR__ . '/..' . '/../lib/Search/FilesSearchProvider.php',

apps/files/lib/AppInfo/Application.php

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OCA\Files\Event\LoadSidebar;
4545
use OCA\Files\Listener\LegacyLoadAdditionalScriptsAdapter;
4646
use OCA\Files\Listener\LoadSidebarListener;
47+
use OCA\Files\Listener\RenderReferenceEventListener;
4748
use OCA\Files\Notification\Notifier;
4849
use OCA\Files\Search\FilesSearchProvider;
4950
use OCA\Files\Service\TagService;
@@ -52,6 +53,7 @@
5253
use OCP\AppFramework\Bootstrap\IBootContext;
5354
use OCP\AppFramework\Bootstrap\IBootstrap;
5455
use OCP\AppFramework\Bootstrap\IRegistrationContext;
56+
use OCP\Collaboration\Reference\RenderReferenceEvent;
5557
use OCP\Collaboration\Resources\IProviderManager;
5658
use OCP\IConfig;
5759
use OCP\IL10N;
@@ -116,6 +118,7 @@ public function register(IRegistrationContext $context): void {
116118

117119
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LegacyLoadAdditionalScriptsAdapter::class);
118120
$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
121+
$context->registerEventListener(RenderReferenceEvent::class, RenderReferenceEventListener::class);
119122

120123
$context->registerSearchProvider(FilesSearchProvider::class);
121124

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @author Julius Härtl <jus@bitgrid.net>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
25+
namespace OCA\Files\Listener;
26+
27+
use OCP\Collaboration\Reference\RenderReferenceEvent;
28+
use OCP\EventDispatcher\Event;
29+
use OCP\EventDispatcher\IEventListener;
30+
31+
class RenderReferenceEventListener implements IEventListener {
32+
public function handle(Event $event): void {
33+
if (!$event instanceof RenderReferenceEvent) {
34+
return;
35+
}
36+
37+
\OCP\Util::addScript('files', 'reference-files');
38+
}
39+
}

apps/files/src/reference-files.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
3+
*
4+
* @author Julius Härtl <jus@bitgrid.net>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
import Vue from 'vue'
23+
import { translate as t } from '@nextcloud/l10n'
24+
25+
import { registerWidget } from '@nextcloud/vue-richtext'
26+
27+
import FileWidget from './views/ReferenceFileWidget.vue'
28+
29+
Vue.mixin({
30+
methods: {
31+
t,
32+
},
33+
})
34+
35+
registerWidget('file', (el, { richObjectType, richObject, accessible }) => {
36+
const Widget = Vue.extend(FileWidget)
37+
new Widget({
38+
propsData: {
39+
richObjectType,
40+
richObject,
41+
accessible,
42+
},
43+
}).$mount(el)
44+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<!--
2+
- @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
3+
-
4+
- @author Julius Härtl <jus@bitgrid.net>
5+
-
6+
- @license GNU AGPL version 3 or any later version
7+
-
8+
- This program is free software: you can redistribute it and/or modify
9+
- it under the terms of the GNU Affero General Public License as
10+
- published by the Free Software Foundation, either version 3 of the
11+
- License, or (at your option) any later version.
12+
-
13+
- This program is distributed in the hope that it will be useful,
14+
- but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
- GNU Affero General Public License for more details.
17+
-
18+
- You should have received a copy of the GNU Affero General Public License
19+
- along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
-->
21+
22+
<template>
23+
<div v-if="!accessible" class="widget-file widget-file--no-access">
24+
<div class="widget-file--image widget-file--image--icon icon-folder" />
25+
<div class="widget-file--details">
26+
<p class="widget-file--title">
27+
{{ t('files', 'File cannot be accessed') }}
28+
</p>
29+
<p class="widget-file--description">
30+
{{ t('files', 'You might not have have permissions to view it, ask the sender to share it') }}
31+
</p>
32+
</div>
33+
</div>
34+
<a v-else
35+
class="widget-file"
36+
:href="richObject.link"
37+
@click.prevent="navigate">
38+
<div class="widget-file--image" :class="filePreviewClass" :style="filePreview" />
39+
<div class="widget-file--details">
40+
<p class="widget-file--title">{{ richObject.name }}</p>
41+
<p class="widget-file--description">{{ fileSize }}<br>{{ fileMtime }}</p>
42+
<p class="widget-file--link">{{ filePath }}</p>
43+
</div>
44+
</a>
45+
</template>
46+
<script>
47+
import { generateUrl } from '@nextcloud/router'
48+
import path from 'path'
49+
50+
export default {
51+
name: 'ReferenceFileWidget',
52+
props: {
53+
richObject: {
54+
type: Object,
55+
required: true,
56+
},
57+
accessible: {
58+
type: Boolean,
59+
default: true,
60+
},
61+
},
62+
data() {
63+
return {
64+
previewUrl: window.OC.MimeType.getIconUrl(this.richObject.mimetype),
65+
}
66+
},
67+
computed: {
68+
fileSize() {
69+
return window.OC.Util.humanFileSize(this.richObject.size)
70+
},
71+
fileMtime() {
72+
return window.OC.Util.relativeModifiedDate(this.richObject.mtime * 1000)
73+
},
74+
filePath() {
75+
return path.dirname(this.richObject.path)
76+
},
77+
filePreview() {
78+
if (this.previewUrl) {
79+
return {
80+
backgroundImage: 'url(' + this.previewUrl + ')',
81+
}
82+
}
83+
84+
return {
85+
backgroundImage: 'url(' + window.OC.MimeType.getIconUrl(this.richObject.mimetype) + ')',
86+
}
87+
88+
},
89+
filePreviewClass() {
90+
if (this.previewUrl) {
91+
return 'widget-file--image--preview'
92+
}
93+
return 'widget-file--image--icon'
94+
95+
},
96+
},
97+
mounted() {
98+
if (this.richObject['preview-available']) {
99+
const previewUrl = generateUrl('/core/preview?fileId={fileId}&x=250&y=250', {
100+
fileId: this.richObject.id,
101+
})
102+
const img = new Image()
103+
img.onload = () => {
104+
this.previewUrl = previewUrl
105+
}
106+
img.onerror = err => {
107+
console.error('could not load recommendation preview', err)
108+
}
109+
img.src = previewUrl
110+
}
111+
},
112+
methods: {
113+
navigate() {
114+
if (OCA.Viewer && OCA.Viewer.mimetypes.indexOf(this.richObject.mimetype) !== -1) {
115+
OCA.Viewer.open({ path: this.richObject.path })
116+
return
117+
}
118+
window.location = generateUrl('/f/' + this.id)
119+
},
120+
},
121+
}
122+
</script>
123+
<style lang="scss" scoped>
124+
.widget-file {
125+
display: flex;
126+
127+
&--image {
128+
min-width: 40%;
129+
background-position: center;
130+
background-size: cover;
131+
background-repeat: no-repeat;
132+
133+
&.widget-file--image--icon {
134+
min-width: 88px;
135+
background-size: 44px;
136+
}
137+
}
138+
139+
&--title {
140+
overflow: hidden;
141+
text-overflow: ellipsis;
142+
white-space: nowrap;
143+
font-weight: bold;
144+
}
145+
146+
&--details {
147+
padding: 12px;
148+
flex-grow: 1;
149+
display: flex;
150+
flex-direction: column;
151+
152+
p {
153+
margin: 0;
154+
padding: 0;
155+
}
156+
}
157+
158+
&--description {
159+
overflow: hidden;
160+
text-overflow: ellipsis;
161+
display: -webkit-box;
162+
-webkit-line-clamp: 3;
163+
line-clamp: 3;
164+
-webkit-box-orient: vertical;
165+
}
166+
167+
&--link {
168+
color: var(--color-text-maxcontrast);
169+
}
170+
171+
&.widget-file--no-access {
172+
padding: 12px;
173+
174+
.widget-file--details {
175+
padding: 0;
176+
}
177+
}
178+
}
179+
</style>

dist/core-common.js

+2-2
Large diffs are not rendered by default.

dist/core-common.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/composer/composer/autoload_classmap.php

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
'OCP\\Collaboration\\Reference\\IReference' => $baseDir . '/lib/public/Collaboration/Reference/IReference.php',
146146
'OCP\\Collaboration\\Reference\\IReferenceManager' => $baseDir . '/lib/public/Collaboration/Reference/IReferenceManager.php',
147147
'OCP\\Collaboration\\Reference\\IReferenceProvider' => $baseDir . '/lib/public/Collaboration/Reference/IReferenceProvider.php',
148+
'OCP\\Collaboration\\Reference\\RenderReferenceEvent' => $baseDir . '/lib/public/Collaboration/Reference/RenderReferenceEvent.php',
148149
'OCP\\Collaboration\\Resources\\CollectionException' => $baseDir . '/lib/public/Collaboration/Resources/CollectionException.php',
149150
'OCP\\Collaboration\\Resources\\ICollection' => $baseDir . '/lib/public/Collaboration/Resources/ICollection.php',
150151
'OCP\\Collaboration\\Resources\\IManager' => $baseDir . '/lib/public/Collaboration/Resources/IManager.php',

lib/composer/composer/autoload_static.php

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
178178
'OCP\\Collaboration\\Reference\\IReference' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReference.php',
179179
'OCP\\Collaboration\\Reference\\IReferenceManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReferenceManager.php',
180180
'OCP\\Collaboration\\Reference\\IReferenceProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReferenceProvider.php',
181+
'OCP\\Collaboration\\Reference\\RenderReferenceEvent' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/RenderReferenceEvent.php',
181182
'OCP\\Collaboration\\Resources\\CollectionException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/CollectionException.php',
182183
'OCP\\Collaboration\\Resources\\ICollection' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ICollection.php',
183184
'OCP\\Collaboration\\Resources\\IManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IManager.php',

lib/private/Collaboration/Reference/File/FileReferenceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ private function fetchReference(Reference $reference): void {
133133
'id' => $file->getId(),
134134
'name' => $file->getName(),
135135
'size' => $file->getSize(),
136-
'path' => $file->getPath(),
136+
'path' => $userFolder->getRelativePath($file->getPath()),
137137
'link' => $reference->getUrl(),
138138
'mimetype' => $file->getMimetype(),
139+
'mtime' => $file->getMTime(),
139140
'preview-available' => $this->previewManager->isAvailable($file)
140141
]);
141142
} catch (InvalidPathException|NotFoundException|NotPermittedException|NoUserException $e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
4+
*
5+
* @author Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
declare(strict_types=1);
24+
/**
25+
* @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
26+
*
27+
* @author Julius Härtl <jus@bitgrid.net>
28+
*
29+
* @license GNU AGPL version 3 or any later version
30+
*
31+
* This program is free software: you can redistribute it and/or modify
32+
* it under the terms of the GNU Affero General Public License as
33+
* published by the Free Software Foundation, either version 3 of the
34+
* License, or (at your option) any later version.
35+
*
36+
* This program is distributed in the hope that it will be useful,
37+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
38+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39+
* GNU Affero General Public License for more details.
40+
*
41+
* You should have received a copy of the GNU Affero General Public License
42+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
43+
*/
44+
45+
namespace OCP\Collaboration\Reference;
46+
47+
use OCP\EventDispatcher\Event;
48+
49+
/**
50+
* Event that apps can emit on their page rendering to trigger loading of aditional
51+
* scripts for reference widget rendering
52+
*
53+
* @since 25.0.0
54+
*/
55+
class RenderReferenceEvent extends Event {
56+
}

0 commit comments

Comments
 (0)