draft-js-checkable-list-item
Version:
Checkable list item for Draft.js
88 lines (62 loc) • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _EditorState = _interopRequireDefault(require("draft-js/lib/EditorState"));
var _draftJsModifiers = require("draft-js-modifiers");
var _constants = require("./constants");
var _CheckableListItem = _interopRequireDefault(require("./CheckableListItem"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
var CheckableListItemUtils =
/*#__PURE__*/
function () {
function CheckableListItemUtils() {
_classCallCheck(this, CheckableListItemUtils);
}
_createClass(CheckableListItemUtils, null, [{
key: "toggleChecked",
value: function toggleChecked(editorState, block) {
return (0, _draftJsModifiers.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 !== _constants.UNORDERED_LIST_ITEM && type !== _constants.ORDERED_LIST_ITEM && type !== _constants.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 !== _constants.UNORDERED_LIST_ITEM && typeAbove !== _constants.ORDERED_LIST_ITEM && typeAbove !== _constants.CHECKABLE_LIST_ITEM) {
return editorState;
}
var depth = block.getDepth();
if (!event.shiftKey && depth === maxDepth) {
return editorState;
}
maxDepth = Math.min(blockAbove.getDepth() + 1, maxDepth);
return (0, _draftJsModifiers.adjustBlockDepth)(editorState, event.shiftKey ? -1 : 1, maxDepth);
}
}]);
return CheckableListItemUtils;
}();
exports.default = CheckableListItemUtils;