Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for opening files through viewer #2778

Merged
merged 3 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\TalkSession;
use OCA\Viewer\Event\LoadViewer;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
Expand All @@ -40,6 +41,7 @@
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\NotPermittedException;
use OCP\IInitialStateService;
Expand All @@ -53,6 +55,8 @@
class PageController extends Controller {
/** @var string|null */
private $userId;
/** @var IEventDispatcher */
private $eventDispatcher;
/** @var RoomController */
private $api;
/** @var TalkSession */
Expand All @@ -78,6 +82,7 @@ class PageController extends Controller {

public function __construct(string $appName,
IRequest $request,
IEventDispatcher $eventDispatcher,
RoomController $api,
TalkSession $session,
IUserSession $userSession,
Expand All @@ -91,6 +96,7 @@ public function __construct(string $appName,
IRootFolder $rootFolder,
Config $config) {
parent::__construct($appName, $request);
$this->eventDispatcher = $eventDispatcher;
$this->api = $api;
$this->talkSession = $session;
$this->userSession = $userSession;
Expand Down Expand Up @@ -243,6 +249,10 @@ public function index(string $token = '', string $callUser = '', string $passwor
}
}

if (class_exists(LoadViewer::class)) {
$this->eventDispatcher->dispatchTyped(new LoadViewer());
}

$params = [
'token' => $token,
'signaling-settings' => $this->config->getSettings($this->userId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
<template>
<a :href="link"
class="container"
:class="{ 'is-viewer-available': isViewerAvailable }"
target="_blank"
rel="noopener noreferrer">
rel="noopener noreferrer"
@click="showPreview">
<img v-if="!isLoading && !failed"
class="preview"
alt=""
Expand Down Expand Up @@ -56,6 +58,10 @@ export default {
type: String,
required: true,
},
path: {
type: String,
required: true,
},
link: {
type: String,
default: '',
Expand Down Expand Up @@ -91,6 +97,27 @@ export default {
height: previewSize,
})
},
isViewerAvailable() {
if (!OCA.Viewer) {
return false
}

const availableHandlers = OCA.Viewer.availableHandlers
for (let i = 0; i < availableHandlers.length; i++) {
if (availableHandlers[i].mimes.includes(this.mimetype)) {
return true
}
}

return false
},
internalAbsolutePath() {
if (this.path.startsWith('/')) {
return this.path
}

return '/' + this.path
},
},
mounted() {
const img = new Image()
Expand All @@ -103,6 +130,22 @@ export default {
}
img.src = this.previewUrl
},
methods: {
showPreview(event) {
if (!this.isViewerAvailable) {
// Regular event handling by opening the link.
return
}

event.stopPropagation()
event.preventDefault()

OCA.Viewer.open({
// Viewer expects an internal absolute path starting with "/".
path: this.internalAbsolutePath,
})
},
},
}
</script>

Expand All @@ -114,6 +157,22 @@ export default {
image. */
display: inline-block;

/* Show a hover colour around the preview when navigating with the
* keyboard through the file links (or hovering them with the mouse). */
&:hover,
&:focus,
&:active {
.preview {
background-color: var(--color-background-hover);

/* Trick to keep the same position while adding a padding to show
* the background. */
box-sizing: content-box !important;
padding: 10px;
margin: -10px;
}
}

.preview {
display: block;
width: 128px;
Expand All @@ -125,6 +184,12 @@ export default {
force it to be on its own line below the preview. */
display: block;
}

&:not(.is-viewer-available) {
strong:after {
content: " ↗";
}
}
}

</style>