]> BookStack Code Mirror - bookstack/blob - resources/js/components/attachments-list.js
Comments: Fixed display, added archive list support for editor toolbox
[bookstack] / resources / js / components / attachments-list.js
1 import {Component} from './component';
2
3 /**
4  * Attachments List
5  * Adds '?open=true' query to file attachment links
6  * when ctrl/cmd is pressed down.
7  */
8 export class AttachmentsList extends Component {
9
10     setup() {
11         this.container = this.$el;
12         this.fileLinks = this.$manyRefs.linkTypeFile;
13
14         this.setupListeners();
15     }
16
17     setupListeners() {
18         const isExpectedKey = event => event.key === 'Control' || event.key === 'Meta';
19         window.addEventListener('keydown', event => {
20             if (isExpectedKey(event)) {
21                 this.addOpenQueryToLinks();
22             }
23         }, {passive: true});
24         window.addEventListener('keyup', event => {
25             if (isExpectedKey(event)) {
26                 this.removeOpenQueryFromLinks();
27             }
28         }, {passive: true});
29     }
30
31     addOpenQueryToLinks() {
32         for (const link of this.fileLinks) {
33             if (link.href.split('?')[1] !== 'open=true') {
34                 link.href += '?open=true';
35                 link.setAttribute('target', '_blank');
36             }
37         }
38     }
39
40     removeOpenQueryFromLinks() {
41         for (const link of this.fileLinks) {
42             link.href = link.href.split('?')[0];
43             link.removeAttribute('target');
44         }
45     }
46
47 }