From 05f763651eadedacf833cafe7f6c6950201c75bb Mon Sep 17 00:00:00 2001 From: QiShaoXuan Date: Thu, 15 Sep 2022 16:45:28 +0800 Subject: [PATCH] fix: can not convert url text to link after paste --- .../src/editor/clipboard/clipboardUtils.ts | 12 +++++++- .../editor-core/src/editor/clipboard/utils.ts | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/libs/components/editor-core/src/editor/clipboard/clipboardUtils.ts b/libs/components/editor-core/src/editor/clipboard/clipboardUtils.ts index 2b93912d67..6682dfc80f 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 2478a5d722..e40d1484c6 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 } = {};