-
-
Notifications
You must be signed in to change notification settings - Fork 271
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
Add direct image upload to the EasyMDE field #3597
Changes from 4 commits
832362f
388ec3f
4a2c632
d87a912
b807555
94ad7fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,36 +1,106 @@ | ||||||
import { Controller } from '@hotwired/stimulus' | ||||||
import EasyMDE from 'easymde' | ||||||
import { Controller } from "@hotwired/stimulus"; | ||||||
import EasyMDE from "easymde"; | ||||||
import { DirectUpload } from "@rails/activestorage"; | ||||||
|
||||||
export default class extends Controller { | ||||||
static targets = ['element'] | ||||||
static targets = ["element"]; | ||||||
|
||||||
get view() { | ||||||
return this.elementTarget.dataset.view | ||||||
return this.elementTarget.dataset.view; | ||||||
} | ||||||
|
||||||
get componentOptions() { | ||||||
try { | ||||||
return JSON.parse(this.elementTarget.dataset.componentOptions) | ||||||
return JSON.parse(this.elementTarget.dataset.componentOptions); | ||||||
} catch (error) { | ||||||
return {} | ||||||
return {}; | ||||||
} | ||||||
} | ||||||
|
||||||
connect() { | ||||||
console.log("Hello from the connection") | ||||||
|
||||||
const options = { | ||||||
element: this.elementTarget, | ||||||
spellChecker: this.componentOptions.spell_checker, | ||||||
autoRefresh: { delay: 500}, | ||||||
autoRefresh: { delay: 500 }, | ||||||
}; | ||||||
|
||||||
if (this.view === "show") { | ||||||
options.toolbar = false; | ||||||
options.status = false; | ||||||
} | ||||||
|
||||||
if (this.view === 'show') { | ||||||
options.toolbar = false | ||||||
options.status = false | ||||||
|
||||||
if (this.componentOptions.image_upload) { | ||||||
this._configureImageUploads(options); | ||||||
} | ||||||
|
||||||
const easyMde = new EasyMDE(options) | ||||||
if (this.view === 'show') { | ||||||
easyMde.codemirror.options.readOnly = true | ||||||
const easyMde = new EasyMDE(options); | ||||||
if (this.view === "show") { | ||||||
easyMde.codemirror.options.readOnly = true; | ||||||
} | ||||||
} | ||||||
|
||||||
_configureImageUploads(options) { | ||||||
options.uploadImage = true; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use the JS private notiation please.
Suggested change
Then can be used liek so |
||||||
options.imageUploadEndpoint = this.elementTarget.dataset.uploadUrl; | ||||||
options.imageUploadFunction = this._handleImageUpload.bind(this); | ||||||
options.imageAccept = "image/*"; | ||||||
options.previewImagesInEditor = true; | ||||||
options.toolbar = this.toolbarItems; | ||||||
return options; | ||||||
} | ||||||
|
||||||
_handleImageUpload(file, onSuccess, onError) { | ||||||
const upload = new DirectUpload(file, this.elementTarget.dataset.uploadUrl); | ||||||
upload.create((error, blob) => { | ||||||
if (error) return onError(error); | ||||||
const imageUrl = this._encodedImageUrl(blob); | ||||||
onSuccess(imageUrl); | ||||||
}); | ||||||
} | ||||||
|
||||||
_encodedImageUrl(blob) { | ||||||
return `/rails/active_storage/blobs/redirect/${ | ||||||
blob.signed_id | ||||||
}/${encodeURIComponent(blob.filename)}`; | ||||||
} | ||||||
|
||||||
get toolbarItems() { | ||||||
const baseItems = [ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||||||
"bold", | ||||||
"italic", | ||||||
"heading", | ||||||
"|", | ||||||
"quote", | ||||||
"unordered-list", | ||||||
"ordered-list", | ||||||
"|", | ||||||
"link", | ||||||
"image", | ||||||
]; | ||||||
|
||||||
const uploadImageItem = this.componentOptions.image_upload | ||||||
? [ | ||||||
{ | ||||||
name: "upload-image", | ||||||
action: EasyMDE.drawUploadedImage, | ||||||
className: "fa fa-file-picture-o", | ||||||
title: "Upload & insert image", | ||||||
}, | ||||||
] | ||||||
: []; | ||||||
|
||||||
return [ | ||||||
...baseItems, | ||||||
...uploadImageItem, | ||||||
"|", | ||||||
"preview", | ||||||
"side-by-side", | ||||||
"fullscreen", | ||||||
"|", | ||||||
"guide", | ||||||
]; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unrelated