Merge branch 'feat/css-preview' of github.com:Innei/xLog into dev
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
"use client"
|
||||
|
||||
import { useParams } from "next/navigation"
|
||||
import { useEffect, useState } from "react"
|
||||
import { FC, useEffect, useRef, useState } from "react"
|
||||
import toast from "react-hot-toast"
|
||||
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
|
||||
import { MonacoEditor } from "~/components/common/Monaco"
|
||||
import { SettingsLayout } from "~/components/dashboard/SettingsLayout"
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import { FieldLabel } from "~/components/ui/FieldLabel"
|
||||
import { useGetState } from "~/hooks/useGetState"
|
||||
import { getSiteLink } from "~/lib/helpers"
|
||||
import { useTranslation } from "~/lib/i18n/client"
|
||||
import { useGetSite, useUpdateSite } from "~/queries/site"
|
||||
|
||||
@@ -18,7 +22,6 @@ export default function SettingsCSSPage() {
|
||||
const updateSite = useUpdateSite()
|
||||
const site = useGetSite(subdomain)
|
||||
const { t } = useTranslation("dashboard")
|
||||
|
||||
const [css, setCss] = useState("")
|
||||
const handleSubmit = (e: any) => {
|
||||
e.preventDefault()
|
||||
@@ -106,7 +109,12 @@ export default function SettingsCSSPage() {
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-5">
|
||||
<div className="mt-5 space-x-2">
|
||||
<PreviewButton
|
||||
css={css}
|
||||
subdomain={subdomain}
|
||||
handle={site.data?.handle!}
|
||||
/>
|
||||
<Button type="submit" isLoading={updateSite.isLoading}>
|
||||
{t("Save")}
|
||||
</Button>
|
||||
@@ -115,3 +123,98 @@ export default function SettingsCSSPage() {
|
||||
</SettingsLayout>
|
||||
)
|
||||
}
|
||||
|
||||
const PreviewButton: FC<{
|
||||
css: string
|
||||
subdomain: string
|
||||
custom_domain?: string
|
||||
}> = ({ css, subdomain, custom_domain }) => {
|
||||
const { t } = useTranslation("dashboard")
|
||||
|
||||
const { data: siteLink } = useQuery({
|
||||
queryKey: ["site_link", subdomain],
|
||||
|
||||
queryFn: async () => {
|
||||
return getSiteLink({
|
||||
subdomain,
|
||||
domain: custom_domain,
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
const datasetRef = useRef({
|
||||
previewWindowOrigin: "",
|
||||
previewWindow: null as null | Window,
|
||||
}).current
|
||||
const [isInPreview, setIsPreview] = useState(false)
|
||||
|
||||
const handlePreview = () => {
|
||||
if (!siteLink) {
|
||||
return
|
||||
}
|
||||
let url: URL = new URL(siteLink)
|
||||
|
||||
url.searchParams.set("origin", location.origin)
|
||||
url.searchParams.set("css-preview", "true")
|
||||
|
||||
const finalUrl = url.toString()
|
||||
|
||||
const forkWindow = window.open(finalUrl)
|
||||
if (!forkWindow) {
|
||||
toast.error("Failed to open preview window")
|
||||
return
|
||||
}
|
||||
|
||||
datasetRef.previewWindowOrigin = url.origin
|
||||
datasetRef.previewWindow = forkWindow
|
||||
setIsPreview(true)
|
||||
}
|
||||
|
||||
const getCSS = useGetState(css)
|
||||
|
||||
useEffect(() => {
|
||||
if (!isInPreview) return
|
||||
|
||||
const handler = (e: MessageEvent<any>): void => {
|
||||
const { previewWindowOrigin, previewWindow } = datasetRef
|
||||
|
||||
if (!isInPreview) return
|
||||
if (e.origin !== previewWindowOrigin) return
|
||||
|
||||
if (!previewWindow) return
|
||||
previewWindow.postMessage(
|
||||
JSON.stringify({
|
||||
type: "preview",
|
||||
data: { css: getCSS() },
|
||||
}),
|
||||
previewWindowOrigin,
|
||||
)
|
||||
}
|
||||
window.addEventListener("message", handler)
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("message", handler)
|
||||
}
|
||||
}, [isInPreview])
|
||||
|
||||
const getIsPreview = useGetState(isInPreview)
|
||||
useEffect(() => {
|
||||
if (!getIsPreview()) return
|
||||
|
||||
if (!datasetRef.previewWindow) return
|
||||
|
||||
datasetRef.previewWindow.postMessage(
|
||||
JSON.stringify({
|
||||
type: "preview",
|
||||
data: { css: getCSS() },
|
||||
}),
|
||||
datasetRef.previewWindowOrigin,
|
||||
)
|
||||
}, [css])
|
||||
|
||||
return (
|
||||
<Button variant="secondary" onClick={handlePreview}>
|
||||
{t("Preview")}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import { Hydrate, dehydrate } from "@tanstack/react-query"
|
||||
|
||||
import { BlockchainInfo } from "~/components/common/BlockchainInfo"
|
||||
import { SitePlayerContainer } from "~/components/common/SitePlayer"
|
||||
import Style from "~/components/common/Style"
|
||||
import { BackToTopFAB } from "~/components/site/BackToTopFAB"
|
||||
import { CustomSiteStyle } from "~/components/site/CustomSiteStyle"
|
||||
import SiteFooter from "~/components/site/SiteFooter"
|
||||
import { SiteHeader } from "~/components/site/SiteHeader"
|
||||
import { FABContainer } from "~/components/ui/FAB"
|
||||
@@ -169,7 +169,7 @@ export default async function SiteLayout({
|
||||
<div
|
||||
className={`xlog-page xlog-page-${type} xlog-user xlog-deprecated-class`}
|
||||
>
|
||||
<Style content={site?.metadata?.content?.css} />
|
||||
<CustomSiteStyle content={site.metadata?.content?.css || ""} />
|
||||
{colors?.light && colors?.dark && (
|
||||
<style>
|
||||
{`.light {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { CharacterEntity } from "crossbell"
|
||||
import Link from "next/link"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useRouter, useSearchParams } from "next/navigation"
|
||||
import { memo } from "react"
|
||||
import reactStringReplace from "react-string-replace"
|
||||
|
||||
@@ -40,11 +40,15 @@ const Card = ({
|
||||
const router = useRouter()
|
||||
const { t } = useTranslation("common")
|
||||
const date = useDate()
|
||||
const searchParams = useSearchParams()
|
||||
|
||||
let queryString = searchParams.toString()
|
||||
queryString = queryString ? `?${queryString}` : ""
|
||||
|
||||
return (
|
||||
<Link
|
||||
target={isBlank ? "_blank" : undefined}
|
||||
href={`${linkPrefix || ""}/${post.metadata?.content?.slug}`}
|
||||
href={`${linkPrefix || ""}/${post.metadata?.content?.slug}${queryString}`}
|
||||
className={cn(
|
||||
"xlog-post sm:hover:bg-hover transition-all rounded-2xl flex flex-col items-center hover:opacity-100 group border relative",
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"use client"
|
||||
|
||||
import { FC, startTransition, useEffect, useState } from "react"
|
||||
|
||||
import { IS_DEV } from "~/lib/constants"
|
||||
import { SITE_URL } from "~/lib/env"
|
||||
|
||||
import Style from "../common/Style"
|
||||
|
||||
export const CustomSiteStyle: FC<{
|
||||
content: string
|
||||
}> = (props) => {
|
||||
const { content } = props
|
||||
const [currentStyle, setCurrentStyle] = useState(content)
|
||||
|
||||
useEffect(() => {
|
||||
setCurrentStyle(content)
|
||||
}, [content])
|
||||
|
||||
useEffect(() => {
|
||||
const search = location.search
|
||||
const searchParams = new URLSearchParams(search)
|
||||
|
||||
const targinOrigin = IS_DEV ? "http://localhost:2222" : SITE_URL
|
||||
|
||||
const isCSSPreview = searchParams.get("css-preview")
|
||||
|
||||
if (!targinOrigin || !isCSSPreview) {
|
||||
return
|
||||
}
|
||||
window.opener.postMessage("Preview Ready", targinOrigin)
|
||||
|
||||
const handler = (e: MessageEvent) => {
|
||||
if (e.origin !== targinOrigin) {
|
||||
return
|
||||
}
|
||||
|
||||
const parsedData = JSON.parse(e.data)
|
||||
|
||||
if (parsedData.type === "preview") {
|
||||
const { css } = parsedData.data
|
||||
|
||||
startTransition(() => {
|
||||
setCurrentStyle(css)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("message", handler)
|
||||
return () => {
|
||||
window.removeEventListener("message", handler)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return <Style content={currentStyle} />
|
||||
}
|
||||
Reference in New Issue
Block a user