/
index.native.js
132 lines (113 loc) · 2.9 KB
/
index.native.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
/**
* External dependencies
*/
import { find } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { withSpokenMessages } from '@wordpress/components';
import { RichTextToolbarButton } from '@wordpress/block-editor';
import {
applyFormat,
getActiveFormat,
getTextContent,
isCollapsed,
removeFormat,
slice,
} from '@wordpress/rich-text';
import { isURL } from '@wordpress/url';
/**
* Internal dependencies
*/
import ModalLinkUI from './modal';
const name = 'core/link';
export const link = {
name,
title: __( 'Link' ),
tagName: 'a',
className: null,
attributes: {
url: 'href',
target: 'target',
},
edit: withSpokenMessages( class LinkEdit extends Component {
constructor() {
super( ...arguments );
this.addLink = this.addLink.bind( this );
this.stopAddingLink = this.stopAddingLink.bind( this );
this.onRemoveFormat = this.onRemoveFormat.bind( this );
this.state = {
addingLink: false,
};
}
addLink() {
const { value, onChange } = this.props;
const text = getTextContent( slice( value ) );
if ( text && isURL( text ) ) {
onChange( applyFormat( value, { type: name, attributes: { url: text } } ) );
} else {
this.setState( { addingLink: true } );
}
}
stopAddingLink() {
this.setState( { addingLink: false } );
}
getLinkSelection() {
const { value, isActive } = this.props;
const startFormat = getActiveFormat( value, 'core/link' );
// if the link isn't selected, get the link manually by looking around the cursor
// TODO: handle partly selected links
if ( startFormat && isCollapsed( value ) && isActive ) {
let startIndex = value.start;
let endIndex = value.end;
while ( find( value.formats[ startIndex ], startFormat ) ) {
startIndex--;
}
endIndex++;
while ( find( value.formats[ endIndex ], startFormat ) ) {
endIndex++;
}
return {
...value,
start: startIndex + 1,
end: endIndex,
};
}
return value;
}
onRemoveFormat() {
const { onChange, speak } = this.props;
const linkSelection = this.getLinkSelection();
onChange( removeFormat( linkSelection, name ) );
speak( __( 'Link removed.' ), 'assertive' );
}
render() {
const { isActive, activeAttributes, onChange } = this.props;
const linkSelection = this.getLinkSelection();
return (
<>
<ModalLinkUI
isVisible={ this.state.addingLink }
isActive={ isActive }
activeAttributes={ activeAttributes }
onClose={ this.stopAddingLink }
onChange={ onChange }
onRemove={ this.onRemoveFormat }
value={ linkSelection }
/>
<RichTextToolbarButton
name="link"
icon="admin-links"
title={ __( 'Link' ) }
onClick={ this.addLink }
isActive={ isActive }
shortcutType="primary"
shortcutCharacter="k"
/>
</>
);
}
} ),
};