<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dinkar Sharma</title>
    <description>The latest articles on DEV Community by Dinkar Sharma (@dinkarsharma).</description>
    <link>https://dev.to/dinkarsharma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1134638%2F22d86a59-8c80-4954-a53b-436beaaf4848.jpeg</url>
      <title>DEV Community: Dinkar Sharma</title>
      <link>https://dev.to/dinkarsharma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dinkarsharma"/>
    <language>en</language>
    <item>
      <title>Codemirror with Vue 2: Addons</title>
      <dc:creator>Dinkar Sharma</dc:creator>
      <pubDate>Sun, 13 Aug 2023 16:21:00 +0000</pubDate>
      <link>https://dev.to/dinkarsharma/codemirror-with-vue-2-addons-4il9</link>
      <guid>https://dev.to/dinkarsharma/codemirror-with-vue-2-addons-4il9</guid>
      <description>&lt;p&gt;In the world of web development, integrating powerful tools into your projects can greatly enhance the user experience and streamline your workflow. If you're currently utilizing a fundamental CodeMirror setup within the Vue 2 framework (if you haven't, you can find a guide on setting up CodeMirror with Vue 2 &lt;a href="https://dev.to/dinkarsharma/codemirror-with-vue-2-539f"&gt;here&lt;/a&gt;), you might have encountered a common predicament. Unlike the conventional code editors you're accustomed to, the integrated code editor might not be exhibiting the anticipated behavior. This discrepancy can lead to frustration, but fear not, as this post will delve into the intricacies of this issue and provide insights on how to overcome it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Following are some of the functionalities that you should really add to your code editor:&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Auto Close Brackets:
&lt;/h3&gt;

&lt;p&gt;Brackets should be automatically closed while typing. This can be done as following: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First import closebrackets addon, like this:
&lt;code&gt;import 'codemirror/addon/edit/closebrackets.js'
&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then add autoCloseBrackets inside configuration options like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Codemirror.fromTextArea(document.getElementById('codeEditor'), {
            lineNumbers: true,
            line: true,
            theme: 'dracula',
            tabSize: 4,
            mode: `text/javascript`,
            autoCloseBrackets: true,
        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;That's it now autoCloseBrackets functionality is enabled in your code editor&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Match Tags and Auto Close Tags:
&lt;/h3&gt;

&lt;p&gt;If you are enabling a language which includes tags like HTML then these are useful. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First import them from addons folder:&lt;br&gt;
&lt;code&gt;import 'codemirror/addon/edit/matchtags.js'&lt;/code&gt;&lt;br&gt;
&lt;code&gt;import 'codemirror/addon/edit/closetag.js'&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then add them to configuration options:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Codemirror.fromTextArea(document.getElementById('codeEditor'), {
            lineNumbers: true,
            line: true,
            theme: 'dracula',
            tabSize: 4,
            mode: `text/javascript`,
            autoCloseBrackets: true,
            matchTags: true,
            autoCloseTags: true,
        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly you can add many more addons. Now that you know how you can include addons to your code editor, you should go through this part of the documentation: &lt;a href="https://codemirror.net/5/doc/manual.html#addons"&gt;Link&lt;/a&gt;, here all the other addons are given with there details.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>vue</category>
      <category>codemirror</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Codemirror with Vue 2 : Setup</title>
      <dc:creator>Dinkar Sharma</dc:creator>
      <pubDate>Fri, 11 Aug 2023 17:29:56 +0000</pubDate>
      <link>https://dev.to/dinkarsharma/codemirror-with-vue-2-539f</link>
      <guid>https://dev.to/dinkarsharma/codemirror-with-vue-2-539f</guid>
      <description>&lt;h3&gt;
  
  
  Codemirror 6+ is not compatible with vue 2.7 and below, so we'll be using Codemirror 5 which is quite easy to setup with Vue 2
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First we'll install codemirror 5 using npm or yarn like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install codemirror@5.65&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn add codemirror@5.65&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Then we'll create a new file (lets say, CodeEditor.vue)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then inside template we'll add textarea tag with id and v-model... Something like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;textarea v-model="code" id="codeEditor"&amp;gt;&amp;lt;/textarea&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
   data(){
     return {
        code:'console.log("hello world")',
     }
   }
}
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Then we'll import codemirror into the file and update the textarea to codemirror... also don't forget to import css file from codemirror as it is required&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also import a mode (like here we are importing javascript) as it will be responsible for language styling according to the type of language&lt;br&gt;
(I have also added how you should add mode in configuration options further down)&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
import * as Codemirror from 'codemirror'
import 'codemirror/lib/codemirror.css'
import 'codemirror/mode/javascript/javascript.js'

export default {
   data(){
     return {
        code:'console.log("hello world")',
     }
   },
   mounted(){
      Codemirror.fromTextArea(document.getElementById('codeEditor'), {

            lineNumbers: true,
            line: true,
            tabSize: 4,
            mode: 'text/javascript',

        })
   }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;That's it you have successfully integrated Codemirror to Vue 2&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Now let's add some basic customizations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Let's first add a theme for the codeEditor&lt;/li&gt;
&lt;li&gt;For dracula 🧛🏻 theme, import this theme using following import statement: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;import 'codemirror/theme/dracula.css'&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Then add theme in configuration options like this:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      Codemirror.fromTextArea(document.getElementById('codeEditor'), {

            lineNumbers: true,
            line: true,
            tabSize: 4,
            mode: 'text/javascript',
            theme: 'dracula',

        })
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;There are many themes that you can choose from... I'll recommend you to go to node_modules &amp;gt; codemirror &amp;gt; theme folder here you'll find all the themes that you can apply.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Now lets find out how to add correct mode value in configuration options
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You should go to node_modules &amp;gt; codemirror &amp;gt; mode folder, here you'll find all the languages that can be added.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should know that for C, C++, Java and few more languages you need to import clike... like this:&lt;br&gt;
&lt;code&gt;import 'codemirror/mode/clike/clike.js'&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But the way these modes are added to configuration options are different.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example if you want to add C to your codemirror then you'll need to add this to your configuration options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Codemirror.fromTextArea(document.getElementById('codeEditor'), {

            lineNumbers: true,
            line: true,
            tabSize: 4,
            mode: 'text/x-csrc',
            theme: 'dracula',

        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Now let's see how you'll know which is the right value for mode:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First open this link on new tab: &lt;a href="https://codemirror.net/5/mode/index.html"&gt;link to Codemirror 5&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here you'll find all the different languages supported by codemirror 5&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find your language, in our case click on ' C, C++, C# '&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here you'll find the mode value you need to use at bottom of the page&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  There are still many more customizations that you can do, like adding 'autoCloseBrackets' functionality to your codemirror.
&lt;/h4&gt;

&lt;p&gt;For that I'll add another post and add its link: &lt;a href="https://dev.to/dinkarsharma/codemirror-with-vue-2-addons-4il9"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>codemirror</category>
      <category>vue</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
