Skip to content

Commit

Permalink
make it compatible with Avo's media library
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Jan 31, 2025
1 parent e3635b8 commit a1dda3a
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 272 deletions.
1 change: 1 addition & 0 deletions app/assets/images/marksmith/svgs/gallery.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/assets/images/marksmith/svgs/markdown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/assets/images/marksmith/svgs/paperclip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 35 additions & 15 deletions app/assets/javascripts/marksmith_controller-full.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2825,6 +2825,8 @@ var MarksmithController = (function () {
previewUrl: String,
extraPreviewParams: { type: Object, default: {} },
fieldId: String,
galleryEnabled: { type: Boolean, default: false },
galleryOpenPath: String,
}

static targets = ['fieldContainer', 'fieldElement', 'previewElement', 'writeTabButton', 'previewTabButton', 'toolbar']
Expand Down Expand Up @@ -2877,14 +2879,14 @@ var MarksmithController = (function () {

dropUpload(event) {
event.preventDefault();
this.uploadFiles(event.dataTransfer.files);
this.#uploadFiles(event.dataTransfer.files);
}

pasteUpload(event) {
if (!event.clipboardData.files.length) return

event.preventDefault();
this.uploadFiles(event.clipboardData.files);
this.#uploadFiles(event.clipboardData.files);
}

buttonUpload(event) {
Expand All @@ -2896,40 +2898,58 @@ var MarksmithController = (function () {
fileInput.accept = 'image/*,.pdf,.doc,.docx,.txt';

fileInput.addEventListener('change', (e) => {
this.uploadFiles(e.target.files);
this.#uploadFiles(e.target.files);
});

fileInput.click();
}

uploadFiles(files) {
Array.from(files).forEach((file) => this.uploadFile(file));
// Invoked by the other controllers (media-library)
insertAttachments(attachments, event) {
const editorAttachments = attachments.map((attachment) => {
const { blob, path, url } = attachment;
const link = this.#markdownLinkFromUrl(blob.filename, path, blob.content_type);

this.#injectLink(link);
});

this.editor?.chain().focus().setAttachment(editorAttachments).run();
}

#uploadFiles(files) {
Array.from(files).forEach((file) => this.#uploadFile(file));
}

uploadFile(file) {
#uploadFile(file) {
const upload = new DirectUpload(file, this.attachUrlValue);

upload.create((error, blob) => {
if (error) {
console.log('Error', error);
} else {
const text = this.markdownLink(blob);
const start = this.fieldElementTarget.selectionStart;
const end = this.fieldElementTarget.selectionEnd;
this.fieldElementTarget.setRangeText(text, start, end);
const link = this.#markdownLinkFromUrl(blob.filename, this.#pathFromBlob(blob), blob.content_type);
this.#injectLink(link);
}
});
}

markdownLink(blob) {
const { filename } = blob;
const url = `/rails/active_storage/blobs/${blob.signed_id}/${filename}`;
const prefix = (this.isImage(blob.content_type) ? '!' : '');
#injectLink(link) {
const start = this.fieldElementTarget.selectionStart;
const end = this.fieldElementTarget.selectionEnd;
this.fieldElementTarget.setRangeText(link, start, end);
}

#pathFromBlob(blob) {
return `/rails/active_storage/blobs/redirect/${blob.signed_id}/${blob.filename}`
}

#markdownLinkFromUrl(filename, url, contentType) {
const prefix = (this.#isImage(contentType) ? '!' : '');

return `${prefix}[${filename}](${url})\n`
}

isImage(contentType) {
#isImage(contentType) {
return ['image/jpeg', 'image/gif', 'image/png'].includes(contentType)
}
}
Expand Down
50 changes: 35 additions & 15 deletions app/assets/javascripts/marksmith_controller-no-stimulus.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,8 @@ var MarksmithController = (function (stimulus) {
previewUrl: String,
extraPreviewParams: { type: Object, default: {} },
fieldId: String,
galleryEnabled: { type: Boolean, default: false },
galleryOpenPath: String,
}

static targets = ['fieldContainer', 'fieldElement', 'previewElement', 'writeTabButton', 'previewTabButton', 'toolbar']
Expand Down Expand Up @@ -2387,14 +2389,14 @@ var MarksmithController = (function (stimulus) {

dropUpload(event) {
event.preventDefault();
this.uploadFiles(event.dataTransfer.files);
this.#uploadFiles(event.dataTransfer.files);
}

pasteUpload(event) {
if (!event.clipboardData.files.length) return

event.preventDefault();
this.uploadFiles(event.clipboardData.files);
this.#uploadFiles(event.clipboardData.files);
}

buttonUpload(event) {
Expand All @@ -2406,40 +2408,58 @@ var MarksmithController = (function (stimulus) {
fileInput.accept = 'image/*,.pdf,.doc,.docx,.txt';

fileInput.addEventListener('change', (e) => {
this.uploadFiles(e.target.files);
this.#uploadFiles(e.target.files);
});

fileInput.click();
}

uploadFiles(files) {
Array.from(files).forEach((file) => this.uploadFile(file));
// Invoked by the other controllers (media-library)
insertAttachments(attachments, event) {
const editorAttachments = attachments.map((attachment) => {
const { blob, path, url } = attachment;
const link = this.#markdownLinkFromUrl(blob.filename, path, blob.content_type);

this.#injectLink(link);
});

this.editor?.chain().focus().setAttachment(editorAttachments).run();
}

#uploadFiles(files) {
Array.from(files).forEach((file) => this.#uploadFile(file));
}

uploadFile(file) {
#uploadFile(file) {
const upload = new DirectUpload(file, this.attachUrlValue);

upload.create((error, blob) => {
if (error) {
console.log('Error', error);
} else {
const text = this.markdownLink(blob);
const start = this.fieldElementTarget.selectionStart;
const end = this.fieldElementTarget.selectionEnd;
this.fieldElementTarget.setRangeText(text, start, end);
const link = this.#markdownLinkFromUrl(blob.filename, this.#pathFromBlob(blob), blob.content_type);
this.#injectLink(link);
}
});
}

markdownLink(blob) {
const { filename } = blob;
const url = `/rails/active_storage/blobs/${blob.signed_id}/${filename}`;
const prefix = (this.isImage(blob.content_type) ? '!' : '');
#injectLink(link) {
const start = this.fieldElementTarget.selectionStart;
const end = this.fieldElementTarget.selectionEnd;
this.fieldElementTarget.setRangeText(link, start, end);
}

#pathFromBlob(blob) {
return `/rails/active_storage/blobs/redirect/${blob.signed_id}/${blob.filename}`
}

#markdownLinkFromUrl(filename, url, contentType) {
const prefix = (this.#isImage(contentType) ? '!' : '');

return `${prefix}[${filename}](${url})\n`
}

isImage(contentType) {
#isImage(contentType) {
return ['image/jpeg', 'image/gif', 'image/png'].includes(contentType)
}
}
Expand Down
10 changes: 5 additions & 5 deletions app/assets/stylesheets/marksmith.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*! tailwindcss v4.0.0-beta.10 | MIT License | https://tailwindcss.com */
:root {
/*! tailwindcss v4.0.1 | MIT License | https://tailwindcss.com */
:root, :host {
--ms-font-sans: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji',
'Segoe UI Symbol', 'Noto Color Emoji';
--ms-font-serif: ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif;
Expand Down Expand Up @@ -788,6 +788,9 @@
margin-bottom: 0;
}
}
.ms\:mr-1 {
margin-right: calc(var(--ms-spacing) * 1);
}
.ms\:block {
display: block;
}
Expand Down Expand Up @@ -841,9 +844,6 @@
.ms\:items-center {
align-items: center;
}
.ms\:gap-x-2 {
column-gap: calc(var(--ms-spacing) * 2);
}
.ms\:gap-y-1 {
row-gap: calc(var(--ms-spacing) * 1);
}
Expand Down
14 changes: 14 additions & 0 deletions app/components/marksmith/markdown_field/edit_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<%= field_wrapper **field_wrapper_args, full_width: true do %>
<%= @form.marksmith @field.id,
gallery: {
enabled: true,
open_path: avo.attach_media_path,
turbo_frame: ::Avo::MODAL_FRAME_ID,
params: {
resource_name: @resource.singular_route_key,
controller_name: "marksmith",
controller_selector: "[data-unique-selector='#{unique_id}']",
record_id: @resource&.record&.to_param,
}
},
controller_data_attributes: {
unique_selector: unique_id, # it must coincide with the selector above
},
extra_preview_params: {
resource_class: @resource.class.name,
field_id: field.id,
Expand Down
3 changes: 3 additions & 0 deletions app/components/marksmith/markdown_field/edit_component.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

class Marksmith::MarkdownField::EditComponent < Avo::Fields::EditComponent
def unique_id
[@field.type, @resource&.singular_route_key, @field.id].compact.join("_")

Check failure on line 5 in app/components/marksmith/markdown_field/edit_component.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 5 in app/components/marksmith/markdown_field/edit_component.rb

View workflow job for this annotation

GitHub Actions / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class extends Controller {
previewUrl: String,
extraPreviewParams: { type: Object, default: {} },
fieldId: String,
galleryEnabled: { type: Boolean, default: false },
galleryOpenPath: String,
}

static targets = ['fieldContainer', 'fieldElement', 'previewElement', 'writeTabButton', 'previewTabButton', 'toolbar']
Expand Down Expand Up @@ -67,14 +69,14 @@ export default class extends Controller {

dropUpload(event) {
event.preventDefault()
this.uploadFiles(event.dataTransfer.files)
this.#uploadFiles(event.dataTransfer.files)
}

pasteUpload(event) {
if (!event.clipboardData.files.length) return

event.preventDefault()
this.uploadFiles(event.clipboardData.files)
this.#uploadFiles(event.clipboardData.files)
}

buttonUpload(event) {
Expand All @@ -86,40 +88,58 @@ export default class extends Controller {
fileInput.accept = 'image/*,.pdf,.doc,.docx,.txt'

fileInput.addEventListener('change', (e) => {
this.uploadFiles(e.target.files)
this.#uploadFiles(e.target.files)
})

fileInput.click()
}

uploadFiles(files) {
Array.from(files).forEach((file) => this.uploadFile(file))
// Invoked by the other controllers (media-library)
insertAttachments(attachments, event) {
const editorAttachments = attachments.map((attachment) => {
const { blob, path, url } = attachment
const link = this.#markdownLinkFromUrl(blob.filename, path, blob.content_type)

this.#injectLink(link)
})

this.editor?.chain().focus().setAttachment(editorAttachments).run();
}

#uploadFiles(files) {
Array.from(files).forEach((file) => this.#uploadFile(file))
}

uploadFile(file) {
#uploadFile(file) {
const upload = new DirectUpload(file, this.attachUrlValue)

upload.create((error, blob) => {
if (error) {
console.log('Error', error)
} else {
const text = this.markdownLink(blob)
const start = this.fieldElementTarget.selectionStart
const end = this.fieldElementTarget.selectionEnd
this.fieldElementTarget.setRangeText(text, start, end)
const link = this.#markdownLinkFromUrl(blob.filename, this.#pathFromBlob(blob), blob.content_type)
this.#injectLink(link)
}
})
}

markdownLink(blob) {
const { filename } = blob
const url = `/rails/active_storage/blobs/${blob.signed_id}/${filename}`
const prefix = (this.isImage(blob.content_type) ? '!' : '')
#injectLink(link) {
const start = this.fieldElementTarget.selectionStart
const end = this.fieldElementTarget.selectionEnd
this.fieldElementTarget.setRangeText(link, start, end)
}

#pathFromBlob(blob) {
return `/rails/active_storage/blobs/redirect/${blob.signed_id}/${blob.filename}`
}

#markdownLinkFromUrl(filename, url, contentType) {
const prefix = (this.#isImage(contentType) ? '!' : '')

return `${prefix}[${filename}](${url})\n`
}

isImage(contentType) {
#isImage(contentType) {
return ['image/jpeg', 'image/gif', 'image/png'].includes(contentType)
}
}
Loading

0 comments on commit a1dda3a

Please sign in to comment.