Files: 9429dd07ea740e2d002c966affa5cdaccc0d73f3 / modules_extra / raw.js
1864 bytesRaw
1 | var h = require('hyperscript') |
2 | |
3 | exports.gives = 'message_meta' |
4 | |
5 | |
6 | exports.create = function (api) { |
7 | return function (msg) { |
8 | var tmp = h('div') |
9 | var el |
10 | var pre |
11 | const symbol = '\u2699' |
12 | var clicked = false |
13 | |
14 | return h('a', { |
15 | title: 'View raw data', |
16 | style: { |
17 | order: 99, |
18 | color: 'gray', |
19 | 'font-size': '1rem', |
20 | cursor: 'pointer', |
21 | }, |
22 | onclick: function () { |
23 | clicked = !clicked |
24 | |
25 | // HACK (mw) yo we need a better way to replace the content |
26 | var msgEl = this.parentNode.parentNode |
27 | var msgContentEl = msgEl.querySelector('.\\.raw-content') |
28 | if (clicked) { |
29 | // move away the content |
30 | while (el = msgContentEl.firstChild) |
31 | tmp.appendChild(el) |
32 | // show the raw stuff |
33 | if (!pre) pre = h('pre', buildRawMsg(msg) ) |
34 | msgContentEl.appendChild(pre) |
35 | } else { |
36 | // hide the raw stuff |
37 | msgContentEl.removeChild(pre) |
38 | // put back the content |
39 | while (el = tmp.firstChild) |
40 | msgContentEl.appendChild(el) |
41 | } |
42 | } |
43 | }, symbol) |
44 | } |
45 | } |
46 | |
47 | |
48 | function buildRawMsg (msg) { |
49 | return colorKeys(linkify( |
50 | JSON.stringify({ |
51 | key: msg.key, |
52 | value: msg.value |
53 | }, 0, 2) |
54 | )) |
55 | } |
56 | |
57 | function colorKeys (chunks) { |
58 | var newArray = [] |
59 | chunks.forEach(chunk => { |
60 | if (typeof chunk !== 'string') return newArray.push(chunk) |
61 | |
62 | var arr = chunk.split(/("[^"]+":)/) |
63 | for (var i = 1; i < arr.length; i += 2) { |
64 | arr[i] = h('span', arr[i]) |
65 | } |
66 | newArray = [...newArray, ...arr] |
67 | }) |
68 | |
69 | return newArray |
70 | } |
71 | |
72 | function linkify (text) { |
73 | // from ssb-ref |
74 | var refRegex = /((?:@|%|&)[A-Za-z0-9\/+]{43}=\.[\w\d]+)/g |
75 | |
76 | var arr = text.split(refRegex) |
77 | for (var i = 1; i < arr.length; i += 2) { |
78 | arr[i] = h('a', {href: '#' + arr[i]}, arr[i]) |
79 | } |
80 | return arr |
81 | } |
82 |
Built with git-ssb-web