-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
156 lines (143 loc) · 3.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import {
getTextContent,
applyFormat,
removeFormat,
slice,
isCollapsed,
} from '@wordpress/rich-text';
import { isURL, isEmail } from '@wordpress/url';
import {
RichTextToolbarButton,
RichTextShortcut,
} from '@wordpress/block-editor';
import { decodeEntities } from '@wordpress/html-entities';
import { link as linkIcon, linkOff } from '@wordpress/icons';
import { speak } from '@wordpress/a11y';
/**
* Internal dependencies
*/
import InlineLinkUI from './inline';
import { isValidHref } from './utils';
const name = 'core/link';
const title = __( 'Link' );
function Edit( {
isActive,
activeAttributes,
value,
onChange,
onFocus,
contentRef,
} ) {
const [ addingLink, setAddingLink ] = useState( false );
function addLink() {
const text = getTextContent( slice( value ) );
if ( text && isURL( text ) && isValidHref( text ) ) {
onChange(
applyFormat( value, {
type: name,
attributes: { url: text },
} )
);
} else if ( text && isEmail( text ) ) {
onChange(
applyFormat( value, {
type: name,
attributes: { url: `mailto:${ text }` },
} )
);
} else {
setAddingLink( true );
}
}
function stopAddingLink( returnFocus = true ) {
setAddingLink( false );
if ( returnFocus ) {
onFocus();
}
}
function onRemoveFormat() {
onChange( removeFormat( value, name ) );
speak( __( 'Link removed.' ), 'assertive' );
}
return (
<>
<RichTextShortcut type="primary" character="k" onUse={ addLink } />
<RichTextShortcut
type="primaryShift"
character="k"
onUse={ onRemoveFormat }
/>
{ isActive && (
<RichTextToolbarButton
name="link"
icon={ linkOff }
title={ __( 'Unlink' ) }
onClick={ onRemoveFormat }
isActive={ isActive }
shortcutType="primaryShift"
shortcutCharacter="k"
/>
) }
{ ! isActive && (
<RichTextToolbarButton
name="link"
icon={ linkIcon }
title={ title }
onClick={ addLink }
isActive={ isActive }
shortcutType="primary"
shortcutCharacter="k"
/>
) }
{ ( addingLink || isActive ) && (
<InlineLinkUI
addingLink={ addingLink }
stopAddingLink={ stopAddingLink }
isActive={ isActive }
activeAttributes={ activeAttributes }
value={ value }
onChange={ onChange }
contentRef={ contentRef }
/>
) }
</>
);
}
export const link = {
name,
title,
tagName: 'a',
className: null,
attributes: {
url: 'href',
type: 'data-type',
id: 'data-id',
target: 'target',
},
__unstablePasteRule( value, { html, plainText } ) {
if ( isCollapsed( value ) ) {
return value;
}
const pastedText = ( html || plainText )
.replace( /<[^>]+>/g, '' )
.trim();
// A URL was pasted, turn the selection into a link.
if ( ! isURL( pastedText ) ) {
return value;
}
// Allows us to ask for this information when we get a report.
window.console.log( 'Created link:\n\n', pastedText );
return applyFormat( value, {
type: name,
attributes: {
url: decodeEntities( pastedText ),
},
} );
},
edit: Edit,
};