@prisma-cms/editor
Version:
Editor for @prisma-cms
120 lines • 4.36 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const prop_types_1 = __importDefault(require("prop-types"));
const draft_js_android_fix_1 = require("draft-js-android-fix");
class PrismaDecorator extends react_1.Component {
constructor() {
super(...arguments);
this.state = {
editing: false,
showEditor: false,
};
this.startEdit = () => {
// event.preventDefault();
// event.stopPropagation();
const { editing } = this.state;
const { onEditStart } = this.props;
if (!editing) {
this.setState({
editing: true,
});
onEditStart();
}
return;
};
this.endEdit = () => {
// event.preventDefault();
// event.stopPropagation();
const { editing } = this.state;
const { onEditEnd } = this.props;
if (editing) {
this.setState({
editing: false,
});
onEditEnd();
}
return;
};
this.showEditor = () => {
// event.preventDefault();
// event.stopPropagation();
this.setState({
showEditor: true,
});
};
this.hideEditor = () => {
this.setState({
showEditor: false,
});
};
this.updateData = (data) => {
const { entityKey, contentState, getEditorState, onChange } = this.props;
const blocksArray = contentState.getBlocksAsArray();
let newContentState = draft_js_android_fix_1.ContentState.createFromBlockArray(blocksArray);
newContentState = newContentState.mergeEntityData(entityKey, data);
const editorState = getEditorState();
const newEditorState = draft_js_android_fix_1.EditorState.push(editorState, newContentState, 'change-block-data');
onChange(newEditorState);
this.forceUpdate();
};
}
// componentWillReceiveProps(nextProps, nextState) {
// }
getCurrentEntity() {
const { entityKey, contentState } = this.props;
return contentState.getEntity(entityKey);
}
getEntityRange(editorState, entityKey) {
const block = this.getSelectedBlock(editorState);
let entityRange;
block.findEntityRanges((value) => value.get('entity') === entityKey, (start, end) => {
entityRange = {
start,
end,
text: block.get('text').slice(start, end),
};
});
return entityRange;
}
/**
* Function returns the first selected block.
*/
getSelectedBlock(editorState) {
if (editorState) {
return this.getSelectedBlocksList(editorState).get(0);
}
return undefined;
}
getSelectedBlocksList(editorState) {
return this.getSelectedBlocksMap(editorState).toList();
}
getSelectedBlocksMap(editorState) {
const selectionState = editorState.getSelection();
const contentState = editorState.getCurrentContent();
const startKey = selectionState.getStartKey();
const endKey = selectionState.getEndKey();
const blockMap = contentState.getBlockMap();
return blockMap
.toSeq()
.skipUntil((_, k) => k === startKey)
.takeUntil((_, k) => k === endKey)
.concat([[endKey, blockMap.get(endKey)]]);
}
}
PrismaDecorator.propTypes = {
entityKey: prop_types_1.default.string.isRequired,
children: prop_types_1.default.array,
contentState: prop_types_1.default.object,
onEditStart: prop_types_1.default.func.isRequired,
onEditEnd: prop_types_1.default.func.isRequired,
onChange: prop_types_1.default.func.isRequired,
getEditorState: prop_types_1.default.func.isRequired,
isReadOnly: prop_types_1.default.func.isRequired,
};
PrismaDecorator.contextTypes = {};
exports.default = PrismaDecorator;
//# sourceMappingURL=index.js.map