Skip to content

Commit

Permalink
fix(ava-react/ntv): allow copy ntv by default behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
BBSQQ committed May 9, 2024
1 parent 4e3a8ac commit c21e9c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions packages/ava-react/src/NarrativeTextVis/NarrativeTextVis.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/ava-react/src/NarrativeTextVis/types/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit c21e9c8

Please sign in to comment.