html5sticky.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /**
  2. * 笔记管理工具
  3. * @author zxlie
  4. */
  5. let stickywidth = 220; // width of sticky note (can't be less than 200)
  6. let stickyheight = 200; // height of sticky note (can't be less than 200)
  7. let max_notes = 10000; // maximum number of notes one can store
  8. let allowed_tags = '<br /><br><ol></ol><ul></ul><li></li><strong></strong><i></i>';
  9. let html5sticky = {};
  10. const STICKYNOTES_ALLKEYS = 'stickynotes|allkeys';
  11. const STICKYNOTES_FOLDERS = 'stickynotes|folders';
  12. const STICKYNOTES_SELECTED_FOLDER = 'stickynotes|selected|folder';
  13. // add a note
  14. html5sticky.addNote = function () {
  15. // count total present notes
  16. let tnotes = $('.note_common').length;
  17. if (tnotes === max_notes) {
  18. html5sticky.showMessage('#FFE16B', 'black', '当前便签笔记已经足够多了,不能再添加了!');
  19. return false;
  20. }
  21. // unique localstorage identifier for this sticky note
  22. let nindex = 'stickynote_' + (new Date * 1);
  23. let dated = getDateTime();
  24. let dateStr = new Date();
  25. // get random color
  26. let bgcolor = html5sticky.getColor();
  27. let folderId = html5sticky.getCurrentFolder()[1];
  28. let stickynote = $('<div class="note_common ' + bgcolor + '" />').appendTo($('#m_' + folderId));
  29. // add tape to stickynote
  30. html5sticky.addPin(stickynote);
  31. $(stickynote).append($('<h2>' + dated + '</h2>'));
  32. $(stickynote).append($('<p></p>'));
  33. // append identifier
  34. $(stickynote).append($('<span id="idf_' + nindex + '" />'));
  35. // set width and height of the sticky note
  36. $('.note_common').css({width: stickywidth + 'px', height: stickyheight + 'px'});
  37. $('.note_common p').css({height: (stickyheight - 60) + 'px', width: (stickywidth + 9) + 'px'});
  38. if (!$("#removenotes").is(':visible')) {
  39. $('#removenotes').slideDown('slow');
  40. }
  41. // scroll to newly added sticky note
  42. $('html, body').animate({
  43. scrollTop: $(stickynote).offset().top
  44. });
  45. // 先存key,再存数据
  46. let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
  47. allKeys.push(nindex + '|text');
  48. allKeys.push(nindex + '|bgcolor');
  49. allKeys.push(nindex + '|dated');
  50. allKeys.push(nindex + '|folderid');
  51. localStorage.setItem(STICKYNOTES_ALLKEYS, allKeys.join(','));
  52. // 存数据
  53. localStorage.setItem(nindex + '|text', $(stickynote).find('h2').text() + '|' + $(stickynote).find('p').text());
  54. localStorage.setItem(nindex + '|bgcolor', bgcolor);
  55. localStorage.setItem(nindex + '|dated', dated + '|' + getISODateTime(dateStr));
  56. localStorage.setItem(nindex + '|folderid', folderId);
  57. html5sticky.enlargeNote(stickynote);
  58. let elCounter = $('#f_' + folderId).find('i');
  59. elCounter.text('(' + (parseInt(elCounter.text().replace(/\W/, '')) + 1) + ')');
  60. };
  61. // save note
  62. html5sticky.saveNote = function (el) {
  63. let identifier = html5sticky.getIdentifier($(el));
  64. let htext = html5sticky.stripTags($(el).closest('.bignote').find('.hedit')[0].value, allowed_tags);
  65. let ptext = html5sticky.stripTags($(el).closest('.bignote').find('.pedit')[0].value, allowed_tags);
  66. ptext = ptext.replace(/\r?\n/g, '<br />');
  67. localStorage.setItem(identifier + '|text', htext + '|' + ptext);
  68. $('[id^=idf_' + identifier + ']').closest('.note_common').find('h2').text(htext);
  69. $('[id^=idf_' + identifier + ']').closest('.note_common').find('p').html(ptext);
  70. html5sticky.showMessage('#9BED87', 'black', '笔记保存成功!');
  71. };
  72. // get note identifier
  73. html5sticky.getIdentifier = function (el) {
  74. if (!el) {
  75. return 'stickynote_' + (new Date * 1 + Math.floor(Math.random() * 10));
  76. }
  77. let identifier = $(el).closest('.bignote').find('[id^=idf_]').attr('id');
  78. if (typeof identifier == 'undefined' || identifier == null) {
  79. identifier = $(el).closest('.note_common').find('[id^=idf_]').attr('id');
  80. }
  81. if (typeof identifier != 'undefined') {
  82. identifier = identifier.replace('idf_', '');
  83. return identifier;
  84. }
  85. else {
  86. return false;
  87. }
  88. };
  89. // delete note
  90. html5sticky.deleteNote = function (el) {
  91. if (confirm('确定要删除这个便签笔记吗,一旦删除则不可恢复,请三思?')) {
  92. let identifier = html5sticky.getIdentifier($(el));
  93. localStorage.removeItem(identifier);
  94. localStorage.removeItem(identifier + '|text');
  95. localStorage.removeItem(identifier + '|bgcolor');
  96. localStorage.removeItem(identifier + '|dated');
  97. localStorage.removeItem(identifier + '|folderid');
  98. let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
  99. ['text', 'bgcolor', 'dated', 'folderid'].forEach(function (item) {
  100. let id = identifier + '|' + item;
  101. allKeys.indexOf(id) > -1 && allKeys.splice(allKeys.indexOf(id), 1);
  102. });
  103. localStorage.setItem(STICKYNOTES_ALLKEYS, allKeys.join(','));
  104. $(el).closest('.note_common').fadeOut('slow', function () {
  105. $(el).closest('.note_common').remove();
  106. if (!$(".note_common").length > 0) {
  107. $('#removenotes').slideUp('slow');
  108. }
  109. });
  110. }
  111. };
  112. // delete all notes
  113. html5sticky.deleteAllNotes = function () {
  114. if (confirm('建议删除之前先【全部导出】,否则一旦删除则不可恢复,请三思?')) {
  115. $('.note_common').fadeOut('slow', function () {
  116. $('.note_common').remove();
  117. let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
  118. allKeys.forEach(function (key) {
  119. localStorage.removeItem(key);
  120. });
  121. localStorage.removeItem(STICKYNOTES_ALLKEYS);
  122. html5sticky.deleteAllFolders();
  123. location.reload(true);
  124. });
  125. }
  126. };
  127. // close big note
  128. html5sticky.closeNote = function (el) {
  129. $(el).closest('.bignote')[html5sticky.getAnimation(true)]('slow', function () {
  130. $('#overlay').remove();
  131. });
  132. };
  133. // edit note
  134. html5sticky.editNote = function ($clone, el) {
  135. let ptext = $clone.find('p').html();
  136. ptext = ptext.replace(/(<br \/>|<br>)/g, '\n');
  137. $clone.find('p').replaceWith('<textarea class="pedit" placeholder="在这里添加笔记" />');
  138. $clone.find('.pedit')
  139. .val(ptext)
  140. .css({
  141. 'marginTop': '5px',
  142. 'resize': 'none',
  143. 'outline': 'none'
  144. })
  145. .addClass('inset')
  146. .width('568px')
  147. .height('280px');
  148. // make content editable
  149. let htext = $clone.find('h2').text();
  150. $clone.find('h2').replaceWith('<input type="text" class="hedit" />');
  151. $('.hedit').addClass('inset').val(html5sticky.stripTags(htext, allowed_tags)).width(250);
  152. // put in Close button
  153. $('<a href="#" class="close_stickynote"><img src="./img/delete.png" alt="" title="关闭笔记"></a>')
  154. .css({
  155. position: 'absolute',
  156. top: 7,
  157. right: 5
  158. })
  159. .appendTo($clone);
  160. // put in Save button
  161. $('<a href="#" class="save_stickynote"><img src="./img/save.png" alt="" title="保存笔记"></a>')
  162. .css({
  163. position: 'absolute',
  164. top: 5,
  165. right: 50
  166. })
  167. .appendTo($clone);
  168. };
  169. // load all notes
  170. html5sticky.loadNotes = function (folderId) {
  171. let mainEl = $('#m_' + folderId);
  172. if (!mainEl[0]) {
  173. mainEl = $('<div/>').attr('id', 'm_' + folderId).addClass('clearfix').appendTo('#main');
  174. mainEl.removeClass('hide').siblings('div').addClass('hide');
  175. } else {
  176. mainEl.removeClass('hide').siblings('div').addClass('hide');
  177. return false;
  178. }
  179. // load notes
  180. let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
  181. let counter = 0;
  182. allKeys.forEach(key => {
  183. if (!/\|text/.test(key)) {
  184. return false;
  185. }
  186. let id = key.replace('|text', '');
  187. let stickynote, bgcolor, htext, ptext, temp_array, folderid;
  188. // 按照folder id寻找对应目录下的便签
  189. folderid = localStorage.getItem(id + '|folderid') || '0';
  190. if (String(folderId) !== folderid) {
  191. return false;
  192. }
  193. // get color and rotation level
  194. bgcolor = localStorage.getItem(id + '|bgcolor');
  195. // get text info
  196. temp_array = localStorage.getItem(id + '|text').split('|');
  197. htext = temp_array[0];
  198. ptext = temp_array[1];
  199. stickynote = $('<div class="note_common ' + bgcolor + '" />').appendTo(mainEl);
  200. html5sticky.addPin(stickynote);
  201. $(stickynote).append($('<h2></h2>'));
  202. $(stickynote).append($('<p></p>'));
  203. // append identifier
  204. $(stickynote).append($('<span id="idf_' + id + '" />'));
  205. $(stickynote).find('h2').text(html5sticky.stripTags(htext, allowed_tags));
  206. $(stickynote).find('p').html(html5sticky.stripTags(ptext, allowed_tags));
  207. // set width and height of the sticky note
  208. $('.note_common').css({width: stickywidth + 'px', height: stickyheight + 'px'});
  209. $('.note_common p').css({height: (stickyheight - 60) + 'px', width: (stickywidth - 24) + 'px'});
  210. counter++;
  211. });
  212. $('#f_' + folderId).find('i').text('(' + counter + ')');
  213. };
  214. // collapse notes
  215. html5sticky.collapse = function () {
  216. let height = parseInt($('.note_common:first').find('h2').height() || 0, 10) + 'px';
  217. $('.note_common').animate({height: height}, function () {
  218. $('.note_common').find('p').hide();
  219. });
  220. };
  221. // expand notes
  222. html5sticky.expand = function () {
  223. $('.note_common').animate({height: stickyheight}, function () {
  224. $('.note_common').find('p').fadeIn('slow');
  225. });
  226. };
  227. // share note
  228. html5sticky.showMessage = function (bgcolor, color, msg, callback) {
  229. if (!$('#smsg').is(':visible')) {
  230. $('html, body').animate({
  231. scrollTop: 0
  232. }, 500, function () {
  233. if (!$('#smsg').length) {
  234. $('<div id="smsg">' + msg + '</div>').appendTo($('body')).css({
  235. position: 'absolute',
  236. top: 0,
  237. left: 0,
  238. right: 0,
  239. height: '40px',
  240. lineHeight: '40px',
  241. background: bgcolor,
  242. color: color,
  243. zIndex: 1000,
  244. fontWeight: 'bold',
  245. textAlign: 'center',
  246. opacity: 0.9,
  247. margin: 'auto',
  248. display: 'none'
  249. }).slideDown('show');
  250. setTimeout(function () {
  251. $('#smsg').animate({'width': 'hide'}, function () {
  252. $('#smsg').remove();
  253. callback && callback();
  254. });
  255. }, 2000);
  256. }
  257. });
  258. }
  259. };
  260. // get random color
  261. html5sticky.getColor = function () {
  262. let text = "";
  263. let possible = "0123456789";
  264. text += possible.charAt(Math.floor(Math.random() * possible.length));
  265. return 'stickynote' + text;
  266. };
  267. // get random animation string
  268. html5sticky.getAnimation = function (hideAnimation) {
  269. let words = [];
  270. if (typeof hideAnimation !== 'undefined') {
  271. words[1] = "hide";
  272. words[2] = "fadeOut";
  273. words[3] = "slideUp";
  274. }
  275. else {
  276. words[1] = "show";
  277. words[2] = "fadeIn";
  278. words[3] = "slideDown";
  279. }
  280. // Generate a random number between 1 and 3
  281. let rnd = Math.ceil(Math.random() * 3);
  282. return words[rnd];
  283. };
  284. // add pin to note
  285. html5sticky.addPin = function (el) {
  286. let close = $('<div class="btn-close"><a href="#" class="delete_stickynote"><img src="./img/delete.png" width="24" alt="" title="删除笔记"></a></div>');
  287. let tag = $('<div align="center"><img src="./img/pin.png" alt="" title="关闭"></div>');
  288. $(close).css({
  289. position: 'absolute',
  290. top: -15,
  291. right: -15
  292. }).hide().prependTo($(el));
  293. $(tag).css({
  294. position: 'absolute',
  295. zIndex: 99,
  296. top: -15,
  297. left: parseInt(stickywidth / 2, 10) - 10
  298. }).prependTo($(el));
  299. };
  300. // enlarge note for editing
  301. html5sticky.enlargeNote = function (el) {
  302. $this = $(el);
  303. // create overlay
  304. $('<div id="overlay" />').css({
  305. position: 'fixed',
  306. background: 'rgba(0,0,0,0.5)',
  307. top: '0',
  308. left: '0',
  309. width: '100%',
  310. height: '100%',
  311. zIndex: '100'
  312. }).appendTo($('body'));
  313. $clone = $(el).clone().removeClass('note_common').addClass('bignote').appendTo($('#overlay'));
  314. // remove the pin
  315. $clone.find($('img[src*="pin.png"]').closest('div')).hide();
  316. // change delete button title
  317. $clone.find($('img[src*="delete.png"]').closest('div')).hide();
  318. $($clone).css({
  319. position: 'absolute',
  320. zIndex: 500,
  321. cursor: 'default',
  322. paddingTop: '5px',
  323. width: '600px',
  324. height: '376px',
  325. top: '50%',
  326. left: '50%',
  327. display: 'none',
  328. marginLeft: '-300px',
  329. marginTop: '-200px'
  330. });
  331. $($clone)[html5sticky.getAnimation()](400);
  332. // add date and time info
  333. let dateStr = '', dateAgo = '';
  334. let identifier = html5sticky.getIdentifier($(el));
  335. let dateTime = localStorage.getItem(identifier + '|dated');
  336. let timeImg = '<img class="left" align="absmiddle" src="./img/time.png">';
  337. dateStr = dateTime.split('|')[0];
  338. dateAgo = prettyDate(dateTime.split('|')[1]);
  339. dateStr = (dateStr.length > 0) ? '创建于:' + dateStr : '';
  340. dateAgo = (dateAgo.length > 0) ? ' (' + dateAgo + ')' : '';
  341. timeImg = (dateStr.length > 0) ? timeImg : '';
  342. $('<div class="timeago left" />').prependTo($clone);
  343. $('.timeago').css({fontSize: '12px', fontFamily: 'tahoma'})
  344. .html(timeImg + '&nbsp;&nbsp;' + dateStr + dateAgo)
  345. .after('<div class="clear" />');
  346. // hide the utility buttons
  347. $($clone).find('.icons-footer').hide();
  348. // make content editable
  349. html5sticky.editNote($clone, el);
  350. };
  351. // http://phpjs.org/functions/strip_tags:535
  352. html5sticky.stripTags = function (input, allowed) {
  353. allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
  354. let tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
  355. commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
  356. return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
  357. return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
  358. });
  359. };
  360. // 全部notes导出到本地
  361. html5sticky.export = function () {
  362. chrome.permissions.request({
  363. permissions: ['downloads']
  364. }, (granted) => {
  365. if (granted) {
  366. let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
  367. let zipper = null;
  368. if (allKeys.length) {
  369. zipper = new JSZip();
  370. }
  371. let zpFolder = {};
  372. allKeys.forEach(key => {
  373. if (!/\|text/.test(key)) {
  374. return false;
  375. }
  376. let id = key.replace('|text', '');
  377. let dated, htext, ptext, temp_array, folderid;
  378. dated = localStorage.getItem(id + '|dated');
  379. folderid = localStorage.getItem(id + '|folderid') || '0';
  380. if (!zpFolder[folderid]) {
  381. let forderName = html5sticky.findFolderNameById(folderid);
  382. zpFolder[folderid] = zipper.folder(forderName);
  383. }
  384. // get text info
  385. temp_array = localStorage.getItem(id + '|text').split('|');
  386. htext = temp_array[0];
  387. ptext = temp_array[1];
  388. zpFolder[folderid].file(htext + '.txt', [
  389. '# title:' + htext,
  390. '# date:' + dated,
  391. '# content:\n' + ptext
  392. ].join('\n\n'));
  393. });
  394. if (zipper) {
  395. zipper.generateAsync({type: "blob"})
  396. .then(function (content) {
  397. chrome.downloads.download({
  398. url: URL.createObjectURL(new Blob([content], {type: 'application/octet-stream'})),
  399. saveAs: true,
  400. conflictAction: 'overwrite',
  401. filename: '我的便签笔记-' + (new Date * 1) + '.zip'
  402. });
  403. });
  404. }
  405. } else {
  406. alert('必须接受授权,才能正常下载!');
  407. }
  408. });
  409. };
  410. // 导入笔记
  411. html5sticky.importNotes = function () {
  412. let Model = (function () {
  413. zip.workerScriptsPath = "/static/vendor/jszip/";
  414. let URL = window.webkitURL || window.mozURL || window.URL;
  415. return {
  416. getEntries: function (file, onend) {
  417. zip.createReader(new zip.BlobReader(file), function (zipReader) {
  418. zipReader.getEntries(onend);
  419. }, function (e) {
  420. console.log(e);
  421. });
  422. },
  423. getEntryFile: function (entry, onend, onprogress) {
  424. entry.getData(new zip.TextWriter(), function (text) {
  425. onend(text);
  426. }, onprogress);
  427. }
  428. };
  429. })();
  430. let fileInput = document.createElement('input');
  431. fileInput.type = 'file';
  432. fileInput.accept = 'application/zip';
  433. fileInput.style.cssText = 'position:absolute;top:-100px;left:-100px';
  434. fileInput.addEventListener('change', function (evt) {
  435. Model.getEntries(fileInput.files[0], function (entries) {
  436. let counter = 0;
  437. let size = entries.filter((entry) => !entry.directory).length;
  438. entries.forEach(function (entry) {
  439. if (entry.directory) {
  440. counter++;
  441. let fname = entry.filename.replace(/\//, '');
  442. let folders = html5sticky.loadFolders();
  443. if (!folders[fname]) {
  444. html5sticky.saveFolder(fname, new Date().getTime());
  445. }
  446. } else {
  447. Model.getEntryFile(entry, function (text) {
  448. let identifier = html5sticky.getIdentifier();
  449. let htext = text.split('# date:')[0].split('# title:')[1].trim();
  450. let dtext = text.split('# date:')[1].split('# content:')[0].trim();
  451. let ptext = text.split('# content:')[1].trim().replace(/\r?\n/g, '<br />');
  452. let folderId = html5sticky.findFolderByName(entry.filename.split('/')[0]);
  453. // 先存key,再存数据
  454. let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
  455. allKeys.push(identifier + '|text');
  456. allKeys.push(identifier + '|bgcolor');
  457. allKeys.push(identifier + '|dated');
  458. allKeys.push(identifier + '|folderid');
  459. localStorage.setItem(STICKYNOTES_ALLKEYS, allKeys.join(','));
  460. localStorage.setItem(identifier + '|text', htext + '|' + ptext);
  461. localStorage.setItem(identifier + '|bgcolor', html5sticky.getColor());
  462. localStorage.setItem(identifier + '|dated', dtext);
  463. localStorage.setItem(identifier + '|folderid', folderId);
  464. counter++;
  465. if (counter === size) {
  466. html5sticky.showMessage('#9BED87', 'black', '操作成功!共导入' + counter + '条笔记!', () => {
  467. location.reload();
  468. });
  469. }
  470. });
  471. }
  472. });
  473. });
  474. }, false);
  475. document.body.appendChild(fileInput);
  476. fileInput.click();
  477. };
  478. html5sticky.buildFoldersAndInitNotes = function () {
  479. let folders = html5sticky.loadFolders();
  480. Object.keys(folders).forEach((f, idx) => {
  481. html5sticky.createFolder(f, folders[f]);
  482. html5sticky.loadNotes(folders[f]);
  483. });
  484. let current = html5sticky.getCurrentFolder();
  485. $('li#f_' + current[1]).addClass('x-selected');
  486. html5sticky.loadNotes(current[1]);
  487. };
  488. html5sticky.loadFolders = function () {
  489. let folders = JSON.parse(localStorage.getItem(STICKYNOTES_FOLDERS) || '{}') || {};
  490. if (!folders['默认文件夹']) {
  491. folders['默认文件夹'] = '0';
  492. }
  493. return folders;
  494. };
  495. html5sticky.deleteAllFolders = function () {
  496. localStorage.setItem(STICKYNOTES_FOLDERS, '{}');
  497. localStorage.setItem(STICKYNOTES_SELECTED_FOLDER, '[]')
  498. };
  499. html5sticky.saveFolder = function (folder, time) {
  500. let folders = html5sticky.loadFolders();
  501. folders[folder] = time;
  502. localStorage.setItem(STICKYNOTES_FOLDERS, JSON.stringify(folders));
  503. };
  504. html5sticky.createFolder = function (folder, time) {
  505. folder = folder || window.prompt('新建文件夹');
  506. if (folder) {
  507. if (!time) {
  508. let folders = html5sticky.loadFolders();
  509. if (folders[folder]) {
  510. return alert('你已经创建过这个文件夹!');
  511. }
  512. }
  513. time = time || new Date().getTime();
  514. html5sticky.saveFolder(folder, time);
  515. return $('<li><span></span><i>(0)</i></li>').find('span').text(folder).end().attr('id', 'f_' + time).appendTo('#folders');
  516. } else if (folder !== null) {
  517. return alert('文件夹名不能为空!');
  518. }
  519. };
  520. html5sticky.getCurrentFolder = function () {
  521. let folder = JSON.parse(localStorage.getItem(STICKYNOTES_SELECTED_FOLDER) || '[]') || [];
  522. if (!folder.length) {
  523. folder = ['默认文件夹', '0'];
  524. }
  525. return folder;
  526. };
  527. html5sticky.setCurrentFolder = function (txt, id) {
  528. localStorage.setItem(STICKYNOTES_SELECTED_FOLDER, JSON.stringify([txt, id]));
  529. };
  530. html5sticky.findFolderNameById = function (folderId) {
  531. let folders = html5sticky.loadFolders();
  532. let arr = Object.keys(folders).filter(f => String(folders[f]) === String(folderId));
  533. return arr.length ? arr[0] : '默认文件夹';
  534. };
  535. html5sticky.findFolderByName = function (name) {
  536. let folders = JSON.parse(localStorage.getItem(STICKYNOTES_FOLDERS) || '{}') || {};
  537. if (!folders['默认文件夹']) {
  538. folders['默认文件夹'] = '0';
  539. }
  540. return folders[name];
  541. };