Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Table cannot be displayed when edit again #12

Open
jasperliu911 opened this issue Aug 28, 2024 · 14 comments
Open

[Bug] Table cannot be displayed when edit again #12

jasperliu911 opened this issue Aug 28, 2024 · 14 comments

Comments

@jasperliu911
Copy link

jasperliu911 commented Aug 28, 2024

After the table is saved, it cannot be displayed when it is edited and displayed again

企业微信截图_84502787-5206-4a49-814b-bb1f1fd1dec3

When edit showing
image

@jasperliu911
Copy link
Author

Can you tell me specifically where this code should be placed? Because I am using the npm package form. After passing the html value of the table to the Richtext, I initialize this code and find that although this problem is solved, I cannot insert a table again, and the table cannot be fully displayed in the output html.

@attoae
Copy link
Owner

attoae commented Aug 29, 2024

Do you dependencies on react-quill or vue-quill or other?

@attoae
Copy link
Owner

attoae commented Aug 29, 2024

If it weren't for the abov.
You can refer to the following code (Not HTML assignment, use this method after creating an editor instance):

const quill = new Quill('#root', options);
const delta = quill.clipboard.convert({ html });
const [range] = quill.selection.getRange();
quill.updateContents(delta, Quill.sources.USER);
quill.setSelection(
  delta.length() - range.length,
  Quill.sources.SILENT,
);
quill.scrollIntoView();

@jasperliu911
Copy link
Author

Our actual use may be more complicated than the demo. We did this code after getting the html when initializing the rich text component and when listening to html changes. The problem was temporarily solved. But another problem arose, that is, the table could not be displayed normally when inserted for the second time. We temporarily solved the problem by replace the span tag in the table in the html, but it seems that it did not solve it fundamentally.

@jasperliu911 jasperliu911 changed the title After the table is saved, it cannot be displayed when it is edited and displayed again [Bug] Table cannot be displayed when edit again Aug 30, 2024
@dustinormond
Copy link

dustinormond commented Sep 13, 2024

I did a workaround to get it working. Here is my code:

const html = $(quillID).html();
$(quillID).html("");
const quillEditor = new Quill(quillID, options);
const delta = quillEditor.clipboard.convert({ html })
quillEditor.updateContents(delta, Quill.sources.USER);

@junjiebyr
Copy link

junjiebyr commented Sep 25, 2024

Do you dependencies on react-quill or vue-quill or other?

@attoae hello,I am using react-quill, so where should this code be placed?

@attoae
Copy link
Owner

attoae commented Sep 26, 2024

@attoae hello,I am using react-quill, so where should this code be placed?

This library(react-quill) does not yet support Quill 2.0 version.

import { useEffect } from 'react';
import Quill from 'quill';
import QuillTableBetter from 'quill-table-better';
import 'quill/dist/quill.snow.css';
import 'quill-table-better/dist/quill-table-better.css';

Quill.register({
  'modules/table-better': QuillTableBetter
}, true);

const options = {
  theme: 'snow',
  modules: {
    toolbar: ['bold', 'italic', 'underline', 'strike', 'table-better'],
    table: false,
    'table-better': {
      language: 'en_US',
      menus: ['column', 'row', 'merge', 'table', 'cell', 'wrap', 'delete'],
      toolbarTable: true
    },
    keyboard: {
      bindings: QuillTableBetter.keyboardBindings
    }
  }
};

// The html here is sample data, the real data is what you get from the database.
const html = `
  <table class="ql-table-better">
  <tbody>
    <tr>
      <td data-row="row-jd3a" width="72" class="ql-cell-focused">
        <p class="ql-table-block" data-cell="cell-wmy6"><br></p>
      </td>
      <td data-row="row-jd3a" width="72">
        <p class="ql-table-block" data-cell="cell-tni4"><br></p>
      </td>
    </tr>
    <tr>
      <td data-row="row-cu2a" width="72">
        <p class="ql-table-block" data-cell="cell-zmnp"><br></p>
      </td>
      <td data-row="row-cu2a" width="72">
        <p class="ql-table-block" data-cell="cell-7o44"><br></p>
      </td>
    </tr>
  </tbody>
  </table>
`;

function Editor() {
  useEffect(() => {
    const quill = new Quill('#editor', options);
    const delta = quill.clipboard.convert({ html });
    const [range] = quill.selection.getRange();
    quill.updateContents(delta, Quill.sources.USER);
    quill.setSelection(
      delta.length() - (range?.length || 0),
      Quill.sources.SILENT,
    );
    quill.scrollIntoView();
  }, []);

  return (
    <div id="editor"></div>
  );
}

export default Editor;

@junjiebyr
Copy link

junjiebyr commented Sep 26, 2024

This library(react-quill) does not yet support Quill 2.0 version.

@attoae thanks, but i am using react-quill-new, it used quill 2.0. In addition, i save the delta mode in database, not html. so i'm worried about data format incompatibility

@attoae
Copy link
Owner

attoae commented Sep 26, 2024

@junjiebyr

function Editor() {
  const quillRef = useRef(null);
  useEffect(() => {
    if (quillRef.current) {
      const quill = quillRef.current.getEditor();
      const [range] = quill.selection.getRange();
      quill.updateContents(delta, Quill.sources.USER);
      quill.setSelection(
        delta.length() - (range?.length || 0),
        Quill.sources.SILENT,
      );
      quill.scrollIntoView();
    }
  }, [quillRef]);
  
  return (
    <ReactQuill ref={quillRef} theme="snow" modules={modules} />
  );
}

@junjiebyr
Copy link

@attoae Thank you for your patience. I'll try first

@junjiebyr
Copy link

@attoae Excuse me for bothering you. In my scenario, I want to switch between edit mode and read mode, and the styles are somewhat different. So, I have two ReactQuill components, and I hope to change the display when clicking a button. Also, I want to display tables in read-only mode, but it is not working at the moment. Can you help me take a look? here is my
demo:
https://codesandbox.io/p/sandbox/react-quill-vqk6hp

@attoae
Copy link
Owner

attoae commented Sep 26, 2024

@attoae Excuse me for bothering you. In my scenario, I want to switch between edit mode and read mode, and the styles are somewhat different. So, I have two ReactQuill components, and I hope to change the display when clicking a button. Also, I want to display tables in read-only mode, but it is not working at the moment. Can you help me take a look? here is my demo: https://codesandbox.io/p/sandbox/react-quill-vqk6hp

import Delta from 'quill-delta';

const [value, setValue] = useState(new Delta());
const quillRef = useRef(null);

useEffect(() => {
    if (quillRef.current || preview) {
      const quill = quillRef.current.getEditor();
      const [range] = quill.selection.getRange();
      quill.setContents();
      quill.updateContents(value.delete(1));
      !preview && quill.setSelection(
        value.length() - (range?.length || 0),
        Quill.sources.SILENT,
      );
      !preview && quill.scrollIntoView();
    }
  }, [quillRef, preview]);

  return (
    <>
      <ReactQuill
        ref={quillRef}
        key={preview ? 'preview' : 'edit'}
        id={preview ? 'preview' : 'edit'}
        theme={preview ? 'bubble' : 'snow'}
        readOnly={preview}
        modules={modules}
        onChange={handleQuillValueChange}
      />
      <button onClick={() => setPreview((value) => !value)} type="button">
        { preview ? 'Change Editor' : 'Change Preview' }
      </button>
    </>
  );

@junjiebyr
Copy link

@attoae I used this code. But when i click the button, the table disappeared

@attoae
Copy link
Owner

attoae commented Sep 27, 2024

@attoae I used this code. But when i click the button, the table disappeared

Using React 17.x, useEffect of React 18.x will be refreshed multiple times.
https://codesandbox.io/p/sandbox/react-quill-forked-qxhymx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants