UNPKG

@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

54 lines 2.85 kB
import { isInsideWebSpotlightPreviewIFrame } from "./configuration"; import { DataAttribute, DisableableFeature, MetadataAttribute } from "./dataAttributes/attributes"; import { getHighlightTypeForElement, HighlightType } from "./dataAttributes/elementHighlight"; const DisabledHighlightFeatureSelector = `[${MetadataAttribute.DisableFeatures}*="${DisableableFeature.Highlight}"]`; const ElementSelector = `*[${DataAttribute.ElementCodename}]:not(${DisabledHighlightFeatureSelector})`; const ContentComponentSelector = `*[${DataAttribute.ComponentId}]:not([${DataAttribute.ElementCodename}]):not(${DisabledHighlightFeatureSelector})`; const ContentItemSelector = `*[${DataAttribute.ItemId}]:not([${DataAttribute.ComponentId}]):not([${DataAttribute.ElementCodename}]):not(${DisabledHighlightFeatureSelector})`; const ElementsWithAddButtonSelector = `*[${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>} */ export function getAugmentableDescendants(node, configuration) { const isInsideWebSpotlight = 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). */ export function isElementAugmentable(element, configuration) { return (shouldElementHaveHighlight(element, configuration) || shouldElementHaveAddButton(element, configuration)); } /** * Check if node should have highlights based on its data-attributes. */ export function shouldElementHaveHighlight(element, configuration) { const highlightType = getHighlightTypeForElement(element); switch (highlightType) { case HighlightType.None: return false; case HighlightType.Element: case HighlightType.ContentItem: return true; case HighlightType.ContentComponent: return configuration.debug || isInsideWebSpotlightPreviewIFrame(configuration); } } /** * Check if node should have a add button based on its data-attributes. */ export function shouldElementHaveAddButton(element, configuration) { return (((isInsideWebSpotlightPreviewIFrame(configuration) || configuration.debug) && element && element.hasAttribute(MetadataAttribute.AddButton)) ?? false); } //# sourceMappingURL=customElements.js.map