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

Add contentState argument to decorator #601

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/model/decorators/CompositeDraftDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
var Immutable = require('immutable');

import type ContentBlock from 'ContentBlock';
import type ContentState from 'ContentState';
import type {DraftDecorator} from 'DraftDecorator';

var {List} = Immutable;
Expand Down Expand Up @@ -51,7 +52,7 @@ class CompositeDraftDecorator {
this._decorators = decorators.slice();
}

getDecorations(block: ContentBlock): List<?string> {
getDecorations(block: ContentBlock, newContent: ContentState): List<?string> {
var decorations = Array(block.getText().length).fill(null);

this._decorators.forEach(
Expand All @@ -66,7 +67,7 @@ class CompositeDraftDecorator {
occupySlice(decorations, start, end, ii + DELIMITER + counter);
counter++;
}
});
}, newContent);
}
);

Expand Down
3 changes: 2 additions & 1 deletion src/model/decorators/DraftDecoratorType.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'use strict';

import type ContentBlock from 'ContentBlock';
import type ContentState from 'ContentState';
import type {List} from 'immutable';

/**
Expand All @@ -25,7 +26,7 @@ export type DraftDecoratorType = {
/**
* Given a `ContentBlock`, return an immutable List of decorator keys.
*/
getDecorations(block: ContentBlock): List<?string>,
getDecorations(block: ContentBlock, newContent: ContentState): List<?string>,

/**
* Given a decorator key, return the component to use when rendering
Expand Down
6 changes: 4 additions & 2 deletions src/model/immutable/BlockTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var findRangesImmutable = require('findRangesImmutable');

import type CharacterMetadata from 'CharacterMetadata';
import type ContentBlock from 'ContentBlock';
import type ContentState from 'ContentState';
import type {DraftDecoratorType} from 'DraftDecoratorType';

var {
Expand Down Expand Up @@ -61,7 +62,8 @@ var BlockTree = {
*/
generate: function(
block: ContentBlock,
decorator: ?DraftDecoratorType
decorator: ?DraftDecoratorType,
newContent: ContentState
): List<DecoratorRange> {
var textLength = block.getLength();
if (!textLength) {
Expand All @@ -79,7 +81,7 @@ var BlockTree = {

var leafSets = [];
var decorations = decorator ?
decorator.getDecorations(block) :
decorator.getDecorations(block, newContent) :
List(Repeat(null, textLength));

var chars = block.getCharacterList();
Expand Down
16 changes: 9 additions & 7 deletions src/model/immutable/EditorState.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ class EditorState {
newContent.getBlockMap(),
treeMap,
decorator,
existingDecorator
existingDecorator,
newContent
);
} else {
newTreeMap = generateNewTreeMap(newContent, decorator);
Expand Down Expand Up @@ -515,7 +516,7 @@ function generateNewTreeMap(
): OrderedMap<string, List<any>> {
return contentState
.getBlockMap()
.map(block => BlockTree.generate(block, decorator))
.map(block => BlockTree.generate(block, decorator, contentState))
.toOrderedMap();
}

Expand All @@ -535,7 +536,7 @@ function regenerateTreeForNewBlocks(
newBlockMap
.toSeq()
.filter((block, key) => block !== prevBlockMap.get(key))
.map(block => BlockTree.generate(block, decorator))
.map(block => BlockTree.generate(block, decorator, editorState.getCurrentContent()))
);
}

Expand All @@ -551,18 +552,19 @@ function regenerateTreeForNewDecorator(
blockMap: BlockMap,
previousTreeMap: OrderedMap<string, List<any>>,
decorator: DraftDecoratorType,
existingDecorator: DraftDecoratorType
existingDecorator: DraftDecoratorType,
newContent: ContentState
): OrderedMap<string, List<any>> {
return previousTreeMap.merge(
blockMap
.toSeq()
.filter(block => {
return (
decorator.getDecorations(block) !==
existingDecorator.getDecorations(block)
decorator.getDecorations(block, newContent) !==
existingDecorator.getDecorations(block, newContent)
);
})
.map(block => BlockTree.generate(block, decorator))
.map(block => BlockTree.generate(block, decorator, newContent))
);
}

Expand Down