@prisma-cms/editor
Version:
Editor for @prisma-cms
138 lines • 5.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decorator = void 0;
const prop_types_1 = __importDefault(require("prop-types"));
const draft_js_android_fix_1 = require("draft-js-android-fix");
const Link_1 = __importDefault(require("material-ui-icons/Link"));
const ToggleBlockType_1 = __importDefault(require("../ToggleBlockType"));
const decorator_1 = __importDefault(require("./decorator"));
exports.decorator = decorator_1.default;
class LinkControl extends ToggleBlockType_1.default {
constructor() {
super(...arguments);
// addLink = (linkTitle, linkUrl, linkUrlOption) => {
this.addLink = () => {
let linkTitle, linkUrl, linkTarget;
const { editorState, onChange } = this.props;
const currentEntity = this.getCurrentEntity();
const selectionText = this.getSelectionText(editorState);
if (!selectionText) {
return;
}
else {
linkTitle = selectionText;
}
// return;
// const { currentEntity } = this.state;
const selection = editorState.getSelection();
if (currentEntity) {
// const entityRange = getEntityRange(editorState, currentEntity);
// selection = selection.merge({
// anchorOffset: entityRange.start,
// focusOffset: entityRange.end,
// });
}
const entityKey = editorState
.getCurrentContent()
.createEntity('LINK', 'MUTABLE', {
url: linkUrl,
target: linkTarget,
})
.getLastCreatedEntityKey();
const contentState = draft_js_android_fix_1.Modifier.replaceText(editorState.getCurrentContent(), selection, `${linkTitle}`, editorState.getCurrentInlineStyle(), entityKey);
// return;
/**
* В версии 0.11-alpha здесь ломается.
* https://draftjs.org/docs/v0-10-api-migration.html#content
*/
const newEditorState = draft_js_android_fix_1.EditorState.push(editorState, contentState, 'insert-characters');
// return;
// insert a blank space after link
// selection = newEditorState.getSelection()
// selection = newEditorState.getSelection().merge({
// anchorOffset: selection.get('anchorOffset') + linkTitle.length,
// focusOffset: selection.get('anchorOffset') + linkTitle.length,
// });
// newEditorState = EditorState.acceptSelection(newEditorState, selection);
// contentState = Modifier.insertText(
// newEditorState.getCurrentContent(),
// selection,
// ' ',
// newEditorState.getCurrentInlineStyle(),
// undefined,
// );
onChange(draft_js_android_fix_1.EditorState.push(newEditorState, contentState, 'insert-characters'));
// this.doCollapse();
};
this.toggleBlockType = () => {
// const {
// onChange,
// editorState,
// blockType,
// } = this.props;
// this.addLink("test link", "https://modxclub.ru")
// const newState = RichUtils.toggleBlockType(
// editorState,
// blockType,
// );
// if (newState) {
// onChange(newState);
// }
const action = this.getCurrentAction();
if (action) {
return action();
}
};
// componentWillReceiveProps(nextProps, nextState) {
// }
// render() {
// const {
// editorState,
// onChange,
// blockType,
// icon: Icon,
// ...other
// } = this.props;
// return <IconButton
// onClick={() => this.toggleBlockType()}
// {...other}
// >
// <Icon />
// </IconButton>
// }
}
getCurrentEntity() {
const { editorState } = this.props;
return this.getSelectionEntity(editorState);
// if (editorState) {
// this.setState({
// currentEntity: getSelectionEntity(editorState),
// });
// }
// modalHandler.registerCallBack(this.expandCollapse);
}
getCurrentAction() {
let action;
const isSelected = this.isTextSelected();
if (isSelected) {
action = this.addLink;
}
return action;
}
isDisabled() {
const { disabled,
// editorState,
} = this.props;
const action = this.getCurrentAction();
return disabled || !action;
}
}
LinkControl.propTypes = Object.assign(Object.assign({}, ToggleBlockType_1.default.propTypes), { blockType: prop_types_1.default.string });
LinkControl.defaultProps = {
icon: Link_1.default,
};
exports.default = LinkControl;
//# sourceMappingURL=index.js.map