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

134 lines 6.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KontentSmartLinkEvent = void 0; const iframe_1 = require("./utils/iframe"); const DOMSmartLinkManager_1 = require("./lib/DOMSmartLinkManager"); const storage_1 = require("./utils/storage"); const IFrameCommunicator_1 = require("./lib/IFrameCommunicator"); const IFrameCommunicatorTypes_1 = require("./lib/IFrameCommunicatorTypes"); const queryParams_1 = require("./utils/queryParams"); const components_1 = require("./web-components/components"); const configuration_1 = require("./utils/configuration"); const Logger_1 = require("./lib/Logger"); const reload_1 = require("./utils/reload"); const events_1 = require("./utils/events"); var KontentSmartLinkEvent; (function (KontentSmartLinkEvent) { KontentSmartLinkEvent["Refresh"] = "refresh"; KontentSmartLinkEvent["Update"] = "update"; })(KontentSmartLinkEvent || (exports.KontentSmartLinkEvent = KontentSmartLinkEvent = {})); class KontentSmartLinkSDK { configuration = configuration_1.defaultConfiguration; iframeCommunicator; domSmartLinkManager; events; queryPresenceIntervalCleanup = null; constructor(configuration) { this.configuration = { ...this.configuration, ...configuration }; this.events = new Map(); this.iframeCommunicator = new IFrameCommunicator_1.IFrameCommunicator(); this.domSmartLinkManager = new DOMSmartLinkManager_1.DOMSmartLinkManager(this.iframeCommunicator, this.configuration); this.initialize(); } static getSettingsStorage() { return (0, storage_1.createStorage)('kontent-smart-link:iframe-settings'); } initialize = async () => { await (0, components_1.defineAllRequiredWebComponents)(); const level = this.configuration.debug ? Logger_1.LogLevel.Debug : Logger_1.LogLevel.Info; (0, Logger_1.setLogLevel)(level); if (this.configuration.queryParam) { this.queryPresenceIntervalCleanup = (0, queryParams_1.watchQueryParamPresence)(this.configuration.queryParam, this.domSmartLinkManager.toggle); } else { this.domSmartLinkManager.enable(); } if ((0, iframe_1.isInsideIFrame)()) { this.initializeIFrameCommunication(); } }; destroy = () => { this.events = new Map(); this.queryPresenceIntervalCleanup?.(); this.iframeCommunicator.destroy(); this.domSmartLinkManager.destroy(); }; updateConfiguration = (configuration) => { if (configuration.queryParam !== undefined) { if (configuration.queryParam === '') { this.domSmartLinkManager.enable(); } else if (configuration.queryParam !== this.configuration.queryParam) { this.queryPresenceIntervalCleanup?.(); this.queryPresenceIntervalCleanup = (0, queryParams_1.watchQueryParamPresence)(configuration.queryParam, this.domSmartLinkManager.toggle); } } if (configuration.debug) { (0, Logger_1.setLogLevel)(Logger_1.LogLevel.Debug); } // need to use .assign to prevent changing the reference of the configuration object Object.assign(this.configuration, configuration); }; on = (event, handler) => { (0, events_1.addListener)(this.events, event, handler); }; off = (event, handler) => { (0, events_1.removeListener)(this.events, event, handler); }; initializeIFrameCommunication = () => { this.iframeCommunicator.initialize(); const storage = KontentSmartLinkSDK.getSettingsStorage(); const settings = storage.get(); const enabled = settings !== null ? settings?.enabled : true; const messageData = { enabled, languageCodename: this.configuration.defaultDataAttributes.languageCodename ?? null, projectId: this.configuration.defaultDataAttributes.environmentId ?? null, supportedFeatures: { previewIFrameCurrentUrlHandler: true, refreshHandler: true, updateHandler: true, }, }; this.iframeCommunicator.sendMessageWithResponse(IFrameCommunicatorTypes_1.IFrameMessageType.Initialized, messageData, () => { Object.assign(this.configuration, { isInsideWebSpotlight: true }); this.queryPresenceIntervalCleanup?.(); this.domSmartLinkManager.disable(); if (enabled) { this.domSmartLinkManager.enable(); } this.iframeCommunicator.addMessageListener(IFrameCommunicatorTypes_1.IFrameMessageType.Status, this.handleStatusMessage); this.iframeCommunicator.addMessageListener(IFrameCommunicatorTypes_1.IFrameMessageType.RefreshPreview, this.handleRefreshMessage); this.iframeCommunicator.addMessageListener(IFrameCommunicatorTypes_1.IFrameMessageType.UpdatePreview, this.handleUpdateMessage); this.iframeCommunicator.addMessageListener(IFrameCommunicatorTypes_1.IFrameMessageType.PreviewIFrameCurrentUrl, this.handlePreviewIFrameCurrentUrlRequestMessage); }); }; handleStatusMessage = (data) => { if (!data || !this.domSmartLinkManager) return; this.domSmartLinkManager.toggle(data.enabled); KontentSmartLinkSDK.getSettingsStorage().set({ enabled: data.enabled, }); }; handleRefreshMessage = (data, metadata) => { const isCustomRefreshHandlerImplemented = this.events.has(KontentSmartLinkEvent.Refresh); if (isCustomRefreshHandlerImplemented) { (0, events_1.emitEvents)(this.events, KontentSmartLinkEvent.Refresh, data, metadata, reload_1.reload); } else { (0, reload_1.reload)(); } }; handleUpdateMessage = (data) => { (0, events_1.emitEvents)(this.events, KontentSmartLinkEvent.Update, data, undefined, undefined); }; handlePreviewIFrameCurrentUrlRequestMessage = () => { const messageData = { previewUrl: window.location.href, }; this.iframeCommunicator.sendMessage(IFrameCommunicatorTypes_1.IFrameMessageType.PreviewIFrameCurrentUrlResponse, messageData); }; } exports.default = KontentSmartLinkSDK; //# sourceMappingURL=sdk.js.map