Skip to content

Commit 78e79e9

Browse files
committed
remember selected playground format
1 parent 71dc885 commit 78e79e9

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

packages/host/app/components/operator-mode/code-submode/playground-panel.gts

+28-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { action } from '@ember/object';
44
import type Owner from '@ember/owner';
55
import { service } from '@ember/service';
66
import Component from '@glimmer/component';
7-
import { tracked } from '@glimmer/tracking';
87

98
import Folder from '@cardstack/boxel-icons/folder';
109
import { restartableTask, task } from 'ember-concurrency';
@@ -248,11 +247,13 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
248247
{{/if}}
249248
{{/if}}
250249
</div>
251-
<FormatChooser
252-
class='format-chooser'
253-
@format={{this.format}}
254-
@setFormat={{this.setFormat}}
255-
/>
250+
{{#if this.card}}
251+
<FormatChooser
252+
class='format-chooser'
253+
@format={{this.format}}
254+
@setFormat={{this.setFormat}}
255+
/>
256+
{{/if}}
256257
</div>
257258
<style scoped>
258259
.playground-panel-content {
@@ -356,8 +357,10 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
356357
@service private declare realm: RealmService;
357358
@service private declare realmServer: RealmServerService;
358359
@service declare recentFilesService: RecentFilesService;
359-
@tracked private format: Format = 'isolated';
360-
private playgroundSelections: Record<string, string>;
360+
private playgroundSelections: Record<
361+
string, // moduleId
362+
{ cardId: string; format: Format }
363+
>; // TrackedObject
361364

362365
constructor(owner: Owner, args: PlaygroundContentSignature['Args']) {
363366
super(owner, args);
@@ -404,22 +407,27 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
404407
};
405408
}
406409

407-
private cardResource = getCard(this, () =>
408-
this.playgroundSelections[this.args.moduleId]?.replace(/\.json$/, ''),
410+
private cardResource = getCard(
411+
this,
412+
() => this.playgroundSelections[this.args.moduleId]?.cardId,
409413
);
410414

411415
private get card(): CardDef | undefined {
412416
return this.cardResource.card;
413417
}
414418

419+
private get format(): Format {
420+
return this.playgroundSelections[this.args.moduleId]?.format ?? 'isolated';
421+
}
422+
415423
private copyToClipboard = task(async (id: string) => {
416424
await navigator.clipboard.writeText(id);
417425
});
418426

419-
private openInInteractMode = task(async (id: string, format: Format) => {
427+
private openInInteractMode = task(async (id: string) => {
420428
await this.operatorModeStateService.openCardInInteractMode(
421429
new URL(id),
422-
format,
430+
this.format === 'edit' ? 'edit' : 'isolated',
423431
);
424432
});
425433

@@ -439,28 +447,31 @@ class PlaygroundPanelContent extends Component<PlaygroundContentSignature> {
439447
icon: IconCode,
440448
}),
441449
new MenuItem('Open in Interact Mode', 'action', {
442-
action: () => this.openInInteractMode.perform(cardId, this.format),
450+
action: () => this.openInInteractMode.perform(cardId),
443451
icon: Eye,
444452
}),
445453
];
446454
return menuItems;
447455
}
448456

449-
private persistSelections = (cardId: string) => {
450-
this.playgroundSelections[this.args.moduleId] = cardId;
457+
private persistSelections = (cardId: string, format = this.format) => {
458+
this.playgroundSelections[this.args.moduleId] = { cardId, format };
451459
window.localStorage.setItem(
452460
'playground-selections',
453461
JSON.stringify(this.playgroundSelections),
454462
);
455463
};
456464

457465
@action private onSelect(card: PrerenderedCard) {
458-
this.persistSelections(card.url);
466+
this.persistSelections(card.url.replace(/\.json$/, ''));
459467
}
460468

461469
@action
462470
private setFormat(format: Format) {
463-
this.format = format;
471+
if (!this.card?.id) {
472+
return;
473+
}
474+
this.persistSelections(this.card.id, format);
464475
}
465476

466477
private chooseCard = restartableTask(async () => {

0 commit comments

Comments
 (0)