From b34885efc639c10cbf06c35cd2d4ff6055e36b45 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 28 Oct 2026 10:02:45 +0900 Subject: [PATCH] refac --- backend/open_webui/models/notes.py | 466 ++++++++- backend/open_webui/retrieval/utils.py | 16 +- backend/open_webui/routers/notes.py | 61 +- backend/open_webui/routers/search.py | 11 +- backend/open_webui/socket/main.py | 68 +- backend/open_webui/tools/builtin.py | 89 +- .../open_webui/utils/access_control/files.py | 6 +- backend/open_webui/utils/middleware.py | 23 - src/lib/apis/notes/index.ts | 35 +- src/lib/components/chat/Chat.svelte | 37 +- .../MarkdownInlineTokens/NoteLinkToken.svelte | 6 +- src/lib/components/common/FileItem.svelte | 2 +- .../common/ResizableSidePanel.svelte | 30 +- .../components/common/RichTextInput.svelte | 43 +- .../common/RichTextInput/Collaboration.ts | 37 +- .../common/RichTextInput/FileAttachment.ts | 95 ++ .../layout/Search/NotePreview.svelte | 17 +- src/lib/components/notes/NoteEditor.svelte | 924 +++++++++--------- src/lib/components/notes/NotePanel.svelte | 20 +- src/lib/components/notes/NoteTabMenu.svelte | 74 ++ src/lib/components/notes/NoteTabs.svelte | 385 ++++++++ src/lib/components/notes/Notes.svelte | 33 +- src/lib/components/notes/utils.ts | 204 +++- 23 files changed, 2059 insertions(+), 623 deletions(-) create mode 100644 src/lib/components/common/RichTextInput/FileAttachment.ts create mode 100644 src/lib/components/notes/NoteTabMenu.svelte create mode 100644 src/lib/components/notes/NoteTabs.svelte diff --git a/backend/open_webui/models/notes.py b/backend/open_webui/models/notes.py index 40ca4ef95e..b73457646e 100644 --- a/backend/open_webui/models/notes.py +++ b/backend/open_webui/models/notes.py @@ -1,17 +1,389 @@ +import json +import re import time import uuid -from functools import lru_cache -from typing import Optional +from copy import deepcopy +from html import escape +from typing import Literal, Optional +import markdown +from bs4 import BeautifulSoup from open_webui.internal.db import Base, get_async_db_context from open_webui.models.access_grants import AccessGrantModel, AccessGrants from open_webui.models.groups import Groups -from open_webui.models.users import User, UserModel, UserResponse, Users +from open_webui.models.users import User, UserModel, UserResponse from open_webui.utils.json_codec import JSONCodec from pydantic import BaseModel, ConfigDict, Field, field_validator -from sqlalchemy import JSON, BigInteger, Boolean, Column, ForeignKey, Text, cast, delete, func, or_, select, update +from sqlalchemy import JSON, BigInteger, Column, ForeignKey, Text, cast, delete, func, or_, select, update from sqlalchemy.ext.asyncio import AsyncSession + +class NotePageConflict(ValueError): + pass + + +class NotePageOperation(BaseModel): + action: Literal['create', 'rename', 'duplicate', 'reorder', 'move', 'delete'] + page_id: str | None = None + new_id: str | None = None + parent_id: str | None = None + before_id: str | None = None + title: str | None = Field(default=None, max_length=200) + order: list[str] | None = None + revision: int = Field(ge=0) + + +def order_pages(pages: list[dict]) -> list[dict]: + """Keep sibling order while storing each subtree together, parent first.""" + ids = {page['id'] for page in pages} + if len(ids) != len(pages): + raise ValueError('Page IDs must be unique') + children: dict[str | None, list[dict]] = {} + for page in pages: + if page['id'] == 'main': + page['parent_id'] = None + else: + page.setdefault('parent_id', None) + parent = page['parent_id'] + if parent is not None and parent not in ids: + raise ValueError('Parent page no longer exists') + children.setdefault(parent, []).append(page) + ordered = [] + roots = children.get(None, []) + roots.sort(key=lambda page: page['id'] != 'main') + stack = list(reversed(roots)) + while stack: + page = stack.pop() + ordered.append(page) + stack.extend(reversed(children.get(page['id'], []))) + if len(ordered) != len(pages): + raise ValueError('A page cannot be nested inside itself or its subpages') + return ordered + + +def subtree_ids(pages: list[dict], page_id: str) -> set[str]: + # Tabs are in preorder, so all ancestors precede their descendants. + result = {page_id} + for page in pages: + if page.get('parent_id') in result: + result.add(page['id']) + return result + + +def page_paths(pages: list[dict]) -> dict[str, str]: + paths = {} + for page in pages: + parent_path = paths.get(page.get('parent_id')) + paths[page['id']] = f'{parent_path} / {page["title"]}' if parent_path else page['title'] + return paths + + +def get_content_files(content: dict | None) -> list[dict]: + """Attachment blocks are authoritative; HTML/Markdown support external note writers.""" + if not isinstance(content, dict): + return [] + document = content.get('json') + files = {} + if isinstance(document, dict): + stack = [document] + while stack: + node = stack.pop() + if not isinstance(node, dict): + continue + if node.get('type') == 'fileAttachment': + attrs = node.get('attrs') or {} + file_id = attrs.get('id') + if isinstance(file_id, str) and re.fullmatch(r'[\w-]+', file_id): + files[file_id] = { + 'type': 'file', + 'id': file_id, + 'name': attrs.get('name') or file_id, + 'size': attrs.get('size', 0), + } + stack.extend(reversed(node.get('content') or [])) + else: + html = content.get('html') or markdown.markdown(content.get('md') or '') + for link in BeautifulSoup(html, 'html.parser').find_all('a', href=True): + match = re.fullmatch(r'/api/v1/files/([\w-]+)/content', link['href']) + if match: + file_id = match[1] + files[file_id] = {'type': 'file', 'id': file_id, 'name': link.get_text() or file_id, 'size': 0} + return list(files.values()) + + +def migrate_page_files(page: dict, files: list[dict]) -> None: + """Consume legacy attachment lists once, preserving existing content and inline images.""" + content = page['content'] + existing = {file['id'] for file in get_content_files(content)} + changed = False + for file in files: + if not isinstance(file, dict) or not file.get('id'): + continue + if file.get('type') == 'image': + reference = f'data://{file["id"]}' + url = file.get('url') or '' + if not url: + continue + # These assets also back images in older versions of the page. + for snapshot in [content, *(page.get('versions') or [])]: + if not isinstance(snapshot, dict): + continue + encoded = json.dumps(snapshot) + if reference in encoded: + snapshot.update(json.loads(encoded.replace(reference, json.dumps(url)[1:-1]))) + changed = True + elif file['id'] not in existing: + attrs = {'id': file['id'], 'name': file.get('name') or file['id'], 'size': file.get('size') or 0} + href = f'/api/v1/files/{file["id"]}/content' + block = f'
{escape(attrs["name"])}
' + content['html'] = (content.get('html') or markdown.markdown(content.get('md') or '')) + block + content['md'] = ( + (content.get('md') or '') + '\n\n' + f'[{attrs["name"].replace("[", "").replace("]", "")}]({href})' + ) + if isinstance(content.get('json'), dict): + content['json'].setdefault('content', []).append({'type': 'fileAttachment', 'attrs': attrs}) + existing.add(file['id']) + changed = True + if changed: + # Old collaboration documents must not overwrite the migrated content. + page['generation'] = page.get('generation', 0) + 1 + + +def get_note_files(data: dict | None) -> list[dict]: + return [file for page in normalize_pages(data)['pages'] for file in get_content_files(page['content'])] + + +def normalize_pages(data: dict | None, note_title: str | None = None) -> dict: + data = deepcopy(data or {}) + if not data.get('pages'): + data['pages'] = [ + { + 'id': 'main', + 'title': note_title or 'Untitled', + 'content': data.get('content') or {'json': None, 'html': '', 'md': ''}, + 'versions': data.get('versions') or [], + 'revision': 0, + 'generation': 0, + } + ] + if not any(page['id'] == 'main' for page in data['pages']): + data['pages'].insert( + 0, + { + 'id': 'main', + 'title': note_title or 'Untitled', + 'content': {'json': None, 'html': '', 'md': ''}, + 'versions': [], + 'revision': 0, + 'generation': 0, + }, + ) + if note_title is not None: + for page in data['pages']: + if page['id'] == 'main' and page.get('inherit_note_title', True): + page['title'] = note_title or 'Untitled' + data['pages'] = order_pages(data['pages']) + legacy_files = data.pop('files', None) or [] + for page in data['pages']: + files = page.pop('files', None) + if files is None: + # Old whole-note files belong to main; subpages only retain referenced images. + references = json.dumps([page.get('content'), page.get('versions')]) + files = ( + legacy_files + if page['id'] == 'main' + else [ + file + for file in legacy_files + if isinstance(file, dict) + and file.get('type') == 'image' + and f'data://{file.get("id")}' in references + ] + ) + if files: + migrate_page_files(page, files) + data.setdefault('pages_revision', 0) + data.pop('versions', None) + # A derived projection keeps existing SQL search and whole-note retrieval cheap. + pages = data['pages'] + paths = page_paths(pages) + data['content'] = ( + deepcopy(pages[0]['content']) + if len(pages) == 1 + else { + 'json': None, + 'md': '\n\n'.join(f'## {paths[p["id"]]}\n\n{p["content"].get("md", "")}' for p in pages), + 'html': '', + } + ) + data['search_text'] = '\n\n'.join(f'{paths[p["id"]]}\n{p["content"].get("md", "")}' for p in pages) + return data + + +def get_page(data: dict, page_id: str | None = None) -> dict: + pages = normalize_pages(data)['pages'] + if page_id is None: + if len(pages) != 1: + raise NotePageConflict('Select a page before editing this note') + return pages[0] + page = next((page for page in pages if page['id'] == page_id), None) + if page is None: + raise NotePageConflict('Page no longer exists') + return page + + +def document_page(data: dict, document_id: str) -> dict: + parts = document_id.split(':') + if len(parts) == 2: + page = get_page(data) + if page.get('generation', 0) != 0: + raise NotePageConflict('Reload this page before editing') + return page + if len(parts) != 5 or parts[2] != 'page': + raise ValueError('Invalid note document ID') + page = get_page(data, parts[3]) + if str(page.get('generation', 0)) != parts[4]: + raise NotePageConflict('Page changed; reload before editing') + return page + + +def apply_page_operation(data: dict, operation: NotePageOperation) -> dict: + data = normalize_pages(data) + pages = data['pages'] + if operation.action in ('create', 'duplicate'): + if not operation.new_id: + raise ValueError('A new page ID is required') + uuid.UUID(operation.new_id) + if any(p['id'] == operation.new_id for p in pages): + return data + if operation.revision != data['pages_revision']: + raise NotePageConflict('Pages changed. Refresh and try again') + if operation.action == 'create': + title = (operation.title or '').strip() + if not title: + raise ValueError('Page title is required') + pages.append( + { + 'id': operation.new_id, + 'parent_id': operation.parent_id, + 'title': title, + 'content': {'json': None, 'html': '', 'md': ''}, + 'versions': [], + 'revision': 0, + 'generation': 0, + } + ) + elif operation.action == 'reorder': + order = operation.order or [] + if len(order) != len(pages) or set(order) != {p['id'] for p in pages}: + raise ValueError('Order must contain each page exactly once') + if order[0] != 'main': + raise ValueError('The default page must stay first') + lookup = {p['id']: p for p in pages} + data['pages'] = [lookup[id] for id in order] + else: + page = next((p for p in pages if p['id'] == operation.page_id), None) + if page is None: + raise NotePageConflict('Page no longer exists') + if page['id'] == 'main' and operation.action in ('move', 'delete'): + raise ValueError('The default page cannot be moved or deleted') + if operation.action == 'rename': + title = (operation.title or '').strip() + if not title: + raise ValueError('Page title is required') + page['title'] = title + if page['id'] == 'main': + page['inherit_note_title'] = False + elif operation.action == 'move': + parent_id = operation.parent_id + if operation.before_id == 'main': + raise ValueError('The default page must stay first') + branch_ids = subtree_ids(pages, page['id']) + if parent_id in branch_ids: + raise ValueError('A page cannot be nested inside itself or its subpages') + if parent_id is not None and not any(p['id'] == parent_id for p in pages): + raise ValueError('Parent page no longer exists') + branch = [p for p in pages if p['id'] in branch_ids] + remaining = [p for p in pages if p['id'] not in branch_ids] + position = len(remaining) + if operation.before_id is not None: + anchor = next((p for p in remaining if p['id'] == operation.before_id), None) + if anchor is None or anchor['parent_id'] != parent_id: + raise ValueError('Move target must be a sibling in the destination') + position = remaining.index(anchor) + page['parent_id'] = parent_id + data['pages'] = remaining[:position] + branch + remaining[position:] + elif operation.action == 'delete': + if len(pages) == 1: + raise ValueError('A note must have at least one page') + # Deleting one tab never silently deletes its children's content. + for child in pages: + if child['parent_id'] == page['id']: + child['parent_id'] = page['parent_id'] + pages.remove(page) + elif operation.action == 'duplicate': + branch_ids = subtree_ids(pages, page['id']) + branch = [p for p in pages if p['id'] in branch_ids] + ids = {p['id']: str(uuid.uuid5(uuid.UUID(operation.new_id), p['id'])) for p in branch} + ids[page['id']] = operation.new_id + if set(ids.values()) & {p['id'] for p in pages}: + raise ValueError('A duplicated page ID already exists') + copies = deepcopy(branch) + for copy in copies: + copy.update( + id=ids[copy['id']], + parent_id=ids.get(copy['parent_id'], copy['parent_id']), + versions=[], + revision=0, + generation=0, + ) + copies[0]['title'] = page['title'][:193] + ' (Copy)' + position = pages.index(branch[-1]) + 1 + pages[position:position] = copies + data['pages_revision'] += 1 + return normalize_pages(data) + + +def update_page_content( + data: dict, page_id: str | None, changes: dict, generation: int | None = None, revision: int | None = None +) -> dict: + data = normalize_pages(data) + target = get_page(data, page_id) + page = next(p for p in data['pages'] if p['id'] == target['id']) + if generation is not None and generation != page.get('generation', 0): + raise NotePageConflict('Page changed; reload before editing') + if revision is not None and revision != page.get('revision', 0): + raise NotePageConflict('Page content changed. Read it again before editing') + if 'content' in changes: + content = changes['content'] + if not isinstance(content, dict) or any( + key in content and not isinstance(content[key], str) for key in ('md', 'html') + ): + raise ValueError('Page content must contain text markdown and HTML') + page['content'] = {'json': None, 'html': '', 'md': '', **deepcopy(content)} + page['revision'] = page.get('revision', 0) + 1 + # Socket snapshots retain their generation. External replacements invalidate old documents. + if generation is None: + page['generation'] = page.get('generation', 0) + 1 + if 'versions' in changes: + page['versions'] = deepcopy(changes['versions']) + if 'files' in changes: + raise ValueError('Attach files through page content') + return normalize_pages(data) + + +def matching_page(data: dict, query: str = '') -> dict: + pages = normalize_pages(data)['pages'] + paths = page_paths(pages) + terms = query.lower().split() + for require_all in (True, False): + for page in pages: + text = f'{paths[page["id"]]} {page["content"].get("md", "")}'.lower() + if terms and (all(t in text for t in terms) if require_all else any(t in text for t in terms)): + return page + return pages[0] + + #################### # Note DB Schema #################### @@ -102,6 +474,8 @@ class NoteForm(BaseModel): @field_validator('data', mode='before') @classmethod def sanitize_data(cls, data): + if isinstance(data, dict) and 'pages' in data: + raise ValueError('Create pages using page operations') return sanitize_note_data(data) @@ -109,6 +483,10 @@ class NoteUpdateForm(BaseModel): title: Optional[str] = None data: Optional[dict] = None meta: Optional[dict] = None + page_id: Optional[str] = None + page_revision: Optional[int] = None + page_generation: Optional[int] = None + page_operation: Optional[NotePageOperation] = None @field_validator('data', mode='before') @classmethod @@ -147,7 +525,7 @@ class NoteTable: ) -> NoteModel: # We exclude access_grants to inject them note_model = NoteModel.model_validate(note) - note_model.data = note_model.data or {} + note_model.data = normalize_pages(note_model.data, note_model.title) note_model.access_grants = ( access_grants if access_grants is not None else await self._get_access_grants(note_model.id, db=db) ) @@ -178,6 +556,7 @@ class NoteTable: } ) + note.data = normalize_pages(note.data, note.title) new_note = Note(**note.model_dump(exclude={'access_grants', 'is_pinned'})) db.add(new_note) @@ -221,7 +600,13 @@ class NoteTable: or_( func.replace(func.replace(Note.title, '-', ''), ' ', '').ilike(f'%{word}%'), func.replace( - func.replace(Note.data['content']['md'].as_string(), '-', ''), + func.replace( + func.coalesce( + Note.data['search_text'].as_string(), Note.data['content']['md'].as_string() + ), + '-', + '', + ), ' ', '', ).ilike(f'%{word}%'), @@ -331,7 +716,7 @@ class NoteTable: async def get_note_by_id(self, id: str, db: Optional[AsyncSession] = None) -> Optional[NoteModel]: async with get_async_db_context(db) as db: - result = await db.execute(select(Note).filter(Note.id == id)) + result = await db.execute(select(Note).filter(Note.id == id).execution_options(populate_existing=True)) note = result.scalars().first() return await self._to_note_model(note, db=db) if note else None @@ -349,39 +734,58 @@ class NoteTable: note_id for note_id, data in result.all() if isinstance(data, dict) - and isinstance(data.get('files'), list) and any( isinstance(item, dict) and item.get('type') == 'file' and item.get('id') == file_id - for item in data['files'] + for item in get_note_files(data) ) ] async def update_note_by_id( self, id: str, form_data: NoteUpdateForm, db: Optional[AsyncSession] = None ) -> Optional[NoteModel]: + changes = form_data.model_dump(exclude_unset=True) + incoming = changes.get('data') or {} + if 'pages' in incoming or 'pages_revision' in incoming: + raise ValueError('Use page operations instead of replacing all pages') async with get_async_db_context(db) as db: - result = await db.execute(select(Note).filter(Note.id == id)) - note = result.scalars().first() - if not note: - return None - - form_data = form_data.model_dump(exclude_unset=True) - note.data = sanitize_note_data(note.data) or {} - - if 'title' in form_data: - note.title = form_data['title'] - if 'data' in form_data: - note.data = {**(note.data or {}), **(form_data['data'] or {})} - if 'meta' in form_data: - note.meta = {**(note.meta or {}), **(form_data['meta'] or {})} - - if not db.is_modified(note): - return await self._to_note_model(note, db=db) - - note.updated_at = int(time.time_ns()) - - await db.commit() - return await self._to_note_model(note, db=db) if note else None + # CAS protects the entire JSON from writers on other pages and other workers. + for _ in range(8): + result = await db.execute(select(Note).where(Note.id == id).execution_options(populate_existing=True)) + note = result.scalars().first() + if not note: + return None + previous_updated_at = note.updated_at + data = normalize_pages(sanitize_note_data(note.data), note.title) + if form_data.page_operation: + data = apply_page_operation(data, form_data.page_operation) + if any(key in incoming for key in ('content', 'versions', 'files')): + data = update_page_content( + data, + form_data.page_id or ('main' if not {'content', 'versions'} & incoming.keys() else None), + incoming, + form_data.page_generation, + form_data.page_revision, + ) + data = normalize_pages( + {**data, **{k: v for k, v in incoming.items() if k not in ('content', 'versions', 'files')}}, + changes.get('title', note.title), + ) + values = {'data': data, 'updated_at': max(time.time_ns(), (previous_updated_at or 0) + 1)} + if 'title' in changes: + values['title'] = changes['title'] + if 'meta' in changes: + values['meta'] = {**(note.meta or {}), **(changes['meta'] or {})} + result = await db.execute( + update(Note) + .where(Note.id == id, Note.updated_at == previous_updated_at) + .values(**values) + .execution_options(synchronize_session=False) + ) + if result.rowcount: + await db.commit() + return await self.get_note_by_id(id, db=db) + await db.rollback() + raise NotePageConflict('Note changed while saving. Please try again') async def toggle_note_pinned_by_id( self, id: str, user_id: str, db: Optional[AsyncSession] = None diff --git a/backend/open_webui/retrieval/utils.py b/backend/open_webui/retrieval/utils.py index 9577b22b53..e98a1c2a30 100644 --- a/backend/open_webui/retrieval/utils.py +++ b/backend/open_webui/retrieval/utils.py @@ -1461,8 +1461,20 @@ async def get_sources_from_items( ): # User has access to the note query_result = { - 'documents': [[note.data.get('content', {}).get('md', '')]], - 'metadatas': [[{'file_id': note.id, 'name': note.title}]], + 'documents': [[page['content'].get('md', '') for page in note.data['pages']]], + 'metadatas': [ + [ + { + 'file_id': note.id, + 'note_id': note.id, + 'page_id': page['id'], + 'parent_id': page.get('parent_id'), + 'name': f'{note.title} / {page["title"]}', + 'source': f'/notes/{note.id}?page={page["id"]}', + } + for page in note.data['pages'] + ] + ], } elif item.get('type') == 'chat': diff --git a/backend/open_webui/routers/notes.py b/backend/open_webui/routers/notes.py index c21b71cdf0..7be91b2e61 100644 --- a/backend/open_webui/routers/notes.py +++ b/backend/open_webui/routers/notes.py @@ -19,12 +19,18 @@ from open_webui.models.notes import ( NoteForm, NoteListResponse, NoteModel, + NotePageConflict, + NotePageOperation, Notes, NoteUpdateForm, NoteUserResponse, + get_content_files, + get_note_files, + matching_page, + normalize_pages, ) from open_webui.models.users import UserModel, UserResponse, Users -from open_webui.socket.main import leave_room_for_users, sio +from open_webui.socket.main import close_changed_note_documents, leave_room_for_users, sio from open_webui.utils.access_control import ( filter_allowed_access_grants, has_permission, @@ -56,11 +62,17 @@ async def check_notes_access(user: UserModel, db: AsyncSession): ) -def _truncate_note_data(data: Optional[dict], max_length: int = 1000) -> Optional[dict]: +def _truncate_note_data(data: Optional[dict], max_length: int = 1000, query: str = '') -> Optional[dict]: if not data: return data - md = (data.get('content') or {}).get('md') or '' - return {'content': {'md': md[:max_length]}} + page = matching_page(data, query) + md = page['content'].get('md') or '' + return { + 'content': {'md': md[:max_length]}, + 'page_count': len(data.get('pages') or [None]), + 'page_id': page['id'], + 'page_title': page['title'], + } async def leave_note_rooms_for_revoked_users( @@ -76,7 +88,10 @@ async def leave_note_rooms_for_revoked_users( users = await Users.get_users_by_user_ids(list(revoked_user_ids), db=db) # Admins retain access to notes regardless of grants. user_ids = [user.id for user in users if user.role != 'admin'] - for room in [f'note:{note.id}', f'doc_note:{note.id}']: + page_rooms = [ + f'doc_note:{note.id}:page:{p["id"]}:{p.get("generation", 0)}' for p in normalize_pages(note.data)['pages'] + ] + for room in [f'note:{note.id}', f'doc_note:{note.id}', *page_rooms]: await leave_room_for_users(room, user_ids) @@ -210,7 +225,7 @@ async def search_notes( pinned_note_ids = await Notes.get_pinned_note_ids(user.id, db=db) for note in result.items: note.is_pinned = note.id in pinned_note_ids - note.data = _truncate_note_data(note.data) + note.data = _truncate_note_data(note.data, query=query or '') return result @@ -237,6 +252,9 @@ async def create_new_note( db=db, ) + if not await has_access_to_note_files(None, get_note_files(form_data.data), user, db=db): + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()) + try: note = await Notes.insert_new_note(user.id, form_data, db=db) await publish_event( @@ -536,13 +554,16 @@ async def update_note_by_id( if ( form_data.data - and 'files' in form_data.data - and not await has_access_to_note_files(note, form_data.data['files'], user, db=db) + and not await has_access_to_note_files(note, get_content_files(form_data.data.get('content')), user, db=db) ): raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()) try: + if form_data.page_generation is not None: + raise ValueError('Page generation is managed by collaboration') + previous = note note = await Notes.update_note_by_id(id, form_data, db=db) + await close_changed_note_documents(previous, note) pinned_note_ids = await Notes.get_pinned_note_ids(user.id, db=db) note.is_pinned = note.id in pinned_note_ids @@ -552,6 +573,8 @@ async def update_note_by_id( event_data['data'] = { key: note.data.get(key) for key in form_data.data.keys() if note.data is not None and key in note.data } + if form_data.page_operation or (form_data.data and {'content', 'files'} & form_data.data.keys()): + event_data['data'] = note.data await sio.emit( 'events:note', @@ -567,11 +590,26 @@ async def update_note_by_id( data={'title': note.title}, ) return note + except NotePageConflict as e: + raise HTTPException(status_code=409, detail=str(e)) + except ValueError as e: + raise HTTPException(status_code=400, detail=str(e)) except Exception as e: log.exception(e) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()) +@router.post('/{id}/pages/update', response_model=NoteModel) +async def update_note_pages( + request: Request, + id: str, + form_data: NotePageOperation, + user=Depends(get_verified_user), + db: AsyncSession = Depends(get_async_session), +): + return await update_note_by_id(request, id, NoteUpdateForm(page_operation=form_data), user, db) + + ############################ # UpdateNoteAccessById ############################ @@ -704,8 +742,13 @@ async def delete_note_by_id( raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()) try: + previous = note + page_rooms = [ + f'doc_note:{id}:page:{p["id"]}:{p.get("generation", 0)}' for p in normalize_pages(note.data)['pages'] + ] note = await Notes.delete_note_by_id(id, db=db) - for room in [f'note:{id}', f'doc_note:{id}']: + await close_changed_note_documents(previous, None) + for room in [f'note:{id}', f'doc_note:{id}', *page_rooms]: await sio.close_room(room) await publish_event( diff --git a/backend/open_webui/routers/search.py b/backend/open_webui/routers/search.py index 1091d7581f..c5850bdd05 100644 --- a/backend/open_webui/routers/search.py +++ b/backend/open_webui/routers/search.py @@ -12,7 +12,7 @@ from open_webui.models.config import Config from open_webui.models.folders import Folder, Folders from open_webui.models.groups import Groups from open_webui.models.messages import Message -from open_webui.models.notes import Note, PinnedNote +from open_webui.models.notes import Note, PinnedNote, matching_page from open_webui.models.shared_chats import SharedChat from open_webui.models.users import User from open_webui.utils.access_control import has_permission @@ -338,7 +338,9 @@ async def search_workspace( # noqa: C901 — keep permission scoping and source db, select(Note, pinned, note_shared), Note, {'user_id': user.id, 'group_ids': groups}, 'note', 'read' ) stmt = stmt.where(sharing_filter(Note.user_id)) - stmt = apply_match(stmt, Note, Note.data['content']['md'].as_string()) + stmt = apply_match( + stmt, Note, func.coalesce(Note.data['search_text'].as_string(), Note.data['content']['md'].as_string()) + ) rank = title_rank(Note.title, query) if query else case((pinned, 0), else_=3) sources['note'] = (Note, stmt, rank, cast(Note.updated_at.op('/')(literal(1_000_000_000)), BigInteger)) @@ -421,14 +423,15 @@ async def search_workspace( # noqa: C901 — keep permission scoping and source match.rank, ) elif match.source == 'note': - content = ((item.data or {}).get('content') or {}).get('md') or '' + page = matching_page(item.data, query) + content = page['content'].get('md', '') append_result( 'note:' + item.id, 'note', item.title, 'Shared note' if item.user_id != user.id or row[2] else 'Personal note', content if isinstance(content, str) else '', - '/notes/' + item.id + ('?' + urlencode({'search': query}) if query else ''), + '/notes/' + item.id + '?' + urlencode({'page': page['id'], 'search': query}), match.updated_at, item.user_id != user.id or row[2], row[1], diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index 2d235175cf..99931b4e06 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -40,7 +40,7 @@ from open_webui.models.channels import Channels from open_webui.models.chats import Chats from open_webui.models.config import Config from open_webui.models.folders import Folders -from open_webui.models.notes import Notes, NoteUpdateForm +from open_webui.models.notes import Notes, NoteUpdateForm, document_page, get_content_files, get_page, normalize_pages from open_webui.models.users import UserNameResponse, Users from open_webui.socket.encryption import Relay from open_webui.socket.redis_room_channels import AsyncRedisRoomChannelManager @@ -695,7 +695,7 @@ async def join_channel(sid, data): await sio.enter_room(sid, f'channel:{channel.id}') -@sio.on('join-note') +@sio.on('note:join') async def join_note(sid, data): auth = data['auth'] if 'auth' in data else None if not auth or 'token' not in auth: @@ -736,6 +736,13 @@ async def join_note(sid, data): await sio.enter_room(sid, f'note:{note.id}') +@sio.on('note:leave') +async def leave_note(sid, data): + note_id = data.get('note_id') + if isinstance(note_id, str): + await sio.leave_room(sid, f'note:{note_id}') + + @sio.on('events:channel') async def channel_events(sid, data): room = f'channel:{data["channel_id"]}' @@ -886,6 +893,23 @@ def normalize_document_id(document_id: str) -> str: return document_id +async def close_changed_note_documents(previous, current): + remaining = {p['id']: p.get('generation', 0) for p in normalize_pages(current.data)['pages']} if current else {} + for page in normalize_pages(previous.data)['pages']: + if remaining.get(page['id']) == page.get('generation', 0): + continue + ids = [f'note:{previous.id}:page:{page["id"]}:{page.get("generation", 0)}'] + if len(previous.data['pages']) == 1: + ids.append(f'note:{previous.id}') + for document_id in ids: + async with YDOC_MANAGER.lock(document_id): + await stop_item_tasks(REDIS, document_id) + await sio.close_room(f'doc_{document_id}') + for sid in list(await YDOC_MANAGER.get_users(document_id)): + await YDOC_MANAGER.remove_user(document_id, sid) + await YDOC_MANAGER.clear_document(document_id) + + def with_document_lock(handler): @wraps(handler) async def wrapped(sid, data): @@ -920,6 +944,7 @@ async def ydoc_document_join(sid, data): if not note: log.error(f'Note {note_id} not found') return + page = document_page(note.data, document_id) if ( user.get('role') != 'admin' @@ -964,7 +989,7 @@ async def ydoc_document_join(sid, data): { 'document_id': document_id, 'state': list(state_update), # Convert bytes to list for JSON - 'content': note.data.get('content') if document_id.startswith('note:') and note.data else None, + 'content': page['content'] if document_id.startswith('note:') else None, }, room=sid, ) @@ -1012,7 +1037,31 @@ async def document_save_handler(document_id, data, user): log.error(f'User {user.get("id")} does not have write access to note {note_id}') return - return await Notes.update_note_by_id(note_id, NoteUpdateForm(data=data)) + page = document_page(note.data, document_id) + if not isinstance(data.get('content'), dict): + return None + editor = await Users.get_user_by_id(user['id']) + if not editor or not await has_access_to_note_files(note, get_content_files(data['content']), editor): + raise ValueError('Cannot attach files you cannot access') + saved = await Notes.update_note_by_id( + note_id, + NoteUpdateForm( + data={'content': data['content']}, + page_id=page['id'], + page_generation=page.get('generation', 0), + ), + ) + if saved: + await sio.emit( + 'events:note', + { + 'id': note_id, + 'page': get_page(saved.data, page['id']), + 'updated_at': saved.updated_at, + }, + to=f'note:{note_id}', + ) + return saved @sio.on('ydoc:document:state') @@ -1084,6 +1133,7 @@ async def yjs_document_update(sid, data): if not note: log.error(f'Note {note_id} not found') return + page = document_page(note.data, document_id) if ( user.get('role') != 'admin' @@ -1099,9 +1149,9 @@ async def yjs_document_update(sid, data): return snapshot = data.get('data') - if isinstance(snapshot, dict) and 'files' in snapshot: + if isinstance(snapshot, dict) and 'content' in snapshot: editor = await Users.get_user_by_id(user['id']) - if not editor or not await has_access_to_note_files(note, snapshot['files'], editor): + if not editor or not await has_access_to_note_files(note, get_content_files(snapshot['content']), editor): await sio.emit('error', {'message': 'Cannot attach files you cannot access'}, room=sid) return @@ -1143,6 +1193,11 @@ async def yjs_document_update(sid, data): if data.get('initialize'): return update if stored else None + if data.get('flush'): + await stop_item_tasks(REDIS, document_id) + saved = await document_save_handler(document_id, data.get('data', {}), user) + return {'ok': bool(saved)} + async def debounced_save(): await asyncio.sleep(0.5) async with YDOC_MANAGER.lock(document_id): @@ -1167,6 +1222,7 @@ async def yjs_document_update(sid, data): except Exception as e: log.error(f'Error in yjs_document_update: {e}') + return {'error': str(e)} @sio.on('ydoc:document:leave') diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index 72dd8422a7..c2d18366a4 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -26,8 +26,8 @@ from open_webui.models.config import Config from open_webui.models.groups import Groups from open_webui.models.memories import Memories from open_webui.models.messages import Message, Messages -from open_webui.models.notes import Notes -from open_webui.models.users import UserModel +from open_webui.models.notes import NotePageConflict, Notes, get_content_files, get_page, matching_page +from open_webui.models.users import UserModel, Users from open_webui.retrieval.utils import filter_source_metadata, get_content_from_url from open_webui.retrieval.vector.async_client import ASYNC_VECTOR_DB_CLIENT from open_webui.routers.images import ( @@ -61,9 +61,9 @@ from open_webui.routers.memories import ( update_memories as _update_memories, ) from open_webui.routers.retrieval import search_web as _search_web -from open_webui.socket.main import sio -from open_webui.tasks import stop_item_tasks +from open_webui.socket.main import close_changed_note_documents, sio from open_webui.tools.knowledge_fs import kb_exec # noqa: F401 — re-exported +from open_webui.utils.access_control.files import has_access_to_note_files from open_webui.utils.chat_id import is_saved_chat_id from open_webui.utils.json_codec import JSONCodec from open_webui.utils.notifications import notify_target @@ -1202,10 +1202,11 @@ async def search_notes( if end_ts and note.updated_at > end_ts: continue - # Extract a snippet from the markdown content + # Extract a snippet from the matching page. + page = matching_page(note.data, query) content_snippet = '' - if note.data and note.data.get('content', {}).get('md'): - md_content = note.data['content']['md'] + if page['content'].get('md'): + md_content = page['content']['md'] content_lower = md_content.lower() # Find the first matching word to center the snippet around. @@ -1235,6 +1236,9 @@ async def search_notes( 'id': note.id, 'title': note.title, 'snippet': content_snippet, + 'page_id': page['id'], + 'page_title': page['title'], + 'url': f'/notes/{note.id}?page={page["id"]}', 'updated_at': note.updated_at, } ) @@ -1250,6 +1254,7 @@ async def search_notes( async def view_note( note_id: str, + page_id: Optional[str] = None, __request__: Request = None, __user__: dict = None, ) -> str: @@ -1257,6 +1262,7 @@ async def view_note( Get the full content of a note by its ID. :param note_id: The ID of the note to retrieve + :param page_id: Page to read; omit to inspect the complete note and its page directory :return: JSON with the note's id, title, and full markdown content """ if __request__ is None: @@ -1290,16 +1296,24 @@ async def view_note( ): return JSONCodec.dumps({'error': 'Access denied'}) - # Extract markdown content - content = '' - if note.data and note.data.get('content', {}).get('md'): - content = note.data['content']['md'] + page = get_page(note.data, page_id) if page_id or len(note.data['pages']) == 1 else None + content = page['content'].get('md', '') if page else note.data['content'].get('md', '') + # Keep the version read by this tool run out of the model-facing interface. + if not hasattr(__request__.state, 'note_page_revisions'): + __request__.state.note_page_revisions = {} + for read_page in [page] if page else note.data['pages']: + __request__.state.note_page_revisions[(user_id, note.id, read_page['id'])] = read_page.get('revision', 0) return JSONCodec.dumps( { 'id': note.id, 'title': note.title, 'content': content, + 'page_id': page['id'] if page else None, + 'page_title': page['title'] if page else None, + 'pages': [ + {'id': p['id'], 'title': p['title'], 'parent_id': p.get('parent_id')} for p in note.data['pages'] + ], 'updated_at': note.updated_at, 'created_at': note.created_at, }, @@ -1340,6 +1354,9 @@ async def write_note( access_grants=[], # Private by default - only owner can access ) + editor = await Users.get_user_by_id(user_id) + if not editor or not await has_access_to_note_files(None, get_content_files(form.data['content']), editor): + return JSONCodec.dumps({'error': 'Cannot attach files you cannot access'}) new_note = await Notes.insert_new_note(user_id, form) if not new_note: @@ -1364,12 +1381,14 @@ async def replace_note_content( content: Optional[str] = None, operations: Optional[list[dict]] = None, title: Optional[str] = None, + page_id: Optional[str] = None, __request__: Request = None, __user__: dict = None, ) -> str: """ Update an existing note by replacing the whole markdown content, exact text, or character ranges. + Read the note with view_note before editing. If it changes meanwhile, read it again before retrying. Prefer "replace_text" when only part of the note changes. Supply either find or start/end, not both. find is non-empty, literal, and case-sensitive. Text matches are non-overlapping. Multiple matches require a 0-indexed occurrence or replace_all=true; @@ -1389,6 +1408,7 @@ async def replace_note_content( - {"action": "replace_text", "start": 0, "end": 10, "content": "...", "expected": "..."} - {"action": "replace_text", "find": "old text", "content": "new text", "occurrence": 0, "replace_all": false} :param title: Optional new title for the note + :param page_id: Page to edit; required for notes with multiple pages. Use the active page_id supplied in the request unless the user selects another page :return: JSON with success status and updated note info """ if __request__ is None: @@ -1409,7 +1429,16 @@ async def replace_note_content( if __user__.get('role') != 'admin' and not await _has_write_access_to_note(note, user_id): return JSONCodec.dumps({'error': 'Write access denied', 'code': 'write_access_denied'}) - current_content = ((note.data or {}).get('content') or {}).get('md') or '' + page = get_page(note.data, page_id) + page_id = page['id'] + read_revisions = getattr(__request__.state, 'note_page_revisions', {}) + read_key = (user_id, note.id, page_id) + if read_key not in read_revisions: + return JSONCodec.dumps({'error': 'Read this page with view_note before editing', 'code': 'read_required'}) + read_revision = read_revisions[read_key] + if read_revision != page.get('revision', 0): + raise NotePageConflict('Page changed since you read it. Use view_note again before editing') + current_content = page['content'].get('md') or '' applied_operation_count = 0 range_operations = [] if operations is not None: @@ -1557,37 +1586,38 @@ async def replace_note_content( elif content is None: return JSONCodec.dumps({'error': 'content or operations is required', 'code': 'content_required'}) - try: - await stop_item_tasks(__request__.app.state.redis, f'note:{note_id}') - except Exception: - pass - update_data = { + 'page_id': page_id, + 'page_revision': read_revision, 'data': { - **(note.data or {}), 'content': { - **((note.data or {}).get('content') or {}), 'json': None, 'html': '', 'md': content, }, - } + }, } if title: update_data['title'] = title + editor = await Users.get_user_by_id(__user__['id']) + if not editor or not await has_access_to_note_files(note, get_content_files(update_data['data']['content']), editor): + return JSONCodec.dumps({'error': 'Cannot attach files you cannot access'}) form = NoteUpdateForm(**update_data) updated_note = await Notes.update_note_by_id(note_id, form) if not updated_note: return JSONCodec.dumps({'error': 'Failed to update note', 'code': 'update_failed'}) + read_revisions[read_key] = get_page(updated_note.data, page_id).get('revision', 0) + await close_changed_note_documents(note, updated_note) await _emit_note_updated(__request__, __user__, updated_note) return JSONCodec.dumps( { 'status': 'success', 'id': updated_note.id, + 'page_id': page_id, 'title': updated_note.title, 'updated_at': updated_note.updated_at, 'applied_operation_count': applied_operation_count, @@ -1595,6 +1625,8 @@ async def replace_note_content( }, ensure_ascii=False, ) + except NotePageConflict as e: + return JSONCodec.dumps({'error': str(e), 'code': 'page_conflict'}) except Exception as e: log.exception(f'replace_note_content error: {e}') return JSONCodec.dumps({'error': str(e), 'code': 'unexpected_error'}) @@ -3624,6 +3656,7 @@ async def view_skill( return JSONCodec.dumps(skill, ensure_ascii=False) from types import SimpleNamespace + from open_webui.models.skills import get_skill_snapshot from open_webui.routers.skills import authorized_skill from open_webui.utils.skill_files import file_summaries @@ -3671,6 +3704,7 @@ async def read_skill_file( try: from types import SimpleNamespace from urllib.parse import urlencode + from open_webui.models.skills import get_skill_snapshot from open_webui.routers.skills import authorized_skill from open_webui.utils.skill_files import file_summaries @@ -3777,6 +3811,7 @@ async def grep_skill_files( files = [{'path': 'SKILL.md', 'content': skill['content']}] else: from types import SimpleNamespace + from open_webui.models.skills import get_skill_snapshot from open_webui.routers.skills import authorized_skill @@ -3827,10 +3862,11 @@ async def create_skill( """ try: from types import SimpleNamespace - from open_webui.routers.skills import create_new_skill + from open_webui.models.skills import SkillForm - from open_webui.utils.skill_files import frontmatter + from open_webui.routers.skills import create_new_skill from open_webui.utils.access_control import has_permission + from open_webui.utils.skill_files import frontmatter if __request__ is None or not __user__: raise ValueError('Request and user context required') @@ -3877,9 +3913,10 @@ async def update_skill_files( """ try: from types import SimpleNamespace - from open_webui.routers.skills import authorized_skill - from open_webui.models.skills import Skills + from open_webui.events import EVENTS, publish_event + from open_webui.models.skills import Skills + from open_webui.routers.skills import authorized_skill if __request__ is None or not __user__: raise ValueError('Request and user context required') @@ -4147,7 +4184,7 @@ async def create_automation( return JSONCodec.dumps({'error': 'User context not available'}) try: - from open_webui.models.automations import AutomationData, AutomationForm, AutomationTarget, Automations + from open_webui.models.automations import AutomationData, AutomationForm, Automations, AutomationTarget from open_webui.models.users import Users from open_webui.routers.automations import check_automation_limits from open_webui.utils.automations import next_n_runs_ns, next_run_ns, validate_rrule @@ -4246,7 +4283,7 @@ async def update_automation( return JSONCodec.dumps({'error': 'User context not available'}) try: - from open_webui.models.automations import AutomationData, AutomationForm, AutomationTarget, Automations + from open_webui.models.automations import AutomationData, AutomationForm, Automations, AutomationTarget from open_webui.models.users import Users from open_webui.routers.automations import check_automation_limits from open_webui.utils.automations import next_n_runs_ns, next_run_ns, validate_rrule diff --git a/backend/open_webui/utils/access_control/files.py b/backend/open_webui/utils/access_control/files.py index c8a10910da..4052bd0798 100644 --- a/backend/open_webui/utils/access_control/files.py +++ b/backend/open_webui/utils/access_control/files.py @@ -8,7 +8,7 @@ from open_webui.models.folders import FolderModel from open_webui.models.groups import Groups from open_webui.models.knowledge import Knowledges from open_webui.models.models import Models -from open_webui.models.notes import NoteModel, Notes +from open_webui.models.notes import NoteModel, Notes, get_note_files from open_webui.models.users import UserModel, Users from sqlalchemy.ext.asyncio import AsyncSession @@ -28,14 +28,14 @@ def get_note_file_ids(entries: list[dict] | None) -> set[str]: async def has_access_to_note_files( - note: NoteModel, entries: list[dict] | None, user: UserModel, db: AsyncSession | None = None + note: NoteModel | None, entries: list[dict] | None, user: UserModel, db: AsyncSession | None = None ) -> bool: """Check the editor's read access to newly attached files.""" if entries is not None and not isinstance(entries, list): return False # Existing attachments may belong to another editor; only additions need permission. - added_ids = get_note_file_ids(entries) - get_note_file_ids((note.data or {}).get('files')) + added_ids = get_note_file_ids(entries) - get_note_file_ids(get_note_files(note.data) if note else []) if not added_ids or user.role == 'admin': return True diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 8a4e2ba902..934a078f2c 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -52,7 +52,6 @@ from open_webui.models.chats import Chats from open_webui.models.config import Config from open_webui.models.folders import Folders from open_webui.models.models import Models -from open_webui.models.notes import Notes from open_webui.models.users import UserModel, Users from open_webui.retrieval.utils import filter_source_metadata, get_sources_from_items from open_webui.routers.images import ( @@ -2486,28 +2485,6 @@ async def process_chat_payload(request, form_data, user, metadata, model): activated_skill_ids = mentioned_skill_ids available_skills = [] terminal_skills = [] - if is_note_chat: - note_id = (chat.meta or {}).get('note_id') - note = await Notes.get_note_by_id(note_id) if note_id else None - if note and ( - user.role == 'admin' - or note.user_id == user.id - or await AccessGrants.has_access( - user_id=user.id, - resource_type='note', - resource_id=note.id, - permission='read', - ) - ): - note_files = [ - file - for file in ((note.data or {}).get('files') or []) - if isinstance(file, dict) - and file.get('type') != 'image' - and not (file.get('content_type') or '').startswith('image/') - ] - if note_files: - files = [*(files or []), *note_files] model_builtin_tools = model.get('info', {}).get('meta', {}).get('builtinTools') or {} diff --git a/src/lib/apis/notes/index.ts b/src/lib/apis/notes/index.ts index 71626953f3..d8b00d7184 100644 --- a/src/lib/apis/notes/index.ts +++ b/src/lib/apis/notes/index.ts @@ -8,6 +8,39 @@ type NoteItem = { access_grants?: object[]; }; +export type NoteContent = { json: any; html: string; md: string }; +export type NotePage = { + id: string; + parent_id?: string | null; + inherit_note_title?: boolean; + title: string; + content: NoteContent; + versions: NoteContent[]; + revision: number; + generation: number; +}; +export type NotePageOperation = { + action: 'create' | 'rename' | 'duplicate' | 'reorder' | 'move' | 'delete'; + revision: number; + page_id?: string; + new_id?: string; + parent_id?: string | null; + before_id?: string | null; + title?: string; + order?: string[]; +}; + +export const updateNotePages = async (token: string, id: string, operation: NotePageOperation) => { + const response = await fetch(`${WEBUI_API_BASE_URL}/notes/${id}/pages/update`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', authorization: `Bearer ${token}` }, + body: JSON.stringify(operation) + }); + const result = await response.json(); + if (!response.ok) throw new Error(result.detail || 'Failed to update pages'); + return result; +}; + export const createNewNote = async (token: string, note: NoteItem) => { let error = null; @@ -318,7 +351,7 @@ export const createNoteChatById = async (token: string, id: string) => { export const updateNoteById = async ( token: string, id: string, - note: Omit + note: Omit & { page_id?: string } ) => { let error = null; diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index a6c460d289..a1608ff27a 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -151,6 +151,9 @@ export let embeddedDraftKey = ''; export let suggestedPrompts = []; export let selectedText = ''; + export let notePageId = ''; + export let noteFiles = []; + export let onBeforeNoteSubmit: (() => Promise) | null = null; export let onInsertToNote: ((content: string) => void) | null = null; export let onCloseEmbedded: (() => void) | null = null; export let onNewEmbeddedChat: (() => void | Promise) | null = null; @@ -856,10 +859,16 @@ } }; - const withSelectedText = (text: string) => - embedded && selectedText?.trim() - ? `${text}\n\nSelected note text for replace_note_content operations:\n${selectedText.trim()}` - : text; + const withSelectedText = (text: string) => { + if (!embedded) return text; + return ( + text + + (notePageId ? `\n\nActive note page_id: ${notePageId}` : '') + + (selectedText?.trim() + ? `\n\nSelected note text for replace_note_content operations:\n${selectedText.trim()}` + : '') + ); + }; const noteChatDebug = (message: string, data: Record = {}) => { if (!embedded) return; console.info('[note-chat]', message, { @@ -3136,7 +3145,11 @@ entry.files.some((file: any) => file.type === 'filesystem') ); const queuedMessages = - boundary === 0 ? queue.slice(0, 1) : boundary < 0 ? [...queue] : queue.slice(0, boundary); + embedded || boundary === 0 + ? queue.slice(0, 1) + : boundary < 0 + ? [...queue] + : queue.slice(0, boundary); const queuedMessageIds = new Set(queuedMessages.map((m) => m.id)); try { const combinedPrompt = queuedMessages.map((m) => m.prompt).join('\n\n'); @@ -3840,6 +3853,14 @@ bridge: bridgeRequest = null }: { _raw?: boolean; bridge?: { userMessageId: string; modelId: string } | null } = {} ): Promise => { + if (embedded && onBeforeNoteSubmit) { + try { + await onBeforeNoteSubmit(); + } catch (error) { + toast.error(String(error)); + return { status: 'rejected' }; + } + } if (processing === 'uploading') return { status: 'deferred' }; emitTyping(false); if (bridgeRequest && (selectedModelIds.length !== 1 || !bridgeRequest.modelId)) @@ -4390,7 +4411,11 @@ stop: getStopTokens() }, - files: (files?.length ?? 0) > 0 ? files : undefined, + files: embedded + ? [...(files ?? []), ...noteFiles] + : (files?.length ?? 0) > 0 + ? files + : undefined, filter_ids: $config?.features?.enable_functions && selectedFilterIds.length > 0 diff --git a/src/lib/components/chat/Messages/Markdown/MarkdownInlineTokens/NoteLinkToken.svelte b/src/lib/components/chat/Messages/Markdown/MarkdownInlineTokens/NoteLinkToken.svelte index df40d9a693..7e49a5f030 100644 --- a/src/lib/components/chat/Messages/Markdown/MarkdownInlineTokens/NoteLinkToken.svelte +++ b/src/lib/components/chat/Messages/Markdown/MarkdownInlineTokens/NoteLinkToken.svelte @@ -19,7 +19,9 @@ try { const note = await getNoteById(localStorage.token, noteId); if (note) { - title = note.title || ''; + const pageId = new URL(href, window.location.origin).searchParams.get('page'); + const page = note.data?.pages?.find((p) => p.id === pageId); + title = page ? `${note.title} / ${page.title}` : note.title || ''; if (note.user_id) { try { @@ -48,7 +50,7 @@ on:click|preventDefault|stopPropagation={() => { try { const url = new URL(href, window.location.origin); - goto(url.pathname); + goto(url.pathname + url.search + url.hash); } catch { // fallback } diff --git a/src/lib/components/common/FileItem.svelte b/src/lib/components/common/FileItem.svelte index 1fbe877c4a..9900bdf68e 100644 --- a/src/lib/components/common/FileItem.svelte +++ b/src/lib/components/common/FileItem.svelte @@ -23,7 +23,7 @@ export let modal = false; export let loading = false; - export let item = null; + export let item: any = null; export let edit = false; export let small = false; diff --git a/src/lib/components/common/ResizableSidePanel.svelte b/src/lib/components/common/ResizableSidePanel.svelte index 04948787e3..ccf2073360 100644 --- a/src/lib/components/common/ResizableSidePanel.svelte +++ b/src/lib/components/common/ResizableSidePanel.svelte @@ -11,6 +11,7 @@ export let minSiblingWidth = 0; export let closeOnDragBelowMinWidth = false; export let storageKey = ''; + export let subtleDivider = false; export let className = ''; export let resizerId = 'controls-resizer'; export let onClose: () => void = () => {}; @@ -104,8 +105,9 @@ onMount(() => { if (!storageKey) return; - const storedWidth = Number(localStorage.getItem(storageKey)); - if (!Number.isNaN(storedWidth)) { + const stored = localStorage.getItem(storageKey); + const storedWidth = Number(stored); + if (stored !== null && Number.isFinite(storedWidth) && storedWidth > 0) { width = clamp(storedWidth); } }); @@ -141,7 +143,9 @@ {#if side === 'right'}
+ {#if subtleDivider} + + {/if}
+ {#if subtleDivider} + + {/if} `${content}` }); - import { onMount, onDestroy, tick, getContext } from 'svelte'; + import { onMount, onDestroy, tick, getContext, getAllContexts } from 'svelte'; import { createEventDispatcher } from 'svelte'; const i18n = getContext('i18n'); + const componentContext = getAllContexts(); const eventDispatch = createEventDispatcher(); import { Fragment, DOMParser, Slice, type Node, type Schema } from 'prosemirror-model'; @@ -150,13 +151,40 @@ // Bubble and Floating menus are currently fixed to v2 due to styling issues in v3 // TODO: Update to v3 when styling issues are resolved import BubbleMenu from '@tiptap/extension-bubble-menu'; - import FloatingMenu from '@tiptap/extension-floating-menu'; + import FloatingMenu, { FloatingMenuView } from '@tiptap/extension-floating-menu'; + + class MountedFloatingMenuView extends FloatingMenuView { + updatePosition() { + // Tiptap leaves a pending resize timer alive after destroying a menu. + // Switching note tabs can destroy its view before that timer runs. + if (this.editor.isDestroyed || this.view.isDestroyed) return; + super.updatePosition(); + } + } + + const MountedFloatingMenu = FloatingMenu.extend({ + addProseMirrorPlugins() { + const element = this.options.element; + if (!element) return []; + return [ + new Plugin({ + key: + typeof this.options.pluginKey === 'string' + ? new PluginKey(this.options.pluginKey) + : this.options.pluginKey, + view: (view) => + new MountedFloatingMenuView({ ...this.options, element, editor: this.editor, view }) + }) + ]; + } + }); import { TableKit } from '@tiptap/extension-table'; import { ListKit } from '@tiptap/extension-list'; import { Placeholder, CharacterCount } from '@tiptap/extensions'; import Image from './RichTextInput/Image/index.js'; + import FileAttachment from './RichTextInput/FileAttachment'; // import TiptapImage from '@tiptap/extension-image'; import FileHandler from '@tiptap/extension-file-handler'; @@ -218,6 +246,7 @@ ); export let editor: Editor | null = null; + $: eventDispatch('editor', editor); export let socket = null; export let user = null; @@ -246,6 +275,7 @@ export let link = false; export let image = false; export let fileHandler = false; + export let attachments = false; export let suggestions = null; export let onFileDrop = (currentEditor, files, pos) => { @@ -393,6 +423,10 @@ $: serializeContent(richText); let provider: SocketIOCollaborationProvider | null = null; + export const flush = async () => { + await tick(); + await provider?.flush(); + }; let floatingMenuElement: Element | null = null; let bubbleMenuElement: Element | null = null; @@ -912,7 +946,8 @@ : []), CharacterCount.configure({}), - ...(image ? [Image] : []), + ...(image ? [Image.configure({ allowBase64: attachments })] : []), + ...(attachments ? [FileAttachment.configure({ context: componentContext })] : []), ...(fileHandler ? [ FileHandler.configure({ @@ -958,7 +993,7 @@ return view.hasFocus() && from !== to; } }), - FloatingMenu.configure({ + MountedFloatingMenu.configure({ element: floatingMenuElement, appendTo: () => document.body, options: { diff --git a/src/lib/components/common/RichTextInput/Collaboration.ts b/src/lib/components/common/RichTextInput/Collaboration.ts index 29e30c630d..b1df24f69a 100644 --- a/src/lib/components/common/RichTextInput/Collaboration.ts +++ b/src/lib/components/common/RichTextInput/Collaboration.ts @@ -46,6 +46,7 @@ export class SocketIOCollaborationProvider { private synced = false; private editor: Editor | null = null; private editorContentGetter: EditorContentGetter | null = null; + private listeners: Array<[string, (...args: any[]) => void]> = []; private contentSnapshotTimer: ReturnType | null = null; constructor( @@ -168,9 +169,31 @@ export class SocketIOCollaborationProvider { } } + private listen(event: string, handler: (...args: any[]) => void) { + this.listeners.push([event, handler]); + this.socket.on(event, handler); + } + + public async flush() { + await tick(); + if (!this.isConnected || !this.synced || !this.editorContentGetter) { + throw new Error('Tab is not connected. Reconnect before leaving this tab.'); + } + if (this.contentSnapshotTimer) { + clearTimeout(this.contentSnapshotTimer); + this.contentSnapshotTimer = null; + } + const result = await this.socket.timeout(10000).emitWithAck('ydoc:document:update', { + document_id: this.documentId, + flush: true, + data: { content: this.editorContentGetter() } + }); + if (!result?.ok) throw new Error(result?.error || 'Failed to save page'); + } + private setupEventListeners() { // Listen for document updates from server - this.socket.on('ydoc:document:update', (data) => { + this.listen('ydoc:document:update', (data) => { if (data.document_id === this.documentId && data.socket_id !== this.socket.id) { try { const update = new Uint8Array(data.update); @@ -188,7 +211,7 @@ export class SocketIOCollaborationProvider { }); // Listen for document state from server - this.socket.on('ydoc:document:state', async (data) => { + this.listen('ydoc:document:state', async (data) => { if (data.document_id === this.documentId) { try { if (data.state) { @@ -241,7 +264,7 @@ export class SocketIOCollaborationProvider { }); // Listen for awareness updates - this.socket.on('ydoc:awareness:update', (data) => { + this.listen('ydoc:awareness:update', (data) => { if (data.document_id === this.documentId) { try { const awarenessUpdate = new Uint8Array(data.update); @@ -260,6 +283,7 @@ export class SocketIOCollaborationProvider { this.doc.on('update', async (update, origin) => { if (this.editor && origin !== 'server' && this.isConnected) { await tick(); // Ensure the DOM is updated before sending + if (!this.editor || !this.isConnected) return; this.socket.emit('ydoc:document:update', { document_id: this.documentId, user_id: this.user?.id, @@ -316,9 +340,8 @@ export class SocketIOCollaborationProvider { }; public destroy() { - this.socket.off('ydoc:document:update'); - this.socket.off('ydoc:document:state'); - this.socket.off('ydoc:awareness:update'); + for (const [event, handler] of this.listeners) this.socket.off(event, handler); + this.listeners = []; this.socket.off('connect', this.onConnect); this.socket.off('disconnect', this.onDisconnect); @@ -336,6 +359,8 @@ export class SocketIOCollaborationProvider { this.editor = null; this.editorContentGetter = null; + this.isConnected = false; + this.doc.destroy(); } } diff --git a/src/lib/components/common/RichTextInput/FileAttachment.ts b/src/lib/components/common/RichTextInput/FileAttachment.ts new file mode 100644 index 0000000000..6046f040c3 --- /dev/null +++ b/src/lib/components/common/RichTextInput/FileAttachment.ts @@ -0,0 +1,95 @@ +import { Node } from '@tiptap/core'; +import { createClassComponent } from 'svelte/legacy'; +import FileItem from '../FileItem.svelte'; + +export default Node.create<{ context: Map }>({ + name: 'fileAttachment', + group: 'block', + atom: true, + draggable: true, + addOptions() { + return { context: new Map() }; + }, + addAttributes() { + return { + id: { default: '', rendered: false }, + name: { default: '', rendered: false }, + size: { default: 0, rendered: false } + }; + }, + parseHTML() { + return [ + { + tag: 'div[data-file-id]', + getAttrs: (element) => ({ + id: element.getAttribute('data-file-id'), + name: element.textContent, + size: Number(element.getAttribute('data-file-size')) || 0 + }) + }, + { + tag: 'a[href^="/api/v1/files/"]', + priority: 100, + getAttrs: (element) => { + const match = element + .getAttribute('href') + ?.match(/^\/api\/v1\/files\/([\w-]+)\/content$/); + return match ? { id: match[1], name: element.textContent, size: 0 } : false; + } + } + ]; + }, + renderHTML({ node }) { + return [ + 'div', + { 'data-file-id': node.attrs.id, 'data-file-size': node.attrs.size }, + ['a', { href: `/api/v1/files/${encodeURIComponent(node.attrs.id)}/content` }, node.attrs.name] + ]; + }, + renderText({ node }) { + return node.attrs.name; + }, + addNodeView() { + return ({ node, editor, getPos }) => { + const dom = document.createElement('div'); + dom.className = 'not-prose my-2 w-fit max-w-full'; + dom.contentEditable = 'false'; + dom.dataset.fileId = node.attrs.id; + const component = createClassComponent({ + component: FileItem, + target: dom, + context: this.options.context, + props: { + item: { ...node.attrs, type: 'file' }, + name: node.attrs.name, + type: 'file', + size: node.attrs.size, + url: node.attrs.id, + dismissible: editor.isEditable, + edit: editor.isEditable, + small: true, + modal: true, + className: 'w-56 max-w-full', + colorClassName: + 'bg-gray-50/60 dark:bg-white/[0.03] border border-gray-100/80 dark:border-white/5' + } + }); + component.$on('dismiss', () => { + const pos = getPos(); + if (editor.isEditable && typeof pos === 'number') { + editor + .chain() + .focus() + .deleteRange({ from: pos, to: pos + node.nodeSize }) + .run(); + } + }); + return { + dom, + stopEvent: (event) => !event.type.startsWith('drag'), + ignoreMutation: () => true, + destroy: () => component.$destroy() + }; + }; + } +}); diff --git a/src/lib/components/layout/Search/NotePreview.svelte b/src/lib/components/layout/Search/NotePreview.svelte index e9f0736cd4..131ac337ca 100644 --- a/src/lib/components/layout/Search/NotePreview.svelte +++ b/src/lib/components/layout/Search/NotePreview.svelte @@ -21,8 +21,21 @@ content = ''; const note = await getNoteById(localStorage.token, noteId).catch(() => null); if (version !== generation) return; - if (note) content = note.data?.content?.md ?? ''; - else error = true; + if (note) { + const terms = search.toLowerCase().split(/\s+/).filter(Boolean); + const pages = note.data?.pages ?? []; + const match = + pages.find( + (p) => + terms.length && + terms.every((t) => `${p.title} ${p.content.md}`.toLowerCase().includes(t)) + ) ?? + pages.find((p) => + terms.some((t) => `${p.title} ${p.content.md}`.toLowerCase().includes(t)) + ) ?? + pages[0]; + content = match ? `## ${match.title}\n\n${match.content.md}` : (note.data?.content?.md ?? ''); + } else error = true; loading = false; await tick(); if (version !== generation || !body || !search.trim()) return; diff --git a/src/lib/components/notes/NoteEditor.svelte b/src/lib/components/notes/NoteEditor.svelte index f29057237e..8b82f40999 100644 --- a/src/lib/components/notes/NoteEditor.svelte +++ b/src/lib/components/notes/NoteEditor.svelte @@ -10,7 +10,7 @@ import { toast } from 'svelte-sonner'; import { equalEditorJSON } from '$lib/utils/editorJson'; - import { goto, onNavigate } from '$app/navigation'; + import { goto, onNavigate, replaceState } from '$app/navigation'; import { page } from '$app/stores'; import dayjs from '$lib/dayjs'; @@ -30,7 +30,7 @@ isHeicImage } from '$lib/utils'; import { WEBUI_BASE_URL } from '$lib/constants'; - import { getFileById, uploadFile } from '$lib/apis/files'; + import { uploadFile } from '$lib/apis/files'; import { generateOpenAIChatCompletion } from '$lib/apis/openai'; import { @@ -45,7 +45,10 @@ pinnedNotes } from '$lib/stores'; - import { downloadPdf } from './utils'; + import { contentFiles, downloadNote } from './utils'; + import NoteTabs from './NoteTabs.svelte'; + import ChevronDown from '../icons/ChevronDown.svelte'; + import type { NotePage, NotePageOperation } from '$lib/apis/notes'; import Chat from '$lib/components/chat/Chat.svelte'; @@ -59,6 +62,7 @@ getNoteChatById, getNoteChatsById, updateNoteById, + updateNotePages, updateNoteAccessGrants, toggleNotePinnedStatusById, getPinnedNoteList @@ -66,7 +70,6 @@ import { deleteChatById } from '$lib/apis/chats'; import RichTextInput from '../common/RichTextInput.svelte'; - import FileItem from '../common/FileItem.svelte'; import Spinner from '../common/Spinner.svelte'; import Mic from '../icons/Mic.svelte'; import VoiceRecording from '../chat/MessageInput/VoiceRecording.svelte'; @@ -91,7 +94,7 @@ let revealedSearch = ''; $: if (editor && note?.id === id && $page.url.searchParams.get('search')) { const query = $page.url.searchParams.get('search')!.trim().toLowerCase(); - const key = `${id}:${query}`; + const key = `${id}:${activePageId}:${query}`; if (query && revealedSearch !== key) { revealedSearch = key; tick().then(() => { @@ -113,22 +116,162 @@ } let autoFormat = true; let note = null; + let activePageId = ''; + let activePage: NotePage; + let pageBusy = false; + let showNoteTabs = false; + let noteTabsPreferenceReady = false; + $: if (noteTabsPreferenceReady && $user?.id) { + localStorage.setItem(`note-tabs-open:${$user.id}`, String(showNoteTabs)); + } + const pendingFileUploads = new Set>(); + let previousPage: NotePage | null = null; + let loadGeneration = 0; + let pageScroll: Record = {}; + $: activePage = note?.data?.pages?.find((p) => p.id === activePageId); + $: if ( + note?.id === id && + $page.url.searchParams.get('page') && + $page.url.searchParams.get('page') !== activePageId + ) { + activatePage($page.url.searchParams.get('page')); + } - const newNote = { - title: '', - data: { - content: { - json: null, - html: '', - md: '' - }, - versions: [], - files: null - }, - // pages: [], // TODO: Implement pages for notes to allow users to create multiple pages in a note - meta: null, - access_grants: [] - }; + function activatePage(requested: string) { + const target = note?.data?.pages?.find((p) => p.id === requested); + if (!target || target.id === activePageId) return; + const body = document.getElementById('note-content-container'); + if (body && activePageId) pageScroll[activePageId] = body.scrollTop; + activePageId = target.id; + selectedContent = null; + versionIdx = null; + revealedSearch = ''; + localStorage.setItem(`note-page:${$user?.id}:${id}`, target.id); + tick().then(() => { + const container = document.getElementById('note-content-container'); + if (container) container.scrollTop = pageScroll[target.id] ?? 0; + document + .getElementById(`note-page-tab-${target.id}`) + ?.scrollIntoView({ block: 'nearest', inline: 'nearest' }); + }); + } + + async function flushActivePage() { + while (pendingFileUploads.size) await Promise.all([...pendingFileUploads]); + if (note?.write_access && versionIdx === null && inputElement) await inputElement.flush(); + if (debounceTimeout || titleInputFocused) { + if (debounceTimeout) clearTimeout(debounceTimeout); + debounceTimeout = null; + await saveHandler(true); + } + } + + async function selectPage(pageId: string) { + if (pageBusy || pageId === activePageId) return; + const url = new URL($page.url); + url.searchParams.set('page', pageId); + try { + pageBusy = true; + await flushActivePage(); + await goto(url.pathname + url.search, { noScroll: true, keepFocus: true }); + } catch (error) { + toast.error(String(error)); + } finally { + pageBusy = false; + } + } + + async function pageAction( + operation: Omit + ): Promise { + if (pageBusy) return null; + pageBusy = true; + try { + if ( + ['create', 'duplicate'].includes(operation.action) || + (operation.action === 'delete' && operation.page_id === activePageId) + ) { + await flushActivePage(); + } + const previous = note.data.pages; + const activeIndex = previous.findIndex((p) => p.id === activePageId); + const newId = ['create', 'duplicate'].includes(operation.action) ? uuidv4() : undefined; + const saved = await updateNotePages(localStorage.token, id, { + ...operation, + new_id: newId, + revision: note.data.pages_revision + }); + mergePages(saved.data.pages, saved.data.pages_revision); + const target = + newId ?? + (saved.data.pages.some((p) => p.id === activePageId) + ? activePageId + : saved.data.pages[Math.min(activeIndex, saved.data.pages.length - 1)].id); + activatePage(target); + const url = new URL($page.url); + url.searchParams.set('page', target); + // The old page was flushed above; the new editor is still initializing. + await goto(url.pathname + url.search, { + replaceState: true, + noScroll: true, + keepFocus: true + }); + return newId ?? target; + } catch (error) { + toast.error(String(error)); + const current = await getNoteById(localStorage.token, id).catch(() => null); + if (current) mergePages(current.data.pages, current.data.pages_revision); + return null; + } finally { + pageBusy = false; + } + } + + function mergePages(incoming: NotePage[], revision: number) { + if (!incoming || revision < (note.data.pages_revision ?? 0)) return; + const previous = note.data.pages; + const oldIndex = previous.findIndex((p) => p.id === activePageId); + const oldPage = previous[oldIndex]; + const nextPage = incoming.find((p) => p.id === activePageId); + if ( + !pageBusy && + note.write_access && + oldPage?.content?.md && + (!nextPage || + (nextPage.generation > oldPage.generation && nextPage.content.md !== oldPage.content.md)) + ) { + previousPage = structuredClone(oldPage); + } + note.data.pages = incoming.map((remote) => { + const local = previous.find((p) => p.id === remote.id); + if (local && local.generation > remote.generation) + return { + ...local, + title: remote.title, + inherit_note_title: remote.inherit_note_title, + parent_id: remote.parent_id + }; + if (local && local.generation === remote.generation) { + if (local.revision > remote.revision) + return { + ...local, + title: remote.title, + inherit_note_title: remote.inherit_note_title, + parent_id: remote.parent_id + }; + if (remote.id === activePageId && note.write_access) + return { ...remote, content: local.content, versions: local.versions }; + } + return remote; + }); + note.data.pages_revision = revision; + if (!note.data.pages.some((p) => p.id === activePageId)) { + activatePage(note.data.pages[Math.max(0, Math.min(oldIndex, note.data.pages.length - 1))].id); + const url = new URL($page.url); + url.searchParams.set('page', activePageId); + replaceState(url, $page.state); + } + } const hasPublicReadGrant = (grants) => Array.isArray(grants) && @@ -139,8 +282,6 @@ grant?.permission === 'read' ); - let files = []; - let wordCount = 0; let charCount = 0; @@ -156,6 +297,7 @@ let displayMediaRecord = false; let showNoteChat = false; + $: if (showNoteChat) showNoteTabs = false; let noteChatId = null; let noteChatLoading = false; let noteChats = []; @@ -165,12 +307,7 @@ let selectedContent = null; let noteAttachmentFiles = []; let noteChatSuggestedPrompts = []; - let pendingNoteEvent = null; - let pendingNoteEventTimer = null; - let lastLocalContentChangeAt = 0; - $: noteAttachmentFiles = (files ?? []).filter( - (file) => file?.type !== 'image' && !(file?.content_type ?? '').startsWith('image/') - ); + $: noteAttachmentFiles = contentFiles(activePage?.content); $: noteChatSuggestedPrompts = [ $i18n.t('Enhance this note and update it.'), $i18n.t('Summarize this note.'), @@ -193,10 +330,12 @@ // Computed HTML for editor: fall back to markdown if HTML is missing $: editorHtml = - note?.data?.content?.html || - (note?.data?.content?.md ? marked.parse(note.data.content.md) : ''); + activePage?.content?.html || + (activePage?.content?.md ? marked.parse(activePage.content.md) : ''); const init = async () => { + const generation = ++loadGeneration; + const noteId = id; autoFormat = true; loading = true; const res = await getNoteById(localStorage.token, id).catch((error) => { @@ -204,14 +343,24 @@ return null; }); + if (generation !== loadGeneration || id !== noteId) return; if (res) { + if (note?.id && note.id !== res.id) $socket?.emit('note:leave', { note_id: note.id }); + previousPage = null; note = res; + const requested = $page.url.searchParams.get('page'); + const remembered = localStorage.getItem(`note-page:${$user?.id}:${id}`); + activePageId = ''; + activatePage( + note.data.pages.find((p) => p.id === (requested || remembered))?.id ?? note.data.pages[0].id + ); + if (requested && requested !== activePageId) + toast.info($i18n.t('This page no longer exists')); if (!Array.isArray(note?.access_grants)) { note.access_grants = []; } - files = res.data.files || []; - $socket?.emit('join-note', { + $socket?.emit('note:join', { note_id: id, auth: { token: localStorage.token @@ -229,14 +378,12 @@ let debounceTimeout: NodeJS.Timeout | null = null; - const saveHandler = async () => { + const saveHandler = async (propagateError = false) => { const res = await updateNoteById(localStorage.token, id, { - title: note?.title === '' ? $i18n.t('Untitled') : note.title, - data: { - files: files - } + title: note?.title === '' ? $i18n.t('Untitled') : note.title }).catch((e) => { toast.error(`${e}`); + if (propagateError) throw e; }); if (res) { @@ -255,108 +402,16 @@ }, 200); }; - onNavigate(() => { - if (titleInputFocused) { - changeDebounceHandler(); + onNavigate(async () => { + if (!note || !inputElement || loading || pageBusy) return; + try { + await flushActivePage(); + } catch (error) { + toast.error(String(error)); + throw error; } - - if (!debounceTimeout) { - return; - } - - clearTimeout(debounceTimeout); - debounceTimeout = null; - return saveHandler(); }); - const applyExternalNoteContent = async (_note) => { - const incomingContent = _note.data?.content; - const contentLength = incomingContent?.md?.length ?? incomingContent?.html?.length ?? 0; - - console.info('[note-chat] external note event apply requested', { - noteId: _note.id, - eventUpdatedAt: _note.updated_at, - currentUpdatedAt: note?.updated_at, - editorPresent: !!editor, - contentLength - }); - - if (_note.updated_at && note?.updated_at && _note.updated_at < note.updated_at) { - console.info('[note-chat] external note event skipped', { - noteId: _note.id, - reason: 'stale', - eventUpdatedAt: _note.updated_at, - currentUpdatedAt: note.updated_at, - contentLength - }); - return false; - } - - const elapsed = Date.now() - lastLocalContentChangeAt; - if (elapsed < 800) { - pendingNoteEvent = _note; - if (pendingNoteEventTimer) { - clearTimeout(pendingNoteEventTimer); - } - pendingNoteEventTimer = setTimeout(async () => { - const event = pendingNoteEvent; - pendingNoteEvent = null; - if (event) { - await applyExternalNoteContent(event); - } - }, 850 - elapsed); - - console.info('[note-chat] external note event deferred', { - noteId: _note.id, - reason: 'local-edit-settling', - eventUpdatedAt: _note.updated_at, - contentLength - }); - return false; - } - - note.data.content = { - ...note.data.content, - ...incomingContent - }; - if (_note.updated_at) { - note.updated_at = _note.updated_at; - } - - if (!editor) { - console.info('[note-chat] external note event applied', { - noteId: _note.id, - reason: 'no-editor', - eventUpdatedAt: _note.updated_at, - contentLength - }); - return true; - } - - const selection = editor.state.selection; - editor.commands.setContent(incomingContent.html || marked.parse(incomingContent.md ?? '')); - await tick(); - - const docSize = editor.state.doc.content.size; - const from = Math.min(selection.from, docSize); - const to = Math.min(selection.to, docSize); - if (from < to) { - editor.commands.setTextSelection({ from, to }); - const text = editor.state.doc.textBetween(from, to, ' '); - selectedContent = text ? { text, from, to } : null; - } else { - selectedContent = null; - } - - console.info('[note-chat] external note event applied', { - noteId: _note.id, - reason: 'content', - eventUpdatedAt: _note.updated_at, - contentLength - }); - return true; - }; - $: if (id) { init(); } @@ -367,21 +422,21 @@ function insertNoteVersion(note) { const current = { - json: note.data.content.json, - html: note.data.content.html, - md: note.data.content.md + json: activePage.content.json, + html: activePage.content.html, + md: activePage.content.md }; - const lastVersion = note.data.versions?.at(-1); + const lastVersion = activePage.versions?.at(-1); if (!lastVersion || !areContentsEqual(lastVersion, current)) { - note.data.versions = (note.data.versions ?? []).concat(current); + activePage.versions = (activePage.versions ?? []).concat(current); return true; } return false; } const generateTitleHandler = async () => { - const content = note.data.content.md; + const content = note.data.pages.map((p) => `${p.title}\n${p.content.md}`).join('\n\n'); const DEFAULT_TITLE_GENERATION_PROMPT_TEMPLATE = `### Task: Generate a concise title summarizing the content in the content's primary language. ### Guidelines: @@ -455,52 +510,52 @@ ${content} }; function setContentByVersion(versionIdx) { - if (!note.data.versions?.length) return; + if (!activePage.versions?.length) return; let idx = versionIdx; - if (idx === null) idx = note.data.versions.length - 1; // latest - const v = note.data.versions[idx]; + if (idx === null) idx = activePage.versions.length - 1; // latest + const v = activePage.versions[idx]; - note.data.content.json = v.json; - note.data.content.html = v.html; - note.data.content.md = v.md; + activePage.content.json = v.json; + activePage.content.html = v.html; + activePage.content.md = v.md; if (versionIdx === null) { - const lastVersion = note.data.versions.at(-1); - const currentContent = note.data.content; + const lastVersion = activePage.versions.at(-1); + const currentContent = activePage.content; if (areContentsEqual(lastVersion, currentContent)) { // remove the last version - note.data.versions = note.data.versions.slice(0, -1); + activePage.versions = activePage.versions.slice(0, -1); } } } // Navigation function versionNavigateHandler(direction) { - if (!note.data.versions || note.data.versions.length === 0) return; + if (!activePage.versions || activePage.versions.length === 0) return; if (versionIdx === null) { // Get latest snapshots - const lastVersion = note.data.versions.at(-1); - const currentContent = note.data.content; + const lastVersion = activePage.versions.at(-1); + const currentContent = activePage.content; if (!areContentsEqual(lastVersion, currentContent)) { // If the current content is different from the last version, insert a new version insertNoteVersion(note); - versionIdx = note.data.versions.length - 1; + versionIdx = activePage.versions.length - 1; } else { - versionIdx = note.data.versions.length; + versionIdx = activePage.versions.length; } } if (direction === 'prev') { if (versionIdx > 0) versionIdx -= 1; } else if (direction === 'next') { - if (versionIdx < note.data.versions.length - 1) versionIdx += 1; + if (versionIdx < activePage.versions.length - 1) versionIdx += 1; else versionIdx = null; // Reset to latest - if (versionIdx === note.data.versions.length - 1) { + if (versionIdx === activePage.versions.length - 1) { // If we reach the latest version, reset to null versionIdx = null; } @@ -509,86 +564,6 @@ ${content} setContentByVersion(versionIdx); } - const uploadFileHandler = async (file) => { - const tempItemId = uuidv4(); - const fileItem = { - type: 'file', - file: '', - id: null, - url: '', - name: file.name, - collection_name: '', - status: 'uploading', - size: file.size, - error: '', - itemId: tempItemId - }; - - if (fileItem.size == 0) { - toast.error($i18n.t('You cannot upload an empty file.')); - return null; - } - - files = [...files, fileItem]; - - try { - // If the file is an audio file, provide the language for STT. - let metadata = null; - if ( - (file.type.startsWith('audio/') || file.type.startsWith('video/')) && - $settings?.audio?.stt?.language - ) { - metadata = { - language: $settings?.audio?.stt?.language - }; - } - - // During the file upload, file content is automatically extracted. - const uploadedFile = await uploadFile(localStorage.token, file, metadata); - - if (uploadedFile) { - console.log('File upload completed:', uploadedFile); - - if (uploadedFile.error) { - console.warn('File upload warning:', uploadedFile.error); - toast.warning(uploadedFile.error); - } - - fileItem.status = 'uploaded'; - fileItem.file = await getFileById(localStorage.token, uploadedFile.id).catch((e) => { - toast.error(`${e}`); - return null; - }); - fileItem.id = uploadedFile.id; - fileItem.collection_name = - uploadedFile?.meta?.collection_name || uploadedFile?.collection_name; - - fileItem.url = `${uploadedFile.id}`; - - files = files.map((item) => (item?.itemId === tempItemId ? fileItem : item)); - } else { - files = files.filter((item) => item?.itemId !== tempItemId); - } - } catch (e) { - toast.error(`${e}`); - files = files.filter((item) => item?.itemId !== tempItemId); - } - - if (files.length > 0) { - note.data.files = files; - } else { - note.data.files = null; - } - - if (editor) { - editor.storage.files = files; - } - - changeDebounceHandler(); - - return fileItem; - }; - const compressImageHandler = async (imageUrl, settings = {}, config = {}) => { // Quick shortcut so we don’t do unnecessary work. const settingsCompression = settings?.imageCompression ?? false; @@ -625,7 +600,48 @@ ${content} return imageUrl; }; - const inputFileHandler = async (file) => { + const inputFileHandler = ( + file, + uploadEditor = editor, + pos = uploadEditor?.state.selection.from + ) => { + if (!uploadEditor || uploadEditor.isDestroyed || !file) return Promise.resolve(null); + // Keep the insertion point attached to the document while the upload is in flight. + let position = pos; + const trackPosition = ({ transaction }) => { + position = transaction.mapping.map(position); + }; + uploadEditor.on('transaction', trackPosition); + const pending = (async () => { + const uploading = toast.loading($i18n.t('Uploading...')); + try { + const item = await processInputFile(file); + if (!item || uploadEditor.isDestroyed) return null; + uploadEditor + .chain() + .insertContentAt( + position, + item.type === 'image' + ? { type: 'image', attrs: { src: item.url } } + : { type: 'fileAttachment', attrs: { id: item.id, name: item.name, size: item.size } } + ) + .focus() + .run(); + return item; + } catch (error) { + toast.error(String(error)); + return null; + } finally { + toast.dismiss(uploading); + uploadEditor.off('transaction', trackPosition); + } + })(); + pendingFileUploads.add(pending); + pending.finally(() => pendingFileUploads.delete(pending)); + return pending; + }; + + const processInputFile = async (file) => { console.log('Processing file:', { name: file.name, type: file.type, @@ -663,13 +679,6 @@ ${content} type: 'image', url: `${imageUrl}` }; - files = [...files, fileItem]; - note.data.files = files; - if (editor) { - editor.storage.files = files; - } - - changeDebounceHandler(); resolve(fileItem); } catch (err) { reject(err); @@ -681,41 +690,38 @@ ${content} return await uploadImagePromise; } else { - return await uploadFileHandler(file); + if (!file.size) throw new Error($i18n.t('You cannot upload an empty file.')); + const metadata = + (file.type.startsWith('audio/') || file.type.startsWith('video/')) && + $settings?.audio?.stt?.language + ? { language: $settings.audio.stt.language } + : null; + const uploaded = await uploadFile(localStorage.token, file, metadata); + if (!uploaded?.id) return null; + if (uploaded.error) toast.warning(uploaded.error); + return { type: 'file', id: uploaded.id, name: file.name, size: file.size }; } }; - const inputFilesHandler = async (inputFiles) => { - console.log('Input files handler called with:', inputFiles); - inputFiles.forEach(async (file) => { - await inputFileHandler(file); - }); + const inputFilesHandler = async ( + inputFiles, + uploadEditor = editor, + position = uploadEditor?.state.selection.from + ) => { + for (const file of inputFiles) { + await inputFileHandler(file, uploadEditor, position); + position = uploadEditor?.state.selection.to; + } }; const uploadNoteFilesHandler = () => { const input = document.createElement('input'); input.type = 'file'; input.multiple = true; + const uploadEditor = editor; + const position = uploadEditor?.state.selection.from; + input.onchange = () => inputFilesHandler(Array.from(input.files ?? []), uploadEditor, position); input.click(); - - input.onchange = async () => { - const inputFiles = Array.from(input.files ?? []); - for (const file of inputFiles) { - const fileItem = await inputFileHandler(file); - if (fileItem?.type === 'image' && editor) { - editor - .chain() - .insertContentAt(editor.state.selection.$anchor.pos, { - type: 'image', - attrs: { - src: `data://${fileItem.id}` - } - }) - .focus() - .run(); - } - } - }; }; const openNoteChat = async () => { @@ -825,20 +831,13 @@ ${content} noteChats = chats; }; - const downloadHandler = async (type) => { - console.log('downloadHandler', type); - if (type === 'txt') { - const blob = new Blob([note.data.content.md], { type: 'text/plain' }); - saveAs(blob, `${note.title}.txt`); - } else if (type === 'md') { - const blob = new Blob([note.data.content.md], { type: 'text/markdown' }); - saveAs(blob, `${note.title}.md`); - } else if (type === 'pdf') { - try { - await downloadPdf(note); - } catch (error) { - toast.error(`${error}`); - } + const downloadHandler = async (type, pageId: string | null = null) => { + try { + await flushActivePage(); + const saved = await getNoteById(localStorage.token, id); + await downloadNote(saved, type, pageId); + } catch (error) { + toast.error(String(error)); } }; @@ -909,60 +908,37 @@ ${content} }; const noteEventHandler = async (_note) => { - console.log('noteEventHandler', _note); - if (_note.id !== id) return; - - if (_note.updated_at && note?.updated_at && _note.updated_at < note.updated_at) { - console.info('[note-chat] external note event skipped', { - noteId: _note.id, - reason: 'stale-event', - eventUpdatedAt: _note.updated_at, - currentUpdatedAt: note.updated_at, - contentLength: _note.data?.content?.md?.length ?? _note.data?.content?.html?.length ?? 0 - }); + if (_note.id !== id || !note) return; + if (_note.page) { + const remote = _note.page; + const local = note.data.pages.find((p) => p.id === remote.id); + if (!local || remote.revision < local.revision || remote.generation < local.generation) + return; + if ( + remote.id === activePageId && + note.write_access && + remote.generation === local.generation + ) { + local.revision = remote.revision; + } else { + note.data.pages = note.data.pages.map((p) => (p.id === remote.id ? remote : p)); + if (remote.id === activePageId && !note.write_access) + editor?.commands.setContent( + remote.content.json || remote.content.html || marked.parse(remote.content.md) + ); + } return; } + if (_note.data?.pages) mergePages(_note.data.pages, _note.data.pages_revision); + if (_note.access_grants) note.access_grants = _note.access_grants; + if (_note.title && !titleInputFocused && !debounceTimeout) note.title = _note.title; - if (_note.access_grants && _note.access_grants !== note.access_grants) { - note.access_grants = _note.access_grants; - } - - // A pending local save holds newer files than an echo of an earlier save. - if (_note.data && 'files' in _note.data && !debounceTimeout) { - files = _note.data.files ?? []; - note.data.files = files.length > 0 ? files : null; - } - - if (_note.data?.content) { - await applyExternalNoteContent(_note); - } - - if (_note.title && _note.title) { - note.title = _note.title; - } - - if (_note.updated_at) { - note.updated_at = _note.updated_at; - } - - if (editor) { - editor.storage.files = files; - } - await tick(); - - for (const file of files) { - if (file.type === 'image' || (file?.content_type ?? '').startsWith('image/')) { - const e = new CustomEvent('data', { files: files }); - - const img = document.getElementById(`image:${file.id}`); - if (img) { - img.dispatchEvent(e); - } - } - } + if (_note.updated_at) note.updated_at = _note.updated_at; }; onMount(async () => { + showNoteTabs = localStorage.getItem(`note-tabs-open:${$user?.id}`) === 'true'; + noteTabsPreferenceReady = true; await tick(); selectedModelId = @@ -977,11 +953,10 @@ ${content} }); onDestroy(() => { - console.log('destroy'); + loadGeneration++; + $socket?.emit('note:leave', { note_id: note?.id }); + if (debounceTimeout) clearTimeout(debounceTimeout); $socket?.off('events:note', noteEventHandler); - if (pendingNoteEventTimer) { - clearTimeout(pendingNoteEventTimer); - } const dropzoneElement = document.getElementById('note-editor'); @@ -1004,7 +979,7 @@ ${content} -{#if note} +{#if note && activePage} 0) { - await uploadFileHandler(files[0]); + await inputFileHandler(files[0]); } }; }} @@ -1257,8 +1232,8 @@ ${content} }} onCopyToClipboard={async () => { const res = await copyToClipboard( - note.data.content.md, - note.data.content.html, + activePage.content.md, + activePage.content.html, true ).catch((error) => { toast.error(`${error}`); @@ -1275,7 +1250,7 @@ ${content} isPinned={$pinnedNotes.some((n) => n.id === note.id)} onPin={async () => { await toggleNotePinnedStatusById(localStorage.token, note.id); - note = await getNoteById(localStorage.token, note.id); + note.is_pinned = !note.is_pinned; pinnedNotes.set(await getPinnedNoteList(localStorage.token).catch(() => [])); }} > @@ -1320,8 +1295,25 @@ ${content} }} >
+
+ +
@@ -1376,135 +1368,124 @@ ${content}
-
- {#if noteAttachmentFiles.length > 0} -
- {#each noteAttachmentFiles as file, fileIdx (file?.id ?? file?.itemId ?? file?.url ?? fileIdx)} - { - files = files.filter((item) => item !== file); - note.data.files = files.length > 0 ? files : null; - if (editor) { - editor.storage.files = files; - } - changeDebounceHandler(); - }} - /> - {/each} -
- {/if} - - {#key autoFormat} - { - const { from, to } = editor.state.selection; - const selectedText = editor.state.doc.textBetween(from, to, ' '); - - if (selectedText.length === 0) { - selectedContent = null; - } else { - selectedContent = { - text: selectedText, - from: from, - to: to - }; - } + {#if previousPage} +
+ {$i18n.t( + 'A page changed elsewhere. Your previous text is available to download.' + )} + + +
+ {/if} +
+ + (showNoteTabs = false)} + pages={note.data.pages.map((page) => + page.id === 'main' && page.inherit_note_title !== false + ? { ...page, title: note.title || $i18n.t('Untitled') } + : page + )} + activeId={activePageId} + editable={note.write_access} + busy={pageBusy} + onSelect={async (pageId) => { + await selectPage(pageId); + if ($mobile) showNoteTabs = false; }} - onChange={(content) => { - lastLocalContentChangeAt = Date.now(); - note.data.content.html = content.html; - note.data.content.md = content.md; - }} - fileHandler={true} - onFileDrop={(currentEditor, files, pos) => { - files.forEach(async (file) => { - const fileItem = await inputFileHandler(file).catch((error) => { - return null; - }); - - if (fileItem?.type === 'image') { - // If the file is an image, insert it directly - currentEditor - .chain() - .insertContentAt(pos, { - type: 'image', - attrs: { - src: `data://${fileItem.id}` - } - }) - .focus() - .run(); - } - }); - }} - onFilePaste={() => {}} - on:paste={async (e) => { - e = e.detail.event || e; - const clipboardData = e.clipboardData || window.clipboardData; - console.log('Clipboard data:', clipboardData); - - if (clipboardData && clipboardData.items) { - console.log('Clipboard data items:', clipboardData.items); - for (const item of clipboardData.items) { - console.log('Clipboard item:', item); - if (item.type.indexOf('image') !== -1) { - const blob = item.getAsFile(); - const fileItem = await inputFileHandler(blob); - - if (editor) { - editor - ?.chain() - .insertContentAt(editor.state.selection.$anchor.pos, { - type: 'image', - attrs: { - src: `data://${fileItem.id}` // Use data URI for the image - } - }) - .focus() - .run(); - } - } else if (item?.kind === 'file') { - const file = item.getAsFile(); - await inputFileHandler(file); - e.preventDefault(); - } - } - } + onAction={pageAction} + onCopyLink={async (pageId) => { + await copyToClipboard( + `${window.location.origin}/notes/${id}?page=${encodeURIComponent(pageId)}` + ); + toast.success($i18n.t('Copied link to clipboard')); }} + onDownload={(pageId, format) => downloadHandler(format, pageId)} /> - {/key} + +
+ {#each [activePage] as renderedPage (`${note.id}:${renderedPage.id}:${renderedPage.generation}:${autoFormat}`)} + (editor = event.detail)} + id={`note-${note.id}-${renderedPage.id}`} + className="input-prose-sm px-0.5 flex-1 min-h-[12rem]" + json={true} + bind:value={renderedPage.content.json} + html={editorHtml} + documentId={`note:${note.id}:page:${renderedPage.id}:${renderedPage.generation}`} + collaboration={true} + socket={$socket} + user={$user} + dragHandle={true} + link={true} + image={true} + attachments={true} + placeholder={$i18n.t('Write something...')} + editable={versionIdx === null && note?.write_access} + onSelectionUpdate={({ editor }) => { + const { from, to } = editor.state.selection; + const selectedText = editor.state.doc.textBetween(from, to, ' '); + + if (selectedText.length === 0) { + selectedContent = null; + } else { + selectedContent = { + text: selectedText, + from: from, + to: to + }; + } + }} + onChange={(content) => { + renderedPage.content.json = content.json; + renderedPage.content.html = content.html; + renderedPage.content.md = content.md; + }} + fileHandler={true} + onFileDrop={(currentEditor, files, pos) => + inputFilesHandler(files, currentEditor, pos)} + onFilePaste={() => {}} + on:paste={(event) => { + const e = event.detail.event || event; + const pasted = Array.from(e.clipboardData?.files ?? []); + if (pasted.length) { + e.preventDefault(); + inputFilesHandler(pasted); + } + }} + /> + {/each} +
{/if} @@ -1525,7 +1506,7 @@ ${content} }} onConfirm={(data) => { if (data?.file) { - uploadFileHandler(data?.file); + inputFileHandler(data?.file); } recording = false; @@ -1549,6 +1530,9 @@ ${content} embeddedDraftKey={noteChatDraftKey} suggestedPrompts={noteChatSuggestedPrompts} selectedText={selectedContent?.text ?? ''} + notePageId={activePageId} + noteFiles={noteAttachmentFiles} + onBeforeNoteSubmit={flushActivePage} onInsertToNote={note?.write_access ? insertHandler : null} onNewEmbeddedChat={createNoteChat} onCreateEmbeddedChat={createNoteChatOnFirstMessage} diff --git a/src/lib/components/notes/NotePanel.svelte b/src/lib/components/notes/NotePanel.svelte index acf0781e8c..c232b5b175 100644 --- a/src/lib/components/notes/NotePanel.svelte +++ b/src/lib/components/notes/NotePanel.svelte @@ -5,10 +5,16 @@ import ResizableSidePanel from '../common/ResizableSidePanel.svelte'; export let show = false; + export let side: 'left' | 'right' = 'right'; + export let panelWidth = 350; + export let minWidth = 350; + export let closeOnDragBelowMinWidth = true; + export let resizerId = 'controls-resizer'; + export let storageKey = ''; + export let floating = false; let mediaQuery: MediaQueryList; let largeScreen = false; - let panelWidth = 350; const handleMediaQuery = (e: MediaQueryListEvent | MediaQueryList) => { largeScreen = e.matches; @@ -42,14 +48,20 @@
diff --git a/src/lib/components/notes/NoteTabMenu.svelte b/src/lib/components/notes/NoteTabMenu.svelte new file mode 100644 index 0000000000..0cf02f8d86 --- /dev/null +++ b/src/lib/components/notes/NoteTabMenu.svelte @@ -0,0 +1,74 @@ + + + + +
+ + {#if editable} + + + {#if page.id !== 'main'} + + + + {/if} + {/if} +
+ + {#each ['md', 'txt', 'pdf'] as format} + + {/each} + {#if editable && page.id !== 'main'} +
+ + {/if} +
+
+
diff --git a/src/lib/components/notes/NoteTabs.svelte b/src/lib/components/notes/NoteTabs.svelte new file mode 100644 index 0000000000..ded48ed721 --- /dev/null +++ b/src/lib/components/notes/NoteTabs.svelte @@ -0,0 +1,385 @@ + + +
+
+ {$i18n.t('Pages')} + + +
+ +
+
+ {#each rows as { page, depth, hasChildren, position, siblings, path } (page.id)} +
+ {#if hasChildren} + + {:else}{/if} + {#if renaming === page.id} + { + if (event.isComposing) return; + if (event.key === 'Enter') { + event.preventDefault(); + commitRename(); + } + if (event.key === 'Escape') renaming = ''; + }} + /> + {:else} + + {/if} + {#if editable} + + {/if} + { + deleting = page; + confirmDelete = true; + }} + className={activeId === page.id + ? '' + : 'sm:opacity-0 sm:group-hover:opacity-100 sm:group-focus-within:opacity-100'} + /> +
+ {/each} +
+ {#if editable} + + {/if} +
+
+ + deleting && onAction({ action: 'delete', page_id: deleting.id })} +> +
+ {$i18n.t('Delete "{{title}}"? This cannot be undone.', { title: deleting?.title ?? '' })} + {#if deleting && pages.some((page) => page.parent_id === deleting.id)}

+ {$i18n.t('Its subpages will be kept and moved up one level.')} +

{/if} +
+
+ + + moving && + moveParent !== (moving.parent_id ?? '') && + onAction({ action: 'move', page_id: moving.id, parent_id: moveParent || null })} +> +
+ {$i18n.t('Choose a parent page. Subpages move with this page.')} +
+ +
+ + diff --git a/src/lib/components/notes/Notes.svelte b/src/lib/components/notes/Notes.svelte index 6a99404923..9238bd2cf4 100644 --- a/src/lib/components/notes/Notes.svelte +++ b/src/lib/components/notes/Notes.svelte @@ -38,7 +38,7 @@ getPinnedNoteList } from '$lib/apis/notes'; import { capitalizeFirstLetter, copyToClipboard, formatNumber, getTimeRange } from '$lib/utils'; - import { downloadPdf, createNoteHandler } from './utils'; + import { downloadNote, createNoteHandler } from './utils'; import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte'; import GarbageBin from '../icons/GarbageBin.svelte'; @@ -95,18 +95,10 @@ if (!note) return; - if (type === 'txt') { - const blob = new Blob([note.data.content.md], { type: 'text/plain' }); - saveAs(blob, `${note.title}.txt`); - } else if (type === 'md') { - const blob = new Blob([note.data.content.md], { type: 'text/markdown' }); - saveAs(blob, `${note.title}.md`); - } else if (type === 'pdf') { - try { - await downloadPdf(note); - } catch (error) { - toast.error(`${error}`); - } + try { + await downloadNote(note, type); + } catch (error) { + toast.error(String(error)); } }; @@ -204,6 +196,11 @@ init(); } + const noteUrl = (note) => + query && note.data?.page_id + ? `/notes/${note.id}?page=${encodeURIComponent(note.data.page_id)}&search=${encodeURIComponent(query)}` + : `/notes/${note.id}`; + const getItemsPage = async () => { itemsLoading = true; @@ -621,13 +618,13 @@ role="presentation" on:click={(e) => { if (e.target instanceof Element && e.target.closest('a')) return; - goto(`/notes/${note.id}`); + goto(noteUrl(note)); }} >
@@ -762,7 +759,7 @@ class="group relative flex h-26 min-w-0 w-full flex-col rounded-md border border-black/[0.06] bg-gray-50/40 p-2 text-left transition-colors duration-75 hover:bg-gray-50/40 focus-within:bg-gray-50/40 dark:border-white/[0.06] dark:bg-gray-900 dark:hover:bg-white/4 dark:focus-within:bg-white/4" >
- + - +
diff --git a/src/lib/components/notes/utils.ts b/src/lib/components/notes/utils.ts index 74b0bddf05..a8c1f3aa40 100644 --- a/src/lib/components/notes/utils.ts +++ b/src/lib/components/notes/utils.ts @@ -3,6 +3,60 @@ import { toast } from 'svelte-sonner'; import { createNewNote } from '$lib/apis/notes'; import { safeImageUrl } from '$lib/utils/safeImageUrl'; +import { marked } from 'marked'; +// @ts-expect-error The installed file-saver package has no type declarations. +import fileSaver from 'file-saver'; +import type { NoteContent, NotePage, NotePageOperation } from '$lib/apis/notes'; + +export const downloadNote = async ( + note: { title: string; data: { pages?: NotePage[]; content: NoteContent; files?: any[] } }, + format: string, + pageId: string | null = null +) => { + const pages = ( + note.data.pages ?? [ + { + id: 'main', + title: note.title, + inherit_note_title: true, + content: note.data.content, + files: note.data.files + } + ] + ).map((page) => + page.id === 'main' && page.inherit_note_title !== false ? { ...page, title: note.title } : page + ); + const paths = new Map(tabRows(pages).map(({ page, path }) => [page.id, path])); + const selected = pageId ? pages.filter((page) => page.id === pageId) : pages; + if (!selected.length) throw new Error('Page no longer exists'); + const title = pageId && pageId !== 'main' ? `${note.title} - ${selected[0].title}` : note.title; + const md = + selected.length === 1 + ? selected[0].content.md + : selected.map((page) => `## ${paths.get(page.id)}\n\n${page.content.md}`).join('\n\n'); + if (format !== 'pdf') { + fileSaver.saveAs( + new Blob([md], { type: format === 'md' ? 'text/markdown' : 'text/plain' }), + `${title}.${format}` + ); + return; + } + const html = selected + .map((page) => { + const heading = document.createElement('h2'); + heading.textContent = paths.get(page.id) ?? page.title; + return `
${selected.length > 1 ? heading.outerHTML : ''}${page.content.html || marked.parse(page.content.md || '')}
`; + }) + .join(''); + await downloadPdf({ + ...note, + title, + data: { + ...note.data, + content: { html, md } + } + }); +}; export const downloadPdf = async (note) => { const [{ default: jsPDF }, { default: html2canvas }] = await Promise.all([ @@ -74,6 +128,9 @@ export const downloadPdf = async (note) => { const pagePixelHeight = Math.floor((virtualWidth * pageHeightMM) / pageWidthMM); const rootTop = node.getBoundingClientRect().top; const contentHeight = Math.ceil(node.getBoundingClientRect().height); + const pageBoundaries = Array.from(node.querySelectorAll('[data-note-page]')) + .slice(1) + .map((element) => Math.floor(element.getBoundingClientRect().top - rootTop)); const blocks = []; const trackedElements = new Set(); @@ -105,7 +162,8 @@ export const downloadPdf = async (note) => { let page = 0; while (offsetY < contentHeight) { - const sliceHeight = Math.min(pagePixelHeight, contentHeight - offsetY); + const nextBoundary = pageBoundaries.find((top) => top > offsetY) ?? contentHeight; + const sliceHeight = Math.min(pagePixelHeight, contentHeight - offsetY, nextBoundary - offsetY); const sliceBlocks = blocks.filter( (block) => block.bottom > offsetY && block.top < offsetY + sliceHeight ); @@ -202,3 +260,147 @@ export const createNoteHandler = async (title: string, md?: string, html?: strin return res; } }; + +type Tab = Pick; + +export function tabRows(pages: T[], collapsed = new Set()) { + const children = new Map(); + for (const page of pages) { + const parent = page.parent_id ?? null; + if (!children.has(parent)) children.set(parent, []); + children.get(parent)!.push(page); + } + const rows: { + page: T; + depth: number; + path: string; + hasChildren: boolean; + position: number; + siblings: number; + }[] = []; + const stack = (children.get(null) ?? []) + .map((page, index, roots) => ({ + page, + depth: 0, + path: page.title, + position: index + 1, + siblings: roots.length + })) + .reverse(); + while (stack.length) { + const row = stack.pop()!; + const nested = children.get(row.page.id) ?? []; + rows.push({ ...row, hasChildren: nested.length > 0 }); + if (!collapsed.has(row.page.id)) { + stack.push( + ...nested + .map((page, index) => ({ + page, + depth: row.depth + 1, + path: `${row.path} / ${page.title}`, + position: index + 1, + siblings: nested.length + })) + .reverse() + ); + } + } + return rows; +} + +export function tabAncestors(pages: Tab[], id: string) { + const lookup = new Map(pages.map((page) => [page.id, page])); + const ancestors: string[] = []; + let parent = lookup.get(id)?.parent_id; + while (parent && !ancestors.includes(parent)) { + ancestors.unshift(parent); + parent = lookup.get(parent)?.parent_id; + } + return ancestors; +} + +export function tabBranchIds(pages: Tab[], id: string) { + const children = new Map(); + for (const page of pages) { + if (page.parent_id) { + if (!children.has(page.parent_id)) children.set(page.parent_id, []); + children.get(page.parent_id)!.push(page.id); + } + } + const ids = new Set(); + const stack = [id]; + while (stack.length) { + const next = stack.pop()!; + if (ids.has(next)) continue; + ids.add(next); + stack.push(...(children.get(next) ?? [])); + } + return ids; +} + +export function siblingMove( + pages: Tab[], + id: string, + direction: number +): Omit | null { + const page = pages.find((page) => page.id === id); + if (!page || page.id === 'main') return null; + const parent_id = page.parent_id ?? null; + const siblings = pages.filter( + (page) => page.id !== 'main' && (page.parent_id ?? null) === parent_id + ); + const index = siblings.findIndex((page) => page.id === id); + const next = index + direction; + if (next < 0 || next >= siblings.length) return null; + return { + action: 'move', + page_id: id, + parent_id, + before_id: siblings[direction < 0 ? next : next + 1]?.id ?? null + }; +} + +export type PageDropPosition = 'before' | 'inside' | 'after'; + +export function pageDropMove( + pages: Tab[], + id: string, + targetId: string, + position: PageDropPosition +): Omit | null { + const target = pages.find((page) => page.id === targetId); + if ( + id === 'main' || + !pages.some((page) => page.id === id) || + !target || + tabBranchIds(pages, id).has(targetId) || + (targetId === 'main' && position === 'before') + ) + return null; + const parent_id = position === 'inside' ? targetId : (target.parent_id ?? null); + const siblings = pages.filter((page) => (page.parent_id ?? null) === parent_id && page.id !== id); + return { + action: 'move', + page_id: id, + parent_id, + before_id: + position === 'before' + ? targetId + : position === 'after' + ? (siblings[siblings.findIndex((page) => page.id === targetId) + 1]?.id ?? null) + : null + }; +} + +export function contentFiles(content: NoteContent | undefined) { + const files = new Map(); + const stack = content?.json ? [content.json] : []; + while (stack.length) { + const node = stack.pop(); + if (node?.type === 'fileAttachment' && node.attrs?.id) { + files.set(node.attrs.id, { ...node.attrs, type: 'file' }); + } + stack.push(...(node?.content ?? []).slice().reverse()); + } + return [...files.values()]; +}