Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Tweaks for Knossos Integration #122

Merged
merged 7 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/components/markdown-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { ref } from "vue";

const description = ref(null);
const description1 = ref(null);
const description2 = ref(null);
const description3 = ref(null);

Expand All @@ -27,6 +28,20 @@ const description = ref(null)
<MarkdownEditor v-model="description" />
```

## With options
<DemoContainer>
<MarkdownEditor v-model="description1" placeholder="Enter a description" max-length="30" />
</DemoContainer>

```vue
<script setup>
import { ref } from "vue";
const description = ref(null)
</script>

<MarkdownEditor v-model="description" placeholder="Enter a description" max-length="30" />
```

## With image upload
<DemoContainer>
<MarkdownEditor v-model="description2" :on-image-upload="onImageUpload" />
Expand All @@ -41,6 +56,9 @@ const description = ref(null)
const onImageUpload = (file: File): string => {
// Upload the file to your server and return a URL
// This example url will not work bc of proxy

// If the upload fails, throw an error and it will show as
// a Validation Error to the user
return URL.createObjectURL(file).replace("blob:", "");
};
</script>
Expand Down
2 changes: 0 additions & 2 deletions lib/assets/styles/classes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
:where(input) {
box-sizing: border-box;
max-height: 40px;
width: 24rem;
flex-basis: 24rem;

&:not(.stylized-toggle) {
max-width: 100%;
Expand Down
67 changes: 58 additions & 9 deletions lib/components/base/MarkdownEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,19 @@
</div>
<div ref="editorRef" :class="{ hide: previewMode }" />
<div v-if="!previewMode" class="info-blurb">
<InfoIcon />
<span>
This editor supports
<a href="https://docs.modrinth.com/docs/markdown" target="_blank">Markdown formatting</a>.
</span>
<div class="info-blurb">
<InfoIcon />
<span
>This editor supports
<a class="link" href="https://docs.modrinth.com/docs/markdown" target="_blank"
>Markdown formatting</a
>.</span
>
</div>
<div :class="{ hide: !props.maxLength }" class="max-length-label">
<span>Max length: </span>
<span>{{ props.maxLength ?? 'Unlimited' }}</span>
</div>
</div>
<div
v-if="previewMode"
Expand All @@ -242,7 +250,7 @@
import { type Component, computed, ref, onMounted, onBeforeUnmount } from 'vue'

import { EditorState } from '@codemirror/state'
import { EditorView, keymap } from '@codemirror/view'
import { EditorView, keymap, placeholder as cm_placeholder } from '@codemirror/view'
import { markdown } from '@codemirror/lang-markdown'
import { indentWithTab, historyKeymap, history } from '@codemirror/commands'

Expand All @@ -268,6 +276,7 @@ import {
Toggle,
FileInput,
UploadIcon,
InfoIcon,
Chips,
} from '@/components'
import { markdownCommands, modrinthMarkdownEditorKeymap } from '@/helpers/codemirror'
Expand All @@ -278,13 +287,21 @@ const props = withDefaults(
modelValue: string
disabled: boolean
headingButtons: boolean
/**
* @param file The file to upload
* @throws If the file is invalid or the upload fails
*/
onImageUpload?: (file: File) => Promise<string>
placeholder?: string
maxLength?: number
}>(),
{
modelValue: '',
disabled: false,
headingButtons: true,
onImageUpload: undefined,
placeholder: undefined,
maxLength: undefined,
}
)

Expand Down Expand Up @@ -321,7 +338,6 @@ onMounted(() => {
paste: (ev, view) => {
// If the user's pasting a url, automatically convert it to a link with the selection as the text or the url itself if no selection content.
const url = ev.clipboardData?.getData('text/plain')

if (url) {
try {
cleanUrl(url)
Expand All @@ -337,6 +353,35 @@ onMounted(() => {
const linkMarkdown = `[${linkText}](${url})`
return markdownCommands.replaceSelection(view, linkMarkdown)
}
// Check if the length of the document is greater than the max length. If it is, prevent the paste.
if (props.maxLength && view.state.doc.length > props.maxLength) {
ev.preventDefault()
return false
}
},
beforeinput: (ev, view) => {
if (props.maxLength && view.state.doc.length > props.maxLength) {
ev.preventDefault()
// Calculate how many characters to remove from the end
const excessLength = view.state.doc.length - props.maxLength
// Dispatch transaction to remove excess characters
view.dispatch({
changes: { from: view.state.doc.length - excessLength, to: view.state.doc.length },
selection: { anchor: props.maxLength, head: props.maxLength }, // Place cursor at the end
})
return true
}
},
blur: (_, view) => {
if (props.maxLength && view.state.doc.length > props.maxLength) {
// Calculate how many characters to remove from the end
const excessLength = view.state.doc.length - props.maxLength
// Dispatch transaction to remove excess characters
view.dispatch({
changes: { from: view.state.doc.length - excessLength, to: view.state.doc.length },
selection: { anchor: props.maxLength, head: props.maxLength }, // Place cursor at the end
})
}
},
})

Expand All @@ -353,6 +398,7 @@ onMounted(() => {
addKeymap: false,
}),
keymap.of(historyKeymap),
cm_placeholder(props.placeholder || ''),
],
})

Expand Down Expand Up @@ -532,6 +578,9 @@ const handleImageUpload = async (files: FileList) => {
linkUrl.value = url
validateURL()
} catch (error) {
if (error instanceof Error) {
linkValidationErrorMessage.value = error.message
}
console.error(error)
}
}
Expand Down Expand Up @@ -577,7 +626,7 @@ function openVideoModal() {
}
</script>

<style scoped>
<style lang="scss">
.display-options {
margin-bottom: var(--gap-sm);
}
Expand All @@ -586,7 +635,6 @@ function openVideoModal() {
display: flex;
align-items: center;
flex-wrap: wrap;
overflow: hidden;
justify-content: space-between;
margin-bottom: var(--gap-sm);
gap: var(--gap-xs);
Expand Down Expand Up @@ -635,6 +683,7 @@ function openVideoModal() {
.info-blurb {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--gap-xs);
}

Expand Down