feat: add URL state synchronization for navigation

- Added UrlStateService to sync app state with URL parameters for note selection, tags, folders, and search
- Implemented URL state effects in AppComponent to handle navigation from URL parameters
- Updated sidebar and layout components to reflect URL state changes in UI
- Added URL state updates when navigating via note selection, tag clicks, and search
- Modified note sharing to use URL parameters instead of route paths
- Added auto-opening of relevant
This commit is contained in:
2025-10-24 23:23:30 -04:00
parent 0f7cc552ca
commit 96745e9997
28 changed files with 5991 additions and 360 deletions
@@ -0,0 +1,579 @@
/**
* EXEMPLES D'INTÉGRATION DU UrlStateService
*
* Ce fichier contient des exemples complets d'intégration du UrlStateService
* dans les différents composants de l'application.
*
* À adapter selon votre structure de composants.
*/
import { Component, inject, effect, signal, computed } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UrlStateService } from '../services/url-state.service';
import { VaultService } from '../../services/vault.service';
import type { Note } from '../../types';
// ============================================================================
// EXEMPLE 1: NotesListComponent avec synchronisation d'URL
// ============================================================================
/**
* Exemple d'intégration dans NotesListComponent
* Synchronise les filtres (tag, folder, quick link) avec l'URL
*/
@Component({
selector: 'app-notes-list-with-url-state',
standalone: true,
imports: [CommonModule],
template: `
<div class="notes-list-container">
<!-- Afficher le filtre actif depuis l'URL -->
<div *ngIf="urlState.activeTag() as tag" class="filter-badge">
Tag: #{{ tag }}
<button (click)="clearFilter()">✕</button>
</div>
<div *ngIf="urlState.activeFolder() as folder" class="filter-badge">
Folder: {{ folder }}
<button (click)="clearFilter()">✕</button>
</div>
<div *ngIf="urlState.activeQuickLink() as quick" class="filter-badge">
{{ quick }}
<button (click)="clearFilter()">✕</button>
</div>
<!-- Liste des notes -->
<ul class="notes">
<li *ngFor="let note of filteredNotes()"
[class.active]="urlState.isNoteOpen(note.filePath)"
(click)="selectNote(note)">
{{ note.title }}
</li>
</ul>
</div>
`,
styles: [`
.filter-badge {
padding: 8px 12px;
background: var(--primary);
color: white;
border-radius: 4px;
margin-bottom: 8px;
display: flex;
justify-content: space-between;
align-items: center;
}
.notes {
list-style: none;
padding: 0;
margin: 0;
}
.notes li {
padding: 8px;
cursor: pointer;
border-radius: 4px;
transition: background 0.2s;
}
.notes li:hover {
background: var(--surface1);
}
.notes li.active {
background: var(--primary);
color: white;
}
`]
})
export class NotesListWithUrlStateExample {
urlState = inject(UrlStateService);
vault = inject(VaultService);
// Signaux dérivés de l'URL
activeTag = this.urlState.activeTag;
activeFolder = this.urlState.activeFolder;
activeQuickLink = this.urlState.activeQuickLink;
// Notes filtrées basées sur l'état de l'URL
filteredNotes = computed(() => {
const allNotes = this.vault.allNotes();
const tag = this.activeTag();
const folder = this.activeFolder();
return allNotes.filter(note => {
if (tag && !note.tags?.includes(tag)) return false;
if (folder && !note.filePath?.startsWith(folder)) return false;
return true;
});
});
constructor() {
// Écouter les changements d'état
effect(() => {
const state = this.urlState.currentState();
console.log('État actuel:', state);
// Réagir aux changements
if (state.tag) {
console.log('Filtre par tag:', state.tag);
}
if (state.folder) {
console.log('Filtre par dossier:', state.folder);
}
});
}
// Sélectionner une note et mettre à jour l'URL
selectNote(note: Note): void {
this.urlState.openNote(note.filePath);
}
// Effacer le filtre
clearFilter(): void {
this.urlState.resetState();
}
}
// ============================================================================
// EXEMPLE 2: NoteViewComponent avec chargement depuis l'URL
// ============================================================================
/**
* Exemple d'intégration dans NoteViewComponent
* Charge la note depuis l'URL et affiche son contenu
*/
@Component({
selector: 'app-note-view-with-url-state',
standalone: true,
imports: [CommonModule],
template: `
<div class="note-view-container">
<!-- Afficher la note ouverte -->
<div *ngIf="currentNote() as note" class="note-content">
<h1>{{ note.title }}</h1>
<div class="note-meta">
<span>Chemin: {{ note.filePath }}</span>
<span>Tags: {{ note.tags?.join(', ') }}</span>
</div>
<div class="note-body" [innerHTML]="note.content"></div>
</div>
<!-- État de chargement -->
<div *ngIf="!currentNote() && urlState.currentState().note" class="loading">
Chargement de la note...
</div>
<!-- Aucune note sélectionnée -->
<div *ngIf="!currentNote() && !urlState.currentState().note" class="empty">
Sélectionnez une note pour la lire
</div>
</div>
`,
styles: [`
.note-view-container {
padding: 20px;
height: 100%;
overflow-y: auto;
}
.note-content h1 {
margin-top: 0;
}
.note-meta {
display: flex;
gap: 16px;
font-size: 0.875rem;
color: var(--muted);
margin-bottom: 16px;
}
.note-body {
line-height: 1.6;
}
.loading, .empty {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
color: var(--muted);
}
`]
})
export class NoteViewWithUrlStateExample {
urlState = inject(UrlStateService);
vault = inject(VaultService);
// Signal de la note actuelle depuis l'URL
currentNote = this.urlState.currentNote;
constructor() {
// Charger la note quand l'URL change
effect(async () => {
const note = this.currentNote();
if (note) {
// Charger le contenu complet si nécessaire
await this.vault.ensureNoteLoadedByPath(note.filePath);
}
});
}
}
// ============================================================================
// EXEMPLE 3: TagsComponent avec synchronisation d'URL
// ============================================================================
/**
* Exemple d'intégration dans TagsComponent
* Synchronise la sélection de tags avec l'URL
*/
@Component({
selector: 'app-tags-with-url-state',
standalone: true,
imports: [CommonModule],
template: `
<div class="tags-container">
<h3>Tags</h3>
<div class="tags-list">
<button *ngFor="let tag of vault.tags()"
[class.active]="urlState.isTagActive(tag.name)"
(click)="selectTag(tag.name)"
class="tag-button">
#{{ tag.name }} ({{ tag.count }})
</button>
</div>
</div>
`,
styles: [`
.tags-container {
padding: 16px;
}
.tags-list {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.tag-button {
padding: 6px 12px;
border: 1px solid var(--border);
border-radius: 16px;
background: transparent;
cursor: pointer;
transition: all 0.2s;
font-size: 0.875rem;
}
.tag-button:hover {
background: var(--surface1);
}
.tag-button.active {
background: var(--primary);
color: white;
border-color: var(--primary);
}
`]
})
export class TagsWithUrlStateExample {
urlState = inject(UrlStateService);
vault = inject(VaultService);
selectTag(tagName: string): void {
this.urlState.filterByTag(tagName);
}
}
// ============================================================================
// EXEMPLE 4: FoldersSidebarComponent avec synchronisation d'URL
// ============================================================================
/**
* Exemple d'intégration dans FoldersSidebarComponent
* Synchronise la sélection de dossiers avec l'URL
*/
@Component({
selector: 'app-folders-with-url-state',
standalone: true,
imports: [CommonModule],
template: `
<div class="folders-container">
<h3>Dossiers</h3>
<div class="folders-tree">
<button *ngFor="let folder of vault.fileTree()"
[class.active]="urlState.isFolderActive(folder.path)"
(click)="selectFolder(folder.path)"
class="folder-button">
📁 {{ folder.name }}
</button>
</div>
</div>
`,
styles: [`
.folders-container {
padding: 16px;
}
.folders-tree {
display: flex;
flex-direction: column;
gap: 4px;
}
.folder-button {
padding: 8px 12px;
border: none;
background: transparent;
cursor: pointer;
text-align: left;
border-radius: 4px;
transition: background 0.2s;
}
.folder-button:hover {
background: var(--surface1);
}
.folder-button.active {
background: var(--primary);
color: white;
}
`]
})
export class FoldersWithUrlStateExample {
urlState = inject(UrlStateService);
vault = inject(VaultService);
selectFolder(folderPath: string): void {
this.urlState.filterByFolder(folderPath);
}
}
// ============================================================================
// EXEMPLE 5: SearchComponent avec synchronisation d'URL
// ============================================================================
/**
* Exemple d'intégration dans SearchComponent
* Synchronise la recherche avec l'URL
*/
@Component({
selector: 'app-search-with-url-state',
standalone: true,
imports: [CommonModule],
template: `
<div class="search-container">
<input type="text"
[value]="urlState.activeSearch() || ''"
(input)="onSearch($event)"
placeholder="Rechercher..."
class="search-input" />
<!-- Résultats de recherche -->
<div *ngIf="searchResults() as results" class="search-results">
<div *ngIf="results.length === 0" class="no-results">
Aucun résultat
</div>
<ul *ngIf="results.length > 0">
<li *ngFor="let note of results"
(click)="selectNote(note)">
{{ note.title }}
</li>
</ul>
</div>
</div>
`,
styles: [`
.search-container {
padding: 16px;
}
.search-input {
width: 100%;
padding: 8px 12px;
border: 1px solid var(--border);
border-radius: 4px;
font-size: 1rem;
}
.search-results {
margin-top: 12px;
max-height: 300px;
overflow-y: auto;
}
.search-results ul {
list-style: none;
padding: 0;
margin: 0;
}
.search-results li {
padding: 8px;
cursor: pointer;
border-radius: 4px;
transition: background 0.2s;
}
.search-results li:hover {
background: var(--surface1);
}
.no-results {
color: var(--muted);
text-align: center;
padding: 16px;
}
`]
})
export class SearchWithUrlStateExample {
urlState = inject(UrlStateService);
vault = inject(VaultService);
searchResults = computed(() => {
const searchTerm = this.urlState.activeSearch();
if (!searchTerm) return [];
const term = searchTerm.toLowerCase();
return this.vault.allNotes().filter(note =>
note.title.toLowerCase().includes(term) ||
note.content?.toLowerCase().includes(term)
);
});
onSearch(event: Event): void {
const searchTerm = (event.target as HTMLInputElement).value;
this.urlState.updateSearch(searchTerm);
}
selectNote(note: Note): void {
this.urlState.openNote(note.filePath);
}
}
// ============================================================================
// EXEMPLE 6: Partage de lien
// ============================================================================
/**
* Exemple d'intégration pour le partage de lien
* Génère et copie une URL partageble
*/
@Component({
selector: 'app-share-button-with-url-state',
standalone: true,
template: `
<button (click)="shareCurrentState()" class="share-button">
📤 Partager
</button>
`,
styles: [`
.share-button {
padding: 8px 16px;
background: var(--primary);
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.2s;
}
.share-button:hover {
background: var(--primary-dark);
}
`]
})
export class ShareButtonWithUrlStateExample {
urlState = inject(UrlStateService);
async shareCurrentState(): Promise<void> {
try {
await this.urlState.copyCurrentUrlToClipboard();
console.log('Lien copié!');
// Afficher un toast de confirmation
} catch (error) {
console.error('Erreur lors de la copie:', error);
}
}
}
// ============================================================================
// EXEMPLE 7: Historique de navigation
// ============================================================================
/**
* Exemple d'intégration pour l'historique de navigation
* Permet de revenir à l'état précédent
*/
@Component({
selector: 'app-navigation-history-with-url-state',
standalone: true,
template: `
<div class="navigation-controls">
<button (click)="goBack()" [disabled]="!canGoBack()" class="nav-button">
← Retour
</button>
<button (click)="reset()" class="nav-button">
🏠 Accueil
</button>
</div>
`,
styles: [`
.navigation-controls {
display: flex;
gap: 8px;
padding: 8px;
}
.nav-button {
padding: 8px 12px;
border: 1px solid var(--border);
background: transparent;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s;
}
.nav-button:hover:not(:disabled) {
background: var(--surface1);
}
.nav-button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
`]
})
export class NavigationHistoryWithUrlStateExample {
urlState = inject(UrlStateService);
canGoBack = computed(() => {
const previous = this.urlState.previousState();
const current = this.urlState.currentState();
return JSON.stringify(previous) !== JSON.stringify(current);
});
goBack(): void {
const previousState = this.urlState.getPreviousState();
if (previousState.note) {
this.urlState.openNote(previousState.note);
} else if (previousState.tag) {
this.urlState.filterByTag(previousState.tag);
} else if (previousState.folder) {
this.urlState.filterByFolder(previousState.folder);
} else {
this.urlState.resetState();
}
}
reset(): void {
this.urlState.resetState();
}
}