mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-28 17:02:04 +03:00
Merge pull request #2791 from BookStackApp/attachments_open_in_browser
Attachment serving without forced download
This commit is contained in:
47
resources/js/components/attachments-list.js
Normal file
47
resources/js/components/attachments-list.js
Normal file
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Attachments List
|
||||
* Adds '?open=true' query to file attachment links
|
||||
* when ctrl/cmd is pressed down.
|
||||
* @extends {Component}
|
||||
*/
|
||||
class AttachmentsList {
|
||||
|
||||
setup() {
|
||||
this.container = this.$el;
|
||||
this.setupListeners();
|
||||
}
|
||||
|
||||
setupListeners() {
|
||||
const isExpectedKey = (event) => event.key === 'Control' || event.key === 'Meta';
|
||||
window.addEventListener('keydown', event => {
|
||||
if (isExpectedKey(event)) {
|
||||
this.addOpenQueryToLinks();
|
||||
}
|
||||
}, {passive: true});
|
||||
window.addEventListener('keyup', event => {
|
||||
if (isExpectedKey(event)) {
|
||||
this.removeOpenQueryFromLinks();
|
||||
}
|
||||
}, {passive: true});
|
||||
}
|
||||
|
||||
addOpenQueryToLinks() {
|
||||
const links = this.container.querySelectorAll('a.attachment-file');
|
||||
for (const link of links) {
|
||||
if (link.href.split('?')[1] !== 'open=true') {
|
||||
link.href = link.href + '?open=true';
|
||||
link.setAttribute('target', '_blank');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
removeOpenQueryFromLinks() {
|
||||
const links = this.container.querySelectorAll('a.attachment-file');
|
||||
for (const link of links) {
|
||||
link.href = link.href.split('?')[0];
|
||||
link.removeAttribute('target');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default AttachmentsList;
|
@ -2,6 +2,7 @@ import addRemoveRows from "./add-remove-rows.js"
|
||||
import ajaxDeleteRow from "./ajax-delete-row.js"
|
||||
import ajaxForm from "./ajax-form.js"
|
||||
import attachments from "./attachments.js"
|
||||
import attachmentsList from "./attachments-list.js"
|
||||
import autoSuggest from "./auto-suggest.js"
|
||||
import backToTop from "./back-to-top.js"
|
||||
import bookSort from "./book-sort.js"
|
||||
@ -56,6 +57,7 @@ const componentMapping = {
|
||||
"ajax-delete-row": ajaxDeleteRow,
|
||||
"ajax-form": ajaxForm,
|
||||
"attachments": attachments,
|
||||
"attachments-list": attachmentsList,
|
||||
"auto-suggest": autoSuggest,
|
||||
"back-to-top": backToTop,
|
||||
"book-sort": bookSort,
|
||||
|
@ -1,8 +1,10 @@
|
||||
@foreach($attachments as $attachment)
|
||||
<div class="attachment icon-list">
|
||||
<a class="icon-list-item py-xs" href="{{ $attachment->getUrl() }}" @if($attachment->external) target="_blank" @endif>
|
||||
<span class="icon">@icon($attachment->external ? 'export' : 'file')</span>
|
||||
<span>{{ $attachment->name }}</span>
|
||||
</a>
|
||||
</div>
|
||||
@endforeach
|
||||
<div component="attachments-list">
|
||||
@foreach($attachments as $attachment)
|
||||
<div class="attachment icon-list">
|
||||
<a class="icon-list-item py-xs attachment-{{ $attachment->external ? 'link' : 'file' }}" href="{{ $attachment->getUrl() }}" @if($attachment->external) target="_blank" @endif>
|
||||
<span class="icon">@icon($attachment->external ? 'export' : 'file')</span>
|
||||
<span>{{ $attachment->name }}</span>
|
||||
</a>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
Reference in New Issue
Block a user