Is there a way to incorporate the ACE code editor into a Vue project without relying on its built-in Vue

Just starting out with Vue and I'm looking to incorporate the ace code editor (this package) into my project. However, I want to avoid using the vue2-component & vue3-component versions for learning purposes. How can I achieve this?

What's the reasoning behind it?

As someone who has been programming since before the jQuery era, I am now catching up on modern web development by diving into Vue. I've noticed that many Vue component packages are not as updated as their plain JS library counterparts. My goal is to understand how to incorporate non-vue libraries into Vue.

UPDATE:

Following the first suggestion, the editor is functional but the syntax highlighting & themes are not displaying properly in the code editor. It seems like there may be an issue with loading or applying stylesheets.

The console is showing some errors that I am not sure how to address.

https://i.stack.imgur.com/Cmm16.png

UPDATE 2:

https://i.stack.imgur.com/0SKkb.png

Answer №1

Integrating third-party libraries into Vue is a breeze. If you're using a package manager like npm to add the ace-code library, simply install it and import it into the desired component. Let's replicate the initial example from the ace documentation.

Ace Code Component:

AceCode.vue

<script setup lang="ts">
import { onMounted } from "vue";
import ace from "ace-code";
onMounted(() => {
  ace.edit("editor");
});
</script>

<template>
  <div id="editor"></div>
</template>

<style scoped>
#editor {
  position: absolute;
  width: 500px;
  height: 400px;
}
</style>

As shown, the css and html remain the same, but the logic requires slight adjustments. Ensure that Vue renders the HTML before calling the edit method. This can be achieved by utilizing the onMounted method provided by Vue.

Answer №2

After attempting to implement the code provided by Andres Abadia, I am still encountering an error:

loader is not configured

(for those using JavaScript, remember to remove the lang="ts" from the script tag)

Despite ace-code working, there seems to be a problem with loading themes. Why is this happening?

https://i.stack.imgur.com/kcnA1.png

The issue lies in utilizing the raw files of the ace-code package as if they were meant for a standalone framework environment. If you wish to incorporate highlights or other features from it, you must load additional scripts via CDN individually, leading to potential conflicts with the defined key. My suggestion is to directly use the required package which is ace-builds, accompanied by all the generated files (I can provide snippets for Vue2 & Vue3). The package includes a specific webpack-resolver from the Ace team that enables your loader (Webpack in this case, otherwise Vite might throw a different error) to efficiently load and interpret all the necessary files. By following this approach along with the provided snippets, you can effectively work with the external library Ace code.

Remember to install file-loader as a dev dependency to facilitate loading the generated file from the ace-builds package.

You may still encounter require errors due to the library's reliance on require statements. With the information provided, consider employing a loader or transpiler like Babel to translate from CommonJS to ES6.

https://i.stack.imgur.com/wL805.png

For Vue2:

<template>
  <div class="ace-container">
    <!-- ID is used in official documents, but refraining from its use here avoids potential packaging issues later on; instead, utilize ref or DOM -->
    <div class="ace-editor" ref="ace"></div>
  </div>
</template>

<script>
import ace from'ace-builds'
import'ace-builds/webpack-resolver'
import'ace-builds/src-noconflict/theme-monokai'
import'ace-builds/src-noconflict/mode-javascript'

export default {
  mounted () {
    this.aceEditor = ace.edit(this.$refs.ace, {
      maxLines: 20,
      minLines: 10,
      fontSize: 14,
      theme: this.themePath,
      mode: this.modePath,
      tabSize: 4
    })
  },
  data () {
    return {
      aceEditor: null,
      themePath:'ace/theme/monokai',
      modePath:'ace/mode/javascript'
    }
  }
}
</script>

<style scoped>
.ace-editor {
  width: 100%;
  height: 400px;
}
</style>

For Vue3:

<template>
  <div class="ace-container">
    <!-- Similar to Vue2, avoid using ID and opt for ref or DOM to prevent future packaging problems -->
    <div id="editor"></div>
  </div>
</template>

<script setup>
import {onMounted} from "vue";
import ace from "ace-builds";
import 'ace-builds/webpack-resolver'
import 'ace-builds/src-noconflict/theme-clouds';
import 'ace-builds/src-noconflict/mode-latex';


onMounted(() => {
  ace.edit('editor', {
    maxLines: 20,
    minLines: 10,
    fontSize: 14,
    theme: 'ace/theme/monokai',
    mode: 'ace/mode/javascript',
    tabSize: 4
  })
});
</script>

<style scoped>
#editor {
  width: 100%;
  height: 400px;
}
</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In Typescript with Vue.JS, the type 'Users[]' does not include the essential properties of type 'ArrayConstructor' such as isArray, prototype, from, of, and [Symbol.species]

Embarking on my journey with typescript and vuejs, I stumbled upon a perplexing error that has halted my progress for the past 48 hours. The error message reads as: Type 'Users[]' is missing the following properties from type 'ArrayConstruct ...

Modify the database once authorized by the administrator

`I have a webpage that showcases testimonials. I am looking to only display the testimonials with a "status" of "1" in my database. How can I quickly update the "status" column from "0" to "1" right after the admin clicks on update? I am also incorporati ...

Chosen option within the AntD Select component

Using the AntD select component, there seems to be an issue where it displays "id" instead of "name" when selecting options. Is there a solution available for this problem? const MyComponent = () => { const data = [ { id: "5c83c2d ...

Changing a file object into an image object in AngularJS

Can a file object be converted to an image object? I am in need of the width and height from the file (which is an image). Here is my code: view: <button id="image_file" class="md-button md-raised md-primary" type="file" ngf-select="uploadFile($file ...

My attempts to load the .mtl and .obj files using THREE.js have been unsuccessful

I've been working on creating a 3D model viewer using three.js, but I'm encountering issues with loading .mtl and .obj files. Despite following a tutorial at , the only model that loads successfully is the female one. Here is the code snippet I ...

"Access denied" error when using Internet Explorer and jQuery

Attempting to make an AJAX call using jQuery's $.post in Internet Explorer is resulting in an error message stating "Permission denied." This peculiar issue only seems to occur when accessing a page after having visited another page. For example, if ...

Only if there is an update in the SQL database, I wish to refresh the div

I have created a voting system and I am facing an issue with the data updating. I have implemented a setInterval function in javascript to load the data every 2 seconds, but it's not working as expected. There are no errors shown, but the data is not ...

Utilizing Laravel Web and API Routes for an Application integrated with VueJS on the front end

Our team is in the process of building a multi-page application with VueJS as our frontend JavaScript framework and Element.io as our CSS framework. We are not currently exposing any web services or APIs. The application we are developing is responsive, ...

Is there an implicit transformation or action involved when the promise chain is resolved or rejected in $q?

Is there an intrinsic digest/apply included when the promise chain is resolved/rejected in $q? I recently switched from using $q to q in my code and it seems like a digest is no longer happening, leading to different outcomes. What could be causing this c ...

Issues with premature execution of Angular JS code have been observed at times

Currently facing an issue with my code where the updateProduct() function call is happening before the forEach loop begins running on about 1 out of every 10 page loads. Not sure why this is occurring or how to solve it. Any insights on what might be causi ...

Are there any discussions underway about updating JSON standards to officially permit unquoted property names?

Technically speaking, the following JSON is considered invalid: { color: "blue", size: 14 } This is because according to the specification, property names like "color" and "size" should be enclosed in quotes, like this: { "color": "blue", "size" ...

Move the DIV element to a static section within the DOM

In my Vue app, I have implemented methods to dynamically move a DIV called 'toolbox' to different sections of the DOM. Currently, the DIV is positioned on the bottom right of the screen and remains static even when scrolling. My goal is to use t ...

Is Angular 2+ responsible for loading the entire module or only the exported components within it?

I'm dealing with a situation where I have a large module but only need to export one specific component. I'm wondering if Angular loads the entire module or just the exported components, as I want to optimize performance without compromising the ...

Sharing data between React JS components Passing information between different components in React JS

My NavBar.js component contains login information for the logged-in user. It displays "Welcome" along with the user details when the user is logged in. Now, I want to replicate this functionality in the ProductList.js component so that when a user posts a ...

Is it possible for the Vue computed function to use destructuring assignment for the parameter even when no arguments are provided?

new Vue({ el: "#app", data: { value: "text", }, computed:{ all: function({value}){ return value } } }); <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"> ...

Creating a toggle button for a div element to expand and collapse its content: Step-by-step guide

For my e-commerce site selling musical instruments, I'm designing a product landing page that showcases guitars, keyboards, violins, and cellos. As part of the design, I want to include expandable sections with detailed information about each instrume ...

Variables in the $scope object in AngularJS

Within my $scope in angularJS, I am working with two types of variables. 1). $scope.name and $scope.title are linked to input boxes in the UI html code. 2). On the other hand, $scope.sum and $scope.difference are internally used within my JS code. I need ...

Issue: jQuery Datatable not functioning properly while attempting to populate the table through JavaScript.Explanation: The

I'm currently working on populating a table using JavaScript. However, I've encountered an issue where the table is being populated but it shows "No data available in table". Furthermore, when I try to interact with the data, it disappears. This ...

In Stripe.js, the background color and height of the credit card input cannot be customized

I'm struggling with customizing the appearance of a Stripe credit card input within my Vue.js application. I want to adjust the background color to #f1f1f1 and set the height to 60px, but despite trying both base styles and css, I can't seem to g ...

Steps to temporarily turn off Backbone.sync for a fresh model and then reactivate it once the user clicks the save button

I am currently working with a Backbone collection model that consists of sub-models as elements, along with views to edit it. My objective is to have the syncing functionality "turned off" initially when the model is created, so that the back end is not c ...