circuit-bricks
Version:
A modular, Lego-style SVG circuit component system for React (ALPHA - Not for production use)
89 lines (86 loc) • 3.12 kB
JavaScript
"use client";
import { battery_default, capacitor_default, diode_default, ground_default, ic_default, led_default, resistor_default, switch_default, transistor_npn_default, validateComponentSchema, voltage_source_default } from "./ic-CL9tBH6p.mjs";
//#region src/registry/components/index.ts
const registerBuiltInComponents = () => {
registerComponent(resistor_default);
registerComponent(capacitor_default);
registerComponent(ground_default);
registerComponent(switch_default);
registerComponent(battery_default);
registerComponent(voltage_source_default);
registerComponent(diode_default);
registerComponent(transistor_npn_default);
registerComponent(led_default);
registerComponent(ic_default);
};
var components_default = registerBuiltInComponents;
//#endregion
//#region src/registry/index.ts
const componentRegistry = {};
/**
* Register a component schema in the registry
*
* This function adds a new component schema to the registry or updates an existing one.
* Custom components can be registered to extend the library with new circuit elements.
*
* @param {ComponentSchema} schema - The component schema to register
*
* @example
* // Register a custom LED component
* registerComponent({
* id: 'custom-led',
* name: 'Custom LED',
* category: 'output',
* description: 'A light-emitting diode with customizable color',
* defaultWidth: 30,
* defaultHeight: 20,
* ports: [
* { id: 'anode', x: 0, y: 10, type: 'input' },
* { id: 'cathode', x: 30, y: 10, type: 'output' }
* ],
* properties: [
* { id: 'color', name: 'Color', type: 'color', default: '#ff0000' }
* ],
* svgPath: `<circle cx="15" cy="10" r="8" fill="currentColor" />`
* });
*/
function registerComponent(schema) {
const validationResult = validateComponentSchema(schema);
if (!validationResult.success) throw new Error(`Invalid component schema: ${validationResult.error}`);
if (componentRegistry[schema.id]) console.warn(`Component with ID ${schema.id} already exists in registry. Overwriting.`);
componentRegistry[schema.id] = schema;
}
/**
* Get a component schema from the registry by ID
*
* Retrieves a component's schema definition by its unique identifier.
* Returns undefined if no component with the specified ID exists.
*
* @param {string} id - The unique identifier of the component
* @returns {ComponentSchema | undefined} The component schema or undefined if not found
*
* @example
* const resistorSchema = getComponentSchema('resistor');
* if (resistorSchema) {
* console.log(`Resistor has ${resistorSchema.ports.length} ports`);
* }
*/
function getComponentSchema(id) {
return componentRegistry[id];
}
/**
* Get all component schemas in the registry
*/
function getAllComponents() {
return Object.values(componentRegistry);
}
/**
* Get component schemas by category
*/
function getComponentsByCategory(category) {
return Object.values(componentRegistry).filter((schema) => schema.category === category);
}
components_default();
//#endregion
export { getAllComponents, getComponentSchema, getComponentsByCategory, registerComponent };
//# sourceMappingURL=registry-DltAe79Q.mjs.map