@@ -3,22 +3,24 @@ import {customElement, property} from 'lit/decorators.js';
3
3
import { html } from 'lit-html' ;
4
4
import { styleMap } from 'lit-html/directives/style-map.js' ;
5
5
6
- export const COLORS = [
7
- 'white' ,
8
- 'red' ,
9
- 'orange' ,
10
- 'yellow' ,
11
- 'green' ,
12
- 'blue' ,
13
- 'indigo' ,
14
- 'violet' ,
15
- 'grey' ,
16
- 'black' ,
17
- 'lightgrey' ,
6
+ export const COLORS : Array < [ string , string ] > = [
7
+ [ 'white' , 'black' ] ,
8
+ [ 'red' , 'black' ] ,
9
+ [ 'orange' , 'black' ] ,
10
+ [ 'yellow' , 'black' ] ,
11
+ [ 'green' , 'black' ] ,
12
+ [ 'blue' , 'black' ] ,
13
+ [ 'indigo' , 'white' ] ,
14
+ [ 'violet' , 'black' ] ,
15
+ [ 'grey' , 'black' ] ,
16
+ [ 'black' , 'white' ] ,
17
+ [ 'lightgrey' , 'black' ] ,
18
+ [ 'cyan' , 'black' ] ,
18
19
] ;
19
20
20
21
@customElement ( 'e-grid' )
21
22
export class EGrid extends LitElement {
23
+ @property ( { attribute : false } ) color = '' ;
22
24
@property ( { attribute : false } )
23
25
get size ( ) : number {
24
26
return Number ( this . style . getPropertyValue ( '--size' ) ) || 8 ;
@@ -31,6 +33,8 @@ export class EGrid extends LitElement {
31
33
32
34
colors : number [ ] = [ ] ;
33
35
36
+ private lastMoveIndex = - 1 ;
37
+
34
38
constructor ( ) {
35
39
super ( ) ;
36
40
this . size = 5 ;
@@ -43,6 +47,7 @@ export class EGrid extends LitElement {
43
47
column-count: var(--size, 8);
44
48
column-gap: 0;
45
49
border: 1px solid black;
50
+ user-select: none;
46
51
}
47
52
div {
48
53
border: 1px solid black;
@@ -53,20 +58,40 @@ export class EGrid extends LitElement {
53
58
` ;
54
59
}
55
60
61
+ private getColorIndex ( index : number ) : number {
62
+ const colorIndex = COLORS . findIndex ( ( [ c , ] ) => this . color == c ) ;
63
+ if ( colorIndex >= 0 ) {
64
+ return colorIndex ;
65
+ }
66
+ return ( ( this . colors [ index ] ?? 0 ) + 1 ) % COLORS . length ;
67
+ }
68
+
69
+ private onCellMove ( event : PointerEvent , index : number ) {
70
+ if ( ! event . isPrimary || index === this . lastMoveIndex ) {
71
+ return ;
72
+ }
73
+ if ( event . pointerType === 'mouse' && event . buttons === 0 ) {
74
+ return ;
75
+ }
76
+ this . lastMoveIndex = index ;
77
+ this . onCellClick ( index ) ;
78
+ }
79
+
56
80
private onCellClick ( index : number ) {
57
- const value = ( ( this . colors [ index ] ?? 0 ) + 1 ) % COLORS . length ;
58
- this . colors [ index ] = value ;
81
+ this . colors [ index ] = this . getColorIndex ( index ) ;
59
82
this . requestUpdate ( ) ;
60
83
}
61
84
62
85
private renderCell ( index : number , colorIndex : number ) {
63
86
const style = {
64
- backgroundColor : COLORS [ colorIndex ] ,
87
+ backgroundColor : COLORS [ colorIndex ] [ 0 ] ,
65
88
} ;
66
89
return html `
67
90
< div
91
+ @pointermove =${ ( e : PointerEvent ) => this . onCellMove ( e , index ) }
68
92
@click =${ ( ) => this . onCellClick ( index ) }
69
- style =${ styleMap ( style ) } > </ div > ` ;
93
+ style=${ styleMap ( style ) }
94
+ draggable="false"> </ div > ` ;
70
95
}
71
96
72
97
override render ( ) {
0 commit comments