Skip to content

Commit ccfc0c5

Browse files
committed
Match inputs and outputs
1 parent 59542e5 commit ccfc0c5

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

ToDo.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
ToDo
33
====
4-
- [ ] Match inputs and outputs
5-
- [x] ~~Change worker to modules~~ (tried several attempts can't make it work wit coolprop.wasm and vite)
64
- [ ] Details
75
- [x] Cleanup
86
- [x] JS docs
97
- [ ] Rename variables
108
- [ ] Sessions to Editor States
9+
- [x] Match inputs and outputs
10+
- [x] ~~Change worker to modules~~ (tried several attempts can't make it work wit coolprop.wasm and vite)
1111

1212
# Plots
1313
- [x] Import plotly
@@ -37,6 +37,6 @@ ToDo
3737
- [ ] Tabs
3838
- [ ] Examples
3939
- [ ] IO
40-
- [ ] New nomeclature like iodide
40+
- [ ] New nomenclature like iodide
4141
- [ ] File names
4242
- [ ] Dropdown or tabs

main.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ mathWorker.onmessage = function (oEvent) {
111111
case "math":
112112
out.text.forEach(e => {
113113
const pre = document.createElement("pre");
114+
pre.setAttribute('data-from-line', e.from);
115+
pre.setAttribute('data-to-line', e.to);
114116
if (e.visible) {
115117
const type = e.type;
116118
const value = e.result;
@@ -119,29 +121,21 @@ mathWorker.onmessage = function (oEvent) {
119121
case "string":
120122
div = document.createElement("code");
121123
div.innerHTML = value;
122-
div.setAttribute('data-from-line', e.from)
123-
div.setAttribute('data-to-line', e.to)
124124
pre.appendChild(div);
125125
break;
126126
case "any":
127127
div = document.createElement("div");
128128
div.textContent = value;
129-
div.setAttribute('data-from-line', e.from)
130-
div.setAttribute('data-to-line', e.to)
131129
pre.appendChild(div);
132130
break;
133131
case "error":
134132
div = document.createElement("div");
135133
div.style.color = "red";
136134
div.innerHTML = value;
137-
div.setAttribute('data-from-line', e.from)
138-
div.setAttribute('data-to-line', e.to)
139135
pre.appendChild(div);
140136
break;
141137
case "plot":
142138
div = document.createElement("div");
143-
div.setAttribute('data-from-line', e.from)
144-
div.setAttribute('data-to-line', e.to)
145139
try {
146140
Plotly.newPlot(div, e.result.data, e.result.layout, e.result.config)
147141
} catch (error) {
@@ -150,8 +144,10 @@ mathWorker.onmessage = function (oEvent) {
150144
pre.appendChild(div);
151145
break;
152146
}
153-
outputs.appendChild(pre);
147+
} else {
148+
pre.style.display = 'none'
154149
}
150+
outputs.appendChild(pre);
155151
});
156152
break;
157153
case "markdown":
@@ -163,6 +159,7 @@ mathWorker.onmessage = function (oEvent) {
163159
break;
164160
}
165161
});
162+
updateSelection()
166163
clearTimeout(timerSave);
167164
sessions[lastTab] = editor.state
168165
timerSave = setTimeout(saveSession, waitToSave, tabToSave)
@@ -187,12 +184,42 @@ function createState(ID) {
187184
const text = update.state.doc.toString()
188185
clearTimeout(timer);
189186
timer = setTimeout(sendWorkToMathWorker, wait, text);
187+
} else if (update.selectionSet) {
188+
updateSelection()
190189
}
191190
})
192191
]
193192
})
194193
}
195194

195+
function updateSelection() {
196+
const selectedFrom = editor.state.doc.lineAt(
197+
editor.state.selection.ranges[editor.state.selection.mainIndex].from
198+
).number - 1;
199+
200+
const selectedTo = editor.state.doc.lineAt(
201+
editor.state.selection.ranges[editor.state.selection.mainIndex].to
202+
).number - 1;
203+
204+
const outputs = document.querySelector('#OUTPUT').childNodes;
205+
206+
outputs.forEach(code => {
207+
const thisNode = code;
208+
const fromLine = parseInt(thisNode.getAttribute('data-from-line'), 10);
209+
const toLine = parseInt(thisNode.getAttribute('data-to-line'), 10);
210+
if (
211+
(fromLine >= selectedFrom) && (fromLine <= selectedTo)
212+
||
213+
(toLine >= selectedFrom) && (toLine <= selectedTo)
214+
) {
215+
code.classList.add('highlight');
216+
code.scrollIntoView({ block: 'nearest', inline: 'start' });
217+
} else {
218+
code.classList.remove('highlight');
219+
}
220+
});
221+
}
222+
196223
function getSessionName(ID) {
197224
return 'localSession' + ID
198225
}
@@ -233,7 +260,7 @@ function sendWorkToMathWorker(mathExpressoins) {
233260
if (mathExpressoins != "") {
234261
const expressions = mathExpressoins
235262
.replace(/\r?\n/g, '\n')
236-
.trim()
263+
//.trim()
237264
const request = { expr: expressions }
238265
mathWorker.postMessage(JSON.stringify(request))
239266
}

public/mathWorker.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,16 @@ function makeDoc(code) {
7878
const splitCode = code.split('\n');
7979
const cells = [];
8080
let lastType = '';
81-
let lastLineNum = 0;
8281
parser.clear()
8382
splitCode
8483
.forEach((line, lineNum) => {
8584
const lineType = line.startsWith('# ') ? 'md' : 'math';
8685
const formatedLine = lineType === 'md' ? line.slice(2) : line
8786
if (lastType === lineType) {
8887
cells[cells.length - 1].source.push(formatedLine)
88+
cells[cells.length - 1].to = lineNum
8989
} else {
90-
cells.push({ cell_type: lineType, source: [formatedLine], from: lastLineNum, to: lineNum })
91-
lastLineNum = lineNum
90+
cells.push({ cell_type: lineType, source: [formatedLine], from: lineNum, to: lineNum })
9291
}
9392
lastType = lineType
9493
})
@@ -262,7 +261,7 @@ function processOutput(content, type, from, to) {
262261
const expressions = getExpressions(content.join('\n'));
263262
const results = expressions.map(expression => {
264263
const result = processExpression(expression)
265-
return {
264+
return {
266265
source: expression.source,
267266
from: expression.from + from,
268267
to: expression.to + from,

style.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ main>.markdown-body {
2727
padding: 1em;
2828
}
2929

30+
main>.markdown-body .highlight {
31+
background-color: yellowgreen;
32+
}
33+
3034
#INPUT {
3135
flex: 1;
3236
display: flex;
@@ -96,4 +100,4 @@ fieldset {
96100

97101
#examples button {
98102
font-weight: bolder;
99-
}
103+
}

0 commit comments

Comments
 (0)