diff --git a/libs/components/editor-core/src/editor/clipboard/clipboardUtils.ts b/libs/components/editor-core/src/editor/clipboard/clipboardUtils.ts index 2b93912d6..6682dfc80 100644 --- a/libs/components/editor-core/src/editor/clipboard/clipboardUtils.ts +++ b/libs/components/editor-core/src/editor/clipboard/clipboardUtils.ts @@ -3,7 +3,12 @@ import { SelectBlock, SelectInfo } from '../selection'; import { AsyncBlock } from '../block'; import { ClipBlockInfo, OFFICE_CLIPBOARD_MIMETYPE } from './types'; import { Clip } from './clip'; -import { commonHTML2Block, commonHTML2Text } from './utils'; +import { + commonHTML2Block, + commonHTML2Text, + getIsTextLink, + linkText2Block, +} from './utils'; export class ClipboardUtils { private _editor: Editor; @@ -197,6 +202,11 @@ export class ClipboardUtils { textToBlock(clipData = ''): ClipBlockInfo[] { return clipData.split('\n').map((str: string) => { + const isLink = getIsTextLink(str); + if (isLink) { + return linkText2Block(clipData); + } + return { type: 'text', properties: { diff --git a/libs/components/editor-core/src/editor/clipboard/utils.ts b/libs/components/editor-core/src/editor/clipboard/utils.ts index 2478a5d72..e40d1484c 100644 --- a/libs/components/editor-core/src/editor/clipboard/utils.ts +++ b/libs/components/editor-core/src/editor/clipboard/utils.ts @@ -5,6 +5,34 @@ import { ClipBlockInfo } from './types'; const getIsLink = (htmlElement: HTMLElement) => { return ['A', 'IMG'].includes(htmlElement.tagName); }; +export const getIsTextLink = (str: string) => { + const regex = new RegExp( + /https?:\/\/(www\.)?[-a-zA-Z\d@:%._+~#=]{1,256}\.[a-zA-Z\d()]{1,6}\b([-a-zA-Z\d()@:%_+.~#?&//=]*)/gi + ); + return regex.test(str); +}; +export const linkText2Block = (url: string) => { + return { + type: 'text', + properties: { + text: { + value: [ + { + children: [ + { + text: url, + }, + ], + type: 'link', + url: url, + id: getRandomString('link'), + }, + ], + }, + }, + children: [], + } as unknown as ClipBlockInfo; +}; const getTextStyle = (htmlElement: HTMLElement) => { const tagName = htmlElement.tagName; const textStyle: { [key: string]: any } = {};