about summary refs log tree commit diff
path: root/assets/src/components/DataView.svelte
blob: d615d05f4d2dfe9721de0f83fcf6703942ed2859 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<script lang="ts">
    import data from "../state/data";
    import EyeOpenedIcon from "./icons/EyeOpenedIcon.svelte";
    import EyeClosedIcon from "./icons/EyeClosedIcon.svelte";
    import { isClientShare } from "../state/constant_state";
    import { get } from "svelte/store";
    import copy from "../utils/copy";
    import { toast, ToastType } from "../state/toast";
    import ClipboardIcon from "./icons/ClipboardIcon.svelte";

    export let showCopyButton: boolean = false;
    const iconColor = isClientShare() ? "white" : "black";

    let hidden = true;

    function toggle() {
        hidden = !hidden;
    }

    function copyText() {
        const text = get(data).data;
        copy(text).then(() => {
            toast({
                title: "Copied to clipboard!",
                message: "The shared data is now in your clipboard.",
                type: ToastType.INFO,
            });
        });
    }

    function hideText(text: string): string {
        return text
            .split("")
            .map(c => {
                if (c === "\n") {
                    return "\n";
                } else {
                    return "●";
                }
            })
            .join("");
    }
</script>

<div class="button-overlay">
    <button on:click={toggle} class="top-icon">
        {#if hidden}
            <EyeOpenedIcon color={iconColor} />
        {:else}
            <EyeClosedIcon color={iconColor} />
        {/if}
    </button>
    {#if showCopyButton}
        <button on:click={copyText} class="bottom-icon">
            <ClipboardIcon color={iconColor} />
        </button>
    {/if}
    <div class="data-view">
        <div class="textbox">
            {hidden ? hideText($data.data) : $data.data}
        </div>
    </div>
</div>

<style>
    .button-overlay {
        position: relative;
    }

    .data-view {
        font-size: 14px;
        box-sizing: border-box;
        display: flex;
        align-items: flex-start;

        resize: vertical;

        overflow-y: auto;
        /* Overloads auto on Chrome and Opera */
        overflow-y: overlay;

        min-height: 80px;
        min-width: 300px;
        max-width: 500px;
        width: 100%;
        height: 100px;

        padding: 10px 30px 0 15px;
    }

    .data-view::-webkit-scrollbar {
        width: 5px;
        padding-right: 1px;
        border-radius: 5px;
    }

    .data-view::-webkit-scrollbar-thumb {
        background: #626262;
    }

    .data-view::-webkit-scrollbar-corner {
        background: transparent;
    }

    .data-view::-webkit-scrollbar-track {
        background: transparent;
    }

    .data-view::-webkit-resizer {
        background: transparent;
    }

    button {
        border: none;
        background: none;
        padding: 0;
    }

    .top-icon {
        position: absolute;
        top: 10px;
        right: 10px;
    }

    .bottom-icon {
        position: absolute;
        bottom: 10px;
        right: 10px;
    }

    .textbox {
        font-size: 14px;
        font-family: monospace;
        flex: 1;
        outline: none;
        border: none;
        resize: none;
        overflow: hidden;

        width: 100%;

        background-color: transparent;
        color: inherit;
        white-space: pre-wrap;
        word-wrap: break-word;
    }
</style>