/*
HTML5STICKY (http://github.com/sarfraznawaz2005/HTML5Sticky)
================================================================
Author : Sarfraz Ahmed (sarfraznawaz2005@gmail.com)
Twitter : @sarfraznawaz
Blog : http://sarfraznawaz.wordpress.com/
LICENSE : MIT
================================================================
@updated zhaoxianlie
*/
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 = '
')
.css({
position: 'absolute',
top: 7,
right: 5
})
.appendTo($clone);
// put in Save button
$('
')
.css({
position: 'absolute',
top: 5,
right: 50
})
.appendTo($clone);
};
// get all notes
html5sticky.getNotes = function (folderId) {
let mainEl = $('#main').html('');
let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
allKeys.forEach(key => {
if (!/\|text/.test(key)) {
return false;
}
let id = key.replace('|text', '');
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');
// get text info
temp_array = localStorage.getItem(id + '|text').split('|');
htext = temp_array[0];
ptext = temp_array[1];
stickynote = $('').appendTo(mainEl);
html5sticky.addPin(stickynote);
$(stickynote).append($(''));
$(stickynote).append($(''));
// append identifier
$(stickynote).append($(''));
$(stickynote).find('h2').text(html5sticky.stripTags(htext, allowed_tags));
$(stickynote).find('p').html(html5sticky.stripTags(ptext, allowed_tags));
// 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 - 24) + 'px'});
});
};
// collapse notes
html5sticky.collapse = function () {
let height = parseInt($('.note_common:first').find('h2').height() || 0, 10) + 'px';
$('.note_common').animate({height: height}, function () {
$('.note_common').find('p').hide();
});
};
// expand notes
html5sticky.expand = function () {
$('.note_common').animate({height: stickyheight}, function () {
$('.note_common').find('p').fadeIn('slow');
});
};
// share note
html5sticky.showMessage = function (bgcolor, color, msg) {
if (!$('#smsg').is(':visible')) {
$('html, body').animate({
scrollTop: 0
}, 500, function () {
if (!$('#smsg').length) {
$('
';
dateStr = dateTime.split('|')[0];
dateAgo = prettyDate(dateTime.split('|')[1]);
dateStr = (dateStr.length > 0) ? '创建于:' + dateStr : '';
dateAgo = (dateAgo.length > 0) ? ' (' + dateAgo + ')' : '';
timeImg = (dateStr.length > 0) ? timeImg : '';
$('').prependTo($clone);
$('.timeago').css({fontSize: '12px', fontFamily: 'tahoma'})
.html(timeImg + ' ' + dateStr + dateAgo)
.after('');
// hide the utility buttons
$($clone).find('.icons-footer').hide();
// make content editable
html5sticky.editNote($clone, el);
};
// http://phpjs.org/functions/strip_tags:535
html5sticky.stripTags = function (input, allowed) {
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
let tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
};
// 全部notes导出到本地
html5sticky.export = function () {
chrome.permissions.request({
permissions: ['downloads']
}, (granted) => {
if (granted) {
let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
let zipper = null;
if (allKeys.length) {
zipper = new JSZip();
}
let zpFolder = {};
allKeys.forEach(key => {
if (!/\|text/.test(key)) {
return false;
}
let id = key.replace('|text', '');
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];
zpFolder[folderid].file(htext + '.txt', [
'# title:' + htext,
'# date:' + dated,
'# content:\n' + ptext
].join('\n\n'));
});
if (zipper) {
zipper.generateAsync({type: "blob"})
.then(function (content) {
chrome.downloads.download({
url: URL.createObjectURL(new Blob([content], {type: 'application/octet-stream'})),
saveAs: true,
conflictAction: 'overwrite',
filename: '我的便签笔记-' + (new Date * 1) + '.zip'
});
});
}
} else {
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 $('').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] : '默认文件夹';
};