115 lines
3.8 KiB
JavaScript
115 lines
3.8 KiB
JavaScript
/**
|
|
* 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 CharacterMetadata
|
|
* @format
|
|
*
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
|
|
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 _require = require('immutable'),
|
|
Map = _require.Map,
|
|
OrderedSet = _require.OrderedSet,
|
|
Record = _require.Record;
|
|
|
|
// Immutable.map is typed such that the value for every key in the map
|
|
// must be the same type
|
|
|
|
|
|
var EMPTY_SET = OrderedSet();
|
|
|
|
var defaultRecord = {
|
|
style: EMPTY_SET,
|
|
entity: null
|
|
};
|
|
|
|
var CharacterMetadataRecord = Record(defaultRecord);
|
|
|
|
var CharacterMetadata = function (_CharacterMetadataRec) {
|
|
_inherits(CharacterMetadata, _CharacterMetadataRec);
|
|
|
|
function CharacterMetadata() {
|
|
_classCallCheck(this, CharacterMetadata);
|
|
|
|
return _possibleConstructorReturn(this, _CharacterMetadataRec.apply(this, arguments));
|
|
}
|
|
|
|
CharacterMetadata.prototype.getStyle = function getStyle() {
|
|
return this.get('style');
|
|
};
|
|
|
|
CharacterMetadata.prototype.getEntity = function getEntity() {
|
|
return this.get('entity');
|
|
};
|
|
|
|
CharacterMetadata.prototype.hasStyle = function hasStyle(style) {
|
|
return this.getStyle().includes(style);
|
|
};
|
|
|
|
CharacterMetadata.applyStyle = function applyStyle(record, style) {
|
|
var withStyle = record.set('style', record.getStyle().add(style));
|
|
return CharacterMetadata.create(withStyle);
|
|
};
|
|
|
|
CharacterMetadata.removeStyle = function removeStyle(record, style) {
|
|
var withoutStyle = record.set('style', record.getStyle().remove(style));
|
|
return CharacterMetadata.create(withoutStyle);
|
|
};
|
|
|
|
CharacterMetadata.applyEntity = function applyEntity(record, entityKey) {
|
|
var withEntity = record.getEntity() === entityKey ? record : record.set('entity', entityKey);
|
|
return CharacterMetadata.create(withEntity);
|
|
};
|
|
|
|
/**
|
|
* Use this function instead of the `CharacterMetadata` constructor.
|
|
* Since most content generally uses only a very small number of
|
|
* style/entity permutations, we can reuse these objects as often as
|
|
* possible.
|
|
*/
|
|
|
|
|
|
CharacterMetadata.create = function create(config) {
|
|
if (!config) {
|
|
return EMPTY;
|
|
}
|
|
|
|
var defaultConfig = {
|
|
style: EMPTY_SET,
|
|
entity: null
|
|
};
|
|
|
|
// Fill in unspecified properties, if necessary.
|
|
var configMap = Map(defaultConfig).merge(config);
|
|
|
|
var existing = pool.get(configMap);
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
|
|
var newCharacter = new CharacterMetadata(configMap);
|
|
pool = pool.set(configMap, newCharacter);
|
|
return newCharacter;
|
|
};
|
|
|
|
return CharacterMetadata;
|
|
}(CharacterMetadataRecord);
|
|
|
|
var EMPTY = new CharacterMetadata();
|
|
var pool = Map([[Map(defaultRecord), EMPTY]]);
|
|
|
|
CharacterMetadata.EMPTY = EMPTY;
|
|
|
|
module.exports = CharacterMetadata; |