forked from SamyPesse/draft-js-multidecorators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (66 loc) · 1.86 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
73
74
75
76
77
78
79
80
81
82
83
84
85
var Immutable = require('immutable');
var KEY_SEPARATOR = '-';
function MultiDecorator(decorators) {
this.decorators = Immutable.List(decorators);
}
/**
Return list of decoration IDs per character
@param {ContentBlock}
@return {List<String>}
*/
MultiDecorator.prototype.getDecorations = function(block, contentState) {
var decorations = Array(block.getText().length).fill(null);
this.decorators.forEach(function(decorator, i) {
var _decorations = decorator.getDecorations(block, contentState);
_decorations.forEach(function(key, offset) {
if (!key) {
return;
}
key = i + KEY_SEPARATOR + key;
decorations[offset] = key;
});
});
return Immutable.List(decorations);
};
/**
Return component to render a decoration
@param {String}
@return {Function}
*/
MultiDecorator.prototype.getComponentForKey = function(key) {
var decorator = this.getDecoratorForKey(key);
return decorator.getComponentForKey(
this.getInnerKey(key)
);
};
/**
Return props to render a decoration
@param {String}
@return {Object}
*/
MultiDecorator.prototype.getPropsForKey = function(key) {
var decorator = this.getDecoratorForKey(key);
return decorator.getPropsForKey(
this.getInnerKey(key)
);
};
/**
Return a decorator for a specific key
@param {String}
@return {Decorator}
*/
MultiDecorator.prototype.getDecoratorForKey = function(key) {
var parts = key.split(KEY_SEPARATOR);
var index = Number(parts[0]);
return this.decorators.get(index);
};
/**
Return inner key for a decorator
@param {String}
@return {String}
*/
MultiDecorator.prototype.getInnerKey = function(key) {
var parts = key.split(KEY_SEPARATOR);
return parts.slice(1).join(KEY_SEPARATOR);
};
module.exports = MultiDecorator;