如何在React ace编辑器中添加自定义自动建议列表

时间:2020-09-15 11:47:16

标签: ace-editor react-ace

我已经安装了react-ace和ace-builds,并且我的组件看起来像:

import React from 'react';
import ReactAceEditor from 'react-ace';
import 'ace-builds/src-noconflict/mode-jsx';
import 'ace-builds/src-min-noconflict/ext-searchbox';
import 'ace-builds/src-min-noconflict/ext-language_tools';
import 'ace-builds/src-noconflict/mode-python';
import 'ace-builds/src-noconflict/snippets/python';
import 'ace-builds/src-noconflict/theme-xcode';

const AceEditor = (props) => {
  const {
    mode = 'python',
    theme = 'xcode',
    width = 'auto',
    placeholder = 'Placeholder Text',
    name = 'ace-editor',
    fontSize = 14,
    showPrintMargin = true,
    showGutter = true,
    highlightActiveLine = true,
    ...rest
  } = props;

const dataList = [
  { name: 'regularExpr', value: 'regularExpr', meta: 'metaProperty'},
  { name: 'isAllowSubnet', value: 'isAllowSubnet', meta: 'metaProperty'} ];

const customAceEditorCompleter = {
  getCompletions: (editor, session, caretPosition2d, prefix, callback) => {
    callback(
      null,
      dataList.map((item) => {
        return {
          name: item.name,
          value: item.value,
          meta: item.meta,
        };
      })
    );
  },
  getDocTooltip: (item) => {
    const pythonStyleSignature = `${item.name} ${item.value} ${item.meta}`;
    item.docHTML = ReactDOMServer.renderToStaticMarkup(
      <div>
        <b>{pythonStyleSignature}</b>
      </div>
    );
  },
};

  return (
    <ReactAceEditor
      name={name}
      mode={mode}
      theme={theme}
      placeholder={placeholder}
      editorProps={{ $blockScrolling: true }}
      setOptions={{
        enableBasicAutocompletion:[customAceEditorCompleter],
        enableLiveAutocompletion: true,
        enableSnippets: true,
      }}
      {...rest}
    />
  );
};

export default AceEditor;

我能够添加自定义自动完成器。但是内置的自动建议不再可见。自动完成程序是否可以同时显示自定义列表和内置自动建议列表?

0 个答案:

没有答案