Skip to content

Commit 58935e0

Browse files
committedMar 20, 2025
check if right spec before setting it for field playground
1 parent 7ced2c8 commit 58935e0

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed
 

‎packages/base/spec.gts

-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ class Isolated extends Component<typeof Spec> {
198198
-webkit-line-clamp: 2;
199199
overflow: hidden;
200200
text-wrap: pretty;
201-
word-break: break-word;
202201
}
203202
.box {
204203
border: 1px solid var(--boxel-border-color);

‎packages/host/app/components/operator-mode/code-submode/spec-preview.gts

+19-10
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ interface ContentSignature {
203203
spec: Spec | undefined;
204204
isLoading: boolean;
205205
viewCardInPlayground: (cardDefOrId: CardDefOrId) => void;
206-
onSpecView: (specId: string) => void;
206+
onSpecView: (spec: Spec) => void;
207207
};
208208
}
209209

@@ -351,7 +351,7 @@ class SpecPreviewContent extends GlimmerComponent<ContentSignature> {
351351
@card={{@spec}}
352352
@format='edit'
353353
@cardContext={{this.cardContext}}
354-
{{SpecPreviewModifier id=@spec.id onSpecView=@onSpecView}}
354+
{{SpecPreviewModifier spec=@spec onSpecView=@onSpecView}}
355355
/>
356356
{{/if}}
357357
</div>
@@ -675,8 +675,17 @@ export default class SpecPreview extends GlimmerComponent<Signature> {
675675
);
676676
}
677677

678-
private onSpecView = (id: string) => {
679-
this.updatePlaygroundSelections(id, true);
678+
private onSpecView = (spec: Spec) => {
679+
if (!spec.isField) {
680+
return; // not a field spec
681+
}
682+
if (
683+
this.getSelectedDeclarationAsCodeRef.name !== spec.ref.name ||
684+
this.getSelectedDeclarationAsCodeRef.module !== spec.moduleHref // absolute url
685+
) {
686+
return; // not the right field spec
687+
}
688+
this.updatePlaygroundSelections(spec.id, true);
680689
};
681690

682691
private viewCardInPlayground = (card: CardDefOrId) => {
@@ -786,8 +795,8 @@ function getRelativePath(baseUrl: string, targetUrl: string) {
786795
interface ModifierSignature {
787796
Args: {
788797
Named: {
789-
id?: string;
790-
onSpecView?: (id: string) => void;
798+
spec?: Spec;
799+
onSpecView?: (spec: Spec) => void;
791800
};
792801
};
793802
}
@@ -798,13 +807,13 @@ export class SpecPreviewModifier extends Modifier<ModifierSignature> {
798807
modify(
799808
_element: HTMLElement,
800809
_positional: [],
801-
{ id, onSpecView }: ModifierSignature['Args']['Named'],
810+
{ spec, onSpecView }: ModifierSignature['Args']['Named'],
802811
) {
803-
if (!id || !onSpecView) {
804-
throw new Error('bug: no id or onSpecView hook');
812+
if (!spec || !onSpecView) {
813+
throw new Error('bug: no spec or onSpecView hook');
805814
}
806815
next(() => {
807-
onSpecView(id);
816+
onSpecView(spec);
808817
});
809818
}
810819
}

‎packages/host/tests/acceptance/code-submode/spec-test.gts

+70
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getPlaygroundSelections,
2727
getRecentFiles,
2828
assertCardExists,
29+
selectDeclaration,
2930
} from '../../helpers/playground';
3031

3132
import { setupApplicationTest } from '../../helpers/setup';
@@ -66,6 +67,9 @@ const personCardSource = `
6667
export class PersonField extends FieldDef {
6768
static displayName = 'PersonField';
6869
}
70+
export class DifferentField extends FieldDef {
71+
static displayName = 'DifferentField';
72+
}
6973
`;
7074

7175
const person1CardSource = `
@@ -461,6 +465,26 @@ module('Acceptance | Spec preview', function (hooks) {
461465
},
462466
},
463467
},
468+
'different-field-entry.json': {
469+
data: {
470+
type: 'card',
471+
attributes: {
472+
title: 'DifferentField',
473+
description: 'Spec for DifferentField',
474+
specType: 'field',
475+
ref: {
476+
module: `./person`,
477+
name: 'DifferentField',
478+
},
479+
},
480+
meta: {
481+
adoptsFrom: {
482+
module: `${baseRealm.url}spec`,
483+
name: 'Spec',
484+
},
485+
},
486+
},
487+
},
464488
'.realm.json': {
465489
name: 'Test Workspace B',
466490
backgroundURL:
@@ -1022,4 +1046,50 @@ module('Acceptance | Spec preview', function (hooks) {
10221046
assert.dom('[data-test-exported-name]').hasText('Pet');
10231047
assert.dom('[data-test-module-href]').hasText(`${testRealmURL}pet`);
10241048
});
1049+
1050+
test('it does not set the wrong spec for field playground', async function (assert) {
1051+
await visitOperatorMode({
1052+
submode: 'code',
1053+
codePath: `${testRealmURL}person.gts`,
1054+
});
1055+
await click('[data-test-accordion-item="playground"] button');
1056+
assert.dom('[data-test-playground-panel]').exists();
1057+
let selection =
1058+
getPlaygroundSelections()?.[`${testRealmURL}person/PersonField`];
1059+
assert.strictEqual(selection, undefined);
1060+
1061+
await selectDeclaration('PersonField');
1062+
selection =
1063+
getPlaygroundSelections()?.[`${testRealmURL}person/PersonField`];
1064+
assert.strictEqual(
1065+
selection,
1066+
undefined,
1067+
'Person Spec is not set as the spec for PersonField',
1068+
);
1069+
assert.dom('[data-test-create-spec-button]').exists();
1070+
1071+
await selectDeclaration('DifferentField');
1072+
let newSelection =
1073+
getPlaygroundSelections()?.[`${testRealmURL}person/DifferentField`];
1074+
assert.strictEqual(
1075+
newSelection?.cardId,
1076+
`${testRealmURL}different-field-entry`,
1077+
);
1078+
assert.dom('[data-test-create-spec-button]').doesNotExist();
1079+
1080+
await selectDeclaration('PersonField');
1081+
selection =
1082+
getPlaygroundSelections()?.[`${testRealmURL}person/PersonField`];
1083+
assert.strictEqual(
1084+
selection,
1085+
undefined,
1086+
'DifferentField Spec is not set as the spec for PersonField',
1087+
);
1088+
assert.strictEqual(
1089+
selection,
1090+
undefined,
1091+
'DifferentField Spec is not set as the spec for PersonField',
1092+
);
1093+
assert.dom('[data-test-create-spec-button]').exists();
1094+
});
10251095
});

0 commit comments

Comments
 (0)