forked from aexol-studio/tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added serialisation and deserialization
- Loading branch information
Showing
9 changed files
with
415 additions
and
25 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { NodeSerialized } from "./Node"; | ||
import { LinkSerialized } from "./Link"; | ||
import { DiagramTheme } from "../Models/index"; | ||
|
||
export interface Format{ | ||
nodes:NodeSerialized[], | ||
links:LinkSerialized[], | ||
theme?:DiagramTheme; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Link } from "../Models/Link"; | ||
import { Node } from "../Models/Node"; | ||
|
||
export interface LinkSerialized extends Pick<Link, "centerPoint"> { | ||
iId: string; | ||
oId: string; | ||
} | ||
|
||
export const serializeLink = ({ centerPoint, i, o }: Link): LinkSerialized => ({ | ||
centerPoint, | ||
iId: i.id, | ||
oId: o.id | ||
}); | ||
export const deserializeLink = ( | ||
{ centerPoint, iId, oId }: LinkSerialized, | ||
nodes: Node[] | ||
): Link => ({ | ||
centerPoint, | ||
i: nodes.find(n => n.id === iId)!, | ||
o: nodes.find(n => n.id === oId)! | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Node } from "../Models/Node"; | ||
import { NodeDefinition } from "../Models/NodeDefinition"; | ||
|
||
export interface NodeDefinitionSerialized | ||
extends Pick<NodeDefinition, "type" | "object" | "main"> {} | ||
|
||
export interface NodeSerialized | ||
extends Pick< | ||
Node, | ||
"id" | "name" | "description" | "x" | "y" | "options" | "readonly" | ||
> { | ||
definition: Pick<NodeDefinition, "type" | "object" | "main">; | ||
editsDefinition?: Pick<NodeDefinition, "type" | "object" | "main">; | ||
} | ||
|
||
export const serializeNodeDefinition = ({ | ||
type, | ||
object, | ||
main | ||
}: NodeDefinition): NodeDefinitionSerialized => ({ | ||
main, | ||
object, | ||
type | ||
}); | ||
|
||
export const deserializeNodeDefinition = ( | ||
{ type, object, main }: NodeDefinitionSerialized, | ||
definitions: NodeDefinition[] | ||
): NodeDefinition => { | ||
return definitions.find( | ||
d => d.type === type && d.object === object && d.main === main | ||
)!; | ||
}; | ||
|
||
export const serializeNode = ({ | ||
id, | ||
name, | ||
description, | ||
x, | ||
y, | ||
options, | ||
readonly, | ||
definition, | ||
editsDefinition | ||
}: Node): NodeSerialized => ({ | ||
id, | ||
name, | ||
description, | ||
x, | ||
y, | ||
options, | ||
readonly, | ||
definition: serializeNodeDefinition(definition), | ||
editsDefinition: editsDefinition | ||
? serializeNodeDefinition(editsDefinition) | ||
: undefined | ||
}); | ||
|
||
export const deserializeNode = ( | ||
{ | ||
id, | ||
x, | ||
y, | ||
name, | ||
options, | ||
definition, | ||
description, | ||
editsDefinition, | ||
readonly | ||
}: NodeSerialized, | ||
definitions: NodeDefinition[] | ||
): Node => { | ||
return { | ||
id, | ||
x, | ||
y, | ||
name, | ||
options, | ||
description, | ||
readonly, | ||
definition: deserializeNodeDefinition(definition, definitions), | ||
editsDefinition: editsDefinition | ||
? deserializeNodeDefinition(editsDefinition, definitions) | ||
: undefined | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { expect } from "chai"; | ||
import "mocha"; | ||
import { NodeDefinition } from "../Models/NodeDefinition"; | ||
import { Utils } from "../Utils/index"; | ||
import { Node } from "../Models/Node"; | ||
import { Link } from "../Models/Link"; | ||
import { Serializer } from "./index"; | ||
|
||
// TODO: This test case should be improved to test more complicated schemas | ||
|
||
describe("Serialize and Deserialize", () => { | ||
it("should serialize and deserialize", () => { | ||
const nodeDefinitions: NodeDefinition[] = [ | ||
{ | ||
id: Utils.generateId(), | ||
type: "dummy", | ||
node: { | ||
name: "dummy" | ||
} | ||
} | ||
]; | ||
nodeDefinitions[0].acceptsInputs = [nodeDefinitions[0]]; | ||
const nodes: Node[] = [ | ||
{ | ||
definition: nodeDefinitions[0], | ||
name: "dummy", | ||
id: Utils.generateId(), | ||
options: ["required"], | ||
x: 0, | ||
y: 0, | ||
description: "Hello world", | ||
editsDefinition: undefined, | ||
readonly: undefined | ||
}, | ||
{ | ||
definition: nodeDefinitions[0], | ||
name: "dummy", | ||
id: Utils.generateId(), | ||
options: ["required"], | ||
x: 0, | ||
y: 0, | ||
description: "Hello world 2", | ||
editsDefinition: undefined, | ||
readonly: undefined | ||
} | ||
]; | ||
const links: Link[] = [ | ||
{ | ||
centerPoint: 0.5, | ||
i: nodes[0], | ||
o: nodes[1] | ||
} | ||
]; | ||
const serialized = Serializer.serialize({ | ||
nodes, | ||
links | ||
}); | ||
const deserialize = Serializer.deserialize(serialized, nodeDefinitions); | ||
expect(JSON.stringify(serialized)).equal( | ||
JSON.stringify(Serializer.serialize(deserialize)) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { DiagramState } from "../Models/index"; | ||
import { Format } from "./Format"; | ||
import { serializeNode, deserializeNode } from "./Node"; | ||
import { serializeLink, deserializeLink } from "./Link"; | ||
import { NodeDefinition } from "../Models/NodeDefinition"; | ||
|
||
export class Serializer { | ||
static serialize(state: Pick<DiagramState, "nodes" | "links">): Format { | ||
return { | ||
nodes: state.nodes.map(serializeNode), | ||
links: state.links.map(serializeLink) | ||
}; | ||
} | ||
static deserialize( | ||
state: Format, | ||
definitions: NodeDefinition[] | ||
): Pick<DiagramState, "nodes" | "links"> { | ||
const nodes = state.nodes.map(n => deserializeNode(n, definitions)); | ||
const links = state.links.map(l => deserializeLink(l, nodes)); | ||
return { | ||
nodes, | ||
links | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters