debian-mirror-gitlab/app/assets/javascripts/vue_shared/components/source_viewer.vue

124 lines
3.1 KiB
Vue
Raw Normal View History

2022-01-26 12:08:38 +05:30
<script>
import { GlSafeHtmlDirective } from '@gitlab/ui';
import LineNumbers from '~/vue_shared/components/line_numbers.vue';
2022-03-02 08:16:31 +05:30
import { sanitize } from '~/lib/dompurify';
const LINE_SELECT_CLASS_NAME = 'hll';
2022-01-26 12:08:38 +05:30
export default {
components: {
LineNumbers,
},
directives: {
SafeHtml: GlSafeHtmlDirective,
},
props: {
content: {
type: String,
required: true,
},
language: {
type: String,
required: false,
default: 'plaintext',
},
autoDetect: {
type: Boolean,
required: false,
default: false,
},
},
data() {
return {
languageDefinition: null,
hljs: null,
};
},
computed: {
lineNumbers() {
return this.content.split('\n').length;
},
highlightedContent() {
let highlightedContent;
if (this.hljs) {
if (this.autoDetect) {
highlightedContent = this.hljs.highlightAuto(this.content).value;
} else if (this.languageDefinition) {
highlightedContent = this.hljs.highlight(this.content, { language: this.language }).value;
}
}
2022-03-02 08:16:31 +05:30
return this.wrapLines(highlightedContent);
},
},
watch: {
highlightedContent() {
this.$nextTick(() => this.selectLine());
},
$route() {
this.selectLine();
2022-01-26 12:08:38 +05:30
},
},
async mounted() {
this.hljs = await this.loadHighlightJS();
if (!this.autoDetect) {
this.languageDefinition = await this.loadLanguage();
}
},
methods: {
loadHighlightJS() {
// With auto-detect enabled we load all common languages else we load only the core (smallest footprint)
return this.autoDetect ? import('highlight.js/lib/common') : import('highlight.js/lib/core');
},
async loadLanguage() {
let languageDefinition;
try {
languageDefinition = await import(`highlight.js/lib/languages/${this.language}`);
this.hljs.registerLanguage(this.language, languageDefinition.default);
} catch (message) {
this.$emit('error', message);
}
return languageDefinition;
},
2022-03-02 08:16:31 +05:30
wrapLines(content) {
return (
content &&
content
.split('\n')
.map((line, i) => `<span id="LC${i + 1}" class="line">${line}</span>`)
.join('\r\n')
);
},
selectLine() {
const hash = sanitize(this.$route.hash);
const lineToSelect = hash && this.$el.querySelector(hash);
if (!lineToSelect) {
return;
}
if (this.$options.currentlySelectedLine) {
this.$options.currentlySelectedLine.classList.remove(LINE_SELECT_CLASS_NAME);
}
lineToSelect.classList.add(LINE_SELECT_CLASS_NAME);
this.$options.currentlySelectedLine = lineToSelect;
lineToSelect.scrollIntoView({ behavior: 'smooth', block: 'center' });
},
2022-01-26 12:08:38 +05:30
},
userColorScheme: window.gon.user_color_scheme,
2022-03-02 08:16:31 +05:30
currentlySelectedLine: null,
2022-01-26 12:08:38 +05:30
};
</script>
<template>
2022-03-02 08:16:31 +05:30
<div class="file-content code js-syntax-highlight" :class="$options.userColorScheme">
2022-01-26 12:08:38 +05:30
<line-numbers :lines="lineNumbers" />
2022-03-02 08:16:31 +05:30
<pre class="code"><code v-safe-html="highlightedContent"></code>
2022-01-26 12:08:38 +05:30
</pre>
</div>
</template>