Unveiling the Magic: Displaying Quill's raw HTML in Vue.js

Within my Vue.js app, I am utilizing the Quill editor to generate raw HTML content that is saved directly to the database without any cleaning. When fetching this content from the backend, the text and styling are displayed correctly (colors, bolding, etc.), but the text alignment is not rendering as expected.

The issue lies in using v-html to display the content where the text alignment does not appear as intended. My goal is to achieve proper text alignment within the rendered HTML without allowing users to modify the content.

Upon further investigation, it was discovered that Quill uses classes such as ql-align-right and ql-align-center for text alignment. Attempts were made to add explicit styling for these classes with no success.

Code snippet

In my homeComponent.vue:

<template>
  <div class="cell medium-8 large-6 columns">
    <div class="blog-post" v-for="feed in feedArray" :key="feed.id">
      <h3>
        <router-link
          :to="`article/${feed.post_uniqueidentity}`"
          class="heading-link"
        >
          {{ feed.post_head }}
        </router-link>
      </h3>
      <small>{{
        moment(feed.post_timestamp).format("dddd, MMMM Do YYYY")
      }}</small>
      <div class="postBody-simple" v-html="feed.post_body"></div>
    </div>
    <div
      v-if="feedArray.length"
      v-observe-visibility="HandledScrollEvent"
    ></div>
  </div>
</template>

<script>

// ignore from now on...

import moment from "moment";
import { mapGetters, mapActions } from "vuex";
import "../assets/css/bootstrap-icons-1.5.0/bootstrap-icons.css";
import { ObserveVisibility } from "vue-observe-visibility";

export default {
  name: "feeds",
  metaInfo: {
    title: "Home",
  },
  directives: {
    ObserveVisibility,
  },
  data() {
    return {
      next: null,
      previous: 1,
      feedArray: [],
    };
  },
  computed: { ...mapGetters(["feedStore"]) },
  methods: {
    ...mapActions([
      "fetchFeed",
      "fetchFeedPagination",
    ]),
    async contentLoader(pageNumber) {
      var localArrayData = await this.fetchFeedPagination(pageNumber);
      this.feedArray.push(...localArrayData.results);
      this.next = localArrayData.next;
    },
    HandledScrollEvent(isVisible) {
      if (!isVisible) {
        return;
      }
      if (this.next) {
        this.contentLoader(this.next);
      }
    },
  },
  async created() {
    this.moment = moment;
    let rawResponse = await this.fetchFeed();
    let isNextPageAvailable = rawResponse.next;
    let isPreviousPageAvailable = rawResponse.previous;
    if (isNextPageAvailable) {
      this.next = isNextPageAvailable;
    }
    this.previous = isPreviousPageAvailable;
    this.feedArray.push(...rawResponse.results);
  },
};
</script>

Frontend

Vue: 2.6.11,

vue-observe-visibility: 1.0.0,

vue-quill-editor: 3.0.6,

vue-router: 3.2.0,

vuex: 3.4.0

Backend

Django: 3.0.5

django-cors-headers: 3.7.0

djangorestframework: 3.12.4

psycopg2: 2.9.1

Database: PostgreSQL

Efforts to address the alignment issue have been made even without additional styling parameters, yet the desired results are not achieved.

Answer №1

According to the instructions outlined in the README file on Github, it is recommended to import the appropriate CSS stylesheets:

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  components: {
    quillEditor
  }
}

To ensure proper rendering of HTML content, these stylesheets are necessary as Quill primarily utilizes CSS classes over inline styles by default. The behavior can be altered through the utilization of attributors, which are detailed in the documentation

var ColorClass = Quill.import('attributors/class/color');
var SizeStyle = Quill.import('attributors/style/size');
Quill.register(ColorClass, true);
Quill.register(SizeStyle, true);

// Proceed with initialization as usual
var quill = new Quill('#editor', {
  modules: {
    toolbar: true
  },
  theme: 'snow'
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Upon script load, a random item from an array will be displayed and recorded in a separate array

I'm working on a fun code project where I aim to display random animal names in different colors that change when a specific keyboard key is pressed. The challenge I'm facing is that the first random animal name in a random color only appears aft ...

Developing a RESTful API with Discord.js integrated into Express

Currently, I am faced with the challenge of integrating the Discord.js library into an Express RESTful API. The main issue at hand is how to effectively share the client instance between multiple controllers. The asynchronous initialization process complic ...

The execution of my code differs between running it locally and in online code editors like CodePen or Plunker

Using jQuery Terminal, I have the following code: function display() { for (var i = 0; i < 100; ++i) { this.echo('line ' + i, { flush: false }); } this.flush(); setTimeout(function() { //thi ...

What is causing my AJAX function to malfunction and throwing an exception for undefined data objects?

I am having trouble with my ajax query in my code. Even though it is returning the required data, it is not displaying properly within the HTML code. I have a common header and footer (PHP) for all other files. Despite my efforts to resolve this issue by s ...

Overflow of text arranged horizontally within a span element situated inside a div container

I am currently working on developing a ticketing system that involves using nested div elements to organize content. Each ticket is represented by a main div containing various other nested divs and media such as images. While the functionality of the sys ...

Experience the powerful combination of Nuxt 3 paired with TailwindCSS and heroicons. Elevate your

Looking for assistance in integrating Heroicons with Nuxt 3. I tried installing Heroicons using the command: yarn add @heroicons/vue In addition, I included @heroicons/vue in my nuxt.config.js: build: { transpile: ["@headlessui/vue", "@her ...

Executing selenium tests on Internet Explorer 11 on a Windows 10 1809 machine without encountering any new pop-up windows

While testing on my computer, I encountered an issue where my test would start successfully, but after opening and closing several Internet Explorer windows during the test, no new windows would open. There were no error messages displayed, and the test se ...

Is it possible to have separate ports for front-end and back-end in NODEJS?

As I develop my NodeJS website, incorporating both front-end and back-end components, it is recommended to organize the files in such a way that the front-end specifically requests data from the back-end via an API. I am curious whether it is considered a ...

Developing a universally accessible variable for utilization in various functions

I'm having trouble understanding why 'currentPos.LatLng' is undefined when trying to access it outside the function even though it's part of an object. I want to be able to retrieve the current position values to use them in another fun ...

Enabling the source map option for the TerserWebpackPlugin webpack plugin has a noticeable impact on the overall build time of webpack

I've made the decision to enable source maps for production. Utilizing TerserWebpackPlugin for minifying my js files, as suggested by webpack documentation. This plugin includes a config option for sourceMap, which according to the docs, should be ena ...

Having difficulty duplicating a button with a click event using jQuery

I have implemented a button that reveals a phone number when clicked using the code below: var shortNumber = $("#phone_ch").text().substring(0, $("#phone_ch").text().length - 12); var onClickInfo = "_gaq.push(['_trackEvent', 'EVENT-CATEGOR ...

Refresh the browser tab's content

How can I reload the specific content of a browser tab? I am looking for a solution that does not rely solely on jQuery (although I prefer it if there are more options available). Here is what I need: I have a page with many links to other pages (the fo ...

Transform a <td> into a table-row (<tr>) nested within a parent <tr> inside an umbrella structure

Similar questions have been asked in the past, but I still haven't found a solution to my specific inquiry. Here it is: I have a table that needs to be sortable using a JavaScript plugin like ListJS. The key requirement is that I must have only one & ...

Prevent JavaScript from being entered into the form

I'm currently developing an "HTML editor" for one of my webpages. Right now, I want to ensure that the editor only allows input of HTML and CSS elements without any Javascript (or Jquery). I've been attempting to prevent the use of <script> ...

Prevent closure of Bootstrap offcanvas on backdrop click in a Blazor .NET application

Trying to implement an offcanvas window form and ensuring it stays open only when the user clicks the close button. Following client requirements, I need to call the offcanvas window via C# with IJSRuntime injection instead of directly from HTML. Operatin ...

Execute JavaScript function after the window has finished loading, regardless of any asynchronous downloads

In my app, there is an initialise function that I want to execute only when two conditions are met: first, the window has finished loading ($(window).load()), and second, Typekit has loaded. $(window).load(function() { try { Typekit.load({ act ...

What is the method to extract mimeType from an HTMLImageElement?

After researching the MDN documentation on HTMLImageElement, I found no mention of the mimeType. I did come across some resources explaining how to retrieve the mimeType from a File Object. However, my specific requirement is to extract the mimeType from ...

Application crash imminent, alert: Uncaught TypeError detected - Unable to access property 'some' of undefined within React

My application has 3 sections. The first section consists of a form where users fill in details about watches, and the data is submitted using the submitHandler function. In the second part, users can enter watches from the first section. When a user click ...

Troubleshooting Azure Routing Issues after Switching to .ejs Files

I am facing an issue where my application breaks on Azure after changing the file extensions and routes to point to the renamed .ejs files, but it works perfectly fine locally. angularApp.js var app = angular.module('theApp', ['ui.router&a ...

The strange behavior of !important, display:none, and .height()

While working with a piece of JS code yesterday, I stumbled upon something peculiar. There was a div element that was initially hidden using display:none, and I was utilizing its height in some JavaScript calculations. Everything was functioning properly u ...