Skip to content
Snippets Groups Projects
Commit 5584663f authored by Maximilian Giller's avatar Maximilian Giller :squid:
Browse files

Implements somewhat working node type editor

parent d9aa637d
No related branches found
No related tags found
1 merge request!2Implemented editor in the react framework
Pipeline #56803 passed
...@@ -10,6 +10,7 @@ import { Node } from "../structures/graph/node"; ...@@ -10,6 +10,7 @@ import { Node } from "../structures/graph/node";
import { HistoryNavigator } from "./historynavigator"; import { HistoryNavigator } from "./historynavigator";
import { GraphElement } from "../structures/graph/graphelement"; import { GraphElement } from "../structures/graph/graphelement";
import { Link } from "../structures/graph/link"; import { Link } from "../structures/graph/link";
import { NodeTypesEditor } from "./nodetypeseditor";
type propTypes = any; type propTypes = any;
type stateTypes = { type stateTypes = {
...@@ -420,45 +421,50 @@ export class Editor extends React.PureComponent<propTypes, stateTypes> { ...@@ -420,45 +421,50 @@ export class Editor extends React.PureComponent<propTypes, stateTypes> {
onChange={this.forceUpdate} onChange={this.forceUpdate}
/> />
<hr /> <hr />
<div> <h3>Node types</h3>
<input <NodeTypesEditor
id="node-labe-visibility" onChange={this.forceUpdate}
type={"checkbox"} graph={this.state.graph}
checked={this.state.visibleLabels} />
onChange={(event) => { <hr />
const newValue = event.target.checked; <h3>Settings</h3>
if (newValue == this.state.visibleLabels) { <input
return; id="node-labe-visibility"
} type={"checkbox"}
checked={this.state.visibleLabels}
this.setState({ onChange={(event) => {
visibleLabels: newValue, const newValue = event.target.checked;
}); if (newValue == this.state.visibleLabels) {
}} return;
/> }
<label htmlFor="node-labe-visibility">
Node labels this.setState({
</label> visibleLabels: newValue,
<br /> });
<input }}
id="connect-on-drag" />
type={"checkbox"} <label htmlFor="node-labe-visibility">
checked={this.state.connectOnDrag} Node labels
onChange={(event) => { </label>
const newValue = event.target.checked; <br />
if (newValue == this.state.connectOnDrag) { <input
return; id="connect-on-drag"
} type={"checkbox"}
checked={this.state.connectOnDrag}
this.setState({ onChange={(event) => {
connectOnDrag: newValue, const newValue = event.target.checked;
}); if (newValue == this.state.connectOnDrag) {
}} return;
/> }
<label htmlFor="connect-on-drag">
Connect nodes when dragged this.setState({
</label> connectOnDrag: newValue,
</div> });
}}
/>
<label htmlFor="connect-on-drag">
Connect nodes when dragged
</label>
</div> </div>
{this.state.graph ? ( {this.state.graph ? (
<ReactForceGraph2d <ReactForceGraph2d
......
div#ks-editor .node-type-name {
width: 45%;
}
div#ks-editor .node-type-color {
width: 5rem;
}
import React from "react";
import { ReactNode } from "react";
import { Graph } from "../structures/graph/graph";
import { NodeType } from "../structures/graph/nodetype";
import "./nodetypeentry.css";
type propTypes = {
graph: Graph;
type: NodeType;
onChange: { (): void };
};
export class NodeTypeEntry extends React.Component<propTypes> {
constructor(props: propTypes) {
super(props);
this.deleteType = this.deleteType.bind(this);
this.handleTextChange = this.handleTextChange.bind(this);
}
private deleteType() {
this.props.type.delete();
}
private isValidColor(color: string): boolean {
if (color.length <= 0 || color[0] !== "#") {
return false;
}
const colorCode = color.substring(1);
if (!(colorCode.length === 3 || colorCode.length === 6)) {
return false;
}
colorCode;
}
/**
* Generic function for handeling a changing text input and applying the new value to the node type.
* @param event Change event of text input.
* @param property Property to give new value.
*/
private handleTextChange(event: any, property: string) {
const newValue = event.target.value;
// Actual change?
if ((this.props.type as any)[property] == newValue) {
return;
}
//TODO: Make sure, that this event is not triggered to quickly!
(this.props.type as any)[property] = newValue;
this.props.type.graph.storeCurrentData(
"Changed " + property + " of type [" + this.props.type + "]"
);
this.props.onChange();
}
render(): ReactNode {
return (
<li className="node-type">
<input
className="node-type-name"
type={"text"}
value={this.props.type.name}
onChange={(event) => this.handleTextChange(event, "name")}
/>
<input
className="node-type-color"
type={"text"}
value={this.props.type.color}
onChange={(event) => this.handleTextChange(event, "color")}
/>
{this.props.graph && this.props.graph.types.length > 1 ? (
<button onClick={this.deleteType}>Delete</button>
) : (
""
)}
</li>
);
}
}
div#ks-editor #node-types-editor ul {
list-style-type: none;
margin: 0;
padding: 0;
}
import React from "react";
import { ReactNode } from "react";
import { Graph } from "../structures/graph/graph";
import "./nodetypeseditor.css";
import { NodeTypeEntry } from "./nodetypeentry";
import { NodeType } from "../structures/graph/nodetype";
type propTypes = {
graph: Graph;
onChange: { (): void };
};
export class NodeTypesEditor extends React.Component<propTypes> {
constructor(props: propTypes) {
super(props);
this.addType = this.addType.bind(this);
}
private addType() {
const type = new NodeType(this.props.graph);
type.add();
}
render(): ReactNode {
if (this.props.graph === undefined) {
return "No graph selected.";
}
return (
<div id="node-types-editor">
<ul>
{this.props.graph.types.map((type) => (
<NodeTypeEntry
onChange={this.props.onChange}
key={type.id}
type={type}
graph={this.props.graph}
/>
))}
</ul>
<button onClick={this.addType}>Add type</button>
</div>
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment