Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React CKeditor Upload Adapter

import React from "react";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import ClassicEditor from "@ckeditor/ckeditor5-build-classic";

const API_URL = "https://77em4-8080.sse.codesandbox.io";
const UPLOAD_ENDPOINT = "upload_files";

export default function MyEditor({ handleChange, ...props }) {
  function uploadAdapter(loader) {
    return {
      upload: () => {
        return new Promise((resolve, reject) => {
          const body = new FormData();
          loader.file.then((file) => {
            body.append("files", file);
            // let headers = new Headers();
            // headers.append("Origin", "http://localhost:3000");
            fetch(`${API_URL}/${UPLOAD_ENDPOINT}`, {
              method: "post",
              body: body
              // mode: "no-cors"
            })
              .then((res) => res.json())
              .then((res) => {
                resolve({
                  default: `${API_URL}/${res.filename}`
                });
              })
              .catch((err) => {
                reject(err);
              });
          });
        });
      }
    };
  }
  function uploadPlugin(editor) {
    editor.plugins.get("FileRepository").createUploadAdapter = (loader) => {
      return uploadAdapter(loader);
    };
  }
  return (
    <div className="App">
      <CKEditor
        config={{
          extraPlugins: [uploadPlugin]
        }}
        editor={ClassicEditor}
        onReady={(editor) => {}}
        onBlur={(event, editor) => {}}
        onFocus={(event, editor) => {}}
        onChange={(event, editor) => {
          handleChange(editor.getData());
        }}
        {...props}
      />
  );
}
Comment

ckeditor image upload react

import React from "react";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import ClassicEditor from "@ckeditor/ckeditor5-build-classic";
import axios from "axios";

export default function TextEditor({ message = "", value, setData, name }) {
  function uploadAdapter(loader) {
    return {
      upload: () => {
        return new Promise((resolve, reject) => {
          var formData = new FormData();
          formData.append("name", "Ali Hamza");
          loader.file.then((file) => {
            formData.append("files", file);

            axios
              .post(route("imageUpload"), formData)
              .then((res) => res.json())
              .then((res) => {
                resolve({
                  default: `/${res.filename}`,
                });
              })
              .catch((err) => {
                reject(err);
              });
          });
        });
      },
    };
  }
  function uploadPlugin(editor) {
    editor.plugins.get("FileRepository").createUploadAdapter = (loader) => {
      return uploadAdapter(loader);
    };
  }
  return (
    <CKEditor
      editor={ClassicEditor}
      config={{
        extraPlugins: [uploadPlugin],
      }}
      data={value}
      onChange={(event, editor) => {
        const data = editor.getData();
        setData((prevState) => ({
          ...prevState,
          [name]: data,
        }));
      }}
    />
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: page redirect after a few seconds 
Javascript :: chrome max local storage size 
Javascript :: import paper material ui 
Javascript :: max value in array javascript 
Javascript :: jquery scroll to element 
Javascript :: error: Error: Unable to resolve module `react-native-gesture-handler` from `node_modules@react-navigation ativelibmoduleScrollables.js`: react-native-gesture-handler could not be found within the project. 
Javascript :: Playing sound in Vue.js 
Javascript :: chart js rotating the x axis labels 
Javascript :: query date range in mongodb 
Javascript :: get columns of array list json js 
Javascript :: how to print to console javascript 
Javascript :: js for of array with index 
Javascript :: get all values of a select javascrip 
Javascript :: detox scroll to element 
Javascript :: javascript change image src 
Javascript :: html-webpack-plugin npm 
Javascript :: ecmascript 
Javascript :: discord.js mention regex 
Javascript :: how to update angular version 
Javascript :: alphabet letters in js code 
Javascript :: refresh page after success ajax 
Javascript :: load script js 
Javascript :: pass js var to laravel route 
Javascript :: leap year condition in javascript 
Javascript :: how to get window size in react js 
Javascript :: show 10 entries datatable jquery hide 
Javascript :: map onliy three object 
Javascript :: how to rotate camera around three JS object 
Javascript :: square every digit javascript 
Javascript :: disable all buttons jquery 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =