-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inline preview customisation styles #4394
ref DEV-1427
- Loading branch information
Showing
32 changed files
with
3,323 additions
and
2,209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+5.03 KB
(100%)
authui/src/authflowv2/icons/material-symbols-outlined-subset.ttf
Binary file not shown.
Binary file modified
BIN
+2.11 KB
(100%)
authui/src/authflowv2/icons/material-symbols-outlined-subset.woff2
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
import { PreviewableResourceController } from "./previewable-resource"; | ||
import { injectCSSAttrs } from "./cssattrs"; | ||
|
||
interface PreviewCustomisationMessage { | ||
cssVars: Record<string, string>; | ||
images: Record<string, string | null>; | ||
translations: Record<string, string>; | ||
} | ||
|
||
function parsePreviewCustomisationMessage( | ||
message: any | ||
): PreviewCustomisationMessage | null { | ||
if (message.type !== "PreviewCustomisationMessage") { | ||
return null; | ||
} | ||
return { | ||
cssVars: message.cssVars ?? {}, | ||
images: message.images ?? {}, | ||
translations: message.translations ?? {}, | ||
}; | ||
} | ||
|
||
export class InlinePreviewController extends Controller { | ||
static outlets = ["previewable-resource"]; | ||
static values = { | ||
isInlinePreview: Boolean, | ||
}; | ||
|
||
declare previewableResourceOutlets: PreviewableResourceController[]; | ||
declare isInlinePreviewValue: boolean; | ||
|
||
windowMessageAllowedOrigins!: string[]; | ||
|
||
connect(): void { | ||
if (!this.isInlinePreviewValue) { | ||
return; | ||
} | ||
const windowMessageAllowedOrigins = ((): string[] => { | ||
const meta: HTMLMetaElement | null = document.querySelector( | ||
"meta[name=x-window-message-allowed-origins]" | ||
); | ||
const content = meta?.content ?? ""; | ||
return content.split(",").map((origin) => origin.trim()); | ||
})(); | ||
this.windowMessageAllowedOrigins = windowMessageAllowedOrigins; | ||
if (windowMessageAllowedOrigins.length === 0) { | ||
return; | ||
} | ||
window.addEventListener("message", this.onReceiveMessage); | ||
} | ||
|
||
disconnect(): void { | ||
window.removeEventListener("message", this.onReceiveMessage); | ||
} | ||
|
||
onReceiveMessage = (e: MessageEvent<any>): void => { | ||
if (!this.windowMessageAllowedOrigins.includes(e.origin)) { | ||
return; | ||
} | ||
const customisationMessage = parsePreviewCustomisationMessage(e.data); | ||
if (customisationMessage == null) { | ||
return; | ||
} | ||
const el = this.element as HTMLElement; | ||
for (const [name, value] of Object.entries(customisationMessage.cssVars)) { | ||
const currentStyle = el.style.getPropertyValue(name); | ||
if (currentStyle !== value) { | ||
el.style.setProperty(name, value); | ||
} | ||
} | ||
|
||
const keyToPreviewableResourceController: Partial< | ||
Record<string, PreviewableResourceController> | ||
> = {}; | ||
this.previewableResourceOutlets.forEach((outlet) => { | ||
keyToPreviewableResourceController[outlet.keyValue] = outlet; | ||
}); | ||
|
||
for (const [key, value] of Object.entries( | ||
customisationMessage.translations | ||
)) { | ||
const outlet = keyToPreviewableResourceController[key]; | ||
outlet?.setValue(value); | ||
} | ||
|
||
for (const [key, value] of Object.entries(customisationMessage.images)) { | ||
const outlet = keyToPreviewableResourceController[key]; | ||
outlet?.setValue(value); | ||
} | ||
|
||
injectCSSAttrs(document.documentElement); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
export class PreviewableResourceController extends Controller { | ||
static values = { | ||
key: String, | ||
changableAttribute: String, | ||
original: String, | ||
}; | ||
|
||
declare keyValue: string; | ||
declare changableAttributeValue: string; | ||
declare originalValue: string; | ||
|
||
setMessage(message: string): void { | ||
this.element.innerHTML = message; | ||
} | ||
|
||
setValue(value: string | null): void { | ||
const valueToSet = value != null ? value : this.originalValue; | ||
if (this.changableAttributeValue === "innerHTML") { | ||
this.element.innerHTML = valueToSet; | ||
} else { | ||
this.element.setAttribute(this.changableAttributeValue, valueToSet); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.