Skip to content

Commit d02fd13

Browse files
committed
feat: added material CDK for color picker popup
1 parent df3ae6b commit d02fd13

File tree

14 files changed

+108
-20
lines changed

14 files changed

+108
-20
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
"private": true,
1414
"dependencies": {
1515
"@angular/animations": "^6.0.0",
16+
"@angular/cdk": "^6.0.2",
1617
"@angular/common": "^6.0.0",
1718
"@angular/compiler": "^6.0.0",
1819
"@angular/core": "^6.0.0",
1920
"@angular/forms": "^6.0.0",
2021
"@angular/http": "^6.0.0",
22+
"@angular/material": "^6.0.2",
2123
"@angular/platform-browser": "^6.0.0",
2224
"@angular/platform-browser-dynamic": "^6.0.0",
2325
"@angular/router": "^6.0.0",

src/app/components/collection/collection.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Store, Select } from '@ngxs/store';
3-
import { InkBucket, InkCollection } from '../../ink.model';
3+
import { InkBucket, InkCollection, InkCollectionMeta } from '../../ink.model';
44
import { Observable } from 'rxjs';
55
import { map } from 'rxjs/operators';
66

@@ -11,7 +11,7 @@ import { map } from 'rxjs/operators';
1111
})
1212
export class CollectionComponent implements OnInit {
1313
bucket: Observable<InkBucket>;
14-
collection: InkCollection;
14+
collection: InkCollectionMeta;
1515
constructor(private store: Store) {
1616
this.store
1717
.select(s => s.collection)

src/app/components/ink/ink.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
<div class="data">
44
{{inkData.value}}
55
</div>
6-
<color-chrome (onChange)="colorChanged(inkData.id,$event)"></color-chrome>
6+
<!-- <color-chrome (onChange)="colorChanged(inkData.id,$event)"></color-chrome> -->
77
</div>
Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
1-
import { Component, OnInit, Input } from '@angular/core';
1+
import { Component, OnInit, Input, OnDestroy, ComponentRef, ElementRef } from '@angular/core';
22
import { Store } from '@ngxs/store';
33
import { InkColor } from '../../ink.model';
44
import { UpdateInkColor } from '../../store/actions/ink.action';
5+
import { Overlay, OverlayRef, OverlayConfig } from '@angular/cdk/overlay';
6+
import { ComponentPortal } from '@angular/cdk/portal';
7+
import { ChromeComponent } from 'ngx-color/chrome';
8+
import { map, debounceTime } from 'rxjs/operators';
59

610
@Component({
711
selector: 'inkapp-ink',
812
templateUrl: './ink.component.html',
913
styles: []
1014
})
11-
export class InkComponent implements OnInit {
15+
export class InkComponent implements OnInit, OnDestroy {
1216
@Input() inkData: InkColor;
1317
@Input() bucketId: number;
14-
constructor(private store: Store) {}
18+
overlayRef: OverlayRef;
19+
colorPanelPortal: ComponentPortal<ChromeComponent>;
20+
colorPickerRef: ComponentRef<ChromeComponent>;
21+
constructor(private store: Store, private overlay: Overlay, private elRef: ElementRef) {}
1522

16-
ngOnInit() {}
17-
openColorPicker() {}
18-
colorChanged(id, c) {
19-
console.log('c', c);
20-
this.store.dispatch(new UpdateInkColor({ bucketId: this.bucketId, value: c.color.hex, meta: c.color, id }));
23+
ngOnInit() {
24+
const config = new OverlayConfig();
25+
config.hasBackdrop = true;
26+
config.backdropClass = 'cdk-overlay-transparent-backdrop';
27+
config.positionStrategy = this.overlay
28+
.position()
29+
.connectedTo(this.elRef, { originX: 'start', originY: 'bottom' }, { overlayX: 'start', overlayY: 'top' });
30+
31+
this.overlayRef = this.overlay.create(config);
32+
this.colorPanelPortal = new ComponentPortal(ChromeComponent);
33+
// (this.colorPanelPortal.component as any).onChange.subscribe(a => {
34+
// console.log('hhh', a);
35+
// });
36+
}
37+
openColorPicker() {
38+
this.colorPickerRef = this.overlayRef.attach(this.colorPanelPortal);
39+
if (this.inkData.meta.hsl) {
40+
this.colorPickerRef.instance.color = this.inkData.meta.hsl;
41+
}
42+
this.colorPickerRef.instance.onChange.pipe(map(c => c.color)).subscribe(color => {
43+
this.store.dispatch(
44+
new UpdateInkColor({ bucketId: this.bucketId, value: color.hex, meta: color, id: this.inkData.id })
45+
);
46+
});
47+
this.overlayRef.backdropClick().subscribe(b => {
48+
console.log('backdrop clocked', b);
49+
if (this.overlayRef && this.overlayRef.hasAttached()) {
50+
this.overlayRef.detach();
51+
this.colorPickerRef.destroy();
52+
return;
53+
}
54+
});
55+
}
56+
ngOnDestroy() {
57+
if (this.overlayRef) {
58+
this.overlayRef.dispose();
59+
}
2160
}
2261
}

src/app/ink.config.ts

Whitespace-only changes.

src/app/ink.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import { SettingsPage } from './pages/settings/settings.component';
1818
import { SettingsState, BucketState, CollectionState, InkState } from './store/states';
1919
import { ColorModule } from './modules/color/color.module';
2020

21+
import { OverlayModule } from '@angular/cdk/overlay';
22+
2123
export const MODULES = [
2224
RoutingModule,
2325
NgxsModule.forRoot([SettingsState, BucketState, CollectionState, InkState]),
2426
NgxsReduxDevtoolsPluginModule.forRoot(),
25-
ColorModule
27+
ColorModule,
28+
OverlayModule
2629
];
2730
export const COMPONENTS = [
2831
InkApp,

src/app/modules/color/color.module.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
24
import { ColorAlphaModule } from 'ngx-color/alpha'; // <color-alpha-picker></color-alpha-picker>
35
import { ColorBlockModule } from 'ngx-color/block'; // <color-block></color-block>
4-
import { ColorChromeModule } from 'ngx-color/chrome'; // <color-chrome></color-chrome>
6+
import { ColorChromeModule, ChromeComponent } from 'ngx-color/chrome'; // <color-chrome></color-chrome>
57
import { ColorCircleModule } from 'ngx-color/circle'; // <color-circle></color-circle>
68
import { ColorCompactModule } from 'ngx-color/compact'; // <color-compact></color-compact>
79
import { ColorGithubModule } from 'ngx-color/github'; // <color-github></color-github>
@@ -12,13 +14,13 @@ import { ColorSketchModule } from 'ngx-color/sketch'; // <color-sketch></color-s
1214
import { ColorSliderModule } from 'ngx-color/slider'; // <color-slider></color-slider>
1315
import { ColorSwatchesModule } from 'ngx-color/swatches'; // <color-swatches></color-swatches>
1416
import { ColorTwitterModule } from 'ngx-color/twitter'; // <color-twitter></color-twitter>
15-
16-
import { CommonModule } from '@angular/common';
17+
import { ChromeColorPickerComponent } from './components/chrome-color-picker.component';
1718

1819
@NgModule({
1920
imports: [CommonModule, ColorChromeModule],
20-
exports: [ColorChromeModule],
21-
declarations: [],
22-
providers: []
21+
exports: [ColorChromeModule, ChromeColorPickerComponent],
22+
declarations: [ChromeColorPickerComponent],
23+
providers: [],
24+
entryComponents: [ChromeComponent, ChromeColorPickerComponent]
2325
})
2426
export class ColorModule {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Component, OnInit, EventEmitter } from '@angular/core';
2+
import { Store } from '@ngxs/store';
3+
import { Ink, InkColor } from '../../../ink.model';
4+
import { AddNewInk, UpdateInkColor } from '../../../store/actions/ink.action';
5+
import { Subject } from 'rxjs';
6+
7+
@Component({
8+
selector: 'inkapp-chrome-color-picker',
9+
templateUrl: 'chrome-color-picker.template.html'
10+
})
11+
export class ChromeColorPickerComponent implements OnInit {
12+
inkData: InkColor;
13+
bucketId: InkColor;
14+
private colorChanged: Subject<any> = new Subject();
15+
constructor(private store: Store) {}
16+
17+
ngOnInit() {}
18+
onColorChange(color) {
19+
this.colorChanged.next(color);
20+
return this.colorChanged.asObservable();
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<color-chrome (onChange)="onColorChange($event)"></color-chrome>

src/app/services/db.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as Datastore from 'nedb';
66
})
77
export class DBService {
88
db;
9-
constructor() {
9+
_constructor() {
1010
this.db = new Datastore({ filename: 'ink/db' });
1111
this.db.loadDatabase(e => {});
1212
const doc = {

src/app/store/states/ink.state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class InkState {
2121
state = state.map((a: any) => {
2222
if (a.id === action.inkData.id) {
2323
a.value = action.inkData.value;
24+
a.meta = action.inkData.meta;
2425
}
2526
return a;
2627
});

src/styles/vendors/__index.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
@import './bootstrap';
2-
@import './pretty-checkbox';
2+
@import './pretty-checkbox';
3+
@import './material';

src/styles/vendors/_material.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

0 commit comments

Comments
 (0)