mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-18 18:46:19 +08:00
fix: can not convert url text to link after paste
This commit is contained in:
@@ -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: {
|
||||
|
||||
@@ -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 } = {};
|
||||
|
||||
Reference in New Issue
Block a user