fix loading livereload extension
This commit is contained in:
parent
a756a0484f
commit
b234ead964
4 changed files with 20 additions and 17 deletions
2
framework/assets/dist/htmgo.js
vendored
2
framework/assets/dist/htmgo.js
vendored
File diff suppressed because one or more lines are too long
13
framework/assets/js/htmxextensions/extension.ts
Normal file
13
framework/assets/js/htmxextensions/extension.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
|
||||||
|
export function hasExtension(name: string): boolean {
|
||||||
|
for (const element of Array.from(document.querySelectorAll("[hx-ext]"))) {
|
||||||
|
const value = element.getAttribute("hx-ext");
|
||||||
|
if(value != null) {
|
||||||
|
const split = value.split(" ").map(s => s.replace(",", ""))
|
||||||
|
if(split.includes(name)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
@ -1,24 +1,18 @@
|
||||||
import htmx from "htmx.org";
|
import htmx from "htmx.org";
|
||||||
|
import {hasExtension} from "./extension";
|
||||||
|
|
||||||
let lastVersion = "";
|
let lastVersion = "";
|
||||||
|
|
||||||
htmx.defineExtension("livereload", {
|
htmx.defineExtension("livereload", {
|
||||||
init: function () {
|
init: function () {
|
||||||
|
|
||||||
let enabled = false
|
let enabled = hasExtension("livereload")
|
||||||
for (const element of Array.from(htmx.findAll("[hx-ext]"))) {
|
|
||||||
const value = element.getAttribute("hx-ext");
|
|
||||||
if(value?.split(" ").includes("livereload")) {
|
|
||||||
enabled = true
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!enabled) {
|
if(!enabled) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('livereload extension initialized.');
|
console.info('livereload extension initialized.');
|
||||||
// Create a new EventSource object and point it to your SSE endpoint
|
// Create a new EventSource object and point it to your SSE endpoint
|
||||||
const eventSource = new EventSource('/dev/livereload');
|
const eventSource = new EventSource('/dev/livereload');
|
||||||
// Listen for messages from the server
|
// Listen for messages from the server
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,9 @@
|
||||||
import {ws} from "./ws";
|
import {ws} from "./ws";
|
||||||
|
import {hasExtension} from "./extension";
|
||||||
|
|
||||||
window.onload = function () {
|
window.onload = function () {
|
||||||
const elements = document.querySelectorAll("[hx-extension]");
|
if(hasExtension("ws")) {
|
||||||
for (let element of Array.from(elements)) {
|
|
||||||
const value = element.getAttribute("hx-extension");
|
|
||||||
if(value != null && value.split(" ").includes("ws")) {
|
|
||||||
addWsEventHandlers()
|
addWsEventHandlers()
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue