2024-08-03 22:03:27 -04:00
|
|
|
<script lang="ts">
|
2024-08-04 13:27:05 -04:00
|
|
|
import { isValidUrl } from '$lib';
|
2024-08-04 13:33:49 -04:00
|
|
|
import type { Collection, Note, User } from '$lib/types';
|
2024-08-03 22:03:27 -04:00
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
import { onMount } from 'svelte';
|
2024-11-03 22:55:38 -05:00
|
|
|
import { t } from 'svelte-i18n';
|
2024-12-26 21:56:14 -05:00
|
|
|
import MarkdownEditor from './MarkdownEditor.svelte';
|
2024-08-03 22:03:27 -04:00
|
|
|
let modal: HTMLDialogElement;
|
2024-12-26 21:56:14 -05:00
|
|
|
import { marked } from 'marked'; // Import the markdown parser
|
|
|
|
|
|
|
|
const renderMarkdown = (markdown: string) => {
|
|
|
|
return marked(markdown);
|
|
|
|
};
|
2024-08-03 22:03:27 -04:00
|
|
|
|
2024-08-04 09:56:58 -04:00
|
|
|
export let note: Note | null = null;
|
|
|
|
export let collection: Collection;
|
2024-08-04 13:33:49 -04:00
|
|
|
export let user: User | null = null;
|
2024-08-04 09:56:58 -04:00
|
|
|
|
2024-12-26 21:56:14 -05:00
|
|
|
let constrainDates: boolean = false;
|
|
|
|
|
|
|
|
let isReadOnly =
|
|
|
|
!(note && user?.uuid == note?.user_id) &&
|
|
|
|
!(user && collection && collection.shared_with && collection.shared_with.includes(user.uuid)) &&
|
|
|
|
!!note;
|
|
|
|
|
2024-08-04 13:27:05 -04:00
|
|
|
let warning: string | null = '';
|
|
|
|
|
2024-08-04 12:45:37 -04:00
|
|
|
let newLink: string = '';
|
|
|
|
|
|
|
|
function addLink() {
|
2024-08-04 13:27:05 -04:00
|
|
|
// check to make it a valid URL
|
|
|
|
if (!isValidUrl(newLink)) {
|
2024-11-04 19:25:07 -05:00
|
|
|
warning = $t('notes.invalid_url');
|
2024-08-04 13:27:05 -04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
warning = null;
|
|
|
|
}
|
|
|
|
|
2024-08-04 12:45:37 -04:00
|
|
|
if (newLink.trim().length > 0) {
|
|
|
|
newNote.links = [...newNote.links, newLink];
|
|
|
|
newLink = '';
|
|
|
|
}
|
|
|
|
console.log(newNote.links);
|
|
|
|
}
|
|
|
|
|
2024-08-04 09:56:58 -04:00
|
|
|
let newNote = {
|
|
|
|
name: note?.name || '',
|
|
|
|
content: note?.content || '',
|
2024-08-04 13:27:05 -04:00
|
|
|
date: note?.date || undefined || null,
|
2024-08-04 09:56:58 -04:00
|
|
|
links: note?.links || [],
|
|
|
|
collection: collection.id,
|
|
|
|
is_public: collection.is_public
|
|
|
|
};
|
2024-08-03 22:03:27 -04:00
|
|
|
|
2024-08-04 09:56:58 -04:00
|
|
|
let initialName: string = note?.name || '';
|
2024-08-03 22:03:27 -04:00
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
modal = document.getElementById('my_modal_1') as HTMLDialogElement;
|
|
|
|
if (modal) {
|
|
|
|
modal.showModal();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
dispatch('close');
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleKeydown(event: KeyboardEvent) {
|
|
|
|
if (event.key === 'Escape') {
|
|
|
|
dispatch('close');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-04 09:56:58 -04:00
|
|
|
async function save() {
|
2024-08-04 13:27:05 -04:00
|
|
|
// handles empty date
|
|
|
|
if (newNote.date == '') {
|
|
|
|
newNote.date = null;
|
|
|
|
}
|
|
|
|
|
2024-08-04 09:56:58 -04:00
|
|
|
if (note && note.id) {
|
2024-08-04 12:45:37 -04:00
|
|
|
console.log('newNote', newNote);
|
2024-08-04 09:56:58 -04:00
|
|
|
const res = await fetch(`/api/notes/${note.id}`, {
|
|
|
|
method: 'PATCH',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(newNote)
|
|
|
|
});
|
|
|
|
if (res.ok) {
|
|
|
|
let data = await res.json();
|
|
|
|
if (data) {
|
|
|
|
dispatch('save', data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.error('Failed to save note');
|
|
|
|
}
|
|
|
|
} else {
|
2024-08-04 12:45:37 -04:00
|
|
|
console.log('newNote', newNote);
|
2024-08-04 09:56:58 -04:00
|
|
|
const res = await fetch(`/api/notes/`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(newNote)
|
|
|
|
});
|
|
|
|
if (res.ok) {
|
2024-08-04 12:37:04 -04:00
|
|
|
let data = await res.json();
|
|
|
|
if (data) {
|
|
|
|
dispatch('create', data);
|
|
|
|
}
|
2024-08-04 09:56:58 -04:00
|
|
|
} else {
|
|
|
|
let data = await res.json();
|
2024-11-03 22:55:38 -05:00
|
|
|
console.error($t('notes.failed_to_save'), data);
|
2024-08-04 09:56:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-03 22:03:27 -04:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<dialog id="my_modal_1" class="modal">
|
|
|
|
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
|
2024-12-26 21:56:14 -05:00
|
|
|
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
|
|
|
|
<div class="modal-box w-11/12 max-w-3xl" role="dialog" on:keydown={handleKeydown} tabindex="0">
|
|
|
|
<h3 class="font-bold text-2xl">
|
|
|
|
{#if note?.id && !isReadOnly}
|
|
|
|
<p class="font-semibold text-md mb-2">
|
|
|
|
{$t('notes.editing_note')}
|
|
|
|
{initialName}
|
|
|
|
</p>
|
|
|
|
{:else if !isReadOnly}
|
|
|
|
{$t('notes.note_editor')}
|
|
|
|
{:else}
|
|
|
|
{$t('notes.note_viewer')}
|
|
|
|
{/if}
|
|
|
|
</h3>
|
2024-08-03 22:03:27 -04:00
|
|
|
|
2024-12-26 21:56:14 -05:00
|
|
|
<div class="modal-action items-center">
|
|
|
|
<form method="post" style="width: 100%;" on:submit|preventDefault>
|
|
|
|
<!-- Basic Information Section -->
|
|
|
|
<div class="collapse collapse-plus bg-base-200 mb-4">
|
|
|
|
<input type="checkbox" id="collapse-plus-1" checked />
|
|
|
|
<div class="collapse-title text-lg font-bold">
|
|
|
|
{$t('adventures.basic_information')}
|
|
|
|
</div>
|
|
|
|
<div class="collapse-content">
|
|
|
|
<!-- Name Input -->
|
|
|
|
<div class="form-control mb-2">
|
|
|
|
<label for="name">{$t('adventures.name')}</label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="name"
|
|
|
|
readonly={isReadOnly}
|
|
|
|
class="input input-bordered w-full max-w-xs"
|
|
|
|
bind:value={newNote.name}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Date Input -->
|
|
|
|
<div class="form-control mb-2">
|
|
|
|
<label for="content">{$t('adventures.date')}</label>
|
|
|
|
{#if collection && collection.start_date && collection.end_date && !isReadOnly}<label
|
|
|
|
class="label cursor-pointer flex items-start space-x-2"
|
|
|
|
>
|
|
|
|
<span class="label-text">{$t('adventures.date_constrain')}</span>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
class="toggle toggle-primary"
|
|
|
|
id="constrain_dates"
|
|
|
|
name="constrain_dates"
|
|
|
|
on:change={() => (constrainDates = !constrainDates)}
|
|
|
|
/></label
|
2024-08-04 13:33:49 -04:00
|
|
|
>
|
2024-12-26 21:56:14 -05:00
|
|
|
{/if}
|
|
|
|
<input
|
|
|
|
type="date"
|
|
|
|
id="date"
|
|
|
|
name="date"
|
|
|
|
readonly={isReadOnly}
|
|
|
|
min={constrainDates ? collection.start_date : ''}
|
|
|
|
max={constrainDates ? collection.end_date : ''}
|
|
|
|
bind:value={newNote.date}
|
|
|
|
class="input input-bordered w-full max-w-xs mt-1"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Content Textarea -->
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label for="content">{$t('notes.content')}</label><br />
|
|
|
|
{#if !isReadOnly}
|
|
|
|
<MarkdownEditor bind:text={newNote.content} editor_height={'h-32'} />
|
|
|
|
{:else if note}
|
|
|
|
<p class="text-sm text-muted-foreground" style="white-space: pre-wrap;"></p>
|
|
|
|
<article
|
|
|
|
class="prose overflow-auto h-full max-w-full p-4 border border-base-300 rounded-lg mb-4 mt-4"
|
|
|
|
>
|
|
|
|
{@html renderMarkdown(note.content || '')}
|
|
|
|
</article>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Links Section -->
|
|
|
|
{#if !isReadOnly}
|
|
|
|
<div class="form-control mb-2">
|
|
|
|
<label for="content">{$t('adventures.links')}</label>
|
|
|
|
<input
|
|
|
|
type="url"
|
|
|
|
class="input input-bordered w-full mb-1"
|
|
|
|
placeholder="{$t('notes.add_a_link')} (e.g. https://example.com)"
|
|
|
|
bind:value={newLink}
|
|
|
|
on:keydown={(e) => {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
e.preventDefault();
|
|
|
|
addLink();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<button type="button" class="btn btn-sm btn-primary mt-1" on:click={addLink}>
|
|
|
|
{$t('adventures.add')}
|
2024-08-04 13:33:49 -04:00
|
|
|
</button>
|
2024-12-26 21:56:14 -05:00
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<!-- Links List -->
|
|
|
|
{#if newNote.links.length > 0}
|
|
|
|
<ul class="list-none">
|
|
|
|
{#each newNote.links as link, i}
|
|
|
|
<li class="mb-4 flex justify-between items-center">
|
|
|
|
<a href={link} class="link link-primary" target="_blank">
|
|
|
|
{link}
|
|
|
|
</a>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-sm btn-error"
|
|
|
|
disabled={isReadOnly}
|
|
|
|
on:click={() => {
|
|
|
|
newNote.links = newNote.links.filter((_, index) => index !== i);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{$t('adventures.remove')}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
{/each}
|
|
|
|
</ul>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-08-04 13:33:49 -04:00
|
|
|
|
2024-12-26 21:56:14 -05:00
|
|
|
<!-- Warning Message -->
|
2024-08-04 13:33:49 -04:00
|
|
|
{#if warning}
|
2024-12-26 21:56:14 -05:00
|
|
|
<div role="alert" class="alert alert-error mb-4">
|
2024-08-04 13:33:49 -04:00
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
class="h-6 w-6 shrink-0 stroke-current"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
stroke-linecap="round"
|
|
|
|
stroke-linejoin="round"
|
|
|
|
stroke-width="2"
|
|
|
|
d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
<span>{warning}</span>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
2024-12-26 21:56:14 -05:00
|
|
|
<!-- Public Note Alert -->
|
2024-08-04 13:33:49 -04:00
|
|
|
{#if collection.is_public}
|
2024-12-26 21:56:14 -05:00
|
|
|
<div role="alert" class="alert mb-4">
|
2024-08-04 13:33:49 -04:00
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
class="h-6 w-6 shrink-0 stroke-current"
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
stroke-linecap="round"
|
|
|
|
stroke-linejoin="round"
|
|
|
|
stroke-width="2"
|
|
|
|
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
|
|
|
|
></path>
|
|
|
|
</svg>
|
2024-11-03 22:55:38 -05:00
|
|
|
<span>{$t('notes.note_public')}</span>
|
2024-08-04 13:33:49 -04:00
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
2024-12-26 21:56:14 -05:00
|
|
|
<!-- Action Buttons -->
|
|
|
|
<div class="mt-4">
|
|
|
|
<button class="btn btn-primary mr-1" disabled={isReadOnly} on:click={save}>
|
|
|
|
{$t('notes.save')}
|
2024-12-26 22:09:17 -05:00
|
|
|
</button><button class="btn btn-neutral" on:click={close}>
|
|
|
|
{$t('about.close')}
|
2024-12-26 21:56:14 -05:00
|
|
|
</button>
|
|
|
|
</div>
|
2024-08-04 13:33:49 -04:00
|
|
|
</form>
|
2024-12-26 21:56:14 -05:00
|
|
|
</div>
|
2024-08-03 22:03:27 -04:00
|
|
|
</div>
|
|
|
|
</dialog>
|