wiki-plugin-mech 0.1.26 → 0.1.27

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.
Files changed (2) hide show
  1. package/client/mech.js +143 -21
  2. package/package.json +1 -1
package/client/mech.js CHANGED
@@ -2,6 +2,7 @@
2
2
  (function() {
3
3
  "use strict"
4
4
  const uniq = (value, index, self) => self.indexOf(value) === index
5
+ const delay = time => new Promise(res => setTimeout(res,time))
5
6
 
6
7
  function expand(text) {
7
8
  return text
@@ -275,6 +276,7 @@
275
276
  if(!('neighborhood' in state)) return trouble(elem,`WALK expects state.neighborhood, like from NEIGHBORS.`)
276
277
  inspect(elem,'neighborhood',state)
277
278
  const [,count,way] = command.match(/\b(\d+)? *(steps|days|weeks|months|hubs)\b/) || []
279
+ if(!way && command != 'WALK') return trouble(elem, `WALK can't understand rest of this block.`)
278
280
  const steps = walks(count,way,state.neighborhood)
279
281
  const aspects = steps.filter(({graph})=>graph)
280
282
  if(state.debug) console.log({steps})
@@ -287,7 +289,7 @@
287
289
  state.aspect = state.aspect || []
288
290
  const obj = state.aspect.find(obj => obj.id == elem.id)
289
291
  if(obj) obj.result = aspects
290
- else state.aspect.push({id:elem.id, result:aspects})
292
+ else state.aspect.push({id:elem.id, result:aspects, source:command})
291
293
  item.classList.add('aspect-source')
292
294
  item.aspectData = () => state.aspect.map(obj => obj.result).flat()
293
295
  if(state.debug) console.log({command,state:state.aspect,item:item.aspectData()})
@@ -674,17 +676,29 @@
674
676
  elem.innerHTML = command + ` ⇒ sent`
675
677
  }
676
678
 
677
- function solo_emit({elem,command,state}) {
679
+ async function solo_emit({elem,command,state}) {
678
680
  if(!('aspect' in state)) return trouble(elem,`"SOLO" expects "aspect" state, like from "WALK".`)
679
681
  inspect(elem,'aspect',state)
682
+ elem.innerHTML = command
680
683
  const todo = state.aspect.map(each => ({
681
- source:each.id,
684
+ source:each.source || each.id,
682
685
  aspects:each.result
683
686
  }))
687
+ const aspects = todo.reduce((sum,each) => sum+each.aspects.length, 0)
688
+ elem.innerHTML += ` ⇒ ${todo.length} sources, ${aspects} aspects`
689
+
684
690
  // from Solo plugin, client/solo.js
685
691
  const pageKey = elem.closest('.page').dataset.key
686
692
  const doing = {type:'batch', sources:todo, pageKey}
687
693
  console.log({pageKey,doing})
694
+
695
+ if (typeof window.soloListener == "undefined" || window.soloListener == null) {
696
+ console.log('**** Adding solo listener')
697
+ window.soloListener = soloListener
698
+ window.addEventListener("message", soloListener)
699
+ }
700
+
701
+ await delay(750)
688
702
  const popup = window.open('/plugins/solo/dialog/#','solo','popup,height=720,width=1280')
689
703
  if (popup.location.pathname != '/plugins/solo/dialog/'){
690
704
  console.log('launching new dialog')
@@ -815,6 +829,9 @@
815
829
  const find = slug => neighborhood.find(info => info.slug == slug)
816
830
  const prob = n => Math.floor(n * Math.abs(Math.random()-Math.random()))
817
831
  const rand = a => a[prob(a.length)]
832
+ const good = info => info.links && Object.keys(info.links).length < 10
833
+ const back = slug => neighborhood.filter(info => good(info) && slug in info.links)
834
+ const newr = infos => infos.toSorted((a,b)=>b.date-a.date).slice(0,3)
818
835
  const domains = neighborhood
819
836
  .map(info => info.domain)
820
837
  .filter(uniq)
@@ -869,27 +886,32 @@
869
886
  for(const stop of dates) {
870
887
  const start = stop-interval
871
888
  const name = `${way.replace(/s$/,'')} ${new Date(start).toLocaleDateString()}`
872
- const graph = new Graph()
873
- const node = info => {
874
- return graph.addUniqNode('',{
875
- name:info.title.replaceAll(/ /g,"\n"),
876
- title:info.title,
877
- site:info.domain
878
- })
879
- }
880
889
  const here = neighborhood
881
890
  .filter(info => info.date < stop && info.date >= start)
882
891
  .filter(info => !(info.links && Object.keys(info.links).length > 5))
883
892
  if(here.length) {
884
- for (const info of here) {
885
- const nid = node(info)
886
- for (const link in (info.links||{})) {
887
- const linked = find(link)
888
- if(linked)
889
- graph.addRel('',nid,node(linked))
893
+ const domains = here.reduce((set,info) => {set.add(info.domain); return set}, new Set())
894
+ for (const domain of domains) {
895
+ const graph = new Graph()
896
+ const node = info => {
897
+ return graph.addUniqNode('',{
898
+ name:info.title.replaceAll(/ /g,"\n"),
899
+ title:info.title,
900
+ site:info.domain,
901
+ date:info.date
902
+ })
890
903
  }
904
+ const author = domain.split(/\.|\:/)[0]
905
+ for (const info of here.filter(info => info.domain == domain)) {
906
+ const nid = node(info)
907
+ for (const link in (info.links||{})) {
908
+ const linked = find(link)
909
+ if(linked)
910
+ graph.addRel('',nid,node(linked))
911
+ }
912
+ }
913
+ aspects.push({name:`${name} ${author}`,graph})
891
914
  }
892
- aspects.push({name,graph})
893
915
  }
894
916
  }
895
917
  return aspects
@@ -915,6 +937,12 @@
915
937
  .slice(0,count)
916
938
  console.log({hits,hubs})
917
939
 
940
+ // hub[0] => slug
941
+ // find(slug) => info
942
+ // node(info) => nid
943
+ // back(slug) => infos
944
+ // newr(infos) => infos
945
+
918
946
  for(const hub of hubs) {
919
947
  const name = `hub ${hub[1]} ${hub[0]}`
920
948
  const graph = new Graph()
@@ -925,18 +953,68 @@
925
953
  site:info.domain
926
954
  })
927
955
  }
956
+ // hub
928
957
  const info = find(hub[0])
929
958
  const nid = node(info)
959
+
960
+ // parents of hub
961
+ for(const parent of newr(back(info.slug))) {
962
+ graph.addRel('',node(parent),nid)
963
+ }
964
+
965
+ // hub children
930
966
  for(const link in (info.links||{})) {
931
- const linked = find(link)
932
- if(linked)
933
- graph.addRel('',nid,node(linked))
967
+ const child = find(link)
968
+ if(child) {
969
+ const cid = node(child)
970
+ graph.addRel('',nid,cid)
971
+
972
+ // parents of children
973
+ for(const parent of newr(back(child.slug))) {
974
+ graph.addRel('',node(parent),cid)
975
+ }
976
+ }
934
977
  }
978
+
979
+
935
980
  aspects.push({name,graph})
936
981
  }
937
982
  return aspects
938
983
  }
939
984
 
985
+
986
+ /*
987
+
988
+ for (const name of newest(Object.keys(info.links||{}))){
989
+ const kid = node(name)
990
+ graph.addRel('',nid,kid)
991
+
992
+ const parents = neighborhood.find(info => info.slug==name)?.cites || []
993
+ for (const parent of newest(parents))
994
+ graph.addRel('',node(parent),kid)
995
+ }
996
+
997
+ for (const name of newest(info.cites))
998
+ graph.addRel('',node(name),nid)
999
+ return graph
1000
+ }
1001
+
1002
+ function newest(slugs) {
1003
+ const recent = slug => neighborhood
1004
+ .filter(info => info.slug == slug)
1005
+ return slugs
1006
+ .map(slug => [slug,recent(slug)])
1007
+ .filter(pair => pair[1].length)
1008
+ .map(pair => [pair[0],pair[1].sort((a,b) => b.date - a.date)[0]])
1009
+ .sort((a,b) => b[1].date - a[1].date)
1010
+ .map(pair => pair[0])
1011
+ .slice(0,3)
1012
+ }
1013
+
1014
+ */
1015
+
1016
+
1017
+
940
1018
  }
941
1019
 
942
1020
 
@@ -1114,6 +1192,50 @@
1114
1192
  page.journal.push(action);
1115
1193
  }
1116
1194
 
1195
+
1196
+ // adapted from Solo client
1197
+
1198
+ function soloListener(event) {
1199
+
1200
+ if (!event.data) return
1201
+ const { data } = event
1202
+ if (data?.action == "publishSourceData" && data?.name == "aspect") {
1203
+ if (wiki.debug) console.log('soloListener - source update', {event,data})
1204
+ return
1205
+ }
1206
+
1207
+ // only continue if event is from a solo popup.
1208
+ // events from a popup window will have an opener
1209
+ // ensure that the popup window is one of ours
1210
+
1211
+ if (!event.source.opener || event.source.location.pathname !== '/plugins/solo/dialog/') {
1212
+ if (wiki.debug) {console.log('soloListener - not for us', {event})}
1213
+ return
1214
+ }
1215
+ if (wiki.debug) {console.log('soloListener - ours', {event})}
1216
+
1217
+ const { action, keepLineup=false, pageKey=null, title=null, context=null, page=null} = data;
1218
+
1219
+ let $page = null
1220
+ if (pageKey != null) {
1221
+ $page = keepLineup ? null : $('.page').filter((i, el) => $(el).data('key') == pageKey)
1222
+ }
1223
+
1224
+ switch (action) {
1225
+ case 'doInternalLink':
1226
+ wiki.pageHandler.context = context
1227
+ wiki.doInternalLink(title, $page)
1228
+ break
1229
+ case 'showResult':
1230
+ const options = keepLineup ? {} : {$page}
1231
+ wiki.showResult(wiki.newPage(page), options)
1232
+ break
1233
+ default:
1234
+ console.error({ where:'soloListener', message: "unknown action", data })
1235
+ }
1236
+ }
1237
+
1238
+
1117
1239
  function create(revIndex, data) {
1118
1240
  revIndex = +revIndex;
1119
1241
  const revJournal = data.journal.slice(0, revIndex + 1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-mech",
3
- "version": "0.1.26",
3
+ "version": "0.1.27",
4
4
  "description": "Federated Wiki - Mechanism Scripting Plugin",
5
5
  "keywords": [
6
6
  "mech",