@@ -32,6 +32,7 @@ import {
32
32
type LooseSingleCardDocument ,
33
33
} from ' @cardstack/runtime-common' ;
34
34
35
+ import CopyCardCommand from ' @cardstack/host/commands/copy-card' ;
35
36
import config from ' @cardstack/host/config/environment' ;
36
37
import { StackItem , isIndexCard } from ' @cardstack/host/lib/stack-item' ;
37
38
@@ -420,28 +421,19 @@ export default class InteractSubmode extends Component<Signature> {
420
421
}
421
422
422
423
private _copyCard = dropTask (
423
- async (card : CardDef , stackIndex : number , done : Deferred <CardDef >) => {
424
+ async (
425
+ sourceCard : CardDef ,
426
+ stackIndex : number ,
427
+ done : Deferred < CardDef > ,
428
+ ) => {
424
429
let newCard: CardDef | undefined ;
425
430
try {
426
- // use existing card in stack to determine realm url,
427
- // otherwise use user's first writable realm
428
- let topCard =
429
- this .operatorModeStateService .topMostStackItems ()[stackIndex ]?.card ;
430
- let realmURL: URL | undefined ;
431
- if (topCard ) {
432
- let url = await this .cardService .getRealmURL (topCard );
433
- // open card might be from a realm in which we don't have write permissions
434
- if (url && this .realm .canWrite (url .href )) {
435
- realmURL = url ;
436
- }
437
- }
438
- if (! realmURL ) {
439
- if (! this .realm .defaultWritableRealm ) {
440
- throw new Error (' Could not find a writable realm' );
441
- }
442
- realmURL = new URL (this .realm .defaultWritableRealm .path );
443
- }
444
- newCard = await this .cardService .copyCard (card , realmURL );
431
+ let { commandContext } = this .commandService ;
432
+ const result = await new CopyCardCommand (commandContext ).execute ({
433
+ sourceCard ,
434
+ targetStackIndex: stackIndex ,
435
+ });
436
+ newCard = result .newCard ;
445
437
} catch (e ) {
446
438
done .reject (e );
447
439
} finally {
@@ -475,7 +467,12 @@ export default class InteractSubmode extends Component<Signature> {
475
467
sources .sort ((a , b ) => a .title .localeCompare (b .title ));
476
468
let scrollToCard: CardDef | undefined ;
477
469
for (let [index, card] of sources .entries ()) {
478
- let newCard = await this .cardService .copyCard (card , realmURL );
470
+ let { newCard } = await new CopyCardCommand (
471
+ this .commandService .commandContext ,
472
+ ).execute ({
473
+ sourceCard: card ,
474
+ targetRealmUrl: realmURL .href ,
475
+ });
479
476
if (index === 0 ) {
480
477
scrollToCard = newCard ; // we scroll to the first card lexically by title
481
478
}
0 commit comments