draft-js-checkable-list-item
Version:
Checkable list item for Draft.js
76 lines (56 loc) • 3.05 kB
JavaScript
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return right[Symbol.hasInstance](left); } else { return left instanceof right; } }
function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
import EditorState from 'draft-js/lib/EditorState';
import { adjustBlockDepth, mergeBlockDataByKey } from 'draft-js-modifiers';
import { CHECKABLE_LIST_ITEM, UNORDERED_LIST_ITEM, ORDERED_LIST_ITEM } from './constants';
import CheckableListItem from './CheckableListItem';
var CheckableListItemUtils =
/*#__PURE__*/
function () {
function CheckableListItemUtils() {
_classCallCheck(this, CheckableListItemUtils);
}
_createClass(CheckableListItemUtils, null, [{
key: "toggleChecked",
value: function toggleChecked(editorState, block) {
return mergeBlockDataByKey(editorState, block.getKey(), {
checked: !block.getData().get('checked')
});
} // https://github.com/facebook/draft-js/blob/0.10-stable/src/model/modifier/RichTextEditorUtil.js#L190
}, {
key: "onTab",
value: function onTab(event, editorState, maxDepth) {
var selection = editorState.getSelection();
var key = selection.getAnchorKey();
if (key !== selection.getFocusKey()) {
return editorState;
}
var content = editorState.getCurrentContent();
var block = content.getBlockForKey(key);
var type = block.getType();
if (type !== UNORDERED_LIST_ITEM && type !== ORDERED_LIST_ITEM && type !== CHECKABLE_LIST_ITEM) {
return editorState;
}
event.preventDefault(); // Only allow indenting one level beyond the block above, and only if
// the block above is a list item as well.
var blockAbove = content.getBlockBefore(key);
if (!blockAbove) {
return editorState;
}
var typeAbove = blockAbove.getType();
if (typeAbove !== UNORDERED_LIST_ITEM && typeAbove !== ORDERED_LIST_ITEM && typeAbove !== CHECKABLE_LIST_ITEM) {
return editorState;
}
var depth = block.getDepth();
if (!event.shiftKey && depth === maxDepth) {
return editorState;
}
maxDepth = Math.min(blockAbove.getDepth() + 1, maxDepth);
return adjustBlockDepth(editorState, event.shiftKey ? -1 : 1, maxDepth);
}
}]);
return CheckableListItemUtils;
}();
export { CheckableListItemUtils as default };