Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should not do anything on backspace if something is selected', () => {
const initialText = 'hello';
const currentContent = ContentState.createFromText(initialText);
const selectInitialtext = SelectionState.createEmpty(
currentContent
.getBlockMap()
.first()
.getKey()
);
const editorState = EditorState.create({
allowUndo: true,
currentContent,
// Focus the entire initial word
selection: selectInitialtext.set('focusOffset', initialText.length)
});
expect(handleKeyCommand(editorState, 'backspace')).toEqual(undefined);
});
refreshSelection(newEditorState) {
const { editorState } = this.state
// Setting cursor position after inserting to content
const s = this.state.editorState.getSelection()
const c = editorState.getCurrentContent()
const focusOffset = s.getFocusOffset()
const anchorKey = s.getAnchorKey()
let selectionState = SelectionState.createEmpty(s.getAnchorKey())
// console.log anchorKey, focusOffset
selectionState = selectionState.merge({
anchorOffset: focusOffset,
focusKey: anchorKey,
focusOffset
})
let newState = EditorState.forceSelection(newEditorState, selectionState)
return this.onChange(newState)
}
static constructTicket(editorState, blockKey, blockData, insertEmpty = true) {
let entityKey;
entityKey = this.createTicketEntity(
blockData.url,
blockData.name,
blockData.about,
blockData.image,
);
const _editorState = EditorState.forceSelection(
editorState,
SelectionState.createEmpty(blockKey)
); // We are creating entity in wrong place!!!
return EntityTools.insertEntityKeyIntoAtomicBlock(
_editorState,
entityKey,
insertEmpty
);
}
}
it('always returns currently focused/selected block', () => {
expect(getCurrentBlock(es).getKey()).to.equal(block1.key);
const selection = SelectionState.createEmpty(block2.key);
const es2 = EditorState.acceptSelection(es, selection);
expect(getCurrentBlock(es2).getKey()).to.equal(block2.key);
});
});
this.mention = createMention({
prefix: this.getPrefix(props),
tag: props.tag,
mode: props.mode,
mentionStyle: props.mentionStyle,
});
this.Suggestions = this.mention.Suggestions;
this.plugins = [this.mention];
this.state = {
suggestions: props.suggestions,
value: props.value && EditorState.createWithContent(props.value, new CompositeDecorator(this.mention.decorators)),
selection: SelectionState.createEmpty(),
};
if (typeof props.defaultValue === 'string') {
console.warn('The property `defaultValue` now allow `EditorState` only, see http://react-component.github.io/editor-mention/examples/defaultValue.html ');
}
if (props.value !== undefined) {
this.controlledMode = true;
}
}
componentWillReceiveProps(nextProps) {
handleMiddleware(textToEntity, defaultTextToEntity),
handleMiddleware(htmlToBlock, baseCheckBlockType),
createEntityWithContentState,
getEntityWithContentState,
mergeEntityDataWithContentState,
replaceEntityDataWithContentState,
options,
DOMBuilder,
generateKey
);
const blockMap = BlockMapBuilder.createFromArray(contentBlocks);
const firstBlockKey = contentBlocks[0].getKey();
return contentState.merge({
blockMap,
selectionBefore: SelectionState.createEmpty(firstBlockKey),
selectionAfter: SelectionState.createEmpty(firstBlockKey),
});
};
addEmptyLine = () => {
let editorState = this.state.editorState;
const contentState = editorState.getCurrentContent();
const lastBlock = contentState.getLastBlock();
if (lastBlock.getType() === 'code-block' || lastBlock.getType() === 'atomic') {
const blockArray = contentState.getBlocksAsArray();
const newBlock = new ContentBlock({ key: genKey(), type: 'unstyled', text: '' });
const newContentState = ContentState.createFromBlockArray([...blockArray, newBlock]);
const newSelectionState = SelectionState.createEmpty(newBlock.getKey());
editorState = EditorState.push(editorState, newContentState, 'insert-characters');
editorState = EditorState.forceSelection(editorState, newSelectionState);
this.onChange(editorState);
}
}
const insertImage = (editorState, matchArr) => {
const currentContent = editorState.getCurrentContent();
const selection = editorState.getSelection();
const key = selection.getStartKey();
const [
matchText,
alt,
src,
title
] = matchArr;
const { index } = matchArr;
const focusOffset = index + matchText.length;
const wordSelection = SelectionState.createEmpty(key).merge({
anchorOffset: index,
focusOffset
});
const nextContent = currentContent.createEntity(
'IMG',
'IMMUTABLE',
{ alt, src, title }
);
const entityKey = nextContent.getLastCreatedEntityKey();
let newContentState = Modifier.replaceText(
nextContent,
wordSelection,
'\u200B',
null,
entityKey
);