/** * 笔记管理工具 * @author zxlie */ let stickywidth = 220; // width of sticky note (can't be less than 200) let stickyheight = 200; // height of sticky note (can't be less than 200) let max_notes = 10000; // maximum number of notes one can store let allowed_tags = '

  1. '; 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 () { // count total present notes let tnotes = $('.note_common').length; if (tnotes === max_notes) { html5sticky.showMessage('#FFE16B', 'black', '当前便签笔记已经足够多了,不能再添加了!'); return false; } // unique localstorage identifier for this sticky note let nindex = 'stickynote_' + (new Date * 1); let dated = getDateTime(); let dateStr = new Date(); // get random color let bgcolor = html5sticky.getColor(); let folderId = html5sticky.getCurrentFolder()[1]; let stickynote = $('
    ').appendTo($('#main')); // add tape to stickynote html5sticky.addPin(stickynote); $(stickynote).append($('

    ' + dated + '

    ')); $(stickynote).append($('

    ')); // append identifier $(stickynote).append($('')); // set width and height of the sticky note $('.note_common').css({width: stickywidth + 'px', height: stickyheight + 'px'}); $('.note_common p').css({height: (stickyheight - 60) + 'px', width: (stickywidth + 9) + 'px'}); // scroll to newly added sticky note $('html, body').animate({ scrollTop: $(stickynote).offset().top }); // 先存key,再存数据 let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(','); allKeys.push(nindex + '|text'); allKeys.push(nindex + '|bgcolor'); allKeys.push(nindex + '|dated'); allKeys.push(nindex + '|folderid'); localStorage.setItem(STICKYNOTES_ALLKEYS, allKeys.join(',')); // 存数据 localStorage.setItem(nindex + '|text', $(stickynote).find('h2').text() + '|' + $(stickynote).find('p').text()); localStorage.setItem(nindex + '|bgcolor', bgcolor); localStorage.setItem(nindex + '|dated', dated + '|' + getISODateTime(dateStr)); localStorage.setItem(nindex + '|folderid', folderId); html5sticky.enlargeNote(stickynote); html5sticky.updateNotesCountForFolder(); }; // save note html5sticky.saveNote = function (el) { let identifier = html5sticky.getIdentifier($(el)); let htext = html5sticky.stripTags($(el).closest('.bignote').find('.hedit')[0].value, allowed_tags); let ptext = html5sticky.stripTags($(el).closest('.bignote').find('.pedit')[0].value, allowed_tags); ptext = ptext.replace(/\r?\n/g, '
    '); localStorage.setItem(identifier + '|text', htext + '|' + ptext); let theNoteEl = $('[id^=idf_' + identifier + ']').closest('.note_common'); theNoteEl.find('h2').text(htext); theNoteEl.find('p').html(ptext); let oldFolder = localStorage.getItem(identifier + '|folderid') || 0; let newFolder = $(el).closest('.bignote').find('.fedit')[0].value; localStorage.setItem(identifier + '|folderid', newFolder); html5sticky.closeNote(el); html5sticky.showMessage('#9BED87', 'black', '笔记保存成功!'); // 发生了文件夹变更 if (String(oldFolder) !== String(newFolder)) { theNoteEl.fadeOut('slow', function () { theNoteEl.remove(); if (!$(".note_common").length > 0) { $('#removenotes').slideUp('slow'); } }); html5sticky.updateNotesCountForFolder(); } }; html5sticky.updateNotesCountForFolder = function () { // 刷新folder计数 let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(','); let folders = html5sticky.loadFolders(); Object.keys(folders).forEach(f => { let counter = allKeys.filter(key => /\|folderid/.test(key) && (String(folders[f]) === (localStorage.getItem(key) || '0'))).length; $('#f_' + folders[f]).find('i').text('(' + counter + ')'); }); }; // get note identifier html5sticky.getIdentifier = function (el) { if (!el) { return 'stickynote_' + (new Date * 1 + Math.floor(Math.random() * 10)); } let identifier = $(el).closest('.bignote').find('[id^=idf_]').attr('id'); if (typeof identifier == 'undefined' || identifier == null) { identifier = $(el).closest('.note_common').find('[id^=idf_]').attr('id'); } if (typeof identifier != 'undefined') { identifier = identifier.replace('idf_', ''); return identifier; } else { return false; } }; html5sticky.deleteNoteById = function (identifier) { localStorage.removeItem(identifier); 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', 'folderid'].forEach(function (item) { let id = identifier + '|' + item; allKeys.indexOf(id) > -1 && allKeys.splice(allKeys.indexOf(id), 1); }); localStorage.setItem(STICKYNOTES_ALLKEYS, allKeys.join(',')); }; // delete note html5sticky.deleteNote = function (el) { if (confirm('确定要删除这个便签笔记吗,一旦删除则不可恢复,请三思?')) { let identifier = html5sticky.getIdentifier($(el)); html5sticky.deleteNoteById(identifier); $(el).closest('.note_common').fadeOut('slow', function () { $(el).closest('.note_common').remove(); }); } }; // delete all notes html5sticky.deleteAllNotes = function () { if (confirm('建议删除之前先【全部导出】,否则一旦删除则不可恢复,请三思?')) { $('.note_common').fadeOut('slow', function () { $('.note_common').remove(); let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(','); allKeys.forEach(function (key) { localStorage.removeItem(key); }); localStorage.removeItem(STICKYNOTES_ALLKEYS); html5sticky.deleteAllFolders(); location.reload(true); }); } }; // close big note html5sticky.closeNote = function (el) { $(el).closest('.bignote')[html5sticky.getAnimation(true)]('slow', function () { $('#overlay').remove(); }); }; // edit note html5sticky.editNote = function ($clone, el) { let ptext = $clone.find('p').html(); ptext = ptext.replace(/(
    |
    )/g, '\n'); $clone.find('p').replaceWith('