-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
162 lines (144 loc) · 4.29 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
157
158
159
160
161
162
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
Toolbar,
withSpokenMessages,
} from '@wordpress/components';
import { rawShortcut } from '@wordpress/keycodes';
import { Component } from '@wordpress/element';
import {
applyFormat,
removeFormat,
getActiveFormat,
getTextContent,
slice,
} from '@wordpress/rich-text';
import { isURL } from '@wordpress/url';
/**
* Internal dependencies
*/
import { FORMATTING_CONTROLS } from '../formatting-controls';
import LinkContainer from './link-container';
import ToolbarContainer from './toolbar-container';
class FormatToolbar extends Component {
constructor( { editor } ) {
super( ...arguments );
this.removeLink = this.removeLink.bind( this );
this.addLink = this.addLink.bind( this );
this.stopAddingLink = this.stopAddingLink.bind( this );
this.applyFormat = this.applyFormat.bind( this );
this.removeFormat = this.removeFormat.bind( this );
this.getActiveFormat = this.getActiveFormat.bind( this );
this.toggleFormat = this.toggleFormat.bind( this );
this.state = {
addingLink: false,
};
if ( editor ) {
editor.shortcuts.add( rawShortcut.primary( 'k' ), '', this.addLink );
editor.shortcuts.add( rawShortcut.access( 'a' ), '', this.addLink );
editor.shortcuts.add( rawShortcut.access( 's' ), '', this.removeLink );
editor.shortcuts.add( rawShortcut.access( 'd' ), '', () => this.toggleFormat( { type: 'del' } ) );
editor.shortcuts.add( rawShortcut.access( 'x' ), '', () => this.toggleFormat( { type: 'code' } ) );
}
}
removeLink() {
this.removeFormat( 'a' );
this.props.speak( __( 'Link removed.' ), 'assertive' );
}
addLink() {
const text = getTextContent( slice( this.props.record ) );
if ( text && isURL( text ) ) {
this.applyFormat( {
type: 'a',
attributes: {
href: text,
},
} );
} else {
this.setState( { addingLink: true } );
}
}
stopAddingLink() {
this.setState( { addingLink: false } );
}
/**
* Apply a format with the current value and selection.
*
* @param {Object} format The format to apply.
*/
applyFormat( format ) {
this.props.onChange( applyFormat( this.props.record, format ) );
}
/**
* Remove a format from the current value with the current selection.
*
* @param {string} formatType The type of format to remove.
*/
removeFormat( formatType ) {
this.props.onChange( removeFormat( this.props.record, formatType ) );
}
/**
* Get the current format based on the selection
*
* @param {string} formatType The type of format to check.
*
* @return {boolean} Whether the format is active or not.
*/
getActiveFormat( formatType ) {
return getActiveFormat( this.props.record, formatType );
}
/**
* Toggle a format based on the selection.
*
* @param {Object} format The format to toggle.
*/
toggleFormat( format ) {
if ( this.getActiveFormat( format.type ) ) {
this.removeFormat( format.type );
} else {
this.applyFormat( format );
}
}
render() {
const link = this.getActiveFormat( 'a' );
const toolbarControls = FORMATTING_CONTROLS
.filter( ( control ) => this.props.enabledControls.indexOf( control.format ) !== -1 )
.map( ( control ) => {
if ( control.format === 'link' ) {
const linkIsActive = link !== undefined;
return {
...control,
shortcut: linkIsActive ? control.activeShortcut : control.shortcut,
icon: linkIsActive ? 'editor-unlink' : 'admin-links', // TODO: Need proper unlink icon
title: linkIsActive ? __( 'Unlink' ) : __( 'Link' ),
onClick: linkIsActive ? this.removeLink : this.addLink,
isActive: !! linkIsActive,
};
}
return {
...control,
onClick: () => this.toggleFormat( { type: control.selector } ),
isActive: this.getActiveFormat( control.selector ) !== undefined,
};
} );
return (
<ToolbarContainer>
<Toolbar controls={ toolbarControls } />
<LinkContainer
link={ link }
record={ this.props.record }
onChange={ this.props.onChange }
applyFormat={ this.applyFormat }
removeFormat={ this.removeFormat }
getActiveFormat={ this.getActiveFormat }
toggleFormat={ this.toggleFormat }
addingLink={ this.state.addingLink }
stopAddingLink={ this.stopAddingLink }
/>
</ToolbarContainer>
);
}
}
export default withSpokenMessages( FormatToolbar );