Browse Source

便签笔记支持创建文件夹分类

zxlie 7 years ago
parent
commit
98bab204eb

+ 1 - 1
apps/manifest.json

@@ -1,6 +1,6 @@
 {
   "name": "WEB前端助手(FeHelper)",
-  "version": "2018.11.1921",
+  "version": "2018.11.2222",
   "manifest_version": 2,
   "default_locale": "zh_CN",
   "description": "FE助手:包括JSON格式化、二维码生成与解码、信息编解码、代码压缩、美化、页面取色、Markdown与HTML互转、网页滚动截屏、正则表达式、时间转换工具、编码规范检测、页面性能检测、Ajax接口调试、密码生成器、JSON比对工具、网页编码设置、便签笔记",

+ 1 - 1
apps/qr-code/decode.js

@@ -13,7 +13,7 @@ module.exports = (() => {
                 '<div style="position: relative;top: 100px;left: ' + ($('body').width() / 2 - 200) + 'px;border:1px solid #000;background:#fff;width:420px;padding:15px;border-radius:5px 5px;box-shadow:2px 2px 5px #000;">' +
                 '<div style="margin: 0 0 10px 0;font-size: 14px;font-weight: bold;">二维码解码结果:</div>' +
                 '<textarea style="display:block;border-radius:5px 5px;width:398px;border:1px solid #aaa;min-height:80px;resize:none;box-shadow:2px 2px 5px #aaa;padding:10px;font-size:14px;color:#888;"></textarea>' +
-                '<div style="margin-top:10px;">' +
+                '<div style="margin-top:10px;font-size:0">' +
                 '<span id="__fehelper_qr_msg_" style="float: right;color:#f00;display:none;">复制成功!</span>' +
                 '<a id="__fehelper_qr_open_" style="margin-right:20px;color: #00f;text-decoration: underline;display: inline;font-size:12px" href="#">打开</a>' +
                 '<a id="__fehelper_qr_copy_" style="margin-right:20px;color: #00f;text-decoration: underline;display: inline;font-size:12px" href="#">复制</a>' +

+ 87 - 28
apps/sticky-notes/html5sticky.js

@@ -17,6 +17,8 @@ let allowed_tags = '<br /><br><ol></ol><ul></ul><li></li><strong></strong><i></i
 
 let html5sticky = {};
 const STICKYNOTES_ALLKEYS = 'stickynotes|allkeys';
+const STICKYNOTES_FOLDERS = 'stickynotes|folders';
+const STICKYNOTES_SELECTED_FOLDER = 'stickynotes|selected|folder';
 
 // add a note
 html5sticky.addNote = function () {
@@ -66,6 +68,7 @@ html5sticky.addNote = function () {
     allKeys.push(nindex + '|text');
     allKeys.push(nindex + '|bgcolor');
     allKeys.push(nindex + '|dated');
+    allKeys.push(nindex + '|folderid');
     localStorage.setItem(STICKYNOTES_ALLKEYS, allKeys.join(','));
 
     // 存数据
@@ -73,6 +76,8 @@ html5sticky.addNote = function () {
     localStorage.setItem(nindex + '|text', $(stickynote).find('h2').text() + '|' + $(stickynote).find('p').text());
     localStorage.setItem(nindex + '|bgcolor', bgcolor);
     localStorage.setItem(nindex + '|dated', dated + '|' + getISODateTime(dateStr));
+    let folder = html5sticky.getCurrentFolder();
+    localStorage.setItem(nindex + '|folderid', folder[1]);
 
     html5sticky.enlargeNote(stickynote);
 };
@@ -122,9 +127,10 @@ html5sticky.deleteNote = function (el) {
         localStorage.removeItem(identifier + '|text');
         localStorage.removeItem(identifier + '|bgcolor');
         localStorage.removeItem(identifier + '|dated');
+        localStorage.removeItem(identifier + '|folderid');
 
         let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
-        ['text', 'bgcolor', 'dated'].forEach(function (item) {
+        ['text', 'bgcolor', 'dated', 'folderid'].forEach(function (item) {
             let id = identifier + '|' + item;
             allKeys.indexOf(id) > -1 && allKeys.splice(allKeys.indexOf(id), 1);
         });
@@ -207,8 +213,8 @@ html5sticky.editNote = function ($clone, el) {
 };
 
 // get all notes
-html5sticky.getNotes = function () {
-
+html5sticky.getNotes = function (folderId) {
+    let mainEl = $('#main').html('');
     let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
     allKeys.forEach(key => {
 
@@ -217,7 +223,13 @@ html5sticky.getNotes = function () {
         }
 
         let id = key.replace('|text', '');
-        let stickynote, bgcolor, htext, ptext, temp_array;
+        let stickynote, bgcolor, htext, ptext, temp_array, folderid;
+
+        // 按照folder id寻找对应目录下的便签
+        folderid = localStorage.getItem(id + '|folderid') || '0';
+        if (folderId !== folderid) {
+            return false;
+        }
 
         // get color and rotation level
         bgcolor = localStorage.getItem(id + '|bgcolor');
@@ -227,7 +239,7 @@ html5sticky.getNotes = function () {
         htext = temp_array[0];
         ptext = temp_array[1];
 
-        stickynote = $('<div class="note_common ' + bgcolor + '" />').appendTo($('#main'));
+        stickynote = $('<div class="note_common ' + bgcolor + '" />').appendTo(mainEl);
         html5sticky.addPin(stickynote);
 
         $(stickynote).append($('<h2></h2>'));
@@ -413,26 +425,6 @@ html5sticky.enlargeNote = function (el) {
     html5sticky.editNote($clone, el);
 };
 
-html5sticky.setup = function () {
-
-    let fontFamily = $('.note_common p').css('font-family');
-
-    // Architects font needs to have lesser line height / spacing for lists
-    if (typeof fontFamily !== 'undefined' && fontFamily.indexOf('Architects') >= 0) {
-        $('.note_common ul').css({lineHeight: '10px', padding: '0px', margin: '0px'});
-        $('.note_common ol').css({lineHeight: '0px', padding: '0px', margin: '0px'});
-    }
-
-    // sticky notes can't be less than 200x200
-    if (stickywidth < 200) {
-        stickywidth = 200;
-    }
-
-    if (stickyheight < 200) {
-        stickyheight = 200;
-    }
-};
-
 // http://phpjs.org/functions/strip_tags:535
 html5sticky.stripTags = function (input, allowed) {
     allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
@@ -456,6 +448,7 @@ html5sticky.export = function () {
             if (allKeys.length) {
                 zipper = new JSZip();
             }
+            let zpFolder = {};
             allKeys.forEach(key => {
 
                 if (!/\|text/.test(key)) {
@@ -463,16 +456,21 @@ html5sticky.export = function () {
                 }
 
                 let id = key.replace('|text', '');
-                let dated, htext, ptext, temp_array;
+                let dated, htext, ptext, temp_array, folderid;
 
                 dated = localStorage.getItem(id + '|dated');
+                folderid = localStorage.getItem(id + '|folderid') || '0';
+                if (!zpFolder[folderid]) {
+                    let forderName = html5sticky.findFolderNameById(folderid);
+                    zpFolder[folderid] = zipper.folder(forderName);
+                }
 
                 // get text info
                 temp_array = localStorage.getItem(id + '|text').split('|');
                 htext = temp_array[0];
                 ptext = temp_array[1];
 
-                zipper.file(htext + '.txt', [
+                zpFolder[folderid].file(htext + '.txt', [
                     '# title:' + htext,
                     '# date:' + dated,
                     '# content:\n' + ptext
@@ -486,7 +484,7 @@ html5sticky.export = function () {
                             url: URL.createObjectURL(new Blob([content], {type: 'application/octet-stream'})),
                             saveAs: true,
                             conflictAction: 'overwrite',
-                            filename: 'fehelper-sticky-notes-' + (new Date*1) + '.zip'
+                            filename: '我的便签笔记-' + (new Date * 1) + '.zip'
                         });
                     });
             }
@@ -494,4 +492,65 @@ html5sticky.export = function () {
             alert('必须接受授权,才能正常下载!');
         }
     });
+};
+
+html5sticky.buildFoldersAndInitNotes = function () {
+    let folders = html5sticky.loadFolders();
+    Object.keys(folders).forEach((f, idx) => {
+        html5sticky.createFolder(f, folders[f]);
+    });
+
+    let current = html5sticky.getCurrentFolder();
+    $('li#f_' + current[1]).addClass('x-selected');
+    html5sticky.getNotes(current[1]);
+
+};
+
+html5sticky.loadFolders = function () {
+    let folders = JSON.parse(localStorage.getItem(STICKYNOTES_FOLDERS) || '{}') || {};
+    if (!folders['默认文件夹']) {
+        folders['默认文件夹'] = '0';
+    }
+    return folders;
+};
+
+html5sticky.saveFolder = function (folder, time) {
+    let folders = html5sticky.loadFolders();
+    folders[folder] = time;
+    localStorage.setItem(STICKYNOTES_FOLDERS, JSON.stringify(folders));
+};
+
+html5sticky.createFolder = function (folder, time) {
+    folder = folder || window.prompt('新建文件夹');
+    if (folder) {
+        if (!time) {
+            let folders = html5sticky.loadFolders();
+            if (folders[folder]) {
+                return alert('你已经创建过这个文件夹!');
+            }
+        }
+        time = time || new Date().getTime();
+        html5sticky.saveFolder(folder, time);
+        return $('<li/>').text(folder).attr('id', 'f_' + time).appendTo('#folders');
+    } else {
+        return alert('文件夹名不能为空!');
+    }
+};
+
+html5sticky.getCurrentFolder = function () {
+    let folder = JSON.parse(localStorage.getItem(STICKYNOTES_SELECTED_FOLDER) || '[]') || [];
+    if (!folder.length) {
+        folder = ['默认文件夹', '0'];
+    }
+    return folder;
+};
+
+html5sticky.setCurrentFolder = function (txt, id) {
+    localStorage.setItem(STICKYNOTES_SELECTED_FOLDER, JSON.stringify([txt, id]));
+};
+
+html5sticky.findFolderNameById = function (folderId) {
+    let folders = html5sticky.loadFolders();
+    let arr = Object.keys(folders).filter(f => String(folders[f]) === String(folderId));
+    return arr.length ? arr[0] : '默认文件夹';
 };

+ 54 - 6
apps/sticky-notes/index.css

@@ -20,10 +20,62 @@
 	margin-left: 10px;
 	color: #bbb;
 }
+.x-wrapper {
+	position: relative;
+	color: #666;
+}
+.x-wrapper .x-sidebar {
+	position: absolute;
+	left: 0;
+	top:0;
+	width: 300px;
+	bottom: 0;
+	background: #fefefe;
+	border-right: 1px solid #ddd;
+}
+.x-wrapper #main {
+	margin-left:300px;
+	padding: 20px;
+	min-height: 500px;
+}
+.x-sidebar .x-tools {
+	background: #f1f1f1;
+	padding: 2px 10px;
+	text-align: right;
+	border-bottom: 1px solid #ccc;
+}
+.x-sidebar .x-tools a{
+	color:#48b;
+	font-size: 12px;
+}
+.x-sidebar ul {
+	margin: 0;
+	padding: 0;
+	list-style: none;
+}
+.x-sidebar ul li {
+	padding: 5px 10px;
+	background-color: #fff;
+	border-bottom: 1px solid #eee;
+	cursor: default;
+	font-size: 14px;
+	overflow: hidden;
+	text-overflow:ellipsis;
+	white-space: nowrap;
+	position: relative;
+}
+.x-sidebar ul li.x-selected:after {
+	content: '\2714';
+	position: absolute;
+	right: 10px;
+}
+.x-sidebar ul li:hover {
+	background: #f5f5f5;
+	color: #48b;
+}
 
 
-
-/* common styles start */
+	/* common styles start */
 h1{font-size:1.6em;}
 h2{font-size:1em;}
 a,a:visited{color:#fff;outline:none !important;text-decoration:none;}
@@ -43,10 +95,6 @@ h1{float:left; margin-right:30px; line-height:20px;color:#f6f6f6;text-shadow: 1p
 .topsection img{margin-top:7px;float:left;margin-right:3px;}
 .topsection a{float:left;display:inline;}
 
-#main{
-	margin: 20px;
-	min-height: 200px;
-}
 
 #head{
 	position:absolute;

+ 10 - 2
apps/sticky-notes/index.html

@@ -16,7 +16,7 @@
                             <img src="../static/img/fe-16.png" alt="fehelper"/> FeHelper</a>:我的便签笔记
 
                         <span class="x-toolbox">
-                            <a href="#" id="addnote" class="x-tooltip blue-tooltip">新建</a><span class="x-line">|</span>
+                            <a href="#" id="addnote" class="x-tooltip blue-tooltip">新建笔记</a><span class="x-line">|</span>
                             <a href="#" id="collapse" class="x-tooltip blue-tooltip">折叠模式</a><span class="x-line">|</span>
                             <a href="#" id="expand" class="x-tooltip blue-tooltip">预览模式</a><span class="x-line">|</span>
                             <a href="#" id="removenotes" class="x-tooltip blue-tooltip">全部删除</a><span class="x-line">|</span>
@@ -26,7 +26,15 @@
                     </h3>
                 </div>
 
-                <div id="main"></div>
+                <div class="x-wrapper">
+                    <div class="x-sidebar">
+                        <div class="x-tools">
+                            <a href="#" id="createFolder">新建文件夹</a>
+                        </div>
+                        <ul id="folders"></ul>
+                    </div>
+                    <div id="main"></div>
+                </div>
                 <div class="clearfix"></div>
             </div>
         </div>

+ 20 - 6
apps/sticky-notes/index.js

@@ -65,20 +65,34 @@ let StickyNotes = (() => {
         });
 
         // 下载
-        $('#export').click(function(e){
+        $('#export').click(function (e) {
             html5sticky.export();
             return false;
         });
+
+        // 文件夹选中
+        $('#folders').delegate('li', 'click', function (e) {
+            $(this).addClass('x-selected').siblings('li').removeClass('x-selected');
+            let txt = $(this).text();
+            let id = $(this).attr('id').replace(/^f_/, '');
+            html5sticky.setCurrentFolder(txt, id);
+            html5sticky.getNotes(id);
+        });
+
+        // 创建文件夹
+        $('#createFolder').click(function (e) {
+            let el = html5sticky.createFolder();
+            if (el) {
+                el.trigger('click');
+            }
+            return false;
+        });
     };
 
     // 初始化
     let init = () => {
         $(function () {
-            // initial setup
-            html5sticky.setup();
-
-            // get any saved notes on page load
-            html5sticky.getNotes();
+            html5sticky.buildFoldersAndInitNotes();
 
             addListener();
         });