From c21e9c8b8eebef164811a2a8b03bf396d33fcc39 Mon Sep 17 00:00:00 2001 From: bbsqq <1491812683@qq.com> Date: Tue, 7 May 2024 19:30:25 +0800 Subject: [PATCH] fix(ava-react/ntv): allow copy ntv by default behavior --- .../src/NarrativeTextVis/NarrativeTextVis.tsx | 16 +++++++++------- .../src/NarrativeTextVis/types/props.ts | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/ava-react/src/NarrativeTextVis/NarrativeTextVis.tsx b/packages/ava-react/src/NarrativeTextVis/NarrativeTextVis.tsx index 224085f6..ced61d04 100644 --- a/packages/ava-react/src/NarrativeTextVis/NarrativeTextVis.tsx +++ b/packages/ava-react/src/NarrativeTextVis/NarrativeTextVis.tsx @@ -1,6 +1,7 @@ import React, { useRef, useEffect } from 'react'; import { v4 } from 'uuid'; +import { isFunction, isNull, isUndefined } from 'lodash'; import { classnames as cx } from '../utils'; @@ -52,21 +53,22 @@ export function NarrativeTextVis({ useEffect(() => { const onCopy = async (event: ClipboardEvent) => { const { plainText, html } = await getSelectionContentForCopy(); - // 如果没有传递复制方法,默认行为是拦截用户复制操作(使用快捷键或右键选择复制均会触发),将转换后的内容放进剪切板 - // if no `copyNarrative` passed in, the default behavior when user conduct `copy` is to put the transformed html and plainText into user's clipboard - if (!copyNarrative) { + if (isFunction(copyNarrative)) { + copyNarrative({ spec, plainText, html }); + } else if (isUndefined(copyNarrative)) { // 仅成功解析出 plainText 才拦截处理,其他情况下走默认处理 + // 如果没有传递复制方法,默认行为是拦截用户复制操作(使用快捷键或右键选择复制均会触发),将转换后的内容放进剪切板 + // if no `copyNarrative` passed in, the default behavior when user conduct `copy` is to put the transformed html and plainText into user's clipboard // TODO @羽然 此处修改逻辑仅针对复制 disabled Input 的情况,还有更多情况待进一步兼容 if (plainText) { event.preventDefault(); copyToClipboard(html, plainText, onCopySuccess, onCopyFailure); } - } else { - copyNarrative({ spec, plainText, html }); } }; - - narrativeDomRef.current?.addEventListener('copy', onCopy); + if (!isNull(copyNarrative)) { + narrativeDomRef.current?.addEventListener('copy', onCopy); + } return () => { narrativeDomRef.current?.removeEventListener('copy', onCopy); }; diff --git a/packages/ava-react/src/NarrativeTextVis/types/props.ts b/packages/ava-react/src/NarrativeTextVis/types/props.ts index 1ba40133..82e0544c 100644 --- a/packages/ava-react/src/NarrativeTextVis/types/props.ts +++ b/packages/ava-react/src/NarrativeTextVis/types/props.ts @@ -130,7 +130,7 @@ export type NarrativeTextVisProps = ThemeStylesProps & spec: NarrativeTextSpec; /** * @description the function to be called when copy event is listened. If it is undefined, the default behavior is to put the transformed html and plain text into user's clipboard - * @description.监听到 copy 事件时执行的函数,可用于控制复制的内容和复制行为,如果不传,默认将会把转换后的富文本和纯文本内容放入剪切板 + * @description.监听到 copy 事件时执行的函数,可用于控制复制的内容和复制行为,如果不传,默认将会把转换后的富文本和纯文本内容放入剪切板;如果为 null 则执行浏览器默认行为 */ - copyNarrative?: (content: { spec: NarrativeTextSpec; plainText: string; html: string }) => void; + copyNarrative?: null | ((content: { spec: NarrativeTextSpec; plainText: string; html: string }) => void); };