DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • AI, ML, and Data Science: Shaping the Future of Automation
  • Rethinking Recruitment: A Journey Through Hiring Practices
  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Create Your Own AI-Powered Virtual Tutor: An Easy Tutorial

Getting Started With Ace Editor

Here is a quick, handy guide for getting started with Ace editor.

By 
Ivan Nikitin user avatar
Ivan Nikitin
·
Nov. 15, 16 · Opinion
Likes (5)
Comment
Save
Tweet
Share
32.7K Views

Join the DZone community and get the full member experience.

Join For Free

Getting Started With Ace Editor

Ace is a high-performance code editor for the web. Ace is written in JavaScript and you can embed it into your web pages should you need code editing capabilities. It cannot match the performance of native text editors like Sublime but it’s fast enough for occasional code editing. We used it as a JavaScript editor in Xenarius, our mobile app builder, and now I’m going to explain how you can use Ace in your own project.

Start by cloning the ace repository. You will need 4 files from it.

  • ace.js – it’s the main file of the Ace library,
  • ext-language_tools.js – provides the text autocomplete feature,
  • theme-chrome.js – defines editor styles,
  • mode-javascript.js and worker-javascript.js –contain JavaScript validator and highlighter.

Add these files to your project and link the *ace.js* file in your HTML page. Other files will be loaded automatically.

In case you’re using Browserify or Webpack, Thorsten Lorenz made a Browserify-compatible version of Ace. You can find it on GitHub: https://github.com/thlorenz/brace

Once the files are loaded, create a <div>HTML element and initialize Ace in it.

var aceEditor = window.ace.edit(divElement);

Now, you can use the aceEditor variable to access and configure Ace editor.

Configuring Ace

When you configure Ace, keep in mind that some Ace options should be set for a specific Ace instance, while others – for all instances at once.

aceEditor.setOptions({
   enableBasicAutocompletion: true, // the editor completes the statement when you hit Ctrl + Space
   enableLiveAutocompletion: true, // the editor completes the statement while you are typing
   showPrintMargin: false, // hides the vertical limiting strip
   maxLines: Infinity,
   fontSize: "100%" // ensures that the editor fits in the environment
});

// defines the style of the editor
aceEditor.setTheme("ace/theme/chrome");
// hides line numbers (widens the area occupied by error and warning messages)
aceEditor.renderer.setOption("showLineNumbers", false); 
// ensures proper autocomplete, validation and highlighting of JavaScript code
aceEditor.getSession().setMode("ace/mode/javascript_expression");

Shared Ace for the Inline Mode

In the inline mode, the Ace editor gets activated each time you click a text. And it is always the same instance of the editor as it was the first time. Furthermore, all inline elements will use the same instance.

Initialized in the beginning, this instance resides in a special parking place waiting for a user to call the editor. When it happens, the instance gets relocated to the appropriate place in the DOM, and gets back after the user closes the editor. A shared instance can be accessed using a closure reminiscent of the singleton design pattern.

var getSharedAce = function() {
    var sharedAce;
    return () => {
        if(!sharedAce) {
            var $sharedAceHolder = $("<div id='shared-ace-holder'>").hide().appendTo(document.body);
            sharedAce = createAce($sharedAceHolder);
        }
        return sharedAce;
    };
} ();

KnockoutJS Integration Specifics

If you’re using KnockoutJS in your app, you may face a caveat. Ace is bound to the model with a custom binding handler. Its update method relocates the instance to the parking place and back, and subscribes/unsubscribes from the text change event. Note that Ace subscribes to this event on its own, which means that calling  ace.off("change") without specifying the handler leads to rendering issues.

Custom Autocomplete Rules

In our product, we use Ace when editing inline expressions, not actual JavaScript files. Unfortunately, standard error-checking, highlight and autocomplete libraries don’t fit that purpose well. For example, Ace would show an error if there is no semicolon at the end of the line, while our expressions are perfectly valid without it. We had to dig into error-checker and highlighter to solve these problems.

We wanted the autocompleter to show expression context variables. To this end, we needed to populate its dictionary with custom keywords. If you need to use custom autocomplete rules in Ace like we do, add a custom completer to the language_tools ace extension (found in ace/ext/language_tools.js). But there is a limitation: all Ace instances use a single completer. To work around this limitation, we cache rule sets and choose the appropriate one in the completer using a closure.

var ace = window["ace"],
langTools = ace.require("ace/ext/language_tools"),
contextCompleter = {
getCompletions(editor, session, pos, prefix, callback) {
var dataHolder = AppPlayer.findInArray(dataHolders, holder => holder.editor === editor);
callback(null, dataHolder && dataHolder.data);
}
};
    langTools.addCompleter(contextCompleter);

Thus, you can use different autocomplete rule sets inside different operations while having only one Ace instance and a single completer. The screenshot below shows autocomplete popup with our custom variables we added into it.

Autocomplete and Syntax Highlighting in JavaScript Mode

You can find the dictionary of highlighted keywords in the mode-javascript.js file in the keywordMapper variable. Autocomplete uses the same dictionary as well.

We started by removing unsupported keywords from the list. Also, we introduced a special mode for JavaScript statements. This mode disables several rules, like the requirement that every expression should end with a semicolon. To implement this mode, we created new "-mode" and "-worker" files, based on the existing "javascript-mode.js" and "worker-javascript.js" files. We’ve just removed checks that aren’t necessary for our product. If you’re making your own Ace mode, don’t forget to change the mode name at the end of the mode file:

ace.define("ace/mode/your-name-here", ...

The screenshot below shows that Ace no longer shows a warning about a missing semicolon.

Not too complex, is it?

ACE (editor)

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!