@@ -58,6 +58,7 @@ const createIframeShellDocument = ({ channelId, parentOrigin, importMap }) => {
5858 renderedNodes: [],
5959 visualConfig: {
6060 cssText: '',
61+ userStyleSheets: [],
6162 hostPadding: '',
6263 backgroundColor: '',
6364 },
@@ -114,9 +115,26 @@ const createIframeShellDocument = ({ channelId, parentOrigin, importMap }) => {
114115 ].join('\\n')
115116 }
116117
117- const __knightedApplyVisualConfig = ({ cssText = '', hostPadding = '', backgroundColor = '' }) => {
118+ const __knightedApplyVisualConfig = ({
119+ cssText = '',
120+ userStyleSheets = [],
121+ hostPadding = '',
122+ backgroundColor = '',
123+ }) => {
124+ const normalizedUserStyleSheets = Array.isArray(userStyleSheets)
125+ ? userStyleSheets
126+ .filter(styleText => typeof styleText === 'string')
127+ .map(styleText => String(styleText))
128+ : []
129+ const fallbackUserStyleText = typeof cssText === 'string' ? cssText : ''
130+ const desiredUserStyleSheets =
131+ normalizedUserStyleSheets.length > 0
132+ ? normalizedUserStyleSheets
133+ : [fallbackUserStyleText]
134+
118135 __knightedState.visualConfig = {
119- cssText: typeof cssText === 'string' ? cssText : '',
136+ cssText: fallbackUserStyleText,
137+ userStyleSheets: desiredUserStyleSheets,
120138 hostPadding: typeof hostPadding === 'string' ? hostPadding : '',
121139 backgroundColor: typeof backgroundColor === 'string' ? backgroundColor : '',
122140 }
@@ -128,25 +146,55 @@ const createIframeShellDocument = ({ channelId, parentOrigin, importMap }) => {
128146 document.head.append(baseStyleElement)
129147 }
130148
131- let userStyleElement = document.getElementById('knighted-preview-user-styles')
132- if (!(userStyleElement instanceof HTMLStyleElement)) {
133- userStyleElement = document.createElement('style')
134- userStyleElement.id = 'knighted-preview-user-styles'
135- document.head.append(userStyleElement)
149+ const desiredUserStyleElementIds = __knightedState.visualConfig.userStyleSheets.map(
150+ (_styleText, index) =>
151+ index === 0
152+ ? 'knighted-preview-user-styles'
153+ : 'knighted-preview-user-styles-' + index,
154+ )
155+
156+ const desiredUserStyleElementIdSet = new Set(desiredUserStyleElementIds)
157+ const existingUserStyleElements = Array.from(
158+ document.head.querySelectorAll('style[id^="knighted-preview-user-styles"]'),
159+ )
160+ for (const existingUserStyleElement of existingUserStyleElements) {
161+ if (!desiredUserStyleElementIdSet.has(existingUserStyleElement.id)) {
162+ existingUserStyleElement.remove()
163+ }
136164 }
137165
166+ const userStyleElements = desiredUserStyleElementIds.map(styleElementId => {
167+ let userStyleElement = document.getElementById(styleElementId)
168+ if (!(userStyleElement instanceof HTMLStyleElement)) {
169+ userStyleElement = document.createElement('style')
170+ userStyleElement.id = styleElementId
171+ document.head.append(userStyleElement)
172+ }
173+
174+ return userStyleElement
175+ })
176+
177+ const firstUserStyleElement = userStyleElements[0]
138178 const isBaseAfterUser =
139- (baseStyleElement.compareDocumentPosition(userStyleElement) &
179+ firstUserStyleElement instanceof HTMLStyleElement &&
180+ (baseStyleElement.compareDocumentPosition(firstUserStyleElement) &
140181 Node.DOCUMENT_POSITION_PRECEDING) !==
141182 0
142- if (isBaseAfterUser) {
143- document.head.insertBefore(baseStyleElement, userStyleElement )
183+ if (isBaseAfterUser && firstUserStyleElement instanceof HTMLStyleElement ) {
184+ document.head.insertBefore(baseStyleElement, firstUserStyleElement )
144185 }
145186
146187 baseStyleElement.textContent = __knightedToBaseStyles(
147188 __knightedState.visualConfig.hostPadding,
148189 )
149- userStyleElement.textContent = String(__knightedState.visualConfig.cssText)
190+
191+ for (let index = 0; index < userStyleElements.length; index += 1) {
192+ const userStyleElement = userStyleElements[index]
193+ userStyleElement.textContent = String(
194+ __knightedState.visualConfig.userStyleSheets[index] ?? '',
195+ )
196+ document.head.append(userStyleElement)
197+ }
150198
151199 if (__knightedState.visualConfig.hostPadding.trim().length > 0) {
152200 document.documentElement.style.setProperty(
@@ -574,6 +622,7 @@ export const createWorkspaceIframePreviewBridge = ({
574622 entryExportName,
575623 importMap,
576624 cssText,
625+ userStyleSheets = [ ] ,
577626 hostPadding = '' ,
578627 backgroundColor = '' ,
579628 runtimeSpecifiers,
@@ -616,6 +665,7 @@ export const createWorkspaceIframePreviewBridge = ({
616665 entryExportName,
617666 runtimeSpecifiers,
618667 cssText,
668+ userStyleSheets,
619669 hostPadding,
620670 backgroundColor,
621671 importMap,
0 commit comments