@c2n/playground 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Nguyen Thai Vinh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Playground
2
+
3
+ # References
4
+
5
+ - JSON Pointer: https://www.rfc-editor.org/rfc/rfc6901.txt
6
+ - JSON Schema: https://json-schema.org/draft/2020-12/json-schema-core.html#name-json-schema-objects-and-key
7
+ - JSON Form: https://jsonforms.io/docs/
package/index.html ADDED
@@ -0,0 +1,21 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <link rel="icon" type="image/svg+xml" href="/vite.svg" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>Vite + Lit + TS</title>
8
+ <script type="module" src="/src/index.ts"></script>
9
+ <script src="https://cdn.tailwindcss.com"></script>
10
+ <style>
11
+ body {
12
+ padding: 24px;
13
+ }
14
+ .demo {
15
+ display: flex;
16
+ align-items: center;
17
+ }
18
+ </style>
19
+ </head>
20
+ <body></body>
21
+ </html>
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@c2n/playground",
3
+ "version": "0.0.6",
4
+ "scripts": {},
5
+ "author": "code2nguyen@gmail.com",
6
+ "publishConfig": {
7
+ "registry": "https://registry.npmjs.org",
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/code2nguyen/web-components.git"
13
+ },
14
+ "dependencies": {
15
+ "custom-elements-manifest": "2.1.0"
16
+ },
17
+ "devDependencies": {
18
+ "@types/json-schema": "7.0.15"
19
+ },
20
+ "gitHead": "2921054bc75299da66dad11c1e374246ff88a51e"
21
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "simple-form",
4
+ "type": "object",
5
+ "properties": {}
6
+ }
package/src/index.ts ADDED
File without changes
@@ -0,0 +1,31 @@
1
+ import { Package } from 'custom-elements-manifest'
2
+
3
+ type RuleEffectType = 'HIDE' | 'SHOW' | 'DISABLE' | 'ENABLE'
4
+
5
+ interface RuleCondition {
6
+ expect: unknown
7
+ }
8
+
9
+ interface RuleDefinition {
10
+ trigger: string
11
+ effect?: RuleEffectType
12
+ condition: RuleCondition
13
+ }
14
+
15
+ interface LinkDefinition {
16
+ source: string
17
+ target: string
18
+ }
19
+
20
+ interface SimpleElement {
21
+ tag: string
22
+ id: string
23
+ children?: SimpleElement[]
24
+ }
25
+
26
+ export interface FormConfig {
27
+ packages: Package[]
28
+ layout: SimpleElement
29
+ rules?: RuleDefinition[]
30
+ links?: LinkDefinition[]
31
+ }
@@ -0,0 +1,7 @@
1
+ export interface ComponentInstanceState {
2
+ [key: string]: unknown
3
+ }
4
+
5
+ export interface FormState {
6
+ [id: string]: ComponentInstanceState[]
7
+ }
@@ -0,0 +1,45 @@
1
+ import { LitElement, html, css, type TemplateResult } from 'lit'
2
+ import { customElement, property } from 'lit/decorators.js'
3
+ import type { ComponentInstanceState, FormState } from './form-state'
4
+ import type { FormConfig } from './form-config'
5
+ import type { Attribute, CustomElement } from 'custom-elements-manifest'
6
+ import { unsafeStatic } from 'lit/static-html.js'
7
+
8
+ @customElement('json-form')
9
+ export class JsonForm extends LitElement {
10
+ static override styles = [
11
+ css`
12
+ :host {
13
+ display: block;
14
+ }
15
+ `,
16
+ ]
17
+
18
+ @property({ attribute: false, type: Object }) config: FormConfig = { packages: [], layout: { tag: 'div', id: '' } }
19
+ @property({ attribute: false, type: Object }) state: FormState = {}
20
+
21
+ override render() {
22
+ return html``
23
+ }
24
+
25
+ renderComponent(component: CustomElement, state: ComponentInstanceState): TemplateResult | null {
26
+ const tag = component.tagName
27
+ const attributes = component.attributes ?? []
28
+ if (!tag) {
29
+ return null
30
+ }
31
+ return html`${unsafeStatic(tag)} ${this.renderComponentAttribute(attributes, state)}> </${unsafeStatic(tag)}>`
32
+ }
33
+
34
+ renderComponentAttribute(attributes: Attribute[], state: ComponentInstanceState) {
35
+ return attributes.map((attr) => {
36
+ const type = attr.type?.text
37
+ if (type === 'boolean') {
38
+ return html`?${unsafeStatic(type)}=${state[type]}`
39
+ } else if (type) {
40
+ return html`${unsafeStatic(type)}=${state[type]}`
41
+ }
42
+ return null
43
+ })
44
+ }
45
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "files": [],
3
+ "references": [
4
+ {
5
+ "path": "./tsconfig.lib.json"
6
+ },
7
+ {
8
+ "path": "./tsconfig.node.json"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@c2n/config/tsconfig.lib.json",
3
+ "include": ["src/**/*"],
4
+ "compilerOptions": {
5
+ "rootDir": ".",
6
+ "outDir": "./types",
7
+ "tsBuildInfoFile": "./dist/.tsbuildinfo"
8
+ }
9
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "@c2n/config/tsconfig.node.json",
3
+ "include": ["vite.config.ts"]
4
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { defineConfig } from 'vite'
2
+
3
+ // https://vitejs.dev/config/
4
+ export default defineConfig({
5
+ build: {
6
+ lib: {
7
+ entry: 'src/index.ts',
8
+ formats: ['es'],
9
+ },
10
+ rollupOptions: {
11
+ external: /^lit|@c2n/,
12
+ },
13
+ },
14
+ })