4

I am using ace editor and trying to achieve auto-completion in editor. I tried with options but its not working and I am getting warnings.Any idea?

Ace Editor Component

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>

Issue:

Auto-complete is not working.

Warning messages in console

enter image description here

Akkusativobjekt
  • 2,005
  • 1
  • 21
  • 26
Shailesh Ladumor
  • 7,052
  • 5
  • 42
  • 55

3 Answers3

4

Try this 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": [],

for more discusstion

Ajay
  • 848
  • 8
  • 17
2

If you are using Angular 7.X the most important part of working with Ace Editor is to import scripts in angular.json file.

"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" }
            ]

If you doesn't wont autocomlete option you can omit this two lines:

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

Of course you need to choos good worker , mode and theme exactly for your needs. In this example I use Vibrant Ink theme and configuration for xml and html documents.

What is also important, default xml snippets are empty, so you should use your own snnippets.

kris_IV
  • 2,396
  • 21
  • 42
  • @AhmedE.Eldeeb they don't have any Angular docs. I look into library code for solutions and configuration and on this webpage: https://ace.c9.io/build/kitchen-sink.html but you should be careful because sometimes Angular versions use different names for properties or library doesn't pass all properties. – kris_IV Jan 27 '20 at 20:06
0

based on this code and this post

if you run

npm i ace-builds

then add to your scripts property in angular.jsonthese lines

...
scripts:[
  "node_modules/ace-builds/src-noconflict/ace.js",
  "node_modules/ace-builds/src-noconflict/ext-language_tools.js"
]
...

then in your angular code:

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

looking at this.aceEditor.$options to see if options were added.

JSmith
  • 4,519
  • 4
  • 29
  • 45