5

我需要在工具栏部分添加自定义下拉菜单。

这里附加的图像类似于想要下拉菜单这可能吗?

<img src="https://i.imgur.com/OhYeFsL.png" alt="Dropdown menu editor">

找到下面的详细图片

在此处输入图像描述

我使用了 react-draft-wysiwyg 内容编辑器。

https://github.com/jpuri/react-draft-wysiwyg

https://jpuri.github.io/react-draft-wysiwyg/#/d

在工具栏部分添加自定义下拉菜单。

4

2 回答 2

15

我希望这仍然是相关的,但这是我的方式。

对于自定义下拉菜单,我创建了一个新组件并使用文档https://jpuri.github.io/react-draft-wysiwyg/#/docs中的“向工具栏添加新选项”的方法

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { EditorState, Modifier } from 'draft-js';

class Placeholders extends Component {
  static propTypes = {
    onChange: PropTypes.func,
    editorState: PropTypes.object,
  }

  state = {
    open: false
  }

  openPlaceholderDropdown = () => this.setState({open: !this.state.open})

  addPlaceholder = (placeholder) => {
    const { editorState, onChange } = this.props;
    const contentState = Modifier.replaceText(
    editorState.getCurrentContent(),
    editorState.getSelection(),
    placeholder,
    editorState.getCurrentInlineStyle(),
    );
    onChange(EditorState.push(editorState, contentState, 'insert-characters'));
  }

  placeholderOptions = [
    {key: "firstName", value: "{{firstName}}", text: "First Name"},
    {key: "lastName", value: "{{lastName}}", text: "Last name"},
    {key: "company", value: "{{company}}", text: "Company"},
    {key: "address", value: "{{address}}", text: "Address"},
    {key: "zip", value: "{{zip}}", text: "Zip"},
    {key: "city", value: "{{city}}", text: "City"}
  ]

  listItem = this.placeholderOptions.map(item => (
    <li 
      onClick={this.addPlaceholder.bind(this, item.value)} 
      key={item.key}
      className="rdw-dropdownoption-default placeholder-li"
    >{item.text}</li>
  ))

  render() {
    return (
      <div onClick={this.openPlaceholderDropdown} className="rdw-block-wrapper" aria-label="rdw-block-control">
        <div className="rdw-dropdown-wrapper rdw-block-dropdown" aria-label="rdw-dropdown">
          <div className="rdw-dropdown-selectedtext" title="Placeholders">
            <span>Placeholder</span> 
            <div className={`rdw-dropdown-caretto${this.state.open? "close": "open"}`}></div>
          </div>
          <ul className={`rdw-dropdown-optionwrapper ${this.state.open? "": "placeholder-ul"}`}>
            {this.listItem}
          </ul>
        </div>
      </div>
    );
  }
}

export default Placeholders;

我使用自定义下拉列表来添加占位符。但本质仍然保持不变,因为我使用文档中的示例作为自定义按钮。

为了呈现按钮本身,我使用了与其他下拉按钮相同的样式、类和结构。我刚刚将锚标记切换为 div 标记,并为悬停样式和胡萝卜更改添加了自定义类。我还使用事件来切换类。

  .placeholder-ul{
    visibility: hidden;
  }
  .placeholder-li:hover {
    background: #F1F1F1;
  }

最后,不要忘记将自定义按钮导入并添加到编辑器。

<Editor
   editorState={this.state.editorState}
   onEditorStateChange={this.onEditorStateChange}
   toolbarCustomButtons={[<Placeholders />]}
/>
于 2019-02-20T09:52:57.750 回答
1

我使用了 Tomas 他的代码并将其更新为 TypeScript / Function 组件。可以同意此解决方案在 2020 年仍然适用于 Draft.js v0.10.5

type ReplacementsProps = {
  onChange?: (editorState: EditorState) => void,
  editorState: EditorState,
}


export const Replacements = ({onChange, editorState}: ReplacementsProps) => {
  const [open, setOpen] = useState<boolean>(false);

  const addPlaceholder = (placeholder: string): void => {
    const contentState = Modifier.replaceText(
      editorState.getCurrentContent(),
      editorState.getSelection(),
      placeholder,
      editorState.getCurrentInlineStyle(),
    );
    const result = EditorState.push(editorState, contentState, 'insert-characters');
    if (onChange) {
      onChange(result);
    }
  };

  return (
    <div onClick={() => setOpen(!open)} className="rdw-block-wrapper" aria-label="rdw-block-control" role="button" tabIndex={0}>
      <div className="rdw-dropdown-wrapper rdw-block-dropdown" aria-label="rdw-dropdown" style={{width: 180}}>
        <div className="rdw-dropdown-selectedtext">
          <span>YOuR TITLE HERE</span>
          <div className={`rdw-dropdown-caretto${open ? 'close' : 'open'}`} />
        </div>
        <ul className={`rdw-dropdown-optionwrapper ${open ? '' : 'placeholder-ul'}`}>
          {placeholderOptions.map(item => (
            <li
              onClick={() => addPlaceholder(item.value)}
              key={item.value}
              className="rdw-dropdownoption-default placeholder-li"
            >
              {item.text}
            </li>
          ))}
        </ul>
      </div>
    </div>
  );
};

于 2020-04-25T19:21:58.610 回答