@silexlabs/grapesjs-symbols
Version:
Symbols for GrapesJS
93 lines • 2.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.allowDrop = allowDrop;
exports.getSymbols = getSymbols;
exports.getSymbol = getSymbol;
exports.createSymbol = createSymbol;
exports.unbindSymbolInstance = unbindSymbolInstance;
exports.deleteSymbol = deleteSymbol;
/**
* Make sure we can drop a symbol instance
*/
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
var current = component;
do {
var 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
*/
function getSymbols(editor) {
return editor.Components.getSymbols().map(function (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
*/
function getSymbol(editor, component) {
var 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
*/
function createSymbol(editor, component) {
if (!component) {
throw new Error('No component provided to create a symbol.');
}
var 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
*/
function unbindSymbolInstance(editor, component) {
var 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
*/
function deleteSymbol(editor, symbol) {
var symbolInfo = editor.Components.getSymbolInfo(symbol);
if (!symbolInfo) {
throw new Error("Can not delete symbol: symbol info not found for ".concat((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