/**
* NoteBase - Abstract base class for note views (widget, index).
*
* Shared logic: loading, sorting, utilities, action handlers.
* Subclasses implement:
* _getTemplateId() → template id string
* _renderContent() → render notes into the container
* _setupEventListeners() → attach UI event listeners
*/
import { DBManager } from '../services/DBManager.js';
import { DialogService } from '../services/DialogService.js';
import { Router } from '../services/Router.js';
import './NoteItem.js';
import './ShareButton.js';
export class NoteBase extends HTMLElement {
_notes = [];
_sortMode = 'updated-desc';
_timeoutId = null;
_isListView = true;
connectedCallback() {
const templateId = this._getTemplateId();
const template = document.getElementById(templateId);
if (!template) {
console.error(`NoteBase: Template with id '${templateId}' not found`);
return;
}
const content = template.content.cloneNode(true);
this.appendChild(content);
this._init();
}
async _init() {
try {
await DBManager.init();
this._notes = await DBManager.getItems({ type: 'note', archived: false });
this._sortNotes();
this._renderContent();
this._setupEventListeners();
} catch (error) {
console.error('Error initializing notes:', error);
this._notes = [];
this._renderContent();
this._setupEventListeners();
}
}
// ------------------------------------------------------------------
// Sorting
// ------------------------------------------------------------------
_sortNotes() {
const mode = this._sortMode;
this._notes.sort((a, b) => {
if (mode === 'title-asc') {
const titleA = (this._extractTitle(a.content) || '').toLowerCase();
const titleB = (this._extractTitle(b.content) || '').toLowerCase();
return titleA.localeCompare(titleB);
}
if (mode === 'created-asc') {
const dateA = new Date(a.meta?.createdAt || 0);
const dateB = new Date(b.meta?.createdAt || 0);
return dateA - dateB;
}
// updated-desc (default) and created-desc both use updatedAt, falling back to createdAt
const dateA = new Date(a.meta?.updatedAt || a.meta?.createdAt || 0);
const dateB = new Date(b.meta?.updatedAt || b.meta?.createdAt || 0);
return dateB - dateA;
});
}
_setSortMode(mode) {
if (this._sortMode === mode) return;
this._sortMode = mode;
this._sortNotes();
this._renderContent();
}
// ------------------------------------------------------------------
// Utilities
// ------------------------------------------------------------------
_extractTitle(content) {
if (!content) return 'Untitled';
const firstLine = content.split('\n')[0].trim();
return firstLine || 'Untitled';
}
_getPreview(content, maxLength = 60) {
if (!content) return '';
const text = content.replace(/\n/g, ' ').trim();
if (text.length <= maxLength) return text;
return text.slice(0, maxLength) + '...';
}
_formatDate(dateString) {
if (!dateString) return '';
return new Date(dateString).toLocaleString();
}
_generateNoteId(content) {
const now = new Date();
const timestamp = now.getFullYear().toString() +
String(now.getMonth() + 1).padStart(2, '0') +
String(now.getDate()).padStart(2, '0') +
String(now.getHours()).padStart(2, '0') +
String(now.getMinutes()).padStart(2, '0') +
String(now.getSeconds()).padStart(2, '0');
const text = content || 'untitled';
const slug = text
.slice(0, 20)
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
return `${timestamp}-${slug || 'note'}`;
}
// ------------------------------------------------------------------
// CRUD helpers
// ------------------------------------------------------------------
async _createNewNote() {
const now = new Date().toISOString();
const newNote = {
id: this._generateNoteId(''),
type: 'note',
content: '',
links: [],
meta: {
createdAt: now,
updatedAt: now,
archived: false,
completed: false
}
};
this._notes.unshift(newNote);
try {
await DBManager.saveItem(newNote);
} catch (error) {
console.error('Error creating new note:', error);
}
Router.go(`/note/${newNote.id}`);
}
async _createNoteWithContent(content) {
const now = new Date().toISOString();
const newNote = {
id: this._generateNoteId(content),
type: 'note',
content: content,
links: [],
meta: {
createdAt: now,
updatedAt: now,
archived: false,
completed: false
}
};
this._notes.unshift(newNote);
try {
await DBManager.saveItem(newNote);
} catch (error) {
console.error('Error creating note:', error);
}
return newNote;
}
async _reloadNotes() {
this._notes = await DBManager.getItems({ type: 'note', archived: false });
this._sortNotes();
this._renderContent();
}
// ------------------------------------------------------------------
// Shared action handlers
// ------------------------------------------------------------------
async _showNoteActions(noteId) {
const note = this._notes.find(n => n.id === noteId);
if (!note) return;
const action = await DialogService.showActions([
{ label: 'Share', icon: '', action: 'share' },
{ label: 'Duplicate Note', icon: '', action: 'duplicate' },
{ label: 'Copy to List', icon: '', action: 'copy-to-list' },
{ label: 'Convert to List', icon: '', action: 'convert' },
{ label: 'Move to List', icon: '', action: 'move-to-list' },
{ label: 'Merge with Note', icon: '', action: 'merge' },
{ label: 'Archive', icon: '', action: 'archive', danger: true }
]);
if (!action) return;
try {
switch (action) {
case 'share':
await this._doShareNote(noteId);
break;
case 'duplicate':
await this._doDuplicateNote(noteId);
break;
case 'copy-to-list':
await this._doCopyToList(noteId);
break;
case 'convert':
await this._doConvertToList(noteId);
break;
case 'move-to-list':
await this._doMoveToList(noteId);
break;
case 'merge':
await this._doMergeWithNote(noteId);
break;
case 'archive':
await this._doArchiveNote(noteId);
break;
}
} catch (error) {
console.error('Note action error:', error);
alert(error.message || 'Action failed');
}
}
async _doShareNote(noteId) {
const note = this._notes.find(n => n.id === noteId);
if (!note) return;
const title = this._extractTitle(note.content);
const shareBtn = document.createElement('share-button');
shareBtn.setAttribute('type', 'note');
shareBtn.setAttribute('data-id', noteId);
shareBtn.setAttribute('title', title);
shareBtn.style.position = 'fixed';
shareBtn.style.top = '-9999px';
shareBtn.style.left = '-9999px';
document.body.appendChild(shareBtn);
requestAnimationFrame(() => {
const btn = shareBtn.querySelector('button, .share-trigger-btn, [type="button"]');
if (btn) {
btn.click();
} else {
shareBtn.click();
}
setTimeout(() => {
if (shareBtn.parentNode) shareBtn.parentNode.removeChild(shareBtn);
}, 100);
});
}
async _doDuplicateNote(noteId) {
const newNoteId = await DBManager.duplicateNote(noteId);
await this._reloadNotes();
Router.go(`/note/${newNoteId}`);
}
async _doCopyToList(noteId) {
const lists = await DBManager.getItems({ type: 'list', archived: false });
if (lists.length === 0) {
alert('No lists available. Create a list first.');
return;
}
const note = this._notes.find(n => n.id === noteId);
const title = this._extractTitle(note.content);
const target = await DialogService.pickItem(
lists.map(l => ({ id: l.id, title: l.content || 'Unnamed List', subtitle: `${l.items?.length || 0} items` })),
{ title: `Copy "${title}" to which list?` }
);
if (!target) return;
await DBManager.copyNoteToList(noteId, target.id);
}
async _doConvertToList(noteId) {
const note = this._notes.find(n => n.id === noteId);
const title = this._extractTitle(note.content);
const confirmed = await DialogService.confirm(`Convert "${title}" to a list? Each line will become an item.`, 'Convert');
if (!confirmed) return;
const newListId = await DBManager.convertNoteToList(noteId);
await this._reloadNotes();
Router.go(`/list/${newListId}`);
}
async _doMoveToList(noteId) {
const lists = await DBManager.getItems({ type: 'list', archived: false });
if (lists.length === 0) {
alert('No lists available. Create a list first.');
return;
}
const note = this._notes.find(n => n.id === noteId);
const title = this._extractTitle(note.content);
const target = await DialogService.pickItem(
lists.map(l => ({ id: l.id, title: this._extractTitle(l.content), subtitle: `${l.items?.length || 0} items` })),
{ title: 'Move to which list?' }
);
if (!target) return;
const confirmed = await DialogService.confirm(`Add "${title}" to "${this._extractTitle(target.content)}"?`, 'Move');
if (!confirmed) return;
await DBManager.moveNoteToList(noteId, target.id);
await this._reloadNotes();
Router.go(`/list/${target.id}`);
}
async _doMergeWithNote(sourceId) {
const otherNotes = this._notes.filter(n => n.id !== sourceId);
if (otherNotes.length === 0) {
alert('No other notes to merge with.');
return;
}
const source = this._notes.find(n => n.id === sourceId);
const sourceTitle = this._extractTitle(source.content);
const target = await DialogService.pickItem(
otherNotes.map(n => ({ id: n.id, title: this._extractTitle(n.content), subtitle: this._formatDate(n.meta?.updatedAt || n.meta?.createdAt) })),
{ title: 'Merge into which note?' }
);
if (!target) return;
const targetTitle = this._extractTitle(target.content);
const confirmed = await DialogService.confirm(`Merge "${sourceTitle}" into "${targetTitle}"?`, 'Merge');
if (!confirmed) return;
await DBManager.mergeNotes(target.id, sourceId);
await this._reloadNotes();
}
async _doArchiveNote(noteId) {
await DBManager.archiveItem(noteId);
await this._reloadNotes();
}
// ------------------------------------------------------------------
// Abstract methods — subclasses must implement
// ------------------------------------------------------------------
_getTemplateId() {
throw new Error('NoteBase subclasses must implement _getTemplateId()');
}
_renderContent() {
throw new Error('NoteBase subclasses must implement _renderContent()');
}
_setupEventListeners() {
throw new Error('NoteBase subclasses must implement _setupEventListeners()');
}
}