Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

146 строки
4.5 KiB

  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. *
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  13. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  14. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  15. var React = require("react");
  16. var UserAgent = require("fbjs/lib/UserAgent");
  17. var invariant = require("fbjs/lib/invariant");
  18. var isElement = require("./isElement"); // In IE, spans with <br> tags render as two newlines. By rendering a span
  19. // with only a newline character, we can be sure to render a single line.
  20. var useNewlineChar = UserAgent.isBrowser('IE <= 11');
  21. /**
  22. * Check whether the node should be considered a newline.
  23. */
  24. function isNewline(node) {
  25. return useNewlineChar ? node.textContent === '\n' : node.tagName === 'BR';
  26. }
  27. /**
  28. * Placeholder elements for empty text content.
  29. *
  30. * What is this `data-text` attribute, anyway? It turns out that we need to
  31. * put an attribute on the lowest-level text node in order to preserve correct
  32. * spellcheck handling. If the <span> is naked, Chrome and Safari may do
  33. * bizarre things to do the DOM -- split text nodes, create extra spans, etc.
  34. * If the <span> has an attribute, this appears not to happen.
  35. * See http://jsfiddle.net/9khdavod/ for the failure case, and
  36. * http://jsfiddle.net/7pg143f7/ for the fixed case.
  37. */
  38. var NEWLINE_A = function NEWLINE_A(ref) {
  39. return useNewlineChar ? React.createElement("span", {
  40. key: "A",
  41. "data-text": "true",
  42. ref: ref
  43. }, '\n') : React.createElement("br", {
  44. key: "A",
  45. "data-text": "true",
  46. ref: ref
  47. });
  48. };
  49. var NEWLINE_B = function NEWLINE_B(ref) {
  50. return useNewlineChar ? React.createElement("span", {
  51. key: "B",
  52. "data-text": "true",
  53. ref: ref
  54. }, '\n') : React.createElement("br", {
  55. key: "B",
  56. "data-text": "true",
  57. ref: ref
  58. });
  59. };
  60. /**
  61. * The lowest-level component in a `DraftEditor`, the text node component
  62. * replaces the default React text node implementation. This allows us to
  63. * perform custom handling of newline behavior and avoid re-rendering text
  64. * nodes with DOM state that already matches the expectations of our immutable
  65. * editor state.
  66. */
  67. var DraftEditorTextNode =
  68. /*#__PURE__*/
  69. function (_React$Component) {
  70. _inheritsLoose(DraftEditorTextNode, _React$Component);
  71. function DraftEditorTextNode(props) {
  72. var _this;
  73. _this = _React$Component.call(this, props) || this; // By flipping this flag, we also keep flipping keys which forces
  74. // React to remount this node every time it rerenders.
  75. _defineProperty(_assertThisInitialized(_this), "_forceFlag", void 0);
  76. _defineProperty(_assertThisInitialized(_this), "_node", void 0);
  77. _this._forceFlag = false;
  78. return _this;
  79. }
  80. var _proto = DraftEditorTextNode.prototype;
  81. _proto.shouldComponentUpdate = function shouldComponentUpdate(nextProps) {
  82. var node = this._node;
  83. var shouldBeNewline = nextProps.children === '';
  84. !isElement(node) ? process.env.NODE_ENV !== "production" ? invariant(false, 'node is not an Element') : invariant(false) : void 0;
  85. var elementNode = node;
  86. if (shouldBeNewline) {
  87. return !isNewline(elementNode);
  88. }
  89. return elementNode.textContent !== nextProps.children;
  90. };
  91. _proto.componentDidMount = function componentDidMount() {
  92. this._forceFlag = !this._forceFlag;
  93. };
  94. _proto.componentDidUpdate = function componentDidUpdate() {
  95. this._forceFlag = !this._forceFlag;
  96. };
  97. _proto.render = function render() {
  98. var _this2 = this;
  99. if (this.props.children === '') {
  100. return this._forceFlag ? NEWLINE_A(function (ref) {
  101. return _this2._node = ref;
  102. }) : NEWLINE_B(function (ref) {
  103. return _this2._node = ref;
  104. });
  105. }
  106. return React.createElement("span", {
  107. key: this._forceFlag ? 'A' : 'B',
  108. "data-text": "true",
  109. ref: function ref(_ref) {
  110. return _this2._node = _ref;
  111. }
  112. }, this.props.children);
  113. };
  114. return DraftEditorTextNode;
  115. }(React.Component);
  116. module.exports = DraftEditorTextNode;