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.
 
 
 
 

102 lines
2.9 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 _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  13. var _require = require("immutable"),
  14. Map = _require.Map,
  15. OrderedSet = _require.OrderedSet,
  16. Record = _require.Record; // Immutable.map is typed such that the value for every key in the map
  17. // must be the same type
  18. var EMPTY_SET = OrderedSet();
  19. var defaultRecord = {
  20. style: EMPTY_SET,
  21. entity: null
  22. };
  23. var CharacterMetadataRecord = Record(defaultRecord);
  24. var CharacterMetadata =
  25. /*#__PURE__*/
  26. function (_CharacterMetadataRec) {
  27. _inheritsLoose(CharacterMetadata, _CharacterMetadataRec);
  28. function CharacterMetadata() {
  29. return _CharacterMetadataRec.apply(this, arguments) || this;
  30. }
  31. var _proto = CharacterMetadata.prototype;
  32. _proto.getStyle = function getStyle() {
  33. return this.get('style');
  34. };
  35. _proto.getEntity = function getEntity() {
  36. return this.get('entity');
  37. };
  38. _proto.hasStyle = function hasStyle(style) {
  39. return this.getStyle().includes(style);
  40. };
  41. CharacterMetadata.applyStyle = function applyStyle(record, style) {
  42. var withStyle = record.set('style', record.getStyle().add(style));
  43. return CharacterMetadata.create(withStyle);
  44. };
  45. CharacterMetadata.removeStyle = function removeStyle(record, style) {
  46. var withoutStyle = record.set('style', record.getStyle().remove(style));
  47. return CharacterMetadata.create(withoutStyle);
  48. };
  49. CharacterMetadata.applyEntity = function applyEntity(record, entityKey) {
  50. var withEntity = record.getEntity() === entityKey ? record : record.set('entity', entityKey);
  51. return CharacterMetadata.create(withEntity);
  52. }
  53. /**
  54. * Use this function instead of the `CharacterMetadata` constructor.
  55. * Since most content generally uses only a very small number of
  56. * style/entity permutations, we can reuse these objects as often as
  57. * possible.
  58. */
  59. ;
  60. CharacterMetadata.create = function create(config) {
  61. if (!config) {
  62. return EMPTY;
  63. }
  64. var defaultConfig = {
  65. style: EMPTY_SET,
  66. entity: null
  67. }; // Fill in unspecified properties, if necessary.
  68. var configMap = Map(defaultConfig).merge(config);
  69. var existing = pool.get(configMap);
  70. if (existing) {
  71. return existing;
  72. }
  73. var newCharacter = new CharacterMetadata(configMap);
  74. pool = pool.set(configMap, newCharacter);
  75. return newCharacter;
  76. };
  77. return CharacterMetadata;
  78. }(CharacterMetadataRecord);
  79. var EMPTY = new CharacterMetadata();
  80. var pool = Map([[Map(defaultRecord), EMPTY]]);
  81. CharacterMetadata.EMPTY = EMPTY;
  82. module.exports = CharacterMetadata;