@@ -2,6 +2,7 @@ import R from 'ramda'
22
33import Prism from 'mastani-codehighlight'
44import { makeDebugger } from '../../utils/functions'
5+ import network from '../../utils/network'
56
67/* eslint-disable no-unused-vars */
78const debug = makeDebugger ( 'L:cheatsheetViewer' )
@@ -37,15 +38,42 @@ export const convertTaskTag = R.compose(
3738 R . replace ( / < l i > \[ x \] / g, '<li class="task-done">' )
3839)
3940
41+ const CheatsheetCDN =
42+ 'https://raw.githubusercontent.com/mydearxym/mastani-cheatsheets/master'
43+
4044export function getData ( which ) {
4145 setTimeout ( ( ) => {
42- cheatsheetViewer . SR71$ . getCheatsheet ( which )
46+ const url = `${ CheatsheetCDN } /${ which } .md`
47+ network . GET ( url ) . then ( res => {
48+ /* debug('GET ', res) */
49+ if ( res . error ) return handleError ( res )
50+
51+ let source = ''
52+ try {
53+ source = transMarkDownforRender ( res )
54+ } catch ( err ) {
55+ return handleError ( { error : 'parse_error' } )
56+ }
57+ handleLoaded ( source )
58+ } )
59+ /* cheatsheetViewer.SR71$.getCheatsheet(which) */
4360 } , 2000 )
4461 cheatsheetViewer . markState ( {
4562 state : 'loading' ,
4663 } )
4764}
4865
66+ function handleError ( res ) {
67+ switch ( res . error ) {
68+ case 404 :
69+ return handle404Error ( )
70+ case 'parse_error' :
71+ return handleParseError ( )
72+ default :
73+ debug ( res )
74+ }
75+ }
76+
4977function handleParseError ( errMsg ) {
5078 cheatsheetViewer . markState ( {
5179 current : '' ,
@@ -55,8 +83,7 @@ function handleParseError(errMsg) {
5583 Prism . highlightAll ( )
5684}
5785
58- const is404 = v => R . trim ( v ) === '404: Not Found'
59- function handle404 ( ) {
86+ function handle404Error ( ) {
6087 cheatsheetViewer . markState ( {
6188 current : '' ,
6289 state : '404' ,
@@ -75,22 +102,5 @@ function handleLoaded(source) {
75102
76103export function init ( selectedStore ) {
77104 cheatsheetViewer = selectedStore
78- // debug('cheatsheetviewer current: ', cheatsheetViewer.current)
79-
80- cheatsheetViewer . SR71$ . cheatsheet ( ) . subscribe ( res => {
81- // console.info('res: ', res)
82- if ( is404 ( res ) ) {
83- handle404 ( )
84- } else {
85- let source = ''
86-
87- try {
88- source = transMarkDownforRender ( res )
89- } catch ( err ) {
90- handleParseError ( err )
91- return false
92- }
93- handleLoaded ( source )
94- }
95- } )
105+ debug ( cheatsheetViewer )
96106}
0 commit comments