feat: support HTML preview
parent
f8e5341ac0
commit
a7d6ea011d
@ -0,0 +1,74 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import FullScreenModal from '@/app/components/base/fullscreen-modal'
|
||||||
|
import cn from '@/utils/classnames'
|
||||||
|
import s from './style.module.css'
|
||||||
|
import { useTranslation } from 'react-i18next'
|
||||||
|
import ActionButton from '../action-button'
|
||||||
|
import Tooltip from '../tooltip'
|
||||||
|
|
||||||
|
type HTMLPreviewBtnProps = {
|
||||||
|
content: string,
|
||||||
|
completed?: boolean,
|
||||||
|
}
|
||||||
|
|
||||||
|
const prefixPreview = 'appOverview.overview.appInfo.preview'
|
||||||
|
|
||||||
|
type CreateIframeModalProps = {
|
||||||
|
show: boolean,
|
||||||
|
onClose: () => void,
|
||||||
|
content: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
const CreateIframeModal = ({ show, onClose, content }: CreateIframeModalProps) => {
|
||||||
|
const { t } = useTranslation()
|
||||||
|
return (
|
||||||
|
<FullScreenModal
|
||||||
|
overflowVisible
|
||||||
|
closable
|
||||||
|
open={show}
|
||||||
|
onClose={onClose}
|
||||||
|
>
|
||||||
|
<iframe
|
||||||
|
srcDoc={content}
|
||||||
|
sandbox="allow-scripts"
|
||||||
|
title={t(`${prefixPreview}`)}
|
||||||
|
style={{ width: '100%', height: '100%', border: 'none' }}
|
||||||
|
/>
|
||||||
|
</FullScreenModal>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const HTMLPreviewBtn = ({
|
||||||
|
content,
|
||||||
|
completed = false,
|
||||||
|
}: HTMLPreviewBtnProps) => {
|
||||||
|
const { t } = useTranslation()
|
||||||
|
const [show, setShow] = React.useState(false)
|
||||||
|
|
||||||
|
const openModal = () => {
|
||||||
|
setShow(true)
|
||||||
|
}
|
||||||
|
const closeModal = () => {
|
||||||
|
setShow(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!completed) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Tooltip
|
||||||
|
popupContent={t(`${prefixPreview}`)}>
|
||||||
|
<ActionButton onClick={openModal}>
|
||||||
|
<div className={cn('h-4 w-4', s.previewIcon)}></div>
|
||||||
|
</ActionButton>
|
||||||
|
</Tooltip>
|
||||||
|
<CreateIframeModal
|
||||||
|
show={show}
|
||||||
|
onClose={closeModal}
|
||||||
|
content={content}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default HTMLPreviewBtn
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
.previewIcon {
|
||||||
|
background-image: url(~@/app/components/develop/secret-key/assets/play.svg);
|
||||||
|
background-position: center;
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
export const isCompleteHTML = (code: string): boolean => {
|
||||||
|
// simple check
|
||||||
|
const completeHTMLRegex = /<\s*html\s*[^>]*>[\s\S]*<\/html\s*>/i
|
||||||
|
return completeHTMLRegex.test(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getPureContent = (code: string): string => {
|
||||||
|
return String(code).replace(/\n$/, '').trim()
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue