Skip to content
Snippets Groups Projects
Select Git revision
  • 364947b8bc161339c54c9733acb512f4bc6b444e
  • mui5-annotation-on-video-stable default
  • get_setter_canvasSizeInformations
  • fix-error-div-into-p
  • annotation-on-video-v2
  • detached
  • annotation-on-video-r17
  • mui5
  • mui5-react-18
  • jacob-test
  • annotation-on-video protected
  • master
  • test-antoinev1
  • 20-fetch-thumbnail-on-annotation
  • add-research-field
  • Save
  • add-plugin
  • 14-wip-no-seek-to
  • 14-bug-on-video-time-control
  • 9_wip_videotests
  • _upgrade_material_ui
  • latest-tetras-16
  • v3.3.0
  • v3.2.0
  • v3.1.1
  • v3.1.0
  • v3.0.0
  • v3.0.0-rc.7
  • v3.0.0-rc.6
  • v3.0.0-rc.5
  • v3.0.0-rc.4
  • v3.0.0-rc.3
  • v3.0.0-rc.2
  • v3.0.0-rc.1
  • v3.0.0-beta.10
  • v3.0.0-beta.9
  • v3.0.0-beta.8
  • v3.0.0-beta.7
  • v3.0.0-beta.6
  • v3.0.0-beta.5
  • v3.0.0-beta.3
41 results

WindowTopBar.test.js

Blame
  • TextEditor.js 2.45 KiB
    import React, { Component } from 'react';
    import PropTypes from 'prop-types';
    import { Editor, EditorState, RichUtils } from 'draft-js';
    import ToggleButton from '@material-ui/lab/ToggleButton';
    import ToggleButtonGroup from '@material-ui/lab/ToggleButtonGroup';
    import BoldIcon from '@material-ui/icons/FormatBold';
    import ItalicIcon from '@material-ui/icons/FormatItalic';
    import { stateToHTML } from 'draft-js-export-html';
    
    /** */
    class TextEditor extends Component {
      /** */
      constructor(props) {
        super(props);
        this.state = { editorState: EditorState.createEmpty() };
        this.onChange = this.onChange.bind(this);
        this.handleKeyCommand = this.handleKeyCommand.bind(this);
        this.handleFormating = this.handleFormating.bind(this);
      }
    
      /** */
      onChange(editorState) {
        const { updateAnnotationBody } = this.props;
        this.setState({ editorState });
        console.log(this.props);
        if (updateAnnotationBody) {
          const options = {
            inlineStyles: {
              BOLD: { element: 'b' },
              ITALIC: { element: 'i' },
            },
          };
          updateAnnotationBody(stateToHTML(editorState.getCurrentContent(), options).toString());
        }
      }
    
      /** */
      handleFormating(e, newFormat) {
        const { editorState } = this.state;
        this.onChange(RichUtils.toggleInlineStyle(editorState, newFormat));
      }
    
      /** */
      handleKeyCommand(command, editorState) {
        const newState = RichUtils.handleKeyCommand(editorState, command);
        if (newState) {
          this.onChange(newState);
          return 'handled';
        }
        return 'not-handled';
      }
    
      /** */
      render() {
        const { editorState } = this.state;
        const currentStyle = editorState.getCurrentInlineStyle();
        return (
          <div>
            <ToggleButtonGroup
              size="small"
              value={currentStyle.toArray()}
            >
              <ToggleButton
                onClick={this.handleFormating}
                value="BOLD"
              >
                <BoldIcon />
              </ToggleButton>
              <ToggleButton
                onClick={this.handleFormating}
                value="ITALIC"
              >
                <ItalicIcon />
              </ToggleButton>
            </ToggleButtonGroup>
            <Editor
              editorState={editorState}
              handleKeyCommand={this.handleKeyCommand}
              onChange={this.onChange}
            />
          </div>
        );
      }
    }
    
    TextEditor.propTypes = {
      updateAnnotationBody: PropTypes.func,
    };
    
    TextEditor.defaultProps = {
      updateAnnotationBody: () => {},
    };
    
    export default TextEditor;