Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color Picker??? #341

Open
IN2TEC opened this issue Oct 16, 2023 · 0 comments
Open

Color Picker??? #341

IN2TEC opened this issue Oct 16, 2023 · 0 comments

Comments

@IN2TEC
Copy link

IN2TEC commented Oct 16, 2023

Hi there, I am new to this and I am trying to implement a Color Picker to format a selected text. But whatever I do, I am not getting it to work as the color picker wont even show.

import React, { useState } from 'react';
import MUIRichTextEditor from 'mui-rte';
import { Modifier, EditorState } from 'draft-js';
import { SketchPicker } from 'react-color';
import {IconButton }from  "@mui/material";
import ColorLensIcon from '@mui/icons-material/ColorLens';

const ColorPickerControl = ({ editorState, onChange }) => {
  const [showColorPicker, setShowColorPicker] = useState(false);
  const [currentColor, setCurrentColor] = useState('#000');
  const [selectedText, setSelectedText] = useState('');

  const handleColorChange = (color) => {
    setCurrentColor(color.hex);
  };

  const applyColor = () => {
    const contentState = editorState.getCurrentContent();
    const selection = editorState.getSelection();

    // Create a new content state with the selected text colored
    const contentWithColor = Modifier.applyInlineStyle(
      contentState,
      selection,
      'CUSTOM_COLOR',
      currentColor
    );

    // Apply the new content state to the editor
    onChange(EditorState.push(editorState, contentWithColor, 'change-color'));

    // Close the color picker
    setShowColorPicker(false);
  };

  const handleColorPickerClick = () => {
    const selection = editorState.getSelection();
    const currentContent = editorState.getCurrentContent();
    const selectedText = currentContent
      .getBlockForKey(selection.getStartKey())
      .getText()
      .slice(selection.getStartOffset(), selection.getEndOffset());

    setSelectedText(selectedText);
    setShowColorPicker(true);
  };

  return (
    <div>
      <IconButton onClick={handleColorPickerClick}>
        <ColorLensIcon />
      </IconButton>
      {showColorPicker && (
        <div>
          <SketchPicker
            color={currentColor}
            onChange={handleColorChange}
          />
          <IconButton onClick={applyColor}>
            Apply
          </IconButton>
        </div>
      )}
      {selectedText && (
        <div>
          Selected Text: {selectedText}
        </div>
      )}
    </div>
  );
};

export const IntuRichTextEditor = ({ field }) => {
  const customControls = [
    {
      name: 'color-picker',
      type: 'callback',
      icon: <ColorLensIcon />,
      onClick: (editorState, _name, _anchor) => {
        // Open the color picker by setting the state in ColorPickerControl
      },
      component: <ColorPickerControl />,
    },
  ];

  return (
    <MUIRichTextEditor
      controls={[
        'title',
        'bold',
        'italic',
        'underline',
        'strikethrough',
        'highlight',
        'undo',
        'redo',
        'link',
        'media',
        'numberList',
        'bulletList',
        'quote',
        'code',
        'clear',
        'color-picker',
      ]}
      customControls={customControls}
      {...field}
    />
  );
};

Can someone help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant