Crafting a Custom Button for the Froala Editor in Vue.js

vue js custom button

Creating a custom button for the Froala Editor within a Vue.js application allows developers to extend the editor’s capabilities to match their project’s specific needs. This comprehensive guide will walk you through the process step by step.

vue js custom button main image

Prerequisites:

Basic understanding of and Vue.js.

Node.js and NPM are installed on your machine.

A fresh Vue.js project. If you don’t have one, set it up using the Vue CLI.

Step-by-Step Integration:

1. Setting up a New Vue Project (if needed):

If you don’t already have a Vue project,

npm install -g @vue/cli

vue create froala-vue-project

cd froala-vue-project

2. Install the vue-froala-wysiwyg package:

npm install vue-froala-wysiwyg --save

3. Setting up Froala in the project

Open the src/main.js file:

import 'froala-editor/js/plugins.pkgd.min.js';

import 'froala-editor/css/froala_editor.pkgd.min.css';

import { createApp } from 'vue';
import App from './App.vue';
import VueFroala from 'vue-froala-wysiwyg';

const app = createApp(App);
app.use(VueFroala);
app.mount('#app');

4. Set up the Froala component in your Vue template:

In src/App.vue:

 

<template>
  <div id="app">
    <froala :tag="'textarea'" :config="config" v-model:value="content"></froala>
  </div>
</template>

<script>
import FroalaEditor from 'froala-editor';

export default {
  name: 'App',
  data() {
    return {
      content: 'Your initial content here!',
      config: {}
    };
  }
};
</script>

5. Creating Your Custom Button:

  1. Define the Icon: Choose an SVG icon or use a predefined one. Here, we’ll use the ‘star’ icon:
  2. Register the Custom Button: Define your button’s functionality. In this example, the button will insert custom text:
    1. FroalaEditor.DefineIcon('myButton', {NAME: 'star', SVG_KEY: 'star'});
  3. Integrate the Button into the Froala Toolbar: Update the config object in your App.vue:
    1. config: {
        toolbarButtons: [['bold', 'italic', 'underline', 'myButton']]
        // ... any other configurations
      }
      

Here’s the final look of your App.Vue file

<template>
  <div id="app">
    <froala :tag="'textarea'" :config="config" v-model:value="content"></froala>
  </div>
</template>

<script>
import FroalaEditor from 'froala-editor';

export default {
  name: 'App',
  data() {
    return {
      content: 'Your initial content here!',
      config: {
        //... (you can add your initial configuration here)
        toolbarButtons: [['bold', 'italic', 'underline', 'myButton']]
      }
    };
  },
  created() {
    // Define the Icon for the Custom Button
    FroalaEditor.DefineIcon('myButton', {NAME: 'star', SVG_KEY: 'star'});

    // Register the Custom Button's action
    FroalaEditor.RegisterCommand('myButton', {
      title: 'Insert Custom Text',
      focus: true,
      undo: true,
      refreshAfterCallback: true,
      callback: function() {
        this.html.insert('Watch the magic happen🌠');
      }
    });
  }
};
</script>

6. Run and Test:

Launch your Vue.js application:

 

npm run serve

Open your browser to view the application. You should spot the Froala Editor with your custom button integrated. Clicking it will insert the pre-set text.

 

Here’s a demo of the working code:

 

Extra Content: The Seamless Integration of Froala with Vue.js 3 LTS Version

With the long-term support (LTS) version of Vue.js 3 now available, the benefits of using Froala with Vue.js have become even more pronounced. Here’s why:

1. Flexibility Meets Stability

The LTS version of Vue.js 3 offers a stable platform that receives regular maintenance and security updates. When you combine this stability with Froala’s adaptability, developers get a blend of reliability and flexibility that few combinations can offer.

2. Enhanced Performance:

Vue.js 3 has brought numerous performance improvements. One notable change is the async setup, which allows you to handle asynchronous operations directly within the setup function. Combined with Froala’s efficient rendering, this results in a smoother user experience.

 

<template>
  <froala :tag="'textarea'" :config="config" v-model:value="content"></froala>
</template>

<script>
import { ref } from 'vue';
import FroalaEditor from 'froala-editor';

export default {
  async setup() {
    const content = ref('Your initial content here!');
    const config = ref({
      //... (your initial configuration here)
    });

    // Let's say you fetch some content asynchronously for the editor.
    const fetchedContent = await fetchContentFromServer();
    content.value = fetchedContent;

    return {
      content,
      config
    };
  }
};
</script>

3. Better Composition API:

Vue.js 3 introduces the Composition API, which allows a more flexible code organization. Combined with Froala’s modular design, this results in cleaner and more maintainable code.

 

import { ref, computed } from 'vue';
import FroalaEditor from 'froala-editor';

export default {
  setup() {
    const content = ref('Your initial content here!');
    
    // Using the Composition API to create a computed property
    const wordCount = computed(() => {
      return content.value.split(' ').length;
    });

    // Define and register a custom button using Froala's methods within the Composition API
    FroalaEditor.DefineIcon('wordCountButton', {NAME: 'counter', SVG_KEY: 'counter'});
    FroalaEditor.RegisterCommand('wordCountButton', {
      title: 'Word Count',
      focus: false,
      undo: false,
      refreshAfterCallback: false,
      callback: function() {
        alert(`Current word count: ${wordCount.value}`);
      }
    });

    return {
      content,
      wordCount
    };
  }
};

4. Improved Customizability:

Vue.js 3 LTS comes with enhanced support for TypeScript. This means developers can now easily define types for their custom buttons or other editor extensions.

 

import { Ref, ref } from 'vue';
import FroalaEditor from 'froala-editor';

interface EditorConfig {
  toolbarButtons?: string[][];
  //... other configuration options
}

export default {
  setup(): { content: Ref<string>; config: Ref<EditorConfig> } {
    const content: Ref<string> = ref('Your initial content here!');
    const config: Ref<EditorConfig> = ref({
      toolbarButtons: [['bold', 'italic', 'underline', 'wordCountButton']]
      // ... other configurations
    });

    return {
      content,
      config
    };
  }
};

The code snippets provided demonstrate how effortlessly Vue.js 3 LTS and Froala Editor can integrate. Together, they offer a robust platform for developers aiming to create top-notch digital solutions.

 

Conclusion

Successfully adding a custom button for the Froala Editor within a Vue.js app enhances the user experience and demonstrates the flexibility of both Vue.js and Froala. This tutorial aimed to deliver a clear roadmap from the beginning to the end of the process, and we urge readers to explore further customizations to meet their unique demands. With tools like Vue.js and Froala, the possibilities are vast. Happy coding!

Do checkout our Vue.js documentation to learn more.

 

Download Froala Editor

Posted on August 16, 2023

Carl Cruz

Product Marketing Manager for Froala. A technical enthusiast at heart.

No comment yet, add your voice below!


Add a Comment

Your email address will not be published.