Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require mode, theme or addon of code mirror in browserify

Has anybody try to use code mirror via browserify?

I find nothing is visible, even though it already generated all the html tags.

The code :

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 wonder how i should require the js mode?

like image 326
kikkpunk Avatar asked Sep 07 '14 23:09

kikkpunk


2 Answers

I actually dealed with that problem by using require() for all dependencies of the demonstration of html5complete mode demo like that:

// 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'
});

In my .less files, I imported the CSS like that:

@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 did not took really care about the quality of that trick.

like image 150
zeropaper Avatar answered Sep 21 '22 18:09

zeropaper


Here's what's working for me. Using import instead of require, but same gist:

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

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

...


<Codemirror ref="editor" value={this.props.value} onChange={this.props.updateCode} options={options}/>
like image 39
ekatz Avatar answered Sep 18 '22 18:09

ekatz