我在react项目中有羽毛笔编辑器,并且使用了图像处理程序来上传图像,但是我必须在羽毛笔编辑器中为所有图像元素添加类。 这是我的图像处理程序代码
imageHandler() {
let self=this
const Cryptr = require('cryptr');
const cryptr = new Cryptr(key);
const users = localStorage.getItem('users') ? JSON.parse(cryptr.decrypt(localStorage.getItem('users'))) : {}
let loggedinUser = users[users.lastLoginId];
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.click();
input.onchange = async () => {
const file = input.files[0];
const formData = new FormData();
// const formData = new FormData();
formData.append('image', file);
const config = {
headers: {
'content-type': 'multipart/form-data',
'x-access-handler': loggedinUser.token
}
};
const res = new Promise(function(resolve, reject) {
axios.post(API_URL + 'api/v1/postblogimage', formData, config)
.then((response) => {
if (response.data.error == 'false' || response.data.error == false) {
if (response.data.status == 200 && response.data.message == "Image uploaded successfully") {
const range = self.quill.getSelection(true);
// Insert temporary loading placeholder image
// this.quill.insertEmbed(range.index, 'image', `${window.location.origin}/images/loaders/placeholder.gif`);
// Move cursor to right side of image (easier to continue typing)
self.quill.setSelection(range.index + 1);
// Remove placeholder image
self.quill.deleteText(range.index, 1);
// Insert uploaded image
self.quill.insertEmbed(range.index, 'image', response.data.data[0].imageURL);
}
}
// }
}).catch((error) => {
// reject(Error("It broke"));
});
});
};
}
有人可以帮我这个忙吗,因为我已经尝试了很长时间,但是没有成功。 任何帮助或建议将不胜感激。
答案 0 :(得分:0)
One workaround is to change className in formats/image
module
您必须import it和register it again
import ReactQuill, { Quill } from "react-quill";
var Image = Quill.import('formats/image');
Image.className = 'custom-class-to-image';
Quill.register(Image, true);
import React, {useRef, useState, useEffect} from "react";
import ReactDOM from "react-dom";
import ReactQuill, { Quill } from "react-quill";
import 'react-quill/dist/quill.snow.css';
import "./styles.css";
var Image = Quill.import('formats/image');
Image.className = 'custom-class-to-image';
Quill.register(Image, true);
function App() {
const [value, setValue] = useState('');
const ref = useRef(null);
useEffect(() => {
if(ref && ref.current) {
const quill = ref.current;
const editor = quill.getEditor();
editor.insertEmbed(0, 'image', 'https://i.picsum.photos/id/211/200/300.jpg')
}
}, [ref])
return (
<div className="App">
<ReactQuill
ref={ref}
theme="snow"
value={value}
/>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);