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

Having trouble rendering a dynamic table with JavaScript utilizing a JSON object

I am struggling to retrieve data in JSON format and display it in a table. Despite trying various methods, I have been unable to make it work. Below is the code for this function. Can someone please assist me in identifying what is wrong with it? As of now ...

JavaScript form button press tracker

Hello! I've been tackling a challenge involving a JavaScript function to count button clicks. The catch is, the button type is set to submit, causing the page to reload every time I click it. Below is the snippet of code that contains the problemati ...

Discovering the Newest Product Updates through API Integration

I have a component that displays only the most recent product fetched from an API: const about = ({products}) => { const data = products.attributes console.log(data) return ( <div> <h1>{data.Name}</h1> ...

How can escape characters be utilized in the JavaScript split function?

Here are two examples that achieve the same result. I'm curious as to why Option 1 was included in a code example I came across instead of Option 2? What is the significance of the forward/backward slashes in '/\&/'? Option 1: ...

Encountering the "excessive re-renders" issue when transferring data through React Context

React Context i18n Implementation i18n .use(initReactI18next) // passes i18n down to react-i18next .init({ resources: { en: { translation: translationsEn }, bn: { translation: translationsBn }, }, lng: "bn ...

Update the HighChart Pie chart depending on the selection in the dropdown menu

Currently, I am working on creating a pie chart using data retrieved from a web socket in JSON format. Once the JSON object is returned, if the user selects the "Pie Chart" option, another Select dropdown will be displayed to choose a specific time period. ...

Generating unique ObjectIDs for each object is crucial before saving documents in Mongoose

I need to generate a unique ObjectID for every object within my array. The challenge is that I am fetching products from another server using a .forEach statement and adding them to my array without a Schema that automatically creates an ObjectID.... Prod ...

Choosing the state object name dynamically within a React JS component

I have a quick question about updating state in React. How can I change a specific object in a copy of the state that is selected using e.target.name and then set to e.target.value? For example, if I want to change newState.age when e.target.name = age i ...

jQuery live function is not functioning as anticipated

I am facing issues with ajax requests and simple <input type="submit"/>. I have a practice of loading views within other views, in a modular way, using jQuery's .load(url) function to move from one view to another. The problem arises when I loa ...

Retrieve a specific key value from a dictionary within a Flask function by employing JavaScript

I'm currently working on a feature where a user can input something in a search field, and upon submitting, the script should send a request to a Flask function using JavaScript. The response data should then be loaded accordingly. However, I've ...

Every time I hit the refresh button, I find myself forcefully logged out

After switching from using localStorage to cookies in my React JS web app, I am experiencing an issue where I get logged out whenever I refresh the page. Even though the cookies are still stored in the browser, the authentication process seems to be failin ...

Guide on how to create a custom response using class-validator in NestJS

Is it feasible to customize the error response generated by class-validator in NestJs? The default error message structure in NestJS looks like this: { "statusCode": 400, "error": "Bad Request", "message": [ { "target": {} ...

Tips for creating a static background when displaying a modal popup in AngularJS

Incorporating a modal popup to modify a row within a grid view has been my recent task. Leveraging the row.getProperty() function, I successfully extracted the row values within the modal. However, an inconvenience emerged when attempting to edit a value ...

Utilize text wrapping to ensure a fixed maximum height for content display

I am in need of a div that contains text spanning multiple lines, with both a fixed width and a maximum height. Currently, I have applied the CSS property overflow: hidden;. However, my issue arises when the last line of text exceeds the maximum height of ...

Ways to store a filestream coming from Node.js into AngularJS

When using my express server, I have a post-request set up to retrieve a pdf file from Amazon S3 and then send it back to Angular. This is the endpoint in my express server: var fileStream = s3.getObject(options).createReadStream(); fileStream.pipe(res); ...

Exploring Angular 4: Iterating Over Observables to Fetch Data into a Fresh Array

Context Currently, I am in the process of developing a find feature for a chat application. In this setup, each set of messages is identified by an index. The goal of the `find()` function is to retrieve each message collection reference from the `message ...

Can anyone tell me the best way to access the name attribute of an HTML element in TypeScript?

Currently, my code is utilizing the name attribute to verify whether the user has entered information in a specific field and validating the input. However, I am facing an issue where the submit button remains active even if there are empty fields presen ...

Adjusting the position of a stationary element when the page is unresponsive and scrolling

Managing a large web page with extensive JavaScript functionality can be challenging, especially when dealing with fixed position elements that update based on user scroll behavior. A common issue that arises is the noticeable jumping of these elements whe ...

Slide in parts gradually by scrolling up and down, avoiding sudden appearance all at once

I have implemented a slider on my website using jQuery functions. For scrolling down, the following code snippet is used: jQuery("#downClick").click(function() { jQuery("html, body").animate({ scrollTop: jQuery(document).height() }, "slow"); ...

What is the best way to update a deeply nested array of objects?

I have an array of objects with nested data that includes product, task, instrument details, and assets. I am attempting to locate a specific instrument by supplier ID and modify its asset values based on a given number. const data = [ { // Data for ...