feat: insert chat message to notes

This commit is contained in:
Timothy Jaeryang Baek 2025-07-07 21:22:07 +04:00
parent a3c2018a4a
commit ade4b0d691
6 changed files with 91 additions and 32 deletions

View file

@ -204,6 +204,23 @@
focus(); focus();
}; };
export const insertContent = (content) => {
if (!editor) return;
const { state, view } = editor;
const { schema, tr } = state;
// If content is a string, convert it to a ProseMirror node
const htmlContent = marked.parse(content, {
breaks: true,
gfm: true
});
// insert the HTML content at the current selection
editor.commands.insertContent(htmlContent);
focus();
};
export const replaceVariables = (variables) => { export const replaceVariables = (variables) => {
if (!editor) return; if (!editor) return;
const { state, view } = editor; const { state, view } = editor;

View file

@ -10,6 +10,7 @@
export let className = export let className =
'w-full rounded-lg px-3.5 py-2 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-hidden h-full'; 'w-full rounded-lg px-3.5 py-2 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-hidden h-full';
export let onBlur = () => {};
let textareaElement; let textareaElement;
// Adjust height on mount and after setting the element. // Adjust height on mount and after setting the element.
@ -59,4 +60,5 @@
on:focus={() => { on:focus={() => {
resize(); resize();
}} }}
on:blur={onBlur}
/> />

View file

@ -111,6 +111,8 @@
let enhancing = false; let enhancing = false;
let streaming = false; let streaming = false;
let inputElement = null;
const init = async () => { const init = async () => {
loading = true; loading = true;
const res = await getNoteById(localStorage.token, id).catch((error) => { const res = await getNoteById(localStorage.token, id).catch((error) => {
@ -638,6 +640,10 @@ Provide the enhanced notes in markdown format. Use markdown syntax for headings,
dragged = false; dragged = false;
}; };
const insertHandler = (content) => {
inputElement?.insertContent(content);
};
onMount(async () => { onMount(async () => {
await tick(); await tick();
@ -896,6 +902,7 @@ Provide the enhanced notes in markdown format. Use markdown syntax for headings,
{/if} {/if}
<RichTextInput <RichTextInput
bind:this={inputElement}
className="input-prose-sm px-0.5" className="input-prose-sm px-0.5"
bind:value={note.data.content.json} bind:value={note.data.content.json}
html={note.data?.content?.html} html={note.data?.content?.html}
@ -1035,7 +1042,14 @@ Provide the enhanced notes in markdown format. Use markdown syntax for headings,
</Pane> </Pane>
<NotePanel bind:show={showPanel}> <NotePanel bind:show={showPanel}>
{#if selectedPanel === 'chat'} {#if selectedPanel === 'chat'}
<Chat bind:show={showPanel} bind:selectedModelId bind:messages {files} {note} /> <Chat
bind:show={showPanel}
bind:selectedModelId
bind:messages
{files}
{note}
onInsert={insertHandler}
/>
{:else if selectedPanel === 'settings'} {:else if selectedPanel === 'settings'}
<Settings bind:show={showPanel} bind:selectedModelId /> <Settings bind:show={showPanel} bind:selectedModelId />
{/if} {/if}

View file

@ -30,6 +30,8 @@
export let files = []; export let files = [];
export let messages = []; export let messages = [];
export let onInsert = (content) => {};
let loaded = false; let loaded = false;
let loading = false; let loading = false;
@ -245,7 +247,7 @@
> >
<div class=" h-full w-full flex flex-col"> <div class=" h-full w-full flex flex-col">
<div class="flex-1 p-1"> <div class="flex-1 p-1">
<Messages bind:messages /> <Messages bind:messages {onInsert} />
</div> </div>
</div> </div>
</div> </div>

View file

@ -7,12 +7,15 @@
import Markdown from '$lib/components/chat/Messages/Markdown.svelte'; import Markdown from '$lib/components/chat/Messages/Markdown.svelte';
import Pencil from '$lib/components/icons/Pencil.svelte'; import Pencil from '$lib/components/icons/Pencil.svelte';
import Textarea from '$lib/components/common/Textarea.svelte'; import Textarea from '$lib/components/common/Textarea.svelte';
import DocumentArrowUp from '$lib/components/icons/DocumentArrowUp.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
export let message; export let message;
export let idx; export let idx;
export let onDelete; export let onDelete;
export let onEdit; export let onEdit;
export let onInsert;
let textAreaElement: HTMLTextAreaElement; let textAreaElement: HTMLTextAreaElement;
</script> </script>
@ -24,36 +27,51 @@
</div> </div>
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<button <Tooltip placement="top" content={$i18n.t('Insert')}>
class=" text-transparent group-hover:text-gray-500 dark:hover:text-gray-300 transition" <button
on:click={() => { class=" text-transparent group-hover:text-gray-500 dark:hover:text-gray-300 transition"
onEdit(); on:click={() => {
}} onInsert();
> }}
<Pencil className="size-3.5" strokeWidth="2" />
</button>
<button
class=" text-transparent group-hover:text-gray-500 dark:hover:text-gray-300 transition"
on:click={() => {
onDelete();
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="2"
stroke="currentColor"
class="size-4"
> >
<path <DocumentArrowUp className="size-3.5" strokeWidth="2" />
stroke-linecap="round" </button>
stroke-linejoin="round" </Tooltip>
d="M15 12H9m12 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
/> <Tooltip placement="top" content={$i18n.t('Edit')}>
</svg> <button
</button> class=" text-transparent group-hover:text-gray-500 dark:hover:text-gray-300 transition"
on:click={() => {
onEdit();
}}
>
<Pencil className="size-3.5" strokeWidth="2" />
</button>
</Tooltip>
<Tooltip placement="top" content={$i18n.t('Delete')}>
<button
class=" text-transparent group-hover:text-gray-500 dark:hover:text-gray-300 transition"
on:click={() => {
onDelete();
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="2"
stroke="currentColor"
class="size-4"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M15 12H9m12 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
/>
</svg>
</button>
</Tooltip>
</div> </div>
</div> </div>
@ -67,11 +85,13 @@
</div> </div>
{:else if message?.edit === true} {:else if message?.edit === true}
<Textarea <Textarea
class="w-full bg-transparent outline-hidden rounded-lg text-sm resize-none overflow-hidden"
placeholder={$i18n.t(`Enter {{role}} message here`, { placeholder={$i18n.t(`Enter {{role}} message here`, {
role: message.role === 'user' ? $i18n.t('a user') : $i18n.t('an assistant') role: message.role === 'user' ? $i18n.t('a user') : $i18n.t('an assistant')
})} })}
bind:value={message.content} bind:value={message.content}
onBlur={() => {
message.edit = false;
}}
/> />
{:else} {:else}
<div class=" markdown-prose-sm"> <div class=" markdown-prose-sm">

View file

@ -5,6 +5,7 @@
const i18n = getContext('i18n'); const i18n = getContext('i18n');
export let messages = []; export let messages = [];
export let onInsert = (content: string) => {};
</script> </script>
<div class="space-y-3 pb-12"> <div class="space-y-3 pb-12">
@ -12,6 +13,9 @@
<Message <Message
{message} {message}
{idx} {idx}
onInsert={() => {
onInsert(message?.content ?? '');
}}
onEdit={() => { onEdit={() => {
messages = messages.map((msg, messageIdx) => { messages = messages.map((msg, messageIdx) => {
if (messageIdx === idx) { if (messageIdx === idx) {