Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
toggle: (block, action, editorState, setEditorState) => {
const selection = editorState.getSelection();
if (selection.isCollapsed()) {
return;
}
if (action.active(block, editorState)) {
setEditorState(RichUtils.toggleLink(editorState, selection, null));
} else {
const href = window.prompt('Enter a URL'); // eslint-disable-line no-alert
const entityKey = Entity.create('link', 'MUTABLE', { href });
setEditorState(RichUtils.toggleLink(editorState, selection, entityKey));
}
},
},
);
entityKey = this.props.entityKey;
}
else {
contentWithEntity = content.createEntity(
entityType.type,
'MUTABLE',
option
);
entityKey = contentWithEntity.getLastCreatedEntityKey();
}
const newEditorState = EditorState.set(
editorState,
{currentContent: contentWithEntity}
);
const nextState = RichUtils.toggleLink(
newEditorState,
newEditorState.getSelection(),
entityKey
);
// Avoid a redundant bug with Draftjs and Firefox.
if (this.input) {
this.input.blur();
}
return onComplete(nextState);
};
export function editorStateSettingLink(editorState, selection, data) {
const contentState = editorState.getCurrentContent();
const entityKey = getCurrentLinkEntityKey(editorState);
let nextEditorState = editorState;
if (!entityKey) {
const contentStateWithEntity = contentState.createEntity(ENTITY_TYPE, 'MUTABLE', data);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
nextEditorState = EditorState.set(editorState, { currentContent: contentStateWithEntity });
nextEditorState = RichUtils.toggleLink(nextEditorState, selection, entityKey);
} else {
nextEditorState = EditorState.set(editorState, {
currentContent: editorState.getCurrentContent().replaceEntityData(entityKey, data),
});
// this is a hack that forces the editor to update
// https://github.com/facebook/draft-js/issues/1047
nextEditorState = EditorState.forceSelection(nextEditorState, editorState.getSelection());
}
return nextEditorState;
}
_removeLink: Function = (): void => {
const { editorState } = this.state;
const selection = editorState.getSelection();
(this: any).onChange(RichUtils.toggleLink(editorState, selection, null));
}
const removeLink = () => {
const selection = editorState.getSelection()
updateStateForPopover(RichUtils.toggleLink(editorState, selection, null))
}
const contentState = editorState.getCurrentContent()
let replaceEditorState = null
const data = {
url: url
}
if (urlKey) {
contentState.replaceEntityData(urlKey, data)
replaceEditorState = EditorState.push(editorState, contentState, "apply-entity")
}
else {
const contentStateWithEntity = contentState.createEntity('LINK', 'MUTABLE', data)
const entityKey = contentStateWithEntity.getLastCreatedEntityKey()
const newEditorState = EditorState.set(editorState, { currentContent: contentStateWithEntity })
replaceEditorState = RichUtils.toggleLink(
newEditorState,
newEditorState.getSelection(),
entityKey)
}
updateStateForPopover(replaceEditorState)
}
export const makePlainText = (editorState) => {
const selection = editorState.getSelection()
const noLinks = RichUtils.toggleLink(editorState, selection, null)
const unstyled = RichUtils.toggleBlockType(noLinks, 'unstyled')
const currentBlocks = unstyled.getCurrentContent().getBlocksAsArray()
const plainBlocks = currentBlocks.map((contentBlock) => {
return stripCharacterStyles(contentBlock)
})
const newContent = ContentState.createFromBlockArray(plainBlocks)
const newState = EditorState.push(editorState, newContent, null)
return newState
}
removeLink = () => {
const { editorState } = this.state
const selection = editorState.getSelection()
const state = {
editorState,
plugin: null,
showUrlInput: false,
url: ''
}
if (!selection.isCollapsed()) {
state.editorState = RichUtils.toggleLink(editorState, selection, null)
}
this.setState(state)
}
createLinkAtSelection(editorState, url) {
const contentState = editorState
.getCurrentContent()
.createEntity('LINK', 'MUTABLE', { url });
const entityKey = contentState.getLastCreatedEntityKey();
const withLink = RichUtils.toggleLink(
editorState,
editorState.getSelection(),
entityKey
);
return EditorState.forceSelection(withLink, editorState.getSelection());
},
unlink() {
if (!this.selection.isCollapsed()) {
this.props.onChange(RichUtils.toggleLink(this.props.editorState, this.selection, null));
}
}