How to use draft-js - 10 common examples

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 assembl / assembl / assembl / static2 / workspaces / draft-js-attachment-plugin / src / components / __test__ / Image.spec.jsx View on Github external
it('should render an image (url)', () => {
    let contentState = ContentState.createFromText('');
    // $FlowFixMe DraftEntityType is too restrictive in DraftJS (see https://github.com/facebook/draft-js/issues/868 )
    contentState = contentState.createEntity(ENTITY_TYPES.image, ENTITY_MUTABILITY.immutable, {
      mimeType: 'image/png',
      src: 'my-img.png',
      title: 'My image'
    });
    const entityKey = contentState.getLastCreatedEntityKey();
    let editorState = EditorState.createWithContent(contentState);
    editorState = AtomicBlockUtils.insertAtomicBlock(editorState, entityKey, ' ');
    contentState = editorState.getCurrentContent();
    const blocks = contentState.getBlocksAsArray();
    const atomicBlock = blocks[1];

    const props = {
      block: atomicBlock,
      contentState: contentState
    };
    const component = renderer.create(<img>);
    const tree = component.toJSON();
    expect(tree).toMatchSnapshot();
  });
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 assembl / assembl / assembl / static2 / workspaces / assembl-test-editor-utils / src / index.js View on Github external
id: '1',
      mimeType: 'image/png',
      src: imgSrc,
      title: 'My image'
    });
    const imgEntityKey = contentState.getLastCreatedEntityKey();
    // $FlowFixMe DraftEntityType is too restrictive in DraftJS (see https://github.com/facebook/draft-js/issues/868 )
    contentState = contentState.createEntity(ENTITY_TYPES.document, ENTITY_MUTABILITY.immutable, {
      id: '2',
      mimeType: 'application/pdf',
      src: fileSrc,
      title: 'My document'
    });
    const docEntityKey = contentState.getLastCreatedEntityKey();

    let editorState = EditorState.createWithContent(contentState);
    editorState = AtomicBlockUtils.insertAtomicBlock(editorState, imgEntityKey, ' ');
    editorState = AtomicBlockUtils.insertAtomicBlock(editorState, docEntityKey, ' ');
    return editorState;
  },
github assembl / assembl / assembl / static2 / workspaces / assembl-test-editor-utils / src / index.js View on Github external
src: imgSrc,
      title: 'My image'
    });
    const imgEntityKey = contentState.getLastCreatedEntityKey();
    // $FlowFixMe DraftEntityType is too restrictive in DraftJS (see https://github.com/facebook/draft-js/issues/868 )
    contentState = contentState.createEntity(ENTITY_TYPES.document, ENTITY_MUTABILITY.immutable, {
      id: '2',
      mimeType: 'application/pdf',
      src: fileSrc,
      title: 'My document'
    });
    const docEntityKey = contentState.getLastCreatedEntityKey();

    let editorState = EditorState.createWithContent(contentState);
    editorState = AtomicBlockUtils.insertAtomicBlock(editorState, imgEntityKey, ' ');
    editorState = AtomicBlockUtils.insertAtomicBlock(editorState, docEntityKey, ' ');
    return editorState;
  },
github LessWrong2 / Lesswrong2 / packages / lesswrong / components / async / AsyncCommentEditor.jsx View on Github external
constructor(props, context) {
    super(props,context);
    const document = this.props.document;
    let state = {};
    if (document && document.content) {
      try {
        state = EditorState.createWithContent(convertFromRaw(document.content));
      } catch(e) {
        // eslint-disable-next-line no-console
        console.warn("Invalid comment content, restoring from HTML instead", document);
        state = document && document.htmlBody && EditorState.createWithContent(htmlToDraft(document.htmlBody, {flat: true}))
      }
    } else if (document && document.htmlBody) {
      state = EditorState.createWithContent(htmlToDraft(document.htmlBody, {flat: true}));
    } else {
      state = EditorState.createEmpty();
    }

    // Check whether we have a state from a previous session saved (in localstorage)
    const savedState = this.getSavedState();
    if (savedState) {
      try {
        // eslint-disable-next-line no-console
        console.log("Restoring saved comment state: ", savedState);
        state = EditorState.createWithContent(convertFromRaw(savedState))
      } catch(e) {
        // eslint-disable-next-line no-console
        console.error(e)
      }
    }

    this.state = {
github chaskiq / chaskiq / app / javascript / src / editor / components / editor.js View on Github external
// 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 icelab / draft-js-autolist-plugin / src / index.js View on Github external
handleReturn (e, editorState, { setEditorState }) {
      let content = editorState.getCurrentContent()

      // Retrieve current block
      const selection = editorState.getSelection()
      const blockKey = selection.getStartKey()
      let block = content.getBlockForKey(blockKey)
      const blockType = block.getType()

      // If it’s a list-item and it’s empty, toggle its blockType (which should
      // make it 'unstyled')
      if (/list-item/.test(blockType) && block.getText() === '') {
        editorState = RichUtils.toggleBlockType(editorState, blockType)
        content = editorState.getCurrentContent()
        editorState = EditorState.forceSelection(editorState, content.getSelectionAfter())
        setEditorState(editorState)
        return 'handled'
      }
      return 'not-handled'
    },
github leejaen / react-lz-editor / src / editor / index.jsx View on Github external
_handleKeyCommand(command) {
    // console.log("command",command);
    const {editorState} = this.state;
    const newState = RichUtils.handleKeyCommand(editorState, command);
    if (command === 'editor-save'&amp;&amp;this.props.autoSave==true) {
      // window.localDB//start20Text
      // let Data=PRO_COMMON.localDB.getter("grab_news_data") || [];

      let rawContentState = editorState.getCurrentContent()
      let content = "",newText="";

      const ConvertFormatProps = this.props.convertFormat;
      if(ConvertFormatProps === 'html') {
        content = stateToHTML(rawContentState);
        newText=content.replace(/&lt;[^&gt;]*&gt;|&amp;[^;]*;/g, "");
      }else if (ConvertFormatProps === 'markdown') {
        content = stateToMD(rawContentState);
      }else if(ConvertFormatProps === 'raw') {
        const rawContent = convertToRaw(rawContentState);
        content = JSON.stringify(rawContent);
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 artsy / positron / src / client / components / draft / paragraph / utils / utils.js View on Github external
export const insertPastedState = (pastedState, editorState) => {
  /**
   * Merges a state created from pasted text into editorState
   */
  const blockMap = pastedState.getCurrentContent().blockMap

  // Merge blockMap from pasted text into existing content
  const modifiedContent = Modifier.replaceWithFragment(
    editorState.getCurrentContent(),
    editorState.getSelection(),
    blockMap
  )
  // Create a new editorState from merged content
  return EditorState.push(
    editorState, modifiedContent, 'insert-fragment'
  )
}