feat(journals): implement bulk Markdown import with multiple parsing strategies
- Added 'ae_journals_parsers.ts' with Standard, Personal Log, and Amazon Vine parsers (ported from Python) - Created 'AeCompModalJournalImport' for file selection, preview, and API submission - Integrated Import button into Journals list view
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
import { goto } from '$app/navigation';
|
||||
|
||||
// *** Import other supporting libraries
|
||||
import { BookPlus, FolderPlus, Library, Loader, SquareLibrary, Wrench } from '@lucide/svelte';
|
||||
import { BookPlus, FolderPlus, Library, Loader, SquareLibrary, Wrench, FileUp } from '@lucide/svelte';
|
||||
import { liveQuery } from 'dexie';
|
||||
import { Modal } from 'flowbite-svelte';
|
||||
|
||||
@@ -33,10 +33,19 @@
|
||||
import Modal_journals_cfg from './modal_journals_config.svelte';
|
||||
import Journal_obj_li from './ae_comp__journal_obj_li.svelte';
|
||||
import AeCompJournalEntryQuickAdd from './ae_comp__journal_entry_quick_add.svelte';
|
||||
import AeCompModalJournalImport from './ae_comp__modal_journal_import.svelte';
|
||||
|
||||
// import Element_data_store from '$lib/element_data_store_v2.svelte';
|
||||
|
||||
let ae_acct = data[$slct.account_id];
|
||||
let show_import_modal = $state(false);
|
||||
|
||||
function handle_import_complete() {
|
||||
// Trigger a refresh of the journal list?
|
||||
// Usually liveQuery handles it if data changed in IDB.
|
||||
// But if we just created entries, we might want to refresh the selected journal view if active.
|
||||
// $journals_trig.journal_entry_li = true;
|
||||
}
|
||||
// $journals_slct.journal_obj = ae_acct.slct.journal_obj;
|
||||
// $journals_slct.journal_obj_li = ae_acct.slct.journal_obj_li;
|
||||
|
||||
@@ -204,6 +213,24 @@
|
||||
<span class="hidden md:inline"> New Journal </span>
|
||||
</button>
|
||||
|
||||
<!-- Import Entries button -->
|
||||
<button
|
||||
type="button"
|
||||
class="
|
||||
btn btn-sm
|
||||
preset-tonal-secondary border border-secondary-500
|
||||
hover:preset-filled-secondary-500
|
||||
transition
|
||||
"
|
||||
onclick={() => {
|
||||
show_import_modal = true;
|
||||
}}
|
||||
title="Import entries from Markdown files"
|
||||
>
|
||||
<FileUp class="mx-1" />
|
||||
<span class="hidden md:inline"> Import </span>
|
||||
</button>
|
||||
|
||||
<!-- Show Journals Config button -->
|
||||
<button
|
||||
type="button"
|
||||
@@ -326,5 +353,11 @@
|
||||
<Modal_journals_cfg show={$journals_sess.show__modal__journals_config} />
|
||||
{/if}
|
||||
|
||||
<AeCompModalJournalImport
|
||||
bind:open={show_import_modal}
|
||||
onClose={() => (show_import_modal = false)}
|
||||
onImportComplete={handle_import_complete}
|
||||
/>
|
||||
|
||||
<style lang="postcss">
|
||||
</style>
|
||||
|
||||
195
src/routes/journals/ae_comp__modal_journal_import.svelte
Normal file
195
src/routes/journals/ae_comp__modal_journal_import.svelte
Normal file
@@ -0,0 +1,195 @@
|
||||
<script lang="ts">
|
||||
import { Modal } from 'flowbite-svelte';
|
||||
import { Upload, FileText, AlertCircle, Check, X, RefreshCw } from '@lucide/svelte';
|
||||
import { PARSERS, type AeJournalEntryInput } from '$lib/ae_journals/ae_journals_parsers';
|
||||
import { journals_func } from '$lib/ae_journals/ae_journals_functions';
|
||||
import { ae_api } from '$lib/stores/ae_stores';
|
||||
import { journals_slct } from '$lib/ae_journals/ae_journals_stores';
|
||||
|
||||
interface Props {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
onImportComplete: () => void;
|
||||
}
|
||||
|
||||
let { open = $bindable(false), onClose, onImportComplete }: Props = $props();
|
||||
|
||||
let files: FileList | null = $state(null);
|
||||
let selected_parser: keyof typeof PARSERS = $state('standard');
|
||||
let parsed_entries: AeJournalEntryInput[] = $state([]);
|
||||
let is_parsing = $state(false);
|
||||
let is_importing = $state(false);
|
||||
let import_log: string[] = $state([]);
|
||||
|
||||
// Watch for file selection or parser change to trigger parsing
|
||||
$effect(() => {
|
||||
if (files && files.length > 0) {
|
||||
parse_files();
|
||||
}
|
||||
});
|
||||
|
||||
async function parse_files() {
|
||||
if (!files) return;
|
||||
is_parsing = true;
|
||||
parsed_entries = [];
|
||||
|
||||
const parser = PARSERS[selected_parser];
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
try {
|
||||
const text = await file.text();
|
||||
const entries = await parser(file, text);
|
||||
parsed_entries = [...parsed_entries, ...entries];
|
||||
} catch (err) {
|
||||
console.error(`Error parsing ${file.name}:`, err);
|
||||
}
|
||||
}
|
||||
is_parsing = false;
|
||||
}
|
||||
|
||||
async function handle_import() {
|
||||
if (parsed_entries.length === 0) return;
|
||||
is_importing = true;
|
||||
import_log = [];
|
||||
|
||||
const journal_id = $journals_slct.journal_id;
|
||||
if (!journal_id) {
|
||||
alert("No target journal selected. Please select a journal in the background first.");
|
||||
is_importing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
let success_count = 0;
|
||||
|
||||
for (const entry of parsed_entries) {
|
||||
try {
|
||||
// Construct payload
|
||||
const data_kv = {
|
||||
name: entry.name,
|
||||
content: entry.content,
|
||||
tags: entry.tags.join(', '),
|
||||
type_code: entry.type_code || 'note',
|
||||
created_on: entry.created_on,
|
||||
updated_on: entry.updated_on
|
||||
};
|
||||
|
||||
const res = await journals_func.create_ae_obj__journal_entry({
|
||||
api_cfg: $ae_api,
|
||||
journal_id: journal_id,
|
||||
data_kv: data_kv,
|
||||
log_lvl: 0
|
||||
});
|
||||
|
||||
if (res) {
|
||||
import_log.push(`✅ Imported: ${entry.name}`);
|
||||
success_count++;
|
||||
} else {
|
||||
import_log.push(`❌ Failed: ${entry.name}`);
|
||||
}
|
||||
} catch (err) {
|
||||
import_log.push(`❌ Error: ${entry.name} - ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
is_importing = false;
|
||||
alert(`Import complete! ${success_count}/${parsed_entries.length} imported.`);
|
||||
onImportComplete();
|
||||
open = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<Modal
|
||||
title="Import Journal Entries"
|
||||
bind:open={open}
|
||||
autoclose={false}
|
||||
size="xl"
|
||||
class="w-full"
|
||||
>
|
||||
<div class="space-y-4">
|
||||
<!-- Configuration -->
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label class="label">
|
||||
<span>Parser Strategy</span>
|
||||
<select class="select" bind:value={selected_parser}>
|
||||
<option value="standard">Standard (1 File = 1 Entry)</option>
|
||||
<option value="personal_log">Personal Log (Split by Date)</option>
|
||||
<option value="amazon_vine">Amazon Vine Reviews</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<div>
|
||||
<label class="label">
|
||||
<span>Select Files</span>
|
||||
<input
|
||||
type="file"
|
||||
class="file-input w-full"
|
||||
multiple
|
||||
accept=".md,.txt"
|
||||
onchange={(e) => files = e.currentTarget.files}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Preview -->
|
||||
<div class="border rounded-lg p-2 bg-gray-50 dark:bg-gray-900 max-h-64 overflow-y-auto">
|
||||
<h4 class="font-bold mb-2 flex justify-between">
|
||||
<span>Preview ({parsed_entries.length} entries)</span>
|
||||
{#if is_parsing}
|
||||
<RefreshCw class="animate-spin" />
|
||||
{/if}
|
||||
</h4>
|
||||
|
||||
{#if parsed_entries.length > 0}
|
||||
<table class="table table-compact w-full text-xs">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Date</th>
|
||||
<th>Tags</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#each parsed_entries as entry}
|
||||
<tr>
|
||||
<td class="truncate max-w-[200px]" title={entry.name}>{entry.name}</td>
|
||||
<td>{entry.created_on?.substring(0,10)}</td>
|
||||
<td>{entry.tags.join(', ')}</td>
|
||||
</tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
{:else if files && files.length > 0 && !is_parsing}
|
||||
<div class="text-center text-gray-500 py-4">No entries found in selected files.</div>
|
||||
{:else if !files}
|
||||
<div class="text-center text-gray-500 py-4">Select files to preview import.</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<!-- Import Log -->
|
||||
{#if import_log.length > 0}
|
||||
<div class="bg-black text-green-400 p-2 rounded text-xs font-mono max-h-32 overflow-y-auto">
|
||||
{#each import_log as log}
|
||||
<div>{log}</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="modal-action">
|
||||
<button class="btn preset-tonal-secondary" onclick={onClose}>Cancel</button>
|
||||
<button
|
||||
class="btn preset-filled-primary"
|
||||
disabled={parsed_entries.length === 0 || is_importing}
|
||||
onclick={handle_import}
|
||||
>
|
||||
{#if is_importing}
|
||||
Importing...
|
||||
{:else}
|
||||
Import {parsed_entries.length} Entries
|
||||
{/if}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
Reference in New Issue
Block a user