BookStack/resources/js/components/attachments-list.js
Dan Brown 09c6a3c240
Started refactor and alignment of component system
- Updates old components to newer format, removes legacy component
support.
- Makes component registration easier and less duplicated.
- Adds base component class to extend for better editor support.
- Aligns global window exposure usage and aligns with other service
  names.
2022-11-14 23:19:02 +00:00

48 lines
1.4 KiB
JavaScript

import {Component} from "./component";
/**
* Attachments List
* Adds '?open=true' query to file attachment links
* when ctrl/cmd is pressed down.
*/
export class AttachmentsList extends Component {
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;