|
42 | 42 | return fetch(`${api}/${path}`).then( |
43 | 43 | (resp)=>this.normalize(resp) |
44 | 44 | ).catch(data=>{ |
45 | | - console.log(data); |
46 | | - sel('header > code') |
47 | | - .textContent = data.statusText + ' ' + data.url; |
| 45 | + document.dispatchEvent( |
| 46 | + new CustomEvent('gohub-catch', {detail: data }) |
| 47 | + ); |
48 | 48 | return data; |
49 | 49 | }); |
50 | 50 | } |
|
67 | 67 | slice(1).toLowerCase(), |
68 | 68 | text = Base64.decode(json.content), |
69 | 69 | content = ext === 'json' && |
70 | | - JSON.parse(text) || text; |
71 | | - |
72 | | - return storeCache( |
73 | | - url, { |
74 | | - owner, repo, |
75 | | - name: json.name, |
76 | | - url: json.html_url, |
77 | | - sha: json.sha, |
78 | | - path, |
79 | | - ext, |
80 | | - content, |
81 | | - } |
| 70 | + JSON.parse(text) || text, |
| 71 | + data = storeCache( |
| 72 | + url, { |
| 73 | + owner, repo, |
| 74 | + name: json.name, |
| 75 | + url: json.html_url, |
| 76 | + sha: json.sha, |
| 77 | + path, |
| 78 | + ext, |
| 79 | + content, |
| 80 | + } |
| 81 | + ); |
| 82 | + document.dispatchEvent( |
| 83 | + new CustomEvent('gohub-got', {detail: data }) |
82 | 84 | ); |
| 85 | + return data; |
83 | 86 | }); |
84 | 87 | } |
85 | 88 | } |
|
88 | 91 | if (!owner) return gohub.prototype; |
89 | 92 | return new gohub(owner, repo); |
90 | 93 | }; |
| 94 | +}(this)); |
91 | 95 |
|
| 96 | +(function (global) { |
92 | 97 | global.all = function (s, node) { |
93 | 98 | return (node || document).querySelectorAll(s); |
94 | 99 | }; |
|
98 | 103 | }; |
99 | 104 |
|
100 | 105 | global.on = function (type, listener, useCapture) { |
101 | | - let node = this instanceof Node && this || document.body; |
| 106 | + let node = this instanceof Node && this || document; |
102 | 107 | node.addEventListener(type, listener, useCapture); |
103 | 108 | }; |
104 | 109 |
|
105 | 110 | global.emit = function (type, detail) { |
106 | | - let node = this instanceof Node && this || document.body; |
| 111 | + let node = this instanceof Node && this || document; |
107 | 112 | node.dispatchEvent( |
108 | 113 | new CustomEvent(type, {detail: detail }) |
109 | 114 | ); |
110 | 115 | }; |
111 | | - |
112 | 116 | global.is = { |
113 | 117 | array:(v)=>Array.isArray(v), |
114 | 118 | object:(v)=>Object.prototype.toString.call(v) === '[object Object]', |
115 | 119 | string:(v)=>typeof v === 'string', |
116 | 120 | }; |
117 | | - |
118 | 121 | }(this)); |
| 122 | + |
119 | 123 | (function (global) { |
120 | 124 | let tmpl = PowJS(sel('template')); |
121 | 125 |
|
122 | 126 | on.call(sel('#panel'), 'update', (event)=> { |
123 | 127 | tmpl.render(event.detail).removeChilds(); |
124 | 128 | }); |
| 129 | + |
125 | 130 | on.call(sel('#panel'), 'click',(event)=> { |
126 | | - let target = event.target; |
127 | | - if (target.matches('summary')) { |
128 | | - if (target.nextElementSibling) return; |
129 | | - let repo = target.closest('details').getAttribute('repo'); |
130 | | - gohub(repo).list().then(data=>tmpl.render(data).removeChilds()); |
| 131 | + let |
| 132 | + repo, path, target = event.target; |
| 133 | + if (target.matches('summary')){ |
| 134 | + repo = !target.nextElementSibling && getRepo(target); |
| 135 | + path = 'contents/golist.json'; |
| 136 | + }else if (target.matches('p')) { |
| 137 | + repo = getRepo(target); |
| 138 | + path = getContentPath(target); |
131 | 139 | } |
| 140 | + if(repo) gohub(repo).get(path||''); |
132 | 141 | }); |
| 142 | + |
| 143 | + function getRepo(n) { |
| 144 | + return n.closest('details').getAttribute('repo'); |
| 145 | + } |
| 146 | + |
| 147 | + function getContentPath(n) { |
| 148 | + let p = n.closest('details'); |
| 149 | + return `contents/${p.getAttribute('subdir')}/${n.textContent}/${p.getAttribute('filename')}`; |
| 150 | + } |
133 | 151 | }(this)); |
| 152 | + |
134 | 153 | (function (global) { |
135 | 154 |
|
136 | 155 | let |
|
145 | 164 | autoCloseBrackets: true, |
146 | 165 | matchBrackets: true, |
147 | 166 | showCursorWhenSelecting: true, |
148 | | - theme: "twilight", |
149 | | - tabSize: 2 |
| 167 | + theme: "chrome-devtools", |
| 168 | + tabSize: 2, |
| 169 | + readOnly: true |
150 | 170 | }), |
151 | 171 | split = {}; |
152 | 172 |
|
|
159 | 179 | }); |
160 | 180 |
|
161 | 181 | CodeMirror.modeURL = '//unpkg.com/[email protected]/mode/%N/%N.js'; |
162 | | - CodeMirror.autoLoadMode(editor,'go'); |
| 182 | + CodeMirror.autoLoadMode(editor, 'go'); |
163 | 183 |
|
164 | | - if (location.hostname !== 'localhost') |
165 | | - global.addEventListener('beforeunload', function (e) { |
166 | | - e.returnValue = '\o/'; |
167 | | - }); |
| 184 | + // if (location.hostname !== 'localhost') |
| 185 | + // global.addEventListener('beforeunload', function (e) { |
| 186 | + // e.returnValue = '\o/'; |
| 187 | + // }); |
168 | 188 |
|
169 | | - let gh = gohub('gohub/gohub.github.io'); |
| 189 | + on('gohub-got', event=>render(event.detail)); |
170 | 190 |
|
171 | | - fetch('/README.md?'+Date.now()).then((resp)=>{ |
172 | | - return resp.text().then((content)=>( |
173 | | - {content,ext: 'md',url:'README.md'} |
174 | | - )).then(data =>render(data)); |
175 | | - }); |
| 191 | + on('gohub-catch', (event)=>{ |
| 192 | + sel('header code').textContent = |
| 193 | + event.detail.statusText + ': ' + event.detail.url; |
| 194 | + }, true); |
| 195 | + |
| 196 | + let gh = gohub('gohub/gohub.github.io'); |
176 | 197 |
|
177 | | - gh.list().then(data =>render(data)); |
| 198 | + gh.readme(); |
| 199 | + gh.list(); |
178 | 200 |
|
179 | 201 | function render(data) { |
180 | 202 | let content = data && data.content; |
181 | | - if(!content) return; |
| 203 | + if(!content) |
| 204 | + return console.log('Oop!'); |
182 | 205 |
|
183 | 206 | if (typeof content !== 'string') |
184 | 207 | return emit.call(sel('#panel'), 'update', data); |
|
193 | 216 | preview.querySelectorAll('pre code[class*=lang-]') |
194 | 217 | .forEach((e)=>hljs.highlightBlock(e)); |
195 | 218 | } else { |
196 | | - editor.setValue(content); |
197 | | - editor.focus(); |
198 | 219 | main.setAttribute('class',''); |
| 220 | + editor.focus(); |
| 221 | + editor.setValue(content); |
199 | 222 | } |
200 | 223 | } |
201 | 224 |
|
|
0 commit comments