-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb87f73
commit 92ce1b7
Showing
9 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Calculator { | ||
expr; | ||
operator; | ||
|
||
constructor(expr) { | ||
this.expr = expr; | ||
|
||
this.operator = ['+', '-', '*', '/'].find(operator => | ||
expr.includes(operator), | ||
); | ||
} | ||
|
||
count() { | ||
const [num1, num2] = this.expr.split(this.operator).map(v => parseInt(v)); | ||
|
||
switch (this.operator) { | ||
case '+': { | ||
return num1 + num2; | ||
} | ||
case '-': { | ||
return num1 - num2; | ||
} | ||
case '*': { | ||
return num1 * num2; | ||
} | ||
case '/': { | ||
return num1 / num2; | ||
} | ||
} | ||
} | ||
|
||
get result() { | ||
return this.count(); | ||
} | ||
} | ||
|
||
const calculator = new Calculator('1*8'); | ||
|
||
console.log(calculator.result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<div [class]="bem.block()"> | ||
<div | ||
[class]="bem.element('viewer-container')" | ||
*ngIf="$$mode() === EditableMode.View" | ||
> | ||
<ng-container [ngTemplateOutlet]="viewer"> </ng-container> | ||
<button | ||
aui-button="text" | ||
[square]="true" | ||
(click)="$$mode.set(EditableMode.Edit)" | ||
> | ||
<aui-icon icon="pencil"></aui-icon> | ||
</button> | ||
</div> | ||
<ng-container *ngIf="$$mode() === EditableMode.Edit"> | ||
<div [class]="bem.element('editor-container')"> | ||
<ng-container [ngTemplateOutlet]="editor"> </ng-container> | ||
|
||
<button | ||
aui-button="text" | ||
[square]="true" | ||
(click)="saveEdit()" | ||
> | ||
<aui-icon icon="check"></aui-icon> | ||
</button> | ||
<button | ||
aui-button="text" | ||
[square]="true" | ||
(click)="cancelEdit()" | ||
> | ||
<aui-icon icon="xmark"></aui-icon> | ||
</button> | ||
</div> | ||
</ng-container> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
@import '../theme/var'; | ||
@import '../theme/mixin'; | ||
|
||
.aui-editable { | ||
.aui-button--text { | ||
color: use-rgb(main-text); | ||
|
||
&:first-of-type { | ||
margin-left: 8px; | ||
} | ||
|
||
&:hover { | ||
color: use-rgb(primary); | ||
} | ||
} | ||
|
||
&__view-container, | ||
&__editor-container { | ||
display: flex; | ||
align-items: center; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { AsyncPipe, NgFor, NgIf, NgTemplateOutlet } from '@angular/common'; | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
ContentChild, | ||
EventEmitter, | ||
Output, | ||
TemplateRef, | ||
effect, | ||
signal, | ||
} from '@angular/core'; | ||
|
||
import { ButtonModule } from '../button'; | ||
import { IconComponent } from '../icon'; | ||
import { buildBem } from '../internal/utils'; | ||
|
||
import { | ||
EditableEditorDirective, | ||
EditableViewerDirective, | ||
} from './editable.directive'; | ||
import { EditableMode } from './editable.type'; | ||
|
||
const bem = buildBem('aui-editable'); | ||
|
||
@Component({ | ||
selector: 'aui-editable', | ||
templateUrl: 'editable.component.html', | ||
styleUrls: ['editable.component.scss'], | ||
standalone: true, | ||
imports: [ | ||
NgIf, | ||
NgFor, | ||
AsyncPipe, | ||
NgTemplateOutlet, | ||
IconComponent, | ||
ButtonModule, | ||
], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class EditableComponent { | ||
@Output() save: EventEmitter<void> = new EventEmitter<void>(); | ||
@Output() cancel: EventEmitter<void> = new EventEmitter<void>(); | ||
@Output() modeChange: EventEmitter<EditableMode> = | ||
new EventEmitter<EditableMode>(); | ||
|
||
@ContentChild(EditableViewerDirective, { read: TemplateRef }) | ||
viewer: TemplateRef<unknown>; | ||
|
||
@ContentChild(EditableEditorDirective, { read: TemplateRef }) | ||
editor: TemplateRef<unknown>; | ||
|
||
EditableMode = EditableMode; | ||
bem = bem; | ||
|
||
$$mode = signal(EditableMode.View); | ||
|
||
constructor() { | ||
effect(() => { | ||
this.modeChange.emit(this.$$mode()); | ||
}); | ||
} | ||
|
||
saveEdit() { | ||
this.$$mode.set(EditableMode.View); | ||
this.save.emit(); | ||
} | ||
|
||
cancelEdit() { | ||
this.$$mode.set(EditableMode.View); | ||
this.cancel.emit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Directive } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '*[auiEditableViewer]', | ||
standalone: true, | ||
}) | ||
export class EditableViewerDirective {} | ||
|
||
@Directive({ | ||
selector: '*[auiEditableEditor]', | ||
standalone: true, | ||
}) | ||
export class EditableEditorDirective {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NgModule } from '@angular/core'; | ||
|
||
import { EditableComponent } from './editable.component'; | ||
import { | ||
EditableEditorDirective, | ||
EditableViewerDirective, | ||
} from './editable.directive'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
EditableComponent, | ||
EditableViewerDirective, | ||
EditableEditorDirective, | ||
], | ||
exports: [ | ||
EditableComponent, | ||
EditableViewerDirective, | ||
EditableEditorDirective, | ||
], | ||
}) | ||
export class EditableModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum EditableMode { | ||
View = 'view', | ||
Edit = 'edit', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './editable.component'; | ||
export * from './editable.directive'; | ||
export * from './editable.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters