contenido
Version:
Contenido is a library with a set of tools to help you create your own rich text editor on top of draft-js without thinking about how to handle things.
33 lines (25 loc) • 826 B
text/typescript
// Core
import { EditorState, RichUtils } from '../../core';
// Custom Types
import type { LinkAttributes, State, StateHandler } from '../../types';
const addLink = (
state: State,
stateHandler: StateHandler,
attributes?: LinkAttributes,
customLinkKey?: string
) => {
const currentContent = state.getCurrentContent();
const contentStateWithEntity = currentContent.createEntity(
customLinkKey ?? 'link',
'IMMUTABLE',
attributes
);
const entityKey = currentContent.getLastCreatedEntityKey();
const newState = EditorState.set(state, {
currentContent: contentStateWithEntity,
});
const selection = newState.getSelection();
const stateToSet = RichUtils.toggleLink(newState, selection, entityKey);
stateHandler(stateToSet);
};
export default addLink;