UNPKG

d2-ui

Version:
210 lines (178 loc) 9.5 kB
/** * 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 * @typechecks * */ 'use strict'; var _extends = Object.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; }; var _createClass = (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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } 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 ContentBlock = require('./ContentBlock'); 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 SelectionState = require('./SelectionState'); 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 nullthrows = require('fbjs/lib/nullthrows'); var SCROLL_BUFFER = 10; /** * 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); _get(Object.getPrototypeOf(DraftEditorBlock.prototype), 'constructor', this).apply(this, arguments); } /** * Return whether a block overlaps with either edge of the `SelectionState`. */ _createClass(DraftEditorBlock, [{ key: 'shouldComponentUpdate', value: 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. */ }, { key: 'componentDidMount', value: 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; 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 { 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); } } } }, { key: '_renderChildren', value: function _renderChildren() { var _this = 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, blockKey: blockKey, start: start, selection: hasSelection ? _this.props.selection : undefined, forceSelection: _this.props.forceSelection, text: text.slice(start, end), styleSet: block.getInlineStyleAt(start), customStyleMap: _this.props.customStyleMap, isLast: ii === lastLeafSet && jj === lastLeaf }); }).toArray(); var decoratorKey = leafSet.get('decoratorKey'); if (decoratorKey == null) { return leaves; } if (!_this.props.decorator) { return leaves; } var decorator = nullthrows(_this.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), _this.props.direction); return React.createElement( DecoratorComponent, _extends({}, decoratorProps, { decoratedText: decoratedText, dir: dir, key: decoratorOffsetKey, entityKey: block.getEntityAt(leafSet.get('start')), offsetKey: decoratorOffsetKey }), leaves ); }).toArray(); } }, { key: 'render', value: function render() { var _props = this.props; var direction = _props.direction; var 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); function isBlockOnSelectionEdge(selection, key) { return selection.getAnchorKey() === key || selection.getFocusKey() === key; } module.exports = DraftEditorBlock;