dioxus start working

This commit is contained in:
rewin
2024-02-07 22:23:10 +03:00
parent 7e0c3e850a
commit 73705ccec5
10 changed files with 855 additions and 172 deletions
+23 -9
View File
@@ -231,16 +231,30 @@ export class AppElement extends LitElement {
@space-change=${this.onTransformSpaceChange}
></sts-transform-toolbar>
<sts-inspector></sts-inspector>
<style>
.vertical-list {
display: flex;
flex-direction: column;
position: absolute;
top: 16px;
right: 16px;
<div class="hierarchy v-start h-end">
${this.sceneObjects.map(obj => html`
<sts-tree class="scene-object"
id=${obj.id}
.name=${obj.name}
.treeChildren=${obj.children}
></sts-tree>
`)}
gap: 16px;
width: 290px;
}
</style>
<div class="vertical-list">
<div class="hierarchy">
${this.sceneObjects.map(obj => html`
<sts-tree class="scene-object"
id=${obj.id}
.name=${obj.name}
.treeChildren=${obj.children}
></sts-tree>
`)}
</div>
<sts-inspector id="inspector"></sts-inspector>
</div>
` : nothing}
+238 -8
View File
@@ -2,20 +2,250 @@ import { on } from "@storyteller/framework";
import { type InspectorDomChanged } from "dist/studio/studio";
import { LitElement, html, unsafeCSS, nothing, HTMLTemplateResult } from "lit";
import { customElement, state } from "lit/decorators.js";
import { property } from "lodash";
//vdom example
/// {
///"subtree":0,
/// "dirty_scopes":[],
/// "templates":[{"name":"studio\\src\\ui\\inspector.rs:46:15:160899","roots":[{"type":"Element","tag":"div","namespace":null,"attrs":[],"children":[{"type":"Text","text":"Inspector"},{"type":"Element","tag":"div","namespace":null,"attrs":[],"children":[{"type":"Text","text":"hello world"}]}]}],"node_paths":[],"attr_paths":[]}],
/// "edits":[{"type":"LoadTemplate","name":"studio\\src\\ui\\inspector.rs:46:15:160899","index":0,"id":1},{"type":"AppendChildren","id":0,"m":1}]}
@customElement("sts-inspector")
export class AppElement extends LitElement {
@state() vdom?: string | null;
@state() generation = 0;
@state() vdom_elements?: VdomElement[] | null;
@on("window:inspector-dom-changed")
onInspectorDomChanged({ detail: state}: InspectorDomChanged): void {
console.log("inspector dom changed:", state);
}
// @on("window:inspector-dom-changed")
// onInspectorDomChanged({ detail: state}: InspectorDomChanged): void {
// console.log("inspector dom changed:", state);
// //change vdom and activate render
// this.vdom = null;
// this.vdom = state;
// this.generation += 1;
protected override render = () => html`
<div id="some_id">
// // Activate the render function
// this.requestUpdate();
// }
</div>
`;
protected override render() {
this.shadowRoot
return html`
<style>
.inspector {
overflow: auto;
box-sizing: border-box;
width: 290px;
max-height: calc(100vh - 32px);
padding: 10px 15px;
color: #FFFC;
background: #0006;
backdrop-filter: blur(16px);
border: 8px solid transparent;
border-radius: 0.5rem;
pointer-events: auto;
&::-webkit-scrollbar {
width: 8px;
height: 8px;
&-thumb {
border-radius: 2px;
background: #FFF2;
&:hover {
background: #FFF3;
}
}
}
}
</style>
<div id="inspector" class="inspector"></div>
`
//convert string vdom to json object
// if (this.vdom) {
// var vdom = JSON.parse(this.vdom);
// var templates = vdom["templates"];
// var edits = vdom["edits"];
// console.log("edits", edits);
// console.log("templates", templates);
// var element_stack = [];
// var element_tree = [];
// //loop through edits
// for (const edit of edits) {
// var type = edit["type"];
// if (type == "LoadTemplate") {
// var index = edit["index"];
// var template_name = edit["name"];
// var id = edit["id"];
// var target_root = null;
// for (const template of templates) {
// if (template["name"] == template_name) {
// target_root = template["roots"][index];
// break;
// }
// }
// if (target_root == null) {
// console.log("template not found:", template_name);
// continue;
// } else {
// target_root["id"] = id;
// element_stack.push(target_root);
// }
// } else if (type == "AppendChildren") {
// var id = edit["id"];
// var count = edit["m"];
// if (id == 0) {
// for (var i = 0; i < count; i++) {
// var current_element = element_stack.pop();
// element_tree.push(current_element);
// }
// } else {
// //TODO
// }
// } else if (type == "HydrateText") {
// var child_path = edit["path"];
// var target_node = element_stack[element_stack.length - 1];
// for (const path of child_path) {
// target_node = target_node["children"][path];
// }
// //add text node to target_node
// if (target_node["children"] == undefined) {
// target_node["children"] = [];
// }
// target_node["children"].push({type: "Text", text: edit["value"], id: edit["id"]});
// }
// }
// console.log("element tree", element_tree);
// //convert any to VdomElement in element_tree
// var vdom_elements = element_tree.map((element) => {
// return element as VdomElement;
// });
// this.vdom_elements = vdom_elements;
// return html`
// <style>
// .inspector {
// overflow: auto;
// box-sizing: border-box;
// width: 290px;
// max-height: calc(100vh - 32px);
// padding: 10px 15px;
// color: #FFFC;
// background: #0006;
// backdrop-filter: blur(16px);
// border: 8px solid transparent;
// border-radius: 0.5rem;
// pointer-events: auto;
// &::-webkit-scrollbar {
// width: 8px;
// height: 8px;
// &-thumb {
// border-radius: 2px;
// background: #FFF2;
// &:hover {
// background: #FFF3;
// }
// }
// }
// }
// </style>
// <div id="inspector" class="inspector">
// ${this.vdom_elements.map((element, index) => html`<sts-inspector-element generation=${this.generation} .vdom=${element} key=${index}></sts-inspector-element>`)}
// </div>`
// } else {
// //For each vdom element render InspectorElement
// return html`
// `
// }
}
}
export interface VdomElement {
id?: number;
type?: string;
text?: string;
tag?: string;
namespace?: string;
attrs?: any[];
children?: VdomElement[];
}
/// Element for recursive render vdom tree
@customElement("sts-inspector-element")
export class InspectorElement extends LitElement {
@property({ type: Object, attribute: true })
vdom: VdomElement;
@property({ type: Number, attribute: true })
generation: number;
protected override render() {
var tag = this.vdom["tag"];
var type = this.vdom.type;
if (type == "Text") {
return html`<div>${this.vdom["text"]}</div>`
} else if (type == "Dynamic") {
return html`
<div id=${this.vdom["id"]}>
${this.vdom["children"].map((element) => html`<sts-inspector-element .vdom=${element} .generation=${this.generation}></sts-inspector-element>`)}
</div>
`
}
if (tag == "div") {
return html`
<style>
.tabber {
padding: 0 0 0 10px;
}
</style>
<div id=${this.vdom["id"]} class="tabber">
${this.vdom["children"].map((element) => html`<sts-inspector-element .vdom=${element} .generation=${this.generation}></sts-inspector-element>`)}
</div>`
} else {
return html`<div>${this.vdom["text"]}</div>`
}
}
}
+226
View File
@@ -0,0 +1,226 @@
import { type InspectorDomChanged } from "dist/studio/studio";
console.log("Hello, World!");
function load_child(stack : any[], child_path : number[]) {
// iterate through each number and get that child
var node : any = stack[stack.length - 1];
for (const ptr of child_path) {
var steps = ptr;
var m = 0;
for (node = node.firstChild; m < steps; m++) {
node = node.nextSibling;
}
}
return node;
}
function laod_template(edit : any, stack : any[]) {
}
function apply_muts(
event : InspectorDomChanged
) {
if (event.detail !== null) {
var lit_root = document.getElementById("inspector");
var shadowroot = lit_root?.shadowRoot;
var root = shadowroot?.getElementById("inspector");
console.log("root", root);
if (root) {
//remove all children
while (root.firstChild) {
root.removeChild(root.firstChild);
}
} else {
console.log("root not found");
return;
}
var vdom = JSON.parse(event.detail);
var templates = vdom["templates"];
var edits = vdom["edits"];
console.log("edits", edits);
console.log("templates", templates);
var element_stack = [];
var nodes : any = {}
nodes[0] = document.createElement("div");
root.appendChild(nodes[0]);
element_stack.push(root);
//loop through edits
for (const edit of edits) {
var type = edit["type"];
if (type == "LoadTemplate") {
// var index = edit["index"];
// var template_name = edit["name"];
// var id = edit["id"];
// var target_root = null;
// for (const template of templates) {
// if (template["name"] == template_name) {
// target_root = template["roots"][index];
// break;
// }
// }
// if (target_root == null) {
// console.log("template not found:", template_name);
// continue;
// } else {
// target_root["id"] = id;
// element_stack.push(target_root);
// }
var id = edit["id"];
var index = edit["index"];
var template_name = edit["name"];
var template_node = null;
for (const template of templates) {
if (template["name"] == template_name) {
template_node = template["roots"][index];
break;
}
}
if (template_node == null) {
console.log("template not found:", template_name);
continue;
} else {
var node = recursive_render(null, template_node);
nodes[id] = node;
element_stack.push(node);
}
} else if (type == "AppendChildren") {
// var id = edit["id"];
// var count = edit["m"];
// if (id == 0) {
// for (var i = 0; i < count; i++) {
// var current_element = element_stack.pop();
// element_tree.push(current_element);
// }
// } else {
// //TODO
// }
var id = edit["id"];
var many = edit["m"];
var target_node = nodes[id];
for (var i = 0; i < many; i++) {
var current_element = element_stack.pop();
target_node.appendChild(current_element);
}
} else if (type == "HydrateText") {
// var child_path = edit["path"];
// var target_node = element_stack[element_stack.length - 1];
// for (const path of child_path) {
// target_node = target_node["children"][path];
// }
// if (target_node == null) {
// console.log("target_node not found:", child_path);
// continue;
// }
// //add text node to target_node
// if (target_node["children"] == undefined) {
// target_node["children"] = [];
// }
// target_node["children"].push({type: "Text", text: edit["value"], id: edit["id"]});
var node : HTMLElement = load_child(element_stack, edit["path"]);
if (node) {
node.textContent = edit["value"];
} else {
console.log("node not found:", edit["path"]);
}
} else if (type == "ReplacePlaceholder") {
var child_path = edit["path"];
var manu = edit["m"];
var els = [];
for (var i = 0; i < manu; i++) {
var el = element_stack.pop();
els.push(el);
}
var target_node = load_child(element_stack, child_path);
target_node.replaceWith(...els);
console.log("")
}
}
} else {
console.log('Selection has been cleared.');
}
}
document.addEventListener('inspector-dom-changed', (event: CustomEvent) => {
// Cast the event to the InspectorDomChanged type to access its details in TypeScript
const customEvent = event as InspectorDomChanged;
// Now you can access the detail property, which is either a string or null
apply_muts(customEvent);
});
export interface VdomElement {
id?: number;
type?: string;
text?: string;
tag?: string;
namespace?: string;
attrs?: any[];
children?: VdomElement[];
}
function recursive_render(
root: HTMLElement | null,
element: VdomElement,
) {
//create element
var tag = element["tag"];
if (element.type == "Text") {
tag = "div";
} else if (element.type == "Dynamic") {
tag = "div";
}
var new_element = document.createElement(element["tag"] as string);
//set text
if (element.text != undefined) {
new_element.textContent = element.text;
}
//set id
if (element.id != undefined) {
new_element.id = element.id.toString();
}
//parent to root
if (root != null) {
root.appendChild(new_element);
}
//render children
if (element["children"] != undefined) {
for (const child of element["children"]) {
recursive_render(new_element, child);
}
}
return new_element;
}
+1
View File
@@ -1 +1,2 @@
import "./app/app.element";
import "./dioxus-machine";