22
loading...
This website collects cookies to deliver better user experience
"ide
". Yeah, just three letters, we don't want a complicated name. Before we start, let's look at some common configurations which we can set."The language, whether to show line numbers, theme, whether the editor should be read only."
Open your text editor and type the following insidefunction ide(language, lineNumbers = true, readOnly = false, theme = "default"){
//Our JavaScript function to simplify CodeMirror
var textareas = document.getElementsByClassName(language); //Get all elements having a class name of the language.
for (let i = 0; i < textareas.length; i++){
CodeMirror.fromTextArea(textareas[i], {
mode : language,
theme : theme,
lineNumbers : lineNumbers,
readOnly : readOnly
});
}
}
ide
which takes four (4) parameters. The parameters are language, lineNumbers, readOnly, theme
. The language parameter refers to the programming language which you want to use.lineNumbers
refers to whether the editor should show line numbers, it's set to true by default. readOnly
parameter is to specify whether the text editor should be read-only or not, it's set to false by default. The theme refers to the theme which you want to use for your text editor, it's set to default by default.textareas
. It's an array containing elements with class name "programming language"
. Then we iterate through the elements and create CodeMirror instances for each element and apply the parameters to the instance.<DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="codemirror/lib/codemirror.js"></script>
<link rel="stylesheet" type = "text/css" src="codemirror/lib/codemirror.css">
<script> type="text/javascript" src="script.js"</script>
<!-- Since, we are using python to test our function, we will import it's file -->
<script type="text/javascript" src="codemirror/mode/python/python.js"></script>
<title>CodeMirror Editor </title>
</head>
<body>
<h1>Hello World</h1>
<textarea class="python">
def function():
for i in range(1, 10):
print(i)
</textarea>
<script>
ide("python"); //Call our function to initialize CodeMirror on the textarea with class "python"
</script>
</body>
</html>
ide("python")
. This is the code which is going to turn the textarea into an editor. Yeah, it's just one line. Even, if we were to have multiple textareas with class of python, this single line of code will convert them all into editors. When we were created our function, we said it takes four parameters right? Yeah it works without them because they are set by default but if you want more control, you can add the parameters. ide("python", lineNumbers = false)
or if we wanted to make our editor read-only, we can write our parameters as ide("python",lineNumbers = true, readOnly = True)
or better still, if we wanted to add a theme, we can write the parameters as ide("python", lineNumbers = true, readOnly = False, theme = "dracula")
.Note that, if we set a theme, we must import the theme which we set, so for a text editor with a theme of Dracula, we will add this to our head element <link type="text/css" rel="stylesheet" href="codemirror/theme/dracula.css">
. 22