Skip to content

Commit ba386c1

Browse files
author
vikasrohit
authored
Merge pull request #3029 from appirio-tech/feature/draftjs-link-editor
New DraftJs Link Editor functionality…
2 parents f65e72d + 3e7e38b commit ba386c1

File tree

14 files changed

+1714
-175
lines changed

14 files changed

+1714
-175
lines changed

package-lock.json

Lines changed: 75 additions & 89 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
"draft-js": "^0.10.1",
8888
"draft-js-drag-n-drop-plugin": "^2.0.0-rc2",
8989
"draft-js-image-plugin": "^2.0.0-rc2",
90-
"draft-js-link-plugin": "^1.2.2",
9190
"draft-js-mention-plugin": "^2.0.0-rc2",
9291
"draft-js-plugins-editor": "^2.0.0-rc2",
9392
"draft-js-utils": "^0.1.7",
@@ -125,6 +124,7 @@
125124
"react-infinite-scroller": "^1.1.1",
126125
"react-layout-pane": "^0.1.16",
127126
"react-modal": "^1.9.7",
127+
"react-popper": "^1.3.3",
128128
"react-redux": "^4.4.5",
129129
"react-responsive": "^4.1.0",
130130
"react-router-dom": "^4.2.2",

src/components/RichTextArea/AddLinkButton.jsx

Lines changed: 10 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom'
3-
import {EditorState, RichUtils, SelectionState} from 'draft-js'
3+
import {EditorState, RichUtils} from 'draft-js'
44
import addImage from 'draft-js-image-plugin/lib/modifiers/addImage'
55
import linkifyIt from 'linkify-it'
66
import tlds from 'tlds'
7-
import {hasEntity, getCurrentEntity} from '../../helpers/draftJSHelper'
87
import EditorIcons from './EditorIcons'
98
import Alert from 'react-s-alert'
109

@@ -175,31 +174,6 @@ class LinkModal extends React.Component {
175174
}
176175
}
177176

178-
class AddLinkModal extends React.Component {
179-
render () {
180-
const editorState = this.props.getEditorState()
181-
const entitySelected = hasEntity('LINK', editorState)
182-
const entity = getCurrentEntity(editorState)
183-
let entityData = null
184-
185-
if (entitySelected && entity) {
186-
entityData = entity.getData()
187-
}
188-
const url = entityData ? entityData.url : null
189-
190-
return (
191-
<div className={'addLinkModal'}>
192-
<LinkModal
193-
url={url}
194-
showUnlink={entitySelected}
195-
type={'LINK'}
196-
{...this.props}
197-
/>
198-
</div>
199-
)
200-
}
201-
}
202-
203177
class AddImageModal extends React.Component {
204178
render () {
205179
return (
@@ -224,51 +198,20 @@ export default class AddLinkButton extends React.Component {
224198
}
225199

226200
toggleAddLink() {
227-
const editorState = this.props.getEditorState()
228-
const selection = editorState.getSelection()
229-
if (selection.isCollapsed()) {
230-
Alert.error('Please select some piece of text .')
231-
}
232-
if (!selection.getHasFocus()) {
233-
return
234-
}
235-
if (this.props.type === 'link' && selection.isCollapsed()) {
236-
const currentEntity = getCurrentEntity(editorState)
237-
if (currentEntity && currentEntity.getType() === 'LINK') {
238-
return
201+
if (this.props.type !== 'link') {
202+
const editorState = this.props.getEditorState()
203+
const selection = editorState.getSelection()
204+
if (selection.isCollapsed()) {
205+
Alert.error('Please select some piece of text .')
239206
}
240-
241-
const key = selection.getAnchorKey()
242-
const block = editorState
243-
.getCurrentContent()
244-
.getBlockForKey(key)
245-
const text = block.getText()
246-
const match = linkify.match(text)
247-
248-
if (!match || !match.length) {
207+
if (!selection.getHasFocus()) {
249208
return
250209
}
251210

252-
const contentState = editorState.getCurrentContent()
253-
const contentStateWithEntity = contentState.createEntity('LINK', 'MUTABLE', {url: match[0].url})
254-
const entityKey = contentStateWithEntity.getLastCreatedEntityKey()
255-
256-
const selectionState = SelectionState.createEmpty(key)
257-
const updatedSelection = selectionState.merge({
258-
anchorOffset: 0,
259-
focusOffset: block.getLength()
260-
})
261-
262-
const newState = RichUtils.toggleLink(
263-
editorState,
264-
updatedSelection,
265-
entityKey
266-
)
267-
this.props.setEditorState(newState)
268-
return
211+
this.show()
212+
} else {
213+
this.props.onEditLink()
269214
}
270-
271-
this.show()
272215
}
273216

274217
show() {
@@ -296,15 +239,6 @@ export default class AddLinkButton extends React.Component {
296239
EditorIcons.render(type, active || modalVisible)
297240
}
298241
</button>
299-
{
300-
modalVisible && type === 'link' &&
301-
<AddLinkModal
302-
getEditorState={getEditorState}
303-
setEditorState={setEditorState}
304-
theme={theme}
305-
closeModal={this.hide}
306-
/>
307-
}
308242
{
309243
modalVisible && type === 'image' &&
310244
<AddImageModal
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
@import "~tc-ui/src/styles/tc-includes";
2+
3+
.wrapper {
4+
font-size: 14px;
5+
background-color: $tc-gray-0;
6+
padding: $base-unit * 3;
7+
border-radius: $base-unit;
8+
box-shadow: 1px 1px 18px 1px rgba($tc-gray-50, 0.34);
9+
z-index: 4;
10+
}
11+
12+
.editForm {
13+
width: 360px;
14+
}
15+
16+
.actionBtnLink,
17+
.link:link,
18+
.link:active,
19+
.link:visited {
20+
color: $tc-dark-blue-110;
21+
text-decoration: none;
22+
cursor: pointer;
23+
max-width: 400px;
24+
overflow: hidden;
25+
text-overflow: ellipsis;
26+
}
27+
28+
.formControlContainer {
29+
position: relative;
30+
margin-top: 15px;
31+
margin-bottom: 18px;
32+
}
33+
34+
.formControlLabel {
35+
position: absolute;
36+
background-color: $tc-gray-0;
37+
color: $tc-gray-70;
38+
font-size: 12px;
39+
left: 12px;
40+
top: 0;
41+
transform: translateY(-50%);
42+
}
43+
44+
.formControl {
45+
border-color: $tc-gray-10;
46+
47+
&:focus {
48+
border-color: $tc-light-blue-110;
49+
}
50+
}
51+
52+
.applyButtonWrapper {
53+
text-align: right;
54+
}
55+
56+
.popperArrow {
57+
width: 0;
58+
height: 0;
59+
border-style: solid;
60+
position: absolute;
61+
margin: 5px;
62+
}
63+
64+
.wrapper[data-placement^="top"],
65+
.wrapper[data-placement^="bottom"] {
66+
.popperArrow {
67+
border-width: 5px;
68+
left: calc(50% - 5px);
69+
margin-top: 0;
70+
margin-bottom: 0;
71+
}
72+
}
73+
74+
.wrapper[data-placement^="top"] {
75+
margin-bottom: 5px;
76+
77+
.popperArrow {
78+
bottom: -5px;
79+
border-bottom-width: 0;
80+
border-color: $tc-gray-0 transparent transparent transparent;
81+
}
82+
}
83+
84+
.wrapper[data-placement^="bottom"] {
85+
margin-top: 5px;
86+
87+
.popperArrow {
88+
top: -5px;
89+
border-top-width: 0;
90+
border-color: transparent transparent $tc-gray-0 transparent;
91+
}
92+
}
93+
94+
.wrapper[data-placement^="top"],
95+
.wrapper[data-placement^="bottom"] {
96+
.hidePopperArrow {
97+
border-color: transparent;
98+
}
99+
}

0 commit comments

Comments
 (0)