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

Can I use this module in nextjs? #152

Open
suCozy opened this issue Jul 21, 2023 · 0 comments
Open

Can I use this module in nextjs? #152

suCozy opened this issue Jul 21, 2023 · 0 comments

Comments

@suCozy
Copy link

suCozy commented Jul 21, 2023

My code is as below, but I want to try using the image control library I'd like to ask for advice on what to do if possible

import {
  Dispatch,
  LegacyRef,
  memo,
  SetStateAction,
  useMemo,
  useRef,
} from 'react';

import dynamic from 'next/dynamic';
import ReactQuill, {  ReactQuillProps } from 'react-quill';
import Swal from 'sweetalert2';

import { quillDefaultModule } from '@constants/quill.constant';
import { getMetaData } from '@http/commons.http';
import { uploadFile } from '@http/file.http';

interface IQuillEditor extends ReactQuillProps {
  forwardedRef: LegacyRef<ReactQuill>;
}

const QuillNoSSRWrapper = dynamic(
  async () => {
    const { default: RQ } = await import('react-quill');
    // eslint-disable-next-line react/display-name
    return function editor({ forwardedRef, ...props }: IQuillEditor) {
      return <RQ ref={forwardedRef} {...props} />;
    };
  },
  { ssr: false }
);

function Editor({
  setContent,
  content,
}: {
  setContent: Dispatch<SetStateAction<string>>;
  content: string;
}) {
  const quill = useRef<ReactQuill>(null);

  const modules = useMemo(
    () => ({
      ...quillDefaultModule,
      toolbar: {
        ...quillDefaultModule.toolbar,
        handlers: {
          image: async () => imageHandler(),
        },
        onPaste() {
          console.log('asdfasdf');
        },
      },
    }),
    []
  );

  const imageHandler = () => {
    const input = document.createElement('input');

    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/png, image/jpeg, .gif');

    document.body.appendChild(input);

    input.click();

    input.onchange = async () => {
      try {
        const file = input.files && input.files[0];

        const data = await uploadFile({ file });

        if (quill.current) {
          const range = quill.current.getEditorSelection();

          if (range) {
            quill.current
              .getEditor()
              .insertEmbed(range.index, 'image', data.data.Location);
            quill.current.getEditor().insertText(range.index + 1, ' ');


            quill.current
              .getEditor()
              .setSelection({ index: range.index + 1, length: 0 });
          }

        }
      } catch (error) {
        alert('error');
      }
    };
  };

  return (
    <QuillNoSSRWrapper
      forwardedRef={quill}
      modules={modules}
      onChange={setContent}
      theme="snow"
      defaultValue={content}
      scrollingContainer="html"
    />
  );
}

export default memo(Editor);
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

1 participant