-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
inline.js
118 lines (98 loc) · 2.82 KB
/
inline.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
/**
* External dependencies
*/
import { uniqueId } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useMemo, useEffect, useState } from '@wordpress/element';
import { withSpokenMessages } from '@wordpress/components';
import { prependHTTP } from '@wordpress/url';
import {
create,
insert,
isCollapsed,
applyFormat,
getTextContent,
slice,
} from '@wordpress/rich-text';
import { __experimentalLinkControl as LinkControl } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { createLinkFormat, isValidHref } from './utils';
function InlineLinkUI( { isActive, activeAttributes, addingLink, value, onChange, speak, stopAddingLink } ) {
const [ mountingKey, setMountingKey ] = useState( uniqueId() );
useEffect( () => {
return () => stopAddingLink();
}, [] );
useEffect( () => {
// Should remount the popover to focus it properly when isAdddingLink changes
if ( addingLink ) {
setMountingKey( uniqueId() );
}
}, [ addingLink ] );
const anchorRef = useMemo( () => {
const selection = window.getSelection();
if ( ! selection.rangeCount ) {
return;
}
const range = selection.getRangeAt( 0 );
if ( addingLink ) {
return range;
}
let element = range.startContainer;
// If the caret is right before the element, select the next element.
element = element.nextElementSibling || element;
while ( element.nodeType !== window.Node.ELEMENT_NODE ) {
element = element.parentNode;
}
return element.closest( 'a' );
}, [ isActive, addingLink, value.start, value.end ] );
if ( ! isActive && ! addingLink ) {
return null;
}
const linkValue = {
url: activeAttributes.url,
opensInNewTab: activeAttributes.target === '_blank',
};
const onChangeLink = ( newValue ) => {
const newUrl = prependHTTP( newValue.url );
const selectedText = getTextContent( slice( value ) );
const format = createLinkFormat( {
url: newUrl,
opensInNewWindow: newValue.opensInNewTab,
text: selectedText,
} );
if ( isCollapsed( value ) && ! isActive ) {
const toInsert = applyFormat( create( { text: newUrl } ), format, 0, newUrl.length );
onChange( insert( value, toInsert ) );
} else {
onChange( applyFormat( value, format ) );
}
if ( ! isValidHref( newUrl ) ) {
speak( __( 'Warning: the link has been inserted but may have errors. Please test it.' ), 'assertive' );
} else if ( isActive ) {
speak( __( 'Link edited.' ), 'assertive' );
} else {
speak( __( 'Link inserted.' ), 'assertive' );
}
if ( addingLink ) {
stopAddingLink();
}
};
return (
<LinkControl
key={ mountingKey }
value={ linkValue }
onChange={ onChangeLink }
onClose={ stopAddingLink }
popoverProps={ {
anchorRef,
focusOnMount: addingLink ? 'firstElement' : false,
} }
/>
);
}
export default withSpokenMessages( InlineLinkUI );