mirror of
https://github.com/pyscript/pyscript.git
synced 2022-05-01 19:47:48 +03:00
add pybutton and pytitle
This commit is contained in:
@@ -18,28 +18,27 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body class="container">
|
<body class="container">
|
||||||
<!-- <py-repl id="my-repl" auto-generate="true"> </py-repl> -->
|
|
||||||
<!-- <py-script src="/todo.py"> </py-script> -->
|
|
||||||
|
|
||||||
|
|
||||||
<main class="max-w-xs mx-auto mt-4">
|
<main class="max-w-xs mx-auto mt-4">
|
||||||
<section>
|
<section>
|
||||||
|
|
||||||
<py-list id="mylist"></py-list>
|
<py-title>To Do List</py-title>
|
||||||
|
|
||||||
<div class="text-center w-full mb-8">
|
|
||||||
<h1 class="text-3xl font-bold text-gray-800 uppercase tracking-tight">To Do List</h1>
|
|
||||||
</div>
|
|
||||||
<div>
|
<div>
|
||||||
<input id="new-task-content" class="border flex-1 mr-3 border-gray-300 p-2 rounded" type="text" py-mount>
|
<input id="new-task-content" class="border flex-1 mr-3 border-gray-300 p-2 rounded" type="text" py-mount>
|
||||||
<button id="new-task-btn" class="p-2 text-white bg-blue-600 border border-blue-600 rounded" type="submit" pys-onClick="add_task">
|
|
||||||
Add task
|
<py-button id="new-task-btn" label="Add Task!">
|
||||||
|
def on_click(evt):
|
||||||
|
task = { "content": new_task_content.value, "done": False, "created_at": dt.now() }
|
||||||
|
myList.add(PyItem(task, labels=['content'], state_key="done"))
|
||||||
|
new_task_content.clear()
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<py-list id="myList"></py-list>
|
<py-list id="myList"></py-list>
|
||||||
|
|
||||||
|
|
||||||
<py-repl id="my-repl" auto-generate="true"> </py-repl>
|
<py-repl id="my-repl" auto-generate="true"> </py-repl>
|
||||||
</div>
|
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
64
pyscriptjs/src/components/pybutton.ts
Normal file
64
pyscriptjs/src/components/pybutton.ts
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import { BaseEvalElement } from './base';
|
||||||
|
import { addClasses, ltrim, htmlDecode } from '../utils';
|
||||||
|
|
||||||
|
export class PyButton extends BaseEvalElement {
|
||||||
|
shadow: ShadowRoot;
|
||||||
|
wrapper: HTMLElement;
|
||||||
|
theme: string;
|
||||||
|
widths: Array<string>;
|
||||||
|
label: string;
|
||||||
|
mount_name: string;
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
// attach shadow so we can preserve the element original innerHtml content
|
||||||
|
// this.shadow = this.attachShadow({ mode: 'open'});
|
||||||
|
|
||||||
|
// this.wrapper = document.createElement('slot');
|
||||||
|
// this.shadow.appendChild(this.wrapper);
|
||||||
|
if (this.hasAttribute('label')) {
|
||||||
|
this.label = this.getAttribute('label');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
this.code = htmlDecode(this.innerHTML);
|
||||||
|
this.mount_name = this.id.split("-").join("_");
|
||||||
|
this.innerHTML = '';
|
||||||
|
|
||||||
|
let mainDiv = document.createElement('button');
|
||||||
|
mainDiv.innerHTML = this.label;
|
||||||
|
addClasses(mainDiv, ["p-2", "text-white", "bg-blue-600", "border", "border-blue-600", "rounded"]);
|
||||||
|
|
||||||
|
mainDiv.id = this.id;
|
||||||
|
this.id = `${this.id}-container`;
|
||||||
|
|
||||||
|
this.appendChild(mainDiv);
|
||||||
|
this.code = this.code.split("self").join(this.mount_name);
|
||||||
|
let registrationCode = `${this.mount_name} = Element("${ mainDiv.id }")`;
|
||||||
|
if (this.code.includes("def on_focus")){
|
||||||
|
this.code = this.code.replace("def on_focus", `def on_focus_${this.mount_name}`);
|
||||||
|
registrationCode += `\n${this.mount_name}.element.onfocus = on_focus_${this.mount_name}`
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.code.includes("def on_click")){
|
||||||
|
this.code = this.code.replace("def on_click", `def on_click_${this.mount_name}`);
|
||||||
|
registrationCode += `\n${this.mount_name}.element.onclick = on_click_${this.mount_name}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// now that we appended and the element is attached, lets connect with the event handlers
|
||||||
|
// defined for this widget
|
||||||
|
setTimeout(() => {
|
||||||
|
this.eval(this.code).then(() => {
|
||||||
|
this.eval(registrationCode).then(() => {
|
||||||
|
console.log('registered handlers');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, 4000);
|
||||||
|
|
||||||
|
console.log('py-button connected');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
37
pyscriptjs/src/components/pytitle.ts
Normal file
37
pyscriptjs/src/components/pytitle.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { BaseEvalElement } from './base';
|
||||||
|
import { addClasses, ltrim, htmlDecode } from '../utils';
|
||||||
|
|
||||||
|
export class PyTitle extends BaseEvalElement {
|
||||||
|
shadow: ShadowRoot;
|
||||||
|
wrapper: HTMLElement;
|
||||||
|
theme: string;
|
||||||
|
widths: Array<string>;
|
||||||
|
label: string;
|
||||||
|
mount_name: string;
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
this.label = htmlDecode(this.innerHTML);
|
||||||
|
this.mount_name = this.id.split("-").join("_");
|
||||||
|
this.innerHTML = '';
|
||||||
|
|
||||||
|
let mainDiv = document.createElement('div');
|
||||||
|
let divContent = document.createElement('h1')
|
||||||
|
|
||||||
|
addClasses(mainDiv, ["text-center", "w-full", "mb-8"]);
|
||||||
|
addClasses(divContent, ["text-3xl", "font-bold", "text-gray-800", "uppercase", "tracking-tight"]);
|
||||||
|
divContent.innerHTML = this.label;
|
||||||
|
|
||||||
|
mainDiv.id = this.id;
|
||||||
|
this.id = `${this.id}-container`;
|
||||||
|
mainDiv.appendChild(divContent);
|
||||||
|
this.appendChild(mainDiv);
|
||||||
|
|
||||||
|
console.log('py-title connected');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -4,12 +4,16 @@ import { PyScript } from "./components/pyscript";
|
|||||||
import { PyRepl } from "./components/pyrepl";
|
import { PyRepl } from "./components/pyrepl";
|
||||||
import { PyEnv } from "./components/pyenv";
|
import { PyEnv } from "./components/pyenv";
|
||||||
import { PyBox } from "./components/pybox";
|
import { PyBox } from "./components/pybox";
|
||||||
|
import { PyButton } from "./components/pybutton";
|
||||||
|
import { PyTitle } from "./components/pytitle";
|
||||||
import { PyWidget } from "./components/base";
|
import { PyWidget } from "./components/base";
|
||||||
|
|
||||||
let xPyScript = customElements.define('py-script', PyScript);
|
let xPyScript = customElements.define('py-script', PyScript);
|
||||||
let xPyRepl = customElements.define('py-repl', PyRepl);
|
let xPyRepl = customElements.define('py-repl', PyRepl);
|
||||||
let xPyEnv = customElements.define('py-env', PyEnv);
|
let xPyEnv = customElements.define('py-env', PyEnv);
|
||||||
let xPyBox = customElements.define('py-box', PyBox);
|
let xPyBox = customElements.define('py-box', PyBox);
|
||||||
|
let xPyButton = customElements.define('py-button', PyButton);
|
||||||
|
let xPyTitle = customElements.define('py-title', PyTitle);
|
||||||
let xPyWidget = customElements.define('py-register-widget', PyWidget);
|
let xPyWidget = customElements.define('py-register-widget', PyWidget);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,4 +9,29 @@ const getLastPath = function (str) {
|
|||||||
return str.split('\\').pop().split('/').pop();
|
return str.split('\\').pop().split('/').pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
export {addClasses, getLastPath}
|
function htmlDecode(input) {
|
||||||
|
var doc = new DOMParser().parseFromString(input, "text/html");
|
||||||
|
return ltrim(doc.documentElement.textContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ltrim(code: string): string {
|
||||||
|
const lines = code.split("\n")
|
||||||
|
if (lines.length == 0)
|
||||||
|
return code
|
||||||
|
|
||||||
|
const lengths = lines
|
||||||
|
.filter((line) => line.trim().length != 0)
|
||||||
|
.map((line) => {
|
||||||
|
const [prefix] = line.match(/^\s*/)
|
||||||
|
return prefix.length
|
||||||
|
})
|
||||||
|
|
||||||
|
const k = Math.min(...lengths)
|
||||||
|
|
||||||
|
if (k != 0)
|
||||||
|
return lines.map((line) => line.substring(k)).join("\n")
|
||||||
|
else
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
export {addClasses, getLastPath, ltrim, htmlDecode}
|
||||||
|
|||||||
Reference in New Issue
Block a user