Skip to content

Commit bd4dea1

Browse files
committed
Progress on json editor
1 parent 370337c commit bd4dea1

File tree

20 files changed

+243
-2
lines changed

20 files changed

+243
-2
lines changed

src/pg/json/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# `<pg-json>`
2+
3+
The `pg-json` component renders json and allows values to be modified. The default data type rendering is below.
4+
5+
- `PgJsonArray`
6+
- `PgJsonObject` - `null` is hardcoded
7+
- `PgJsonString`
8+
- `PgJsonBoolean`
9+
- `PgJsonNumber`
10+
11+
```typescript
12+
import '@pictogrammers/components/pg/json';
13+
import PgIcon from '@pictogrammers/components/pg/json';
14+
```
15+
16+
```html
17+
<pg-json part="json"></pg-json>
18+
```
19+
20+
| Attributes | Tested | Default | Description |
21+
| ---------- | -------- | ------- | ----------- |
22+
| value | | `{}` | Set json data |
23+
| root | | `[]` | Root of object to render |
24+
| schema | | `null` | JSON Schema |
25+
| expand | | `0` | Default level to expand |
26+
| limit | | `10` | Arrays max items "...{total - max} view more" |
27+
| loadMore | | `10` | Amount to render after clicking load more |
28+
29+
For a root array type set `root: ['items']` and value `{ items: [] }`.
30+
31+
## Event
32+
33+
```typescript
34+
this.$json.value = {
35+
users: [{
36+
name: 'Dipper Pines',
37+
age: 12,
38+
}]
39+
}; // Array or Object
40+
this.$json.addEventListener('change', (e: any) => {
41+
const { parent, key, path, value } = e.detail;
42+
if (value !== parent[key]) {
43+
parent[key] = value;
44+
}
45+
});
46+
```
47+
48+
The `path` array gives the deep nesting for the record being modified.
49+
50+
```typescript
51+
// Modifying Dipper's name for instance
52+
path = ['users', 0, 'name'];
53+
```
54+
55+
## Schema
56+
57+
Not required if simply editing existing data. To allow complex editing add a defined schema object.
58+
59+
See https://json-schema.org for tutorials on writing your own schema.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="example">
2+
<pg-json part="json"></pg-json>
3+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Component, Part, Prop } from '@pictogrammers/element';
2+
3+
import PgJson from '../../json';
4+
5+
import template from './basic.html';
6+
7+
@Component({
8+
selector: 'x-pg-json-basic',
9+
template
10+
})
11+
export default class XPgJsonBasic extends HTMLElement {
12+
@Part() $json: PgJson;
13+
14+
connectedCallback() {
15+
this.$json.value = {
16+
users: [{
17+
name: 'Dipper Pines',
18+
age: 12,
19+
}]
20+
}; // Array or Object
21+
this.$json.addEventListener('change', (e: any) => {
22+
const { parent, key, path, value } = e.detail;
23+
if (value !== parent[key]) {
24+
parent[key] = value;
25+
}
26+
});
27+
}
28+
}

src/pg/json/json.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:host {
2+
display: inline-flex;
3+
color: var(--pg-icon-color, #222);
4+
}

src/pg/json/json.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div part="container"></div>

src/pg/json/json.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Component, Prop, Part } from '@pictogrammers/element';
2+
3+
import PgJsonArray from '../jsonArray/jsonArray';
4+
import PgJsonObject from '../jsonObject/jsonObject';
5+
6+
import template from './json.html';
7+
import style from './json.css';
8+
9+
function unwrapObject(obj: any) {
10+
return {
11+
items: Object.keys(obj).map((key) => {
12+
return {
13+
key,
14+
value: obj[key],
15+
}
16+
})
17+
};
18+
}
19+
20+
function unwrapArray(items: any) {
21+
return {
22+
items: items.map((item) => {
23+
if (Array.isArray(item)) {
24+
return unwrapArray(item);
25+
} else {
26+
return unwrapObject(item);
27+
}
28+
})
29+
};
30+
}
31+
32+
@Component({
33+
selector: 'pg-json',
34+
style,
35+
template,
36+
})
37+
export default class PgJson extends HTMLElement {
38+
@Prop() value: any = null;
39+
@Prop() root = [];
40+
@Prop() schema: any = null;
41+
42+
@Part() $container: HTMLElement;
43+
44+
render(changes) {
45+
if (changes.value && this.value !== null) {
46+
if (typeof this.value === 'object') {
47+
const $object = document.createElement('pg-json-object') as PgJsonObject;
48+
$object.items = unwrapObject(this.value).items;
49+
this.$container.appendChild($object);
50+
} else if (Array.isArray(this.value)) {
51+
const $array = document.createElement('pg-json-array') as PgJsonArray;
52+
$array.items = unwrapArray(this.value).items;
53+
this.$container.appendChild($array);
54+
}
55+
}
56+
}
57+
}

src/pg/jsonArray/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `<pg-json>`
2+
3+
The `PgJsonArray` is used in `pg-json`.

src/pg/jsonArray/jsonArray.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:host {
2+
display: inline-flex;
3+
}

src/pg/jsonArray/jsonArray.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div part="startLabel">[</div>
2+
<div part="items"></div>
3+
<div part="endLabel">]</div>

src/pg/jsonArray/jsonArray.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Component, Prop, Part } from '@pictogrammers/element';
2+
3+
import template from './json.html';
4+
import style from './json.css';
5+
6+
const noIcon = 'M0 0h24v24H0V0zm2 2v20h20V2H2z';
7+
8+
@Component({
9+
selector: 'pg-json-array',
10+
style,
11+
template,
12+
})
13+
export default class PgJsonArray extends HTMLElement {
14+
@Prop() items: any[] = [];
15+
16+
@Part() $items: HTMLElement;
17+
18+
render(changes) {
19+
20+
}
21+
}

0 commit comments

Comments
 (0)