<?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: Mohit Sehgal</title>
    <description>The latest articles on DEV Community by Mohit Sehgal (@mohitsehgal).</description>
    <link>https://dev.to/mohitsehgal</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%2F702116%2F1458fe69-62ea-4575-beea-521e21a5245d.png</url>
      <title>DEV Community: Mohit Sehgal</title>
      <link>https://dev.to/mohitsehgal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohitsehgal"/>
    <language>en</language>
    <item>
      <title>How to use CKEditor and its plugins with Nuxt.js</title>
      <dc:creator>Mohit Sehgal</dc:creator>
      <pubDate>Sat, 20 Nov 2021 14:06:04 +0000</pubDate>
      <link>https://dev.to/mohitsehgal/how-to-use-ckeditor-and-its-plugins-with-nuxtjs-1ojm</link>
      <guid>https://dev.to/mohitsehgal/how-to-use-ckeditor-and-its-plugins-with-nuxtjs-1ojm</guid>
      <description>&lt;p&gt;CKEditor is Javascript based rich text editor. It has clean UX loaded with features makes it a no-brainer choice for your next custom Javascript CMS.&lt;br&gt;
It can be tedious to figure out its integration with Vue.js Framework like Nuxt.js&lt;br&gt;
&lt;strong&gt;Let's jump straight to steps.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Create Nuxt App
&lt;/h2&gt;

&lt;p&gt;If you already have ongoing project, then you can skip this step. &lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;create-nuxt-app&lt;/code&gt; package using npx.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-nuxt-app ckeditor-nuxt-sample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Choose options suitable to you, here are my selection for this article.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjs6n1916un11r7c1vyxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjs6n1916un11r7c1vyxf.png" alt="Create Nuxt App for integrating CKEditor"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Create Page where you want to use CKEditor
&lt;/h2&gt;

&lt;p&gt;Create file named &lt;code&gt;sample-editor.vue&lt;/code&gt; in &lt;code&gt;pages&lt;/code&gt; directory in your Nuxt project. You can name it the way you want.&lt;/p&gt;

&lt;p&gt;Here is initial code in the file.&lt;br&gt;
&lt;/p&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;h1&amp;gt;Sample Editor will go on this page.&amp;lt;/h1&amp;gt;
&amp;lt;/template&amp;gt;
&amp;lt;script&amp;gt;
export default {}
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now see this page at &lt;a href="https://localhost:3000/sample-editor" rel="noopener noreferrer"&gt;https://localhost:3000/sample-editor&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Packages
&lt;/h2&gt;

&lt;p&gt;Install these packages for CKEditor and full build.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @ckeditor/ckeditor5-vue@23.0.0 --save
npm i @blowstack/ckeditor5-full-free-build@1.0.2 --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Initiate CKEditor and its config
&lt;/h2&gt;

&lt;p&gt;The second package mentioned above has CKEditor build contains all the free plugins for CKEditor. Thanks to BlowStack.&lt;br&gt;
Initialize CKEditor and Build in script section of your vue component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let FullFreeBuildEditor;
let CKEditor;
if (process.client) {
  FullFreeBuildEditor = require('@blowstack/ckeditor5-full-free-build');
  CKEditor = require('@ckeditor/ckeditor5-vue')
}else {
  CKEditor = { component : {template:'&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;'}}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note-  CKEditor can be used only on the client render and not server render hence &lt;code&gt;process.client&lt;/code&gt; check.&lt;/p&gt;

&lt;p&gt;Now you can register the component provided by this package in components section on your page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;components: {
    ckeditor: CKEditor.component
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next you need to pass FullFreeBuildEditor to editor prop of CKEditor component, so that it knows about which features to render.&lt;/p&gt;

&lt;p&gt;We first initialize &lt;code&gt;editor&lt;/code&gt; property in data section like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data() {
  return {
      editor: FullFreeBuildEditor,
  }
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we pass it to ckeditor as a prop. See snippet below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;ckeditor :editor="editor" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this you can see CKEditor like this&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3b6cvgqnvevtcfo2p8ka.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3b6cvgqnvevtcfo2p8ka.png" alt="Basic CKEditor in Nuxt.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Still this is not complete. &lt;br&gt;
How will you bind it to data property of your component? Use &lt;code&gt;v-model&lt;/code&gt;. Here's how.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;ckeditor :editor="editor" v-model="editorInput" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try to display the output just below the editor using following snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container mt-3"&amp;gt;
  &amp;lt;div class="row"&amp;gt;
    &amp;lt;h2 class="col-md-12"&amp;gt;Output&amp;lt;/h2&amp;gt;
      &amp;lt;div&amp;gt;{{editorInput}}&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll see something like this.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1awgn36o55cd7dz9bfv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1awgn36o55cd7dz9bfv.png" alt="Output for CKEditor in Nuxt.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to see preview of this output then you can use &lt;code&gt;v-html&lt;/code&gt; directive. Something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="container mt-3"&amp;gt;
  &amp;lt;div class="row"&amp;gt;
    &amp;lt;h2 class="col-md-12"&amp;gt;Preview&amp;lt;/h2&amp;gt;
      &amp;lt;div v-html="editorInput"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Edit Configuration
&lt;/h2&gt;

&lt;p&gt;The number of features which CKEditor supports can be overwhelming for your users. You can modify the look and limit the features if you want. For that &lt;code&gt;config&lt;/code&gt; prop of CKEditor comes into picture.&lt;/p&gt;

&lt;p&gt;Add new data property called &lt;code&gt;editorConfig&lt;/code&gt; to your component and add it as a prop to &lt;code&gt;ckeditor&lt;/code&gt; component. See the snippet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data(){
  // Other properties
  editorConfig: {
    width: 'auto',
    plugins: [
      'Bold',
      'Link',
      'List',
      'FontSize',
    ],
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CKEditor Line changes as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;ckeditor :editor="editor" :config="editorConfig" v-model="editorInput" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above 2 changes tells &lt;code&gt;ckeditor&lt;/code&gt; to only include &lt;code&gt;bold&lt;/code&gt;,&lt;code&gt;link&lt;/code&gt;,&lt;code&gt;list&lt;/code&gt;,&lt;code&gt;fontSize&lt;/code&gt; plugins and hence only these options. Here is the output.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxultaflt77batcgknq6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxultaflt77batcgknq6j.png" alt="Simple CKEditor after making config changes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can view full list of plugins &lt;a href="https://www.npmjs.com/package/@blowstack/ckeditor-nuxt" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now you have integrated CKEditor totally within your Nuxt.js project. &lt;br&gt;
You'd now see that your code for page component is little unclean. Let's see how to tackle this.&lt;/p&gt;
&lt;h2&gt;
  
  
  Refactor to separate component
&lt;/h2&gt;

&lt;p&gt;Now, we'll cleanup some code. Suppose in real world project, you'll need to use this rich editor at multiple pages. Then you should refactor the code into separate component. Let's call it &lt;code&gt;rich-editor&lt;/code&gt;.&lt;br&gt;
For that create &lt;code&gt;rich-editor.vue&lt;/code&gt; inside &lt;code&gt;components&lt;/code&gt; directory. We will encapsulate CKEditor code inside this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro Tip: If you do this refactor step. You can easily replace CKEditor with some other editor if needed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will move editor config to prop of this &lt;code&gt;rich-editor&lt;/code&gt; component. This will allow us to have rich-editor with different configuration and different features at every page where we need it.&lt;/p&gt;

&lt;p&gt;We will also move &lt;code&gt;value&lt;/code&gt; to prop, so that we can pass &lt;code&gt;v-model&lt;/code&gt; on the component and that variable will bind to the input of the rich-editor.&lt;/p&gt;

&lt;p&gt;Here is the code for &lt;code&gt;rich-editor.vue&lt;/code&gt;&lt;br&gt;
&lt;/p&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;ckeditor
    :editor="editor"
    :value="value"
    :config="config"
    @input="event =&amp;gt; $emit('input', event)"
  /&amp;gt;
&amp;lt;/template&amp;gt;
&amp;lt;script&amp;gt;
  let FullFreeBuildEditor;
  let CKEditor;
  if (process.client) {
    FullFreeBuildEditor = require('@blowstack/ckeditor5-full-free-build');
    CKEditor = require('@ckeditor/ckeditor5-vue')
  }else {
    CKEditor = { component : {template:'&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;'}}
  }

  export default {
    name: 'ck-editor',
    components: {
      ckeditor: CKEditor.component
    },
    props: {
      value: {
        type: String,
        required: false
      },
      config: {
       type: Object,
       required: false,
       default: function () {}
     }
    },
    data() {
      return {
        editor: FullFreeBuildEditor,
      }
    },
  };
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  MathType Plugins
&lt;/h2&gt;

&lt;p&gt;If you want to type Mathematics Equations or Chemistry Equations, then you need this plugin.&lt;br&gt;
You just need to add &lt;code&gt;MathType&lt;/code&gt; to the array of plugins in editor config prop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;editorConfig: {
  width: 'auto',
  plugins: [
    'Bold',
    'Link',
    'List',
    'FontSize',
    `MathType`
  ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all. Allow any complicated maths equations or chemical reactions into your Nuxt app. See Figure below.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57oesehmw8fldmnm1u9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57oesehmw8fldmnm1u9w.png" alt="CKEditor with MathType plugin in Nuxt.js"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Image Plugins
&lt;/h2&gt;

&lt;p&gt;Image plugin allows you to upload images into your editor but you need to give an REST Endpoint where images will be posted. This endpoint should return URL to the uploaded image. That URL can be used to store and display the image along with other content. Here's what you change in config.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//CKEditor Config for Image Upload
editorConfig: {
  width: 'auto', 
  plugins: ['Bold','Link','List','FontSize', `MathType`,`Image`,`ImageUpload`,'SimpleUploadAdapter'],
    simpleUpload: {
       // The URL that the images are uploaded to.
       uploadUrl: 'https://yourwebsite.com/api/upload-image',
    },
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep in mind &lt;code&gt;simpleUpload&lt;/code&gt; and &lt;code&gt;uploadUrl&lt;/code&gt; should be spelled correct in order for this to work. If you are facing any issues with this. Hit me up on DM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embed Plugin
&lt;/h2&gt;

&lt;p&gt;You can embed in video or social media link using &lt;code&gt;MediaEmbed&lt;/code&gt; plugin. Simply push this to plugins array and you have done it. Here is the sample screenshot.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0hdd80h4z7xift34mt1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0hdd80h4z7xift34mt1.png" alt="CKEditor with Embedded Youtube video in Nuxt.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We integrated CKEditor with our fresh Nuxt.js project. After that we refactored code and played around with some different useful plugins. This can be difficult to figure out but its very powerful tool to have. Let me know if you face any difficulties in any of the above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can also check the whole code on &lt;a href="https://github.com/mohitsehgal/ckeditor-nuxt-sample" rel="noopener noreferrer"&gt;this Github repo&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Happy Coding.&lt;br&gt;
Remember, currently this article works for Vue 2 only.&lt;/p&gt;

</description>
      <category>nuxt</category>
      <category>vue</category>
      <category>javascript</category>
      <category>ckeditor</category>
    </item>
  </channel>
</rss>
