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

Better TS Interface for config options. #277

Open
gaqno opened this issue Oct 24, 2024 · 1 comment
Open

Better TS Interface for config options. #277

gaqno opened this issue Oct 24, 2024 · 1 comment

Comments

@gaqno
Copy link

gaqno commented Oct 24, 2024

Jodit Version: 4.1.2

Browser: Chrome
OS: Linux
Is React App: True

Its possible do insert Jodit default intarface for options?
I cannot access function from editor ref, as

const editor: any = editorRef.current?.['editor' **(?)**]; // editor?.selection.insertImage(fileUrl);

Code

// A *self-contained* demonstration of the problem follows...
import React from 'react';
import JoditEditor from 'jodit-react';

const Example = () => {
  const editor = useRef(null);
  const [content, setContent] = useState('');
  const config = useMemo(() => ({
    readonly: false,
    placeholder: props.placeholder || 'Digite...',
    height: 200,
    language: 'pt_br',
    uploader: {
      url: `${import.meta.env}`, // URL do seu endpoint de upload
      insertImageAsBase64URI: false, // Não insere a imagem como base64
      format: "json", // Formato da resposta do servidor
      headers: {
        Authorization: `Bearer ${token}`, // Se precisar de autenticação
      },
      filesVariableName: () => 'imagem', // Nome do arquivo enviado
      process: function (response) {
        // Processa a resposta do servidor e insere a tag <img> no editor
        if (response.fileUrl) {
          const editor: any = editorRef.current?.['editor']; // Pega a instância do editor
          const { fileUrl, fileName } = response || {}; // 'fileUrl' é o campo com o link da imagem
          // Aqui você pode verificar se o arquivo é uma imagem e inserir a tag <img>
          if (fileUrl && /\.(jpg|jpeg|png|gif)$/i.test(fileUrl)) {
            // Se for uma imagem, insere a tag <img> no editor
            editorRef.current

            editor?.selection.insertImage(fileUrl);
          } else {
            // Se não for uma imagem, insere como um link
            editor?.selection.insertHTML(
              `<a href="${fileUrl}" target="_blank">${fileName}</a>`
            );
          }
        }
      },
      isSuccess: function (response) {
        return !response.error; // Verifica se houve sucesso no upload
      },
      error: function (e) {
        console.error("Erro no upload da imagem", e);
      },
    },
    toolbarAdaptive: false,
    buttons: "bold,italic,underline,strikethrough,|,image"
  } as IJoditEditorProps['config']), [props.placeholder]);


  return (
    <JoditEditor
      ref={editor}
      value={content}
      onChange={newContent => setContent(newContent)}
    />
  );
};

export default Example;
@xdan
Copy link
Collaborator

xdan commented Jan 19, 2025

Hi, Try 5.0.9 please

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

2 participants