forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollapsibleCodeListing.vue
146 lines (129 loc) · 3.15 KB
/
CollapsibleCodeListing.vue
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!--
This source file is part of the Swift.org open source project
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
-->
<template>
<div
class="collapsible-code-listing"
:class="{ 'single-line': content[0].code.length === 1 }"
>
<!-- Do not add newlines in <pre>, as they'll appear in the rendered HTML. -->
<pre><CodeBlock><div
v-for="(content, index) in this.content"
:class="['container-general',{ collapsible: content.collapsible === true },
{ collapsed: content.collapsible === true && collapsed }]"
:key="index"
><div
v-for="(line, index) in content.code"
class="code-line-container"
:key="index"
>
<div v-show="showLineNumbers" class="code-number" />
<div class="code-line">{{ line }}</div>
</div></div></CodeBlock></pre>
</div>
</template>
<script>
import CodeBlock from 'docc-render/components/CodeBlock.vue';
export default {
name: 'CollapsibleCodeListing',
components: {
CodeBlock,
},
props: {
collapsed: {
type: Boolean,
required: false,
},
content: {
type: Array,
required: true,
},
showLineNumbers: {
type: Boolean,
default: () => true,
},
},
};
</script>
<style scoped lang="scss">
@import "docc-render/styles/_core.scss";
.container-general {
display: flex;
flex-flow: row wrap;
.code-line {
flex: 1 0 auto;
}
}
.code-line-container {
// ensure the code is at least as wide as the parent
width: 100%;
align-items: center;
display: flex;
border-left: 4px solid transparent;
counter-increment: linenumbers;
padding-right: 14px;
}
.code-number {
@include font-styles(documentation-code-listing-number);
padding: $code-number-padding;
text-align: right;
min-width: 2.01em;
user-select: none;
&::before {
content: counter(linenumbers);
}
}
.code-line {
display: flex;
}
pre {
padding: $code-listing-with-numbers-padding;
display: flex;
// allow items to wrap, because they fill 100% width
flex-flow: row wrap;
overflow: auto;
-webkit-overflow-scrolling: touch;
white-space: pre;
word-wrap: normal;
@include breakpoint(small) {
padding-top: rem(14px);
}
}
.collapsible-code-listing {
background: var(--background, var(--color-code-background));
border-color: var(--colors-grid, var(--color-grid));
color: var(--text, var(--color-code-plain));
border-radius: $border-radius;
border-style: solid;
border-width: 1px;
counter-reset: linenumbers;
font-size: 15px;
&.single-line {
border-radius: $large-border-radius;
}
}
.collapsible {
background: var(--color-code-collapsible-background);
color: var(--color-code-collapsible-text);
}
.collapsed {
&::before {
content: "⋯";
display: inline-block;
font-family: monospace;
font-weight: $font-weight-bold;
height: 100%;
line-height: 1;
text-align: right;
width: 2.3rem;
}
.code-line-container {
height: 0;
visibility: hidden;
}
}
</style>