UNPKG

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