fix(lit-node-editor): resolve rendering, global style leakage, and resizing issues
Build and Deploy / build-and-deploy (push) Has been cancelled
Build and Deploy / build-and-deploy (push) Has been cancelled
This commit is contained in:
@@ -50,9 +50,15 @@ export class Canvas implements ReactiveController {
|
|||||||
context: MatrixContext = defaultMatrix;
|
context: MatrixContext = defaultMatrix;
|
||||||
ctx: CanvasRenderingContext2D;
|
ctx: CanvasRenderingContext2D;
|
||||||
|
|
||||||
|
private _keydownListener = (e: KeyboardEvent) => this.onKeyDown(e);
|
||||||
|
private _preventDefault = (e: Event) => e.preventDefault();
|
||||||
|
|
||||||
hostConnected() {}
|
hostConnected() {}
|
||||||
|
|
||||||
hostDisconnected() {}
|
hostDisconnected() {
|
||||||
|
window.removeEventListener("keydown", this._keydownListener);
|
||||||
|
window.removeEventListener("DOMMouseScroll", this._preventDefault);
|
||||||
|
}
|
||||||
|
|
||||||
nodeTree(): Tree {
|
nodeTree(): Tree {
|
||||||
return {
|
return {
|
||||||
@@ -139,16 +145,11 @@ export class Canvas implements ReactiveController {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Keyboard Events
|
// Keyboard Events
|
||||||
window.addEventListener("keydown", (e) => this.onKeyDown(e));
|
window.addEventListener("keydown", this._keydownListener);
|
||||||
// window.addEventListener("mousewheel", (e) => {
|
window.addEventListener("DOMMouseScroll", this._preventDefault, {
|
||||||
// e.preventDefault();
|
passive: false,
|
||||||
// });
|
|
||||||
window.addEventListener("DOMMouseScroll", (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: https://github.com/shuding/apple-pencil-safari-api-test
|
|
||||||
|
|
||||||
this.render();
|
this.render();
|
||||||
this.host.requestUpdate();
|
this.host.requestUpdate();
|
||||||
}
|
}
|
||||||
@@ -440,8 +441,8 @@ export class Canvas implements ReactiveController {
|
|||||||
resize(size?: Size) {
|
resize(size?: Size) {
|
||||||
const width = size?.width ?? window.innerWidth;
|
const width = size?.width ?? window.innerWidth;
|
||||||
const height = size?.height ?? window.innerHeight;
|
const height = size?.height ?? window.innerHeight;
|
||||||
this.canvas.setAttribute("width", `${width}px`);
|
this.canvas.setAttribute("width", `${width}`);
|
||||||
this.canvas.setAttribute("height", `${height}px`);
|
this.canvas.setAttribute("height", `${height}`);
|
||||||
this.canvas.width = width;
|
this.canvas.width = width;
|
||||||
this.canvas.height = height;
|
this.canvas.height = height;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import "./styles.css";
|
|
||||||
|
|
||||||
import { html, css, LitElement } from "lit";
|
import { html, css, LitElement } from "lit";
|
||||||
import { customElement } from "lit/decorators.js";
|
import { customElement, query } from "lit/decorators.js";
|
||||||
|
|
||||||
import { CanvasNode, Canvas } from "./canvas";
|
import { CanvasNode, Canvas } from "./canvas";
|
||||||
import { BaseTreeNode, styles as treeStyles, template as treeTemplate } from "./ui/tree-view";
|
import { BaseTreeNode, styles as treeStyles, template as treeTemplate } from "./ui/tree-view";
|
||||||
@@ -17,27 +15,47 @@ export class NodeEditor extends LitElement {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@query("#output")
|
||||||
|
outputContainer!: HTMLDivElement;
|
||||||
|
|
||||||
|
private _resizeObserver = new ResizeObserver((entries) => {
|
||||||
|
for (const entry of entries) {
|
||||||
|
const { width, height } = entry.contentRect;
|
||||||
|
this.editor.resize({ width, height });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
static styles = [
|
static styles = [
|
||||||
treeStyles,
|
treeStyles,
|
||||||
css`
|
css`
|
||||||
|
:host {
|
||||||
|
display: block;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
main {
|
main {
|
||||||
height: 100vh;
|
height: 100%;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
}
|
}
|
||||||
#output {
|
#output {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
.sidebar {
|
.sidebar {
|
||||||
width: ${PROPERTY_WIDTH}px;
|
width: ${PROPERTY_WIDTH}px;
|
||||||
|
overflow-y: auto;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
border-right: 1px solid #ddd;
|
||||||
}
|
}
|
||||||
#properties {
|
#properties {
|
||||||
width: ${PROPERTY_WIDTH}px;
|
width: ${PROPERTY_WIDTH}px;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
overflow-y: scroll;
|
overflow-y: auto;
|
||||||
background-color: #eee;
|
background-color: #eee;
|
||||||
|
border-left: 1px solid #ddd;
|
||||||
}
|
}
|
||||||
.property {
|
.property {
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -271,6 +289,8 @@ export class NodeEditor extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
firstUpdated() {
|
firstUpdated() {
|
||||||
|
this._resizeObserver.observe(this.outputContainer);
|
||||||
|
|
||||||
const amount = 10;
|
const amount = 10;
|
||||||
// Create random canvas nodes
|
// Create random canvas nodes
|
||||||
for (let i = 0; i < amount; i++) {
|
for (let i = 0; i < amount; i++) {
|
||||||
@@ -283,13 +303,11 @@ export class NodeEditor extends LitElement {
|
|||||||
const target: CanvasNode = this.editor.store.nodes[i + amount / 2];
|
const target: CanvasNode = this.editor.store.nodes[i + amount / 2];
|
||||||
this.editor.store.linkNodes(source, target, "simple");
|
this.editor.store.linkNodes(source, target, "simple");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
window.addEventListener("resize", () => {
|
disconnectedCallback() {
|
||||||
this.editor.resize({
|
super.disconnectedCallback();
|
||||||
width: window.innerWidth - PROPERTY_WIDTH,
|
this._resizeObserver.disconnect();
|
||||||
height: window.innerHeight,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addRandomNode(i: number = this.editor.store.nodes.length) {
|
addRandomNode(i: number = this.editor.store.nodes.length) {
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
body {
|
:host {
|
||||||
margin: 0;
|
display: block;
|
||||||
padding: 0;
|
|
||||||
width: 100%;
|
|
||||||
height: 100vh;
|
|
||||||
}
|
|
||||||
node-editor {
|
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user