Skip to content

Commit

Permalink
Image-validation (#144)
Browse files Browse the repository at this point in the history
* Add spoiler tag functionality to CodeMirror

* Refactor image insertion logic in
MarkdownEditor.vue

* Refactor modal state reset functions in
MarkdownEditor.vue
  • Loading branch information
darling committed Nov 15, 2023
1 parent fcda489 commit 3eead12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 18 additions & 7 deletions lib/components/base/MarkdownEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<Button :action="() => imageModal?.hide()"><XIcon /> Cancel</Button>
<Button
color="primary"
:disabled="linkValidationErrorMessage || !linkUrl"
:disabled="!canInsertImage"
:action="
() => {
if (editor) markdownCommands.replaceSelection(editor, imageMarkdown)
Expand Down Expand Up @@ -607,6 +607,14 @@ const handleImageUpload = async (files: FileList) => {
const imageUploadOption = ref<string>('upload')
const imageMarkdown = computed(() => (linkMarkdown.value.length ? `!${linkMarkdown.value}` : ''))
const canInsertImage = computed(() => {
// Make sure the image url is valid, there is an image url, and there is alt text
// They need to be valid, and not empty
return (
!linkValidationErrorMessage.value && linkUrl.value?.length > 0 && linkText.value?.length > 0
)
})
const youtubeRegex =
/^(?:https?:)?(?:\/\/)?(?:youtu\.be\/|(?:www\.|m\.)?youtube\.com\/(?:watch|v|embed)(?:\.php)?(?:\?.*v=|\/))([a-zA-Z0-9_-]{7,15})(?:[?&][a-zA-Z0-9_-]+=[a-zA-Z0-9_-]+)*$/
Expand All @@ -623,22 +631,25 @@ const linkModal = ref<InstanceType<typeof Modal> | null>(null)
const imageModal = ref<InstanceType<typeof Modal> | null>(null)
const videoModal = ref<InstanceType<typeof Modal> | null>(null)
function resetModalStates() {
linkText.value = ''
linkUrl.value = ''
linkValidationErrorMessage.value = undefined
}
function openLinkModal() {
if (editor) linkText.value = markdownCommands.yankSelection(editor)
linkUrl.value = ''
resetModalStates()
linkModal.value?.show()
}
function openImageModal() {
linkValidationErrorMessage.value = undefined
linkText.value = ''
linkUrl.value = ''
resetModalStates()
imageModal.value?.show()
}
function openVideoModal() {
linkText.value = ''
linkUrl.value = ''
resetModalStates()
videoModal.value?.show()
}
</script>
Expand Down
4 changes: 3 additions & 1 deletion lib/helpers/codemirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const toggleCodeBlock: Command = ({ state, dispatch }) => {
}

const toggleSpoiler: Command = ({ state, dispatch }) => {
return toggleAround(state, dispatch, '||', '||')
// Insert details tag with a summary tag at the start
const detailsTags = ['\n<details>\n<summary>Spoiler</summary>\n\n', '\n\n</details>\n\n']
return toggleAround(state, dispatch, detailsTags[0], detailsTags[1])
}

const toggleHeader: Command = ({ state, dispatch }) => {
Expand Down

0 comments on commit 3eead12

Please sign in to comment.