orchidsoftware / platform

Orchid is a @laravel package that allows for rapid application development of back-office applications, admin/user panels, and dashboards.
https://orchid.software
MIT License
4.37k stars 643 forks source link

Duplicated Quill toolbar #1053

Closed mwkcoding closed 4 years ago

mwkcoding commented 4 years ago

Describe the bug Two identical Quill toolbars showing up when using the Quill field in a layout.

To Reproduce Steps to reproduce the behavior:

  1. Install Orchid
  2. Follow guide to make Email Sender Layout (https://orchid.software/en/docs/quickstart/)
  3. Visit page and see the duplicated Quill toolbar

Expected behavior Not seeing two Quill toolbars?

Screenshots image

Desktop (please complete the following information):

Server (please complete the following information):

Additional context Add any other context about the problem here.

tabuna commented 4 years ago

Hi @mwkcoding

I could not repeat it following the instructions:

image


Do you have any errors in the browser console? On the screen with a list of fields, is this reproduced?

mwkcoding commented 4 years ago

I have the following errors in my browser console: image

mwkcoding commented 4 years ago

It might be worth noting that this happened after an update of orchid with composer update. I don't remember from or to which version it was though

tabuna commented 4 years ago

@mwkcoding I think it’s now clear why duplication occurs. Please correct error 502, this is due to your web server or tunnel/proxy settings. Apparently this breaks javascript code

mwkcoding commented 4 years ago

@tabuna How do I correct an error I didn't make? I'm using Laravel Valet on MacOS. And like I wrote, this happened after an update.

tabuna commented 4 years ago

502 error is a web server error, can we see the logs? And a detailed answer in the browser network tab?

mwkcoding commented 4 years ago

The error disappeared by itself after I reloaded the page with the devtools open (ignores cache) and I now have this error image

tabuna commented 4 years ago

The error disappeared by itself after I reloaded the page with the devtools open (ignores cache) and I now have this error

Notifications are polled every few minutes. Therefore, that error is no longer there. Hmm, please check in incognito mode with browser extensions disabled.

mwkcoding commented 4 years ago

Incognito has same result.

mwkcoding commented 4 years ago

@tabuna Any idea what I can do to fix this?

mwkcoding commented 4 years ago

It seems to have disappeared after I updated and ran php artisan cache:clear && php artisan config:cache

tabuna commented 4 years ago

Any idea what I can do to fix this?

How about a little research? (Better to do it in a new project.)

You can publish all js/css files to your applications and compile them yourself. To do this, run:

  1. artisan preset orchid-source
  2. npm install
  3. npm run dev

All files are published to your application and you can analyze why you are accessing the element returns null.

tabuna commented 4 years ago

I will close this issue. If you are still experiencing such a problem, please feel free to open it again.

ecuation commented 3 years ago

Same error here, it happens when navigating from pages.

blankster commented 3 years ago

@ecuation did you tried the above? If yes, how can it be reproduced?

Novemburr commented 3 years ago

Same here, I'll see if I can hunt it down. Like as not I'm doing something silly.

EDIT: It was something silly indeed. I had a copy of the editor hidden elsewhere on the page so I could clone it as needed. It contained a copy of the same initialization code which was running twice and double initializing just the first editor.

e1sep0 commented 1 year ago

The same behavior in modal, when i try to create new instance image

When trying to edit some model, behavior is correct: image

Nothing in debug panel

qvarts commented 7 months ago

The same behavior when Turbo cache = true and using browser history navigation buttons.

Steps to reproduce the behavior:

1 Install Orchid 2 Set turbo cache = true in config/platform.php 2 Visit Examples->Form Elements->Text Editors page 3 Go to any other page and go back with the browser history navigation button or Alt + Arrow Left

You will see two SimpleMDE editor and two Quill toolbars like in screenshot:

Firefox_Screenshot_2024-02-08T10-00-35 027Z

JayJCodez commented 1 month ago

The issue most of you are having is due to re-renders. You can keep track of the state of your editor and make sure it renders only once.

Solution:

Initialization Check: Added a check to ensure that Quill is initialized only once. This prevents multiple instances of Quill from being created and thus prevents the toolbar from loading multiple times.

Content Update Check: Adjusted the componentDidUpdate method to only update the content if editorState has changed, without initialising Quill.

Example:

import React, { Component, createRef } from "react";
import Quill from "quill";

import "quill/dist/quill.snow.css";

interface MyEditorProps {
  editorState: string;
  onChange: (newContent: string) => void;
}

class MyEditor extends Component<MyEditorProps> {
  private editorRef = createRef<HTMLDivElement>();
  private quill?: Quill;  // Use optional chaining for Quill instance

  componentDidMount() {
    this.initializeQuill(); // Initialize Quill when the component mounts
  }

  componentDidUpdate(prevProps: MyEditorProps) {
    // Only update the content if editorState has changed
    if (prevProps.editorState !== this.props.editorState && this.quill) {
      this.quill.root.innerHTML = this.props.editorState;
    }
  }

  initializeQuill() {
    if (this.editorRef.current && !this.quill) {
      // Initialize Quill only if it has not been initialized
      this.quill = new Quill(this.editorRef.current, {
        theme: "snow",
        modules: {
          toolbar: [
            [{ header: "1" }, { header: "2" }],
            [{ list: "ordered" }, { list: "bullet" }],
            ["bold", "italic", "underline"],
            [{ align: [] }],
            ["link"],
          ],
        },
      });

      // Set up the event listener for text changes
      this.quill.on("text-change", () => {
        if (this.quill) {
          this.props.onChange(this.quill.root.innerHTML);
        }
      });

      // Set initial content
      if (this.props.editorState) {
        this.quill.root.innerHTML = this.props.editorState;
      }
    }
  }

  render() {
    return (
      <div>
        <div ref={this.editorRef} style={{ minHeight: "200px" }}></div>
      </div>
    );
  }
}

export default MyEditor;

Screenshot 2024-08-19 at 18 39 43 Screenshot 2024-08-19 at 18 39 57