|
|
|
@ -4,7 +4,7 @@
|
|
|
|
|
|
|
|
|
|
<vue-easymde
|
|
|
|
|
:configs="config"
|
|
|
|
|
@change="bubble"
|
|
|
|
|
@change="() => bubble()"
|
|
|
|
|
@update:modelValue="handleInput"
|
|
|
|
|
class="content"
|
|
|
|
|
v-if="isEditActive"
|
|
|
|
@ -66,245 +66,245 @@
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
|
import {defineComponent} from 'vue'
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import {computed, nextTick, onMounted, ref, toRefs, watch} from 'vue'
|
|
|
|
|
|
|
|
|
|
import VueEasymde from './vue-easymde.vue'
|
|
|
|
|
import {marked} from 'marked'
|
|
|
|
|
import DOMPurify from 'dompurify'
|
|
|
|
|
import {setupMarkdownRenderer} from '@/helpers/markdownRenderer'
|
|
|
|
|
|
|
|
|
|
import {createEasyMDEConfig} from './editorConfig'
|
|
|
|
|
|
|
|
|
|
import AttachmentModel from '../../models/attachment'
|
|
|
|
|
import AttachmentService from '../../services/attachment'
|
|
|
|
|
import {findCheckboxesInText} from '../../helpers/checklistFromText'
|
|
|
|
|
import AttachmentModel from '@/models/attachment'
|
|
|
|
|
import AttachmentService from '@/services/attachment'
|
|
|
|
|
|
|
|
|
|
import {setupMarkdownRenderer} from '@/helpers/markdownRenderer'
|
|
|
|
|
import {findCheckboxesInText} from '@/helpers/checklistFromText'
|
|
|
|
|
import {createRandomID} from '@/helpers/randomId'
|
|
|
|
|
|
|
|
|
|
import BaseButton from '@/components/base/BaseButton.vue'
|
|
|
|
|
import ButtonLink from '@/components/misc/ButtonLink.vue'
|
|
|
|
|
import type { IAttachment } from '@/modelTypes/IAttachment'
|
|
|
|
|
import type { ITask } from '@/modelTypes/ITask'
|
|
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
|
name: 'editor',
|
|
|
|
|
components: {
|
|
|
|
|
VueEasymde,
|
|
|
|
|
BaseButton,
|
|
|
|
|
ButtonLink,
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
modelValue: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
modelValue: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
placeholder: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
uploadEnabled: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
uploadCallback: {
|
|
|
|
|
type: Function,
|
|
|
|
|
},
|
|
|
|
|
hasPreview: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
previewIsDefault: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
isEditEnabled: {
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
bottomActions: {
|
|
|
|
|
default: () => [],
|
|
|
|
|
},
|
|
|
|
|
emptyText: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
showSave: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
// If a key is passed the editor will go in "edit" mode when the key is pressed.
|
|
|
|
|
// Disabled if an empty string is passed.
|
|
|
|
|
editShortcut: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
placeholder: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
emits: ['update:modelValue'],
|
|
|
|
|
computed: {
|
|
|
|
|
showPreviewText() {
|
|
|
|
|
return this.isPreviewActive && this.text === '' && this.emptyText !== ''
|
|
|
|
|
},
|
|
|
|
|
showEditButton() {
|
|
|
|
|
return !this.isEditActive && this.text !== ''
|
|
|
|
|
},
|
|
|
|
|
uploadEnabled: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
text: '',
|
|
|
|
|
changeTimeout: null,
|
|
|
|
|
isEditActive: false,
|
|
|
|
|
isPreviewActive: true,
|
|
|
|
|
|
|
|
|
|
preview: '',
|
|
|
|
|
attachmentService: null,
|
|
|
|
|
loadedAttachments: {},
|
|
|
|
|
config: createEasyMDEConfig({
|
|
|
|
|
placeholder: this.placeholder,
|
|
|
|
|
uploadImage: this.uploadEnabled,
|
|
|
|
|
imageUploadFunction: this.uploadCallback,
|
|
|
|
|
}),
|
|
|
|
|
checkboxId: createRandomID(),
|
|
|
|
|
}
|
|
|
|
|
uploadCallback: {
|
|
|
|
|
type: Function,
|
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
modelValue(modelValue) {
|
|
|
|
|
this.text = modelValue
|
|
|
|
|
this.$nextTick(this.renderPreview)
|
|
|
|
|
},
|
|
|
|
|
text(newVal, oldVal) {
|
|
|
|
|
// Only bubble the new value if it actually changed, but not if the component just got mounted and the text changed from the outside.
|
|
|
|
|
if (oldVal === '' && this.text === this.modelValue) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.bubble()
|
|
|
|
|
},
|
|
|
|
|
hasPreview: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
mounted() {
|
|
|
|
|
if (this.modelValue !== '') {
|
|
|
|
|
this.text = this.modelValue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.previewIsDefault && this.hasPreview) {
|
|
|
|
|
this.$nextTick(this.renderPreview)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.isPreviewActive = false
|
|
|
|
|
this.isEditActive = true
|
|
|
|
|
previewIsDefault: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
// This gets triggered when only pasting content into the editor.
|
|
|
|
|
// A change event would not get generated by that, an input event does.
|
|
|
|
|
// Therefore, we're using this handler to catch paste events.
|
|
|
|
|
// But because this also gets triggered when typing into the editor, we give
|
|
|
|
|
// it a higher timeout to make the timouts cancel each other in that case so
|
|
|
|
|
// that in the end, only one change event is triggered to the outside per change.
|
|
|
|
|
handleInput(val) {
|
|
|
|
|
// Don't bubble if the text is up to date
|
|
|
|
|
if (val === this.text) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.text = val
|
|
|
|
|
this.bubble(1000)
|
|
|
|
|
},
|
|
|
|
|
bubble(timeout = 500) {
|
|
|
|
|
if (this.changeTimeout !== null) {
|
|
|
|
|
clearTimeout(this.changeTimeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.changeTimeout = setTimeout(() => {
|
|
|
|
|
this.$emit('update:modelValue', this.text)
|
|
|
|
|
}, timeout)
|
|
|
|
|
},
|
|
|
|
|
replaceAt(str, index, replacement) {
|
|
|
|
|
return str.substr(0, index) + replacement + str.substr(index + replacement.length)
|
|
|
|
|
},
|
|
|
|
|
findNthIndex(str, n) {
|
|
|
|
|
const checkboxes = findCheckboxesInText(str)
|
|
|
|
|
return checkboxes[n]
|
|
|
|
|
},
|
|
|
|
|
renderPreview() {
|
|
|
|
|
setupMarkdownRenderer(this.checkboxId)
|
|
|
|
|
|
|
|
|
|
this.preview = DOMPurify.sanitize(marked(this.text), {ADD_ATTR: ['target']})
|
|
|
|
|
|
|
|
|
|
// Since the render function is synchronous, we can't do async http requests in it.
|
|
|
|
|
// Therefore, we can't resolve the blob url at (markdown) compile time.
|
|
|
|
|
// To work around this, we modify the url after rendering it in the vue component.
|
|
|
|
|
// We're doing the whole thing in the next tick to ensure the image elements are available in the
|
|
|
|
|
// dom tree. If we're calling this right after setting this.preview it could be the images were
|
|
|
|
|
// not already made available.
|
|
|
|
|
// Some docs at https://stackoverflow.com/q/62865160/10924593
|
|
|
|
|
this.$nextTick(async () => {
|
|
|
|
|
const attachmentImage = document.getElementsByClassName('attachment-image')
|
|
|
|
|
if (attachmentImage) {
|
|
|
|
|
for (const img of attachmentImage) {
|
|
|
|
|
// The url is something like /tasks/<id>/attachments/<id>
|
|
|
|
|
const parts = img.dataset.src.substr(window.API_URL.length + 1).split('/')
|
|
|
|
|
const taskId = parseInt(parts[1])
|
|
|
|
|
const attachmentId = parseInt(parts[3])
|
|
|
|
|
const cacheKey = `${taskId}-${attachmentId}`
|
|
|
|
|
|
|
|
|
|
if (typeof this.loadedAttachments[cacheKey] !== 'undefined') {
|
|
|
|
|
img.src = this.loadedAttachments[cacheKey]
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const attachment = new AttachmentModel({taskId: taskId, id: attachmentId})
|
|
|
|
|
|
|
|
|
|
if (this.attachmentService === null) {
|
|
|
|
|
this.attachmentService = new AttachmentService()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const url = await this.attachmentService.getBlobUrl(attachment)
|
|
|
|
|
img.src = url
|
|
|
|
|
this.loadedAttachments[cacheKey] = url
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const textCheckbox = document.getElementsByClassName(`text-checkbox-${this.checkboxId}`)
|
|
|
|
|
if (textCheckbox) {
|
|
|
|
|
for (const check of textCheckbox) {
|
|
|
|
|
check.removeEventListener('change', this.handleCheckboxClick)
|
|
|
|
|
check.addEventListener('change', this.handleCheckboxClick)
|
|
|
|
|
check.parentElement.classList.add('has-checkbox')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
handleCheckboxClick(e) {
|
|
|
|
|
// Find the original markdown checkbox this is targeting
|
|
|
|
|
const checked = e.target.checked
|
|
|
|
|
const numMarkdownCheck = parseInt(e.target.dataset.checkboxNum)
|
|
|
|
|
|
|
|
|
|
const index = this.findNthIndex(this.text, numMarkdownCheck)
|
|
|
|
|
if (index < 0 || typeof index === 'undefined') {
|
|
|
|
|
console.debug('no index found')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
console.debug(index, this.text.substr(index, 9))
|
|
|
|
|
|
|
|
|
|
const listPrefix = this.text.substr(index, 1)
|
|
|
|
|
|
|
|
|
|
if (checked) {
|
|
|
|
|
this.text = this.replaceAt(this.text, index, `${listPrefix} [x] `)
|
|
|
|
|
} else {
|
|
|
|
|
this.text = this.replaceAt(this.text, index, `${listPrefix} [ ] `)
|
|
|
|
|
}
|
|
|
|
|
this.bubble()
|
|
|
|
|
this.renderPreview()
|
|
|
|
|
},
|
|
|
|
|
toggleEdit() {
|
|
|
|
|
if (this.isEditActive) {
|
|
|
|
|
this.isPreviewActive = true
|
|
|
|
|
this.isEditActive = false
|
|
|
|
|
this.renderPreview()
|
|
|
|
|
this.bubble(0) // save instantly
|
|
|
|
|
} else {
|
|
|
|
|
this.isPreviewActive = false
|
|
|
|
|
this.isEditActive = true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
isEditEnabled: {
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
bottomActions: {
|
|
|
|
|
default: () => [],
|
|
|
|
|
},
|
|
|
|
|
emptyText: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
showSave: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
// If a key is passed the editor will go in "edit" mode when the key is pressed.
|
|
|
|
|
// Disabled if an empty string is passed.
|
|
|
|
|
editShortcut: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits(['update:modelValue'])
|
|
|
|
|
|
|
|
|
|
const text = ref('')
|
|
|
|
|
const changeTimeout = ref<ReturnType<typeof setTimeout> | null>(null)
|
|
|
|
|
const isEditActive = ref(false)
|
|
|
|
|
const isPreviewActive = ref(true)
|
|
|
|
|
|
|
|
|
|
const showPreviewText = computed(() => isPreviewActive.value && text.value === '' && props.emptyText !== '')
|
|
|
|
|
const showEditButton = computed(() => !isEditActive.value && text.value !== '')
|
|
|
|
|
|
|
|
|
|
const preview = ref('')
|
|
|
|
|
const attachmentService = new AttachmentService()
|
|
|
|
|
|
|
|
|
|
type CacheKey = `${ITask['id']}-${IAttachment['id']}`
|
|
|
|
|
const loadedAttachments = ref<{[key: CacheKey]: string}>({})
|
|
|
|
|
const config = ref(createEasyMDEConfig({
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
uploadImage: props.uploadEnabled,
|
|
|
|
|
imageUploadFunction: props.uploadCallback,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
const checkboxId = ref(createRandomID())
|
|
|
|
|
|
|
|
|
|
const {modelValue} = toRefs(props)
|
|
|
|
|
|
|
|
|
|
watch(
|
|
|
|
|
modelValue,
|
|
|
|
|
async (value) => {
|
|
|
|
|
text.value = value
|
|
|
|
|
await nextTick()
|
|
|
|
|
renderPreview()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
watch(
|
|
|
|
|
text,
|
|
|
|
|
(newVal, oldVal) => {
|
|
|
|
|
// Only bubble the new value if it actually changed, but not if the component just got mounted and the text changed from the outside.
|
|
|
|
|
if (oldVal === '' && text.value === modelValue.value) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
bubble()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
if (modelValue.value !== '') {
|
|
|
|
|
text.value = modelValue.value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (props.previewIsDefault && props.hasPreview) {
|
|
|
|
|
nextTick(() => renderPreview())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isPreviewActive.value = false
|
|
|
|
|
isEditActive.value = true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This gets triggered when only pasting content into the editor.
|
|
|
|
|
// A change event would not get generated by that, an input event does.
|
|
|
|
|
// Therefore, we're using this handler to catch paste events.
|
|
|
|
|
// But because this also gets triggered when typing into the editor, we give
|
|
|
|
|
// it a higher timeout to make the timouts cancel each other in that case so
|
|
|
|
|
// that in the end, only one change event is triggered to the outside per change.
|
|
|
|
|
function handleInput(val: string) {
|
|
|
|
|
// Don't bubble if the text is up to date
|
|
|
|
|
if (val === text.value) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text.value = val
|
|
|
|
|
bubble(1000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function bubble(timeout = 500) {
|
|
|
|
|
if (changeTimeout.value !== null) {
|
|
|
|
|
clearTimeout(changeTimeout.value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changeTimeout.value = setTimeout(() => {
|
|
|
|
|
emit('update:modelValue', text.value)
|
|
|
|
|
}, timeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function replaceAt(str: string, index: number, replacement: string) {
|
|
|
|
|
return str.slice(0, index) + replacement + str.slice(index + replacement.length)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findNthIndex(str: string, n: number) {
|
|
|
|
|
const checkboxes = findCheckboxesInText(str)
|
|
|
|
|
return checkboxes[n]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderPreview() {
|
|
|
|
|
setupMarkdownRenderer(checkboxId.value)
|
|
|
|
|
|
|
|
|
|
preview.value = DOMPurify.sanitize(marked(text.value), {ADD_ATTR: ['target']})
|
|
|
|
|
|
|
|
|
|
// Since the render function is synchronous, we can't do async http requests in it.
|
|
|
|
|
// Therefore, we can't resolve the blob url at (markdown) compile time.
|
|
|
|
|
// To work around this, we modify the url after rendering it in the vue component.
|
|
|
|
|
// We're doing the whole thing in the next tick to ensure the image elements are available in the
|
|
|
|
|
// dom tree. If we're calling this right after setting this.preview it could be the images were
|
|
|
|
|
// not already made available.
|
|
|
|
|
// Some docs at https://stackoverflow.com/q/62865160/10924593
|
|
|
|
|
nextTick().then(async () => {
|
|
|
|
|
const attachmentImage = document.querySelectorAll<HTMLImageElement>('.attachment-image')
|
|
|
|
|
if (attachmentImage) {
|
|
|
|
|
Array.from(attachmentImage).forEach(async (img) => {
|
|
|
|
|
// The url is something like /tasks/<id>/attachments/<id>
|
|
|
|
|
const parts = img.dataset.src?.slice(window.API_URL.length + 1).split('/')
|
|
|
|
|
const taskId = Number(parts[1])
|
|
|
|
|
const attachmentId = Number(parts[3])
|
|
|
|
|
const cacheKey: CacheKey = `${taskId}-${attachmentId}`
|
|
|
|
|
|
|
|
|
|
if (typeof loadedAttachments.value[cacheKey] !== 'undefined') {
|
|
|
|
|
img.src = loadedAttachments.value[cacheKey]
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const attachment = new AttachmentModel({taskId: taskId, id: attachmentId})
|
|
|
|
|
|
|
|
|
|
const url = await attachmentService.getBlobUrl(attachment)
|
|
|
|
|
img.src = url
|
|
|
|
|
loadedAttachments.value[cacheKey] = url
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const textCheckbox = document.querySelectorAll<HTMLInputElement>(`.text-checkbox-${checkboxId.value}`)
|
|
|
|
|
if (textCheckbox) {
|
|
|
|
|
Array.from(textCheckbox).forEach(check => {
|
|
|
|
|
check.removeEventListener('change', handleCheckboxClick)
|
|
|
|
|
check.addEventListener('change', handleCheckboxClick)
|
|
|
|
|
check.parentElement?.classList.add('has-checkbox')
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleCheckboxClick(e: Event) {
|
|
|
|
|
// Find the original markdown checkbox this is targeting
|
|
|
|
|
const checked = (e.target as HTMLInputElement).checked
|
|
|
|
|
const numMarkdownCheck = Number((e.target as HTMLInputElement).dataset.checkboxNum)
|
|
|
|
|
|
|
|
|
|
const index = findNthIndex(text.value, numMarkdownCheck)
|
|
|
|
|
if (index < 0 || typeof index === 'undefined') {
|
|
|
|
|
console.debug('no index found')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
console.debug(index, text.value.slice(index, 9))
|
|
|
|
|
|
|
|
|
|
const listPrefix = text.value.slice(index, 1)
|
|
|
|
|
|
|
|
|
|
text.value = replaceAt(text.value, index, `${listPrefix} ${checked ? '[x]' : '[ ]'} `)
|
|
|
|
|
bubble()
|
|
|
|
|
renderPreview()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toggleEdit() {
|
|
|
|
|
if (isEditActive.value) {
|
|
|
|
|
isPreviewActive.value = true
|
|
|
|
|
isEditActive.value = false
|
|
|
|
|
renderPreview()
|
|
|
|
|
bubble(0) // save instantly
|
|
|
|
|
} else {
|
|
|
|
|
isPreviewActive.value = false
|
|
|
|
|
isEditActive.value = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
|