aboutsummaryrefslogtreecommitdiff
path: root/front-end/src/views/Blog/components/ContentSearch.vue
blob: 78711964175e3cc58503ec8607cb3c840776ef05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<template>
    <div id="content-search" class="my-4">
        <div class="">
            <div class="">
                <input class="w-full input primary" placeholder="Search..." v-model="search" />
            </div>
        </div>
        <div class="search-results">
            <div v-if="searchResults.length == 0" class="result">
                No results found.
            </div>
            <div v-else v-for="result in searchResults" :key="result.id" @click.prevent="onSelected(result)" class="result">
                <div class="flex-auto result name">
                    {{ result.shortName }}
                </div>
                <div class="result id">
                    {{ result.shortId }}
                </div>
                <div class="rseult controls">
                    <div v-if="waiting">
                        <fa-icon icon="spinner" spin />
                    </div>
                    <div v-else-if="result.copied.value" class="text-sm text-amber-500">
                        copied
                    </div>
                    <div v-else class="">
                         <button class="btn secondary sm borderless" @click="result.copyLink()">
                            <fa-icon icon="link" />
                         </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useClipboard } from '@vueuse/core';
import { apiCall, useWait } from '@vnuge/vnlib.browser';
import { computed, Ref, ref } from 'vue';
import { map, slice, truncate } from 'lodash-es';
import { ContentMeta } from '@vnuge/cmnext-admin';
import { useStore } from '../../../store';

const emit = defineEmits(['selected'])
const store = useStore()

const { waiting } = useWait()

const search = ref('')
const searcher = store.content.createReactiveSearch(search);

interface ContentResult extends ContentMeta {
    readonly shortId: string,
    readonly shortName: string,
    readonly copied: Ref<boolean>,
    copyLink(): void
}

const searchResults = computed<ContentResult[]>(() => {
    const current = slice(searcher.value, 0, 5);

    //Copies the link to the clipboard from the server to insert into the editor
    const copyLink = (result : ContentMeta, copy : (text: string) => Promise<void> ) => {
        apiCall(async () =>{
            const link = await store.content.getPublicUrl(result);
            await copy(link);
        })
    }

    //Formats the result for display
    return map(current, content => {
        //scoped clipboard for copy link
        const { copied, copy } = useClipboard();
        return {
            ...content,
            //truncate the id and name for display
            shortId: truncate(content.id, { length: 18 }),
            shortName: truncate(content.name, { length: 36 }),
            copyLink: () => copyLink(content, copy),
            copied
        }
    })
})

const onSelected = (result: ContentResult) => {
    emit('selected', result)
}

</script>

<style lang="scss">

    .search-results{
        @apply mt-3;
    }
    
    .result{
        @apply flex flex-row items-center justify-between;
        @apply p-1 cursor-pointer hover:bg-gray-100 dark:hover:bg-dark-600;

        .id{
            @apply text-sm;
        }

        .controls{
            @apply min-w-[4rem] text-center;
        }

        &.name{
            @apply text-sm;
        }
    }

</style>