diff --git a/example.html b/example.html
index 2b68379..1342053 100644
--- a/example.html
+++ b/example.html
@@ -25,7 +25,7 @@
max-width: 400px;
overflow: auto;
}
- #code, #fullcode {
+ .code {
max-height: 800px;
width: 400px;
overflow: auto;
@@ -37,12 +37,14 @@
-
-
-
+
+
+
+
+
-
-
+
+
diff --git a/src/demo-config.js b/src/demo-config.js
index 0005133..27b812d 100644
--- a/src/demo-config.js
+++ b/src/demo-config.js
@@ -1,4 +1,48 @@
export const demoConfig = [
+ {
+ condition: "state",
+ entity_id: "binary_sensor.dark_outside",
+ state: "on",
+ },
+ {
+ choose: [
+ {
+ conditions: [
+ {
+ condition: "state",
+ entity_id: "binary_sensor.door_open",
+ state: "on",
+ },
+ ],
+ sequence: [
+ {
+ service: "light.turn_on",
+ entity_id: "light.outdoors",
+ }
+ ],
+ },
+ {
+ conditions: [
+ {
+ condition: "state",
+ entity_id: "input_select.time_of_day",
+ state: "night",
+ },
+ ],
+ sequence: [
+ {
+ service: "light.turn_off",
+ entity_id: "light.outdoors",
+ }
+ ],
+ },
+ ],
+ default: [
+ ],
+ },
+]
+
+export const demoConfig2 = [
{ service: "light.turn_on",
data: {
entity_id: "group.bedroom",
diff --git a/src/main.js b/src/main.js
index 9afd901..c33ad17 100644
--- a/src/main.js
+++ b/src/main.js
@@ -4,121 +4,18 @@ import "@vanillawc/wc-codemirror";
import "./script-graph";
import "./script-graph2";
+import { ActionHandler } from "./script-to-graph";
+
import { mdiAsterisk, mdiArrowUp, mdiArrowDown } from "@mdi/js";
let index_counter = 0;
let nodes = [];
-const structure_tree = (inp) => {
- if(!inp) return null;
- if(Array.isArray(inp)) return inp.map(structure_tree);
- let data = {};
- let type = "YAML";
-
- const idx = index_counter++;
- if("service" in inp) type = "service";
- if("condition" in inp) type = "condition";
- if("delay" in inp) type = "delay";
- if("wait_template" in inp) type = "wait";
- if("event" in inp) type = "event";
- if("repeat" in inp) {
- type = "loop";
- data = {sequence: structure_tree(inp.repeat.sequence)};
- }
- if("choose" in inp) {
- type = "choose";
- let choices = [];
- for (const [i,c] of inp.choose.entries()) {
- const header = {
- type: "chooseChoice",
- idx: [idx, i],
- }
- choices.push([header].concat(structure_tree(c.sequence)));
- }
- choices.push([{
- type: "chooseDefault",
- idx: [idx, -1],
- }].concat(structure_tree(inp.default)));
- data = {
- choices,
- }
- }
- nodes[idx] = inp;
- return {type,
- idx,
- ...data};
-
-}
-
-const structure_tree2 = () => {
- return [
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number 1"),
- },
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number 2"),
- },
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number 3"),
- },
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number 4"),
- children: [
- [
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number A1"),
- },
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number A2"),
- },
- ],
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number B"),
- end: false,
- },
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number B"),
- },
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number B"),
- },
- ]
- },
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number 4"),
- children: [
- {
- icon: mdiArrowUp,
- clickCallback: () => console.log("Return"),
- },
- {
- icon: mdiArrowDown,
- clickCallback: () => console.log("Return"),
- },
- ],
- },
- {
- icon: mdiAsterisk,
- clickCallback: () => console.log("Number 1"),
- },
- ];
-}
-
window.onload = () => {
let src = demoConfig;
- const fullcode = document.querySelector("wc-codemirror");
+ const fullcode = document.querySelector("#fullcode");
fullcode.mode = "yaml";
window.setTimeout(()=> fullcode.value = jsyaml.safeDump(src), 100);
@@ -127,29 +24,28 @@ window.onload = () => {
src = jsyaml.safeLoad(fullcode.value);
index_counter = 0;
nodes = [];
- graph.tree = structure_tree(src);
+ // graph.tree = structure_tree(src);
});
- const graph = document.createElement("script-graph");
- graph.tree = structure_tree(src);
-
const graph2 = document.createElement("script-graph2");
- graph2.tree = structure_tree2(src);
+ const tr = new ActionHandler();
+ window.tr = tr;
+ tr.actions = src;
+ tr.updateCallback = (actions) => {
+ graph2.tree = tr.graph;
+ fullcode.value = jsyaml.safeDump(tr.actions);
+ };
+ tr.selectCallback = (idx, action, update) => {
+ graph2.tree = tr.graph;
+ const code = document.querySelector("#snippet");
+ code.value = jsyaml.safeDump(action);
+ console.log(update);
+ document.querySelector("#saveSnippet").onclick = () => update(jsyaml.safeLoad(code.value));
+ document.querySelector("#deleteSnippet").onclick = () => update(jsyaml.safeLoad(null));
+ }
+
+ // graph2.tree = structure_tree2(src);
+ graph2.tree = tr.graph;
document.querySelector("#graph2").appendChild(graph2);
- graph.addEventListener("selected", (ev) => {
- const idx = ev.detail;
- const code = document.querySelector("code");
- let c;
- if(Array.isArray(idx)) {
- c = nodes[idx[0]].choose[idx[1]];
- } else {
- c = nodes[idx];
- }
- code.innerHTML = JSON.stringify(c, null, ' ');
-
- })
- document.querySelector("#graph").appendChild(graph);
-
-
}
diff --git a/src/script-graph2.ts b/src/script-graph2.ts
index 795957c..fe72b98 100644
--- a/src/script-graph2.ts
+++ b/src/script-graph2.ts
@@ -34,6 +34,7 @@ class ScriptGraph2 extends LitElement {
r="${this.nodeSize/2}"
class="node"
@click=${node.clickCallback}
+ style=${node.styles}
/>
@@ -177,13 +178,17 @@ class ScriptGraph2 extends LitElement {
let tree = this._draw_tree(this.tree);
return html`