Skip to content

Instantly share code, notes, and snippets.

@koenbok
Created October 5, 2016 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koenbok/e6d7315635dade292035ef69896c44dc to your computer and use it in GitHub Desktop.
Save koenbok/e6d7315635dade292035ef69896c44dc to your computer and use it in GitHub Desktop.
// Internal types
interface NodeType {
id: string
type: string
attributes: Map<string, any>
children: List<NodeType>
}
interface TreeType extends NodeType {
_nodes: Map<string, NodeType>
_parents: Map<string, NodeType>
_previous: TreeType
}
// The manager class
class Co {
readonly root: TreeType
// Create and remove (updates root)
create(type: string, parent: string|null|NodeType= null, attributes: any = {}): NodeType {}
remove(id: string) {}
// Getters
get(id: string): NodeType {}
getRoot(): NodeType {}
getChildren(id: string): List<NodeType> {}
getParent(id: string): NodeType {}
// Setters (updates root)
setParent(id: string, parent: NodeType|null) {}
setAttributes(id: string, attributes: any) {}
setAttribute(id: string, name: string, value: any) {}
// Utilities
visitAncestors(id: string, visitor: (id: string, stop: () => void) => boolean) NodeType[] {}
visitDescendants(id: string, visitor: (id: string, stop: () => void) => boolean) NodeType[] {}
visitSiblings(id: string, visitor: (id: string, stop: () => void) => boolean) NodeType[] {}
// History (just as an example)
undo() {}
}
// Code examples (pseudo code)
// Start a new tree for a vekter project
project.co = new Co()
// Render the canvas and the ui
canvas.render(project.co.root)
chrome.render(project.co.root)
// Create a new shape on the canvas
...onMouseDown(event) => {
let parentId = document.elementFromPoint(mouse.point).id
project.co.create("layer", parentId, {x: mouse.point.x, y: mouse.point.y})
}
// Modify a shape on the canvas
...onMouseMove(event) => {
let elementId = document.elementFromPoint(mouse.point).id
project.co.setAttributes(elementId, {x: mouse.point.x, y: mouse.point.y})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment