@kontent-ai/smart-link
Version:
Kontent.ai Smart Link SDK allowing to automatically inject [smart links](https://docs.kontent.ai/tutorials/develop-apps/build-strong-foundation/set-up-editing-from-preview#a-using-smart-links) to Kontent.ai according to manually specified [HTML data attri
60 lines • 3.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAugmentableDescendants = getAugmentableDescendants;
exports.isElementAugmentable = isElementAugmentable;
exports.shouldElementHaveHighlight = shouldElementHaveHighlight;
exports.shouldElementHaveAddButton = shouldElementHaveAddButton;
const configuration_1 = require("./configuration");
const attributes_1 = require("./dataAttributes/attributes");
const elementHighlight_1 = require("./dataAttributes/elementHighlight");
const DisabledHighlightFeatureSelector = `[${attributes_1.MetadataAttribute.DisableFeatures}*="${attributes_1.DisableableFeature.Highlight}"]`;
const ElementSelector = `*[${attributes_1.DataAttribute.ElementCodename}]:not(${DisabledHighlightFeatureSelector})`;
const ContentComponentSelector = `*[${attributes_1.DataAttribute.ComponentId}]:not([${attributes_1.DataAttribute.ElementCodename}]):not(${DisabledHighlightFeatureSelector})`;
const ContentItemSelector = `*[${attributes_1.DataAttribute.ItemId}]:not([${attributes_1.DataAttribute.ComponentId}]):not([${attributes_1.DataAttribute.ElementCodename}]):not(${DisabledHighlightFeatureSelector})`;
const ElementsWithAddButtonSelector = `*[${attributes_1.MetadataAttribute.AddButton}]`;
const AllAugmentableElementsSelector = `${ElementSelector}, ${ContentComponentSelector}, ${ContentItemSelector}, ${ElementsWithAddButtonSelector}`;
const AugmentableElementsSelector = `${ElementSelector}, ${ContentItemSelector}`;
/**
* Find all descendant HTML elements that could be augmented (have highlights or add buttons near them).
*
* @param {HTMLElement | Document} node
* @returns {NodeListOf<HTMLElement>}
*/
function getAugmentableDescendants(node, configuration) {
const isInsideWebSpotlight = (0, configuration_1.isInsideWebSpotlightPreviewIFrame)(configuration);
return configuration.debug || isInsideWebSpotlight
? node.querySelectorAll(AllAugmentableElementsSelector)
: node.querySelectorAll(AugmentableElementsSelector);
}
/**
* Checks if HTML element could be augmented (have highlights or add buttons near them).
*/
function isElementAugmentable(element, configuration) {
return (shouldElementHaveHighlight(element, configuration) ||
shouldElementHaveAddButton(element, configuration));
}
/**
* Check if node should have highlights based on its data-attributes.
*/
function shouldElementHaveHighlight(element, configuration) {
const highlightType = (0, elementHighlight_1.getHighlightTypeForElement)(element);
switch (highlightType) {
case elementHighlight_1.HighlightType.None:
return false;
case elementHighlight_1.HighlightType.Element:
case elementHighlight_1.HighlightType.ContentItem:
return true;
case elementHighlight_1.HighlightType.ContentComponent:
return configuration.debug || (0, configuration_1.isInsideWebSpotlightPreviewIFrame)(configuration);
}
}
/**
* Check if node should have a add button based on its data-attributes.
*/
function shouldElementHaveAddButton(element, configuration) {
return ((((0, configuration_1.isInsideWebSpotlightPreviewIFrame)(configuration) || configuration.debug) &&
element &&
element.hasAttribute(attributes_1.MetadataAttribute.AddButton)) ??
false);
}
//# sourceMappingURL=customElements.js.map