-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathLinkSource.js
117 lines (96 loc) · 2.99 KB
/
LinkSource.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { RichUtils } from 'draft-js';
import Modal from '../components/Modal';
class LinkSource extends Component {
constructor(props) {
super(props);
const { entity } = this.props;
let url = '';
if (entity) {
const data = entity.getData();
url = data.url;
}
this.state = {
url: url,
};
this.onRequestClose = this.onRequestClose.bind(this);
this.onAfterOpen = this.onAfterOpen.bind(this);
this.onConfirm = this.onConfirm.bind(this);
this.onChangeURL = this.onChangeURL.bind(this);
}
onConfirm(e) {
const { editorState, entityType, onComplete } = this.props;
const { url } = this.state;
e.preventDefault();
const contentState = editorState.getCurrentContent();
const data = {
url: url.replace(/\s/g, ''),
};
const contentStateWithEntity = contentState.createEntity(
entityType.type,
'MUTABLE',
data,
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
const nextState = RichUtils.toggleLink(
editorState,
editorState.getSelection(),
entityKey,
);
onComplete(nextState);
}
onRequestClose(e) {
const { onClose } = this.props;
e.preventDefault();
onClose();
}
onAfterOpen() {
if (this.inputRef) {
this.inputRef.focus();
this.inputRef.select();
}
}
onChangeURL(e) {
const url = e.target.value;
this.setState({ url });
}
render() {
const { url } = this.state;
return (
<Modal
onRequestClose={this.onRequestClose}
onAfterOpen={this.onAfterOpen}
isOpen={true}
contentLabel="Link chooser"
>
<form className="LinkSource" onSubmit={this.onConfirm}>
<label className={`form-field`}>
<span className="form-field__label">Link URL</span>
<input
ref={inputRef => {
this.inputRef = inputRef;
}}
type="text"
onChange={this.onChangeURL}
value={url}
placeholder="www.example.com"
/>
</label>
<button>Save</button>
</form>
</Modal>
);
}
}
LinkSource.propTypes = {
editorState: PropTypes.object.isRequired,
onComplete: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
entityType: PropTypes.object.isRequired,
entity: PropTypes.object,
};
LinkSource.defaultProps = {
entity: null,
};
export default LinkSource;