|
| 1 | +/** |
| 2 | + * This file is part of the NocoBase (R) project. |
| 3 | + * Copyright (c) 2020-2024 NocoBase Co., Ltd. |
| 4 | + * Authors: NocoBase Team. |
| 5 | + * |
| 6 | + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. |
| 7 | + * For more information, please refer to: https://www.nocobase.com/agreement. |
| 8 | + */ |
| 9 | + |
| 10 | +import { Popover } from 'antd'; |
| 11 | +import { css } from '@emotion/css'; |
| 12 | +import React, { CSSProperties, useCallback, useEffect, useRef, useState } from 'react'; |
| 13 | +import Vditor from 'vditor'; |
| 14 | +import { useCDN } from './useCDN'; |
| 15 | +import useStyle from './style'; |
| 16 | + |
| 17 | +function convertToText(markdownText: string) { |
| 18 | + const content = markdownText; |
| 19 | + let temp = document.createElement('div'); |
| 20 | + temp.innerHTML = content; |
| 21 | + const text = temp.innerText; |
| 22 | + temp = null; |
| 23 | + return text?.replace(/[\n\r]/g, '') || ''; |
| 24 | +} |
| 25 | + |
| 26 | +const getContentWidth = (element) => { |
| 27 | + if (element) { |
| 28 | + const range = document.createRange(); |
| 29 | + range.selectNodeContents(element); |
| 30 | + const contentWidth = range.getBoundingClientRect().width; |
| 31 | + return contentWidth; |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +function DisplayInner(props: { value: string; style?: CSSProperties }) { |
| 36 | + const containerRef = useRef<HTMLDivElement>(null); |
| 37 | + const { wrapSSR, componentCls, hashId } = useStyle(); |
| 38 | + const cdn = useCDN(); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + Vditor.preview(containerRef.current, props.value ?? '', { |
| 42 | + mode: 'light', |
| 43 | + cdn, |
| 44 | + }); |
| 45 | + setTimeout(() => { |
| 46 | + containerRef.current?.querySelectorAll('img').forEach((img: HTMLImageElement) => { |
| 47 | + img.style.cursor = 'zoom-in'; |
| 48 | + img.addEventListener('click', () => { |
| 49 | + openCustomPreview(img.src); |
| 50 | + }); |
| 51 | + }); |
| 52 | + }, 0); |
| 53 | + }, [props.value]); |
| 54 | + |
| 55 | + return wrapSSR( |
| 56 | + <span className={`${hashId} ${componentCls}`}> |
| 57 | + <span ref={containerRef} style={{ border: 'none', ...(props?.style ?? {}) }} /> |
| 58 | + </span>, |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +function openCustomPreview(src: string) { |
| 63 | + if (document.getElementById('custom-image-preview')) return; |
| 64 | + |
| 65 | + // 创建容器 |
| 66 | + const overlay = document.createElement('span'); |
| 67 | + overlay.id = 'custom-image-preview'; |
| 68 | + Object.assign(overlay.style, { |
| 69 | + position: 'fixed', |
| 70 | + inset: '0', |
| 71 | + backgroundColor: 'rgba(0,0,0,0.85)', |
| 72 | + display: 'flex', |
| 73 | + alignItems: 'center', |
| 74 | + justifyContent: 'center', |
| 75 | + zIndex: '9999', |
| 76 | + cursor: 'zoom-out', |
| 77 | + }); |
| 78 | + |
| 79 | + const img = document.createElement('img'); |
| 80 | + img.src = src; |
| 81 | + Object.assign(img.style, { |
| 82 | + maxWidth: '90%', |
| 83 | + maxHeight: '90%', |
| 84 | + borderRadius: '8px', |
| 85 | + boxShadow: '0 0 20px rgba(0,0,0,0.5)', |
| 86 | + transition: 'transform 0.2s', |
| 87 | + cursor: 'zoom-out', |
| 88 | + }); |
| 89 | + |
| 90 | + overlay.addEventListener('click', () => { |
| 91 | + document.body.removeChild(overlay); |
| 92 | + }); |
| 93 | + |
| 94 | + overlay.appendChild(img); |
| 95 | + document.body.appendChild(overlay); |
| 96 | +} |
| 97 | + |
| 98 | +export const Display = (props) => { |
| 99 | + const { value, textOnly = true } = props; |
| 100 | + const cdn = useCDN(); |
| 101 | + const [popoverVisible, setPopoverVisible] = useState(false); |
| 102 | + const [ellipsis, setEllipsis] = useState(false); |
| 103 | + |
| 104 | + const [text, setText] = useState(''); |
| 105 | + |
| 106 | + const elRef = useRef<HTMLDivElement>(); |
| 107 | + useEffect(() => { |
| 108 | + if (!props.value) return; |
| 109 | + if (textOnly) { |
| 110 | + Vditor.md2html(props.value, { |
| 111 | + mode: 'light', |
| 112 | + cdn, |
| 113 | + }) |
| 114 | + .then((html) => { |
| 115 | + setText(convertToText(html)); |
| 116 | + }) |
| 117 | + .catch(() => setText('')); |
| 118 | + } |
| 119 | + }, [props.value, textOnly]); |
| 120 | + |
| 121 | + const isOverflowTooltip = useCallback(() => { |
| 122 | + if (!elRef.current) return false; |
| 123 | + const contentWidth = getContentWidth(elRef.current); |
| 124 | + const offsetWidth = elRef.current?.offsetWidth; |
| 125 | + return contentWidth > offsetWidth; |
| 126 | + }, [elRef]); |
| 127 | + |
| 128 | + if (props.ellipsis) { |
| 129 | + return ( |
| 130 | + <Popover |
| 131 | + open={popoverVisible} |
| 132 | + getPopupContainer={() => document.getElementsByClassName('ant-drawer-content')?.[0] as HTMLElement} |
| 133 | + onOpenChange={(visible) => { |
| 134 | + setPopoverVisible(ellipsis && visible); |
| 135 | + }} |
| 136 | + overlayStyle={{ maxWidth: 400, maxHeight: 450, overflow: 'auto' }} |
| 137 | + content={<DisplayInner value={value} />} |
| 138 | + > |
| 139 | + <div |
| 140 | + ref={elRef} |
| 141 | + style={{ |
| 142 | + overflow: 'hidden', |
| 143 | + overflowWrap: 'break-word', |
| 144 | + textOverflow: 'ellipsis', |
| 145 | + whiteSpace: 'nowrap', |
| 146 | + wordBreak: 'break-all', |
| 147 | + }} |
| 148 | + onMouseEnter={(e) => { |
| 149 | + const el = e.target as any; |
| 150 | + const isShowTooltips = isOverflowTooltip(); |
| 151 | + if (isShowTooltips) { |
| 152 | + setEllipsis(el.scrollWidth >= el.clientWidth); |
| 153 | + } |
| 154 | + }} |
| 155 | + > |
| 156 | + {textOnly ? ( |
| 157 | + text |
| 158 | + ) : ( |
| 159 | + <div |
| 160 | + className={css` |
| 161 | + .vditor-reset { |
| 162 | + white-space: nowrap; |
| 163 | + display: -webkit-box; |
| 164 | + -webkit-box-orient: vertical; |
| 165 | + -webkit-line-clamp: 1; |
| 166 | + overflow: hidden; |
| 167 | + text-overflow: ellipsis; |
| 168 | + word-break: break-word; |
| 169 | + } |
| 170 | + `} |
| 171 | + > |
| 172 | + <DisplayInner value={value} /> |
| 173 | + </div> |
| 174 | + )} |
| 175 | + </div> |
| 176 | + </Popover> |
| 177 | + ); |
| 178 | + } |
| 179 | + if (textOnly) { |
| 180 | + return text; |
| 181 | + } |
| 182 | + return <DisplayInner value={value} />; |
| 183 | +}; |
0 commit comments