Skip to content
Snippets Groups Projects
Select Git revision
  • ae14d7ad45ba4d7b1bfc191d8bd7df9f05c99795
  • 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

WindowSideBarInfoPanel.test.js

Blame
  • TextEditor.js 3.72 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 { withStyles } from '@material-ui/core/styles';
    import { stateToHTML } from 'draft-js-export-html';
    import { stateFromHTML } from 'draft-js-import-html';
    
    /** */
    class TextEditor extends Component {
      /** */
      constructor(props) {
        super(props);
        this.state = {
          editorState: EditorState.createWithContent(stateFromHTML(props.annoHtml)),
        };
        this.onChange = this.onChange.bind(this);
        this.handleKeyCommand = this.handleKeyCommand.bind(this);
        this.handleFormating = this.handleFormating.bind(this);
        this.handleFocus = this.handleFocus.bind(this);
        this.editorRef = React.createRef();
      }
    
      /**
       * This is a kinda silly hack (but apparently recommended approach) to
       * making sure the whole visible editor area is clickable, not just the first line.
       */
      handleFocus() {
        if (this.editorRef.current) this.editorRef.current.focus();
      }
    
      /** */
      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';
      }
    
      /** */
      onChange(editorState) {
        const { updateAnnotationBody } = this.props;
        this.setState({ editorState });
        if (updateAnnotationBody) {
          const options = {
            inlineStyles: {
              BOLD: { element: 'b' },
              ITALIC: { element: 'i' },
            },
          };
          updateAnnotationBody(stateToHTML(editorState.getCurrentContent(), options).toString());
        }
      }
    
      /** */
      render() {
        const { classes } = this.props;
        const { editorState } = this.state;
        const currentStyle = editorState.getCurrentInlineStyle();
    
        return (
            <div>
              <div className={classes.editorRoot} onClick={this.handleFocus}>
                <Editor
                    editorState={editorState}
                    handleKeyCommand={this.handleKeyCommand}
                    onChange={this.onChange}
                    ref={this.editorRef}
                />
              </div>
              <ToggleButtonGroup
                  size="small"
                  value={currentStyle.toArray()}
              >
                <ToggleButton
                    onClick={this.handleFormating}
                    value="BOLD"
                >
                  <BoldIcon/>
                </ToggleButton>
                <ToggleButton
                    onClick={this.handleFormating}
                    value="ITALIC"
                >
                  <ItalicIcon/>
                </ToggleButton>
              </ToggleButtonGroup>
            </div>
        );
      }
    }
    
    /** */
    const styles = (theme) => ({
      editorRoot: {
        borderColor: theme.palette.type === 'light' ? 'rgba(0, 0, 0, 0.23)' : 'rgba(255, 255, 255, 0.23)',
        borderRadius: theme.shape.borderRadius,
        borderStyle: 'solid',
        borderWidth: 1,
        fontFamily: theme.typography.fontFamily,
        marginBottom: theme.spacing(1),
        marginTop: theme.spacing(1),
        minHeight: theme.typography.fontSize * 6,
        padding: theme.spacing(1),
      },
    });
    
    TextEditor.propTypes = {
      annoHtml: PropTypes.string,
      classes: PropTypes.shape({
        editorRoot: PropTypes.string,
      }).isRequired,
      updateAnnotationBody: PropTypes.func,
    };
    
    TextEditor.defaultProps = {
      annoHtml: '',
      updateAnnotationBody: () => {},
    };
    
    export default withStyles(styles)(TextEditor);