Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit ee5fc12

Browse files
authored
Merge pull request #1269 from matrix-org/luke/upgrade-dep-draft-js-0.11.0
Upgrade draft-js to 0.11.0-alpha
2 parents 5752345 + a27eefd commit ee5fc12

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"classnames": "^2.1.2",
5454
"commonmark": "^0.27.0",
5555
"counterpart": "^0.18.0",
56-
"draft-js": "^0.10.1",
56+
"draft-js": "^0.11.0-alpha",
5757
"draft-js-export-html": "^0.5.0",
5858
"draft-js-export-markdown": "^0.2.0",
5959
"emojione": "2.2.7",

src/RichText.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export const contentStateToHTML = (contentState: ContentState) => {
5151
};
5252

5353
export function htmlToContentState(html: string): ContentState {
54-
return ContentState.createFromBlockArray(convertFromHTML(html));
54+
const blockArray = convertFromHTML(html).contentBlocks;
55+
return ContentState.createFromBlockArray(blockArray);
5556
}
5657

5758
function unicodeToEmojiUri(str) {
@@ -90,7 +91,7 @@ function findWithRegex(regex, contentBlock: ContentBlock, callback: (start: numb
9091

9192
// Workaround for https://github.com/facebook/draft-js/issues/414
9293
let emojiDecorator = {
93-
strategy: (contentBlock, callback) => {
94+
strategy: (contentState, contentBlock, callback) => {
9495
findWithRegex(EMOJI_REGEX, contentBlock, callback);
9596
},
9697
component: (props) => {
@@ -119,7 +120,7 @@ export function getScopedRTDecorators(scope: any): CompositeDecorator {
119120
export function getScopedMDDecorators(scope: any): CompositeDecorator {
120121
let markdownDecorators = ['HR', 'BOLD', 'ITALIC', 'CODE', 'STRIKETHROUGH'].map(
121122
(style) => ({
122-
strategy: (contentBlock, callback) => {
123+
strategy: (contentState, contentBlock, callback) => {
123124
return findWithRegex(MARKDOWN_REGEX[style], contentBlock, callback);
124125
},
125126
component: (props) => (
@@ -130,7 +131,7 @@ export function getScopedMDDecorators(scope: any): CompositeDecorator {
130131
}));
131132

132133
markdownDecorators.push({
133-
strategy: (contentBlock, callback) => {
134+
strategy: (contentState, contentBlock, callback) => {
134135
return findWithRegex(MARKDOWN_REGEX.LINK, contentBlock, callback);
135136
},
136137
component: (props) => (
@@ -238,7 +239,7 @@ export function attachImmutableEntitiesToEmoji(editorState: EditorState): Editor
238239
const existingEntityKey = block.getEntityAt(start);
239240
if (existingEntityKey) {
240241
// avoid manipulation in case the emoji already has an entity
241-
const entity = Entity.get(existingEntityKey);
242+
const entity = newContentState.getEntity(existingEntityKey);
242243
if (entity && entity.get('type') === 'emoji') {
243244
return;
244245
}
@@ -248,7 +249,10 @@ export function attachImmutableEntitiesToEmoji(editorState: EditorState): Editor
248249
.set('anchorOffset', start)
249250
.set('focusOffset', end);
250251
const emojiText = plainText.substring(start, end);
251-
const entityKey = Entity.create('emoji', 'IMMUTABLE', { emojiUnicode: emojiText });
252+
newContentState = newContentState.createEntity(
253+
'emoji', 'IMMUTABLE', { emojiUnicode: emojiText }
254+
);
255+
const entityKey = newContentState.getLastCreatedEntityKey();
252256
newContentState = Modifier.replaceText(
253257
newContentState,
254258
selection,

src/components/views/rooms/MessageComposerInput.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,18 @@ export default class MessageComposerInput extends React.Component {
165165
this.client = MatrixClientPeg.get();
166166
}
167167

168-
findLinkEntities(contentBlock, callback) {
168+
findLinkEntities(contentState: ContentState, contentBlock: ContentBlock, callback) {
169169
contentBlock.findEntityRanges(
170170
(character) => {
171171
const entityKey = character.getEntity();
172172
return (
173173
entityKey !== null &&
174-
Entity.get(entityKey).getType() === 'LINK'
174+
contentState.getEntity(entityKey).getType() === 'LINK'
175175
);
176176
}, callback,
177177
);
178178
}
179+
179180
/*
180181
* "Does the right thing" to create an EditorState, based on:
181182
* - whether we've got rich text mode enabled
@@ -188,7 +189,7 @@ export default class MessageComposerInput extends React.Component {
188189
strategy: this.findLinkEntities.bind(this),
189190
component: (entityProps) => {
190191
const Pill = sdk.getComponent('elements.Pill');
191-
const {url} = Entity.get(entityProps.entityKey).getData();
192+
const {url} = entityProps.contentState.getEntity(entityProps.entityKey).getData();
192193
if (Pill.isPillUrl(url)) {
193194
return <Pill url={url} room={this.props.room} offsetKey={entityProps.offsetKey}/>;
194195
}
@@ -713,7 +714,7 @@ export default class MessageComposerInput extends React.Component {
713714
const hasLink = blocks.some((block) => {
714715
return block.getCharacterList().filter((c) => {
715716
const entityKey = c.getEntity();
716-
return entityKey && Entity.get(entityKey).getType() === 'LINK';
717+
return entityKey && contentState.getEntity(entityKey).getType() === 'LINK';
717718
}).size > 0;
718719
});
719720
shouldSendHTML = hasLink;
@@ -734,8 +735,8 @@ export default class MessageComposerInput extends React.Component {
734735
const pt = contentState.getBlocksAsArray().map((block) => {
735736
let blockText = block.getText();
736737
let offset = 0;
737-
this.findLinkEntities(block, (start, end) => {
738-
const entity = Entity.get(block.getEntityAt(start));
738+
this.findLinkEntities(contentState, block, (start, end) => {
739+
const entity = contentState.getEntity(block.getEntityAt(start));
739740
if (entity.getType() !== 'LINK') {
740741
return;
741742
}
@@ -936,32 +937,27 @@ export default class MessageComposerInput extends React.Component {
936937
}
937938

938939
const {range = null, completion = '', href = null, suffix = ''} = displayedCompletion;
940+
let contentState = activeEditorState.getCurrentContent();
939941

940942
let entityKey;
941-
let mdCompletion;
942943
if (href) {
943-
entityKey = Entity.create('LINK', 'IMMUTABLE', {
944+
contentState = contentState.createEntity('LINK', 'IMMUTABLE', {
944945
url: href,
945946
isCompletion: true,
946947
});
948+
entityKey = contentState.getLastCreatedEntityKey();
947949
}
948950

949951
let selection;
950952
if (range) {
951953
selection = RichText.textOffsetsToSelectionState(
952-
range, activeEditorState.getCurrentContent().getBlocksAsArray(),
954+
range, contentState.getBlocksAsArray(),
953955
);
954956
} else {
955957
selection = activeEditorState.getSelection();
956958
}
957959

958-
let contentState = Modifier.replaceText(
959-
activeEditorState.getCurrentContent(),
960-
selection,
961-
mdCompletion || completion,
962-
null,
963-
entityKey,
964-
);
960+
contentState = Modifier.replaceText(contentState, selection, completion, null, entityKey);
965961

966962
// Move the selection to the end of the block
967963
const afterSelection = contentState.getSelectionAfter();
@@ -1047,7 +1043,7 @@ export default class MessageComposerInput extends React.Component {
10471043
offset -= sum;
10481044

10491045
const entityKey = block.getEntityAt(offset);
1050-
const entity = entityKey ? Entity.get(entityKey) : null;
1046+
const entity = entityKey ? contentState.getEntity(entityKey) : null;
10511047
if (entity && entity.getData().isCompletion) {
10521048
// This is a completed mention, so do not insert MD link, just text
10531049
return text;

0 commit comments

Comments
 (0)