From 3befbc1d68ccd692f438a2d065665c76eb71949f Mon Sep 17 00:00:00 2001 From: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Date: Sun, 26 Jan 2025 15:12:05 +0800 Subject: [PATCH] feat: docx image preview (#13057) --- web/app/components/base/markdown.tsx | 4 +- .../detail/completed/SegmentCard.tsx | 259 ------------------ .../detail/completed/common/chunk-content.tsx | 12 +- .../completed/segment-card/chunk-content.tsx | 56 ++++ .../index.tsx} | 60 ++-- .../detail/completed/segment-detail.tsx | 4 +- .../skeleton/general-list-skeleton.tsx | 2 +- .../documents/detail/new-segment-modal.tsx | 156 ----------- .../components/chunk-detail-modal.tsx | 10 +- .../hit-testing/components/result-item.tsx | 13 +- .../components/datasets/hit-testing/index.tsx | 10 +- web/models/datasets.ts | 8 +- 12 files changed, 115 insertions(+), 479 deletions(-) delete mode 100644 web/app/components/datasets/documents/detail/completed/SegmentCard.tsx create mode 100644 web/app/components/datasets/documents/detail/completed/segment-card/chunk-content.tsx rename web/app/components/datasets/documents/detail/completed/{segment-card.tsx => segment-card/index.tsx} (85%) delete mode 100644 web/app/components/datasets/documents/detail/new-segment-modal.tsx diff --git a/web/app/components/base/markdown.tsx b/web/app/components/base/markdown.tsx index abb9a546ca..978a34a60a 100644 --- a/web/app/components/base/markdown.tsx +++ b/web/app/components/base/markdown.tsx @@ -9,7 +9,7 @@ import RemarkGfm from 'remark-gfm' import RehypeRaw from 'rehype-raw' import SyntaxHighlighter from 'react-syntax-highlighter' import { atelierHeathLight } from 'react-syntax-highlighter/dist/esm/styles/hljs' -import { Component, createContext, memo, useContext, useEffect, useMemo, useRef, useState } from 'react' +import { Component, createContext, memo, useContext, useMemo, useRef, useState } from 'react' import cn from '@/utils/classnames' import CopyBtn from '@/app/components/base/copy-btn' import SVGBtn from '@/app/components/base/svg' @@ -241,7 +241,7 @@ const Link: Components['a'] = ({ node, ...props }) => { export function Markdown(props: { content: string; className?: string }) { const latexContent = preprocessLaTeX(props.content) return ( -
+
= ({ percent, loading }) => { - return ( -
-
-
-
-
{loading ? null : percent.toFixed(2)}
-
- ) -} - -type DocumentTitleProps = { - extension?: string - name?: string - iconCls?: string - textCls?: string - wrapperCls?: string -} - -const DocumentTitle: FC = ({ extension, name, iconCls, textCls, wrapperCls }) => { - const localExtension = extension?.toLowerCase() || name?.split('.')?.pop()?.toLowerCase() - return
-
- {name || '--'} -
-} - -export type UsageScene = 'doc' | 'hitTesting' - -type ISegmentCardProps = { - loading: boolean - detail?: SegmentDetailModel & { document: { name: string } } - contentExternal?: string - refSource?: { - title: string - uri: string - } - isExternal?: boolean - score?: number - onClick?: () => void - onChangeSwitch?: (segId: string, enabled: boolean) => Promise - onDelete?: (segId: string) => Promise - scene?: UsageScene - className?: string - archived?: boolean - embeddingAvailable?: boolean -} - -const SegmentCard: FC = ({ - detail = {}, - contentExternal, - isExternal, - refSource, - score, - onClick, - onChangeSwitch, - onDelete, - loading = true, - scene = 'doc', - className = '', - archived, - embeddingAvailable, -}) => { - const { t } = useTranslation() - const { - id, - position, - enabled, - content, - word_count, - hit_count, - index_node_hash, - answer, - } = detail as Required['detail'] - const isDocScene = scene === 'doc' - const [showModal, setShowModal] = useState(false) - - const renderContent = () => { - if (answer) { - return ( - <> -
-
Q
-
{content}
-
-
-
A
-
{answer}
-
- - ) - } - - if (contentExternal) - return contentExternal - - return content - } - - return ( -
onClick?.()} - > -
- {isDocScene - ? <> - -
- {loading - ? ( - - ) - : ( - <> - - {embeddingAvailable && ( -
- -
) => - e.stopPropagation() - } - className="inline-flex items-center" - > - { - await onChangeSwitch?.(id, val) - }} - /> -
-
- )} - - )} -
- - : ( - score !== null - ? ( -
-
- -
- ) - : null - )} -
- {loading - ? ( -
-
-
- ) - : ( - isDocScene - ? <> -
- {renderContent()} -
-
-
-
-
{formatNumber(word_count)}
-
-
-
-
{formatNumber(hit_count)}
-
-
-
-
{index_node_hash}
-
- {!archived && embeddingAvailable && ( -
{ - e.stopPropagation() - setShowModal(true) - }}> - -
- )} -
- - : <> -
- {renderContent()} -
-
- -
- -
- {isExternal ? t('datasetHitTesting.viewDetail') : t('datasetHitTesting.viewChart')} - -
-
-
- - )} - {showModal - && { await onDelete?.(id) }} - onCancel={() => setShowModal(false)} - /> - } -
- ) -} - -export default SegmentCard diff --git a/web/app/components/datasets/documents/detail/completed/common/chunk-content.tsx b/web/app/components/datasets/documents/detail/completed/common/chunk-content.tsx index e6403fa12f..af2989b188 100644 --- a/web/app/components/datasets/documents/detail/completed/common/chunk-content.tsx +++ b/web/app/components/datasets/documents/detail/completed/common/chunk-content.tsx @@ -3,6 +3,7 @@ import type { ComponentProps, FC } from 'react' import { useTranslation } from 'react-i18next' import { ChunkingMode } from '@/models/datasets' import classNames from '@/utils/classnames' +import { Markdown } from '@/app/components/base/markdown' type IContentProps = ComponentProps<'textarea'> @@ -52,7 +53,7 @@ const AutoResizeTextArea: FC = React.memo(({ if (!textarea) return textarea.style.height = 'auto' - const lineHeight = parseInt(getComputedStyle(textarea).lineHeight) + const lineHeight = Number.parseInt(getComputedStyle(textarea).lineHeight) const textareaHeight = Math.max(textarea.scrollHeight, lineHeight) textarea.style.height = `${textareaHeight}px` }, [value]) @@ -175,6 +176,15 @@ const ChunkContent: FC = ({ /> } + if (!isEditMode) { + return ( + + ) + } + return (