Reflect API change for getting an Entity

pull/21833/head
Luke Barnard 2017-08-03 11:29:26 +01:00
parent 124795006c
commit fb5dc295aa
2 changed files with 21 additions and 17 deletions

View File

@ -238,7 +238,7 @@ export function attachImmutableEntitiesToEmoji(editorState: EditorState): Editor
const existingEntityKey = block.getEntityAt(start); const existingEntityKey = block.getEntityAt(start);
if (existingEntityKey) { if (existingEntityKey) {
// avoid manipulation in case the emoji already has an entity // avoid manipulation in case the emoji already has an entity
const entity = Entity.get(existingEntityKey); const entity = newContentState.getEntity(existingEntityKey);
if (entity && entity.get('type') === 'emoji') { if (entity && entity.get('type') === 'emoji') {
return; return;
} }

View File

@ -165,17 +165,20 @@ export default class MessageComposerInput extends React.Component {
this.client = MatrixClientPeg.get(); this.client = MatrixClientPeg.get();
} }
findLinkEntities(contentBlock, callback) { getLinkFindingStrategy(contentState: ContentState) {
contentBlock.findEntityRanges( return (contentBlock, callback) => {
(character) => { contentBlock.findEntityRanges(
const entityKey = character.getEntity(); (character) => {
return ( const entityKey = character.getEntity();
entityKey !== null && return (
Entity.get(entityKey).getType() === 'LINK' entityKey !== null &&
); contentState.getEntity(entityKey).getType() === 'LINK'
}, callback, );
); }, callback,
);
};
} }
/* /*
* "Does the right thing" to create an EditorState, based on: * "Does the right thing" to create an EditorState, based on:
* - whether we've got rich text mode enabled * - whether we've got rich text mode enabled
@ -185,10 +188,10 @@ export default class MessageComposerInput extends React.Component {
const decorators = richText ? RichText.getScopedRTDecorators(this.props) : const decorators = richText ? RichText.getScopedRTDecorators(this.props) :
RichText.getScopedMDDecorators(this.props); RichText.getScopedMDDecorators(this.props);
decorators.push({ decorators.push({
strategy: this.findLinkEntities.bind(this), strategy: this.getLinkFindingStrategy(contentState),
component: (entityProps) => { component: (entityProps) => {
const Pill = sdk.getComponent('elements.Pill'); const Pill = sdk.getComponent('elements.Pill');
const {url} = Entity.get(entityProps.entityKey).getData(); const {url} = contentState.getEntity(entityProps.entityKey).getData();
if (Pill.isPillUrl(url)) { if (Pill.isPillUrl(url)) {
return <Pill url={url} room={this.props.room} offsetKey={entityProps.offsetKey}/>; return <Pill url={url} room={this.props.room} offsetKey={entityProps.offsetKey}/>;
} }
@ -713,7 +716,7 @@ export default class MessageComposerInput extends React.Component {
const hasLink = blocks.some((block) => { const hasLink = blocks.some((block) => {
return block.getCharacterList().filter((c) => { return block.getCharacterList().filter((c) => {
const entityKey = c.getEntity(); const entityKey = c.getEntity();
return entityKey && Entity.get(entityKey).getType() === 'LINK'; return entityKey && contentState.getEntity(entityKey).getType() === 'LINK';
}).size > 0; }).size > 0;
}); });
shouldSendHTML = hasLink; shouldSendHTML = hasLink;
@ -724,6 +727,7 @@ export default class MessageComposerInput extends React.Component {
); );
} }
} else { } else {
const findLinkEntities = this.getLinkFindingStrategy(contentState);
// Use the original contentState because `contentText` has had mentions // Use the original contentState because `contentText` has had mentions
// stripped and these need to end up in contentHTML. // stripped and these need to end up in contentHTML.
@ -734,8 +738,8 @@ export default class MessageComposerInput extends React.Component {
const pt = contentState.getBlocksAsArray().map((block) => { const pt = contentState.getBlocksAsArray().map((block) => {
let blockText = block.getText(); let blockText = block.getText();
let offset = 0; let offset = 0;
this.findLinkEntities(block, (start, end) => { findLinkEntities(block, (start, end) => {
const entity = Entity.get(block.getEntityAt(start)); const entity = contentState.getEntity(block.getEntityAt(start));
if (entity.getType() !== 'LINK') { if (entity.getType() !== 'LINK') {
return; return;
} }
@ -1042,7 +1046,7 @@ export default class MessageComposerInput extends React.Component {
offset -= sum; offset -= sum;
const entityKey = block.getEntityAt(offset); const entityKey = block.getEntityAt(offset);
const entity = entityKey ? Entity.get(entityKey) : null; const entity = entityKey ? contentState.getEntity(entityKey) : null;
if (entity && entity.getData().isCompletion) { if (entity && entity.getData().isCompletion) {
// This is a completed mention, so do not insert MD link, just text // This is a completed mention, so do not insert MD link, just text
return text; return text;