@@ -10,10 +10,12 @@ function getFileUrl(server: string, sess: string, file: string): string {
10
10
}
11
11
interface ObsidanSageSettings {
12
12
serverURL : string
13
+ displayByDefault : boolean ,
13
14
}
14
15
15
16
const DEFAULT_SETTINGS : ObsidanSageSettings = {
16
- serverURL : "https://sagecell.sagemath.org/"
17
+ serverURL : "https://sagecell.sagemath.org/" ,
18
+ displayByDefault : true ,
17
19
}
18
20
19
21
export default class ObsidianSage extends Plugin {
@@ -29,35 +31,25 @@ export default class ObsidianSage extends Plugin {
29
31
30
32
const cell_session_id = nanoid ( ) ;
31
33
32
- //this.registerDomEvent(window, 'message', console.log);
33
-
34
34
await fetch ( this . settings . serverURL + `kernel?CellSessionID=${ cell_session_id } &timeout=inf&accepted_tos=true` , { method : "POST" } )
35
35
. then ( res => res . json ( ) )
36
36
. then ( ( { ws_url, id } ) => {
37
37
this . ws = new SockJS ( `${ this . settings . serverURL } sockjs?CellSessionID=${ cell_session_id } ` ) ;
38
38
39
- //this.ws.onmessage = console.log;
40
- this . ws . onclose = ( ) => { console . log ( "socket closed" ) ; }
39
+ this . ws . onclose = ( ) => { this . ws = null ; }
41
40
this . ws . onerror = this . connectFailed ;
42
41
43
- //this.registerInterval(window.setInterval(() => { console.log(this.ws) }, 3000));
44
-
45
42
this . ws . onmessage = ( msg : any ) =>
46
43
{
47
44
const data = JSON . parse ( msg . data . substring ( 46 ) ) ;
48
45
const msgType = data . msg_type ;
49
46
const msgId = data . parent_header . msg_id ;
50
47
const content = data . content ;
51
48
52
- console . log ( msgType , content ) ;
53
-
54
49
if ( msgType === 'error' ) {
55
50
//new Notice("Obsidian Sage evaluation error occured.")
56
51
this . outputWriters [ msgId ] . appendError ( content ) ;
57
52
}
58
- //if (msgType === 'display_data') {
59
- // console.log(content.data['text/html']);
60
- //}
61
53
62
54
if ( msgType == 'stream' && content . text ) {
63
55
this . outputWriters [ msgId ] . appendText ( content . text ) ;
@@ -66,7 +58,6 @@ export default class ObsidianSage extends Plugin {
66
58
this . outputWriters [ msgId ] . appendImage ( getFileUrl ( this . settings . serverURL , id , content . data [ 'text/image-filename' ] ) ) ;
67
59
}
68
60
if ( msgType == 'display_data' && content . data [ 'text/html' ] ) {
69
- console . log ( "got html" , content . data [ 'text/html' ] )
70
61
this . outputWriters [ msgId ] . appendInteractiveElement ( this . settings . serverURL , id , content . data [ 'text/html' ] ) ;
71
62
//this.outputWriters[msgId].appendSafeHTML(content.data['text/html']);
72
63
}
@@ -109,21 +100,8 @@ export default class ObsidianSage extends Plugin {
109
100
const code_disp = wrapper . createEl ( "pre" ) ;
110
101
code_disp . addClass ( 'sagecell-display-code' )
111
102
code_disp . innerText = src ;
112
- this . outputWriters [ req_id ] = new OutputWriter ( wrapper ) ;
103
+ this . outputWriters [ req_id ] = new OutputWriter ( wrapper , this . settings . displayByDefault ) ;
113
104
this . ws . send ( `${ session_id } /channels,${ payload } ` ) ;
114
- //console.log("\n\n\n\n\n\n\n\n\nsent a request!", payload)
115
- //wrapper.innerText = rows.join("\n");
116
- //const body = table.createEl("tbody");
117
- //
118
- //for (let i = 0; i < rows.length; i++) {
119
- // const cols = rows[i].split(",");
120
- //
121
- // const row = body.createEl("tr");
122
- //
123
- // for (let j = 0; j < cols.length; j++) {
124
- // row.createEl("td", { text: cols[j] });
125
- // }
126
- //}
127
105
} ) ;
128
106
} )
129
107
. catch ( this . connectFailed ) ;
@@ -161,16 +139,25 @@ class SettingTab extends PluginSettingTab {
161
139
containerEl . createEl ( 'h2' , { text : 'Obsidian Sage Plugin Settings' } ) ;
162
140
163
141
new Setting ( containerEl )
164
- . setName ( 'Server URL' )
165
- . setDesc ( 'A SageMathCell server that behaves like https://sagecell.sagemath.org/' )
166
- . addText ( text => text
142
+ . setName ( 'Server URL' )
143
+ . setDesc ( 'A SageMathCell server that behaves like https://sagecell.sagemath.org/' )
144
+ . addText ( text => text
167
145
. setPlaceholder ( 'https://sagecell.sagemath.org/' )
168
146
. setValue ( this . plugin . settings . serverURL )
169
147
. onChange ( async ( value : string ) => {
170
148
if ( value [ value . length - 1 ] !== '/' ) value += '/' ;
171
149
this . plugin . settings . serverURL = value ;
172
150
await this . plugin . saveSettings ( ) ;
173
151
} ) ) ;
152
+ new Setting ( containerEl )
153
+ . setName ( 'Display by Default' )
154
+ . setDesc ( 'Display the execution output by default, instead of folding it away' )
155
+ . addToggle ( val => val
156
+ . setValue ( this . plugin . settings . displayByDefault )
157
+ . onChange ( async ( value : boolean ) => {
158
+ this . plugin . settings . displayByDefault = value ;
159
+ await this . plugin . saveSettings ( ) ;
160
+ } ) ) ;
174
161
}
175
162
}
176
163
@@ -179,15 +166,18 @@ class OutputWriter {
179
166
outputEl : HTMLElement
180
167
target : HTMLElement
181
168
lastType : string
169
+ open : boolean
182
170
183
- constructor ( target : HTMLElement ) {
171
+ constructor ( target : HTMLElement , openByDefault : boolean ) {
184
172
this . target = target ;
185
173
this . lastType = "" ;
174
+ this . open = openByDefault ;
186
175
}
187
176
188
177
initOutput ( ) {
189
178
if ( this . lastType !== "" ) return ;
190
179
const output = this . target . createEl ( 'details' ) ;
180
+ if ( this . open ) output . setAttribute ( "open" , null ) ;
191
181
const summary_text = output . createEl ( 'summary' ) ;
192
182
summary_text . innerText = 'Execution Output' ;
193
183
const actual_output = output . createEl ( 'div' ) ;
@@ -225,7 +215,6 @@ class OutputWriter {
225
215
226
216
appendInteractiveElement ( server : string , sess : string , content : any ) {
227
217
this . initOutput ( ) ;
228
- console . log ( "output inneted" )
229
218
const pattern = RegExp ( 'src="cell://(.+\.html)"' )
230
219
const jankily_extracted_id = pattern . exec ( content ) [ 1 ] ;
231
220
const ratio_wrapper = document . createElement ( 'div' )
@@ -235,49 +224,48 @@ class OutputWriter {
235
224
el . addClass ( "sagecell-interactive" ) ;
236
225
ratio_wrapper . appendChild ( el ) ;
237
226
this . outputEl . appendChild ( ratio_wrapper ) ;
238
- //console.log(jankily_extracted_id, );
239
227
}
240
228
241
- appendSafeHTML ( html : string ) {
242
- this . initOutput ( ) ;
243
-
244
- // safety is cringe
245
- //const sanitized = DOMPurify.sanitize(html, { USE_PROFILES: { html: true } });
246
- //console.log("adding sanitized", sanitized);
247
- //this.outputEl.innerHTML += sanitized;
248
- this . outputEl . innerHTML += html ;
249
- this . lastType = 'html' ;
250
-
251
-
252
- //const parser = new DOMParser();
253
- //const unsafeDoc = parser.parseFromString(html, 'text/html');
254
- //
255
- ////unsafeDoc.querySelectorAll('script').forEach((scriptEl: HTMLElement) => {
256
- //// if (scriptEl.type == 'math/tex') {
257
- //// const mathEl = document.createElement('math');
258
- //// mathEl.appendChild(document.createTextNode(scriptEl.innerText));
259
- //// scriptEl.parentNode.replaceChild(mathEl, scriptEl);
260
- //// }
261
- //// });
262
- //
263
- //const safeHTML = DOMPurify.sanitize(unsafeDoc.documentElement.innerHTML);
264
- //const safeDoc = parser.parseFromString(safeHTML, 'text/html');
265
- //
266
- //console.log(safeDoc)
267
- //
268
- ////safeDoc.querySelectorAll('math').forEach((mathEl: HTMLElement) => {
269
- //// const spanEl = document.createElement('span')
270
- //// spanEl.classList = 'math math-inline';
271
- //// spanEl.appendChild(window.MathJax.tex2chtml(mathEl.textContent, {display: false}));
272
- //// mathEl.parentNode.replaceChild(spanEl, mathEl);
273
- //// });
274
- //
275
- //this.outputEl.innerHTML += safeDoc.body.innerHTML;
276
- //this.lastType = 'html';
277
- //
278
- ////window.MathJax.startup.document.clear();
279
- ////window.MathJax.startup.document.updateDocument();
280
- }
229
+ // appendSafeHTML(html: string) {
230
+ // this.initOutput();
231
+ //
232
+ // // safety is cringe
233
+ // //const sanitized = DOMPurify.sanitize(html, { USE_PROFILES: { html: true } });
234
+ // //console.log("adding sanitized", sanitized);
235
+ // //this.outputEl.innerHTML += sanitized;
236
+ // this.outputEl.innerHTML += html;
237
+ // this.lastType = 'html';
238
+ //
239
+ //
240
+ // //const parser = new DOMParser();
241
+ // //const unsafeDoc = parser.parseFromString(html, 'text/html');
242
+ // //
243
+ // ////unsafeDoc.querySelectorAll('script').forEach((scriptEl: HTMLElement) => {
244
+ // //// if (scriptEl.type == 'math/tex') {
245
+ // //// const mathEl = document.createElement('math');
246
+ // //// mathEl.appendChild(document.createTextNode(scriptEl.innerText));
247
+ // //// scriptEl.parentNode.replaceChild(mathEl, scriptEl);
248
+ // //// }
249
+ // //// });
250
+ // //
251
+ // //const safeHTML = DOMPurify.sanitize(unsafeDoc.documentElement.innerHTML);
252
+ // //const safeDoc = parser.parseFromString(safeHTML, 'text/html');
253
+ // //
254
+ // //console.log(safeDoc)
255
+ // //
256
+ // ////safeDoc.querySelectorAll('math').forEach((mathEl: HTMLElement) => {
257
+ // //// const spanEl = document.createElement('span')
258
+ // //// spanEl.classList = 'math math-inline';
259
+ // //// spanEl.appendChild(window.MathJax.tex2chtml(mathEl.textContent, {display: false}));
260
+ // //// mathEl.parentNode.replaceChild(spanEl, mathEl);
261
+ // //// });
262
+ // //
263
+ // //this.outputEl.innerHTML += safeDoc.body.innerHTML;
264
+ // //this.lastType = 'html';
265
+ // //
266
+ // ////window.MathJax.startup.document.clear();
267
+ // ////window.MathJax.startup.document.updateDocument();
268
+ // }
281
269
282
270
appendError ( error : any ) {
283
271
this . initOutput ( ) ;
0 commit comments