Ace编辑器自动完成Angular 2?

时间:2017-07-07 10:14:38

标签: angular ace-editor

我正在使用ace editor并尝试在编辑器中实现自动完成功能。我尝试了选项,但它没有工作,我收到警告。任何想法?

Ace编辑器组件

import {
    Component, EventEmitter, Input, OnInit, Output, ViewChild
} from '@angular/core';

@Component({
    selector: 'neon-ace-editor',
    templateUrl: './ace-editor.component.html',
    styleUrls: ['./ace-editor.component.scss']
})
export class AceEditorComponent implements OnInit {
    @Input() mode = 'html';
    @Input() autoUpdateContent = true;
    @Input() content: string;
    @Output() onContentChange: EventEmitter<string> = new EventEmitter();
    @ViewChild('editor') editor;
    options = {
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: true
    };
    ngOnInit(): void {
        this.editor._editor.$blockScrolling = Infinity;
    }

    onContentChanging(): void {
        this.onContentChange.emit(this.content);
    }
}

Ace Editor Html

<ace-editor [(text)]="content"
            #editor
            (textChanged)="onContentChanging()"
            [options]="options"
            [autoUpdateContent]="autoUpdateContent"
            style="min-height: 500px; width:100%; font-size:18px;overflow: auto;"
            [mode]="mode"></ace-editor>

问题:

自动完成无效。

控制台中的警告消息

enter image description here

3 个答案:

答案 0 :(得分:3)

试试这个 app.module.ts

imports: [
    ...,
    AceEditorModule // import AceEditorModule
  ]

app.component.ts

import {Component, OnInit, ViewChild} from '@angular/core';

import 'brace';
import 'brace/ext/language_tools';
import 'ace-builds/src-min-noconflict/snippets/html';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  text = '';
  @ViewChild('editor') editor;
  options: any = {
    enableBasicAutocompletion: true,
    enableSnippets: true,
    enableLiveAutocompletion: true,
  };

  ngOnInit() {
   // disable Automatically scrolling cursor into view after selection change warning
    this.editor.getEditor().$blockScrolling = Infinity;
  }
}
app.component.html

<ace-editor
  theme="monokai"
  mode="html"
  [options]="options"
  #editor
  style=" min-height: 600px; width:100%;overflow: auto;"
>
</ace-editor>
.angular-cli.json

"scripts": [],

了解更多discusstion

答案 1 :(得分:0)

如果您使用的是Angular 7.X,则使用Ace Editor的最重要的部分是将脚本导入angular.json文件中。

"scripts": [
                "node_modules/ace-builds/src-min/ace.js",
                "node_modules/ace-builds/src-min/mode-xml.js",
                "node_modules/ace-builds/src-min/mode-html.js",
                "node_modules/ace-builds/src-min/theme-vibrant_ink.js",
                "node_modules/ace-builds/src-min/snippets/xml.js",
                "node_modules/ace-builds/src-min/snippets/html.js",
                "node_modules/ace-builds/src-min/ext-beautify.js",
                "node_modules/ace-builds/src-min/ext-searchbox.js",
                "node_modules/ace-builds/src-min/ext-language_tools.js",
                { "bundleName": "worker-xml", "input": "node_modules/ace-builds/src-min/worker-xml.js" },
                { "bundleName": "worker-html", "input": "node_modules/ace-builds/src-min/worker-html.js" }
            ]

如果您不选择自动完成选项,则可以省略以下两行:

                "node_modules/ace-builds/src-min/ext-searchbox.js",
                "node_modules/ace-builds/src-min/ext-language_tools.js",

当然,您需要完全根据自己的需要选择合适的workermodetheme。在此示例中,我对Vibrant Inkxml文档使用html主题和配置。

同样重要的是,默认xml片段为空,因此您应该使用自己的片段。

答案 2 :(得分:0)

基于this code和这个post

如果你跑了

npm i ace-builds

然后在scripts这些行

中添加到您的 angular.json 属性
...
scripts:[
  "node_modules/ace-builds/src-noconflict/ace.js",
  "node_modules/ace-builds/src-noconflict/ext-language_tools.js"
]
...

然后在您的角度代码中:

import * as ace from "ace-builds";
...
this.aceEditor = ace.edit(this.editor.nativeElement);
const options = {
  enableBasicAutocompletion: true,
  enableLiveAutocompletion: true
}
this.aceEditor.setOptions(options);
...

查看 this.aceEditor.$options 以查看是否添加了选项。