-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcode-ref.gts
81 lines (76 loc) · 2.08 KB
/
code-ref.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
import {
Component,
primitive,
serialize,
deserialize,
formatQuery,
queryableValue,
CardDef,
BaseDefConstructor,
BaseInstanceType,
FieldDef,
relativeTo,
type SerializeOpts,
type JSONAPISingleResourceDocument,
} from './card-api';
import { ResolvedCodeRef } from '@cardstack/runtime-common';
import CodeIcon from '@cardstack/boxel-icons/code';
class BaseView extends Component<typeof CodeRefField> {
<template>
<div data-test-ref>
Module:
{{@model.module}}
Name:
{{@model.name}}
</div>
</template>
}
export default class CodeRefField extends FieldDef {
static icon = CodeIcon;
static [primitive]: ResolvedCodeRef;
static [serialize](
codeRef: ResolvedCodeRef,
_doc: JSONAPISingleResourceDocument,
_visited?: Set<string>,
opts?: SerializeOpts,
) {
return {
...codeRef,
...(opts?.maybeRelativeURL && !opts?.useAbsoluteURL
? { module: opts.maybeRelativeURL(codeRef.module) }
: {}),
};
}
static async [deserialize]<T extends BaseDefConstructor>(
this: T,
codeRef: ResolvedCodeRef,
): Promise<BaseInstanceType<T>> {
return { ...codeRef } as BaseInstanceType<T>; // return a new object so that the model cannot be mutated from the outside
}
static [queryableValue](
codeRef: ResolvedCodeRef | undefined,
stack: CardDef[] = [],
) {
return maybeSerializeCodeRef(codeRef, stack);
}
static [formatQuery](codeRef: ResolvedCodeRef) {
return maybeSerializeCodeRef(codeRef);
}
static embedded = class Embedded extends BaseView {};
// The edit template is meant to be read-only, this field card is not mutable
static edit = class Edit extends BaseView {};
}
function maybeSerializeCodeRef(
codeRef: ResolvedCodeRef | undefined,
stack: CardDef[] = [],
) {
if (codeRef) {
// if a stack is passed in, use the containing card to resolve relative references
let moduleHref =
stack.length > 0
? new URL(codeRef.module, stack[0][relativeTo]).href
: codeRef.module;
return `${moduleHref}/${codeRef.name}`;
}
return undefined;
}