{"version":3,"file":"use-editor-iframe-communication2.mjs","sources":["../../../../../../../../packages/components/editor/src/hooks/use-editor-iframe-communication.ts"],"sourcesContent":["import { type ComputedRef } from 'vue'\nimport { type EditorProps } from '../typings'\n\nexport function useEditorIframeCommunication(options: { mergedProps: ComputedRef<EditorProps> }) {\n  const { mergedProps } = options\n\n  function registryOuterChannel(editor: any, callback: (...args: any[]) => any) {\n    editor.contentWindow.addEventListener('message', (e: MessageEvent) => {\n      const messageDataJson = e.data\n      const { eventType, data } = JSON.parse(messageDataJson) ?? {}\n\n      if (eventType === 'preview:file-office') {\n        data && callback(data)\n      }\n    })\n  }\n\n  function registryInnerChannel(editor: any) {\n    const scriptId = editor.dom.uniqueId()\n    const scriptEl = editor.dom.create(\n      'script',\n      {\n        id: scriptId,\n        type: 'text/javascript',\n      },\n      `\n        // utils\n        function closest(el, selector) {\n          const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;\n          while (el) {\n            if (matchesSelector.call(el, selector)) {\n              return el;\n            } else {\n              el = el.parentElement;\n            }\n          }\n          return null;\n        }\n        \n        // listen wrapper el click\n        const editorContentEl = document.querySelector('.ta-editor-content')\n        if (editorContentEl) {\n          editorContentEl.addEventListener('click', (e) => {\n            const targetEl = e.target\n            handleFileOfficeClick(targetEl)\n          })\n        }\n\n        function handleFileOfficeClick(targetEl) {\n          const fileOfficeElClassnames = ['file-office', 'file-office__icon', 'file-office__content']\n          if(targetEl && targetEl.className && fileOfficeElClassnames.some(cls => targetEl.className.includes(cls))) {\n            const fileOfficeEl = closest(targetEl, '.file.file-office')\n            if(fileOfficeEl) {\n              const uploadvarsjson = fileOfficeEl.dataset.uploadvarsjson\n              const previewjson = fileOfficeEl.dataset.previewjson\n              if(previewjson) {\n                const previewInfo = Object.assign(JSON.parse(previewjson), {uploadVarsJson: uploadvarsjson})\n                previewInfo && window.postMessage(JSON.stringify({ eventType: 'preview:file-office', data: previewInfo }), window.location.origin)\n              }\n            }\n          }\n        }\n      `\n    )\n    editor.getDoc().getElementsByTagName('head')[0].appendChild(scriptEl)\n  }\n\n  return {\n    registryInnerChannel,\n    registryOuterChannel,\n  }\n}\n"],"names":[],"mappings":"AAAO,SAAS,4BAA4B,CAAC,OAAO,EAAE;AACtD,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;AAClC,EAAE,SAAS,oBAAoB,CAAC,MAAM,EAAE,QAAQ,EAAE;AAClD,IAAI,MAAM,CAAC,aAAa,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC,CAAC,KAAK;AAC5D,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,IAAI,CAAC;AACrC,MAAM,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,IAAI,EAAE,CAAC;AACpE,MAAM,IAAI,SAAS,KAAK,qBAAqB,EAAE;AAC/C,QAAQ,IAAI,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC;AAC/B,OAAO;AACP,KAAK,CAAC,CAAC;AACP,GAAG;AACH,EAAE,SAAS,oBAAoB,CAAC,MAAM,EAAE;AACxC,IAAI,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC;AAC3C,IAAI,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE;AACjD,MAAM,EAAE,EAAE,QAAQ;AAClB,MAAM,IAAI,EAAE,iBAAiB;AAC7B,KAAK,EAAE,CAAC;AACR;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,CAAC,CAAC,CAAC;AACT,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;AAC1E,GAAG;AACH,EAAE,OAAO;AACT,IAAI,oBAAoB;AACxB,IAAI,oBAAoB;AACxB,GAAG,CAAC;AACJ;;;;"}