Get started with draft-js CDN

BSD-3-Clause licensed

Draft.js is a library for building rich text editors in web applications.

Tags:
  • draftjs
  • editor
  • react
  • richtext

Stable version

Copied!

How to start using draft-js CDN


const { EditorState, ContentState, convertToRaw, Modifier } = require('draft-js');
const Draft = require('draft-js/dist/Draft.min');

// Initialize the editor with some content
const editorState = EditorState.createWithContent(
  ContentState.createFromText('Hello world!')
);

// Create a Draft.js editor
const editor = Draft.Editor.create(document.querySelector('#editor'), editorState, {});

// Add a button to update the text
document.querySelector('#update').addEventListener('click', () => {
  const newContent = editor.getContent();
  const newText = 'New text!';

  // Update the content with new text
  const newEditorState = EditorState.push(editor.getState(), newContent.getPlainText(), 'replace-text');

  // Set the new editor state
  editor.onChange(newEditorState);
});

// Update the content when the user types
editor.on('change', () => {
  console.log('Content changed: ', editor.getContent());
});
Copied!
Copied!
Copied!
Copied!

All versions