Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
Extract inline styles from contentBlock
Browse files Browse the repository at this point in the history
Summary:Addresses #153 by extracting the style key strings from the content block.
Closes #217

Reviewed By: ezequiel

Differential Revision: D3060659

fb-gh-sync-id: 7752f4f6477e61d239c9b945857fd1acca77ff42
shipit-source-id: 7752f4f6477e61d239c9b945857fd1acca77ff42
  • Loading branch information
Patrick Dillon authored and Facebook Github Bot 8 committed Mar 17, 2016
1 parent 31fadb2 commit 0c1ec86
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/model/encoding/__tests__/encodeInlineStyleRanges-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ var {
NONE,
} = SampleDraftInlineStyle;

var {
const {
List,
OrderedSet,
Repeat,
} = Immutable;

const FOO = OrderedSet.of('foo');
const FOO_BAR = OrderedSet.of('foo', 'bar');

describe('encodeInlineStyleRanges', () => {
function createBlock(text, inlineStyles) {
return new ContentBlock({
Expand Down Expand Up @@ -91,6 +95,17 @@ describe('encodeInlineStyleRanges', () => {
]);
});

it('must encode custom styles', () => {
const custom = List([FOO, FOO, FOO_BAR, FOO_BAR, BOLD, BOLD]);
expect(
encodeInlineStyleRanges(createBlock(' '.repeat(6), custom))
).toEqual([
{offset: 0, length: 4, style: 'foo'},
{offset: 2, length: 2, style: 'bar'},
{offset: 4, length: 2, style: 'BOLD'},
]);
});

it('must encode for a complex styled document', () => {
var complex = List([
BOLD, BOLD, BOLD, BOLD, NONE, // "four "
Expand Down
7 changes: 4 additions & 3 deletions src/model/encoding/encodeInlineStyleRanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ function encodeInlineStyleRanges(
block: ContentBlock
): Array<InlineStyleRange> {
var styleList = block.getCharacterList().map(c => c.getStyle()).toList();
var styles = Object.keys(DefaultDraftInlineStyle);
var ranges: Array<Array<InlineStyleRange>> = styles
var ranges = styleList
.flatten()
.toSet()
.map(style => getEncodedInlinesForType(block, styleList, style));

return Array.prototype.concat.apply(EMPTY_ARRAY, ranges);
return Array.prototype.concat.apply(EMPTY_ARRAY, ranges.toJS());
}

module.exports = encodeInlineStyleRanges;

0 comments on commit 0c1ec86

Please sign in to comment.