-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscreenshot-card-demo.gts
More file actions
239 lines (214 loc) · 6.35 KB
/
Copy pathscreenshot-card-demo.gts
File metadata and controls
239 lines (214 loc) · 6.35 KB
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
import {
CardDef,
Component,
contains,
field,
linksToMany,
} from 'https://cardstack.com/base/card-api';
import StringField from 'https://cardstack.com/base/string';
import enumField from 'https://cardstack.com/base/enum';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { on } from '@ember/modifier';
import ScreenshotCardCommand from '@cardstack/boxel-host/commands/screenshot-card';
import { Button } from '@cardstack/boxel-ui/components';
type ScreenshotFormat = 'isolated' | 'embedded';
interface ScreenshotResult {
id: string;
title: string;
imageDefUrl?: string;
error?: string;
}
// Built-in enum field — atom view shows the current value as plain text;
// edit view renders a BoxelSelect dropdown of the configured options.
const FormatField = enumField(StringField, {
options: ['isolated', 'embedded'],
displayName: 'Screenshot Format',
});
class Isolated extends Component<typeof ScreenshotCardDemo> {
@tracked isRunning = false;
@tracked errorMessage: string | null = null;
@tracked results: ScreenshotResult[] = [];
@tracked processedCount = 0;
get hasCommandContext() {
return Boolean(this.args.context?.commandContext);
}
get linkedCards(): any[] {
let cards = (this.args.model as any)?.cards;
return Array.isArray(cards) ? cards.filter((c) => Boolean(c?.id)) : [];
}
get hasLinkedCards() {
return this.linkedCards.length > 0;
}
get isDisabled() {
return this.isRunning || !this.hasCommandContext || !this.hasLinkedCards;
}
get effectiveFormat(): ScreenshotFormat {
let raw = (this.args.model as any)?.format?.trim?.();
return raw === 'embedded' ? 'embedded' : 'isolated';
}
get buttonLabel() {
if (this.isRunning) {
return `Taking screenshots… ${this.processedCount}/${this.linkedCards.length}`;
}
let count = this.linkedCards.length;
return count > 1 ? `Take ${count} Screenshots` : 'Take Screenshot';
}
@action
async takeScreenshots() {
let commandContext = this.args.context?.commandContext;
let cards = this.linkedCards;
if (!commandContext) {
this.errorMessage =
'Command context is unavailable. Open this card in host interact mode.';
return;
}
if (!cards.length) {
this.errorMessage = 'Link at least one card before taking screenshots.';
return;
}
this.isRunning = true;
this.errorMessage = null;
this.results = [];
this.processedCount = 0;
let collected: ScreenshotResult[] = [];
for (let card of cards) {
let title = card?.title ?? card?.id ?? 'Untitled';
try {
let result = await new ScreenshotCardCommand(commandContext).execute({
card,
format: this.effectiveFormat,
});
collected = [
...collected,
{ id: card.id, title, imageDefUrl: result.imageDefUrl },
];
} catch (error) {
collected = [
...collected,
{
id: card?.id ?? title,
title,
error: error instanceof Error ? error.message : String(error),
},
];
}
this.processedCount = collected.length;
this.results = collected;
}
this.isRunning = false;
}
<template>
<article class='screenshot-card-demo'>
<header>
<h2>Screenshot Card Demo</h2>
<p>
Link one or more cards and pick a format, then capture a settled PNG
for each into its own realm under
<code>Screenshots/</code>.
</p>
</header>
<section class='field'>
<label>Cards to screenshot</label>
<@fields.cards />
</section>
<section class='field'>
<label>Format</label>
<@fields.format />
</section>
<section class='actions'>
<Button
data-test-take-screenshot
@disabled={{this.isDisabled}}
{{on 'click' this.takeScreenshots}}
>
{{this.buttonLabel}}
</Button>
</section>
{{#if this.results.length}}
<section class='results'>
{{#each this.results as |result|}}
<div class='result'>
<p class='result-title'>{{result.title}}</p>
{{#if result.imageDefUrl}}
<code class='url'>{{result.imageDefUrl}}</code>
<img src={{result.imageDefUrl}} alt='Card screenshot' />
{{else}}
<p class='status status--error'>{{result.error}}</p>
{{/if}}
</div>
{{/each}}
</section>
{{/if}}
{{#if this.errorMessage}}
<p class='status status--error'>{{this.errorMessage}}</p>
{{/if}}
</article>
<style scoped>
.screenshot-card-demo {
display: flex;
flex-direction: column;
gap: var(--boxel-sp-lg);
padding: var(--boxel-sp-lg);
}
header p {
margin: var(--boxel-sp-xs) 0 0;
color: var(--boxel-700);
}
.field {
display: flex;
flex-direction: column;
gap: var(--boxel-sp-xs);
}
.field label {
font-weight: 600;
}
.actions {
display: flex;
gap: var(--boxel-sp);
}
.results {
display: flex;
flex-direction: column;
gap: var(--boxel-sp);
}
.result {
display: flex;
flex-direction: column;
gap: var(--boxel-sp-xs);
padding: var(--boxel-sp);
border: 1px solid var(--boxel-200);
border-radius: var(--boxel-border-radius-lg);
background: var(--boxel-50);
}
.result-title {
margin: 0;
font-weight: 600;
}
.result img {
max-width: 100%;
border: 1px solid var(--boxel-200);
border-radius: var(--boxel-border-radius);
}
.url {
word-break: break-all;
font-size: var(--boxel-font-sm);
}
.status {
margin: 0;
padding: var(--boxel-sp-sm);
border-radius: var(--boxel-border-radius);
}
.status--error {
background: color-mix(in srgb, var(--boxel-error-100) 12%, white);
color: var(--boxel-error-100);
}
</style>
</template>
}
export class ScreenshotCardDemo extends CardDef {
static displayName = 'Screenshot Card Demo';
@field cards = linksToMany(CardDef);
@field format = contains(FormatField);
static isolated = Isolated;
}