Skip to content

Commit 9fc8682

Browse files
committed
progress
1 parent 956cb96 commit 9fc8682

2 files changed

Lines changed: 43 additions & 14 deletions

File tree

src/components/pg/inputPixelEditor/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ See usage for each method below.
4646
| Method | Tested | Description |
4747
| ---------- | -------- | ----------- |
4848
| `getJson(options)` | - | Get JSON file. |
49+
| `getData()` | - | Get layer data. |
4950
| `setJson(json)` | - | Set JSON file. |
5051
| `reset()` | - | Reset canvas and data. |
5152
| `getExportCanvas()` | - | Get new canvas of export. |
@@ -98,7 +99,7 @@ See usage for each method below.
9899

99100
### `getJson(options)` Method
100101

101-
The `getJson` method allows getting the JSON representation of the current editor. Optionally this can snapshot the entire undo history.
102+
The `getJson` method allows getting the JSON representation of the current editor. Optionally this can snapshot the entire undo history. To get only data by layer call `getData()`.
102103

103104
```typescript
104105
@Part() $editor: PgInputPixelEditor;
@@ -173,8 +174,8 @@ A complete JSON storage for a 10x10 image.
173174
],
174175
"data": [
175176
[
176-
[1, "M...Z"],
177-
[2, "M...Z"]
177+
[{ "color": 1, "path": "M...Z" }],
178+
[{ "color": 2, "path": "M...Z" }]
178179
]
179180
],
180181
"history": []
@@ -220,8 +221,8 @@ The most common layer type.
220221
{
221222
"data": [
222223
[
223-
[{ "color": 1, "path": "M...Z" }],
224-
[{ "color": 2, "path": "M...Z" }]
224+
{ "color": 1, "path": "M...Z" },
225+
{ "color": 2, "path": "M...Z" }
225226
]
226227
]
227228
}

src/components/pg/inputPixelEditor/inputPixelEditor.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ type HistoryItem = {
6464
type History = HistoryItem[];
6565

6666
type Layer = {
67-
name: string,
68-
visible: boolean,
69-
locked: boolean,
70-
opacity: number,
67+
name: string
68+
type: 'pixel' | 'reference'
69+
visible: boolean
70+
locked: boolean
71+
opacity: number
7172
export: boolean
7273
}
7374

@@ -87,11 +88,11 @@ interface File {
8788
}
8889

8990
interface Export {
90-
scale?: number;
91-
x?: number;
92-
y?: number;
93-
width?: number;
94-
height?: number;
91+
scale?: number
92+
x?: number
93+
y?: number
94+
width?: number
95+
height?: number
9596
}
9697

9798
function toColor([r, g, b, a]: Color) {
@@ -256,6 +257,7 @@ export default class PgInputPixelEditor extends HTMLElement {
256257
this.#layer = [0];
257258
this.#layers = [{
258259
name: 'Layer 1',
260+
type: 'pixel',
259261
export: true,
260262
locked: false,
261263
visible: true,
@@ -1369,6 +1371,7 @@ export default class PgInputPixelEditor extends HTMLElement {
13691371
this.#data.push(fillGrid(this.width, this.height));
13701372
this.#layers.push({
13711373
name: 'Layer 1',
1374+
type: 'pixel',
13721375
export: true,
13731376
locked: false,
13741377
visible: true,
@@ -1435,6 +1438,31 @@ export default class PgInputPixelEditor extends HTMLElement {
14351438
return this.#getLayerPaths() as any;
14361439
}
14371440

1441+
/**
1442+
* Save schema for `data`.
1443+
*/
1444+
getData() {
1445+
return this.#layers.map((layer, layerIndex) => {
1446+
switch (layer.type) {
1447+
case 'pixel':
1448+
const colors = this.getLayerColorIndexes(layerIndex);
1449+
return colors.map((color) => {
1450+
return {
1451+
color,
1452+
path: bitmaskToPath(this.#data[layerIndex], {
1453+
scale: 1,
1454+
include: [color]
1455+
}),
1456+
};
1457+
});
1458+
case 'reference':
1459+
return { "id": "uuid", "position": [0, 0] }
1460+
default:
1461+
throw new Error('Not implemented');
1462+
}
1463+
});
1464+
}
1465+
14381466
getExportPath() {
14391467
return bitmaskToPath(this.#export, { scale: 1 });
14401468
}

0 commit comments

Comments
 (0)