139 lines
4.2 KiB
JavaScript
139 lines
4.2 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 ContentBlock
|
|
* @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 CharacterMetadata = require('./CharacterMetadata');
|
|
var Immutable = require('immutable');
|
|
|
|
var findRangesImmutable = require('./findRangesImmutable');
|
|
|
|
var List = Immutable.List,
|
|
Map = Immutable.Map,
|
|
OrderedSet = Immutable.OrderedSet,
|
|
Record = Immutable.Record,
|
|
Repeat = Immutable.Repeat;
|
|
|
|
|
|
var EMPTY_SET = OrderedSet();
|
|
|
|
var defaultRecord = {
|
|
key: '',
|
|
type: 'unstyled',
|
|
text: '',
|
|
characterList: List(),
|
|
depth: 0,
|
|
data: Map()
|
|
};
|
|
|
|
var ContentBlockRecord = Record(defaultRecord);
|
|
|
|
var decorateCharacterList = function decorateCharacterList(config) {
|
|
if (!config) {
|
|
return config;
|
|
}
|
|
|
|
var characterList = config.characterList,
|
|
text = config.text;
|
|
|
|
|
|
if (text && !characterList) {
|
|
config.characterList = List(Repeat(CharacterMetadata.EMPTY, text.length));
|
|
}
|
|
|
|
return config;
|
|
};
|
|
|
|
var ContentBlock = function (_ContentBlockRecord) {
|
|
_inherits(ContentBlock, _ContentBlockRecord);
|
|
|
|
function ContentBlock(config) {
|
|
_classCallCheck(this, ContentBlock);
|
|
|
|
return _possibleConstructorReturn(this, _ContentBlockRecord.call(this, decorateCharacterList(config)));
|
|
}
|
|
|
|
ContentBlock.prototype.getKey = function getKey() {
|
|
return this.get('key');
|
|
};
|
|
|
|
ContentBlock.prototype.getType = function getType() {
|
|
return this.get('type');
|
|
};
|
|
|
|
ContentBlock.prototype.getText = function getText() {
|
|
return this.get('text');
|
|
};
|
|
|
|
ContentBlock.prototype.getCharacterList = function getCharacterList() {
|
|
return this.get('characterList');
|
|
};
|
|
|
|
ContentBlock.prototype.getLength = function getLength() {
|
|
return this.getText().length;
|
|
};
|
|
|
|
ContentBlock.prototype.getDepth = function getDepth() {
|
|
return this.get('depth');
|
|
};
|
|
|
|
ContentBlock.prototype.getData = function getData() {
|
|
return this.get('data');
|
|
};
|
|
|
|
ContentBlock.prototype.getInlineStyleAt = function getInlineStyleAt(offset) {
|
|
var character = this.getCharacterList().get(offset);
|
|
return character ? character.getStyle() : EMPTY_SET;
|
|
};
|
|
|
|
ContentBlock.prototype.getEntityAt = function getEntityAt(offset) {
|
|
var character = this.getCharacterList().get(offset);
|
|
return character ? character.getEntity() : null;
|
|
};
|
|
|
|
/**
|
|
* Execute a callback for every contiguous range of styles within the block.
|
|
*/
|
|
|
|
|
|
ContentBlock.prototype.findStyleRanges = function findStyleRanges(filterFn, callback) {
|
|
findRangesImmutable(this.getCharacterList(), haveEqualStyle, filterFn, callback);
|
|
};
|
|
|
|
/**
|
|
* Execute a callback for every contiguous range of entities within the block.
|
|
*/
|
|
|
|
|
|
ContentBlock.prototype.findEntityRanges = function findEntityRanges(filterFn, callback) {
|
|
findRangesImmutable(this.getCharacterList(), haveEqualEntity, filterFn, callback);
|
|
};
|
|
|
|
return ContentBlock;
|
|
}(ContentBlockRecord);
|
|
|
|
function haveEqualStyle(charA, charB) {
|
|
return charA.getStyle() === charB.getStyle();
|
|
}
|
|
|
|
function haveEqualEntity(charA, charB) {
|
|
return charA.getEntity() === charB.getEntity();
|
|
}
|
|
|
|
module.exports = ContentBlock; |