Skip to content

Commit

Permalink
Minor fix + update service worker to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Baptiste Doderlein committed Jul 5, 2024
1 parent 7146499 commit be75bad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 127 deletions.
9 changes: 3 additions & 6 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ <h2 class="center white-text">Loading Python Interpreter</h2>
</ul>
<ul id="menu-button" class="right">
<li id="flexible-mobile-button">
<a onclick="editors[Math.max(...Object.keys(editors).map(x => +x))+1] = create_editor(id = Math.max(...Object.keys(editors).map(x => +x))+1, name = 'untitled.ml', theme= editors[Math.min(...Object.keys(editors).map(x => +x))].getOption('theme'));"><i
<a onclick="editors[Math.max(...Object.keys(editors).map(x => +x))+1] = create_editor(id = Math.max(...Object.keys(editors).map(x => +x))+1, name = 'untitled.py', theme= editors[Math.min(...Object.keys(editors).map(x => +x))].getOption('theme'));"><i
class="material-icons">add</i></a></li>
<li><a onclick="exec_all(editors[current_editor()])"><i class="material-icons">play_arrow</i></a></li>
<li><a onclick="save(editors[current_editor()]);"><i class="material-icons">save</i></a></li>
Expand All @@ -83,7 +83,7 @@ <h2 class="center white-text">Loading Python Interpreter</h2>
<!-- Mobile Sidenav -->
<ul id="mobile-sidenav" class="sidenav mobile-tabs tabs">
<li id="add_tab" class="tab col s3 onglet">
<a onclick="editors[Math.max(...Object.keys(editors).map(x => +x))+1] = create_editor(id = Math.max(...Object.keys(editors).map(x => +x))+1, name = 'untitled.ml', theme= editors[Math.min(...Object.keys(editors).map(x => +x))].getOption('theme'));">
<a onclick="editors[Math.max(...Object.keys(editors).map(x => +x))+1] = create_editor(id = Math.max(...Object.keys(editors).map(x => +x))+1, name = 'untitled.py', theme= editors[Math.min(...Object.keys(editors).map(x => +x))].getOption('theme'));">
Add file
<i class="material-icons add-icon">add</i>
</a>
Expand Down Expand Up @@ -232,7 +232,7 @@ <h5>Search / Replace</h5>
<div class="modal-content">
<h4>Save as ...</h4>
<div class="input-field col s6">
<input placeholder="untitled.ml" id="saveas_text" type="text" class="white-text">
<input placeholder="untitled.py" id="saveas_text" type="text" class="white-text">
</div>
</div>
<div class="modal-footer help">
Expand Down Expand Up @@ -270,9 +270,6 @@ <h4>Graphics main window</h4>
<div id="toplevel-container">
<mpy-config>
interpreter = "js/micropython/micropython.mjs"
[js_modules.main]
"https://cdn.jsdelivr.net/npm/@xterm/addon-web-links/+esm" = "weblinks"
"https://cdn.jsdelivr.net/npm/@xterm/[email protected]/+esm" = "fit"
</mpy-config>
<script id="toplevel-terminal" type="mpy" terminal worker name="toplevel">
import code
Expand Down
10 changes: 5 additions & 5 deletions src/js/editor_change.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ let calculate_highlight = function (instance) {
* @return {void} Nothing
*/
function save(instance) {
if (instance.name == "untitled.ml") {
if (instance.name == "untitled.py") {
M.Modal.getInstance(document.getElementById('saveas')).open()
} else {
program_save(instance);
Expand All @@ -169,12 +169,12 @@ function save(instance) {
*/
function name_and_save(instance) {
let potential_filename = document.getElementById('saveas_text').value;
let fileNameToSaveAs = "untitled.ml";
let fileNameToSaveAs = "untitled.py";
if (potential_filename !== "") {
if (potential_filename.substr(-3, 3) == ".ml") {
if (potential_filename.substr(-3, 3) == ".py") {
fileNameToSaveAs = potential_filename
} else {
fileNameToSaveAs = potential_filename + ".ml"
fileNameToSaveAs = potential_filename + ".py"
}
}
instance.name = fileNameToSaveAs;
Expand Down Expand Up @@ -593,7 +593,7 @@ function navbar_resize() {
let mobile_button = document.getElementById("flexible-mobile-button");
mobile_button.children[0].children[0].innerText = "add";
mobile_button.children[0].removeAttribute("href");
mobile_button.children[0].setAttribute("onclick", "editors[Math.max(...Object.keys(editors).map(x => +x))+1] = create_editor(id = Math.max(...Object.keys(editors).map(x => +x))+1, name = 'untitled.ml', theme= editors[Math.min(...Object.keys(editors).map(x => +x))].getOption('theme'));");
mobile_button.children[0].setAttribute("onclick", "editors[Math.max(...Object.keys(editors).map(x => +x))+1] = create_editor(id = Math.max(...Object.keys(editors).map(x => +x))+1, name = 'untitled.py', theme= editors[Math.min(...Object.keys(editors).map(x => +x))].getOption('theme'));");
mobile_button.children[0].removeAttribute("data-target");
mobile_button.children[0].removeAttribute("class");
// transfer tabs to navbar
Expand Down
39 changes: 13 additions & 26 deletions src/mini-coi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const staticAssets = [
// './',
'./manifest.json',
'./index.html',
'./favicon.ico'
];


Expand All @@ -19,25 +18,23 @@ const staticAssets = [
}
else {
addEventListener('install', async event => {
skipWaiting();
const cache = await caches.open('static-cache');
cache.addAll(staticAssets);
skipWaiting();
});
addEventListener('activate', e => e.waitUntil(clients.claim()));
addEventListener('fetch', e => {
const req = e.request;
const url = new URL(req.url);
addEventListener('fetch', async e => {
const { request: r } = e;
const url = new URL(r.url);
let cached_req;
/*
if(url.origin === location.url){
cached_req = cacheFirst(req);
} else {
cached_req = newtorkFirst(req);
cached_req = caches.match(r) || fetch(r);
}
else {
cached_req = newtorkFirst(r);
}
*/
if (r.cache === 'only-if-cached' && r.mode !== 'same-origin') return;
e.respondWith(fetch(r).then(r => {
e.respondWith(cached_req.then(r => {
const { body, status, statusText } = r;
if (!status || status > 399) return r;
const h = new Headers(r.headers);
Expand All @@ -50,20 +47,10 @@ const staticAssets = [
}
})(self);

async function cacheFirst(req){
const cachedResponse = caches.match(req);
return cachedResponse || fetch(req);
}

async function newtorkFirst(req){
const cache = await caches.open('dynamic-cache');

try {
const res = await fetch(req);
cache.put(req, res.clone());
return res;
} catch (error) {
return await cache.match(req);
}
async function newtorkFirst(r){
const cache = await caches.open('dynamic-cache');
const res = await fetch(r);
cache.put(r, res.clone());
return res;
}

90 changes: 0 additions & 90 deletions src/serviceWorker.js

This file was deleted.

0 comments on commit be75bad

Please sign in to comment.