elementor
/
assets
/
js
/
packages
/
editor-embedded-documents-manager
/
editor-embedded-documents-manager.js
editor-embedded-documents-manager.js in Elementor Website Builder – more than just a page builder 4.3.2, at assets/js/packages/editor-embedded-documents-manager/editor-embedded-documents-manager.js
| 1 | (function(_elementor_editor_documents, _elementor_editor_styles_repository, _elementor_editor_v1_adapters) { |
| 2 | |
| 3 | //#region \0rolldown/runtime.js |
| 4 | var __defProp = Object.defineProperty; |
| 5 | var __name = (target, value) => __defProp(target, "name", { |
| 6 | value, |
| 7 | configurable: true |
| 8 | }); |
| 9 | var __exportAll = (all, no_symbols) => { |
| 10 | let target = {}; |
| 11 | for (var name in all) { |
| 12 | __defProp(target, name, { |
| 13 | get: all[name], |
| 14 | enumerable: true |
| 15 | }); |
| 16 | } |
| 17 | if (!no_symbols) { |
| 18 | __defProp(target, Symbol.toStringTag, { value: "Module" }); |
| 19 | } |
| 20 | return target; |
| 21 | }; |
| 22 | |
| 23 | //#endregion |
| 24 | |
| 25 | //#region packages/packages/core/editor-embedded-documents-manager/src/styles-provider.ts |
| 26 | var styles = []; |
| 27 | var documentStyles = /* @__PURE__ */ new Map(); |
| 28 | var styleListeners = /* @__PURE__ */ new Set(); |
| 29 | var embeddedDocumentsStylesProvider = (0, _elementor_editor_styles_repository.createStylesProvider)({ |
| 30 | key: "embedded-documents-styles", |
| 31 | priority: 75, |
| 32 | subscribe: (cb) => { |
| 33 | styleListeners.add(cb); |
| 34 | return () => { |
| 35 | styleListeners.delete(cb); |
| 36 | }; |
| 37 | }, |
| 38 | actions: { |
| 39 | all: () => styles, |
| 40 | get: (id) => styles.find((style) => style.id === id) ?? null |
| 41 | } |
| 42 | }); |
| 43 | function notifyStyleListeners() { |
| 44 | styleListeners.forEach((cb) => cb()); |
| 45 | } |
| 46 | function addEmbeddedDocumentStyles(documentId, document) { |
| 47 | const extracted = extractStylesFromDocument(document); |
| 48 | if (!extracted.length) { |
| 49 | if (!documentStyles.has(documentId)) return; |
| 50 | documentStyles.delete(documentId); |
| 51 | } else documentStyles.set(documentId, extracted); |
| 52 | styles = [...documentStyles.values()].flat(); |
| 53 | notifyStyleListeners(); |
| 54 | } |
| 55 | function clearEmbeddedDocumentsStyles() { |
| 56 | documentStyles = /* @__PURE__ */ new Map(); |
| 57 | styles = []; |
| 58 | notifyStyleListeners(); |
| 59 | } |
| 60 | function extractStylesFromDocument(document) { |
| 61 | if (!document.elements?.length) return []; |
| 62 | return document.elements.flatMap(extractStylesFromElement); |
| 63 | } |
| 64 | function extractStylesFromElement(element) { |
| 65 | return [...Object.values(element.styles ?? {}), ...(element.elements ?? []).flatMap(extractStylesFromElement)]; |
| 66 | } |
| 67 | |
| 68 | //#endregion |
| 69 | //#region packages/packages/core/editor-embedded-documents-manager/src/manager.ts |
| 70 | var pendingIds = /* @__PURE__ */ new Set(); |
| 71 | var loadedDocuments = /* @__PURE__ */ new Map(); |
| 72 | var listeners = /* @__PURE__ */ new Set(); |
| 73 | var currentDocumentId = null; |
| 74 | function setCurrentDocumentId(id) { |
| 75 | currentDocumentId = id; |
| 76 | } |
| 77 | function addDocuments(ids) { |
| 78 | const newIds = ids.filter((id) => !isCurrentDocument(id) && !pendingIds.has(id) && !loadedDocuments.has(id)); |
| 79 | newIds.forEach((id) => pendingIds.add(id)); |
| 80 | if (!newIds.length) return; |
| 81 | fetchAndNotify(newIds); |
| 82 | } |
| 83 | function setDocument(documentId, data) { |
| 84 | if (isCurrentDocument(documentId)) return; |
| 85 | const isNew = !loadedDocuments.has(documentId); |
| 86 | loadedDocuments.set(documentId, data); |
| 87 | if (isNew) notifyListeners(documentId, data); |
| 88 | else addEmbeddedDocumentStyles(documentId, data); |
| 89 | } |
| 90 | function onDocumentLoad(callback) { |
| 91 | listeners.add(callback); |
| 92 | Promise.resolve().then(() => { |
| 93 | if (!listeners.has(callback)) return; |
| 94 | loadedDocuments.forEach((data, documentId) => { |
| 95 | if (!isCurrentDocument(documentId)) callback(documentId, data); |
| 96 | }); |
| 97 | }); |
| 98 | return () => { |
| 99 | listeners.delete(callback); |
| 100 | }; |
| 101 | } |
| 102 | function reset() { |
| 103 | pendingIds.clear(); |
| 104 | loadedDocuments.clear(); |
| 105 | clearEmbeddedDocumentsStyles(); |
| 106 | } |
| 107 | var embeddedDocumentsManager = { |
| 108 | addDocuments, |
| 109 | setDocument, |
| 110 | onDocumentLoad, |
| 111 | reset |
| 112 | }; |
| 113 | function isCurrentDocument(documentId) { |
| 114 | return currentDocumentId !== null && documentId === currentDocumentId; |
| 115 | } |
| 116 | async function fetchAndNotify(ids) { |
| 117 | (await Promise.all(ids.map(fetchDocument))).forEach((result, index) => { |
| 118 | const id = ids[index]; |
| 119 | if (!pendingIds.has(id)) return; |
| 120 | pendingIds.delete(id); |
| 121 | if (!result || isCurrentDocument(id)) return; |
| 122 | const { data } = result; |
| 123 | loadedDocuments.set(id, data); |
| 124 | notifyListeners(id, data); |
| 125 | }); |
| 126 | } |
| 127 | async function fetchDocument(id) { |
| 128 | try { |
| 129 | return { |
| 130 | id, |
| 131 | data: await _elementor_editor_v1_adapters.ajax.load({ |
| 132 | data: { id }, |
| 133 | action: "get_document_config", |
| 134 | unique_id: `embedded-document-${id}` |
| 135 | }) |
| 136 | }; |
| 137 | } catch { |
| 138 | return null; |
| 139 | } |
| 140 | } |
| 141 | function notifyListeners(documentId, data) { |
| 142 | if (isCurrentDocument(documentId)) return; |
| 143 | listeners.forEach((cb) => cb(documentId, data)); |
| 144 | addEmbeddedDocumentStyles(documentId, data); |
| 145 | } |
| 146 | |
| 147 | //#endregion |
| 148 | //#region packages/packages/core/editor-embedded-documents-manager/src/init.ts |
| 149 | function init() { |
| 150 | _elementor_editor_styles_repository.stylesRepository.register(embeddedDocumentsStylesProvider); |
| 151 | (0, _elementor_editor_v1_adapters.registerDataHook)("after", "editor/documents/attach-preview", () => { |
| 152 | const { id } = (0, _elementor_editor_documents.getV1CurrentDocument)() ?? {}; |
| 153 | setCurrentDocumentId(id ?? null); |
| 154 | embeddedDocumentsManager.reset(); |
| 155 | }); |
| 156 | } |
| 157 | |
| 158 | //#endregion |
| 159 | //#region packages/packages/core/editor-embedded-documents-manager/src/index.ts |
| 160 | var src_exports = /* @__PURE__ */ __exportAll({ |
| 161 | embeddedDocumentsManager: () => embeddedDocumentsManager, |
| 162 | init: () => init, |
| 163 | setCurrentDocumentId: () => setCurrentDocumentId |
| 164 | }); |
| 165 | |
| 166 | //#endregion |
| 167 | //#region \0elementor-package-library-entry |
| 168 | (window.elementorV2 = window.elementorV2 || {}).editorEmbeddedDocumentsManager = src_exports; |
| 169 | |
| 170 | //#endregion |
| 171 | })(elementorV2.editorDocuments, elementorV2.editorStylesRepository, elementorV2.editorV1Adapters); |
| 172 | window.elementorV2.editorEmbeddedDocumentsManager?.init?.(); |
| 173 | //# sourceMappingURL=editor-embedded-documents-manager.js.map |