Skip to content

Commit

Permalink
fix: TipTap reactive prop destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
dpschen authored and kolaente committed Jan 21, 2025
1 parent 3d33b7c commit 30daf08
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions frontend/src/components/input/editor/TipTap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,7 @@ import {isEditorContentEmpty} from '@/helpers/editorContentEmpty'
import inputPrompt from '@/helpers/inputPrompt'
import {setLinkInEditor} from '@/components/input/editor/setLinkInEditor'
const {
modelValue,
uploadCallback,
isEditEnabled = true,
bottomActions = [],
showSave = false,
placeholder = '',
editShortcut = '',
enableDiscardShortcut = false,
} = defineProps<{
const props = withDefaults(defineProps<{
modelValue: string,
uploadCallback?: UploadCallback,
isEditEnabled?: boolean,
Expand All @@ -199,7 +190,15 @@ const {
placeholder?: string,
editShortcut?: string,
enableDiscardShortcut?: boolean,
}>()
}>(), {
uploadCallback: undefined,
isEditEnabled: true,
bottomActions: () => [],
showSave: false,
placeholder: '',
editShortcut: '',
enableDiscardShortcut: false,
})
const emit = defineEmits(['update:modelValue', 'save'])
Expand Down Expand Up @@ -297,13 +296,13 @@ const CustomImage = Image.extend({
type Mode = 'edit' | 'preview'
const internalMode = ref<Mode>('preview')
const isEditing = computed(() => internalMode.value === 'edit' && isEditEnabled)
const isEditing = computed(() => internalMode.value === 'edit' && props.isEditEnabled)
const contentHasChanged = ref<boolean>(false)
let lastSavedState = ''
watch(
() => modelValue,
() => props.modelValue,
(newValue) => {
if (!contentHasChanged.value) {
lastSavedState = newValue
Expand Down Expand Up @@ -362,8 +361,8 @@ const extensions : Extensions = [
return ''
}
return placeholder !== ''
? placeholder
return props.placeholder !== ''
? props.placeholder
: t('input.editor.placeholder')
},
}),
Expand Down Expand Up @@ -391,7 +390,7 @@ const extensions : Extensions = [
TaskItem.configure({
nested: true,
onReadOnlyChecked: (node: Node, checked: boolean): boolean => {
if (!isEditEnabled) {
if (!props.isEditEnabled) {
return false
}
Expand Down Expand Up @@ -422,7 +421,7 @@ const extensions : Extensions = [
]
// Add a custom extension for the Escape key
if (enableDiscardShortcut) {
if (props.enableDiscardShortcut) {
extensions.push(Extension.create({
name: 'escapeKey',
Expand Down Expand Up @@ -455,7 +454,7 @@ watch(
)
watch(
() => modelValue,
() => props.modelValue,
value => {
if (!editor?.value) return
Expand All @@ -469,8 +468,8 @@ watch(
)
function bubbleNow() {
if (editor.value?.getHTML() === modelValue ||
(editor.value?.getHTML() === '<p></p>') && modelValue === '') {
if (editor.value?.getHTML() === props.modelValue ||
(editor.value?.getHTML() === '<p></p>') && props.modelValue === '') {
return
}
Expand All @@ -495,7 +494,7 @@ function exitEditMode() {
}
function setEditIfApplicable() {
if (!isEditEnabled) return
if (!props.isEditEnabled) return
if (isEditing.value) return
setEdit()
Expand All @@ -513,7 +512,11 @@ onBeforeUnmount(() => editor.value?.destroy())
const uploadInputRef = ref<HTMLInputElement | null>(null)
function uploadAndInsertFiles(files: File[] | FileList) {
uploadCallback(files).then(urls => {
if (typeof props.uploadCallback !== 'undefined') {
throw new Error('Can\'t add files here')
}
props.uploadCallback(files).then(urls => {
urls?.forEach(url => {
editor.value
?.chain()
Expand All @@ -526,7 +529,7 @@ function uploadAndInsertFiles(files: File[] | FileList) {
}
function triggerImageInput(event) {
if (typeof uploadCallback !== 'undefined') {
if (typeof props.uploadCallback !== 'undefined') {
uploadInputRef.value?.click()
return
}
Expand All @@ -536,7 +539,7 @@ function triggerImageInput(event) {
async function addImage(event) {
if (typeof uploadCallback !== 'undefined') {
if (typeof props.uploadCallback !== 'undefined') {
const files = uploadInputRef.value?.files
if (!files || files.length === 0) {
Expand All @@ -561,28 +564,28 @@ function setLink(event) {
}
onMounted(async () => {
if (editShortcut !== '') {
if (props.editShortcut !== '') {
document.addEventListener('keydown', setFocusToEditor)
}
await nextTick()
if (typeof uploadCallback !== 'undefined') {
if (typeof props.uploadCallback !== 'undefined') {
const input = tiptapInstanceRef.value?.querySelectorAll('.tiptap__editor')[0]?.children[0]
input?.addEventListener('paste', handleImagePaste)
}
setModeAndValue(modelValue)
setModeAndValue(props.modelValue)
})
onBeforeUnmount(() => {
nextTick(() => {
if (typeof uploadCallback !== 'undefined') {
if (typeof props.uploadCallback !== 'undefined') {
const input = tiptapInstanceRef.value?.querySelectorAll('.tiptap__editor')[0]?.children[0]
input?.removeEventListener('paste', handleImagePaste)
}
})
if (editShortcut !== '') {
if (props.editShortcut !== '') {
document.removeEventListener('keydown', setFocusToEditor)
}
})
Expand All @@ -601,6 +604,10 @@ function handleImagePaste(event) {
const image = event.clipboardData.items[0]
if (image.kind === 'file' && image.type.startsWith('image/')) {
if (typeof props.uploadCallback !== 'undefined') {
return
}
uploadAndInsertFiles([image.getAsFile()])
}
}
Expand All @@ -613,7 +620,7 @@ function setFocusToEditor(event) {
const hotkeyString = eventToHotkeyString(event)
if (!hotkeyString) return
if (hotkeyString !== editShortcut ||
if (hotkeyString !== props.editShortcut ||
event.target.tagName.toLowerCase() === 'input' ||
event.target.tagName.toLowerCase() === 'textarea' ||
event.target.contentEditable === 'true') {
Expand All @@ -622,7 +629,7 @@ function setFocusToEditor(event) {
event.preventDefault()
if (!isEditing.value && isEditEnabled) {
if (!isEditing.value && props.isEditEnabled) {
internalMode.value = 'edit'
}
Expand Down

0 comments on commit 30daf08

Please sign in to comment.