Skip to content

Commit eb2b277

Browse files
committed
code cleaning; setting to toggle show output by default
1 parent c102845 commit eb2b277

File tree

1 file changed

+61
-73
lines changed

1 file changed

+61
-73
lines changed

main.ts

Lines changed: 61 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ function getFileUrl(server: string, sess: string, file: string): string {
1010
}
1111
interface ObsidanSageSettings {
1212
serverURL: string
13+
displayByDefault: boolean,
1314
}
1415

1516
const DEFAULT_SETTINGS: ObsidanSageSettings = {
16-
serverURL: "https://sagecell.sagemath.org/"
17+
serverURL: "https://sagecell.sagemath.org/",
18+
displayByDefault: true,
1719
}
1820

1921
export default class ObsidianSage extends Plugin {
@@ -29,35 +31,25 @@ export default class ObsidianSage extends Plugin {
2931

3032
const cell_session_id = nanoid();
3133

32-
//this.registerDomEvent(window, 'message', console.log);
33-
3434
await fetch(this.settings.serverURL + `kernel?CellSessionID=${cell_session_id}&timeout=inf&accepted_tos=true`, { method: "POST" })
3535
.then(res => res.json())
3636
.then(({ ws_url, id }) => {
3737
this.ws = new SockJS(`${this.settings.serverURL}sockjs?CellSessionID=${cell_session_id}`);
3838

39-
//this.ws.onmessage = console.log;
40-
this.ws.onclose = () => { console.log("socket closed"); }
39+
this.ws.onclose = () => { this.ws = null; }
4140
this.ws.onerror = this.connectFailed;
4241

43-
//this.registerInterval(window.setInterval(() => { console.log(this.ws) }, 3000));
44-
4542
this.ws.onmessage = (msg: any) =>
4643
{
4744
const data = JSON.parse(msg.data.substring(46));
4845
const msgType = data.msg_type;
4946
const msgId = data.parent_header.msg_id;
5047
const content = data.content;
5148

52-
console.log(msgType, content);
53-
5449
if (msgType === 'error') {
5550
//new Notice("Obsidian Sage evaluation error occured.")
5651
this.outputWriters[msgId].appendError(content);
5752
}
58-
//if (msgType === 'display_data') {
59-
// console.log(content.data['text/html']);
60-
//}
6153

6254
if (msgType == 'stream' && content.text) {
6355
this.outputWriters[msgId].appendText(content.text);
@@ -66,7 +58,6 @@ export default class ObsidianSage extends Plugin {
6658
this.outputWriters[msgId].appendImage(getFileUrl(this.settings.serverURL, id, content.data['text/image-filename']));
6759
}
6860
if (msgType == 'display_data' && content.data['text/html']) {
69-
console.log("got html", content.data['text/html'])
7061
this.outputWriters[msgId].appendInteractiveElement(this.settings.serverURL, id, content.data['text/html']);
7162
//this.outputWriters[msgId].appendSafeHTML(content.data['text/html']);
7263
}
@@ -109,21 +100,8 @@ export default class ObsidianSage extends Plugin {
109100
const code_disp = wrapper.createEl("pre");
110101
code_disp.addClass('sagecell-display-code')
111102
code_disp.innerText = src;
112-
this.outputWriters[req_id] = new OutputWriter(wrapper);
103+
this.outputWriters[req_id] = new OutputWriter(wrapper, this.settings.displayByDefault);
113104
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-
//}
127105
});
128106
})
129107
.catch(this.connectFailed);
@@ -161,16 +139,25 @@ class SettingTab extends PluginSettingTab {
161139
containerEl.createEl('h2', {text: 'Obsidian Sage Plugin Settings'});
162140

163141
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
167145
.setPlaceholder('https://sagecell.sagemath.org/')
168146
.setValue(this.plugin.settings.serverURL)
169147
.onChange(async (value: string) => {
170148
if (value[value.length - 1] !== '/') value += '/';
171149
this.plugin.settings.serverURL = value;
172150
await this.plugin.saveSettings();
173151
}));
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+
}));
174161
}
175162
}
176163

@@ -179,15 +166,18 @@ class OutputWriter {
179166
outputEl: HTMLElement
180167
target: HTMLElement
181168
lastType: string
169+
open: boolean
182170

183-
constructor(target: HTMLElement) {
171+
constructor(target: HTMLElement, openByDefault: boolean) {
184172
this.target = target;
185173
this.lastType = "";
174+
this.open = openByDefault;
186175
}
187176

188177
initOutput() {
189178
if (this.lastType !== "") return;
190179
const output = this.target.createEl('details');
180+
if (this.open) output.setAttribute("open", null);
191181
const summary_text = output.createEl('summary');
192182
summary_text.innerText = 'Execution Output';
193183
const actual_output = output.createEl('div');
@@ -225,7 +215,6 @@ class OutputWriter {
225215

226216
appendInteractiveElement(server: string, sess: string, content: any) {
227217
this.initOutput();
228-
console.log("output inneted")
229218
const pattern = RegExp('src="cell://(.+\.html)"')
230219
const jankily_extracted_id = pattern.exec(content)[1];
231220
const ratio_wrapper = document.createElement('div')
@@ -235,49 +224,48 @@ class OutputWriter {
235224
el.addClass("sagecell-interactive");
236225
ratio_wrapper.appendChild(el);
237226
this.outputEl.appendChild(ratio_wrapper);
238-
//console.log(jankily_extracted_id, );
239227
}
240228

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+
//}
281269

282270
appendError(error: any) {
283271
this.initOutput();

0 commit comments

Comments
 (0)