Skip to content

Commit 1b7d618

Browse files
fix: iframe protocol mismatch in receiver reads. (#104)
1 parent ce43c92 commit 1b7d618

5 files changed

Lines changed: 66 additions & 175 deletions

File tree

docs/issue-99-workspaces-drawer-plan.md

Lines changed: 0 additions & 71 deletions
This file was deleted.

docs/pr-drawer-workspace-context-separation-plan.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

docs/render-pipeline-multitab-spec-plan.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

playwright/layout-panels.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
expectCollapseButtonState,
44
expectPreviewHasRenderedContent,
55
getCollapseButton,
6+
getPreviewFrame,
67
getToolsButton,
78
waitForInitialRender,
89
} from './helpers/app-test-helpers.js'
@@ -61,6 +62,37 @@ test('dark theme defaults preview background to editor background', async ({ pag
6162
expect(toRgbChannels(colors.preview)).toEqual(toRgbChannels(colors.editor))
6263
})
6364

65+
test('changing preview background keeps applied preview styles', async ({ page }) => {
66+
await waitForInitialRender(page)
67+
68+
const previewFrameRoot = getPreviewFrame(page).locator('html')
69+
70+
await expect(previewFrameRoot).toHaveCount(1)
71+
const hasComponentStylesBefore = await previewFrameRoot.evaluate(() => {
72+
const styleElement = document.getElementById('knighted-preview-styles')
73+
if (!(styleElement instanceof HTMLStyleElement)) {
74+
return false
75+
}
76+
77+
return styleElement.textContent?.includes('.counter-button') ?? false
78+
})
79+
expect(hasComponentStylesBefore).toBe(true)
80+
81+
await page.getByLabel('Background').fill('#b1aaaa')
82+
83+
const hasComponentStylesAfter = await previewFrameRoot.evaluate(() => {
84+
const styleElement = document.getElementById('knighted-preview-styles')
85+
if (!(styleElement instanceof HTMLStyleElement)) {
86+
return false
87+
}
88+
89+
return styleElement.textContent?.includes('.counter-button') ?? false
90+
})
91+
expect(hasComponentStylesAfter).toBe(true)
92+
93+
await expect(previewFrameRoot).toHaveCSS('background-color', 'rgb(177, 170, 170)')
94+
})
95+
6496
test('fixed layout keeps preview panel height within editor stack height', async ({
6597
page,
6698
}) => {

src/modules/preview-runtime/iframe-preview-executor.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ const createIframeShellDocument = ({ channelId, parentOrigin, importMap }) => {
5656
entrySpecifier: '',
5757
reactRoot: null,
5858
renderedNodes: [],
59+
visualConfig: {
60+
cssText: '',
61+
hostPadding: '',
62+
backgroundColor: '',
63+
},
5964
}
6065
6166
const __knightedRuntimeErrorFingerprints = new Set()
@@ -110,24 +115,37 @@ const createIframeShellDocument = ({ channelId, parentOrigin, importMap }) => {
110115
}
111116
112117
const __knightedApplyVisualConfig = ({ cssText = '', hostPadding = '', backgroundColor = '' }) => {
118+
__knightedState.visualConfig = {
119+
cssText: typeof cssText === 'string' ? cssText : '',
120+
hostPadding: typeof hostPadding === 'string' ? hostPadding : '',
121+
backgroundColor: typeof backgroundColor === 'string' ? backgroundColor : '',
122+
}
123+
113124
let styleElement = document.getElementById('knighted-preview-styles')
114125
if (!(styleElement instanceof HTMLStyleElement)) {
115126
styleElement = document.createElement('style')
116127
styleElement.id = 'knighted-preview-styles'
117128
document.head.append(styleElement)
118129
}
119130
120-
styleElement.textContent = __knightedToBaseStyles(hostPadding) + '\\n' + String(cssText)
131+
styleElement.textContent =
132+
__knightedToBaseStyles(__knightedState.visualConfig.hostPadding) +
133+
'\\n' +
134+
String(__knightedState.visualConfig.cssText)
121135
122-
if (typeof hostPadding === 'string' && hostPadding.trim().length > 0) {
123-
document.documentElement.style.setProperty('--preview-host-padding', hostPadding.trim())
136+
if (__knightedState.visualConfig.hostPadding.trim().length > 0) {
137+
document.documentElement.style.setProperty(
138+
'--preview-host-padding',
139+
__knightedState.visualConfig.hostPadding.trim(),
140+
)
124141
} else {
125142
document.documentElement.style.removeProperty('--preview-host-padding')
126143
}
127144
128-
if (typeof backgroundColor === 'string' && backgroundColor.length > 0) {
129-
document.documentElement.style.backgroundColor = backgroundColor
130-
document.body.style.backgroundColor = backgroundColor
145+
if (__knightedState.visualConfig.backgroundColor.length > 0) {
146+
document.documentElement.style.backgroundColor =
147+
__knightedState.visualConfig.backgroundColor
148+
document.body.style.backgroundColor = __knightedState.visualConfig.backgroundColor
131149
return
132150
}
133151
@@ -285,7 +303,16 @@ const createIframeShellDocument = ({ channelId, parentOrigin, importMap }) => {
285303
const data = event.data
286304
287305
if (data.type === __knightedMessageTypes.configPatch) {
288-
__knightedApplyVisualConfig(data)
306+
const patch =
307+
data && typeof data.payload === 'object' && data.payload !== null
308+
? data.payload
309+
: data && typeof data === 'object'
310+
? data
311+
: {}
312+
__knightedApplyVisualConfig({
313+
...__knightedState.visualConfig,
314+
...patch,
315+
})
289316
return
290317
}
291318

0 commit comments

Comments
 (0)