UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

124 lines (121 loc) 4.96 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import ReactNodeView from '../react-node-view'; /** * A ReactNodeView that handles React components sensitive * to selection changes. * * If the selection changes, it will attempt to re-render the * React component. Otherwise it does nothing. * * You can subclass `viewShouldUpdate` to include other * props that your component might want to consider before * entering the React lifecycle. These are usually props you * compare in `shouldComponentUpdate`. * * An example: * * ``` * viewShouldUpdate(nextNode) { * if (nextNode.attrs !== this.node.attrs) { * return true; * } * * return super.viewShouldUpdate(nextNode); * }``` */ export var SelectionBasedNodeView = /*#__PURE__*/function (_ReactNodeView) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/max-params function SelectionBasedNodeView(node, view, getPos, portalProviderAPI, eventDispatcher, reactComponentProps, // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any reactComponent, viewShouldUpdate) { var _this; _classCallCheck(this, SelectionBasedNodeView); _this = _callSuper(this, SelectionBasedNodeView, [node, view, getPos, portalProviderAPI, eventDispatcher, reactComponentProps, reactComponent, viewShouldUpdate]); _defineProperty(_this, "isSelectedNode", false); // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/max-params _defineProperty(_this, "isNodeInsideSelection", function (from, to, pos, posEnd) { var _this$getPositionsWit = _this.getPositionsWithDefault(pos, posEnd); pos = _this$getPositionsWit.pos; posEnd = _this$getPositionsWit.posEnd; if (typeof pos !== 'number' || typeof posEnd !== 'number') { return false; } return from <= pos && to >= posEnd; }); // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/max-params _defineProperty(_this, "isSelectionInsideNode", function (from, to, pos, posEnd) { var _this$getPositionsWit2 = _this.getPositionsWithDefault(pos, posEnd); pos = _this$getPositionsWit2.pos; posEnd = _this$getPositionsWit2.posEnd; if (typeof pos !== 'number' || typeof posEnd !== 'number') { return false; } return pos < from && to < posEnd; }); _defineProperty(_this, "insideSelection", function () { var _this$view$state$sele = _this.view.state.selection, from = _this$view$state$sele.from, to = _this$view$state$sele.to; return _this.isSelectedNode || _this.isSelectionInsideNode(from, to); }); _defineProperty(_this, "nodeInsideSelection", function () { var selection = _this.view.state.selection; var from = selection.from, to = selection.to; return _this.isSelectedNode || _this.isNodeInsideSelection(from, to); }); _this.updatePos(); return _this; } /** * Update current node's start and end positions. * * Prefer `this.pos` rather than getPos(), because calling getPos is * expensive, unless you know you're definitely going to render. */ _inherits(SelectionBasedNodeView, _ReactNodeView); return _createClass(SelectionBasedNodeView, [{ key: "updatePos", value: function updatePos() { if (typeof this.getPos === 'boolean') { return; } var pos = this.getPos(); if (typeof pos === 'number') { this.pos = pos; this.posEnd = pos + this.node.nodeSize; } } }, { key: "getPositionsWithDefault", value: function getPositionsWithDefault(pos, posEnd) { return { pos: typeof pos !== 'number' ? this.pos : pos, posEnd: typeof posEnd !== 'number' ? this.posEnd : posEnd }; } }, { key: "selectNode", value: function selectNode() { this.isSelectedNode = true; this.update(this.node, this.decorations); } }, { key: "deselectNode", value: function deselectNode() { this.isSelectedNode = false; this.update(this.node, this.decorations); } }]); }(ReactNodeView);