enigmatic 0.11.1 → 0.11.2

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/components.js CHANGED
@@ -13,4 +13,4 @@ window.components = {
13
13
  template: '<div class="bg-blue-300 text-white font-bold py-2 px-4 rounded">I am Tailwind</div>',
14
14
  onMount: async e => await loadCSS('https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css')
15
15
  }
16
- }
16
+ }
package/enigmatic.css CHANGED
@@ -1,8 +1,34 @@
1
- body {
2
- font-family: var(--font-primary, -apple-system, BlinkMacSystemFont, sans-serif);
3
- height: 100vh;
4
- background-color: var(--bg-color, #fff);
5
- padding: .5rem;
1
+ html {
2
+ max-width: 70ch;
3
+ /* larger spacing on larger screens, very small spacing on tiny screens */
4
+ padding: calc(1vmin + .5rem);
5
+ /* shorthand for margin-left/margin-right */
6
+ margin-inline: auto;
7
+ /* fluid sizing: https://frontaid.io/blog/fluid-typography-2d-css-locks-clamp/ */
8
+ font-size: clamp(1em, 0.909em + 0.45vmin, 1.25em);
9
+ /* use system font stack: https://developer.mozilla.org/en-US/docs/Web/CSS/font-family */
10
+ font-family: system-ui
11
+ }
12
+
13
+ /* increase line-height for everything except headings */
14
+ body :not(:is(h1, h2, h3, h4, h5, h6)) {
15
+ line-height: 1.75;
16
+ }
17
+
18
+ h1,
19
+ h2,
20
+ h3,
21
+ h4,
22
+ h5,
23
+ h6 {
24
+ margin: 3em 0 1em;
25
+ }
26
+
27
+ p,
28
+ ul,
29
+ ol {
30
+ margin-bottom: 2em;
31
+ color: #1d1d1d;
6
32
  }
7
33
 
8
34
  body,
@@ -48,9 +74,14 @@ section {
48
74
  top: 0;
49
75
  }
50
76
 
51
- .bottom { bottom: 0 }
77
+ .bottom {
78
+ bottom: 0
79
+ }
52
80
 
53
- .fill { height: 100vh; width: 100wh }
81
+ .fill {
82
+ height: 100vh;
83
+ width: 100wh
84
+ }
54
85
 
55
86
  .hide {
56
87
  opacity: 0;
package/enigmatic.js CHANGED
@@ -1,26 +1,5 @@
1
- /*
2
- enigmatic v 0.11.0 front end js utils
3
-
4
- Usage:
5
- <div id='mykey' transform='func1' fetch='some.site/data' immediate>${name} ${value}</div>
6
- <div id='mykey2' transform='func2' stream='some.site/data'>${name} ${value}</div>
7
-
8
- $('selector')
9
- $$('selector')
10
- await loadJS('sounds.js')
11
- await loadCSS('enigmatic.css')
12
- await wait(1000)
13
- await ready()
14
- beep()
15
- element(beforeData=>beforeData.field, '<div>${o.mykey}</div>')
16
- window.components
17
- state
18
- await get
19
- await stream
20
- */
21
-
22
- const w = {},
23
- d = document
1
+ const w = {}, d = document
2
+ w.enigmatic = { version: '2022-09-24 0.11.2' }
24
3
 
25
4
  /////// Helpers
26
5
 
@@ -54,8 +33,23 @@ w.ready = async () => {
54
33
  })
55
34
  }
56
35
 
57
- /////// Custom element
36
+ // Template a string, using {$key}, {_key_}, {_val_}
37
+ // ie.. {key1: {name: 'value'}, key2: {name: 'value2'}} OR [{name: 'value'}, {name: 'value2'}]
38
+ w.flatten = (obj, text) => {
39
+ let htmls = ''
40
+ if (obj instanceof Array) obj = { ...obj }
41
+ for (let k in obj) {
42
+ let html = text.replaceAll('{$key}', k)
43
+ for (let j in obj[k]) {
44
+ html = html.replaceAll('{_key_}', j).replaceAll('{_val_}', obj[k][j])
45
+ html = html.replaceAll(`{${j}}`, obj[k][j])
46
+ }
47
+ htmls += html
48
+ }
49
+ return htmls
50
+ }
58
51
 
52
+ /////// Custom element
59
53
  w.element = (
60
54
  name,
61
55
  { onMount = x => x, beforeData = (x) => x, style, template = '' }
@@ -90,7 +84,6 @@ if (window.components) {
90
84
  }
91
85
 
92
86
  /////// State, data, and reactivity
93
-
94
87
  w.state = new Proxy({}, {
95
88
  set: (obj, prop, value) => {
96
89
  console.log(prop, value)
@@ -110,15 +103,18 @@ w.state = new Proxy({}, {
110
103
  }
111
104
  )
112
105
 
113
- w.get = async (
114
- url,
115
- options = null,
116
- transform = null,
117
- key = 'temp'
118
- ) => {
106
+ w.save = (obj, name) => {
107
+ return localStorage.setItem(name, JSON.stringify(obj))
108
+ }
109
+
110
+ w.load = (name) => {
111
+ return localStorage.getItem(name)
112
+ }
113
+
114
+ w.get = async (url, options = {}, transform, key) => {
119
115
  let data = await (await fetch(`https://${url}`, options)).json()
120
116
  if (transform) data = transform(data)
121
- state[key] = data
117
+ if (key) w.state[key] = data
122
118
  return data
123
119
  }
124
120
 
@@ -126,13 +122,12 @@ w.stream = async (url, key) => {
126
122
  const ev = new EventSource(url)
127
123
  ev.onmessage = (ev) => {
128
124
  const data = JSON.parse(ev.data)
129
- w.state[key] = data
125
+ if (key) w.state[key] = data
130
126
  return data
131
127
  }
132
128
  }
133
129
 
134
130
  /////// Startup
135
-
136
131
  w.start = async () => {
137
132
  await w.ready();
138
133
  [...$$('*')].map(e => {
@@ -151,25 +146,18 @@ w.start = async () => {
151
146
  }
152
147
  let dta = e.attr?.data
153
148
  if (dta) {
154
- if(dta.endsWith('[]')) {
155
- dta = dta.replace('[]', '')
156
- }
157
- if (e.innerHTML && e.innerHTML.includes('{')) {
158
- e.template = e.innerHTML.replaceAll('{', '${')
159
- e.innerHTML = ''
149
+ console.log(`processing ${e}`)
150
+ if (e.innerHTML) {
151
+ e.template = e.innerHTML
152
+ if(e.innerHTML.match('{')) {
153
+ e.innerHTML = ''
154
+ }
160
155
  }
161
156
  e.set = (o) => {
162
- e.innerHTML = ''
163
- const template = e.template.replaceAll('{', '{rec.')
164
- if (!Array.isArray(o)) o = [o]
165
- const f = new Function('rec', 'return `' + template + '`')
166
- o.map(rec => {
167
- if(typeof rec !== 'object') rec = {value: rec}
168
- e.innerHTML += f(rec)
169
- })
157
+ e.innerHTML = w.flatten(o, e.template)
170
158
  }
171
159
  if (e.attr?.value) {
172
- let o = e.attr.value
160
+ let o = e.attr.value
173
161
  try { o = JSON.parse(o) } catch(e) {}
174
162
  w.state[dta] = o
175
163
  }
@@ -177,7 +165,6 @@ w.start = async () => {
177
165
  })
178
166
  }
179
167
 
180
- w.enigmatic = { version: '2022-09-24 0.11.1' }
181
168
  Object.assign(window, w);
182
169
 
183
170
  (async () => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "enigmatic",
3
3
  "main": "enigmatic.js",
4
- "version": "0.11.1",
4
+ "version": "0.11.2",
5
5
  "scripts": {
6
6
  "test": "node test.mjs"
7
7
  },
@@ -0,0 +1,16 @@
1
+ <script>
2
+ customElements.define('is-online', class IsOnline extends HTMLElement {
3
+ connectedCallback() {
4
+ this.setAttribute('rel', 'icon')
5
+ this.setAttribute('href', "data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🎯</text></svg>")
6
+ }
7
+ })
8
+ </script>
9
+
10
+ <link rel="stylesheet" href="enigmatic.css">
11
+ <body style="--cols:1fr 6fr 1fr; --rows:1fr 10fr">
12
+ <my-e>hey</my-e>
13
+ <my-e>hey</my-e>
14
+ <my-e>hey</my-e>
15
+ <my-e>hey</my-e>
16
+ <my-e>hey</my-e>
@@ -0,0 +1,25 @@
1
+ <script src='enigmatic.js'></script>
2
+
3
+ <div id='templ' data="akey">
4
+ {$key}: {_key_} : {_val_}, name = {name}, value = {value} <br>
5
+ </div>
6
+
7
+ <button onclick='window.testone()'>Test one</button>
8
+ <button onclick='window.testtwo()'>Test two</button>
9
+
10
+ <script type="module">
11
+ window.testone = () => {
12
+ const testone = [
13
+ { name: 'myname', value: 'myvalue' },
14
+ { name: 'myname2', value: 'myvalue2' },
15
+ ]
16
+ state.akey = testone
17
+ }
18
+ window.testtwo = () => {
19
+ const testt = {
20
+ "k1": { name: 'myname3', value: 'myvalue3' },
21
+ "k2": { name: 'myname4', value: 'myvalue4' },
22
+ }
23
+ state.akey = testt
24
+ }
25
+ </script>
File without changes
@@ -0,0 +1,13 @@
1
+ <link rel=stylesheet href=enigmatic.css>
2
+
3
+ <body style="--cols:1fr 1fr 1fr 10fr; --rows:1fr 10fr">
4
+ <div class="bg-red" style="--span:2">Hello</div>
5
+ <div class="bg-green"></div>
6
+ <div class="bg-yellow"></div>
7
+ <div class="bg-blue"></div>
8
+ <div class="bg-red"></div>
9
+ <div class="bg-green"></div>
10
+ <div class="bg-yellow"></div>
11
+ <div class="bg-blue"></div>
12
+ <div class="bg-red"></div>
13
+ </body>
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+
3
+ <script src=enigmatic.js></script>
4
+ <script type=module src=https://unpkg.com/e-schema@0.0.2/module.mjs></script>
5
+
6
+ <script type="module">
7
+ const es = new eSchema({MyType: {message: 'string'}})
8
+ es.validate({message: 'you are'}, 'MyType')
9
+ save(es.schema, 'myschema')
10
+ console.log(load('myschema'))
11
+ console.log(load('nothing'))
12
+ </script>
@@ -1,15 +0,0 @@
1
- {
2
- // Use IntelliSense to learn about possible attributes.
3
- // Hover to view descriptions of existing attributes.
4
- // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
- "version": "0.2.0",
6
- "configurations": [
7
- {
8
- "type": "chrome",
9
- "request": "launch",
10
- "name": "Launch Chrome against localhost",
11
- "url": "http://localhost:8080/test.html",
12
- "webRoot": "${workspaceFolder}"
13
- }
14
- ]
15
- }