API Reference
interface Task {
id: string; // Unique identifier
text: string; // Task description
completed: boolean; // Completion status
dueDate?: string; // Optional due date (ISO format)
priority: 'low' | 'medium' | 'high'; // Priority level
createdAt: Date; // Creation timestamp
completedAt?: Date; // Completion timestamp (if completed)
}
interface HistoryRecord {
id: string; // Unique identifier
action: 'created' | 'completed' | 'deleted' | 'updated';
description: string; // Human-readable description
timestamp: Date; // Action timestamp
}
todo-tasks
: Array of Task objectstodo-history
: Array of HistoryRecord objects
// Save tasks to local storage
localStorage.setItem('todo-tasks', JSON.stringify(tasks));
// Load tasks from local storage
const tasks = JSON.parse(localStorage.getItem('todo-tasks') || '[]');
// Save history to local storage
localStorage.setItem('todo-history', JSON.stringify(history));
// Load history from local storage
const history = JSON.parse(localStorage.getItem('todo-history') || '[]');
{
"tasks": [
{
"id": "abc123",
"text": "Complete project",
"completed": false,
"dueDate": "2024-02-15",
"priority": "high",
"createdAt": "2024-02-01T10:00:00.000Z",
"completedAt": null
}
],
"exportDate": "2024-02-01T10:00:00.000Z"
}
function validateImportData(data) {
if (!data.tasks || !Array.isArray(data.tasks)) {
throw new Error('Invalid data format: tasks array required');
}
data.tasks.forEach((task) => {
if (!task.id || !task.text || typeof task.completed !== 'boolean') {
throw new Error('Invalid task format');
}
});
return true;
}
// Task created
window.dispatchEvent(
new CustomEvent('taskCreated', {
detail: { task: newTask },
}),
);
// Task completed
window.dispatchEvent(
new CustomEvent('taskCompleted', {
detail: { task: completedTask },
}),
);
// Task updated
window.dispatchEvent(
new CustomEvent('taskUpdated', {
detail: { task: updatedTask },
}),
);
// Task deleted
window.dispatchEvent(
new CustomEvent('taskDeleted', {
detail: { taskId: deletedTaskId },
}),
);
// History record added
window.dispatchEvent(
new CustomEvent('historyAdded', {
detail: { record: newRecord },
}),
);
function generateId() {
return Math.random().toString(36).substr(2, 9);
}
function getTomorrow() {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
return tomorrow.toISOString().split('T')[0];
}
function isOverdue(dueDate) {
return new Date(dueDate) < new Date();
}
function formatDate(date) {
return new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
}).format(new Date(date));
}
function formatTimeAgo(date) {
const now = new Date();
const diff = now.getTime() - date.getTime();
const minutes = Math.floor(diff / 60000);
const hours = Math.floor(diff / 3600000);
const days = Math.floor(diff / 86400000);
if (days > 0) return `${days} days ago`;
if (hours > 0) return `${hours} hours ago`;
if (minutes > 0) return `${minutes} minutes ago`;
return 'Just now';
}
function sortTasks(tasks) {
return tasks.sort((a, b) => {
// Priority sorting
const priorityOrder = { high: 3, medium: 2, low: 1 };
const priorityDiff = priorityOrder[b.priority] - priorityOrder[a.priority];
if (priorityDiff !== 0) return priorityDiff;
// Due date sorting
if (a.dueDate && b.dueDate) {
return new Date(a.dueDate).getTime() - new Date(b.dueDate).getTime();
}
if (a.dueDate) return -1;
if (b.dueDate) return 1;
// Creation time sorting
return b.createdAt.getTime() - a.createdAt.getTime();
});
}
localStorage
- Data persistenceJSON.parse/stringify
- Data serializationCustomEvent
- Event systemIntl.DateTimeFormat
- Date formatting
- Chrome 51+
- Firefox 50+
- Safari 10+
- Edge 12+
try {
const tasks = JSON.parse(localStorage.getItem('todo-tasks'));
} catch (error) {
console.error('Failed to load tasks:', error);
// Fallback to empty array
tasks = [];
}
function validateTask(task) {
const errors = [];
if (!task.text || task.text.trim().length === 0) {
errors.push('Task text is required');
}
if (!['low', 'medium', 'high'].includes(task.priority)) {
errors.push('Invalid priority level');
}
if (task.dueDate && isNaN(Date.parse(task.dueDate))) {
errors.push('Invalid due date format');
}
return errors;
}
// Pagination for large lists
function getTasksPage(tasks, page = 1, pageSize = 50) {
const start = (page - 1) * pageSize;
const end = start + pageSize;
return tasks.slice(start, end);
}
// Virtual scrolling support
function getVisibleTasks(tasks, scrollTop, viewportHeight, itemHeight) {
const startIndex = Math.floor(scrollTop / itemHeight);
const visibleCount = Math.ceil(viewportHeight / itemHeight);
return tasks.slice(startIndex, startIndex + visibleCount);
}
// Limit history records
function addHistoryRecord(history, record, maxRecords = 100) {
history.unshift(record);
if (history.length > maxRecords) {
history = history.slice(0, maxRecords);
}
return history;
}
// Clean up old completed tasks
function cleanupOldTasks(tasks, daysToKeep = 30) {
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - daysToKeep);
return tasks.filter((task) => !task.completed || task.completedAt > cutoff);
}
// Export to external calendar
function exportToCalendar(tasks) {
const calendarEvents = tasks
.filter((task) => task.dueDate && !task.completed)
.map((task) => ({
title: task.text,
start: task.dueDate,
priority: task.priority,
}));
// Generate calendar file or send to API
return calendarEvents;
}
// Import from external source
async function importFromExternal(source) {
try {
const response = await fetch(source);
const data = await response.json();
if (validateImportData(data)) {
return data.tasks;
}
} catch (error) {
console.error('Import failed:', error);
throw error;
}
}
// Send webhook on task completion
async function sendWebhook(task, webhookUrl) {
try {
await fetch(webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
event: 'taskCompleted',
task: task,
timestamp: new Date().toISOString(),
}),
});
} catch (error) {
console.error('Webhook failed:', error);
}
}