wiki-plugin-mech 0.1.1 → 0.1.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/client/mech.js CHANGED
@@ -29,15 +29,14 @@
29
29
  return here
30
30
  }
31
31
 
32
- function format(nest,index) {
32
+ function format(nest) {
33
33
  const unique = Math.floor(Math.random()*1000000)
34
34
  const block = (more,path) => {
35
35
  const html = []
36
36
  for (part of more) {
37
37
  const key = `${unique}.${path.join('.')}`
38
- index[key] = part
39
38
  part.key = key
40
- if(part.command)
39
+ if('command' in part)
41
40
  html.push(`<span id=${key}>${expand(part.command)}</span>`)
42
41
  else
43
42
  html.push(`<div id=${key} style="padding-left:15px">${block(part,[...path,0])}</div> `)
@@ -48,48 +47,97 @@
48
47
  return block(nest,[0])
49
48
  }
50
49
 
51
- function click_emit (elem) {
52
- const body = this
53
- console.log({body,elem})
54
- if (!body.length && !elem.innerHTML.match(/button/)) {
55
- console.log('trouble')
56
- elem.innerHTML += '<button style="border-width:0;color:red;" title="nothing to run">✖︎</button>'
57
- } else
58
- if (!elem.innerHTML.match(/button/)) {
59
- elem.innerHTML += '<button style="border-width:0;">◉</button>'
60
- elem.querySelector('button').addEventListener('click',event => run(this))
61
- }
50
+ function trouble(elem,message) {
51
+ elem.innerHTML += `<button style="border-width:0;color:red;">✖︎</button>`
52
+ elem.querySelector('button').addEventListener('click',event => {
53
+ elem.outerHTML += `<span style="width:80%;color:gray;">${message}</span>` })
62
54
  }
63
55
 
64
- function click_bind (elem) {
65
- }
56
+ function click_emit ({elem,body,state}) {
57
+ if(elem.innerHTML.match(/button/)) return
58
+ if (!body?.length) return trouble(elem,'CLICK expects indented blocks to follow.')
59
+ elem.innerHTML += '<button style="border-width:0;">◉</button>'
60
+ elem.querySelector('button').addEventListener('click',event => run(body,state))
61
+ }
66
62
 
67
- function hello_emit (elem,what) {
68
- const want = what == 'world' ? ' 🌎' : ' 😀'
63
+ function hello_emit ({elem,args}) {
64
+ const want = args[0] == 'world' ? ' 🌎' : ' 😀'
69
65
  elem.innerHTML += want
70
66
  }
71
67
 
72
- function hello_bind (elem) {
68
+ function from_emit ({elem,args,body,state}) {
69
+ const line = elem.innerHTML
70
+ elem.innerHTML = line + ' ⏳'
71
+ fetch(`//${args[0]}.json`)
72
+ .then(res => res.json())
73
+ .then(page => {
74
+ state.page = page
75
+ elem.innerHTML = line + ' ⌛'
76
+ run(body,state)
77
+ })
78
+ }
79
+
80
+ function sensor_emit ({elem,args,body,state}) {
81
+ const line = elem.innerHTML.replaceAll(/ ⌛/g,'')
82
+ if(!('page' in state)) return trouble(elem,`Expect "page" as with FROM.`)
83
+ const datalog = state.page.story.find(item => item.type == 'datalog')
84
+ if(!datalog) return trouble(elem, `Expect Datalog plugin in the page.`)
85
+ const want = args[0]
86
+ if(!want) return trouble(elem, 'SENSOR needs a sensor name.')
87
+ const sensor = datalog.text.split(/\n/)
88
+ .map(line => line.split(/ +/))
89
+ .filter(fields => fields[0] == 'SENSOR')
90
+ .find(fields => fields[1] == want)
91
+ if(!sensor) return trouble(elem, `Expect to find "${want}" in Datalog.`)
92
+ const url = sensor[2]
93
+
94
+ const f = c => 9/5*(c/16)+32
95
+ const avg = a => a.reduce((s,e)=>s+e,0)/a.length
96
+ elem.innerHTML = line + ' ⏳'
97
+ fetch(url)
98
+ .then (res => res.json())
99
+ .then (data => {
100
+ elem.innerHTML = line + ' ⌛'
101
+ const value = f(avg(Object.values(data)))
102
+ state.temperature = `${value.toFixed(2)}°F`
103
+ run(body,state)
104
+ })
105
+ }
106
+
107
+ function report_emit ({elem,command,state}) {
108
+ const value = state?.temperature
109
+ if (!value) return trouble(elem,`Expect data, as from SENSOR.`)
110
+ elem.innerHTML = command + `<br><font face=Arial size=32>${value}</font>`
73
111
  }
74
112
 
75
113
  const blocks = {
76
- CLICK: {emit:click_emit, bind:click_bind},
77
- HELLO: {emit:hello_emit, bind:hello_bind}
114
+ CLICK: {emit:click_emit, bind:null},
115
+ HELLO: {emit:hello_emit, bind:null},
116
+ FROM: {emit:from_emit, bind:null},
117
+ SENSOR: {emit:sensor_emit, bind:null},
118
+ REPORT: {emit:report_emit, bind:null}
78
119
  }
79
120
 
80
- function run (nest) {
81
- console.log({nest})
121
+ function run (nest,state={}) {
82
122
  const scope = nest.slice()
83
123
  while (scope.length) {
84
124
  const code = scope.shift()
85
- if (code.command) {
125
+ if ('command' in code) {
126
+ const command = code.command
86
127
  const elem = document.getElementById(code.key)
87
128
  const [op, ...args] = code.command.split(/ +/)
88
- const more = scope[0]?.command ? null : scope.shift()
89
- console.log({op,args,more})
90
- blocks[op].emit.apply(more||[],[elem,...args])
91
- } else {
92
- run(code)
129
+ const next = scope[0]
130
+ const body = next && ('command' in next) ? null : scope.shift()
131
+ const stuff = {command,op,args,body,elem,state}
132
+ if (blocks[op])
133
+ blocks[op].emit.apply(null,[stuff])
134
+ else
135
+ if (op.match(/^[A-Z]+$/))
136
+ trouble(elem,`${op} doesn't name a block we know.`)
137
+ else if (code.command.match(/\S/))
138
+ trouble(elem, `Expected line to begin with all-caps keyword.`)
139
+ } else if(typeof code == 'array') {
140
+ run(code,state)
93
141
  }
94
142
  }
95
143
  }
@@ -97,10 +145,9 @@
97
145
  function emit($item, item) {
98
146
  const lines = item.text.split(/\n/)
99
147
  const nest = tree(lines,[],0)
100
- const index = {}
101
- const html = format(nest,index)
148
+ const html = format(nest)
102
149
  $item.append(`<div style="background-color:#eee;padding:15px;border-top:8px;">${html}</div>`)
103
- run(nest,index)
150
+ run(nest)
104
151
  }
105
152
 
106
153
  function bind($item, item) {
package/factory.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "Mech",
3
- "title": "Mech Plugin",
3
+ "title": "Script Mechanisms",
4
4
  "category": "other"
5
5
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-mech",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Federated Wiki - Mechanism Scripting Plugin",
5
5
  "keywords": [
6
6
  "mech",