Skip to content

Commit 643fdf8

Browse files
committed
error overlay; improvements
1 parent fc9c447 commit 643fdf8

11 files changed

Lines changed: 168 additions & 180 deletions

‎flm/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
# magic, no pkg_resources, no other stuff like that), especially for possible
55
# future use with transcrypt.
66

7-
__version__ = "0.3.0alpha000028"
7+
__version__ = "0.3.0alpha000029"
88

99
# ALSO BUMP VERSION NUMBER IN pyproject.toml !!

‎flm/main/dist/watch_hotreload_inject.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎flm/main/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"@parcel/core": "^2.16.4",
1111
"morphdom": "^2.7.8",
1212
"parcel": "^2.16.4"
13+
},
14+
"devDependencies": {
15+
"@parcel/transformer-inline-string": "2.16.4"
1316
}
1417
}

‎flm/main/watch.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,18 @@ def do_compile(hotreloader=None):
8181
#
8282
# Compile - main run NOW!
8383
#
84-
info = main.main(**run_kwargs)
84+
try:
85+
info = main.main(**run_kwargs)
86+
except LatexWalkerLocatedError as e:
87+
error_info = {'message': str(e), 'exc': e}
88+
if hotreloader is not None and hotreloader.is_enabled():
89+
hotreloader.new_run_send_error(error_info)
90+
raise
91+
except Exception as e:
92+
error_info = {'message': str(e), 'exc': e}
93+
if hotreloader is not None and hotreloader.is_enabled():
94+
hotreloader.new_run_send_error(error_info)
95+
raise
8596

8697
if hotreloader is not None and hotreloader.is_enabled():
8798
# Inform our hotreloader of a new run.

‎flm/main/watch_hotreload.py‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
import json
77
import threading
88

9+
from html import escape as html_escape
910

1011
import asyncio
1112
import websockets.asyncio.server as websockets_server
1213
# import websockets.exceptions as websockets_exceptions
1314

14-
15-
from lxml import html
16-
1715
from .watch_util import find_available_port
1816

1917

@@ -150,6 +148,8 @@ def inject_hotreload_js(self):
150148
def new_run_send_update(self, info, content):
151149
pass
152150

151+
def new_run_send_error(self, error_info):
152+
pass
153153

154154

155155
class HotReloaderHtml:
@@ -159,10 +159,6 @@ def __init__(self, hotreload_server, computed_format, output):
159159
self.computed_format = computed_format
160160
self.output = output
161161

162-
self.previous_content = None
163-
with open(self.output, 'r', encoding='utf-8') as f:
164-
self.previous_content = f.read()
165-
166162

167163
def is_enabled(self):
168164
return True
@@ -214,7 +210,14 @@ def new_run_send_update(self, info, content):
214210

215211
self.hotreload_server.send_update_info(update_info)
216212

217-
# save this as the current content
218-
self.previous_content = content
213+
214+
def new_run_send_error(self, error_info):
215+
216+
update_info = {
217+
"action": 'error-display',
218+
"content_html": '<pre>' + html_escape(error_info['message']) + '</pre>',
219+
}
220+
221+
self.hotreload_server.send_update_info(update_info)
219222

220223

‎flm/main/watch_hotreload_inject.ts‎

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
//
55
// window, wsHost, wsPort
66
//
7+
const document = window.document;
78

89
import morphdom from 'morphdom';
910

11+
import cssText from 'bundle-text:./watch_hotreload_style.css';
12+
1013
declare var wsHost: string;
1114
declare var wsPort: number;
1215

@@ -154,7 +157,7 @@ function updateInstructionsReceived(mainContainer: HTMLElement, info : UpdateInf
154157
}*/
155158

156159
type UpdateInfo = {
157-
action: 'update-main-content',
160+
action: 'update-main-content'|'error-display',
158161
content_html: string,
159162
};
160163

@@ -360,9 +363,46 @@ function stampMathContentSources(mainContainer: HTMLElement, elements: HTMLEleme
360363
}
361364
}
362365

366+
function clearErrorOverlay()
367+
{
368+
const overlayDiv = document.getElementById('ErrorOverlay');
369+
if (overlayDiv != null) {
370+
overlayDiv.classList.remove('error-overlay-shown');
371+
}
372+
}
373+
function displayErrorOverlay(mainContainer : HTMLElement, info : UpdateInfo)
374+
{
375+
const makeErrorOverlayDiv = () => {
376+
const el = document.getElementById('ErrorOverlay');
377+
if (el != null) {
378+
return el;
379+
}
380+
const newEl = document.createElement('div');
381+
newEl.setAttribute('id', 'ErrorOverlay');
382+
document.body.appendChild(newEl);
383+
return newEl;
384+
};
385+
let overlay = makeErrorOverlayDiv();
386+
387+
overlay.classList.add('error-overlay-shown');
388+
389+
overlay.innerHTML = info.content_html;
390+
}
391+
363392
window.addEventListener("DOMContentLoaded", () => {
364393

394+
//
395+
// inject our style tag, mainly for the error overlay display
396+
//
397+
let style = document.createElement('style');
398+
style.textContent = cssText;
399+
document.head.appendChild(style);
400+
401+
//
402+
// Our main container div
403+
//
365404
const mainContainer = document.getElementById('Main')!;
405+
366406
//
367407
// First of all, stamp all our equations with the source math data to help
368408
// us process updates.
@@ -376,11 +416,18 @@ window.addEventListener("DOMContentLoaded", () => {
376416
websocket.addEventListener("message", function (m) {
377417
console.log("Message!", m);
378418
const info = JSON.parse(m.data) as UpdateInfo;
379-
try {
380-
updateMainContent(mainContainer, info);
381-
} catch (err) {
382-
// failure in the incremental update, so reload everything ... :/
383-
window.location.reload();
419+
clearErrorOverlay();
420+
if (info.action === 'update-main-content') {
421+
try {
422+
updateMainContent(mainContainer, info);
423+
} catch (err) {
424+
// failure in the incremental update, so reload everything ... :/
425+
window.location.reload();
426+
}
427+
} else if (info.action === 'error-display') {
428+
displayErrorOverlay(mainContainer, info);
429+
} else {
430+
console.error("Invalid update info action!", info);
384431
}
385432
});
386433
websocket.addEventListener("open", function () { console.log("websocket open"); });

0 commit comments

Comments
 (0)