Skip to content

Commit

Permalink
refs #2877 Fixed validation issue with required attribute in SimpleMDE
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Aug 18, 2024
1 parent 357283f commit 509dbb6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion public/js/orchid.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/orchid.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 21 additions & 30 deletions resources/js/controllers/simplemde_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class extends ApplicationController {
static values = { text: String }

/**
* Returns the <textarea> element within the current controller element.
*
* @returns {Element}
*/
Expand All @@ -14,18 +15,29 @@ export default class extends ApplicationController {
}

/**
* Returns the input element with the 'upload' class within the current controller element.
*
* @returns {Element}
*/
get uploadInput() {
return this.element.querySelector('.upload');
}

/**
* Initializes the controller and sets up visibility tracking to
* only initialize the editor when the element comes into view.
*/
initialize() {

this.intersectionObserver = new IntersectionObserver((entries) => this.processIntersectionEntries(entries));
this.intersectionObserver.observe(this.element);
}

/**
* Handles IntersectionObserver entries. Initializes the SimpleMDE editor
* when the element becomes visible.
*
* @param {IntersectionObserverEntry[]} entries - Array of intersection observer entries.
*/
processIntersectionEntries(entries) {
entries.forEach((entry) => {
if (!entry.isIntersecting) {
Expand All @@ -34,17 +46,13 @@ export default class extends ApplicationController {

this.intersectionObserver.unobserve(this.element);
this.editor = this.initEditor();

});
}






/**
* Initializes the SimpleMDE editor with configuration and content synchronization.
*
* @returns {SimpleMDE} Returns the initialized SimpleMDE instance.
*/
initEditor() {
const editor = new SimpleMDE({
Expand Down Expand Up @@ -143,43 +151,26 @@ export default class extends ApplicationController {
title: 'Insert Horizontal Line',
},
],
initialValue: this.decodeHtmlJson(this.textValue),
initialValue: JSON.parse(this.textValue),
placeholder: this.textarea.placeholder,
spellChecker: false,
});


// Required attribute https://github.com/sparksuite/simplemde-markdown-editor/issues/324
if (this.textarea.required) {
this.element.querySelector('.CodeMirror textarea').required = true;
}

return editor;
}

/**
*
* @param json
* @returns {string}
*/
decodeHtmlJson(json) {
let text = document.createElement("textarea");
text.innerHTML = JSON.parse(json);

return text.value;
}

/**
*
* Opens the file selection dialog.
*/
showDialogUpload() {
this.uploadInput.click();
}


/**
* Handles file upload, sends the file to the server, and inserts the file URL into the editor.
*
* @param event
* @param {Event} event - The file upload event.
*/
upload(event) {
const file = event.target.files[0];
Expand All @@ -195,11 +186,11 @@ export default class extends ApplicationController {
.post(this.prefix('/systems/files'), formData)
.then((response) => {
this.editor.codemirror.replaceSelection(response.data.url);
event.target.value = null;
event.target.value = null; // Reset input after upload
})
.catch((error) => {
console.warn(error);
event.target.value = null;
event.target.value = null; // Reset input after upload
});
}
}

0 comments on commit 509dbb6

Please sign in to comment.