-
Notifications
You must be signed in to change notification settings - Fork 0
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 share button to preview #15
base: master
Are you sure you want to change the base?
Changes from all commits
723777f
58e0fe6
e447eb8
615f863
0aed6b5
cbd2d79
7bc358a
da5d3c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ function __construct() | |
// Convert a size in byte to something more diggest | ||
$this->twig->addFilter(new TwigFilter("humanFileSize", function($size) { | ||
$units = ["B", "KB", "MB", "GB"]; | ||
for ($i = 0; $size > 1024; $i++) $size /= 1024; | ||
for ($i = 0; $size > 1024 && $i < count($units); $i++) $size /= 1024; | ||
|
||
return round($size, 2) . $units[$i]; | ||
})); | ||
|
@@ -153,6 +153,7 @@ private function readDirectory(string $directory, mixed $config): array|false | |
array_push($files, | ||
[ | ||
"name" => $name, | ||
"share" => urlencode($name), | ||
"isFolder" => $isFolder, | ||
"icon" => $isFolder ? Icons::getFolderIcon() : Icons::getIcon($ext), | ||
"lastModified" => $modified, | ||
|
@@ -288,7 +289,7 @@ public function registerFileDisplay(string $name, string $display) | |
$this->fileDisplays[$name] = $instance; | ||
} | ||
|
||
public function render(string $directory): string | ||
public function render(string $directory, string $preview): string | ||
{ | ||
// read the directory | ||
if (($files = $this->readDirectory($directory, $this->config)) === false) | ||
|
@@ -333,6 +334,10 @@ public function render(string $directory): string | |
} | ||
} | ||
|
||
if (urlencode($f["name"]) === $preview) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opening brace on new line. |
||
$f["preview"] = true; | ||
} | ||
|
||
if (!$displayBack && $f["name"] === "..") | ||
{ } | ||
else if (!$displayOthers && isset($displayMode) && !$this->fileDisplays[$displayMode]->doesHandle($f["extension"])) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,45 +12,71 @@ document.querySelector(".modal").addEventListener("click", function(event) { | |
event.currentTarget.style.display = "none" | ||
}) | ||
|
||
const prev = document.getElementById("selected-preview") | ||
if (prev != undefined) { | ||
showFile(prev.href, prev.dataset.preview, prev.dataset.filename, prev.dataset.share) | ||
} | ||
|
||
function fileClicked(event) { | ||
const target = event.currentTarget | ||
|
||
const file = target.dataset.preview | ||
const filename = target.dataset.filename | ||
const share = target.dataset.share | ||
|
||
// check if file is previewable | ||
if (file) { | ||
event.preventDefault() | ||
|
||
// fetch file preview | ||
fetch("/?preview=" + encodeURIComponent(file)) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw "Failed to preview file: " + response.statusText | ||
} | ||
|
||
return response.text() | ||
}) | ||
.then(content => { | ||
showFileModal(target, filename, content); | ||
document.querySelectorAll('pre code').forEach((el) => { | ||
hljs.highlightElement(el); | ||
}); | ||
}) | ||
showFile(target.href, file, filename, share) | ||
} | ||
} | ||
|
||
function showFileModal(target, filename, content) { | ||
function showFile(href, file, filename, shareName) { | ||
|
||
// fetch file preview | ||
fetch("/?preview=" + encodeURIComponent(file)) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw "Failed to preview file: " + response.statusText | ||
} | ||
|
||
return response.text() | ||
}) | ||
.then(content => { | ||
showFileModal(href, filename, content, shareName); | ||
document.querySelectorAll('pre code').forEach((el) => { | ||
hljs.highlightElement(el); | ||
}); | ||
}) | ||
} | ||
|
||
function showFileModal(href, filename, content, shareName) { | ||
// clone template | ||
const clone = template.content.cloneNode(true) | ||
|
||
// fill out | ||
clone.querySelector("h2").innerText = filename | ||
clone.querySelector("div").innerHTML = content | ||
|
||
clone.querySelector("a").href = target.href | ||
clone.querySelector("a").href = href | ||
|
||
// add into modal and show it | ||
document.querySelector(".modal-body").innerHTML = clone | ||
document.querySelector(".modal-body").innerHTML = "" | ||
document.querySelector(".modal-body").appendChild(clone) | ||
document.getElementById("modal").style.display = "block" | ||
|
||
document.getElementById("share").addEventListener("click", _ => { | ||
const nodes = document.querySelectorAll(".path > a") | ||
const dir = nodes[nodes.length - 1] | ||
Comment on lines
+70
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surely there's a better way to get the directory, like from the URL |
||
const url = `${window.location.origin}${window.location.pathname}?dir=${dir.innerHTML}&share=${shareName}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. URL encode |
||
if (navigator.share) | ||
{ | ||
navigator.share({url: url}); | ||
} | ||
else | ||
{ | ||
window.prompt("Copy to share", url) | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems redundant, we already have the name and url encoding it should be done by the link builder.