@@ -20,18 +20,69 @@ const DEFAULT_SETTINGS: ObsidanSageSettings = {
20
20
21
21
export default class ObsidianSage extends Plugin {
22
22
ws : any ;
23
+ ws_url : any ;
24
+ session_id : any ;
23
25
settings : ObsidanSageSettings ;
24
26
outputWriters : any ;
25
27
26
28
async onload ( ) {
27
- this . outputWriters = { } ;
28
29
await this . loadSettings ( ) ;
29
30
30
31
this . addSettingTab ( new SettingTab ( this . app , this ) ) ;
31
32
33
+ new Notice ( "creating everything" ) ;
34
+ this . createListeners ( )
35
+ . then ( ( ) => {
36
+ this . registerMarkdownCodeBlockProcessor ( "sage" , ( src , el , ctx ) => {
37
+ if ( this . ws == null ) {
38
+ console . warn ( "tried to parse sage math before server connection established." ) ;
39
+ this . createListeners ( ) ;
40
+ return ;
41
+ }
42
+ console . log ( 'rendering eeeeeeeeeeeeeeeeeeeeeeeeee...' , this . ws ) ;
43
+ new Notice ( "renderingggggg" ) ;
44
+
45
+ const req_id = nanoid ( ) ;
46
+ const payload = JSON . stringify ( {
47
+ header : {
48
+ msg_id : req_id ,
49
+ username : "" ,
50
+ session : cell_session_id ,
51
+ msg_type : 'execute_request' ,
52
+ } ,
53
+ metadata : { } ,
54
+ content : {
55
+ code : src ,
56
+ silent : false ,
57
+ user_variables : [ ] ,
58
+ user_expressions : {
59
+ "_sagecell_files" : "sys._sage_.new_files()" ,
60
+ } ,
61
+ allow_stdin : false
62
+ } ,
63
+ parent_header : { }
64
+ } , null , 4 ) ;
65
+
66
+ el . addClass ( 'sagecell-display-wrapper' ) ;
67
+ const wrapper = el . createEl ( "div" ) ;
68
+ const code_disp = wrapper . createEl ( "pre" ) ;
69
+ code_disp . addClass ( 'sagecell-display-code' )
70
+ code_disp . innerText = src ;
71
+ this . outputWriters [ req_id ] = new OutputWriter ( wrapper , this . settings . displayByDefault ) ;
72
+ console . log ( 'just_before_send' , this . ws ) ;
73
+ this . ws . send ( `${ session_id } /channels,${ payload } ` ) ;
74
+ } ) ;
75
+ } )
76
+ . catch ( this . connectFailed ) ;
77
+ }
78
+ async createListeners ( ) {
32
79
const cell_session_id = nanoid ( ) ;
80
+ this . outputWriters = { } ;
81
+ this . ws = null ;
82
+ this . ws_url = null ;
83
+ this . session_id = null ;
33
84
34
- await fetch ( this . settings . serverURL + `kernel?CellSessionID=${ cell_session_id } &timeout=inf&accepted_tos=true` , { method : "POST" } )
85
+ return await fetch ( this . settings . serverURL + `kernel?CellSessionID=${ cell_session_id } &timeout=inf&accepted_tos=true` , { method : "POST" } )
35
86
. then ( res => res . json ( ) )
36
87
. then ( ( { ws_url, id } ) => {
37
88
this . ws = new SockJS ( `${ this . settings . serverURL } sockjs?CellSessionID=${ cell_session_id } ` ) ;
@@ -71,42 +122,6 @@ export default class ObsidianSage extends Plugin {
71
122
72
123
return [ ws_url , id ] ;
73
124
} )
74
- . then ( ( [ ws_url , session_id ] ) => {
75
-
76
- this . registerMarkdownCodeBlockProcessor ( "sage" , ( src , el , ctx ) => {
77
- if ( this . ws == null ) { console . warn ( "tried to parse sage math before server connection established." ) ; return ; }
78
-
79
- const req_id = nanoid ( ) ;
80
- const payload = JSON . stringify ( {
81
- header : {
82
- msg_id : req_id ,
83
- username : "" ,
84
- session : cell_session_id ,
85
- msg_type : 'execute_request' ,
86
- } ,
87
- metadata : { } ,
88
- content : {
89
- code : src ,
90
- silent : false ,
91
- user_variables : [ ] ,
92
- user_expressions : {
93
- "_sagecell_files" : "sys._sage_.new_files()" ,
94
- } ,
95
- allow_stdin : false
96
- } ,
97
- parent_header : { }
98
- } , null , 4 ) ;
99
-
100
- el . addClass ( 'sagecell-display-wrapper' ) ;
101
- const wrapper = el . createEl ( "div" ) ;
102
- const code_disp = wrapper . createEl ( "pre" ) ;
103
- code_disp . addClass ( 'sagecell-display-code' )
104
- code_disp . innerText = src ;
105
- this . outputWriters [ req_id ] = new OutputWriter ( wrapper , this . settings . displayByDefault ) ;
106
- this . ws . send ( `${ session_id } /channels,${ payload } ` ) ;
107
- } ) ;
108
- } )
109
- . catch ( this . connectFailed ) ;
110
125
}
111
126
connectFailed ( e : any ) {
112
127
console . error ( e ) ;
0 commit comments