소스 검색

Sauvegarde des cartes

Arthur Brandao 5 년 전
부모
커밋
8effb09682
2개의 변경된 파일38개의 추가작업 그리고 2개의 파일을 삭제
  1. 34 2
      src/edit.html
  2. 4 0
      src/js/script.js

+ 34 - 2
src/edit.html

@@ -33,7 +33,7 @@
                         <a href="index.html" class="modal-close waves-effect waves-light btn-large red"><i class="large material-icons left">keyboard_backspace</i> Retour</a>
                     </div>
                     <div class="col s6 right-align">
-                        <button class="btn-floating btn-large waves-effect waves-light red tooltipped" data-position="left" data-tooltip="Sauvegarder"><i class="large material-icons">save</i></button>
+                        <button class="btn-floating btn-large waves-effect waves-light red tooltipped" data-position="left" data-tooltip="Sauvegarder" onclick="validCard(saveCard())"><i class="large material-icons">save</i></button>
                     </div>
                 </div>
                 <div class="row">
@@ -59,6 +59,7 @@
         <script type="text/javascript" src="js/binder.js"></script>
         <script type="text/javascript">
             const GET = parseURLParameter();
+            let mdEditor;
 
             $(document).ready(() => {
                 // Set up data and print
@@ -68,7 +69,7 @@
                 // Load tooltip
                 const tooltip = M.Tooltip.init(document.querySelectorAll('.tooltipped'))[0];
                 // Load markdown editor
-                const mdEditor = loadMarkdownEditor();
+                mdEditor = loadMarkdownEditor();
                 // Load data if update and markdown editor
                 if (GET.id) {
                     loadData().then(data => {
@@ -95,6 +96,37 @@
                     }
                 });
             }
+
+            function validCard(callback) {
+                if (callback) {
+                    callback();
+                }
+            }
+
+            function saveCard() {
+                let id;
+                // Update
+                if (GET.id) {
+                    id = GET.id;
+                }
+                // Create
+                else {
+                    id = uniqId();
+                    const list = JSON.parse(localStorage.list);
+                    list.push(id);
+                    localStorage.setItem('list', JSON.stringify(list));
+                }
+                // Set object
+                const data = {
+                    id: id,
+                    title: $('#title').val(),
+                    content: mdEditor.value()
+                }
+                // Save
+                localStorage.setItem(id, JSON.stringify(data));
+                // Go back to main page
+                document.location = 'index.html';
+            }
         </script>
     </body>
 </html>

+ 4 - 0
src/js/script.js

@@ -90,4 +90,8 @@ function loadData(toArray) {
             });
         }
     });
+}
+
+function uniqId() {
+    return '_' + Math.random().toString(36).substr(2, 9);
 }