11<script setup lang="ts">
22import type { NodePath , QueryStats } from ' @devframes/plugin-data-inspector/engine'
3+ import type { QueryObservation } from ' ../composables/query-observation'
34import ActionIconButton from ' @antfu/design/components/Action/ActionIconButton.vue'
45import DisplayBadge from ' @antfu/design/components/Display/DisplayBadge.vue'
56import DisplayBytes from ' @antfu/design/components/Display/DisplayBytes.vue'
67import DisplayDate from ' @antfu/design/components/Display/DisplayDate.vue'
78import DisplayDuration from ' @antfu/design/components/Display/DisplayDuration.vue'
8- import { computed , shallowRef , watch } from ' vue'
9+ import FormTextarea from ' @antfu/design/components/Form/FormTextarea.vue'
10+ import OverlayModal from ' @antfu/design/components/Overlay/OverlayModal.vue'
11+ import { computed , onUnmounted , shallowRef , watch } from ' vue'
912import { useDiscoveryViewer } from ' ../composables/discovery'
1013import { prepareForDisplay } from ' ../composables/display-transform'
14+ import { observationMarkdown } from ' ../composables/query-observation'
1115import { colorScheme } from ' ../composables/scheme'
1216import EditPanel from ' ./EditPanel.vue'
1317
1418const props = defineProps <{
19+ observation: QueryObservation | null
20+ observationNotices: string []
1521 result: unknown
1622 hasResult: boolean
1723 stats: (QueryStats & { rpcMs: number }) | null
@@ -72,16 +78,38 @@ const lockTooltip = computed(() => {
7278 return ' '
7379})
7480
75- const copied = shallowRef (false )
81+ const copied = shallowRef <' json' | ' markdown' | null >(null )
82+ const copying = shallowRef (false )
83+ const manualCopyOpen = shallowRef (false )
84+ const manualCopyText = shallowRef (' ' )
85+ const copyError = shallowRef (' ' )
7686let copiedTimer: ReturnType <typeof setTimeout > | undefined
77- function copyResult(): void {
87+ onUnmounted (() => clearTimeout (copiedTimer ))
88+
89+ async function copyResult(format : ' json' | ' markdown' ): Promise <void > {
90+ if (! props .hasResult || ! props .observation || copying .value )
91+ return
92+ // Serialize at the click, before a refresh can replace the observation.
93+ const text = format === ' markdown'
94+ ? observationMarkdown (props .observation , props .observationNotices )
95+ : JSON .stringify (props .observation .result , null , 2 ) ?? ' null'
96+ copying .value = true
97+ copied .value = null
98+ copyError .value = ' '
99+ clearTimeout (copiedTimer )
78100 try {
79- void navigator .clipboard .writeText (JSON .stringify (props .result , null , 2 ))
80- copied .value = true
81- clearTimeout (copiedTimer )
82- copiedTimer = setTimeout (() => (copied .value = false ), 1200 )
101+ await navigator .clipboard .writeText (text )
102+ copied .value = format
103+ copiedTimer = setTimeout (() => (copied .value = null ), 1200 )
104+ }
105+ catch {
106+ copyError .value = ' Clipboard write failed. Select and copy the complete text below.'
107+ manualCopyText .value = text
108+ manualCopyOpen .value = true
109+ }
110+ finally {
111+ copying .value = false
83112 }
84- catch {}
85113}
86114 </script >
87115
@@ -138,13 +166,22 @@ function copyResult(): void {
138166 @click =" viewer .setExpanded (1 )"
139167 />
140168 <ActionIconButton
141- v-if =" hasResult "
142169 class="text-sm"
143- :icon =" copied ? ' i-ph:check' : ' i-ph:copy-duotone' "
170+ :disabled =" ! hasResult || copying "
171+ :icon =" copied === ' json' ? ' i-ph:check' : ' i-ph:copy-duotone' "
144172 label="Copy result as JSON"
145173 tooltip="Copy the result as JSON"
146- @click =" copyResult "
174+ @click =" copyResult ( ' json ' ) "
147175 />
176+ <ActionIconButton
177+ class="text-sm"
178+ :disabled =" ! hasResult || copying "
179+ :icon =" copied === ' markdown' ? ' i-ph:check' : ' i-ph:file-text-duotone' "
180+ label="Copy result with context"
181+ tooltip="Copy result with context"
182+ @click =" copyResult (' markdown' )"
183+ />
184+ <span class =" sr-only" role =" status" >{{ copied ? 'Copied to clipboard' : '' }}</span >
148185 <ActionIconButton
149186 class="text-sm"
150187 :icon =" running ? ' i-ph:arrows-clockwise animate-spin' : ' i-ph:arrows-clockwise' "
@@ -155,6 +192,14 @@ function copyResult(): void {
155192 />
156193 </div >
157194
195+ <div v-if =" hasResult && observationNotices.length" class =" px-3 py-2 text-xs color-muted border-b border-base" role =" status" >
196+ <p v-for =" notice in observationNotices" :key =" notice" >
197+ {{ notice }}
198+ </p >
199+ </div >
200+ <OverlayModal v-model :open =" manualCopyOpen " title="Copy manually" :description =" copyError " >
201+ <FormTextarea v-model =" manualCopyText " readonly :rows =" 14 " class="font-mono text-xs" aria-label="Complete copy text" @focus =" ($event .target as HTMLTextAreaElement ).select () " />
202+ </OverlayModal >
158203 <div
159204 v-if =" error"
160205 class =" mx-3 mt-2 px-3 py-2 font-mono text-xs whitespace-pre-wrap rounded-lg border border-red-600/40 bg-red-500:8 color-red-700 dark:(border-red-400/40 color-red-300)"
@@ -173,7 +218,7 @@ function copyResult(): void {
173218 <div v-if =" !hasResult" class =" flex-1 grid place-items-center select-none" >
174219 <div class =" flex flex-col items-center gap-2 color-faint" >
175220 <span class =" i-ph:tree-structure-duotone text-3xl" />
176- <span class =" text-sm" >Results render here, start typing a query </span >
221+ <span class =" text-sm" >Run a successful query to view and copy its result </span >
177222 </div >
178223 </div >
179224 </div >
0 commit comments