Image-validation (#144)

* Add spoiler tag functionality to CodeMirror

* Refactor image insertion logic in
MarkdownEditor.vue

* Refactor modal state reset functions in
MarkdownEditor.vue
This commit is contained in:
Carter
2023-11-15 14:36:34 -05:00
committed by GitHub
parent fcda48903b
commit 3eead128a6
2 changed files with 21 additions and 8 deletions

View File

@@ -135,7 +135,7 @@
<Button :action="() => imageModal?.hide()"><XIcon /> Cancel</Button> <Button :action="() => imageModal?.hide()"><XIcon /> Cancel</Button>
<Button <Button
color="primary" color="primary"
:disabled="linkValidationErrorMessage || !linkUrl" :disabled="!canInsertImage"
:action=" :action="
() => { () => {
if (editor) markdownCommands.replaceSelection(editor, imageMarkdown) if (editor) markdownCommands.replaceSelection(editor, imageMarkdown)
@@ -607,6 +607,14 @@ const handleImageUpload = async (files: FileList) => {
const imageUploadOption = ref<string>('upload') const imageUploadOption = ref<string>('upload')
const imageMarkdown = computed(() => (linkMarkdown.value.length ? `!${linkMarkdown.value}` : '')) 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 = 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_-]+)*$/ /^(?: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_-]+)*$/
@@ -623,22 +631,25 @@ const linkModal = ref<InstanceType<typeof Modal> | null>(null)
const imageModal = ref<InstanceType<typeof Modal> | null>(null) const imageModal = ref<InstanceType<typeof Modal> | null>(null)
const videoModal = 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() { function openLinkModal() {
if (editor) linkText.value = markdownCommands.yankSelection(editor) if (editor) linkText.value = markdownCommands.yankSelection(editor)
linkUrl.value = '' resetModalStates()
linkModal.value?.show() linkModal.value?.show()
} }
function openImageModal() { function openImageModal() {
linkValidationErrorMessage.value = undefined resetModalStates()
linkText.value = ''
linkUrl.value = ''
imageModal.value?.show() imageModal.value?.show()
} }
function openVideoModal() { function openVideoModal() {
linkText.value = '' resetModalStates()
linkUrl.value = ''
videoModal.value?.show() videoModal.value?.show()
} }
</script> </script>

View File

@@ -23,7 +23,9 @@ const toggleCodeBlock: Command = ({ state, dispatch }) => {
} }
const toggleSpoiler: 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 }) => { const toggleHeader: Command = ({ state, dispatch }) => {