1- import { Component , Prop , Part , normalizeInt } from '@pictogrammers/element' ;
1+ import { Component , Prop , Part , normalizeInt , normalizeBoolean } from '@pictogrammers/element' ;
22
33import template from './inputPixelEditor.html' ;
44import style from './inputPixelEditor.css' ;
@@ -71,6 +71,7 @@ export default class PgInputPixelEditor extends HTMLElement {
7171 @Prop ( normalizeInt ) height : number = 10 ;
7272 @Prop ( normalizeInt ) size : number = 10 ;
7373 @Prop ( normalizeInt ) gridSize : number = 1 ;
74+ @Prop ( normalizeBoolean ) transparent : boolean = false ;
7475 @Prop ( ) placeholder : string = '' ;
7576
7677 @Part ( ) $canvas : HTMLCanvasElement ;
@@ -175,17 +176,37 @@ export default class PgInputPixelEditor extends HTMLElement {
175176 } ] ;
176177 this . #data = [ fillGrid ( this . width , this . height ) ] ;
177178 this . #reset = false ;
179+ this . #undoHistory = [ ] ;
180+ this . #redoHistory = [ ] ;
181+ } else {
182+ this . #redraw( ) ;
178183 }
179- this . #redraw( ) ;
180184 }
181185
182186 #redraw( ) {
183187 // Render individual pixels
184- this . #data[ this . #layer] . forEach ( ( row , y ) => {
185- row . forEach ( ( cell , x ) => {
186- this . #setPixel( x , y , cell ) ;
187- } ) ;
188- } ) ;
188+ const data = this . #data. toReversed ( ) ;
189+ const layerCount = data . length ;
190+ for ( let y = 0 ; y < this . height ; y ++ ) {
191+ if ( y >= data [ 0 ] . length ) {
192+ for ( let l = 0 ; l < layerCount ; l ++ ) {
193+ data [ l ] . push ( new Array ( this . width ) . fill ( 0 ) ) ;
194+ }
195+ }
196+ for ( let x = 0 ; x < this . width ; x ++ ) {
197+ if ( x >= data [ 0 ] [ y ] . length ) {
198+ for ( let l = 0 ; l < layerCount ; l ++ ) {
199+ data [ l ] [ y ] . push ( 0 ) ;
200+ }
201+ }
202+ for ( let l = 0 ; l < layerCount ; l ++ ) {
203+ if ( data [ l ] [ y ] [ x ] !== 0 ) {
204+ this . #setPixel( x , y , data [ l ] [ y ] [ x ] ) ;
205+ break ;
206+ }
207+ }
208+ }
209+ }
189210 }
190211
191212 #handleChange( ) {
@@ -526,6 +547,7 @@ export default class PgInputPixelEditor extends HTMLElement {
526547 mergeColor ( fromIndex : number , toIndex : number ) {
527548 // ToDo: Code this
528549 }
550+
529551 clear ( ) {
530552 const gridEmpty = fillGrid ( this . width , this . height ) ;
531553 const diff = diffGrid ( this . #data[ this . #layer] , gridEmpty ) ;
@@ -545,13 +567,21 @@ export default class PgInputPixelEditor extends HTMLElement {
545567 this . #data = [ fillGrid ( this . width , this . height ) ] ;
546568 this . #updateGrid( ) ;
547569 }
570+
571+ reset ( ) {
572+ this . #reset = true ;
573+ this . #init( ) ;
574+ }
575+
548576 clearHistory ( ) {
549577 this . #undoHistory = [ ] ;
550578 this . #redoHistory = [ ] ;
551579 }
580+
552581 applyTemplate ( template : number [ ] [ ] ) {
553582 this . #data = [ template ] ;
554583 }
584+
555585 flipHorizontal ( ) {
556586 const cloned = cloneGrid ( this . #data[ this . #layer] ) ;
557587 const w = cloned [ 0 ] . length - 1 ;
@@ -606,14 +636,14 @@ export default class PgInputPixelEditor extends HTMLElement {
606636 // ToDo: Rewrite to use new history api
607637 const revert = this . #undoHistory. pop ( ) ;
608638 if ( ! revert ) { return ; }
609- switch ( revert . type ) {
639+ switch ( revert . type ) {
610640 case HistoryType . Pixel :
611- this . #redoHistory. push ( revert ) ;
612- ( revert . data as HistoryPixelType ) . pixels . forEach ( ( item ) => {
613- const [ x , y ] = item ;
614- this . #data[ this . #layer] [ y ] [ x ] = item [ 2 ] ;
615- // redraw canvas
616- } ) ;
641+ this . #redoHistory. push ( revert ) ;
642+ ( revert . data as HistoryPixelType ) . pixels . forEach ( ( item ) => {
643+ const [ x , y ] = item ;
644+ this . #data[ this . #layer] [ y ] [ x ] = item [ 2 ] ;
645+ // redraw canvas
646+ } ) ;
617647 break ;
618648 }
619649 }
0 commit comments