You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContentState.js 5.8 KiB

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  13. var BlockMapBuilder = require("./BlockMapBuilder");
  14. var CharacterMetadata = require("./CharacterMetadata");
  15. var ContentBlock = require("./ContentBlock");
  16. var ContentBlockNode = require("./ContentBlockNode");
  17. var DraftEntity = require("./DraftEntity");
  18. var SelectionState = require("./SelectionState");
  19. var generateRandomKey = require("./generateRandomKey");
  20. var gkx = require("./gkx");
  21. var Immutable = require("immutable");
  22. var sanitizeDraftText = require("./sanitizeDraftText");
  23. var List = Immutable.List,
  24. Record = Immutable.Record,
  25. Repeat = Immutable.Repeat;
  26. var defaultRecord = {
  27. entityMap: null,
  28. blockMap: null,
  29. selectionBefore: null,
  30. selectionAfter: null
  31. };
  32. var ContentStateRecord = Record(defaultRecord);
  33. var ContentState =
  34. /*#__PURE__*/
  35. function (_ContentStateRecord) {
  36. _inheritsLoose(ContentState, _ContentStateRecord);
  37. function ContentState() {
  38. return _ContentStateRecord.apply(this, arguments) || this;
  39. }
  40. var _proto = ContentState.prototype;
  41. _proto.getEntityMap = function getEntityMap() {
  42. // TODO: update this when we fully remove DraftEntity
  43. return DraftEntity;
  44. };
  45. _proto.getBlockMap = function getBlockMap() {
  46. return this.get('blockMap');
  47. };
  48. _proto.getSelectionBefore = function getSelectionBefore() {
  49. return this.get('selectionBefore');
  50. };
  51. _proto.getSelectionAfter = function getSelectionAfter() {
  52. return this.get('selectionAfter');
  53. };
  54. _proto.getBlockForKey = function getBlockForKey(key) {
  55. var block = this.getBlockMap().get(key);
  56. return block;
  57. };
  58. _proto.getKeyBefore = function getKeyBefore(key) {
  59. return this.getBlockMap().reverse().keySeq().skipUntil(function (v) {
  60. return v === key;
  61. }).skip(1).first();
  62. };
  63. _proto.getKeyAfter = function getKeyAfter(key) {
  64. return this.getBlockMap().keySeq().skipUntil(function (v) {
  65. return v === key;
  66. }).skip(1).first();
  67. };
  68. _proto.getBlockAfter = function getBlockAfter(key) {
  69. return this.getBlockMap().skipUntil(function (_, k) {
  70. return k === key;
  71. }).skip(1).first();
  72. };
  73. _proto.getBlockBefore = function getBlockBefore(key) {
  74. return this.getBlockMap().reverse().skipUntil(function (_, k) {
  75. return k === key;
  76. }).skip(1).first();
  77. };
  78. _proto.getBlocksAsArray = function getBlocksAsArray() {
  79. return this.getBlockMap().toArray();
  80. };
  81. _proto.getFirstBlock = function getFirstBlock() {
  82. return this.getBlockMap().first();
  83. };
  84. _proto.getLastBlock = function getLastBlock() {
  85. return this.getBlockMap().last();
  86. };
  87. _proto.getPlainText = function getPlainText(delimiter) {
  88. return this.getBlockMap().map(function (block) {
  89. return block ? block.getText() : '';
  90. }).join(delimiter || '\n');
  91. };
  92. _proto.getLastCreatedEntityKey = function getLastCreatedEntityKey() {
  93. // TODO: update this when we fully remove DraftEntity
  94. return DraftEntity.__getLastCreatedEntityKey();
  95. };
  96. _proto.hasText = function hasText() {
  97. var blockMap = this.getBlockMap();
  98. return blockMap.size > 1 || // make sure that there are no zero width space chars
  99. escape(blockMap.first().getText()).replace(/%u200B/g, '').length > 0;
  100. };
  101. _proto.createEntity = function createEntity(type, mutability, data) {
  102. // TODO: update this when we fully remove DraftEntity
  103. DraftEntity.__create(type, mutability, data);
  104. return this;
  105. };
  106. _proto.mergeEntityData = function mergeEntityData(key, toMerge) {
  107. // TODO: update this when we fully remove DraftEntity
  108. DraftEntity.__mergeData(key, toMerge);
  109. return this;
  110. };
  111. _proto.replaceEntityData = function replaceEntityData(key, newData) {
  112. // TODO: update this when we fully remove DraftEntity
  113. DraftEntity.__replaceData(key, newData);
  114. return this;
  115. };
  116. _proto.addEntity = function addEntity(instance) {
  117. // TODO: update this when we fully remove DraftEntity
  118. DraftEntity.__add(instance);
  119. return this;
  120. };
  121. _proto.getEntity = function getEntity(key) {
  122. // TODO: update this when we fully remove DraftEntity
  123. return DraftEntity.__get(key);
  124. };
  125. ContentState.createFromBlockArray = function createFromBlockArray( // TODO: update flow type when we completely deprecate the old entity API
  126. blocks, entityMap) {
  127. // TODO: remove this when we completely deprecate the old entity API
  128. var theBlocks = Array.isArray(blocks) ? blocks : blocks.contentBlocks;
  129. var blockMap = BlockMapBuilder.createFromArray(theBlocks);
  130. var selectionState = blockMap.isEmpty() ? new SelectionState() : SelectionState.createEmpty(blockMap.first().getKey());
  131. return new ContentState({
  132. blockMap: blockMap,
  133. entityMap: entityMap || DraftEntity,
  134. selectionBefore: selectionState,
  135. selectionAfter: selectionState
  136. });
  137. };
  138. ContentState.createFromText = function createFromText(text) {
  139. var delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : /\r\n?|\n/g;
  140. var strings = text.split(delimiter);
  141. var blocks = strings.map(function (block) {
  142. block = sanitizeDraftText(block);
  143. var ContentBlockNodeRecord = gkx('draft_tree_data_support') ? ContentBlockNode : ContentBlock;
  144. return new ContentBlockNodeRecord({
  145. key: generateRandomKey(),
  146. text: block,
  147. type: 'unstyled',
  148. characterList: List(Repeat(CharacterMetadata.EMPTY, block.length))
  149. });
  150. });
  151. return ContentState.createFromBlockArray(blocks);
  152. };
  153. return ContentState;
  154. }(ContentStateRecord);
  155. module.exports = ContentState;