enigmatic 0.11.1 → 0.11.3

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,24 @@ 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
+ const val = typeof obj[k] === 'object' ? obj[k][j] : obj[k]
45
+ html = html.replaceAll('{_key_}', j).replaceAll('{_val_}', val)
46
+ html = html.replaceAll(`{${j}}`, val)
47
+ }
48
+ htmls += html
49
+ }
50
+ return htmls
51
+ }
58
52
 
53
+ /////// Custom element
59
54
  w.element = (
60
55
  name,
61
56
  { onMount = x => x, beforeData = (x) => x, style, template = '' }
@@ -74,12 +69,9 @@ w.element = (
74
69
  if (!this.template.match('{')) this.innerHTML = this.template
75
70
  }
76
71
  set(o) {
77
- this.innerHTML = ''
78
72
  o = beforeData(o)
79
- if (!Array.isArray(o)) o = [o]
80
- const m = new Function('o', 'return `' + this.template + '`')
81
- o.map((i) => (this.innerHTML += m(i)))
82
- return true
73
+ w.flatten(o, this.template)
74
+ return o
83
75
  }
84
76
  }
85
77
  )
@@ -90,7 +82,6 @@ if (window.components) {
90
82
  }
91
83
 
92
84
  /////// State, data, and reactivity
93
-
94
85
  w.state = new Proxy({}, {
95
86
  set: (obj, prop, value) => {
96
87
  console.log(prop, value)
@@ -110,15 +101,18 @@ w.state = new Proxy({}, {
110
101
  }
111
102
  )
112
103
 
113
- w.get = async (
114
- url,
115
- options = null,
116
- transform = null,
117
- key = 'temp'
118
- ) => {
104
+ w.save = (obj, name) => {
105
+ return localStorage.setItem(name, JSON.stringify(obj))
106
+ }
107
+
108
+ w.load = (name) => {
109
+ return localStorage.getItem(name)
110
+ }
111
+
112
+ w.get = async (url, options = {}, transform, key) => {
119
113
  let data = await (await fetch(`https://${url}`, options)).json()
120
114
  if (transform) data = transform(data)
121
- state[key] = data
115
+ if (key) w.state[key] = data
122
116
  return data
123
117
  }
124
118
 
@@ -126,13 +120,12 @@ w.stream = async (url, key) => {
126
120
  const ev = new EventSource(url)
127
121
  ev.onmessage = (ev) => {
128
122
  const data = JSON.parse(ev.data)
129
- w.state[key] = data
123
+ if (key) w.state[key] = data
130
124
  return data
131
125
  }
132
126
  }
133
127
 
134
128
  /////// Startup
135
-
136
129
  w.start = async () => {
137
130
  await w.ready();
138
131
  [...$$('*')].map(e => {
@@ -151,25 +144,18 @@ w.start = async () => {
151
144
  }
152
145
  let dta = e.attr?.data
153
146
  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 = ''
147
+ console.log(`processing ${e}`)
148
+ if (e.innerHTML) {
149
+ e.template = e.innerHTML
150
+ if(e.innerHTML.match('{')) {
151
+ e.innerHTML = ''
152
+ }
160
153
  }
161
154
  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
- })
155
+ e.innerHTML = w.flatten(o, e.template)
170
156
  }
171
157
  if (e.attr?.value) {
172
- let o = e.attr.value
158
+ let o = e.attr.value
173
159
  try { o = JSON.parse(o) } catch(e) {}
174
160
  w.state[dta] = o
175
161
  }
@@ -177,7 +163,6 @@ w.start = async () => {
177
163
  })
178
164
  }
179
165
 
180
- w.enigmatic = { version: '2022-09-24 0.11.1' }
181
166
  Object.assign(window, w);
182
167
 
183
168
  (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.3",
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,33 @@
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
+ <button onclick='window.testthree()'>Test three</button>
10
+
11
+ <script type="module">
12
+ window.testone = () => {
13
+ const testone = [
14
+ { name: 'myname', value: 'myvalue' },
15
+ { name: 'myname2', value: 'myvalue2' },
16
+ ]
17
+ state.akey = testone
18
+ }
19
+ window.testtwo = () => {
20
+ const testt = {
21
+ "k1": { name: 'myname3', value: 'myvalue3' },
22
+ "k2": { name: 'myname4', value: 'myvalue4' },
23
+ }
24
+ state.akey = testt
25
+ }
26
+ window.testthree = () => {
27
+ const testt = {
28
+ "k1": 'val1',
29
+ "k2": 'val2',
30
+ }
31
+ state.akey = testt
32
+ }
33
+ </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
- }