| 1 |
(() => { |
| 2 |
let lastDevice = null; |
| 3 |
|
| 4 |
function register() { |
| 5 |
if (lastDevice) { |
| 6 |
return; |
| 7 |
} |
| 8 |
|
| 9 |
const previewEl = document.querySelector( |
| 10 |
".is-tablet-preview, .is-desktop-preview, .is-mobile-preview" |
| 11 |
); |
| 12 |
if (!previewEl) { |
| 13 |
return; |
| 14 |
} |
| 15 |
lastDevice = previewEl.className.match(/is-(\w*)-preview/)[1]; |
| 16 |
const previewObserver = new MutationObserver((mutationList) => { |
| 17 |
for (let mutation of mutationList) { |
| 18 |
if (mutation.attributeName === "class") { |
| 19 |
const currentPreview = |
| 20 |
mutation.target.className.match(/is-(\w*)-preview/)[1]; |
| 21 |
if (!currentPreview) { |
| 22 |
return; |
| 23 |
} |
| 24 |
lastDevice = currentPreview; |
| 25 |
const event = new CustomEvent("TablebergPreviewDeviceChange", { |
| 26 |
detail: { |
| 27 |
currentPreview |
| 28 |
} |
| 29 |
}); |
| 30 |
document.dispatchEvent(event); |
| 31 |
return; |
| 32 |
} |
| 33 |
} |
| 34 |
}); |
| 35 |
|
| 36 |
previewObserver.observe(previewEl, { |
| 37 |
attributes: true, |
| 38 |
attributeFilter: ["class"], |
| 39 |
attributeOldValue: true, |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
window.registerTablebergPreviewDeviceChangeObserver = register; |
| 44 |
window.tablebergGetLastDevice = () => lastDevice; |
| 45 |
})(); |
| 46 |
|