Add section grouping to shared note tabs

Notes can now be clustered into named sections in the tab bar: each tab
gets a 🏷 button to assign it to a section, and clicking a section's
label renames (or, given an empty name, ungroups) every page in it.

note_pages gains a `section` column. Since the app has no migration
framework, database.py adds the column to existing SQLite files at
startup via PRAGMA table_info + ALTER TABLE. core/notes.set_section()
keeps same-section pages contiguous by renumbering position whenever a
page's section changes, so the tab bar and the AI-facing title list
(now grouped under "## section" headings) can both treat "adjacent" as
"same group" without a separate ordering field.

Also fixes a bug in the tab rename input: ref="renameInput" sits inside
a nested v-for, which Vue 3 binds as an array rather than a single
element, so renameInput.value?.select() was throwing on every rename.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-20 00:28:36 +09:00
co-authored by Claude Opus 5
parent b1350eb8bc
commit 1c21fc3406
10 changed files with 388 additions and 68 deletions
+50 -2
View File
@@ -75,17 +75,65 @@ def next_position(db: DBSession) -> int:
return (last.position + 1) if last else 0
def set_section(db: DBSession, page_id: int, section: str | None) -> NotePage | None:
"""ページの所属セクションを変更し、同じセクションのページ群の直後に連続配置し直す。
タブバーはposition順に描画され、隣接して同じsectionが続く区間を1つのグループとして
まとめて表示する(app.api.notes / フロントのグルーピング表示を参照)。そのため
セクションを変更したページは、常に同じセクションの最後のページの直後に移動する。
"""
page = db.get(NotePage, page_id)
if not page:
return None
normalized = (section or "").strip() or None
ordered = [p for p in db.query(NotePage).order_by(NotePage.position, NotePage.id).all() if p.id != page_id]
if normalized is None:
insert_at = len(ordered)
else:
matching = [i for i, p in enumerate(ordered) if p.section == normalized]
insert_at = (matching[-1] + 1) if matching else len(ordered)
ordered.insert(insert_at, page)
page.section = normalized
for i, p in enumerate(ordered):
p.position = i
db.commit()
db.refresh(page)
return page
def rename_section(db: DBSession, old_name: str, new_name: str | None) -> int:
"""あるセクションに属する全ページのセクション名を一括で変更する。new_name が空ならグループ解除になる。
戻り値: 変更したページ数。
"""
normalized_new = (new_name or "").strip() or None
pages = db.query(NotePage).filter(NotePage.section == old_name).order_by(NotePage.position, NotePage.id).all()
for page in pages:
page.section = normalized_new
db.commit()
return len(pages)
def get_notes_context(db: DBSession) -> str | None:
"""共有ノートのタイトル一覧をシステムプロンプト用のテキストとして返す。"""
"""共有ノートのタイトル一覧をシステムプロンプト用のテキストとして返す。セクションごとに見出しを付ける。"""
pages = db.query(NotePage).order_by(NotePage.position, NotePage.id).all()
lines = [
"【共有ノート】",
"ユーザーとあなたが共同で編集できるノートです。以下のページがあります。",
"ユーザーとあなたが共同で編集できるノートです。以下のページがあります"
"(セクションはページをグループ化する見出しで、あれば併記します)。",
"",
]
if pages:
current_section = "__unset__" # どのページのsectionとも一致しない番兵
for page in pages:
if page.section != current_section:
current_section = page.section
if current_section:
lines.append(f"## {current_section}")
preview = " ".join(page.content.split())[:40]
suffix = f" — {preview}…" if preview else " — (空)"
lines.append(f"- {page.title}{suffix}")