refac/enh: rich text input
This commit is contained in:
@@ -120,6 +120,53 @@
|
||||
export let image = false;
|
||||
export let fileHandler = false;
|
||||
|
||||
export let onFileDrop = (currentEditor, files, pos) => {
|
||||
files.forEach((file) => {
|
||||
const fileReader = new FileReader();
|
||||
|
||||
fileReader.readAsDataURL(file);
|
||||
fileReader.onload = () => {
|
||||
currentEditor
|
||||
.chain()
|
||||
.insertContentAt(pos, {
|
||||
type: 'image',
|
||||
attrs: {
|
||||
src: fileReader.result
|
||||
}
|
||||
})
|
||||
.focus()
|
||||
.run();
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export let onFilePaste = (currentEditor, files, htmlContent) => {
|
||||
files.forEach((file) => {
|
||||
if (htmlContent) {
|
||||
// if there is htmlContent, stop manual insertion & let other extensions handle insertion via inputRule
|
||||
// you could extract the pasted file from this url string and upload it to a server for example
|
||||
console.log(htmlContent); // eslint-disable-line no-console
|
||||
return false;
|
||||
}
|
||||
|
||||
const fileReader = new FileReader();
|
||||
|
||||
fileReader.readAsDataURL(file);
|
||||
fileReader.onload = () => {
|
||||
currentEditor
|
||||
.chain()
|
||||
.insertContentAt(currentEditor.state.selection.anchor, {
|
||||
type: 'image',
|
||||
attrs: {
|
||||
src: fileReader.result
|
||||
}
|
||||
})
|
||||
.focus()
|
||||
.run();
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export let id = '';
|
||||
export let value = '';
|
||||
export let html = '';
|
||||
@@ -847,57 +894,12 @@
|
||||
}
|
||||
}),
|
||||
CharacterCount.configure({}),
|
||||
|
||||
...(image ? [Image] : []),
|
||||
...(fileHandler
|
||||
? [
|
||||
FileHandler.configure({
|
||||
allowedMimeTypes: ['image/png', 'image/jpeg', 'image/gif', 'image/webp'],
|
||||
onDrop: (currentEditor, files, pos) => {
|
||||
files.forEach((file) => {
|
||||
const fileReader = new FileReader();
|
||||
|
||||
fileReader.readAsDataURL(file);
|
||||
fileReader.onload = () => {
|
||||
currentEditor
|
||||
.chain()
|
||||
.insertContentAt(pos, {
|
||||
type: 'image',
|
||||
attrs: {
|
||||
src: fileReader.result
|
||||
}
|
||||
})
|
||||
.focus()
|
||||
.run();
|
||||
};
|
||||
});
|
||||
},
|
||||
onPaste: (currentEditor, files, htmlContent) => {
|
||||
files.forEach((file) => {
|
||||
if (htmlContent) {
|
||||
// if there is htmlContent, stop manual insertion & let other extensions handle insertion via inputRule
|
||||
// you could extract the pasted file from this url string and upload it to a server for example
|
||||
console.log(htmlContent); // eslint-disable-line no-console
|
||||
return false;
|
||||
}
|
||||
|
||||
const fileReader = new FileReader();
|
||||
|
||||
fileReader.readAsDataURL(file);
|
||||
fileReader.onload = () => {
|
||||
currentEditor
|
||||
.chain()
|
||||
.insertContentAt(currentEditor.state.selection.anchor, {
|
||||
type: 'image',
|
||||
attrs: {
|
||||
src: fileReader.result
|
||||
}
|
||||
})
|
||||
.focus()
|
||||
.run();
|
||||
};
|
||||
});
|
||||
}
|
||||
onDrop: onFileDrop,
|
||||
onPaste: onFilePaste
|
||||
})
|
||||
]
|
||||
: []),
|
||||
|
||||
Reference in New Issue
Block a user