forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodePreview.vue
348 lines (312 loc) · 8.77 KB
/
CodePreview.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!--
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="['code-preview', { ide: isTargetIDE }]">
<CodeTheme>
<CodeListing v-if="code" v-bind="codeProps" showLineNumbers />
</CodeTheme>
<div
class="runtime-preview"
:class="runtimePreviewClasses"
:style="previewStyles"
>
<div class="runtimve-preview__container">
<button
class="header"
:disabled="!hasRuntimePreview"
:title="runtimePreviewTitle"
@click="togglePreview"
>
<span
class="runtime-preview-label"
:aria-label="textAriaLabel"
>{{ togglePreviewText }}</span>
<DiagonalArrowIcon
:class="[ shouldDisplayHideLabel ? 'preview-hide': 'preview-show' ]"
class="icon-inline preview-icon"
/>
</button>
<transition @leave="handleLeave">
<div class="runtime-preview-asset" v-show="shouldDisplayHideLabel">
<slot />
</div>
</transition>
</div>
</div>
</div>
</template>
<script>
import CodeListing from 'docc-render/components/ContentNode/CodeListing.vue';
import BreakpointEmitter from 'docc-render/components/BreakpointEmitter.vue';
import DiagonalArrowIcon from 'theme/components/Icons/DiagonalArrowIcon.vue';
import CodeTheme from './CodeTheme.vue';
const { BreakpointName } = BreakpointEmitter.constants;
function scaledSize({ width, height }, scale = 1) {
// For images with a width less than or equal to 400px (e.g., watch),
// scale less so that the image is not too small
const smallImageWidth = 400;
const imageScale = width <= smallImageWidth ? 1.75 : 3;
return ({
width: width / (imageScale / scale),
height: height / (imageScale / scale),
});
}
export default {
name: 'CodePreview',
inject: {
isTargetIDE: {
default: false,
},
store: {
default() {
return {
state: {
references: {},
},
};
},
},
},
components: {
DiagonalArrowIcon,
CodeListing,
CodeTheme,
},
props: {
code: {
type: String,
required: true,
},
preview: {
type: String,
required: false,
},
isRuntimePreviewVisible: {
type: Boolean,
required: true,
},
},
data() {
return {
tutorialState: this.store.state,
};
},
computed: {
references: ({ tutorialState }) => tutorialState.references,
currentBreakpoint() {
return this.tutorialState.breakpoint;
},
hasRuntimePreview() {
return !!this.preview;
},
previewAssetSize() {
// All the variants should have the same size, so use the size of the first
// variant. (same method in ImageAsset.vue)
const asset = this.hasRuntimePreview ? this.references[this.preview] : {};
const variants = (asset.variants || [{}])[0] || {};
// In case variants don't have a size, it will fallback to a default size.
const fallbackSize = {
/**
* The value of this fallback size is necessary since
* videos are not provided with size attributes.
* The width result ends up being 300px
* because all sizes bigger than 400 get scaled down by 3 on the `scaledSize` function.
*/
width: 900,
};
let assetSize = variants.size || {};
if (!assetSize.width && !assetSize.height) {
assetSize = fallbackSize;
}
// Scale down images by 80% for medium breakpoint
const scale = this.currentBreakpoint === BreakpointName.medium ? 0.8 : 1;
return scaledSize(assetSize, scale);
},
previewSize() {
const collapsedPreviewSize = {
width: 102,
};
return (this.shouldDisplayHideLabel && this.previewAssetSize) ? ({
width: this.previewAssetSize.width,
}) : (
collapsedPreviewSize
);
},
previewStyles() {
const {
width,
} = this.previewSize;
return {
width: `${width}px`,
};
},
codeProps() {
return this.references[this.code];
},
runtimePreviewClasses() {
return {
collapsed: !this.shouldDisplayHideLabel,
disabled: !this.hasRuntimePreview,
'runtime-preview-ide': this.isTargetIDE,
};
},
shouldDisplayHideLabel() {
return this.hasRuntimePreview && this.isRuntimePreviewVisible;
},
runtimePreviewTitle() {
return this.hasRuntimePreview ? null : this.$t('tutorials.preview.no-preview-available-step');
},
togglePreviewText() {
return this.$tc('tutorials.preview.title', this.hasRuntimePreview ? 1 : 0);
},
textAriaLabel() {
return `${this.togglePreviewText}, ${
this.shouldDisplayHideLabel
? this.$t('verbs.hide')
: this.$t('verbs.show')
}`;
},
},
methods: {
/**
* Delay the hiding of the asset by 200ms.
* This is in sync with the preview transition.
* Its better to rely on the prop changing,
* than on the browser transition event to fire.
* Invisible elements do not fire transition events.
* @param {HTMLElement} el
* @param {function} done - callback to tell Vue we want to hide the component
*/
handleLeave(el, done) {
setTimeout(done, 200);
},
togglePreview() {
if (!this.hasRuntimePreview) {
return;
}
this.$emit('runtime-preview-toggle', !this.isRuntimePreviewVisible);
},
},
};
</script>
<style scoped lang="scss">
@import 'docc-render/styles/_core.scss';
$preview-margin: 1rem;
$runtime-preview-transition-curve: ease-in !default;
$duration: 0.2s;
.code-preview {
position: sticky;
@include overflow-y;
background-color: var(--background, var(--color-step-background));
$height-ide: 100vh;
height: calc(#{$height-ide} - #{$nav-height});
&.ide {
height: $height-ide;
}
:deep(.code-listing) {
color: var(--text, var(--color-code-plain));
.code-line-container {
// add extra padding, so we have extra space on right when scrolling is needed
padding-right: 14px;
}
}
:deep(pre) {
@include font-styles(code-preview);
}
}
.header {
@include font-styles(caption);
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
width: stretch;
cursor: pointer;
font-weight: $font-weight-semibold;
padding: 8px 12px;
border-radius: $border-radius $border-radius 0 0;
z-index: 1;
background: var(--color-runtime-preview-background);
color: var(--colors-runtime-preview-text, var(--color-runtime-preview-text));
&:focus {
outline-style: none;
}
#app.fromkeyboard &:focus {
@include focus-shadow-form-element;
}
}
.runtime-preview {
--color-runtime-preview-shadow: rgba(0, 0, 0, 0.4);
@include prefers-dark {
--color-runtime-preview-shadow: rgba(255, 255, 255, 0.4);
}
position: absolute;
top: 0;
right: 0;
background: var(--color-runtime-preview-background);
border-radius: $border-radius;
margin: $preview-margin;
margin-left: 0;
// For animation
// Width and height must have a pixel value in order to be transitioned
transition: width $duration $runtime-preview-transition-curve;
// fallback to correctly stretch the toggle button for older browsers
@supports not (width: stretch) {
display: flex;
flex-direction: column;
}
.runtimve-preview__container {
border-radius: $border-radius;
overflow: hidden;
}
box-shadow: 0px 0px 3px 0px var(--color-runtime-preview-shadow);
}
.runtime-preview-ide {
top: 0;
.runtime-preview-asset {
:deep(img) {
background-color: var(--color-runtime-preview-background);
}
}
}
.runtime-preview.collapsed {
box-shadow: 0px 0px 3px 0px var(--color-runtime-preview-shadow);
// Set for a specific size of "Preview ↙".
// If this changes in the future, these dimensions or the approach will
// need to be rethought.
// Potentially a JS method that can create the proper dimensions
// based on text.
width: 102px;
.header {
border-radius: $border-radius;
}
}
.runtime-preview.disabled {
box-shadow: 0px 0px 3px 0px transparent;
.header {
color: var(--color-runtime-preview-disabled-text);
cursor: auto;
}
}
.runtime-preview-asset {
border-radius: 0 0 $border-radius $border-radius;
:deep(img) {
border-bottom-left-radius: $border-radius;
border-bottom-right-radius: $border-radius;
}
}
.preview-icon {
height: 0.8em;
width: 0.8em;
user-select: none;
}
// mirror the arrow icon to point down
.preview-show {
transform: scale(-1);
}
</style>