mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-16 09:41:23 +00:00
refac
This commit is contained in:
parent
ab133dba21
commit
b34885efc6
23 changed files with 2059 additions and 623 deletions
|
|
@ -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'<div data-file-id="{escape(file["id"], quote=True)}"><a href="{escape(href, quote=True)}">{escape(attrs["name"])}</a></div>'
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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':
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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],
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<NoteItem, 'access_grants'>
|
||||
note: Omit<NoteItem, 'access_grants'> & { page_id?: string }
|
||||
) => {
|
||||
let error = null;
|
||||
|
||||
|
|
|
|||
|
|
@ -151,6 +151,9 @@
|
|||
export let embeddedDraftKey = '';
|
||||
export let suggestedPrompts = [];
|
||||
export let selectedText = '';
|
||||
export let notePageId = '';
|
||||
export let noteFiles = [];
|
||||
export let onBeforeNoteSubmit: (() => Promise<void>) | null = null;
|
||||
export let onInsertToNote: ((content: string) => void) | null = null;
|
||||
export let onCloseEmbedded: (() => void) | null = null;
|
||||
export let onNewEmbeddedChat: (() => void | Promise<void>) | 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<string, unknown> = {}) => {
|
||||
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<BridgeSubmission> => {
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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'}
|
||||
<!-- svelte-ignore a11y_no_noninteractive_element_interactions, a11y_no_noninteractive_tabindex -->
|
||||
<div
|
||||
class="relative flex items-center justify-center group border-l border-gray-50 dark:border-gray-850/30 hover:border-gray-200 dark:hover:border-gray-800 transition z-20 bg-transparent p-0 appearance-none"
|
||||
class="relative flex items-center justify-center group border-l border-gray-50 dark:border-gray-850/30 hover:border-gray-200 dark:hover:border-gray-800 transition z-20 bg-transparent p-0 appearance-none {subtleDivider
|
||||
? 'border-transparent! outline-none!'
|
||||
: ''}"
|
||||
id={resizerId}
|
||||
on:pointerdown={resizeStartHandler}
|
||||
on:keydown={resizeKeyHandler}
|
||||
|
|
@ -153,6 +157,14 @@
|
|||
aria-valuemin={minWidth}
|
||||
aria-valuemax={maxWidth ?? undefined}
|
||||
>
|
||||
{#if subtleDivider}
|
||||
<span
|
||||
aria-hidden="true"
|
||||
class="pointer-events-none absolute h-8 w-0.5 rounded-full bg-gray-300 dark:bg-gray-600 transition-opacity {isResizing
|
||||
? 'opacity-100'
|
||||
: 'opacity-0 group-hover:opacity-100 group-focus-visible:opacity-100'}"
|
||||
></span>
|
||||
{/if}
|
||||
<span
|
||||
class="absolute -left-1.5 -right-1.5 -top-0 -bottom-0 z-20 cursor-col-resize bg-transparent"
|
||||
style="touch-action: none;"
|
||||
|
|
@ -167,7 +179,9 @@
|
|||
{#if side === 'left'}
|
||||
<!-- svelte-ignore a11y_no_noninteractive_element_interactions, a11y_no_noninteractive_tabindex -->
|
||||
<div
|
||||
class="relative flex items-center justify-center group border-r border-gray-50 dark:border-gray-850/30 hover:border-gray-200 dark:hover:border-gray-800 transition z-20 bg-transparent p-0 appearance-none"
|
||||
class="relative flex items-center justify-center group border-r border-gray-50 dark:border-gray-850/30 hover:border-gray-200 dark:hover:border-gray-800 transition z-20 bg-transparent p-0 appearance-none {subtleDivider
|
||||
? 'border-transparent! outline-none!'
|
||||
: ''}"
|
||||
id={resizerId}
|
||||
on:pointerdown={resizeStartHandler}
|
||||
on:keydown={resizeKeyHandler}
|
||||
|
|
@ -179,6 +193,14 @@
|
|||
aria-valuemin={minWidth}
|
||||
aria-valuemax={maxWidth ?? undefined}
|
||||
>
|
||||
{#if subtleDivider}
|
||||
<span
|
||||
aria-hidden="true"
|
||||
class="pointer-events-none absolute h-8 w-0.5 rounded-full bg-gray-300 dark:bg-gray-600 transition-opacity {isResizing
|
||||
? 'opacity-100'
|
||||
: 'opacity-0 group-hover:opacity-100 group-focus-visible:opacity-100'}"
|
||||
></span>
|
||||
{/if}
|
||||
<span
|
||||
class="absolute -left-1.5 -right-1.5 -top-0 -bottom-0 z-20 cursor-col-resize bg-transparent"
|
||||
style="touch-action: none;"
|
||||
|
|
|
|||
|
|
@ -132,10 +132,11 @@
|
|||
replacement: (content) => `<u>${content}</u>`
|
||||
});
|
||||
|
||||
import { onMount, onDestroy, tick, getContext } from 'svelte';
|
||||
import { onMount, onDestroy, tick, getContext, getAllContexts } from 'svelte';
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
|
||||
const i18n = getContext<typeof import('$lib/i18n').default>('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: {
|
||||
|
|
|
|||
|
|
@ -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<typeof setTimeout> | 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
95
src/lib/components/common/RichTextInput/FileAttachment.ts
Normal file
95
src/lib/components/common/RichTextInput/FileAttachment.ts
Normal file
|
|
@ -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<any, any> }>({
|
||||
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()
|
||||
};
|
||||
};
|
||||
}
|
||||
});
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -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 @@
|
|||
<ResizableSidePanel
|
||||
bind:open={show}
|
||||
bind:width={panelWidth}
|
||||
minWidth={350}
|
||||
{minWidth}
|
||||
{side}
|
||||
{resizerId}
|
||||
{storageKey}
|
||||
subtleDivider={floating}
|
||||
minSiblingWidth={360}
|
||||
closeOnDragBelowMinWidth
|
||||
{closeOnDragBelowMinWidth}
|
||||
className="h-full z-10"
|
||||
>
|
||||
<div class="flex h-full max-h-full min-h-full">
|
||||
<div
|
||||
class="w-full bg-white dark:bg-gray-900 z-40 pointer-events-auto overflow-hidden scrollbar-hidden flex flex-col"
|
||||
class="w-full z-40 pointer-events-auto overflow-hidden scrollbar-hidden flex flex-col {floating
|
||||
? ''
|
||||
: 'bg-white dark:bg-gray-900'}"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
|
|
|
|||
74
src/lib/components/notes/NoteTabMenu.svelte
Normal file
74
src/lib/components/notes/NoteTabMenu.svelte
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<script lang="ts">
|
||||
import { getContext } from 'svelte';
|
||||
import type { NotePage } from '$lib/apis/notes';
|
||||
import Dropdown from '$lib/components/common/Dropdown.svelte';
|
||||
import DropdownMenu from '$lib/components/common/DropdownMenu.svelte';
|
||||
import EllipsisHorizontal from '$lib/components/icons/EllipsisHorizontal.svelte';
|
||||
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
|
||||
export let page: NotePage;
|
||||
export let pages: NotePage[];
|
||||
export let editable = false;
|
||||
export let busy = false;
|
||||
export let className = '';
|
||||
export let onRename: (page: NotePage) => void;
|
||||
export let onDelete: (page: NotePage) => void;
|
||||
export let onCreate: (parentId: string | null) => void;
|
||||
export let onMoveTo: (page: NotePage) => void;
|
||||
export let onMove: (id: string, direction: number) => void;
|
||||
export let onCopyLink: (id: string) => void;
|
||||
export let onDownload: (id: string, type: string) => void;
|
||||
$: siblings = pages.filter((p) => (p.parent_id ?? null) === (page.parent_id ?? null));
|
||||
$: index = siblings.findIndex((p) => p.id === page.id);
|
||||
</script>
|
||||
|
||||
<Dropdown align="start" closeOnSelect>
|
||||
<button
|
||||
class="{className} flex size-5 items-center justify-center rounded-lg text-gray-500 hover:text-gray-900 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5"
|
||||
disabled={busy}
|
||||
aria-label={$i18n.t('Page menu')}><EllipsisHorizontal className="size-3.5" /></button
|
||||
>
|
||||
<div slot="content">
|
||||
<DropdownMenu className="note-tab-options min-w-48 text-sm">
|
||||
{#if editable}
|
||||
<button class="w-full text-start px-3 py-1.5" on:click={() => onCreate(page.id)}
|
||||
>{$i18n.t('Add subpage')}</button
|
||||
>
|
||||
<button class="w-full text-start px-3 py-1.5" on:click={() => onRename(page)}
|
||||
>{$i18n.t('Rename page')}</button
|
||||
>
|
||||
{#if page.id !== 'main'}
|
||||
<button
|
||||
class="w-full text-start px-3 py-1.5 disabled:opacity-40"
|
||||
disabled={index === 0 || siblings[index - 1]?.id === 'main'}
|
||||
on:click={() => onMove(page.id, -1)}>{$i18n.t('Move up')}</button
|
||||
>
|
||||
<button
|
||||
class="w-full text-start px-3 py-1.5 disabled:opacity-40"
|
||||
disabled={index === siblings.length - 1}
|
||||
on:click={() => onMove(page.id, 1)}>{$i18n.t('Move down')}</button
|
||||
>
|
||||
<button class="w-full text-start px-3 py-1.5" on:click={() => onMoveTo(page)}
|
||||
>{$i18n.t('Move page…')}</button
|
||||
>
|
||||
{/if}
|
||||
{/if}
|
||||
<hr class="border-gray-100 dark:border-gray-800" />
|
||||
<button class="w-full text-start px-3 py-1.5" on:click={() => onCopyLink(page.id)}
|
||||
>{$i18n.t('Copy page link')}</button
|
||||
>
|
||||
{#each ['md', 'txt', 'pdf'] as format}
|
||||
<button class="w-full text-start px-3 py-1.5" on:click={() => onDownload(page.id, format)}
|
||||
>{$i18n.t('Download page')} (.{format})</button
|
||||
>
|
||||
{/each}
|
||||
{#if editable && page.id !== 'main'}
|
||||
<hr class="border-gray-100 dark:border-gray-800" />
|
||||
<button
|
||||
class="w-full text-start px-3 py-1.5 disabled:opacity-40"
|
||||
disabled={pages.length === 1}
|
||||
on:click={() => onDelete(page)}>{$i18n.t('Delete page')}</button
|
||||
>
|
||||
{/if}
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
</Dropdown>
|
||||
385
src/lib/components/notes/NoteTabs.svelte
Normal file
385
src/lib/components/notes/NoteTabs.svelte
Normal file
|
|
@ -0,0 +1,385 @@
|
|||
<script lang="ts">
|
||||
import { getContext, tick } from 'svelte';
|
||||
// @ts-expect-error The installed SortableJS package has no type declarations.
|
||||
import Sortable from 'sortablejs';
|
||||
import type { NotePage, NotePageOperation } from '$lib/apis/notes';
|
||||
import NativeSelect from '$lib/components/common/NativeSelect.svelte';
|
||||
import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
|
||||
import {
|
||||
tabRows,
|
||||
tabAncestors,
|
||||
tabBranchIds,
|
||||
siblingMove,
|
||||
pageDropMove,
|
||||
type PageDropPosition
|
||||
} from './utils';
|
||||
import ChevronRight from '$lib/components/icons/ChevronRight.svelte';
|
||||
import ChevronDown from '$lib/components/icons/ChevronDown.svelte';
|
||||
import Plus from '$lib/components/icons/Plus.svelte';
|
||||
import XMark from '$lib/components/icons/XMark.svelte';
|
||||
|
||||
import NoteTabMenu from './NoteTabMenu.svelte';
|
||||
const i18n = getContext<typeof import('$lib/i18n').default>('i18n');
|
||||
export let pages: NotePage[] = [];
|
||||
export let activeId = '';
|
||||
export let editable = false;
|
||||
export let busy = false;
|
||||
export let onSelect: (id: string) => Promise<void>;
|
||||
export let onAction: (operation: Omit<NotePageOperation, 'revision'>) => Promise<string | null>;
|
||||
export let onCopyLink: (id: string) => void;
|
||||
export let onDownload: (id: string, type: string) => void;
|
||||
export let onClose: () => void;
|
||||
let collapsed = new Set<string>();
|
||||
$: rows = tabRows(pages, collapsed);
|
||||
$: allRows = tabRows(pages);
|
||||
$: focusId = rows.some(({ page }) => page.id === activeId)
|
||||
? activeId
|
||||
: [...tabAncestors(pages, activeId)]
|
||||
.reverse()
|
||||
.find((id) => rows.some(({ page }) => page.id === id));
|
||||
$: revealActive(pages, activeId);
|
||||
let moving: NotePage | null = null;
|
||||
let showMove = false;
|
||||
let moveParent = '';
|
||||
$: movingBranch = moving ? tabBranchIds(pages, moving.id) : new Set<string>();
|
||||
$: moveOptions = [
|
||||
{ value: '', label: $i18n.t('Top level') },
|
||||
...allRows
|
||||
.filter(({ page }) => !movingBranch.has(page.id))
|
||||
.map(({ page, path }) => ({ value: page.id, label: path }))
|
||||
];
|
||||
function revealActive(currentPages: NotePage[], id: string) {
|
||||
const ancestors = tabAncestors(currentPages, id);
|
||||
if (ancestors.some((id) => collapsed.has(id)))
|
||||
collapsed = new Set([...collapsed].filter((id) => !ancestors.includes(id)));
|
||||
}
|
||||
function toggleBranch(id: string) {
|
||||
const next = new Set(collapsed);
|
||||
if (next.has(id)) next.delete(id);
|
||||
else next.add(id);
|
||||
collapsed = next;
|
||||
}
|
||||
function moveTo(page: NotePage) {
|
||||
moving = page;
|
||||
moveParent = page.parent_id ?? '';
|
||||
showMove = true;
|
||||
}
|
||||
|
||||
let renaming = '';
|
||||
let title = '';
|
||||
let renameInput: HTMLInputElement;
|
||||
let deleting: NotePage | null = null;
|
||||
let confirmDelete = false;
|
||||
|
||||
async function rename(page: NotePage) {
|
||||
renaming = page.id;
|
||||
title = page.title;
|
||||
await tick();
|
||||
renameInput?.focus();
|
||||
renameInput?.select();
|
||||
}
|
||||
async function commitRename() {
|
||||
if (!renaming) return;
|
||||
const id = renaming;
|
||||
renaming = '';
|
||||
if (title.trim() && title.trim() !== pages.find((p) => p.id === id)?.title) {
|
||||
await onAction({ action: 'rename', page_id: id, title: title.trim() });
|
||||
}
|
||||
}
|
||||
async function create(parentId: string | null = null) {
|
||||
let n = pages.length + 1;
|
||||
while (pages.some((p) => p.title === $i18n.t('Page {{number}}', { number: n }))) n++;
|
||||
const id = await onAction({
|
||||
action: 'create',
|
||||
parent_id: parentId,
|
||||
title: $i18n.t('Page {{number}}', { number: n })
|
||||
});
|
||||
if (id) {
|
||||
await tick();
|
||||
const page = pages.find((p) => p.id === id);
|
||||
if (page) await rename(page);
|
||||
}
|
||||
}
|
||||
function move(id: string, direction: number) {
|
||||
const operation = siblingMove(pages, id, direction);
|
||||
if (operation) return onAction(operation);
|
||||
}
|
||||
|
||||
let dropTarget: { id: string; position: PageDropPosition } | null = null;
|
||||
function sortable(node: HTMLElement, disabled: boolean) {
|
||||
let draggedId = '';
|
||||
let cancelled = false;
|
||||
function trackDrop(event: MouseEvent | TouchEvent) {
|
||||
if (!draggedId || cancelled) return;
|
||||
const point = 'touches' in event ? (event.touches[0] ?? event.changedTouches[0]) : event;
|
||||
const row =
|
||||
point &&
|
||||
document
|
||||
.elementFromPoint(point.clientX, point.clientY)
|
||||
?.closest<HTMLElement>('[data-page-id]');
|
||||
dropTarget = null;
|
||||
if (!row || !node.contains(row)) return;
|
||||
const id = row.dataset.pageId!;
|
||||
const rect = row.getBoundingClientRect();
|
||||
const offset = (point.clientY - rect.top) / rect.height;
|
||||
const position: PageDropPosition =
|
||||
offset < 0.25 ? 'before' : offset > 0.75 ? 'after' : 'inside';
|
||||
if (pageDropMove(pages, draggedId, id, position)) dropTarget = { id, position };
|
||||
}
|
||||
function cancelDrag(event: KeyboardEvent) {
|
||||
if (event.key === 'Escape' && draggedId) {
|
||||
cancelled = true;
|
||||
dropTarget = null;
|
||||
}
|
||||
}
|
||||
document.addEventListener('pointermove', trackDrop);
|
||||
document.addEventListener('touchmove', trackDrop, { passive: true });
|
||||
document.addEventListener('keydown', cancelDrag);
|
||||
const instance = new Sortable(node, {
|
||||
disabled,
|
||||
// Keep the tree stable while showing an explicit nesting or ordering target.
|
||||
sort: false,
|
||||
forceFallback: true,
|
||||
fallbackOnBody: true,
|
||||
fallbackTolerance: 4,
|
||||
ghostClass: 'opacity-40',
|
||||
draggable: '[data-page-id]:not([data-page-id="main"])',
|
||||
handle: '[role="treeitem"]',
|
||||
delay: 200,
|
||||
delayOnTouchOnly: true,
|
||||
onStart: (event: { item: HTMLElement }) => {
|
||||
draggedId = event.item.dataset.pageId!;
|
||||
cancelled = false;
|
||||
},
|
||||
onEnd: (event: { originalEvent?: MouseEvent | TouchEvent }) => {
|
||||
if (event.originalEvent) trackDrop(event.originalEvent);
|
||||
const target = dropTarget;
|
||||
const operation =
|
||||
!cancelled && !event.originalEvent?.type.includes('cancel') && target
|
||||
? pageDropMove(pages, draggedId, target.id, target.position)
|
||||
: null;
|
||||
draggedId = '';
|
||||
dropTarget = null;
|
||||
if (operation && target) {
|
||||
onAction(operation).then((id) => {
|
||||
if (id) collapsed = new Set([...collapsed].filter((id) => id !== operation.parent_id));
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
return {
|
||||
update: (disabled: boolean) => instance.option('disabled', disabled),
|
||||
destroy: () => {
|
||||
instance.destroy();
|
||||
document.removeEventListener('pointermove', trackDrop);
|
||||
document.removeEventListener('touchmove', trackDrop);
|
||||
document.removeEventListener('keydown', cancelDrag);
|
||||
}
|
||||
};
|
||||
}
|
||||
function navigate(event: KeyboardEvent, id: string) {
|
||||
const index = rows.findIndex(({ page }) => page.id === id);
|
||||
const row = rows[index];
|
||||
let target: string | undefined;
|
||||
if (event.key === 'ArrowRight') {
|
||||
if (row.hasChildren && collapsed.has(id)) toggleBranch(id);
|
||||
else if (row.hasChildren) target = rows[index + 1]?.page.id;
|
||||
} else if (event.key === 'ArrowLeft') {
|
||||
if (row.hasChildren && !collapsed.has(id)) toggleBranch(id);
|
||||
else target = row.page.parent_id ?? undefined;
|
||||
} else {
|
||||
const next =
|
||||
event.key === 'ArrowDown'
|
||||
? Math.min(index + 1, rows.length - 1)
|
||||
: event.key === 'ArrowUp'
|
||||
? Math.max(index - 1, 0)
|
||||
: event.key === 'Home'
|
||||
? 0
|
||||
: event.key === 'End'
|
||||
? rows.length - 1
|
||||
: -1;
|
||||
if (next < 0) return;
|
||||
target = rows[next].page.id;
|
||||
}
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (target)
|
||||
onSelect(target).then(() => document.getElementById(`note-page-tab-${target}`)?.focus());
|
||||
}
|
||||
</script>
|
||||
|
||||
<section id="note-tabs-panel" aria-label={$i18n.t('Pages')} class="flex h-full min-h-0 flex-col">
|
||||
<div class="flex h-8 shrink-0 items-center justify-between px-3 text-xs text-gray-500">
|
||||
<span>{$i18n.t('Pages')}</span>
|
||||
|
||||
<button
|
||||
class="flex size-5 items-center justify-center rounded-lg hover:text-gray-900 dark:hover:text-white"
|
||||
aria-label={$i18n.t('Close pages')}
|
||||
on:click={onClose}><XMark className="size-3.5" /></button
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="min-h-0 flex-1 overflow-y-auto px-2 pb-2">
|
||||
<div role="tree" aria-label={$i18n.t('Pages')} use:sortable={!editable || busy || !!renaming}>
|
||||
{#each rows as { page, depth, hasChildren, position, siblings, path } (page.id)}
|
||||
<div
|
||||
data-page-id={page.id}
|
||||
data-drop-position={dropTarget?.id === page.id ? dropTarget.position : undefined}
|
||||
style:margin-inline-start={`${Math.min(depth, 6) * 12}px`}
|
||||
class="page-row group relative flex items-center rounded-xl px-1 text-gray-800 dark:text-gray-200 transition {activeId ===
|
||||
page.id
|
||||
? 'bg-gray-50/60 dark:bg-gray-800/60'
|
||||
: 'hover:bg-gray-50/60 dark:hover:bg-gray-800/60'}"
|
||||
>
|
||||
{#if hasChildren}
|
||||
<button
|
||||
class="flex size-5 shrink-0 items-center justify-center rounded text-gray-400 hover:text-gray-700 dark:hover:text-gray-200"
|
||||
aria-label={$i18n.t(
|
||||
collapsed.has(page.id) ? 'Expand {{title}}' : 'Collapse {{title}}',
|
||||
{ title: page.title }
|
||||
)}
|
||||
aria-expanded={!collapsed.has(page.id)}
|
||||
on:click={() => toggleBranch(page.id)}
|
||||
>
|
||||
{#if collapsed.has(page.id)}<ChevronRight className="size-3" />{:else}<ChevronDown
|
||||
className="size-3"
|
||||
/>{/if}
|
||||
</button>
|
||||
{:else}<span class="size-5 shrink-0" aria-hidden="true"></span>{/if}
|
||||
{#if renaming === page.id}
|
||||
<input
|
||||
bind:this={renameInput}
|
||||
bind:value={title}
|
||||
maxlength="200"
|
||||
aria-label={$i18n.t('Page title')}
|
||||
class="h-[1.6875rem] min-w-0 flex-1 border-0 bg-transparent px-1 py-0 text-[0.8125rem] leading-5 font-normal outline-hidden focus:ring-0"
|
||||
on:blur={commitRename}
|
||||
on:keydown={(event) => {
|
||||
if (event.isComposing) return;
|
||||
if (event.key === 'Enter') {
|
||||
event.preventDefault();
|
||||
commitRename();
|
||||
}
|
||||
if (event.key === 'Escape') renaming = '';
|
||||
}}
|
||||
/>
|
||||
{:else}
|
||||
<button
|
||||
id={`note-page-tab-${page.id}`}
|
||||
role="treeitem"
|
||||
aria-level={depth + 1}
|
||||
aria-posinset={position}
|
||||
aria-setsize={siblings}
|
||||
aria-expanded={hasChildren ? !collapsed.has(page.id) : undefined}
|
||||
aria-selected={activeId === page.id}
|
||||
aria-controls="note-content-container"
|
||||
tabindex={focusId === page.id ? 0 : -1}
|
||||
disabled={busy}
|
||||
class="flex min-w-0 flex-1 items-center h-[1.6875rem] rounded-xl px-1 text-start text-[0.8125rem] leading-5 font-normal"
|
||||
title={path}
|
||||
on:click={() => onSelect(page.id)}
|
||||
on:keydown={(event) => navigate(event, page.id)}
|
||||
><span class="truncate">{page.title}</span></button
|
||||
>
|
||||
{/if}
|
||||
{#if editable}
|
||||
<button
|
||||
class="flex size-5 shrink-0 items-center justify-center rounded-lg text-gray-500 hover:text-gray-900 dark:hover:text-white {activeId ===
|
||||
page.id
|
||||
? ''
|
||||
: 'sm:opacity-0 sm:group-hover:opacity-100 sm:group-focus-within:opacity-100'}"
|
||||
aria-label={$i18n.t('Add subpage to {{title}}', { title: page.title })}
|
||||
title={$i18n.t('Add subpage')}
|
||||
disabled={busy}
|
||||
on:click={() => create(page.id)}
|
||||
><Plus className="size-3.5" strokeWidth="1.5" /></button
|
||||
>
|
||||
{/if}
|
||||
<NoteTabMenu
|
||||
{page}
|
||||
{pages}
|
||||
{editable}
|
||||
{busy}
|
||||
{onCopyLink}
|
||||
{onDownload}
|
||||
onRename={rename}
|
||||
onMove={move}
|
||||
onMoveTo={moveTo}
|
||||
onCreate={create}
|
||||
onDelete={(page) => {
|
||||
deleting = page;
|
||||
confirmDelete = true;
|
||||
}}
|
||||
className={activeId === page.id
|
||||
? ''
|
||||
: 'sm:opacity-0 sm:group-hover:opacity-100 sm:group-focus-within:opacity-100'}
|
||||
/>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{#if editable}
|
||||
<button
|
||||
class="mt-1 flex h-[1.6875rem] w-full items-center rounded-xl px-7 text-start text-[0.8125rem] text-gray-500 hover:bg-gray-50/60 hover:text-gray-900 dark:hover:bg-gray-800/60 dark:hover:text-gray-100 transition disabled:opacity-40"
|
||||
disabled={busy}
|
||||
on:click={() => create(null)}>{$i18n.t('Add page')}</button
|
||||
>
|
||||
{/if}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<ConfirmDialog
|
||||
bind:show={confirmDelete}
|
||||
title={$i18n.t('Delete page?')}
|
||||
on:confirm={() => deleting && onAction({ action: 'delete', page_id: deleting.id })}
|
||||
>
|
||||
<div>
|
||||
{$i18n.t('Delete "{{title}}"? This cannot be undone.', { title: deleting?.title ?? '' })}
|
||||
{#if deleting && pages.some((page) => page.parent_id === deleting.id)}<p class="mt-2">
|
||||
{$i18n.t('Its subpages will be kept and moved up one level.')}
|
||||
</p>{/if}
|
||||
</div>
|
||||
</ConfirmDialog>
|
||||
|
||||
<ConfirmDialog
|
||||
bind:show={showMove}
|
||||
title={$i18n.t('Move page')}
|
||||
confirmLabel={$i18n.t('Move')}
|
||||
on:confirm={() =>
|
||||
moving &&
|
||||
moveParent !== (moving.parent_id ?? '') &&
|
||||
onAction({ action: 'move', page_id: moving.id, parent_id: moveParent || null })}
|
||||
>
|
||||
<div class="text-sm text-gray-500">
|
||||
{$i18n.t('Choose a parent page. Subpages move with this page.')}
|
||||
</div>
|
||||
<label class="block mt-3 text-sm">
|
||||
<span>{$i18n.t('Parent page')}</span>
|
||||
<NativeSelect
|
||||
bind:value={moveParent}
|
||||
options={moveOptions}
|
||||
className="w-full mt-2 rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-900 outline-hidden"
|
||||
/>
|
||||
</label>
|
||||
</ConfirmDialog>
|
||||
|
||||
<style>
|
||||
.page-row[data-drop-position='inside'] {
|
||||
background-color: color-mix(in srgb, currentColor 8%, transparent);
|
||||
}
|
||||
.page-row[data-drop-position='before']::before,
|
||||
.page-row[data-drop-position='after']::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
inset-inline: 4px;
|
||||
height: 1px;
|
||||
border-radius: 1px;
|
||||
background: color-mix(in srgb, currentColor 20%, transparent);
|
||||
pointer-events: none;
|
||||
}
|
||||
.page-row[data-drop-position='before']::before {
|
||||
top: -1px;
|
||||
}
|
||||
.page-row[data-drop-position='after']::after {
|
||||
bottom: -1px;
|
||||
}
|
||||
</style>
|
||||
|
|
@ -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));
|
||||
}}
|
||||
>
|
||||
<div class="flex min-w-0 flex-1 items-center gap-2">
|
||||
<Tooltip content={note.title} className="min-w-0" placement="top-start">
|
||||
<a
|
||||
href={`/notes/${note.id}`}
|
||||
href={noteUrl(note)}
|
||||
dir="auto"
|
||||
class="focus-ring block h-[1.25rem] truncate rounded text-[0.8125rem] leading-5 text-gray-800 group-hover:underline focus-visible:underline dark:text-gray-200"
|
||||
>
|
||||
|
|
@ -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"
|
||||
>
|
||||
<div class="flex min-w-0 items-start">
|
||||
<a href={`/notes/${note.id}`} class="min-w-0 flex-1 pr-5">
|
||||
<a href={noteUrl(note)} class="min-w-0 flex-1 pr-5">
|
||||
<Tooltip content={note.title} placement="top-start">
|
||||
<div
|
||||
class="line-clamp-2 break-words text-xs text-gray-800 dark:text-gray-200"
|
||||
|
|
@ -840,7 +837,7 @@
|
|||
</div>
|
||||
|
||||
<a
|
||||
href={`/notes/${note.id}`}
|
||||
href={noteUrl(note)}
|
||||
class="mt-2 min-h-0 flex-1 overflow-hidden text-[0.6875rem] leading-4 text-gray-500 dark:text-gray-400"
|
||||
style="mask-image: linear-gradient(to bottom, black 65%, transparent 100%);"
|
||||
>
|
||||
|
|
@ -849,7 +846,7 @@
|
|||
</div>
|
||||
</a>
|
||||
|
||||
<a href={`/notes/${note.id}`} class="mt-2 block min-w-0">
|
||||
<a href={noteUrl(note)} class="mt-2 block min-w-0">
|
||||
<div
|
||||
class="flex min-w-0 w-full items-center justify-between gap-1 text-[0.5625rem] leading-3 text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
|
|
|
|||
|
|
@ -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 `<section data-note-page>${selected.length > 1 ? heading.outerHTML : ''}${page.content.html || marked.parse(page.content.md || '')}</section>`;
|
||||
})
|
||||
.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<NotePage, 'id' | 'title' | 'parent_id'>;
|
||||
|
||||
export function tabRows<T extends Tab>(pages: T[], collapsed = new Set<string>()) {
|
||||
const children = new Map<string | null, T[]>();
|
||||
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<string, string[]>();
|
||||
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<string>();
|
||||
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<NotePageOperation, 'revision'> | 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<NotePageOperation, 'revision'> | 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<string, { id: string; type: string; name: string; size: number }>();
|
||||
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()];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue