debian-mirror-gitlab/app/assets/javascripts/blob/components/table_contents.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
1.9 KiB
Vue
Raw Normal View History

2021-09-04 01:27:46 +05:30
<script>
2023-04-23 21:23:45 +05:30
import { GlDisclosureDropdown } from '@gitlab/ui';
2021-09-04 01:27:46 +05:30
function getHeaderNumber(el) {
return parseInt(el.tagName.match(/\d+/)[0], 10);
}
export default {
components: {
2023-04-23 21:23:45 +05:30
GlDisclosureDropdown,
2021-09-04 01:27:46 +05:30
},
data() {
return {
isHidden: false,
items: [],
};
},
mounted() {
this.blobViewer = document.querySelector('.blob-viewer[data-type="rich"]');
2021-12-11 22:18:48 +05:30
const blobViewerAttr = (attr) => this.blobViewer.getAttribute(attr);
2021-09-04 01:27:46 +05:30
this.observer = new MutationObserver(() => {
2021-12-11 22:18:48 +05:30
if (this.blobViewer.classList.contains('hidden') || blobViewerAttr('data-type') !== 'rich') {
2021-09-04 01:27:46 +05:30
this.isHidden = true;
2021-12-11 22:18:48 +05:30
} else if (blobViewerAttr('data-loaded') === 'true') {
2021-09-04 01:27:46 +05:30
this.isHidden = false;
this.generateHeaders();
2022-11-25 23:54:43 +05:30
this.observer.disconnect();
2021-09-04 01:27:46 +05:30
}
});
if (this.blobViewer) {
this.observer.observe(this.blobViewer, {
attributes: true,
});
}
},
beforeDestroy() {
if (this.observer) {
this.observer.disconnect();
}
},
methods: {
2023-04-23 21:23:45 +05:30
close() {
this.$refs.disclosureDropdown?.close();
},
2021-09-04 01:27:46 +05:30
generateHeaders() {
2023-04-23 21:23:45 +05:30
const BASE_PADDING = 16;
2021-09-04 01:27:46 +05:30
const headers = [...this.blobViewer.querySelectorAll('h1,h2,h3,h4,h5,h6')];
2023-04-23 21:23:45 +05:30
if (headers.length === 0) {
return;
2021-09-04 01:27:46 +05:30
}
2023-04-23 21:23:45 +05:30
const firstHeader = getHeaderNumber(headers[0]);
this.items = headers.map((el) => ({
text: el.textContent.trim(),
href: `#${el.querySelector('a').getAttribute('id')}`,
extraAttrs: {
style: {
paddingLeft: `${BASE_PADDING + Math.max((getHeaderNumber(el) - firstHeader) * 8, 0)}px`,
},
},
}));
2021-09-04 01:27:46 +05:30
},
},
};
</script>
<template>
2023-04-23 21:23:45 +05:30
<gl-disclosure-dropdown
v-if="!isHidden && items.length"
ref="disclosureDropdown"
icon="list-bulleted"
class="gl-mr-2"
:items="items"
@action="close"
/>
2021-09-04 01:27:46 +05:30
</template>