Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8185d22
get file extension and realm info
tintinthong Sep 19, 2023
bce717f
reinclude test
tintinthong Sep 19, 2023
33bb5eb
put getRealmInfo in resource
tintinthong Sep 19, 2023
dd5d140
fix url bug
tintinthong Sep 19, 2023
21defef
cache request
tintinthong Sep 19, 2023
f754c0f
lint
tintinthong Sep 19, 2023
69e3ef5
Use adoption manager and use loading states
tintinthong Sep 20, 2023
97d686e
add selection of card
tintinthong Sep 20, 2023
2fb1bc3
mock out some ui
tintinthong Sep 20, 2023
916f4e9
Merge branch 'main' into add-extension-and-realm-info
tintinthong Sep 20, 2023
4a98b73
fix lint
tintinthong Sep 20, 2023
9133227
refactor for better loading pattern and clearer role
tintinthong Sep 20, 2023
6efc1ef
Change selections to a single object
tintinthong Sep 20, 2023
a664938
might as well use a select button
tintinthong Sep 20, 2023
7d32901
add comment
tintinthong Sep 20, 2023
f8a2140
fix loading
tintinthong Sep 20, 2023
a1594a3
place realm info deeper in definition info
tintinthong Sep 20, 2023
efb0691
maintain selected card type api
tintinthong Sep 20, 2023
d84cecb
Merge branch 'main' into add-extension-and-realm-info
tintinthong Sep 20, 2023
20f00a6
TODO for test
tintinthong Sep 20, 2023
d1df438
TODO test
tintinthong Sep 20, 2023
edd0e60
wokraround 4 redirect in host test
tintinthong Sep 20, 2023
346767f
Revert "TODO test"
tintinthong Sep 20, 2023
cc90d68
Revert "TODO for test"
tintinthong Sep 20, 2023
aa1e1fb
fix due to card no longer being resource. TODO: solve .json identific…
tintinthong Sep 22, 2023
60365c7
Merge branch 'main' into add-extension-and-realm-info
tintinthong Sep 23, 2023
466ee51
Fix test using a mock redirected response (#667)
tintinthong Sep 25, 2023
e6ef76e
use pathname
tintinthong Sep 25, 2023
b6e7f77
Merge branch 'main' into add-extension-and-realm-info
tintinthong Sep 26, 2023
3debe8e
move everything to code mode
tintinthong Sep 26, 2023
c9309bd
Fix lint
tintinthong Sep 26, 2023
2c3b147
Fix linting
tintinthong Sep 26, 2023
ffb2ace
fix info service to get card source accept header
tintinthong Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 106 additions & 12 deletions packages/host/app/components/operator-mode/code-mode.gts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ import config from '@cardstack/host/config/environment';

import monacoModifier from '@cardstack/host/modifiers/monaco';

import { adoptionChainResource } from '@cardstack/host/resources/adoption-chain';
import {
getCardType,
type CardType,
} from '@cardstack/host/resources/card-type';
import {
file,
isReady,
Expand Down Expand Up @@ -78,6 +81,8 @@ import RecentFilesService from '@cardstack/host/services/recent-files-service';

import { CardDef } from 'https://cardstack.com/base/card-api';

import { type BaseDef } from 'https://cardstack.com/base/card-api';

import FileTree from '../editor/file-tree';

import BinaryFileInfo from './binary-file-info';
Expand Down Expand Up @@ -111,6 +116,16 @@ const defaultPanelWidths: PanelWidths = {
emptyCodeModePanel: '80%',
};

interface ExportedCard {
cardType: CardType;
card: typeof BaseDef;
}

// Element
// - exported / unexported card or field
// - exported class or function
export type ElementInFile = ExportedCard; // can add more types here

export default class CodeMode extends Component<Signature> {
@service declare monacoService: MonacoService;
@service declare cardService: CardService;
Expand All @@ -122,6 +137,7 @@ export default class CodeMode extends Component<Signature> {
@tracked private loadFileError: string | null = null;
@tracked private maybeMonacoSDK: MonacoSDK | undefined;
@tracked private card: CardDef | undefined;
@tracked private selectedElement: ElementInFile | undefined;
@tracked cardError: Error | undefined;
private hasUnsavedSourceChanges = false;
private hasUnsavedCardChanges = false;
Expand Down Expand Up @@ -298,6 +314,56 @@ export default class CodeMode extends Component<Signature> {
return state;
});

@use private elements = resource(() => {
if (!this.importedModule) {
return new TrackedObject({
error: null,
isLoading: false,
value: [],
load: () => Promise<void>,
});
}

const state: {
isLoading: boolean;
value: ElementInFile[] | null;
error: Error | undefined;
load: () => Promise<void>;
} = new TrackedObject({
isLoading: true,
value: [],
error: undefined,
load: async () => {
state.isLoading = true;
if (this.importedModule === undefined) {
state.value = [];
return;
}
try {
await this.importedModule.loaded;
let module = this.importedModule?.module;
if (module) {
let cards = cardsOrFieldsFromModule(module);
let elements: ElementInFile[] = cards.map((card) => {
return {
cardType: getCardType(this, () => card),
card: card,
};
});
state.value = elements;
}
} catch (error: any) {
state.error = error;
} finally {
state.isLoading = false;
}
},
});

state.load();
return state;
});

private openFile = maybe(this, (context) => {
if (!this.codePath) {
this.setFileView('browser');
Expand Down Expand Up @@ -353,15 +419,6 @@ export default class CodeMode extends Component<Signature> {
}
}
});

@use private adoptionChain = resource(() => {
if (this.importedModule) {
return adoptionChainResource(this, this.importedModule);
} else {
return undefined;
}
});

// We are actually loading cards using a side-effect of this cached getter
// instead of a resource because with a resource it becomes impossible
// to ignore our own auto-save echoes, since the act of auto-saving triggers
Expand Down Expand Up @@ -420,6 +477,31 @@ export default class CodeMode extends Component<Signature> {
return this.card;
}

private get selectedElementInFile() {
if (this.selectedElement) {
return this.selectedElement;
} else {
if (this.elementsInFile === null) {
return;
}
return this.elementsInFile.length > 0
? this.elementsInFile[0]
: undefined;
}
}

@action
private selectElementInFile(el: ElementInFile) {
this.selectedElement = el;
}

get elementsInFile() {
if (this.elements.value === null) {
return [];
}
return this.elements.value;
}

private loadIfDifferent = restartableTask(
async (url: URL, incomingDoc?: SingleCardDocument) => {
await this.withTestWaiters(async () => {
Expand Down Expand Up @@ -691,8 +773,9 @@ export default class CodeMode extends Component<Signature> {
@cardInstance={{this.card}}
@readyFile={{this.readyFile}}
@realmInfo={{this.realmInfo}}
@realmIconURL={{this.realmIconURL}}
@adoptionChain={{this.adoptionChain}}
@selectedElement={{this.selectedElementInFile}}
@elements={{this.elementsInFile}}
@selectElement={{this.selectElementInFile}}
@delete={{this.delete}}
data-test-card-inheritance-panel
/>
Expand Down Expand Up @@ -1025,3 +1108,14 @@ function comparableSerialization(doc: LooseSingleCardDocument) {
}
return doc;
}

function isCardOrField(cardOrField: any): cardOrField is typeof BaseDef {
return typeof cardOrField === 'function' && 'baseDef' in cardOrField;
}

function cardsOrFieldsFromModule(
module: Record<string, any>,
_never?: never, // glint insists that w/o this last param that there are actually no params
): (typeof BaseDef)[] {
return Object.values(module).filter(isCardOrField);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ interface Action {
export interface BaseArgs {
title: string | undefined;
name: string | undefined;
fileExtension: string;
realmInfo: RealmInfo | null;
realmIconURL: string | null | undefined;
fileExtension: string | undefined;
realmInfo: RealmInfo | undefined | null;
isActive: boolean;
}

Expand All @@ -35,6 +34,10 @@ export class BaseDefinitionContainer extends Component<BaseSignature> {
return this.args.realmInfo?.name;
}

get realmIcon(): string | undefined | null {
return this.args.realmInfo?.iconURL;
}

<template>
<div class='container {{if @isActive "active"}}' ...attributes>
<div class='banner'>
Expand All @@ -47,11 +50,13 @@ export class BaseDefinitionContainer extends Component<BaseSignature> {
</div>
<div class='content'>
<div class='definition-info'>
<div class='realm-info'>
<img src={{@realmIconURL}} alt='realm-icon' />
<Label class='realm-name' data-test-definition-realm-name>in
{{this.realmName}}</Label>
</div>
{{#if @realmInfo}}

@jurgenwerk jurgenwerk Sep 25, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the RealmInfoProvider component (it was merged recently) so you don't have to pass the realm info into this component and you can get rid of the realmIcon getter

<div class='realm-info'>
<img src={{this.realmIcon}} alt='realm-icon' />
<Label class='realm-name' data-test-definition-realm-name>in
{{this.realmName}}</Label>
</div>
{{/if}}
<div data-test-definition-name class='definition-name'>{{@name}}</div>
</div>
{{#if @isActive}}
Expand All @@ -60,7 +65,6 @@ export class BaseDefinitionContainer extends Component<BaseSignature> {
</div>

</div>

<style>
.container {
background-color: var(--boxel-light);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class FileDefinitionContainer extends Component<FileSignature> {
@name={{undefined}}
@fileExtension={{@fileExtension}}
@realmInfo={{@realmInfo}}
@realmIconURL={{@realmIconURL}}
@isActive={{true}}
data-test-file-definition
>
Expand All @@ -43,7 +42,6 @@ export class ModuleDefinitionContainer extends Component<ModuleSignature> {
@name={{@name}}
@fileExtension={{@fileExtension}}
@realmInfo={{@realmInfo}}
@realmIconURL={{@realmIconURL}}
@isActive={{@isActive}}
data-test-card-module-definition
>
Expand All @@ -70,7 +68,6 @@ export class InstanceDefinitionContainer extends Component<InstanceSignature> {
@fileExtension={{@fileExtension}}
@name={{@name}}
@realmInfo={{@realmInfo}}
@realmIconURL={{@realmIconURL}}
@isActive={{true}}
data-test-card-instance-definition
>
Expand Down Expand Up @@ -102,7 +99,6 @@ export class ClickableModuleDefinitionContainer extends Component<ClickableModul
@name={{@name}}
@fileExtension={{@fileExtension}}
@realmInfo={{@realmInfo}}
@realmIconURL={{@realmIconURL}}
@isActive={{false}}
data-test-card-module-definition
/>
Expand Down
Loading