-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): add easy-markdown-editor
- Loading branch information
1 parent
d57face
commit 0511cc7
Showing
9 changed files
with
416 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,62 @@ | ||
import { debounce } from "../../lib/utils/debounce.js"; | ||
import EasyMDE from "easymde"; | ||
|
||
export const TextareaFieldComponent = class extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
this.adjustHeight = this.adjustHeight.bind(this); | ||
this.editor = this.getAttribute("editor"); | ||
this.editorId = this.getAttribute("editor-id"); | ||
this.$textarea = this.querySelector("textarea"); | ||
} | ||
|
||
connectedCallback() { | ||
const delay = 100; | ||
if (this.editor !== "") { | ||
return; | ||
} | ||
|
||
this.$textarea.style.overflow = "hidden"; | ||
this.onResize = | ||
delay > 0 ? debounce(this.adjustHeight, delay) : this.adjustHeight; | ||
this.adjustHeight(); | ||
|
||
this.$textarea.addEventListener("input", this.adjustHeight); | ||
window.addEventListener("resize", this.onResize); | ||
new EasyMDE({ | ||
autosave: { | ||
enabled: true, | ||
uniqueId: this.editorId || this.$textarea.id, | ||
}, | ||
blockStyles: { | ||
bold: "**", | ||
italic: "_", | ||
}, | ||
element: this.$textarea, | ||
hideIcons: ["guide", "image"], | ||
// @ts-ignore | ||
showIcons: ["code", "upload-image"], | ||
imageUploadFunction: this.uploadFile, | ||
previewClass: ["editor-preview", "s-flow"], | ||
status: ["upload-image", "words", "autosave", "cursor"], | ||
uploadImage: true, | ||
unorderedListStyle: "-", | ||
}); | ||
} | ||
|
||
disconnectedCallback() { | ||
window.removeEventListener("resize", this.onResize); | ||
} | ||
/** | ||
* Upload file | ||
* @param {object} file - File | ||
* @param {Function} onSuccess - Success callback | ||
* @param {Function} onError - Error callback | ||
* @returns {Promise<string>} - File URL or error message | ||
*/ | ||
async uploadFile(file, onSuccess, onError) { | ||
const formData = new FormData(); | ||
formData.append("file", file); | ||
|
||
try { | ||
const endpointResponse = await fetch("http://localhost:3000/media", { | ||
method: "POST", | ||
body: formData, | ||
}); | ||
|
||
adjustHeight() { | ||
this.$textarea.style.height = "auto"; | ||
this.$textarea.style.height = `${this.$textarea.scrollHeight + 4}px`; | ||
return endpointResponse.ok | ||
? onSuccess(endpointResponse.headers.get("location")) | ||
: onError(endpointResponse.statusText); | ||
} catch (error) { | ||
onError(error.message); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
@import url("../../../../node_modules/codemirror/lib/codemirror.css"); | ||
|
||
.CodeMirror { | ||
--fieldset-flow-space: 0; | ||
|
||
background: var(--color-background); | ||
block-size: 60vh; | ||
border: var(--input-border-width) solid transparent; | ||
border-radius: var(--border-radius-small); | ||
color: var(--color-on-backround); | ||
font-family: var(--font-family-monospace); | ||
margin-block-start: 0; | ||
min-block-size: 30rem; | ||
padding: var(--space-s); | ||
|
||
& :focus-visible { | ||
box-shadow: none; | ||
} | ||
} | ||
|
||
.CodeMirror-focused:not(.CodeMirror-fullscreen) { | ||
border-color: var(--color-on-background); | ||
border-width: var(--input-border-width-focus); | ||
margin-inline: calc(var(--input-border-width-focus) * -1); | ||
outline: var(--input-border-width-focus) solid var(--color-focus); | ||
padding-block: calc(var(--space-s) - var(--input-border-width-focus-offset)); | ||
padding-inline: calc(var(--space-s) + var(--input-border-width-focus-offset)); | ||
} | ||
|
||
.CodeMirror-fullscreen { | ||
block-size: auto; | ||
inset: 0; | ||
inset-block-start: 50px; | ||
position: fixed; | ||
z-index: 8; | ||
} | ||
|
||
.CodeMirror-placeholder { | ||
color: var(--color-on-background); | ||
opacity: 0.5; | ||
} | ||
|
||
.CodeMirror-selected { | ||
background: var(--color-selection); | ||
} | ||
|
||
.CodeMirror-sided { | ||
inline-size: 50%; | ||
} |
Oops, something went wrong.