Skip to content

Commit 617ca67

Browse files
committed
refactor(composer): use RenderComponent API in droppable component
re orchestratora/rfcs#2
1 parent 8d9a212 commit 617ca67

8 files changed

Lines changed: 111 additions & 71 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<orc-orchestrator [config]="initialConfig"></orc-orchestrator>
1+
<orc-render-item [item]="initialConfig"></orc-render-item>

libs/composer/src/lib/composer-canvas/composer-canvas.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import { ComposerDroppableComponent } from '../composer-droppable';
1010
changeDetection: ChangeDetectionStrategy.OnPush,
1111
})
1212
export class ComposerCanvasComponent {
13-
initialConfig: OrchestratorConfigItem = ComposerDroppableComponent.getWrapperConfig();
13+
initialConfig = ComposerDroppableComponent.wrapperConfig;
1414
}

libs/composer/src/lib/composer-components/composer-components.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ComponentFactory,
55
ComponentFactoryResolver,
66
OnInit,
7+
Type,
78
} from '@angular/core';
89
import {
910
ComponentLocatorService,
@@ -17,6 +18,8 @@ import {
1718
changeDetection: ChangeDetectionStrategy.OnPush,
1819
})
1920
export class ComposerComponentsComponent implements OnInit {
21+
static BLACKLIST_COMPONENTS: Type<any>[] = [];
22+
2023
componentsInfo: ComponentFactory<OrchestratorDynamicComponent>[] = [];
2124

2225
constructor(
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
import { Injectable } from '@angular/core';
2-
import {
3-
Option,
4-
OptionRequired,
5-
OrchestratorConfigItem,
6-
OrchestratorDynamicComponentType,
7-
} from '@orchestrator/core';
2+
import { Option, OrchestratorConfigItem } from '@orchestrator/core';
83

4+
/**
5+
* @internal
6+
*/
97
@Injectable({ providedIn: 'root' })
10-
export class ComposerDroppableConfig<C = any>
11-
implements OrchestratorConfigItem<C> {
12-
@OptionRequired()
13-
component: string | OrchestratorDynamicComponentType<C>;
14-
15-
@Option()
16-
config?: C;
17-
18-
@Option()
19-
id?: string;
20-
21-
@Option()
22-
classes?: string | string[] | { [name: string]: boolean };
23-
24-
@Option()
25-
attributes?: { [attr: string]: string };
26-
8+
export class ComposerDroppableConfig<C = any> {
279
@Option()
28-
handlers?: { [event: string]: string | Function };
10+
item?: OrchestratorConfigItem<C>;
2911
}

libs/composer/src/lib/composer-droppable/composer-droppable.component.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
.rendered-item {
2+
position: relative;
3+
display: inline-block;
4+
}
5+
16
.drop-list {
27
padding: 10px;
38
background: lightcyan;
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
<orc-orchestrator *ngIf="config.component" [config]="config"></orc-orchestrator>
2-
<div
3-
*ngIf="!config.component"
4-
class="drop-list"
5-
cdkDropList
6-
(cdkDropListDropped)="drop($event)"
7-
>
8-
<span class="drop-list-hint">Drop component here!</span>
1+
<div class="rendered-item" *ngIf="config?.item?.component; else dropTpl">
2+
<orc-render-item [item]="config.item"></orc-render-item>
93
</div>
4+
<ng-template #dropTpl>
5+
<div
6+
class="drop-list"
7+
cdkDropList
8+
(cdkDropListDropped)="drop($event, configInput.value)"
9+
>
10+
<span class="drop-list-hint">Drop component here!</span>
11+
</div>
12+
<nz-divider nzText="Set component configuration" nzDashed></nz-divider>
13+
<textarea
14+
#configInput
15+
nz-input
16+
[nzAutosize]="{ minRows: 2, maxRows: 6 }"
17+
></textarea>
18+
</ng-template>

libs/composer/src/lib/composer-droppable/composer-droppable.component.ts

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import {
44
Component,
55
EventEmitter,
66
Input,
7+
OnChanges,
8+
Optional,
79
Output,
10+
SimpleChanges,
11+
SkipSelf,
812
} from '@angular/core';
913
import {
1014
DynamicComponent,
1115
OrchestratorConfigItem,
1216
OrchestratorDynamicComponent,
1317
OrchestratorDynamicComponentType,
18+
RenderComponent,
1419
} from '@orchestrator/core';
1520

1621
import { ComposerDroppableConfig } from './composer-droppable-config';
@@ -26,51 +31,83 @@ import { ComposerDroppableConfig } from './composer-droppable-config';
2631
})
2732
@DynamicComponent({ config: ComposerDroppableConfig })
2833
export class ComposerDroppableComponent
29-
implements OrchestratorDynamicComponent {
34+
implements OrchestratorDynamicComponent<ComposerDroppableConfig>, OnChanges {
35+
36+
constructor(
37+
private renderComponent: RenderComponent,
38+
@SkipSelf() @Optional() private parentDroppable: ComposerDroppableComponent,
39+
) {}
40+
static wrapperConfig = Object.freeze<OrchestratorConfigItem>({
41+
component: ComposerDroppableComponent,
42+
});
43+
3044
@Input() config: ComposerDroppableConfig;
3145

32-
@Output() componentDrop = new EventEmitter<OrchestratorConfigItem>();
33-
static getWrapperConfig() {
34-
return {
35-
component: ComposerDroppableComponent,
36-
handlers: {
37-
componentDrop(
38-
updateConfig,
39-
getInjector,
40-
injectFlags,
41-
getComponent,
42-
$config: OrchestratorConfigItem,
43-
) {
44-
updateConfig($config);
45-
const getParentDroppableInjector = getInjector().get(
46-
'getInjector',
47-
injectFlags.SkipSelf,
48-
);
49-
const parentUpdateConfig = getParentDroppableInjector().get(
50-
'updateConfig',
51-
);
52-
const parentGetConfig = getParentDroppableInjector().get('getConfig');
53-
const parentConfigItems = parentGetConfig().items || [];
54-
parentUpdateConfig({
55-
items: [
56-
...parentConfigItems,
57-
getComponent().constructor.getWrapperConfig(),
58-
],
59-
});
60-
},
61-
},
62-
};
63-
}
46+
@Input() items: OrchestratorConfigItem[];
47+
48+
@Output() componentDropped = new EventEmitter<
49+
OrchestratorDynamicComponentType
50+
>();
51+
52+
private droppedConfig: OrchestratorConfigItem;
6453

65-
static wrapComponent(comp) {
54+
static wrapComponent<C>(
55+
comp: OrchestratorDynamicComponentType<C>,
56+
config?: C,
57+
): OrchestratorConfigItem<C> {
6658
return {
6759
component: comp,
68-
items: [ComposerDroppableComponent.getWrapperConfig()],
60+
config,
61+
items: [ComposerDroppableComponent.wrapperConfig],
6962
};
7063
}
7164

72-
drop(e: CdkDragDrop<any>) {
65+
ngOnChanges(changes: SimpleChanges) {
66+
if ('config' in changes && this.droppedConfig) {
67+
this.config = {
68+
...this.config,
69+
item: this.droppedConfig,
70+
};
71+
}
72+
73+
if ('items' in changes) {
74+
this.config = {
75+
...this.config,
76+
item: {
77+
...this.config.item,
78+
items: this.items,
79+
},
80+
};
81+
}
82+
}
83+
84+
drop(e: CdkDragDrop<any>, conf?: string) {
7385
const compType = e.item.data as OrchestratorDynamicComponentType;
74-
this.componentDrop.emit(ComposerDroppableComponent.wrapComponent(compType));
86+
const config = ComposerDroppableComponent.wrapComponent(
87+
compType,
88+
JSON.parse(conf || 'null'),
89+
);
90+
91+
this.droppedConfig = config;
92+
93+
if (this.parentDroppable) {
94+
this.parentDroppable.addItem(config);
95+
} else {
96+
this.config = { item: config };
97+
}
98+
99+
this.componentDropped.emit(compType);
100+
}
101+
102+
addItem(item: OrchestratorConfigItem) {
103+
this.renderComponent.removeItem(ComposerDroppableComponent.wrapperConfig);
104+
105+
this.renderComponent.addItem(
106+
ComposerDroppableComponent.wrapComponent(ComposerDroppableComponent, {
107+
item,
108+
}),
109+
);
110+
111+
this.renderComponent.addItem(ComposerDroppableComponent.wrapperConfig);
75112
}
76113
}

libs/composer/src/lib/composer-droppable/composer-droppable.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { DragDropModule } from '@angular/cdk/drag-drop';
22
import { CommonModule } from '@angular/common';
33
import { NgModule } from '@angular/core';
44
import { OrchestratorCoreModule } from '@orchestrator/core';
5+
import { NzDividerModule } from 'ng-zorro-antd/divider';
6+
import { NzInputModule } from 'ng-zorro-antd/input';
57

68
import { ComposerDroppableComponent } from './composer-droppable.component';
79

@@ -12,6 +14,8 @@ import { ComposerDroppableComponent } from './composer-droppable.component';
1214
imports: [
1315
CommonModule,
1416
DragDropModule,
17+
NzInputModule,
18+
NzDividerModule,
1519
OrchestratorCoreModule.withComponents([ComposerDroppableComponent]),
1620
],
1721
exports: [ComposerDroppableComponent],

0 commit comments

Comments
 (0)