-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
72 lines (62 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var Immutable = require('immutable');
var KEY_SEPARATOR = '-';
/**
* Creates a Draft decorator
* @param {Function} strategy function (contentBlock, callback(start, end, props))
* @param {Function} getComponent function (props) -> React.Component
*/
function SimpleDecorator(strategy, getComponent) {
this.decorated = {};
this.strategy = strategy;
this.getComponent = getComponent;
}
/**
* Return list of decoration IDs per character
* @param {ContentBlock} block
* @return {List<String>}
*/
SimpleDecorator.prototype.getDecorations = function(block, contentState) {
var decorations = Array(block.getText().length).fill(null);
// Apply a decoration to given range, with given props
function callback (start, end, props) {
if (props === undefined) {
props = {};
}
key = blockKey + KEY_SEPARATOR + decorationId;
decorated[blockKey][decorationId] = props;
decorateRange(decorations, start, end, key);
decorationId++;
}
var blockKey = block.getKey();
var key;
var decorationId = 0;
var decorated = this.decorated;
decorated[blockKey] = {};
this.strategy(block, callback, contentState);
return Immutable.List(decorations);
};
/**
* Return component to render a decoration
* @param {String} key
* @return {Function}
*/
SimpleDecorator.prototype.getComponentForKey = function(key) {
return this.getComponent;
};
/**
* Return props to render a decoration
* @param {String} key
* @return {Object}
*/
SimpleDecorator.prototype.getPropsForKey = function(key) {
var parts = key.split(KEY_SEPARATOR);
var blockKey = parts[0];
var decorationId = parts[1];
return this.decorated[blockKey][decorationId];
};
function decorateRange(decorationsArray, start, end, key) {
for (var ii = start; ii < end; ii++) {
decorationsArray[ii] = key;
}
}
module.exports = SimpleDecorator;