Skip to content

Commit

Permalink
feat: in place editor
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxiaolang committed Jul 3, 2024
1 parent fb87f73 commit 92ce1b7
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 0 deletions.
39 changes: 39 additions & 0 deletions index.js
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);
35 changes: 35 additions & 0 deletions src/editable/editable.component.html
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>
22 changes: 22 additions & 0 deletions src/editable/editable.component.scss
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;
}
}
72 changes: 72 additions & 0 deletions src/editable/editable.component.ts
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();
}
}
13 changes: 13 additions & 0 deletions src/editable/editable.directive.ts
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 {}
21 changes: 21 additions & 0 deletions src/editable/editable.module.ts
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 {}
4 changes: 4 additions & 0 deletions src/editable/editable.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum EditableMode {
View = 'view',
Edit = 'edit',
}
3 changes: 3 additions & 0 deletions src/editable/index.ts
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';
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './date-picker';
export * from './dialog';
export * from './drawer';
export * from './dropdown';
export * from './editable';
export * from './form';
export * from './i18n';
export * from './icon';
Expand Down

0 comments on commit 92ce1b7

Please sign in to comment.