|
| 1 | +--- |
| 2 | +id: api-reference-character-metadata |
| 3 | +title: CharacterMetadata |
| 4 | +layout: docs |
| 5 | +category: API Reference |
| 6 | +next: api-reference-entity |
| 7 | +permalink: docs/api-reference-character-metadata.html |
| 8 | +--- |
| 9 | + |
| 10 | +`CharacterMetadata` is an Immutable |
| 11 | +[Record](http://facebook.github.io/immutable-js/docs/#/Record/Record) that |
| 12 | +represents inline style and entity information for a single character. |
| 13 | + |
| 14 | +`CharacterMetadata` objects are aggressively pooled and shared. If two characters |
| 15 | +have the same inline style and entity, they are represented with the same |
| 16 | +`CharacterMetadata` object. We therefore need only as many objects as combinations |
| 17 | +of utilized inline style sets with entity keys, keeping our memory footprint |
| 18 | +small even as the contents grow in size and complexity. |
| 19 | + |
| 20 | +To that end, you should create or apply changes to `CharacterMetadata` objects |
| 21 | +via the provided set of static methods, which will ensure that pooling is utilized. |
| 22 | + |
| 23 | +Most Draft use cases are unlikely to use these static methods, since most common edit |
| 24 | +operations are already implemented and available via utility modules. The getter |
| 25 | +methods, however, may come in handy at render time. |
| 26 | + |
| 27 | +See the API reference on |
| 28 | +[ContentBlock](/draft-js/docs/api-reference-content-block.html#representing-styles-and-entities) |
| 29 | +for information on how `CharacterMetadata` is used within `ContentBlock`. |
| 30 | + |
| 31 | +## Overview |
| 32 | + |
| 33 | +*Static Methods* |
| 34 | + |
| 35 | +<ul class="apiIndex"> |
| 36 | + <li> |
| 37 | + <a href="#create"> |
| 38 | + <pre>static create(...): CharacterMetadata</pre> |
| 39 | + </a> |
| 40 | + </li> |
| 41 | + <li> |
| 42 | + <a href="#applystyle"> |
| 43 | + <pre>static applyStyle(...): CharacterMetadata</pre> |
| 44 | + </a> |
| 45 | + </li> |
| 46 | + <li> |
| 47 | + <a href="#removestyle"> |
| 48 | + <pre>static removeStyle(...): CharacterMetadata</pre> |
| 49 | + </a> |
| 50 | + </li> |
| 51 | + <li> |
| 52 | + <a href="#applyentity"> |
| 53 | + <pre>static applyEntity(...): CharacterMetadata</pre> |
| 54 | + </a> |
| 55 | + </li> |
| 56 | +</ul> |
| 57 | + |
| 58 | +*Methods* |
| 59 | + |
| 60 | +<ul class="apiIndex"> |
| 61 | + <li> |
| 62 | + <a href="#getstyle"> |
| 63 | + <pre>getStyle(): DraftInlineStyle</pre> |
| 64 | + </a> |
| 65 | + </li> |
| 66 | + <li> |
| 67 | + <a href="#hasstyle"> |
| 68 | + <pre>hasStyle(style: string): boolean</pre> |
| 69 | + </a> |
| 70 | + </li> |
| 71 | + <li> |
| 72 | + <a href="#getentity"> |
| 73 | + <pre>getEntity(): ?string</pre> |
| 74 | + </a> |
| 75 | + </li> |
| 76 | +</ul> |
| 77 | + |
| 78 | +## Static Methods |
| 79 | + |
| 80 | +Under the hood, these methods will utilize pooling to return a matching object, |
| 81 | +or return a new object if none exists. |
| 82 | + |
| 83 | +### create |
| 84 | + |
| 85 | +``` |
| 86 | +static create(config?: CharacterMetadataConfig): CharacterMetadata |
| 87 | +``` |
| 88 | +Generates a `CharacterMetadata` object from the provided configuration. This |
| 89 | +function should be used in lieu of a constructor. |
| 90 | + |
| 91 | +The configuration will be used to check whether a pooled match for this |
| 92 | +configuration already exists. If so, the pooled object will be returned. |
| 93 | +Otherwise, a new `CharacterMetadata` will be pooled for this configuration, |
| 94 | +and returned. |
| 95 | + |
| 96 | +### applyStyle |
| 97 | + |
| 98 | +``` |
| 99 | +static applyStyle( |
| 100 | + record: CharacterMetadata, |
| 101 | + style: string |
| 102 | +): CharacterMetadata |
| 103 | +``` |
| 104 | +Apply an inline style to this `CharacterMetadata`. |
| 105 | + |
| 106 | +### removeStyle |
| 107 | + |
| 108 | +``` |
| 109 | +static removeStyle( |
| 110 | + record: CharacterMetadata, |
| 111 | + style: string |
| 112 | +): CharacterMetadata |
| 113 | +``` |
| 114 | +Remove an inline style from this `CharacterMetadata`. |
| 115 | + |
| 116 | +### applyEntity |
| 117 | + |
| 118 | +``` |
| 119 | +static applyEntity( |
| 120 | + record: CharacterMetadata, |
| 121 | + entityKey: ?string |
| 122 | +): CharacterMetadata |
| 123 | +``` |
| 124 | + |
| 125 | +Apply an entity key -- or provide `null` to remove an entity key -- on this |
| 126 | +`CharacterMetadata`. |
| 127 | + |
| 128 | +## Methods |
| 129 | + |
| 130 | +### getStyle |
| 131 | + |
| 132 | +``` |
| 133 | +getStyle(): DraftInlineStyle |
| 134 | +``` |
| 135 | +Returns the `DraftInlineStyle` for this character, an `OrderedSet` of strings |
| 136 | +that represents the inline style to apply for the character at render time. |
| 137 | + |
| 138 | +### hasStyle |
| 139 | + |
| 140 | +``` |
| 141 | +hasStyle(style: string): boolean |
| 142 | +``` |
| 143 | +Returns whether this character has the specified style. |
| 144 | + |
| 145 | +### getEntity |
| 146 | + |
| 147 | +``` |
| 148 | +getEntity(): ?string |
| 149 | +``` |
| 150 | +Returns the entity key (if any) for this character, as mapped to the global set of |
| 151 | +entities tracked by the [`Entity`](https://github.com/facebook/draft-js/blob/master/src/model/entity/DraftEntity.js) |
| 152 | +module. |
| 153 | + |
| 154 | +By tracking a string key here, we can keep the corresponding metadata separate |
| 155 | +from the character representation. |
| 156 | + |
| 157 | +If null, no entity is applied for this character. |
0 commit comments