@silexlabs/grapesjs-symbols
Version:
Symbols for GrapesJS
85 lines • 2.69 kB
JavaScript
/**
* Make sure we can drop a symbol instance
*/
export function allowDrop(editor, component) {
if (!component) {
throw new Error('No component provided to check drop permission.');
}
// Check if the component or one of its parents is a symbol instance
let current = component;
do {
const info = editor.Components.getSymbolInfo(current);
if (info === null || info === void 0 ? void 0 : info.isSymbol) {
return false;
}
current = current.parent();
} while (current);
// If no symbol instance is found, allow the drop
return true;
}
/**
* Get all the symbols
*/
export function getSymbols(editor) {
return editor.Components.getSymbols().map(symbol => {
return editor.Components.getSymbolInfo(symbol);
});
// const symbols = editor.Components.getSymbols()
// return symbols.map(symbol => {
// const info = editor.Components.getSymbolInfo(symbol)
// return {
// symbol,
// info,
// }
// })
}
/**
* Get a symbol by its ID
*/
export function getSymbol(editor, component) {
const info = editor.Components.getSymbolInfo(component);
if (info === null || info === void 0 ? void 0 : info.isSymbol) {
return info;
}
return null;
}
/**
* Create a new symbol from a component
*/
export function createSymbol(editor, component) {
if (!component) {
throw new Error('No component provided to create a symbol.');
}
const symbol = editor.Components.addSymbol(component);
if (symbol) {
return editor.Components.getSymbolInfo(symbol);
}
else {
throw new Error('Failed to create symbol from the provided component.');
}
}
/**
* Bind a symbol instance to a component
*/
export function unbindSymbolInstance(editor, component) {
const info = editor.Components.getSymbolInfo(component);
if (info === null || info === void 0 ? void 0 : info.isInstance) {
editor.Components.detachSymbol(component); // Detach instance from its symbol
}
else {
throw new Error('Component is not a symbol instance.');
}
}
/**
* Delete a symbol instance and all its references
* This will remove the main symbol but leave the instances
*/
export function deleteSymbol(editor, symbol) {
const symbolInfo = editor.Components.getSymbolInfo(symbol);
if (!symbolInfo) {
throw new Error(`Can not delete symbol: symbol info not found for ${(symbol === null || symbol === void 0 ? void 0 : symbol.getId()) || 'unknown'}`);
}
// Delete the symbol
symbol === null || symbol === void 0 ? void 0 : symbol.remove();
}
//# sourceMappingURL=utils.js.map