File size: 8,020 Bytes
0b194e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*

   ELYSIA MARKDOWN STUDIO v1.0 - Utility Functions

   Toast, modals, storage, helpers

*/

const Utils = {
    // Toast Notifications
    toast: {
        show(message, type = "info", duration = 3000) {
            const container = document.getElementById("toast-container");
            const toast = document.createElement("div");
            toast.className = `toast ${type}`;

            const icons = {
                success: "✅",
                error: "❌",
                warning: "⚠️",
                info: "ℹ️",
                loading: "⏳"
            };

            toast.innerHTML = `

                <span class="toast-icon">${icons[type]}</span>

                <span class="toast-message">${message}</span>

                <button class="toast-close">×</button>

            `;

            container.appendChild(toast);

            const close = () => {
                toast.style.animation = "slideOut 0.3s ease";
                setTimeout(() => toast.remove(), 300);
            };

            toast.querySelector(".toast-close").onclick = close;
            if (duration > 0) setTimeout(close, duration);

            return toast;
        },

        success: (msg, duration) => Utils.toast.show(msg, "success", duration),
        error: (msg, duration) => Utils.toast.show(msg, "error", duration),
        warning: (msg, duration) => Utils.toast.show(msg, "warning", duration),
        info: (msg, duration) => Utils.toast.show(msg, "info", duration)
    },

    // Modal Management
    modal: {
        open(modalId) {
            const modal = document.getElementById(modalId);
            if (modal) {
                modal.classList.add("active");
                document.body.style.overflow = "hidden";
            }
        },

        close(modalId) {
            const modal = document.getElementById(modalId);
            if (modal) {
                modal.classList.remove("active");
                document.body.style.overflow = "";
            }
        },

        init() {
            document.querySelectorAll(".modal").forEach(modal => {
                modal.addEventListener("click", e => {
                    if (e.target === modal) {
                        Utils.modal.close(modal.id);
                    }
                });
            });

            document.querySelectorAll(".modal-close, [data-modal]").forEach(btn => {
                btn.addEventListener("click", () => {
                    const modalId = btn.getAttribute("data-modal");
                    if (modalId) Utils.modal.close(modalId);
                });
            });
        }
    },

    // Local Storage Wrapper (with encryption for sensitive data)
    storage: {
        // Simple XOR encryption (basic obfuscation - better than plaintext)
        _encrypt(text) {
            const key = "ElysiaStudio2025"; // Simple key for obfuscation
            let encrypted = "";
            for (let i = 0; i < text.length; i++) {
                encrypted += String.fromCharCode(text.charCodeAt(i) ^ key.charCodeAt(i % key.length));
            }
            return btoa(encrypted); // Base64 encode
        },

        _decrypt(encrypted) {
            try {
                const decoded = atob(encrypted);
                const key = "ElysiaStudio2025";
                let decrypted = "";
                for (let i = 0; i < decoded.length; i++) {
                    decrypted += String.fromCharCode(decoded.charCodeAt(i) ^ key.charCodeAt(i % key.length));
                }
                return decrypted;
            } catch {
                return null;
            }
        },

        get(key, defaultValue = null) {
            try {
                const value = localStorage.getItem(key);
                if (!value) return defaultValue;

                // Decrypt API key if it's stored
                if (key === "apiKey") {
                    const decrypted = this._decrypt(value);
                    return decrypted || defaultValue;
                }

                return JSON.parse(value);
            } catch {
                return defaultValue;
            }
        },

        set(key, value) {
            try {
                // Encrypt API key before storing
                if (key === "apiKey" && value) {
                    localStorage.setItem(key, this._encrypt(value));
                    return true;
                }

                localStorage.setItem(key, JSON.stringify(value));
                return true;
            } catch {
                return false;
            }
        },

        remove(key) {
            localStorage.removeItem(key);
        },

        clear() {
            localStorage.clear();
        }
    },

    // Format Date/Time
    formatDateTime(date) {
        const d = new Date(date);
        return d.toLocaleString();
    },

    // Format Date (short)
    formatDate(date) {
        const d = new Date(date);
        const now = new Date();
        const diff = now - d;

        if (diff < 60000) return "Just now";
        if (diff < 3600000) return `${Math.floor(diff / 60000)}m ago`;
        if (diff < 86400000) return `${Math.floor(diff / 3600000)}h ago`;
        if (diff < 604800000) return `${Math.floor(diff / 86400000)}d ago`;

        return d.toLocaleDateString();
    },

    // Count Words
    countWords(text) {
        return text.trim() ? text.trim().split(/\s+/).length : 0;
    },

    // Count Characters
    countChars(text) {
        return text.length;
    },

    // Count Lines
    countLines(text) {
        return text.split("\n").length;
    },

    // Calculate reading time (average 200 words per minute)
    readingTime(wordCount) {
        const minutes = Math.ceil(wordCount / 200);
        if (minutes < 1) return "< 1 min read";
        if (minutes === 1) return "1 min read";
        return `${minutes} min read`;
    },

    // Download File
    downloadFile(content, filename, mimeType = "text/plain") {
        const blob = new Blob([content], { type: mimeType });
        const url = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = url;
        a.download = filename;
        a.click();
        URL.revokeObjectURL(url);
    },

    // Copy to Clipboard
    async copyToClipboard(text) {
        try {
            await navigator.clipboard.writeText(text);
            Utils.toast.success("Copied to clipboard!");
            return true;
        } catch (err) {
            Utils.toast.error("Failed to copy");
            return false;
        }
    },

    // Debounce Function
    debounce(func, wait) {
        let timeout;
        return function executedFunction(...args) {
            const later = () => {
                clearTimeout(timeout);
                func(...args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    },

    // Generate UUID
    uuid() {
        return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => {
            const r = (Math.random() * 16) | 0;
            const v = c === "x" ? r : (r & 0x3) | 0x8;
            return v.toString(16);
        });
    },

    // Sanitize Filename
    sanitizeFilename(name) {
        return name.replace(/[^a-z0-9_\-\.]/gi, "_");
    },

    // Truncate Text
    truncate(text, maxLength) {
        return text.length > maxLength ? text.substring(0, maxLength) + "..." : text;
    },

    // Escape HTML
    escapeHtml(text) {
        const div = document.createElement("div");
        div.textContent = text;
        return div.innerHTML;
    }
};

// Initialize modals on load
document.addEventListener("DOMContentLoaded", () => {
    Utils.modal.init();
});

export default Utils;