Using Browserify to Import CodeMirror Modes, Themes, and Addons

Has anyone attempted to utilize Code Mirror through browserify?

I can't see anything on the screen, even though all the HTML tags have been generated.

The code snippet is as follows:

var CodeMirror = require('codemirror');
require('codemirror/mode/javascript/javascript.js');

  var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
    lineNumbers: true,
    extraKeys: {
      "Ctrl-Space": "autocomplete"
    },
    mode: {
      name: "javascript",
      globalVars: true
    }
  });

I am wondering how I should include the JavaScript mode properly.

Answer №1

When faced with the issue, I resolved it by utilizing require() to handle all dependencies in showcasing the html5complete mode demo. Here's how I approached it:

// require('codemirror/addon/hint/show-hint');
// require('codemirror/addon/hint/xml-hint');
// require('codemirror/addon/hint/html-hint');

require('codemirror/mode/xml/xml');
require('codemirror/mode/javascript/javascript');
require('codemirror/mode/css/css');
require('codemirror/mode/htmlmixed/htmlmixed');

var CodeMirror = require('codemirror/lib/codemirror');

var editor = CodeMirror.fromTextArea(textareaElement, {
  mode: 'text/html',
  lineWrapping: true,
  extraKeys: {
    'Ctrl-Space': 'autocomplete'
  },
  lineNumbers: true,
  theme: 'monokai'
});

For my .less files, I imported the CSS in this manner:

@import (inline) "./../../node_modules/codemirror/lib/codemirror.css";
@import (inline) "./../../node_modules/codemirror/theme/monokai.css";
// @import (inline) "./../../node_modules/codemirror/addon/hint/show-hint.css";

I didn't pay much attention to the quality of this workaround.

Answer №2

Here's an approach that has been effective for me. I'm utilizing the import statement instead of require, but the concept remains the same:

import 'codemirror/theme/3024-night.css'

const settings = {
      lineNumbers: true,
      readOnly: false,
      mode: 'htmlmixed',
      theme:'3024-night'
};

...

<Codemirror ref="editor" value={this.props.content} onChange={this.props.updateContent} settings={settings}/>

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

Unique phrase: "Personalized text emphasized by a patterned backdrop

I'm facing a challenge and struggling to find a way to highlight text using CSS or jQuery. My goal is to have an image on the left, another one on the right, and a repeated image in between. Since there are some long words involved, I need a dynamic s ...

Techniques for finding the total value of a JSON array

After retrieving values from a database, I am using them in JSON/javascript as an array. For example, see https://i.sstatic.net/nLzk9.png The issue arises when trying to calculate the sum of the array elements. I attempted to solve it with this code: v ...

Expanding the capabilities of jQuery UI event handling

I am looking for a way to enhance dialog functionality by automatically destroying it when closed, without the need to add additional code to each dialog call in my current project. My idea is to override the default dialog close event. After researching ...

Steps for building a react-admin app using the tutorial

Currently, I am using Ubuntu and attempting to develop a react-admin application. The command I executed was "yarn create react-admin test-admin", selecting JSON Server as the data provider and None as the authentication provider. I did not add any resour ...

Having trouble with the Three.js OBJ loader in CodePen?

Currently, I am experiencing a challenge with loading an OBJ file on Three.js. Oddly enough, the functionality seems to be working perfectly fine when I deploy the files on my server as demonstrated here: However, when attempting to run it on Codepen, I e ...

Getting variables from different functions in Python can be achieved by using the return

I am trying to implement a feature where I can fetch a search term from the function getRandomVideo() and then use it in a jQuery statement. For example, if I get "Beethoven" as the search term from the variable searches, I want to use it to retrieve JS ...

Developing interactive checkboxes for individual rows through React.js

Within a form, I have rows containing two inputs each. Upon clicking "add", a new row is created. For the second row onwards, by clicking "add" a checkbox labeled 1 should be added to indicate dependency on the previous row. In addition, for the third row, ...

best way to retrieve nested objects in react components

I am struggling to retrieve the price value in my react component. I have successfully used map to access object properties like images and id, but encountering issues when trying to access the price value. The error message I'm receiving is as follow ...

AngularJS dropdown with multiple selections within a Bootstrap modal

Being new to AngularJS, I apologize for asking this question in advance. I am utilizing Angular bootstrap modal from the following link: https://angular-ui.github.io/bootstrap/ and multiple select dropdown list from: https://codepen.io/long2know/pen/PqLR ...

What is the best way to access the EXIF data of an image (JPG, JPEG, PNG) using node.js?

In my quest to access the EXIF data of an image in order to extract GPS information such as Latitude and Longitude, I have experimented with approximately 4-5 EXIF packages available on npm/node, including exif, exif-parser, node-exif, exifr, exif-js, and ...

Rotating an Object3D in Three.js using an axis rotation technique

I am attempting to change the orientation of a mesh that has been loaded into an Object3D using OBJMTLLoader. var obj = new THREE.Object3D(); // loading process... obj.rotation.y += 0.1; //executed within the update function This method works correctly ...

Is there a way to automatically update the input value when I refresh the page following a click event?

Currently, I have multiple p elements that trigger the redo function upon being clicked. When a p element is clicked, the quest[q] template is loaded onto the .form div. These templates are essentially previously submitted forms that sent values to an obj ...

The command 'postcss' is not compatible and cannot be recognized as an internal or external command, operable program, or batch file

Even though I have postcss installed, I'm encountering the error above in the terminal during the build process. I am using this setup with tailwindcss and my postcss.config.js file is structured as follows: module.exports = { plugins: [ r ...

Create a clickable element that triggers an action for a brief period of time before stopping automatically, using JavaScript, HTML, and CSS

Looking for a way to implement buttons or toggles that activate a feature for 3-5 seconds after the user selects them, without requiring further interaction. Any suggestions on how to achieve this using CSS, HTML, and JS? I currently have toggles in place ...

Having difficulty updating the state with editorState retrieved from the server in ReactJs when using draftJs

Incorporating a rich text editor into React using draftJs has been successful. The editorState data is converted to raw data, stringified, and sent to the server for rendering on the front end. However, I am facing challenges in updating the editorState ...

Add the onclick() functionality to a personalized Angular 4 directive

I'm facing an issue with accessing the style of a button in my directive. I want to add a margin-left property to the button using an onclick() function in the directive. However, it doesn't seem to be working. Strangely, setting the CSS from the ...

another solution instead of using several try-catch blocks within a function

Consider a scenario where we define a function as shown below: async function doSomethingWithFriends() { let user, friends, friendsOfFriends = null; try { user = await getUser(); } catch(err){ return [401, err] } try { ...

Creating 3D extrusions with Three.js by specifying depth and a direction vector

My goal is to extrude a shape and generate an ExtrudeGeometry, but I need the ability to control the direction of the extrusion. Specifically, I have a direction specified in a Vector3. The shape is initially drawn on the x-y plane, with the default extru ...

Looking to utilize JavaScript or jQuery to call a web service without the need for any C# code

I need to access an asp.net web service using only JavaScript or jQuery without any C# code. I have created a simple web service as shown below: [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] ...

Changing the dynamic boundaries does not produce any results in the 'react-leaflet' library

Encountered an issue while trying to utilize the react-leaflet's bounds property. In my scenario, the application has predefined initial bounds in the state (referred to as fallback bounds) which are passed during the first component render. The archi ...