summaryrefslogtreecommitdiff
path: root/packages/frontend/src/components/MkCode.vue
blob: 2c016e4d7cf1dd4d363ac58441dc97cc1d26ed0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<Suspense>
	<template #fallback>
		<MkLoading v-if="!inline ?? true"/>
	</template>
	<code v-if="inline" :class="$style.codeInlineRoot">{{ code }}</code>
	<XCode v-else-if="show && lang" :code="code" :lang="lang"/>
	<pre v-else-if="show" :class="$style.codeBlockFallbackRoot"><code :class="$style.codeBlockFallbackCode">{{ code }}</code></pre>
	<button v-else :class="$style.codePlaceholderRoot" @click="show = true">
		<div :class="$style.codePlaceholderContainer">
			<div><i class="ti ti-code"></i> {{ i18n.ts.code }}</div>
			<div>{{ i18n.ts.clickToShow }}</div>
		</div>
	</button>
</Suspense>
</template>

<script lang="ts" setup>
import { defineAsyncComponent, ref } from 'vue';
import MkLoading from '@/components/global/MkLoading.vue';
import { defaultStore } from '@/store.js';
import { i18n } from '@/i18n.js';

defineProps<{
	code: string;
	lang?: string;
	inline?: boolean;
}>();

const show = ref(!defaultStore.state.dataSaver.code);

const XCode = defineAsyncComponent(() => import('@/components/MkCode.core.vue'));
</script>

<style module lang="scss">
.codeInlineRoot {
	display: inline-block;
	font-family: Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
	overflow-wrap: anywhere;
	color: #D4D4D4;
	background: #1E1E1E;
	padding: .1em;
	border-radius: .3em;
}

.codeBlockFallbackRoot {
	display: block;
	overflow-wrap: anywhere;
	color: #D4D4D4;
	background: #1E1E1E;
	padding: 1em;
	margin: .5em 0;
	overflow: auto;
	border-radius: 8px;
}

.codeBlockFallbackCode {
	font-family: Consolas, Monaco, Andale Mono, Ubuntu Mono, monospace;
}

.codePlaceholderRoot {
	display: block;
	width: 100%;
	background: none;
	border: none;
	outline: none;
  font: inherit;
  color: inherit;
	cursor: pointer;

	box-sizing: border-box;
	border-radius: 8px;
	padding: 24px;
	margin-top: 4px;
	color: #D4D4D4;
	background: #1E1E1E;
}

.codePlaceholderContainer {
	text-align: center;
	font-size: 0.8em;
}
</style>