Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/dist/mason.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 50 additions & 36 deletions resources/js/mason.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,6 @@ export default function masonComponent({
isUndoRedoOperation: false,

async init() {
// Wait for iframe to load
this.$nextTick(() => {
iframe = this.$refs.previewIframe

if (iframe) {
// Load initial content via form submission
this.updatePreview()

iframe.addEventListener('load', () => {
// Restore scroll position after iframe loads
this.restoreScrollPosition()

// Send postMessage once the iframe is loaded with config
this.sendMessageToIframe({
type: 'setContent',
blocks: this.getBlocksFromState(),
dblClickToEdit: dblClickToEdit,
disabled: disabled,
})

// Send initial color mode to iframe
this.sendMessageToIframe({
type: 'setColorMode',
mode: this.colorMode,
})

// Update move buttons after iframe loads
setTimeout(() => {
this.sendMessageToIframe({
type: 'updateMoveButtons',
})
}, 100)
})
}
})

// Listen for messages from iframe
const messageHandler = (event) => {
// Security: verify origin if needed
Expand Down Expand Up @@ -190,6 +154,47 @@ export default function masonComponent({
this.setupDragAndDrop()
},

// Called from x-init on the iframe itself, so that if the element is ever
// replaced (browser extensions injecting nodes can defeat Livewire's morph
// matching) the new element is bound instead of leaving a stale reference.
initPreviewIframe(el) {
if (isDestroyed || !el || el._masonPreviewBound) {
return
}

el._masonPreviewBound = true
iframe = el

el.addEventListener('load', () => {
// Restore scroll position after iframe loads
this.restoreScrollPosition()

// Send postMessage once the iframe is loaded with config
this.sendMessageToIframe({
type: 'setContent',
blocks: this.getBlocksFromState(),
dblClickToEdit: dblClickToEdit,
disabled: disabled,
})

// Send initial color mode to iframe
this.sendMessageToIframe({
type: 'setColorMode',
mode: this.colorMode,
})

// Update move buttons after iframe loads
setTimeout(() => {
this.sendMessageToIframe({
type: 'updateMoveButtons',
})
}, 100)
})

// Load initial content via form submission
this.$nextTick(() => this.updatePreview())
},

getBlocksFromState() {
if (!this.state) {
return []
Expand Down Expand Up @@ -496,6 +501,10 @@ export default function masonComponent({
},

updatePreview() {
if (!iframe) {
return
}

// Save scroll position before updating
this.saveScrollPosition()

Expand Down Expand Up @@ -653,6 +662,11 @@ export default function masonComponent({
})
eventListeners = []

if (iframe) {
// Allow a future init to rebind if the element outlives this component
delete iframe._masonPreviewBound
}

iframe = null
},
}
Expand Down
Loading
Loading