How to use the draft-js.SelectionState.createEmpty function in draft-js

To help you get started, we’ve selected a few draft-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SamyPesse / draft-js-code / lib / __tests__ / handleKeyCommand.js View on Github external
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);
});
github michelson / dante2 / src / components / core / editor.js View on Github external
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)
  }
github webRunes / WRIO-InternetOS / src / CoreEditor / utils / entitytools.js View on Github external
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
      );

  }
}
github brijeshb42 / medium-draft / packages / medium-draft / src / model / index.spec.js View on Github external
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);
    });
  });
github react-component / editor-mention / src / component / Mention.react.jsx View on Github external
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) {
github HubSpot / draft-convert / src / convertFromHTML.js View on Github external
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),
  });
};
github busyorg / busy / src / post / Write / PostEditor.js View on Github external
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);
    }
  }
github LessWrong2 / Lesswrong2 / src / components / async / editor-plugins / markdown-shortcuts-plugin / modifiers / insertImage.js View on Github external
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
  );