|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { OutlineAction } from '../types' |
| 3 | +import { Separator } from 'reka-ui' |
| 4 | +import { useData } from 'vitepress' |
| 5 | +import { computed, ref } from 'vue' |
| 6 | +import { useSourceCode } from '../composables/useSourceCode' |
| 7 | +
|
| 8 | +const { theme } = useData() |
| 9 | +const { |
| 10 | + copyMarkdownContent, |
| 11 | + copyMarkdownLink, |
| 12 | + chatGPTUrl, |
| 13 | + claudeUrl, |
| 14 | + viewAsMarkdown, |
| 15 | + copyOptionsConfig, |
| 16 | + showCopyMarkdown, |
| 17 | +} = useSourceCode() |
| 18 | +
|
| 19 | +const isExpanded = ref(false) |
| 20 | +
|
| 21 | +const customActions = computed<OutlineAction[]>(() => { |
| 22 | + return (theme.value.outlineActions as OutlineAction[] | undefined) || [] |
| 23 | +}) |
| 24 | +
|
| 25 | +const allActions = computed(() => { |
| 26 | + const actions: OutlineAction[] = [] |
| 27 | +
|
| 28 | + if (showCopyMarkdown.value) { |
| 29 | + actions.push({ |
| 30 | + icon: 'i-tabler:copy', |
| 31 | + label: 'Copy page', |
| 32 | + onClick: copyMarkdownContent, |
| 33 | + }) |
| 34 | + } |
| 35 | +
|
| 36 | + return [...actions, ...customActions.value] |
| 37 | +}) |
| 38 | +
|
| 39 | +const nativeOptions = computed(() => { |
| 40 | + const options: OutlineAction[] = [] |
| 41 | +
|
| 42 | + if (copyOptionsConfig.value.markdownLink) { |
| 43 | + options.push({ |
| 44 | + icon: 'i-tabler:link', |
| 45 | + label: 'Copy markdown link', |
| 46 | + onClick: copyMarkdownLink, |
| 47 | + }) |
| 48 | + } |
| 49 | +
|
| 50 | + if (copyOptionsConfig.value.viewMarkdown) { |
| 51 | + options.push({ |
| 52 | + icon: 'i-tabler:eye', |
| 53 | + label: 'View as markdown', |
| 54 | + onClick: viewAsMarkdown, |
| 55 | + }) |
| 56 | + } |
| 57 | +
|
| 58 | + return options |
| 59 | +}) |
| 60 | +
|
| 61 | +const externalOptions = computed(() => { |
| 62 | + const options: OutlineAction[] = [] |
| 63 | +
|
| 64 | + if (copyOptionsConfig.value.chatgpt) { |
| 65 | + options.push({ |
| 66 | + icon: 'i-local:openai', |
| 67 | + label: 'Open in ChatGPT', |
| 68 | + onClick: () => { |
| 69 | + if (typeof window !== 'undefined') { |
| 70 | + window.open(chatGPTUrl.value, '_blank', 'noopener,noreferrer') |
| 71 | + } |
| 72 | + }, |
| 73 | + }) |
| 74 | + } |
| 75 | +
|
| 76 | + if (copyOptionsConfig.value.claude) { |
| 77 | + options.push({ |
| 78 | + icon: 'i-local:claude', |
| 79 | + label: 'Open in Claude', |
| 80 | + onClick: () => { |
| 81 | + if (typeof window !== 'undefined') { |
| 82 | + window.open(claudeUrl.value, '_blank', 'noopener,noreferrer') |
| 83 | + } |
| 84 | + }, |
| 85 | + }) |
| 86 | + } |
| 87 | +
|
| 88 | + return options |
| 89 | +}) |
| 90 | +
|
| 91 | +const hasDropdown = computed(() => nativeOptions.value.length > 0 || externalOptions.value.length > 0) |
| 92 | +
|
| 93 | +function toggleExpanded() { |
| 94 | + isExpanded.value = !isExpanded.value |
| 95 | +} |
| 96 | +</script> |
| 97 | + |
| 98 | +<template> |
| 99 | + <div v-if="allActions.length > 0" f-my-lg> |
| 100 | + <div flex="~ gap-8 items-center wrap"> |
| 101 | + <div v-for="(action, index) in allActions" :key="index" flex="~ items-center" relative> |
| 102 | + <button |
| 103 | + type="button" |
| 104 | + flex="~ items-center gap-8" |
| 105 | + p="x-12 y-8" |
| 106 | + cursor-pointer |
| 107 | + hover:bg-neutral-100 |
| 108 | + rounded-6 |
| 109 | + transition-colors |
| 110 | + f-text-xs |
| 111 | + text-neutral-800 |
| 112 | + border="1 neutral-300" |
| 113 | + @click="() => action.onClick()" |
| 114 | + > |
| 115 | + <div :class="action.icon" /> |
| 116 | + <span>{{ action.label }}</span> |
| 117 | + </button> |
| 118 | + |
| 119 | + <div v-if="index === 0 && hasDropdown" relative ml-8> |
| 120 | + <button |
| 121 | + type="button" |
| 122 | + p="x-8 y-8" |
| 123 | + hover:bg-neutral-100 |
| 124 | + rounded-6 |
| 125 | + transition-colors |
| 126 | + border="1 neutral-300" |
| 127 | + @click.stop="toggleExpanded" |
| 128 | + > |
| 129 | + <div i-tabler:dots /> |
| 130 | + </button> |
| 131 | + |
| 132 | + <Transition |
| 133 | + enter-active-class="transition-all duration-200" |
| 134 | + enter-from-class="opacity-0 scale-95" |
| 135 | + enter-to-class="opacity-100 scale-100" |
| 136 | + leave-active-class="transition-all duration-150" |
| 137 | + leave-from-class="opacity-100 scale-100" |
| 138 | + leave-to-class="opacity-0 scale-95" |
| 139 | + > |
| 140 | + <div v-if="isExpanded" absolute left-0 top="[calc(100%+4px)]" min-w-200 bg-white rounded-8 shadow-lg border="1 neutral-200" z-50 py-6> |
| 141 | + <div v-for="(option, idx) in nativeOptions" :key="`native-${idx}`" flex="~ items-center gap-8" px-10 py-6 cursor-pointer hover:bg-neutral-100 transition-colors f-text-xs text-neutral-800 @click="() => { option.onClick(); isExpanded = false }"> |
| 142 | + <div :class="option.icon" text-14 /> |
| 143 | + <span>{{ option.label }}</span> |
| 144 | + </div> |
| 145 | + |
| 146 | + <Separator v-if="nativeOptions.length > 0 && externalOptions.length > 0" my-4 h-1 bg-neutral-200 /> |
| 147 | + |
| 148 | + <div v-for="(option, idx) in externalOptions" :key="`external-${idx}`" flex="~ items-center gap-8" px-10 py-6 cursor-pointer hover:bg-neutral-100 transition-colors f-text-xs text-neutral-800 @click="() => { option.onClick(); isExpanded = false }"> |
| 149 | + <div :class="option.icon" text-14 /> |
| 150 | + <span>{{ option.label }}</span> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + </Transition> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | +</template> |
0 commit comments