UNPKG

circuit-bricks

Version:

A modular, Lego-style SVG circuit component system for React (ALPHA - Not for production use)

1 lines 9.63 kB
{"version":3,"file":"server-Dcc_xLRK.mjs","names":["builtInComponents: ServerComponentSchema[]","serverComponentRegistry: Record<string, ServerComponentSchema>","schema: ServerComponentSchema","id: string","category: string"],"sources":["../../../src/registry/server.ts"],"sourcesContent":["/**\n * Server-side registry module for component schemas\n *\n * This module provides server-side compatible registry functions\n * without any client-side dependencies or \"use client\" directives.\n * It's specifically designed for use in server-side environments\n * like API routes and LLM integrations.\n */\n\n// Define ComponentSchema interface inline to avoid dependencies\ninterface ServerComponentSchema {\n id: string;\n name: string;\n category: string;\n description: string;\n defaultWidth: number;\n defaultHeight: number;\n ports: Array<{\n id: string;\n x: number;\n y: number;\n type: string;\n label?: string;\n }>;\n properties: Array<{\n key: string;\n label: string;\n type: string;\n unit?: string;\n default: any;\n min?: number;\n max?: number;\n options?: Array<{ value: any; label: string; }>;\n }>;\n svgPath: string;\n}\n\n// Define component schemas inline to avoid imports\nconst builtInComponents: ServerComponentSchema[] = [\n {\n \"id\": \"resistor\",\n \"name\": \"Resistor\",\n \"category\": \"passive\",\n \"description\": \"A passive electrical component that limits current flow\",\n \"defaultWidth\": 60,\n \"defaultHeight\": 20,\n \"ports\": [\n { \"id\": \"left\", \"x\": 0, \"y\": 10, \"type\": \"inout\" },\n { \"id\": \"right\", \"x\": 60, \"y\": 10, \"type\": \"inout\" }\n ],\n \"properties\": [\n {\n \"key\": \"resistance\",\n \"label\": \"Resistance\",\n \"type\": \"number\",\n \"unit\": \"Ω\",\n \"default\": 1000,\n \"min\": 0\n }\n ],\n \"svgPath\": \"M10,10 h10 l5,-5 l10,10 l10,-10 l10,10 l5,-5 h10\"\n },\n {\n \"id\": \"capacitor\",\n \"name\": \"Capacitor\",\n \"category\": \"passive\",\n \"description\": \"A passive electrical component that stores electrical energy\",\n \"defaultWidth\": 40,\n \"defaultHeight\": 30,\n \"ports\": [\n { \"id\": \"left\", \"x\": 0, \"y\": 15, \"type\": \"inout\" },\n { \"id\": \"right\", \"x\": 40, \"y\": 15, \"type\": \"inout\" }\n ],\n \"properties\": [\n {\n \"key\": \"capacitance\",\n \"label\": \"Capacitance\",\n \"type\": \"number\",\n \"unit\": \"F\",\n \"default\": 0.000001,\n \"min\": 0\n }\n ],\n \"svgPath\": \"M15,5 v20 M25,5 v20 M0,15 h15 M25,15 h15\"\n },\n {\n \"id\": \"battery\",\n \"name\": \"Battery\",\n \"category\": \"sources\",\n \"description\": \"A source of electrical energy\",\n \"defaultWidth\": 60,\n \"defaultHeight\": 40,\n \"ports\": [\n { \"id\": \"positive\", \"x\": 60, \"y\": 20, \"type\": \"output\" },\n { \"id\": \"negative\", \"x\": 0, \"y\": 20, \"type\": \"output\" }\n ],\n \"properties\": [\n {\n \"key\": \"voltage\",\n \"label\": \"Voltage\",\n \"type\": \"number\",\n \"unit\": \"V\",\n \"default\": 9,\n \"min\": 0\n }\n ],\n \"svgPath\": \"M10,20 h10 M20,5 v30 M30,10 v20 M40,5 v30 M40,20 h10\"\n },\n {\n \"id\": \"ground\",\n \"name\": \"Ground\",\n \"category\": \"basic\",\n \"description\": \"Electrical ground reference point\",\n \"defaultWidth\": 30,\n \"defaultHeight\": 25,\n \"ports\": [\n { \"id\": \"terminal\", \"x\": 15, \"y\": 0, \"type\": \"input\" }\n ],\n \"properties\": [],\n \"svgPath\": \"M15,0 v10 M5,15 h20 M8,20 h14 M11,25 h8\"\n },\n {\n \"id\": \"led\",\n \"name\": \"LED\",\n \"category\": \"semiconductor\",\n \"description\": \"Light-emitting diode\",\n \"defaultWidth\": 30,\n \"defaultHeight\": 20,\n \"ports\": [\n { \"id\": \"anode\", \"x\": 0, \"y\": 10, \"type\": \"input\" },\n { \"id\": \"cathode\", \"x\": 30, \"y\": 10, \"type\": \"output\" }\n ],\n \"properties\": [\n {\n \"key\": \"color\",\n \"label\": \"Color\",\n \"type\": \"select\",\n \"default\": \"red\",\n \"options\": [\n { \"value\": \"red\", \"label\": \"Red\" },\n { \"value\": \"green\", \"label\": \"Green\" },\n { \"value\": \"blue\", \"label\": \"Blue\" },\n { \"value\": \"yellow\", \"label\": \"Yellow\" }\n ]\n }\n ],\n \"svgPath\": \"M0,10 h8 M8,5 l7,5 l-7,5 z M15,5 v10 M22,10 h8\"\n }\n];\n\n// Server-side registry storage\nconst serverComponentRegistry: Record<string, ServerComponentSchema> = {};\n\n/**\n * Initialize the server-side registry with built-in components\n */\nfunction initializeServerRegistry(): void {\n // Only initialize once\n if (Object.keys(serverComponentRegistry).length > 0) {\n return;\n }\n\n // Register built-in components\n for (const schema of builtInComponents) {\n try {\n // Simple validation - just check if required fields exist\n if (schema && schema.id && schema.name && schema.category) {\n serverComponentRegistry[schema.id] = schema;\n } else {\n console.warn(`Invalid component schema ${schema.id}: missing required fields`);\n }\n } catch (error) {\n console.warn(`Failed to register component ${schema.id}:`, error);\n }\n }\n}\n\n/**\n * Register a component schema in the server-side registry\n */\nexport function registerServerComponent(schema: ServerComponentSchema): void {\n initializeServerRegistry();\n\n // Simple validation - just check if required fields exist\n if (!schema || !schema.id || !schema.name || !schema.category) {\n throw new Error(`Invalid component schema: missing required fields (id, name, category)`);\n }\n\n if (serverComponentRegistry[schema.id]) {\n console.warn(`Component with ID ${schema.id} already exists in server registry. Overwriting.`);\n }\n\n serverComponentRegistry[schema.id] = schema;\n}\n\n/**\n * Get a component schema from the server-side registry by ID\n */\nexport function getServerComponentSchema(id: string): ServerComponentSchema | undefined {\n initializeServerRegistry();\n return serverComponentRegistry[id];\n}\n\n/**\n * Get all component schemas in the server-side registry\n */\nexport function getAllServerComponents(): ServerComponentSchema[] {\n initializeServerRegistry();\n return Object.values(serverComponentRegistry);\n}\n\n/**\n * Get component schemas by category from the server-side registry\n */\nexport function getServerComponentsByCategory(category: string): ServerComponentSchema[] {\n initializeServerRegistry();\n return Object.values(serverComponentRegistry)\n .filter(schema => schema.category === category);\n}\n\n/**\n * Get all unique categories from the server-side registry\n */\nexport function getServerCategories(): string[] {\n initializeServerRegistry();\n const allComponents = getAllServerComponents();\n const categories = [...new Set(allComponents.map(schema => schema.category))];\n return categories.sort();\n}\n\nexport default serverComponentRegistry;\n"],"mappings":";AAsCA,MAAMA,oBAA6C;CACjD;EACE,MAAM;EACN,QAAQ;EACR,YAAY;EACZ,eAAe;EACf,gBAAgB;EAChB,iBAAiB;EACjB,SAAS,CACP;GAAE,MAAM;GAAQ,KAAK;GAAG,KAAK;GAAI,QAAQ;EAAS,GAClD;GAAE,MAAM;GAAS,KAAK;GAAI,KAAK;GAAI,QAAQ;EAAS,CACrD;EACD,cAAc,CACZ;GACE,OAAO;GACP,SAAS;GACT,QAAQ;GACR,QAAQ;GACR,WAAW;GACX,OAAO;EACR,CACF;EACD,WAAW;CACZ;CACD;EACE,MAAM;EACN,QAAQ;EACR,YAAY;EACZ,eAAe;EACf,gBAAgB;EAChB,iBAAiB;EACjB,SAAS,CACP;GAAE,MAAM;GAAQ,KAAK;GAAG,KAAK;GAAI,QAAQ;EAAS,GAClD;GAAE,MAAM;GAAS,KAAK;GAAI,KAAK;GAAI,QAAQ;EAAS,CACrD;EACD,cAAc,CACZ;GACE,OAAO;GACP,SAAS;GACT,QAAQ;GACR,QAAQ;GACR,WAAW;GACX,OAAO;EACR,CACF;EACD,WAAW;CACZ;CACD;EACE,MAAM;EACN,QAAQ;EACR,YAAY;EACZ,eAAe;EACf,gBAAgB;EAChB,iBAAiB;EACjB,SAAS,CACP;GAAE,MAAM;GAAY,KAAK;GAAI,KAAK;GAAI,QAAQ;EAAU,GACxD;GAAE,MAAM;GAAY,KAAK;GAAG,KAAK;GAAI,QAAQ;EAAU,CACxD;EACD,cAAc,CACZ;GACE,OAAO;GACP,SAAS;GACT,QAAQ;GACR,QAAQ;GACR,WAAW;GACX,OAAO;EACR,CACF;EACD,WAAW;CACZ;CACD;EACE,MAAM;EACN,QAAQ;EACR,YAAY;EACZ,eAAe;EACf,gBAAgB;EAChB,iBAAiB;EACjB,SAAS,CACP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