-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(script): add script tag to client widgets
- Loading branch information
Showing
7 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from trame.app import get_server | ||
from trame.widgets import html, client | ||
from trame.ui.html import DivLayout | ||
|
||
server = get_server() | ||
|
||
JS_CONTENT = ( | ||
"import confetti from 'https://esm.sh/canvas-confetti';" | ||
"trame.utils.confetti = confetti;" | ||
) | ||
|
||
with DivLayout(server): | ||
html.Button("Click Me", click="utils.confetti()") | ||
client.Script(JS_CONTENT, module=True) | ||
|
||
server.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const { unref } = window.Vue; | ||
let ID = 1; | ||
|
||
function getNextId() { | ||
return `trame_script_elem_${ID++}`; | ||
} | ||
|
||
export default { | ||
props: { | ||
script: { | ||
type: String, | ||
default: '', | ||
}, | ||
module: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
watch: { | ||
script(scriptContent) { | ||
this.updateContent(scriptContent); | ||
}, | ||
}, | ||
created() { | ||
this.elemId = getNextId(); | ||
this.updateContent(this.script); | ||
}, | ||
beforeUnmount() { | ||
this.removeScript(); | ||
}, | ||
methods: { | ||
updateContent(scriptContent) { | ||
scriptContent = unref(scriptContent); | ||
let elem = document.querySelector(`#${this.elemId}`); | ||
if (scriptContent) { | ||
if (!elem) { | ||
elem = document.createElement('script'); | ||
elem.id = this.elemId; | ||
elem.type = this.module ? 'module' : 'text/javascript'; | ||
document.head.appendChild(elem); | ||
} | ||
elem.innerHTML = scriptContent; | ||
} else { | ||
this.removeScript(); | ||
} | ||
}, | ||
removeScript() { | ||
const elem = document.querySelector(`#${this.elemId}`); | ||
if (elem) { | ||
elem.parentNode.removeChild(elem); | ||
} | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const { unref } = window.Vue; | ||
let ID = 1; | ||
|
||
function getNextId() { | ||
return `trame_script_elem_${ID++}`; | ||
} | ||
|
||
export default { | ||
props: { | ||
script: { | ||
type: String, | ||
default: "", | ||
}, | ||
module: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
watch: { | ||
script(scriptContent) { | ||
this.updateContent(scriptContent); | ||
}, | ||
}, | ||
created() { | ||
this.elemId = getNextId(); | ||
this.updateContent(this.script); | ||
}, | ||
beforeUnmount() { | ||
this.removeScript(); | ||
}, | ||
methods: { | ||
updateContent(scriptContent) { | ||
scriptContent = unref(scriptContent); | ||
let elem = document.querySelector(`#${this.elemId}`); | ||
if (scriptContent) { | ||
if (!elem) { | ||
elem = document.createElement("script"); | ||
elem.id = this.elemId; | ||
elem.type = this.module ? "module" : "text/javascript"; | ||
document.head.appendChild(elem); | ||
} | ||
elem.innerHTML = scriptContent; | ||
} else { | ||
this.removeScript(); | ||
} | ||
}, | ||
removeScript() { | ||
const elem = document.querySelector(`#${this.elemId}`); | ||
if (elem) { | ||
elem.parentNode.removeChild(elem); | ||
} | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,5 +46,4 @@ export default { | |
} | ||
}, | ||
}, | ||
template: `<div class="trame-style" />`, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters