From 86b9e2de76283ac6ee1e7761dcf6ce0730f26e7a Mon Sep 17 00:00:00 2001
From: Mistivia <i@mistivia.com>
Date: Wed, 5 Feb 2025 19:22:13 +0800
Subject: impl save deck

---
 src/components/MainPanel.svelte | 17 +++++++++++------
 src/utils.js                    | 16 ++++++++++++++++
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/components/MainPanel.svelte b/src/components/MainPanel.svelte
index f5dcaf5..b5f5f2e 100644
--- a/src/components/MainPanel.svelte
+++ b/src/components/MainPanel.svelte
@@ -1,15 +1,15 @@
 <script lang="js">
     import CardThumb from './CardThumb.svelte';
     import { deck, setDeck } from '../control/deck';
-    import { parseYdk } from '../utils'
+    import { parseYdk, genYdk, downloadStringAsFile} from '../utils'
 
     let fileInput;
     
-    let openDeck = () => {
+    function openDeck() {
         fileInput.click();
-    };
+    }
 
-    let loadDeck = (event) => {
+    function loadDeck(event) {
         const file = event.target.files[0];
         if (file) {
             const reader = new FileReader();
@@ -19,7 +19,12 @@
             };
             reader.readAsText(file); 
         }
-    };
+    }
+
+    function saveDeck() {
+        let deckString = genYdk($deck);
+        downloadStringAsFile('mydeck.ydk', deckString)
+    }
 
 </script>
 
@@ -28,7 +33,7 @@
 <div class="middle-panel">
     <div class="control-bar">
         <button class="btn" onclick={openDeck}>打开</button>
-        <button class="btn">保存</button>
+        <button class="btn" onclick={saveDeck}>保存</button>
     </div>
     <div class="deck-section">
         <div class="deck-group">
diff --git a/src/utils.js b/src/utils.js
index 05e519d..3176400 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -42,7 +42,23 @@ function genYdk(deck) {
     return ydkContent;
 }
 
+function downloadStringAsFile(filename, text) {
+    const blob = new Blob([text], { type: 'text/plain' });
+    const link = document.createElement('a');
+    link.href = URL.createObjectURL(blob);
+    link.download = filename;
+    document.body.appendChild(link);
+
+    link.click();
+
+    document.body.removeChild(link);
+    URL.revokeObjectURL(link.href);
+}
+
+
 export {
     parseYdk,
     genYdk,
+    downloadStringAsFile,
 };
+
-- 
cgit v1.0

