Skip to content

Commit

Permalink
Add JSON schema for JsonCanvas, CanvasNode, CanvasColor, and Edge int…
Browse files Browse the repository at this point in the history
…erfaces. Define properties and types for nodes and edges. Include descriptions for clarity. Create TypeScript interfaces for JsonCanvas, CanvasNode, TextNode, FileNode, LinkNode, GroupNode, AllNodes, and Edge with detailed attributes and types.

alternative proposal for obsidianmd#10
  • Loading branch information
Mearman committed Mar 14, 2024
1 parent ded83f7 commit 90b330f
Show file tree
Hide file tree
Showing 2 changed files with 281 additions and 0 deletions.
163 changes: 163 additions & 0 deletions jsoncanvas.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/JsonCanvas",
"definitions": {
"JsonCanvas": {
"type": "object",
"properties": {
"nodes": {
"type": "array",
"items": {
"$ref": "#/definitions/CanvasNode"
}
},
"edges": {
"type": "array",
"items": {
"$ref": "#/definitions/Edge"
}
}
},
"additionalProperties": false,
"description": "Top-level interface for the JSON Canvas Spec"
},
"CanvasNode": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"text",
"file",
"link",
"group"
]
},
"x": {
"type": "number"
},
"y": {
"type": "number"
},
"width": {
"type": "number"
},
"height": {
"type": "number"
},
"color": {
"$ref": "#/definitions/CanvasColor"
}
},
"required": [
"id",
"type",
"x",
"y",
"width",
"height"
],
"additionalProperties": false,
"description": "Base node interface for common attributes"
},
"CanvasColor": {
"anyOf": [
{
"type": "string"
},
{
"type": "number",
"const": 1
},
{
"type": "number",
"const": 2
},
{
"type": "number",
"const": 3
},
{
"type": "number",
"const": 4
},
{
"type": "number",
"const": 5
},
{
"type": "number",
"const": 6
}
],
"description": "Type for color, either a string for hex values or a number for preset colors"
},
"Edge": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"fromNode": {
"type": "string"
},
"fromSide": {
"type": "string",
"enum": [
"top",
"right",
"bottom",
"left"
],
"description": "Optional side of the node where the edge starts"
},
"fromEnd": {
"type": "string",
"enum": [
"none",
"arrow"
],
"description": "Optional style of the edge end"
},
"toNode": {
"type": "string"
},
"toSide": {
"type": "string",
"enum": [
"top",
"right",
"bottom",
"left"
],
"description": "Optional side of the node where the edge ends"
},
"toEnd": {
"type": "string",
"enum": [
"none",
"arrow"
],
"description": "Optional style of the edge end"
},
"color": {
"$ref": "#/definitions/CanvasColor"
},
"label": {
"type": "string",
"description": "Optional label for the edge"
}
},
"required": [
"id",
"fromNode",
"toNode"
],
"additionalProperties": false,
"description": "Edge interface for connections between nodes"
}
}
}
118 changes: 118 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Top-level interface for the JSON Canvas Spec
*/
export interface JsonCanvas {
nodes?: CanvasNode[];
edges?: Edge[];
}

export default JsonCanvas;

/**
* Base node interface for common attributes
*/
export interface CanvasNode {
id: string;
type: "text" | "file" | "link" | "group";
x: number;
y: number;
width: number;
height: number;
color?: CanvasColor;
}

/**
* Extended node interface for TextNode type
*/
export interface TextNode extends CanvasNode {
type: "text";
/**
* Text content with Markdown syntax
*/
text: string;
}

/**
* Extended node interface for FileNode type
*/
export interface FileNode extends CanvasNode {
type: "file";
/**
* Path to the file
*/
file: string;
/**
* Optional subpath within the file
*/
subpath?: string;
}

/**
* Extended node interface for LinkNode type
*/
export interface LinkNode extends CanvasNode {
type: "link";
/**
* URL the node references
*/
url: string;
}

/**
* Extended node interface for GroupNode type
*/
export interface GroupNode extends CanvasNode {
type: "group";
/**
* Optional text label for the group
*/
label?: string;
/**
* Optional path to a background image
*/
background?: string;
/**
* Optional rendering style of the background
*/
backgroundStyle?: "cover" | "ratio" | "repeat";
}

/**
* Union type for all node types
*/
export type AllNodes = TextNode | FileNode | LinkNode | GroupNode;

/**
* Edge interface for connections between nodes
*/
export interface Edge {
id: string;
fromNode: string;
/**
* Optional side of the node where the edge starts
*/
fromSide?: "top" | "right" | "bottom" | "left";
/**
* Optional style of the edge end
*/
fromEnd?: "none" | "arrow";
toNode: string;
/**
* Optional side of the node where the edge ends
*/
toSide?: "top" | "right" | "bottom" | "left";
/**
* Optional style of the edge end
*/
toEnd?: "none" | "arrow";
color?: CanvasColor;
/**
* Optional label for the edge
*/
label?: string;
}

/**
* Type for color, either a string for hex values or a number for preset colors
*/
export type CanvasColor = string | 1 | 2 | 3 | 4 | 5 | 6;

0 comments on commit 90b330f

Please sign in to comment.