如何从 language_tools 外部在 React ace 编辑器中添加自定义完成器

问题描述 投票:0回答:2

如何使用 addCompleter 或 setCompleter 等函数从 index.js 在基于 React 的 ace 编辑器中添加自定义完成器

import { render } from "react-dom";
import AceEditor from "../src/ace";
import "brace/mode/jsx";
import 'brace/mode/HCPCustomCalcs'
import 'brace/theme/monokai'
import "brace/snippets/HCPCustomCalcs";
import "brace/ext/language_tools";
const defaultValue = `function onLoad(editor) {
  console.log("i've loaded");
}`;
class App extends Component {

  constructor(props, context) {
    super(props, context);
    this.onChange = this.onChange.bind(this);
}
onChange(newValue) {
    console.log('changes:', newValue);
}
  render() {

      return (
          <div>
              <AceEditor
                  mode="HCPCustomCalcs"
                  theme="monokai"
                  width={ '100%' }
                  height={ '100vh' }
                  onChange={this.onChange}
                  name="UNIQUE_ID_OF_DIV"
                  editorProps={{
                      $blockScrolling: true
                  }}
                  enableBasicAutocompletion={true}
                  enableLiveAutocompletion={true}
                  enableSnippets={true}
              />
          </div>
      );
  }
}
render(<App />, document.getElementById("example"));

我想从这里添加我的自定义完成器。我的完成器是这样的

var myCompleter ={
getCompletions: function(editor, session, pos, prefix, callback) {
        var completions = [];
        ["word1", "word2"].forEach(function(w) {

            completions.push({
                value: w,
                meta: "my completion",

            });
        });
        callback(null, completions);
    }
})}

在普通的 Ace 编辑器中,这很简单。只需调用

var langTools = ace.require("ace/ext/language_tools")
langTools.addCompleter(myCompleter1);```
ace-editor react-ace
2个回答
5
投票

您可以执行与普通 Ace 编辑器非常相似的操作。

const langTools = ace.acequire('ace/ext/language_tools');

langTools.addCompleter(myCompleter);

使用

ace.acequire
可以让您访问隐藏的 ACE 功能。这里有一些文档:https://github.com/thlorenz/brace/wiki/Advanced:-Accessing-somewhat-hidden-ACE-features


0
投票

我需要使用 React JS 创建功能,但我在 langTools 上遇到了问题,我无法使用以下方法进行实例化:

var langTools = ace.require("ace/ext/language_tools")
langTools.addCompleter(myCompleter1);```

返回的错误 addCompleter 未定义,所以我使用:

import { addCompleter } from 'ace-builds/src-noconflict/ext-language_tools';
addCompleter(myCompleter1);

这样我就可以毫无问题地运行它。

辅助链接:

https://gist.github.com/yuvalherziger/aa48782568c6914b55066d290ff88360 https://github.com/securesincity/react-ace/issues/338

© www.soinside.com 2019 - 2024. All rights reserved.