Skip to content

Commit

Permalink
perf: 优化模拟器
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBeard30 committed Mar 29, 2024
1 parent 83345f9 commit 637a6c9
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 30 deletions.
42 changes: 31 additions & 11 deletions src/app/components/container/simulator.component.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChild,
TemplateRef
} from '@angular/core';
import { PcSimulatorComponent } from '../simulators/pc-simulator/pc-simulator.component';
import { Engine, Screen, ScreenType } from '@/app/core/models';
import { MobileSimulatorComponent } from '@/app/components/simulators/mobile-simulator/mobile-simulator.component';
import { NgTemplateOutlet } from '@angular/common';
import { ResponsiveService } from '@/app/services/responsive.service';

@Component({
selector: 'app-simulator',
template: `
@if (screen.type === ScreenType.PC) {
<app-pc-simulator>
<ng-content></ng-content>
<ng-container *ngTemplateOutlet="template"></ng-container>
</app-pc-simulator>
} @else if (screen.type === ScreenType.Mobile) {
<!-- <app-mobile-simulator>-->
<!-- <ng-content></ng-content>-->
<!-- </app-mobile-simulator>-->
<app-mobile-simulator>
<ng-container *ngTemplateOutlet="template"></ng-container>
</app-mobile-simulator>
} @else {
<!-- <app-pc-simulator>-->
<!-- <ng-content></ng-content>-->
<!-- </app-pc-simulator>-->
<app-pc-simulator>
<ng-container *ngTemplateOutlet="template"></ng-container>
</app-pc-simulator>
}
`,
styles: [``],
standalone: true,
imports: [PcSimulatorComponent, MobileSimulatorComponent],
imports: [PcSimulatorComponent, MobileSimulatorComponent, NgTemplateOutlet],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SimulatorComponent {
export class SimulatorComponent implements AfterViewInit {
@ContentChild(TemplateRef) template: TemplateRef<any>;

screen: Screen;
constructor(private designer: Engine) {
constructor(
private designer: Engine,
private responsiveService: ResponsiveService,
private cdr: ChangeDetectorRef
) {
this.screen = this.designer.screen;
}

ngAfterViewInit(): void {
console.log('template>>>', this.template);
this.responsiveService.subscribe(() => this.cdr.detectChanges());
}

protected readonly ScreenType = ScreenType;
}
4 changes: 3 additions & 1 deletion src/app/components/panels/viewport-panel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { SimulatorComponent } from '@/app/components/container/simulator.compone
template: `
<app-workspace-panel-item [style]="style" [flexible]="flexible">
<app-simulator>
<ng-content></ng-content>
<ng-template>
<ng-content></ng-content>
</ng-template>
</app-simulator>
</app-workspace-panel-item>
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { NgOptimizedImage, NgStyle } from '@angular/common';
width: screen.flip ? 946.667 : 460,
height: screen.flip ? 460 : 946.667,
boxShadow: '0 0 20px #0000004d',
borderRadius: 60,
borderRadius: '60px',
backfaceVisibility: 'hidden'
}"
/>
Expand Down Expand Up @@ -70,19 +70,19 @@ export class MobileBodyComponent {
if (this.screen.flip) {
return {
position: 'absolute',
width: 736,
height: 414,
top: 43.3333,
left: 106.667,
width: '736px',
height: '414px',
top: '43.3333px',
left: '106.667px',
overflow: 'hidden'
};
}
return {
position: 'absolute',
width: 414,
height: 736,
top: 126.667,
left: 23.3333,
width: '414px',
height: '736px',
top: '126.667px',
left: '23.3333px',
overflow: 'hidden'
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@
</nz-button-group>
}

@if (use.includes('SCREEN_TYPE') && screen.type == ScreenType.Responsive){
@if (use.includes('SCREEN_TYPE') && screen.type === ScreenType.Responsive){

}

@if (use.includes('SCREEN_TYPE')){
<nz-button-group nzSize="small" [style]="{marginRight: '20px'}">
<button nz-button nzSize="small">
<button nz-button nzSize="small" (click)="selectPc()">
<app-icon icon="Pc"></app-icon>
</button>
<button nz-button nzSize="small">
<button nz-button nzSize="small" (click)="selectMobile()">
<app-icon icon="Mobile"></app-icon>
</button>
<button nz-button nzSize="small">
<button nz-button nzSize="small" (click)="selectResponsive()">
<app-icon icon="Responsive"></app-icon>
</button>
</nz-button-group>
}

@if (use.includes('SCREEN_TYPE') && screen.type == ScreenType.Mobile){
@if (use.includes('SCREEN_TYPE') && screen.type === ScreenType.Mobile){

}

Expand Down
34 changes: 30 additions & 4 deletions src/app/components/widgets/designer-tool/designer-tool.widget.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Component, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { usePrefix } from '@/app/utils';
import { SharedModule } from '@/app/shared/shared.module';
import { IconWidget } from '@/app/components/widgets/icon/icon.widget';
import { ScreenType } from '@/app/core/models/screen';
import { Screen, ScreenType } from '@/app/core/models/screen';
import { Engine } from '@/app/core/models';
import { ResponsiveService } from '@/app/services/responsive.service';

@Component({
selector: 'app-designer-tool-widget',
standalone: true,
templateUrl: './designer-tool.widget.html',
imports: [SharedModule, IconWidget]
imports: [SharedModule, IconWidget],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DesignerToolWidget {
prefix = usePrefix('designer-tools');
Expand All @@ -17,6 +20,29 @@ export class DesignerToolWidget {

@Input() use: string[] = ['HISTORY', 'CURSOR', 'SCREEN_TYPE'];

screen = { type: ScreenType.PC };
screen: Screen;

constructor(
private designer: Engine,
private responsiveService: ResponsiveService
) {
this.screen = this.designer.screen;
}

protected readonly ScreenType = ScreenType;

selectPc() {
this.screen.setType(ScreenType.PC);
this.responsiveService.change();
}

selectMobile() {
this.screen.setType(ScreenType.Mobile);
this.responsiveService.change();
}

selectResponsive() {
this.screen.setType(ScreenType.Responsive);
this.responsiveService.change();
}
}
17 changes: 17 additions & 0 deletions src/app/services/responsive.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class ResponsiveService {
pageDetection$ = new Subject<boolean>();

change() {
this.pageDetection$.next(true);
}

subscribe(callback: () => void) {
this.pageDetection$.subscribe(v => callback());
}
}

0 comments on commit 637a6c9

Please sign in to comment.