|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { Editor } from '@tiptap/core' |
| 3 | +
|
| 4 | +const props = defineProps<{ |
| 5 | + editor: Editor |
| 6 | + autoOpen?: boolean |
| 7 | +}>() |
| 8 | +
|
| 9 | +const open = ref(false) |
| 10 | +const url = ref('') |
| 11 | +
|
| 12 | +const active = computed(() => props.editor.isActive('link')) |
| 13 | +const disabled = computed(() => { |
| 14 | + if (!props.editor.isEditable) return true |
| 15 | + const { selection } = props.editor.state |
| 16 | + return selection.empty && !props.editor.isActive('link') |
| 17 | +}) |
| 18 | +
|
| 19 | +function openPopover() { |
| 20 | + const { href } = props.editor.getAttributes('link') |
| 21 | + url.value = href || '' |
| 22 | + open.value = true |
| 23 | +} |
| 24 | +
|
| 25 | +watch(() => props.editor, (editor, _, onCleanup) => { |
| 26 | + if (!editor) return |
| 27 | +
|
| 28 | + const updateUrl = () => { |
| 29 | + const { href } = editor.getAttributes('link') |
| 30 | + url.value = href || '' |
| 31 | + } |
| 32 | +
|
| 33 | + updateUrl() |
| 34 | + editor.on('selectionUpdate', updateUrl) |
| 35 | +
|
| 36 | + onCleanup(() => { |
| 37 | + editor.off('selectionUpdate', updateUrl) |
| 38 | + }) |
| 39 | +}, { immediate: true }) |
| 40 | +
|
| 41 | +watch(active, (isActive) => { |
| 42 | + if (isActive && props.autoOpen) { |
| 43 | + open.value = true |
| 44 | + } |
| 45 | +}) |
| 46 | +
|
| 47 | +function setLink() { |
| 48 | + if (!url.value) return |
| 49 | +
|
| 50 | + const { selection } = props.editor.state |
| 51 | + const isEmpty = selection.empty |
| 52 | + const hasCode = props.editor.isActive('code') |
| 53 | +
|
| 54 | + let chain = props.editor.chain().focus() |
| 55 | +
|
| 56 | + // When linking code, extend the code mark range first to select the full code. |
| 57 | + if (hasCode && !isEmpty) { |
| 58 | + chain = chain.extendMarkRange('code').setLink({ href: url.value }) |
| 59 | + } else { |
| 60 | + chain = chain.extendMarkRange('link').setLink({ href: url.value }) |
| 61 | +
|
| 62 | + if (isEmpty) { |
| 63 | + chain = chain.insertContent({ type: 'text', text: url.value }) |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + chain.run() |
| 68 | + open.value = false |
| 69 | +} |
| 70 | +
|
| 71 | +function removeLink() { |
| 72 | + props.editor |
| 73 | + .chain() |
| 74 | + .focus() |
| 75 | + .extendMarkRange('link') |
| 76 | + .unsetLink() |
| 77 | + .setMeta('preventAutolink', true) |
| 78 | + .run() |
| 79 | +
|
| 80 | + url.value = '' |
| 81 | + open.value = false |
| 82 | +} |
| 83 | +
|
| 84 | +function openLink() { |
| 85 | + if (!url.value) return |
| 86 | + window.open(url.value, '_blank', 'noopener,noreferrer') |
| 87 | +} |
| 88 | +
|
| 89 | +function handleKeyDown(event: KeyboardEvent) { |
| 90 | + if (event.key === 'Enter') { |
| 91 | + event.preventDefault() |
| 92 | + setLink() |
| 93 | + } |
| 94 | +} |
| 95 | +</script> |
| 96 | + |
| 97 | +<template> |
| 98 | + <UPopover v-model:open="open" :ui="{ content: 'p-0.5' }"> |
| 99 | + <UTooltip text="Link"> |
| 100 | + <UButton |
| 101 | + icon="i-lucide-link" |
| 102 | + color="neutral" |
| 103 | + active-color="primary" |
| 104 | + variant="ghost" |
| 105 | + active-variant="soft" |
| 106 | + size="sm" |
| 107 | + :active="active" |
| 108 | + :disabled="disabled" |
| 109 | + aria-label="Link" |
| 110 | + @click="openPopover" |
| 111 | + /> |
| 112 | + </UTooltip> |
| 113 | + |
| 114 | + <template #content> |
| 115 | + <UInput |
| 116 | + v-model="url" |
| 117 | + autofocus |
| 118 | + name="url" |
| 119 | + type="url" |
| 120 | + variant="none" |
| 121 | + placeholder="Paste a link..." |
| 122 | + @keydown="handleKeyDown" |
| 123 | + > |
| 124 | + <div class="flex items-center mr-0.5"> |
| 125 | + <UButton |
| 126 | + icon="i-lucide-corner-down-left" |
| 127 | + variant="ghost" |
| 128 | + size="sm" |
| 129 | + :disabled="!url && !active" |
| 130 | + title="Apply link" |
| 131 | + @click="setLink" |
| 132 | + /> |
| 133 | + <USeparator orientation="vertical" class="h-6 mx-1" /> |
| 134 | + <UButton |
| 135 | + icon="i-lucide-external-link" |
| 136 | + color="neutral" |
| 137 | + variant="ghost" |
| 138 | + size="sm" |
| 139 | + :disabled="!url && !active" |
| 140 | + title="Open in new window" |
| 141 | + @click="openLink" |
| 142 | + /> |
| 143 | + <UButton |
| 144 | + icon="i-lucide-trash" |
| 145 | + color="neutral" |
| 146 | + variant="ghost" |
| 147 | + size="sm" |
| 148 | + :disabled="!url && !active" |
| 149 | + title="Remove link" |
| 150 | + @click="removeLink" |
| 151 | + /> |
| 152 | + </div> |
| 153 | + </UInput> |
| 154 | + </template> |
| 155 | + </UPopover> |
| 156 | +</template> |
0 commit comments