Sha256: 87d88b90eddd02008dbebbed5e33e267dcf7d9cebf52f0388553fc234513c3a7

Contents?: true

Size: 1.2 KB

Versions: 20

Compression:

Stored size: 1.2 KB

Contents

import { start as decorateIframe } from './iframe-decorator'

const mainWindow = window.parent

export const listenMessages = () => {
  window.addEventListener('message', ({ data: { type, ...data } }) => {
    // a message MUST have a type
    if (!type) return

    switch (type) {
      case 'config':
        decorateIframe({
          primaryColor: data.primaryColor,
          stickySectionIds: data.stickySectionIds,
        })

        // we answer back we're ready!
        postMessage('ready', { message: "👋, I'm a Maglev site and I'm ready" })
        break
      case 'section:add':
      case 'section:move':
      case 'section:update':
      case 'section:remove':
      case 'block:add':
      case 'block:move':
      case 'block:update':
      case 'block:remove':
      case 'style:update':
        triggerEvent(type, data)
        break
      default:
        console.log('[maglev][iframe] unknown message type', type)
        break
    }
  })
}

export const postMessage = (type, data) => {
  mainWindow.postMessage({ type, ...(data || {}) }, '*')
}

// local event
const triggerEvent = (type, data) => {
  const event = new CustomEvent(`maglev:${type}`, { detail: data })
  window.dispatchEvent(event)
}

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
maglevcms-1.7.3 app/frontend/live-preview-client/message.js
maglevcms-1.7.2 app/frontend/live-preview-client/message.js
maglevcms-1.7.1 app/frontend/live-preview-client/message.js
maglevcms-1.7.0 app/frontend/live-preview-client/message.js
maglevcms-1.6.1 app/frontend/live-preview-client/message.js
maglevcms-1.6.0 app/frontend/live-preview-client/message.js
maglevcms-1.5.1 app/frontend/live-preview-client/message.js
maglevcms-1.4.0 app/frontend/live-preview-client/message.js
maglevcms-1.3.0 app/frontend/live-preview-client/message.js
maglevcms-1.2.2 app/frontend/live-preview-client/message.js
maglevcms-1.2.1 app/frontend/live-preview-client/message.js
maglevcms-1.2.0 app/frontend/live-preview-client/message.js
maglevcms-1.1.7 app/frontend/live-preview-client/message.js
maglevcms-1.1.6 app/frontend/live-preview-client/message.js
maglevcms-1.1.5 app/frontend/live-preview-client/message.js
maglevcms-1.1.4 app/frontend/live-preview-client/message.js
maglevcms-1.1.3 app/frontend/live-preview-client/message.js
maglevcms-1.1.2 app/frontend/live-preview-client/message.js
maglevcms-1.1.1 app/frontend/live-preview-client/message.js
maglevcms-1.1.0 app/frontend/live-preview-client/message.js