File size: 10,709 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*

   ELYSIA MARKDOWN STUDIO v1.0 - Editor Module

   Markdown editor with toolbar actions

*/

import Utils from "./utils.js";

const Editor = {
    textarea: null,
    currentDoc: null,
    autoSaveInterval: null,
    autoSaveInProgress: false, // Prevent race conditions

    init() {
        this.textarea = document.getElementById("markdown-editor");
        this.setupEventListeners();
        this.setupToolbar();
        // Don't start auto-save yet - wait for app to be fully initialized
    },

    setupEventListeners() {
        // Input event for stats update
        this.textarea.addEventListener(
            "input",
            Utils.debounce(() => {
                this.updateStats();

                // Check if live preview is enabled
                const livePreview = Utils.storage.get("livePreview", true);
                if (livePreview && window.app?.preview) {
                    window.app.preview.update();
                }

                // Mark unsaved changes
                if (window.app) {
                    window.app.unsavedChanges = true;
                }
            }, 300)
        );

        // Drag & drop for images
        this.textarea.addEventListener("dragover", e => {
            e.preventDefault();
            this.textarea.classList.add("drag-over");
        });

        this.textarea.addEventListener("dragleave", e => {
            e.preventDefault();
            this.textarea.classList.remove("drag-over");
        });

        this.textarea.addEventListener("drop", e => {
            e.preventDefault();
            this.textarea.classList.remove("drag-over");
            this.handleImageDrop(e);
        });

        // Paste images from clipboard
        this.textarea.addEventListener("paste", e => {
            const items = e.clipboardData?.items;
            if (!items) return;

            for (const item of items) {
                if (item.type.startsWith("image/")) {
                    e.preventDefault();
                    this.handleImagePaste(item);
                    break;
                }
            }
        });

        // Keyboard shortcuts
        this.textarea.addEventListener("keydown", e => {
            if (e.ctrlKey || e.metaKey) {
                switch (e.key.toLowerCase()) {
                    case "s":
                        e.preventDefault();
                        window.app?.saveDocument();
                        break;
                    case "b":
                        e.preventDefault();
                        this.wrapSelection("**", "**");
                        break;
                    case "i":
                        e.preventDefault();
                        this.wrapSelection("*", "*");
                        break;
                }
            }
        });
    },

    setupToolbar() {
        document.querySelectorAll(".toolbar-btn").forEach(btn => {
            btn.addEventListener("click", () => {
                const action = btn.getAttribute("data-action");
                this.handleToolbarAction(action);
            });
        });
    },

    handleToolbarAction(action) {
        switch (action) {
            case "bold":
                this.wrapSelection("**", "**");
                break;
            case "italic":
                this.wrapSelection("*", "*");
                break;
            case "strikethrough":
                this.wrapSelection("~~", "~~");
                break;
            case "heading1":
                this.insertAtLineStart("# ");
                break;
            case "heading2":
                this.insertAtLineStart("## ");
                break;
            case "heading3":
                this.insertAtLineStart("### ");
                break;
            case "link":
                this.insertLink();
                break;
            case "image":
                this.insertImage();
                break;
            case "code":
                this.wrapSelection("`", "`");
                break;
            case "quote":
                this.insertAtLineStart("> ");
                break;
            case "ul":
                this.insertAtLineStart("- ");
                break;
            case "ol":
                this.insertAtLineStart("1. ");
                break;
            case "task":
                this.insertAtLineStart("- [ ] ");
                break;
            case "table":
                this.insertTable();
                break;
            case "hr":
                this.insertLine("\n---\n");
                break;
        }

        this.textarea.focus();
    },

    wrapSelection(before, after) {
        const start = this.textarea.selectionStart;
        const end = this.textarea.selectionEnd;
        const text = this.textarea.value;
        const selected = text.substring(start, end);

        const wrapped = before + (selected || "text") + after;
        this.textarea.setRangeText(wrapped, start, end, "select");

        this.textarea.dispatchEvent(new Event("input"));
    },

    insertAtLineStart(prefix) {
        const start = this.textarea.selectionStart;
        const text = this.textarea.value;

        // Find line start
        let lineStart = start;
        while (lineStart > 0 && text[lineStart - 1] !== "\n") {
            lineStart--;
        }

        this.textarea.setRangeText(prefix, lineStart, lineStart, "end");
        this.textarea.dispatchEvent(new Event("input"));
    },

    insertLine(text) {
        const start = this.textarea.selectionStart;
        this.textarea.setRangeText(text, start, start, "end");
        this.textarea.dispatchEvent(new Event("input"));
    },

    insertLink() {
        const url = prompt("Enter URL:");
        if (!url) return;

        const text = prompt("Link text (optional):") || url;
        this.wrapSelection(`[${text}](`, `)`);
    },

    insertImage() {
        const url = prompt("Enter image URL:");
        if (!url) return;

        const alt = prompt("Alt text (optional):") || "image";
        const markdown = `![${alt}](${url})`;

        const start = this.textarea.selectionStart;
        this.textarea.setRangeText(markdown, start, start, "end");
        this.textarea.dispatchEvent(new Event("input"));
    },

    insertTable() {
        const table = `\n| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| Cell 1   | Cell 2   | Cell 3   |\n| Cell 4   | Cell 5   | Cell 6   |\n`;
        this.insertLine(table);
    },

    updateStats() {
        const content = this.textarea.value;

        const wordCount = Utils.countWords(content);
        const charCount = Utils.countChars(content);
        const lineCount = Utils.countLines(content);
        const readingTime = Utils.readingTime(wordCount);

        document.getElementById("word-count").textContent = `${wordCount} words`;
        document.getElementById("char-count").textContent = `${charCount} chars`;
        document.getElementById("line-count").textContent = `${lineCount} lines`;

        // Add reading time if element exists
        const readingTimeEl = document.getElementById("reading-time");
        if (readingTimeEl) {
            readingTimeEl.textContent = readingTime;
        }

        // Update current doc stats if exists
        if (this.currentDoc) {
            this.currentDoc.wordCount = wordCount;
            this.currentDoc.charCount = charCount;
        }
    },

    getContent() {
        return this.textarea.value;
    },

    setContent(content) {
        this.textarea.value = content || "";
        this.updateStats();
        window.app?.preview.update();
    },

    clear() {
        this.setContent("");
    },

    startAutoSave() {
        // Stop any existing interval
        this.stopAutoSave();

        const autoSaveEnabled = Utils.storage.get("autoSave", true);
        if (!autoSaveEnabled) return;

        // Only start if app is fully initialized
        if (!window.app) {
            console.warn("Auto-save deferred - app not initialized yet");
            return;
        }

        this.autoSaveInterval = setInterval(async () => {
            // Prevent concurrent auto-saves
            if (this.autoSaveInProgress) {
                console.log("⏭️ Skipping auto-save - already in progress");
                return;
            }

            if (window.app?.unsavedChanges && this.textarea.value) {
                try {
                    this.autoSaveInProgress = true;
                    await window.app.saveDocument(true); // Silent save
                    console.log("πŸ’Ύ Auto-saved");
                } catch (err) {
                    console.error("Auto-save failed:", err);
                } finally {
                    this.autoSaveInProgress = false;
                }
            }
        }, 30000); // 30 seconds

        console.log("βœ… Auto-save enabled (every 30s)");
    },

    stopAutoSave() {
        if (this.autoSaveInterval) {
            clearInterval(this.autoSaveInterval);
            this.autoSaveInterval = null;
        }
    },

    // Handle image drop
    handleImageDrop(e) {
        const files = e.dataTransfer?.files;
        if (!files || files.length === 0) return;

        for (const file of files) {
            if (file.type.startsWith("image/")) {
                this.insertImageFromFile(file);
            }
        }
    },

    // Handle image paste
    handleImagePaste(item) {
        const file = item.getAsFile();
        if (file) {
            this.insertImageFromFile(file);
        }
    },

    // Insert image from file (convert to base64 data URL)
    insertImageFromFile(file) {
        const reader = new FileReader();

        reader.onload = e => {
            const dataUrl = e.target.result;
            const altText = file.name.replace(/\.[^/.]+$/, ""); // Remove extension
            const markdown = `\n![${altText}](${dataUrl})\n`;

            const start = this.textarea.selectionStart;
            this.textarea.setRangeText(markdown, start, start, "end");
            this.textarea.dispatchEvent(new Event("input"));

            Utils.toast.success(`Image "${file.name}" inserted!`);
        };

        reader.onerror = () => {
            Utils.toast.error("Failed to read image file");
        };

        reader.readAsDataURL(file);
    }
};

export default Editor;