forked from Gitlink/forgeplus-react
206 lines
8.3 KiB
JavaScript
206 lines
8.3 KiB
JavaScript
|
|
/**
|
||
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
||
|
|
* All rights reserved.
|
||
|
|
*
|
||
|
|
* This source code is licensed under the BSD-style license found in the
|
||
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
|
*
|
||
|
|
* @providesModule DraftEditorBlock.react
|
||
|
|
* @format
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
var _assign = require('object-assign');
|
||
|
|
|
||
|
|
var _extends = _assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
||
|
|
|
||
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||
|
|
|
||
|
|
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
||
|
|
|
||
|
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
||
|
|
|
||
|
|
var DraftEditorLeaf = require('./DraftEditorLeaf.react');
|
||
|
|
var DraftOffsetKey = require('./DraftOffsetKey');
|
||
|
|
var React = require('react');
|
||
|
|
var ReactDOM = require('react-dom');
|
||
|
|
var Scroll = require('fbjs/lib/Scroll');
|
||
|
|
var Style = require('fbjs/lib/Style');
|
||
|
|
var UnicodeBidi = require('fbjs/lib/UnicodeBidi');
|
||
|
|
var UnicodeBidiDirection = require('fbjs/lib/UnicodeBidiDirection');
|
||
|
|
|
||
|
|
var cx = require('fbjs/lib/cx');
|
||
|
|
var getElementPosition = require('fbjs/lib/getElementPosition');
|
||
|
|
var getScrollPosition = require('fbjs/lib/getScrollPosition');
|
||
|
|
var getViewportDimensions = require('fbjs/lib/getViewportDimensions');
|
||
|
|
var invariant = require('fbjs/lib/invariant');
|
||
|
|
var nullthrows = require('fbjs/lib/nullthrows');
|
||
|
|
|
||
|
|
var SCROLL_BUFFER = 10;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Return whether a block overlaps with either edge of the `SelectionState`.
|
||
|
|
*/
|
||
|
|
var isBlockOnSelectionEdge = function isBlockOnSelectionEdge(selection, key) {
|
||
|
|
return selection.getAnchorKey() === key || selection.getFocusKey() === key;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The default block renderer for a `DraftEditor` component.
|
||
|
|
*
|
||
|
|
* A `DraftEditorBlock` is able to render a given `ContentBlock` to its
|
||
|
|
* appropriate decorator and inline style components.
|
||
|
|
*/
|
||
|
|
|
||
|
|
var DraftEditorBlock = function (_React$Component) {
|
||
|
|
_inherits(DraftEditorBlock, _React$Component);
|
||
|
|
|
||
|
|
function DraftEditorBlock() {
|
||
|
|
_classCallCheck(this, DraftEditorBlock);
|
||
|
|
|
||
|
|
return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
|
||
|
|
}
|
||
|
|
|
||
|
|
DraftEditorBlock.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps) {
|
||
|
|
return this.props.block !== nextProps.block || this.props.tree !== nextProps.tree || this.props.direction !== nextProps.direction || isBlockOnSelectionEdge(nextProps.selection, nextProps.block.getKey()) && nextProps.forceSelection;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* When a block is mounted and overlaps the selection state, we need to make
|
||
|
|
* sure that the cursor is visible to match native behavior. This may not
|
||
|
|
* be the case if the user has pressed `RETURN` or pasted some content, since
|
||
|
|
* programatically creating these new blocks and setting the DOM selection
|
||
|
|
* will miss out on the browser natively scrolling to that position.
|
||
|
|
*
|
||
|
|
* To replicate native behavior, if the block overlaps the selection state
|
||
|
|
* on mount, force the scroll position. Check the scroll state of the scroll
|
||
|
|
* parent, and adjust it to align the entire block to the bottom of the
|
||
|
|
* scroll parent.
|
||
|
|
*/
|
||
|
|
|
||
|
|
|
||
|
|
DraftEditorBlock.prototype.componentDidMount = function componentDidMount() {
|
||
|
|
var selection = this.props.selection;
|
||
|
|
var endKey = selection.getEndKey();
|
||
|
|
if (!selection.getHasFocus() || endKey !== this.props.block.getKey()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var blockNode = ReactDOM.findDOMNode(this);
|
||
|
|
var scrollParent = Style.getScrollParent(blockNode);
|
||
|
|
var scrollPosition = getScrollPosition(scrollParent);
|
||
|
|
var scrollDelta = void 0;
|
||
|
|
|
||
|
|
if (scrollParent === window) {
|
||
|
|
var nodePosition = getElementPosition(blockNode);
|
||
|
|
var nodeBottom = nodePosition.y + nodePosition.height;
|
||
|
|
var viewportHeight = getViewportDimensions().height;
|
||
|
|
scrollDelta = nodeBottom - viewportHeight;
|
||
|
|
if (scrollDelta > 0) {
|
||
|
|
window.scrollTo(scrollPosition.x, scrollPosition.y + scrollDelta + SCROLL_BUFFER);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
!(blockNode instanceof HTMLElement) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'blockNode is not an HTMLElement') : invariant(false) : void 0;
|
||
|
|
var blockBottom = blockNode.offsetHeight + blockNode.offsetTop;
|
||
|
|
var scrollBottom = scrollParent.offsetHeight + scrollPosition.y;
|
||
|
|
scrollDelta = blockBottom - scrollBottom;
|
||
|
|
if (scrollDelta > 0) {
|
||
|
|
Scroll.setTop(scrollParent, Scroll.getTop(scrollParent) + scrollDelta + SCROLL_BUFFER);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
DraftEditorBlock.prototype._renderChildren = function _renderChildren() {
|
||
|
|
var _this2 = this;
|
||
|
|
|
||
|
|
var block = this.props.block;
|
||
|
|
var blockKey = block.getKey();
|
||
|
|
var text = block.getText();
|
||
|
|
var lastLeafSet = this.props.tree.size - 1;
|
||
|
|
var hasSelection = isBlockOnSelectionEdge(this.props.selection, blockKey);
|
||
|
|
|
||
|
|
return this.props.tree.map(function (leafSet, ii) {
|
||
|
|
var leavesForLeafSet = leafSet.get('leaves');
|
||
|
|
var lastLeaf = leavesForLeafSet.size - 1;
|
||
|
|
var leaves = leavesForLeafSet.map(function (leaf, jj) {
|
||
|
|
var offsetKey = DraftOffsetKey.encode(blockKey, ii, jj);
|
||
|
|
var start = leaf.get('start');
|
||
|
|
var end = leaf.get('end');
|
||
|
|
return React.createElement(DraftEditorLeaf, {
|
||
|
|
key: offsetKey,
|
||
|
|
offsetKey: offsetKey,
|
||
|
|
block: block,
|
||
|
|
start: start,
|
||
|
|
selection: hasSelection ? _this2.props.selection : null,
|
||
|
|
forceSelection: _this2.props.forceSelection,
|
||
|
|
text: text.slice(start, end),
|
||
|
|
styleSet: block.getInlineStyleAt(start),
|
||
|
|
customStyleMap: _this2.props.customStyleMap,
|
||
|
|
customStyleFn: _this2.props.customStyleFn,
|
||
|
|
isLast: ii === lastLeafSet && jj === lastLeaf
|
||
|
|
});
|
||
|
|
}).toArray();
|
||
|
|
|
||
|
|
var decoratorKey = leafSet.get('decoratorKey');
|
||
|
|
if (decoratorKey == null) {
|
||
|
|
return leaves;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!_this2.props.decorator) {
|
||
|
|
return leaves;
|
||
|
|
}
|
||
|
|
|
||
|
|
var decorator = nullthrows(_this2.props.decorator);
|
||
|
|
|
||
|
|
var DecoratorComponent = decorator.getComponentForKey(decoratorKey);
|
||
|
|
if (!DecoratorComponent) {
|
||
|
|
return leaves;
|
||
|
|
}
|
||
|
|
|
||
|
|
var decoratorProps = decorator.getPropsForKey(decoratorKey);
|
||
|
|
var decoratorOffsetKey = DraftOffsetKey.encode(blockKey, ii, 0);
|
||
|
|
var decoratedText = text.slice(leavesForLeafSet.first().get('start'), leavesForLeafSet.last().get('end'));
|
||
|
|
|
||
|
|
// Resetting dir to the same value on a child node makes Chrome/Firefox
|
||
|
|
// confused on cursor movement. See http://jsfiddle.net/d157kLck/3/
|
||
|
|
var dir = UnicodeBidiDirection.getHTMLDirIfDifferent(UnicodeBidi.getDirection(decoratedText), _this2.props.direction);
|
||
|
|
|
||
|
|
return React.createElement(
|
||
|
|
DecoratorComponent,
|
||
|
|
_extends({}, decoratorProps, {
|
||
|
|
contentState: _this2.props.contentState,
|
||
|
|
decoratedText: decoratedText,
|
||
|
|
dir: dir,
|
||
|
|
key: decoratorOffsetKey,
|
||
|
|
entityKey: block.getEntityAt(leafSet.get('start')),
|
||
|
|
offsetKey: decoratorOffsetKey }),
|
||
|
|
leaves
|
||
|
|
);
|
||
|
|
}).toArray();
|
||
|
|
};
|
||
|
|
|
||
|
|
DraftEditorBlock.prototype.render = function render() {
|
||
|
|
var _props = this.props,
|
||
|
|
direction = _props.direction,
|
||
|
|
offsetKey = _props.offsetKey;
|
||
|
|
|
||
|
|
var className = cx({
|
||
|
|
'public/DraftStyleDefault/block': true,
|
||
|
|
'public/DraftStyleDefault/ltr': direction === 'LTR',
|
||
|
|
'public/DraftStyleDefault/rtl': direction === 'RTL'
|
||
|
|
});
|
||
|
|
|
||
|
|
return React.createElement(
|
||
|
|
'div',
|
||
|
|
{ 'data-offset-key': offsetKey, className: className },
|
||
|
|
this._renderChildren()
|
||
|
|
);
|
||
|
|
};
|
||
|
|
|
||
|
|
return DraftEditorBlock;
|
||
|
|
}(React.Component);
|
||
|
|
|
||
|
|
module.exports = DraftEditorBlock;
|