-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathspec.gts
423 lines (400 loc) · 12.4 KB
/
spec.gts
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import {
contains,
field,
Component,
CardDef,
relativeTo,
linksToMany,
FieldDef,
containsMany,
getCardMeta,
type CardorFieldTypeIcon,
} from './card-api';
import StringField from './string';
import BooleanField from './boolean';
import CodeRef from './code-ref';
import MarkdownField from './markdown';
import { restartableTask } from 'ember-concurrency';
import {
FieldContainer,
LoadingIndicator,
Pill,
RealmIcon,
} from '@cardstack/boxel-ui/components';
import {
codeRefWithAbsoluteURL,
Loader,
loadCard,
isResolvedCodeRef,
} from '@cardstack/runtime-common';
import { eq } from '@cardstack/boxel-ui/helpers';
import GlimmerComponent from '@glimmer/component';
import BoxModel from '@cardstack/boxel-icons/box-model';
import BookOpenText from '@cardstack/boxel-icons/book-open-text';
import LayersSubtract from '@cardstack/boxel-icons/layers-subtract';
import GitBranch from '@cardstack/boxel-icons/git-branch';
import { DiagonalArrowLeftUp as ExportArrow } from '@cardstack/boxel-ui/icons';
import StackIcon from '@cardstack/boxel-icons/stack';
import AppsIcon from '@cardstack/boxel-icons/apps';
import LayoutList from '@cardstack/boxel-icons/layout-list';
import Brain from '@cardstack/boxel-icons/brain';
export type SpecType = 'card' | 'field' | 'app' | 'skill';
class SpecTypeField extends StringField {
static displayName = 'Spec Type';
}
export class Spec extends CardDef {
static displayName = 'Spec';
static icon = BoxModel;
@field name = contains(StringField);
@field readMe = contains(MarkdownField);
@field ref = contains(CodeRef);
@field specType = contains(SpecTypeField);
@field isField = contains(BooleanField, {
computeVia: function (this: Spec) {
return this.specType === 'field';
},
});
@field isCard = contains(BooleanField, {
computeVia: function (this: Spec) {
return (
this.specType === 'card' ||
this.specType === 'app' ||
this.specType === 'skill'
);
},
});
@field moduleHref = contains(StringField, {
computeVia: function (this: Spec) {
return new URL(this.ref.module, this[relativeTo]).href;
},
});
@field linkedExamples = linksToMany(CardDef);
@field containedExamples = containsMany(FieldDef, { isUsed: true });
@field title = contains(StringField, {
computeVia: function (this: Spec) {
if (this.name) {
return this.name;
}
return this.ref.name === 'default' ? undefined : this.ref.name;
},
});
static isolated = class Isolated extends Component<typeof this> {
icon: CardorFieldTypeIcon | undefined;
get defaultIcon() {
return this.args.model.constructor?.icon;
}
constructor(owner: any, args: any) {
super(owner, args);
this.loadCardIcon.perform();
}
private loadCardIcon = restartableTask(async () => {
if (this.args.model.ref && this.args.model.id) {
let card = await loadCard(this.args.model.ref, {
loader: myLoader(),
relativeTo: new URL(this.args.model.id),
});
this.icon = card.icon;
}
});
get absoluteRef() {
if (!this.args.model.ref || !this.args.model.id) {
return undefined;
}
let url = new URL(this.args.model.id);
let ref = codeRefWithAbsoluteURL(this.args.model.ref, url);
if (!isResolvedCodeRef(ref)) {
throw new Error('ref is not a resolved code ref');
}
return ref;
}
private get realmInfo() {
return getCardMeta(this.args.model as CardDef, 'realmInfo');
}
<template>
<article class='container'>
<header class='header' aria-labelledby='title'>
<div class='box header-icon-container'>
{{#if this.loadCardIcon.isRunning}}
<LoadingIndicator />
{{else if this.icon}}
<this.icon width='35' height='35' role='presentation' />
{{else}}
<this.defaultIcon width='35' height='35' role='presentation' />
{{/if}}
</div>
<div class='header-info-container'>
<h1 class='title' id='title' data-test-title><@fields.title /></h1>
<p class='description' data-test-description>
<@fields.description />
</p>
</div>
</header>
<section class='readme section'>
<header class='row-header' aria-labelledby='readme'>
<BookOpenText width='20' height='20' role='presentation' />
<h2 id='readme'>Read Me</h2>
</header>
<@fields.readMe />
</section>
<section class='examples section'>
<header class='row-header' aria-labelledby='examples'>
<LayersSubtract width='20' height='20' role='presentation' />
<h2 id='examples'>Examples</h2>
</header>
{{#if (eq @model.specType 'field')}}
<@fields.containedExamples />
{{else}}
<@fields.linkedExamples @subclassType={{this.absoluteRef}} />
{{/if}}
</section>
<section class='module section'>
<header class='row-header' aria-labelledby='module'>
<GitBranch width='20' height='20' role='presentation' />
<h2 id='module'>Module</h2>
</header>
<div class='code-ref-container'>
<FieldContainer @label='URL' @vertical={{true}}>
<div class='code-ref-row'>
<RealmIcon class='realm-icon' @realmInfo={{this.realmInfo}} />
<span class='code-ref-value' data-test-module-href>
{{@model.moduleHref}}
</span>
</div>
</FieldContainer>
<FieldContainer @label='Module Name' @vertical={{true}}>
<div class='code-ref-row'>
<ExportArrow class='exported-arrow' width='10' height='10' />
<div class='code-ref-value' data-test-exported-name>
{{@model.ref.name}}
</div>
<div class='exported-type' data-test-exported-type>
{{@model.specType}}
</div>
</div>
</FieldContainer>
</div>
</section>
</article>
<style scoped>
.container {
--boxel-spec-background-color: #ebeaed;
--boxel-spec-code-ref-background-color: #e2e2e2;
--boxel-spec-code-ref-text-color: #646464;
height: 100%;
min-height: max-content;
padding: var(--boxel-sp);
background-color: var(--boxel-spec-background-color);
}
.section {
margin-top: var(--boxel-sp);
padding-top: var(--boxel-sp);
border-top: 1px solid var(--boxel-400);
}
h1 {
margin: 0;
font-size: 18px;
font-weight: 600;
letter-spacing: var(--boxel-lsp-xs);
line-height: 1.2;
}
h2 {
margin: 0;
font: 600 var(--boxel-font-sm);
letter-spacing: var(--boxel-lsp-xs);
}
p {
margin-top: var(--boxel-sp-4xs);
margin-bottom: 0;
}
.title,
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
text-wrap: pretty;
}
.box {
border: 1px solid var(--boxel-border-color);
border-radius: var(--boxel-border-radius-lg);
background-color: var(--boxel-light);
}
.header {
display: flex;
gap: var(--boxel-sp-sm);
}
.header-icon-container {
flex-shrink: 0;
height: var(--boxel-icon-xxl);
width: var(--boxel-icon-xxl);
display: flex;
align-items: center;
justify-content: center;
background-color: var(--boxel-100);
}
.header-info-container {
flex: 1;
align-self: center;
}
.row-header {
display: flex;
align-items: center;
gap: var(--boxel-sp-xs);
padding-bottom: var(--boxel-sp-lg);
}
.row-content {
margin-top: var(--boxel-sp-sm);
}
/* code ref container styles */
.code-ref-container {
display: flex;
flex-direction: column;
gap: var(--boxel-sp-xs);
}
.code-ref-row {
display: flex;
align-items: center;
gap: var(--boxel-sp-xs);
min-height: var(--boxel-form-control-height);
padding: var(--boxel-sp-xs);
background-color: var(
--boxel-spec-code-ref-background-color,
var(--boxel-100)
);
border: var(--boxel-border);
border-radius: var(--boxel-border-radius);
color: var(--boxel-spec-code-ref-text-color, var(--boxel-450));
}
.code-ref-value {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.exported-type {
margin-left: auto;
color: var(--boxel-450);
font: 500 var(--boxel-font-xs);
letter-spacing: var(--boxel-lsp);
text-transform: uppercase;
}
.exported-arrow {
min-width: 8px;
min-height: 8px;
}
.realm-icon {
width: 18px;
height: 18px;
border: 1px solid var(--boxel-dark);
}
</style>
</template>
};
static embedded = class Embedded extends Component<typeof this> {
get icon() {
return this.args.model.constructor?.icon;
}
<template>
<article class='embedded-spec'>
<div class='header-icon-container'>
<this.icon width='30' height='30' role='presentation' />
</div>
<div class='header-info-container'>
<h3 class='title'><@fields.title /></h3>
<p class='description'><@fields.description /></p>
</div>
{{#if @model.specType}}
<SpecTag @specType={{@model.specType}} />
{{/if}}
</article>
<style scoped>
.embedded-spec {
display: flex;
align-items: center;
gap: var(--boxel-sp-sm);
padding: var(--boxel-sp-xs);
}
.header-icon-container {
flex-shrink: 0;
height: var(--boxel-icon-xl);
width: var(--boxel-icon-xl);
display: flex;
align-items: center;
justify-content: center;
background-color: var(--boxel-100);
border: 1px solid var(--boxel-border-color);
border-radius: var(--boxel-border-radius-lg);
background-color: var(--boxel-light);
}
.header-info-container {
flex: 1;
}
.title {
margin: 0;
font: 600 var(--boxel-font-sm);
letter-spacing: var(--boxel-lsp-xs);
}
.description {
margin: 0;
color: var(--boxel-500);
font: var(--boxel-font-size-xs);
letter-spacing: var(--boxel-lsp-xs);
}
</style>
</template>
};
static edit = Spec.isolated;
}
interface SpecTagSignature {
Element: HTMLDivElement;
Args: {
specType: string;
};
}
export class SpecTag extends GlimmerComponent<SpecTagSignature> {
get icon() {
return getIcon(this.args.specType);
}
<template>
{{#if this.icon}}
<Pill class='spec-tag-pill' ...attributes>
<:iconLeft>
{{this.icon}}
</:iconLeft>
<:default>
{{@specType}}
</:default>
</Pill>
{{/if}}
<style scoped>
.spec-tag-pill {
--pill-font: 500 var(--boxel-font-xs);
--pill-background-color: var(--boxel-200);
word-break: initial;
text-transform: uppercase;
}
</style>
</template>
}
function getIcon(specType: string) {
switch (specType) {
case 'card':
return StackIcon;
case 'app':
return AppsIcon;
case 'field':
return LayoutList;
case 'skill':
return Brain;
default:
return;
}
}
function myLoader(): Loader {
// we know this code is always loaded by an instance of our Loader, which sets
// import.meta.loader.
// When type-checking realm-server, tsc sees this file and thinks
// it will be transpiled to CommonJS and so it complains about this line. But
// this file is always loaded through our loader and always has access to import.meta.
// @ts-ignore
return (import.meta as any).loader;
}