Optimizing Vue Applications: Initializing Froala Editor on Click

Froala Vue WYSIWYG editor Optimization

The Froala Vue WYSIWYG Editor is a powerful tool that allows developers to easily incorporate a rich text editing experience into their Vue.js applications. With its user-friendly interface and extensive customization options, the editor provides a seamless editing experience for users.

We have covered the integration process of Froala and Vue before. However, in some applications, developers may need to initialize the Froala Vue WYSIWYG Editor by clicking on a certain element. By manually initializing the editor on click, developers can control when the editor appears on the page. This helps optimize your application load speed especially if you have more than one editor on the page.

In this article, we will guide you through the process of initializing the Froala Vue WYSIWYG Editor on click. We will cover the necessary prerequisites, step-by-step instructions, and customization options to help you make the most out of this powerful editing tool.

By the end of this guide, you will have a solid understanding of how to set up and initialize the editor on click. So let’s dive in and explore the benefits and importance of on-click initialization for the Froala Vue WYSIWYG Editor.

Optimizing Vue Applications

prerequisites

We assume you have a working knowledge of:

  • JavaScript (including some of the newer, ES2015 features)
  • Vue 3
  • How to use the terminal/command line
  • Node and npm

Create a simple Vue project.

To create a new Vue.js project, run the following commands.

npm install -g @vue/cli

vue create my-froala-vue-app

The above commands will install the Vue CLI globally on your machine and then create a new Vue project called “my-froala-vue-app”.

Install the Froala WYSIWYG Editor package.

Next, you need to install the Froala WYSIWYG Editor package. To do this, navigate to your project directory and run the following command:

cd my-froala-vue-app

npm install vue-froala-wysiwyg

This will install the Froala WYSIWYG Editor package and its dependencies into your project.

Import the Froala WYSIWYG Editor Component

To use the Froala WYSIWYG Editor in your Vue application, you need to import the component. Copy the following code into your “main.js” file.

import { createApp } from "vue";
import App from "./App.vue";

//Import Froala Editor plugins
import "froala-editor/js/plugins.pkgd.min.js";

// Import Froala Editor css files.
import "froala-editor/css/froala_editor.pkgd.min.css";
import "froala-editor/css/froala_style.min.css";

// Import Froala Editor component
import VueFroala from "vue-froala-wysiwyg";

const app = createApp(App);

app.use(VueFroala);
app.mount("#app");

This code imports the VueFroala component from the `vue-froala-wysiwyg` package and registers it. This allows you to use the Froala WYSIWYG Editor component in your Vue components.

Initialize the Froala Vue WYSIWYG Editor on Click

We will add the following code to the “app.vue” file.

<template>
  <div id="app">
    <h1>Vue adapter for the Froala WYSIWYG editor</h1>

    <div class="sample">
      <h2>Sample 4: Manual Initialization</h2>
      <button class="manual" v-on:click="createEditor()" v-if="!showEditor">
        Initialize Editor
      </button>
      <button v-on:click="destroyEditor()" v-if="showEditor">
        Close Editor
      </button>
      <button v-on:click="deleteAll()" v-if="showEditor">Delete All</button>
      <froala
        id="sample5"
        :onManualControllerReady="initialize"
        v-model:value="sample3Text"
      >
        <template v-slot>
          <div>
            Check out the
            <a href="https://www.froala.com/wysiwyg-editor">Froala Editor</a>
          </div>
        </template>
      </froala>
    </div>
  </div>
</template>

<script>
export default {
  name: "app",
  data() {
    return {
      // Sample 4
      sample3Text: null,
      initControls: null,
      showEditor: false,
      deleteAll: null,
      destroyEditor: () => {
        this.initControls.destroy();
        this.showEditor = false;
      },
      createEditor: () => {
        this.initControls.initialize();
        this.showEditor = true;
      },
      initialize: (initControls) => {
        this.initControls = initControls;
        this.deleteAll = () => {
          this.initControls.getEditor().html.set("");
          this.initControls.getEditor().undo.reset();
          this.initControls.getEditor().undo.saveStep();
        };
      },
    };
  },
  created() {},
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin-top: 60px;
}
body {
  padding: 20px;
}

.sample {
  padding-bottom: 50px;
  margin-left: 30px;
  border-top: 1px solid lightgray;
}

.manual {
  margin-bottom: 20px;
}

.button {
  margin-left: 5px;
}
</style>

Let’s break down the code step by step:

The code above defines three buttons: one for initializing the editor when it is not loaded on the page, and the other two buttons appear when the editor is shown. One button is for destroying the editor, and the other is used to clear the editor’s contents.

<h2>Sample 4: Manual Initialization</h2>
<button class="manual" v-on:click="createEditor()" v-if="!showEditor">
  Initialize Editor
</button>
<button v-on:click="destroyEditor()" v-if="showEditor">
 Close Editor
</button>
<button v-on:click="deleteAll()" v-if="showEditor">Delete All</button>

The showEditor is a data property that determines whether the editor is currently shown or not. It is initially set to false. The createEditor method is called when the “Initialize Editor” button is clicked, and it sets showEditor to true and initializes the editor. The destroyEditor method is called when the “Close Editor” button is clicked, and it destroys the editor and sets showEditor back to false. The deleteAll method is called when the “Delete All” button is clicked, and it clears the editor contents.

Our “app.vue” code also includes the Froala Vue component. It has an id of sample5 and a v-model binding to the sample3Text data property. This :onManualControllerReady is used to bind the initialize method to the Froala editor’s manual controller. This allows us to access the editor’s methods and perform actions such as clearing the editor’s contents.

      <froala
        id="sample5"
        :onManualControllerReady="initialize"
        v-model:value="sample3Text"
      >

The initialize method is called when the editor is initialized, and it receives the initControls object as a parameter. Developers can obtain instances of the FroalaEditor object by using the getEditor() method of the initControls object. This allows them to easily access and utilize any Froala API method.

<script>
export default {
  name: "app",
  data() {
    return {
      // Sample 4
      sample3Text: null,
      initControls: null,
      showEditor: false,
      deleteAll: null,
      destroyEditor: () => {
        this.initControls.destroy();
        this.showEditor = false;
      },
      createEditor: () => {
        this.initControls.initialize();
        this.showEditor = true;
      },
      initialize: (initControls) => {
        this.initControls = initControls;
        this.deleteAll = () => {
          this.initControls.getEditor().html.set("");
          this.initControls.getEditor().undo.reset();
          this.initControls.getEditor().undo.saveStep();
        };
      },
    };
  },
  created() {},
};
</script>

Finally, we have a `template` block inside the Froala component where we can add additional HTML content. In this example, we have simple text and a link to the Froala Editor website.

  <template v-slot>
    <div>
      Check out the
      <a href="https://www.froala.com/wysiwyg-editor">Froala Editor</a>
    </div>
  </template>

That’s it! You have now created a simple Vue project with the Froala WYSIWYG Editor integrated. You can now run your Vue project and see the Froala Editor in action.

Run the Vue App

To run the Vue app and see the Froala Vue WYSIWYG Editor in action, navigate to your project directory and run the following command:

npm run serve

This will start the development server at http://localhost:8080

The benefits of loading Froala on click

Loading the Froala Vue WYSIWYG editor on click offers several benefits for developers. It helps improve the initial loading time of the webpage. By deferring the loading of the editor until it is actually needed, developers can ensure that their web pages load quickly and efficiently. This can lead to a better user experience, as visitors won’t have to wait for the editor to load if they don’t plan on using it.

Additionally, loading the Froala editor on click allows developers to save resources and optimize performance. Since the editor is only loaded when necessary, it doesn’t consume unnecessary memory or processing power. This can help improve the overall performance of the webpage and prevent any unnecessary strain on the user’s device.

Moreover, loading the Froala editor on click provides a more seamless and intuitive user experience. Instead of having the editor visible at all times, it only appears when the user explicitly chooses to use it. This allows for a cleaner and less cluttered interface, making it easier for users to focus on the task at hand.

Conclusion

The Froala WYSIWYG Editor is a powerful tool for creating rich text content in web applications. In this tutorial, we learned how to integrate the Froala Editor with a Vue.js project and initialize it on click. This approach offers numerous benefits in terms of performance, user experience, and resource optimization.

Now that you have learned how to integrate and initialize the Froala Editor on click, why not give it a try in your own Vue.js projects? With its user-friendly interface and extensive customization options, the Froala Editor offers a seamless editing experience for your users. Experience the power of the Froala Editor today and enhance the content creation capabilities of your web applications.

Get started with the Froala Vue WYSIWYG Editor now and unleash the full potential of your web applications!

 

Posted on January 15, 2024

Mostafa Yousef

Senior web developer

No comment yet, add your voice below!


Add a Comment

Your email address will not be published.

    Hide Show