@dndbuilder.com/react
Version:
Drag and drop builder for React
171 lines (170 loc) • 5.63 kB
JavaScript
import n from "../../../_virtual/cjs.js";
const t = class t {
// Private constructor to prevent direct instantiation
constructor() {
if (this.registeredBlocks = {}, this.breakpoints = {}, this.groupOrder = [], t.instance)
throw new Error("Use BuilderRegistry.getInstance() to get the instance");
t.instance = this;
}
// Static method to get the instance
static getInstance() {
return t.instance || (t.instance = new t()), t.instance;
}
/**
* Register multiple blocks at once
* @param blocks - Array of block configurations to register
*/
registerBlocks(e) {
return e.forEach((r) => {
this.registerBlock(r);
}), this;
}
/**
* Register a single block configuration
* @param block - Block configuration to register
* @throws Error if the block type is already registered
*/
registerBlock(e) {
if (this.registeredBlocks[e.type])
throw new Error(`Block type "${e.type}" already registered`);
return this.registeredBlocks[e.type] = e, this;
}
getRegisteredBlocks() {
return this.registeredBlocks;
}
/**
* Get all registered blocks
* @returns Object containing all registered blocks
*/
getBlocks() {
return Object.values(this.registeredBlocks);
}
/**
* Get a block by its type
* @param type - The type of the block to retrieve
* @returns The block configuration if found, otherwise undefined
*/
getBlock(e) {
if (!this.registeredBlocks[e]) {
console.warn(`Block type "${e}" is not registered`);
return;
}
return this.registeredBlocks[e];
}
/**
* Get blocks by their group
* @param group - The group to filter blocks by
* @returns Array of blocks that belong to the specified group
*/
getBlocksByGroup(e) {
return Object.values(this.registeredBlocks).filter((r) => r.group === e);
}
/**
* Get all registered block types
* @returns Array of block type strings
*/
getBlockTypes() {
return Object.keys(this.registeredBlocks);
}
/**
* Get all registered groups
* @returns Array of group configurations
*/
setGroupsOrder(e) {
return this.groupOrder = e, this;
}
/**
* Get the order of registered groups
* @returns Array of block groups in the order they were registered
*/
getGroupsOrder() {
return this.groupOrder;
}
/**
* Get the order index of a specific group
* @param group - The group to find the order index for
* @returns The index of the group in the registered order, or -1 if not found
*/
getGroupOrder(e) {
return this.groupOrder.indexOf(e);
}
/**
* Get all registered breakpoints
* @returns Object containing all registered breakpoints
*/
registerBreakpoints(e) {
return e.forEach((r) => {
this.registerBreakpoint(r);
}), this;
}
/**
* Register a single breakpoint configuration
* @param breakpoint - The breakpoint configuration to register
* @throws Error if the breakpoint key is already registered
*/
registerBreakpoint(e) {
if (this.breakpoints[e.key])
throw new Error(`Breakpoint "${e.key}" is already registered`);
return this.breakpoints[e.key] = e, this;
}
/**
* Get a specific breakpoint by its key
* @param key - The key of the breakpoint to retrieve
* @returns The breakpoint configuration if found, otherwise undefined
*/
getBreakpoint(e) {
if (!this.breakpoints[e])
throw new Error(`Breakpoint "${e}" is not registered`);
return this.breakpoints[e];
}
/**
* Get all registered breakpoints
* @returns Array of all registered breakpoint configurations
*/
getBreakpoints() {
return Object.values(this.breakpoints);
}
/**
* Get a media query string for a specific breakpoint
* @param key - The key of the breakpoint to generate the media query for
* @returns Media query string for the specified breakpoint
*/
getMediaQuery(e) {
const r = this.getBreakpoint(e);
return ` (max-width: ${r.maxWidth}px) and (min-width: ${r.minWidth}px)`;
}
/**
* Merge custom configuration with existing configuration
* @param config - Custom builder configuration to merge
* @returns The BuilderRegistry instance for method chaining
*/
mergeConfig(e) {
return e.blocks && e.blocks.length > 0 && e.blocks.forEach((r) => {
if (r.type) {
const s = this.registeredBlocks[r.type];
s ? this.registeredBlocks[r.type] = n(s, r, {
arrayMerge: (d, i) => i
}) : r.label !== void 0 && r.component !== void 0 && r.settings !== void 0 && r.controls !== void 0 ? this.registerBlock(r) : console.warn(
`Block "${r.type}" is missing required properties and cannot be registered`
);
} else
console.warn("Block with undefined type encountered and skipped");
}), e.groups && e.groups.length > 0 && this.setGroupsOrder(e.groups), e.breakpoints && e.breakpoints.length > 0 && e.breakpoints.forEach((r) => {
if (r.key !== void 0) {
const s = this.breakpoints[r.key];
s ? this.breakpoints[r.key] = n(s, r, {
arrayMerge: (d, i) => i
}) : r.label !== void 0 && r.icon !== void 0 && r.previewWidth !== void 0 && r.maxWidth !== void 0 && r.minWidth !== void 0 ? this.registerBreakpoint(r) : console.warn(
`Breakpoint "${r.key}" is missing required properties and cannot be registered`
);
} else
console.warn("Breakpoint with undefined key encountered and skipped");
}), this;
}
};
t.instance = null;
let o = t;
export {
o as BuilderRegistry
};
//# sourceMappingURL=registry.js.map