/** * 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 ContentBlockNode * @format * * * This file is a fork of ContentBlock adding support for nesting references by * providing links to children, parent, prevSibling, and nextSibling. * * This is unstable and not part of the public API and should not be used by * production systems. This file may be update/removed without notice. */ '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 = { parent: null, characterList: List(), data: Map(), depth: 0, key: '', text: '', type: 'unstyled', children: List(), prevSibling: null, nextSibling: null }; var haveEqualStyle = function haveEqualStyle(charA, charB) { return charA.getStyle() === charB.getStyle(); }; var haveEqualEntity = function haveEqualEntity(charA, charB) { return charA.getEntity() === charB.getEntity(); }; 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 ContentBlockNode = function (_Record) { _inherits(ContentBlockNode, _Record); function ContentBlockNode() { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultRecord; _classCallCheck(this, ContentBlockNode); return _possibleConstructorReturn(this, _Record.call(this, decorateCharacterList(props))); } ContentBlockNode.prototype.getKey = function getKey() { return this.get('key'); }; ContentBlockNode.prototype.getType = function getType() { return this.get('type'); }; ContentBlockNode.prototype.getText = function getText() { return this.get('text'); }; ContentBlockNode.prototype.getCharacterList = function getCharacterList() { return this.get('characterList'); }; ContentBlockNode.prototype.getLength = function getLength() { return this.getText().length; }; ContentBlockNode.prototype.getDepth = function getDepth() { return this.get('depth'); }; ContentBlockNode.prototype.getData = function getData() { return this.get('data'); }; ContentBlockNode.prototype.getInlineStyleAt = function getInlineStyleAt(offset) { var character = this.getCharacterList().get(offset); return character ? character.getStyle() : EMPTY_SET; }; ContentBlockNode.prototype.getEntityAt = function getEntityAt(offset) { var character = this.getCharacterList().get(offset); return character ? character.getEntity() : null; }; ContentBlockNode.prototype.getChildKeys = function getChildKeys() { return this.get('children'); }; ContentBlockNode.prototype.getParentKey = function getParentKey() { return this.get('parent'); }; ContentBlockNode.prototype.getPrevSiblingKey = function getPrevSiblingKey() { return this.get('prevSibling'); }; ContentBlockNode.prototype.getNextSiblingKey = function getNextSiblingKey() { return this.get('nextSibling'); }; ContentBlockNode.prototype.findStyleRanges = function findStyleRanges(filterFn, callback) { findRangesImmutable(this.getCharacterList(), haveEqualStyle, filterFn, callback); }; ContentBlockNode.prototype.findEntityRanges = function findEntityRanges(filterFn, callback) { findRangesImmutable(this.getCharacterList(), haveEqualEntity, filterFn, callback); }; return ContentBlockNode; }(Record(defaultRecord)); module.exports = ContentBlockNode;