refactor: unify dropdown menu styles across components
- Removed redundant dropdown menu styles from various components and centralized them in a new `dropdown-menu.less` file. - Updated components to use the unified styles, ensuring consistency in appearance and behavior across the application. This change simplifies maintenance and enhances the visual coherence of dropdown menus.
This commit is contained in:
@@ -1,241 +0,0 @@
|
||||
# Manual Knowledge Download Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED: Use superpowers:subagent-driven-development (if subagents available) or superpowers:executing-plans to implement this plan. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Allow users to download `manual` type knowledge items as `.md` files via the existing download endpoint.
|
||||
|
||||
**Architecture:** Add an early-return branch in `GetKnowledgeFile` that, for manual knowledge, reads `Metadata.content` from the database record and streams it as a `.md` file — no new routes or handlers needed. The frontend adds a download button to the manual UI block and fixes the `link.download` attribute to include the `.md` extension.
|
||||
|
||||
**Tech Stack:** Go 1.24 (Gin, GORM), Vue 3 + TypeScript (TDesign UI, Pinia)
|
||||
|
||||
**Spec:** `docs/superpowers/specs/2026-03-16-manual-knowledge-download-design.md`
|
||||
|
||||
---
|
||||
|
||||
## Files Changed
|
||||
|
||||
| Action | Path | What changes |
|
||||
|--------|------|--------------|
|
||||
| Modify | `internal/application/service/knowledge.go` | Add manual branch in `GetKnowledgeFile` (lines 2408–2425) |
|
||||
| Modify | `frontend/src/components/doc-content.vue` | Add download button to manual block (line 694–700); fix `link.download` in `downloadFile()` (line 630) |
|
||||
|
||||
No new files are created.
|
||||
|
||||
---
|
||||
|
||||
## Chunk 1: Backend — GetKnowledgeFile manual branch
|
||||
|
||||
### Task 1: Add manual type support to GetKnowledgeFile
|
||||
|
||||
**Files:**
|
||||
- Modify: `internal/application/service/knowledge.go:2408-2425`
|
||||
|
||||
- [ ] **Step 1: Verify the current state of GetKnowledgeFile**
|
||||
|
||||
Open `internal/application/service/knowledge.go` and confirm the method looks like this (lines 2408–2425):
|
||||
|
||||
```go
|
||||
// GetKnowledgeFile retrieves the physical file associated with a knowledge entry
|
||||
func (s *knowledgeService) GetKnowledgeFile(ctx context.Context, id string) (io.ReadCloser, string, error) {
|
||||
// Get knowledge record
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
knowledge, err := s.repo.GetKnowledgeByID(ctx, tenantID, id)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
// Resolve KB-level file service with FilePath fallback protection
|
||||
kb, _ := s.kbService.GetKnowledgeBaseByID(ctx, knowledge.KnowledgeBaseID)
|
||||
file, err := s.resolveFileServiceForPath(ctx, kb, knowledge.FilePath).GetFile(ctx, knowledge.FilePath)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return file, knowledge.FileName, nil
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Add the manual branch**
|
||||
|
||||
Replace the body of `GetKnowledgeFile` so it reads:
|
||||
|
||||
```go
|
||||
// GetKnowledgeFile retrieves the physical file associated with a knowledge entry
|
||||
func (s *knowledgeService) GetKnowledgeFile(ctx context.Context, id string) (io.ReadCloser, string, error) {
|
||||
// Get knowledge record
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
knowledge, err := s.repo.GetKnowledgeByID(ctx, tenantID, id)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
// Manual knowledge stores content in Metadata — stream it directly as a .md file.
|
||||
if knowledge.IsManual() {
|
||||
meta, err := knowledge.ManualMetadata()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
// ManualMetadata returns (nil, nil) when Metadata column is empty; treat as empty content.
|
||||
content := ""
|
||||
if meta != nil {
|
||||
content = meta.Content
|
||||
}
|
||||
// Sanitize title: strip characters that are invalid or dangerous in HTTP headers / filenames.
|
||||
safeName := strings.NewReplacer(
|
||||
"\n", "", "\r", "", "/", "-", "\\", "-", "\"", "'",
|
||||
).Replace(knowledge.Title)
|
||||
filename := safeName + ".md"
|
||||
return io.NopCloser(strings.NewReader(content)), filename, nil
|
||||
}
|
||||
|
||||
// Resolve KB-level file service with FilePath fallback protection
|
||||
kb, _ := s.kbService.GetKnowledgeBaseByID(ctx, knowledge.KnowledgeBaseID)
|
||||
file, err := s.resolveFileServiceForPath(ctx, kb, knowledge.FilePath).GetFile(ctx, knowledge.FilePath)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return file, knowledge.FileName, nil
|
||||
}
|
||||
```
|
||||
|
||||
The `strings` package is already imported in this file. `io` is also already imported.
|
||||
|
||||
- [ ] **Step 3: Verify imports are present**
|
||||
|
||||
Confirm that `"io"` and `"strings"` are already in the import block at the top of `internal/application/service/knowledge.go`. They are — do **not** add duplicate imports.
|
||||
|
||||
- [ ] **Step 4: Verify the build compiles**
|
||||
|
||||
Run from the repo root:
|
||||
|
||||
```bash
|
||||
go build ./internal/application/service/...
|
||||
```
|
||||
|
||||
Expected: exits 0, no output.
|
||||
|
||||
- [ ] **Step 5: Run the Go test suite**
|
||||
|
||||
```bash
|
||||
go test -v ./...
|
||||
```
|
||||
|
||||
Expected: all existing tests pass (there are no tests for `GetKnowledgeFile` yet — that's fine, the suite should be green overall).
|
||||
|
||||
- [ ] **Step 6: Commit**
|
||||
|
||||
```bash
|
||||
git add internal/application/service/knowledge.go
|
||||
git commit -m "feat: support manual knowledge download in GetKnowledgeFile"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Chunk 2: Frontend — download button + filename fix
|
||||
|
||||
### Task 2: Add download button to manual knowledge UI block
|
||||
|
||||
**Files:**
|
||||
- Modify: `frontend/src/components/doc-content.vue:694-700`
|
||||
|
||||
- [ ] **Step 1: Locate the manual block in the template**
|
||||
|
||||
In `frontend/src/components/doc-content.vue`, find lines 694–700:
|
||||
|
||||
```vue
|
||||
<!-- 手动创建类型专属区域 -->
|
||||
<div v-else-if="details.type === 'manual'" class="manual_box">
|
||||
<span class="label">{{ $t('knowledgeBase.documentTitle') }}</span>
|
||||
<div class="manual_title_box">
|
||||
<span class="manual_title">{{ details.title }}</span>
|
||||
</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Add the download button**
|
||||
|
||||
Replace those lines with:
|
||||
|
||||
```vue
|
||||
<!-- 手动创建类型专属区域 -->
|
||||
<div v-else-if="details.type === 'manual'" class="manual_box">
|
||||
<span class="label">{{ $t('knowledgeBase.documentTitle') }}</span>
|
||||
<div class="manual_title_box">
|
||||
<span class="manual_title">{{ details.title }}</span>
|
||||
</div>
|
||||
<div class="icon_box" @click="downloadFile()">
|
||||
<img class="download_box" src="@/assets/img/download.svg" alt="">
|
||||
</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
The `.icon_box` and `.download_box` CSS classes are already defined in this component (used by the `file` type block above).
|
||||
|
||||
- [ ] **Step 3: Fix the link.download attribute and a pre-existing removeChild bug in downloadFile()**
|
||||
|
||||
In the same file, locate `downloadFile()` at lines 619–641.
|
||||
|
||||
**Fix 1 — filename extension (line 630).** The current line is:
|
||||
|
||||
```ts
|
||||
link.setAttribute("download", props.details.title);
|
||||
```
|
||||
|
||||
Replace it with:
|
||||
|
||||
```ts
|
||||
link.setAttribute("download", props.details.type === 'manual' ? props.details.title + '.md' : props.details.title);
|
||||
```
|
||||
|
||||
**Fix 2 — pre-existing removeChild bug (line 633).** The link is created but never appended to `document.body`, so `document.body.removeChild(link)` throws a DOM exception at runtime. While fixing this is strictly out of scope, you will be touching this function anyway, so add the missing `appendChild` call right before `link.click()`:
|
||||
|
||||
Before `link.click()`, add:
|
||||
```ts
|
||||
document.body.appendChild(link);
|
||||
```
|
||||
|
||||
The full corrected block (lines 627–635) should read:
|
||||
|
||||
```ts
|
||||
const link = document.createElement("a");
|
||||
link.style.display = "none";
|
||||
link.setAttribute("href", url.value);
|
||||
link.setAttribute("download", props.details.type === 'manual' ? props.details.title + '.md' : props.details.title);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
nextTick(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url.value);
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Run TypeScript type check**
|
||||
|
||||
```bash
|
||||
cd frontend && npm run type-check
|
||||
```
|
||||
|
||||
Expected: exits 0, no type errors.
|
||||
|
||||
- [ ] **Step 5: Commit**
|
||||
|
||||
```bash
|
||||
git add frontend/src/components/doc-content.vue
|
||||
git commit -m "feat: add download button for manual knowledge in doc-content"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Manual Smoke Test (after both tasks)
|
||||
|
||||
Start the dev server and verify end-to-end:
|
||||
|
||||
1. Open a knowledge base that contains a `manual` type knowledge item.
|
||||
2. Navigate to the knowledge detail page — confirm the download icon is visible (same icon as file type).
|
||||
3. Click the download icon.
|
||||
4. Verify the browser downloads a file named `{title}.md`.
|
||||
5. Open the file and confirm the contents match the Markdown content in the editor.
|
||||
6. Edge cases to spot-check:
|
||||
- Knowledge item with empty content → downloads an empty `.md` file (not an error).
|
||||
- Knowledge item whose title contains `/` or `"` → filename has `-` or `'` substituted respectively.
|
||||
@@ -1,132 +0,0 @@
|
||||
# Manual Knowledge Download — Design Spec
|
||||
|
||||
**Date:** 2026-03-16
|
||||
**Status:** Approved
|
||||
|
||||
---
|
||||
|
||||
## Background
|
||||
|
||||
WeKnora supports multiple knowledge types. The `manual` type stores content as Markdown in the database `Metadata.content` field — there is no physical file on disk or in object storage. The existing download endpoint (`GET /api/v1/knowledge/:id/download`) only works for `file` type knowledge; calling it for `manual` type would silently fail with an internal error because `FilePath` is empty.
|
||||
|
||||
The frontend `doc-content.vue` component already omits the download button for `manual` type knowledge. This spec describes how to add proper download support.
|
||||
|
||||
---
|
||||
|
||||
## Goal
|
||||
|
||||
Allow users to download `manual` type knowledge as a `.md` file whose content is the Markdown stored in `Metadata.content` and whose filename is `{title}.md`.
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
### Backend — `internal/application/service/knowledge.go`
|
||||
|
||||
**Method:** `GetKnowledgeFile(ctx, id) (io.ReadCloser, string, error)`
|
||||
|
||||
Add an early-return branch before the existing file-service lookup:
|
||||
|
||||
```go
|
||||
if knowledge.IsManual() {
|
||||
meta, err := knowledge.ManualMetadata()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
// ManualMetadata returns (nil, nil) when Metadata column is empty;
|
||||
// treat as empty content rather than an error.
|
||||
content := ""
|
||||
if meta != nil {
|
||||
content = meta.Content
|
||||
}
|
||||
// Sanitize title for use as a filename: strip newlines and path separators.
|
||||
safeName := strings.NewReplacer(
|
||||
"\n", "", "\r", "", "/", "-", "\\", "-", "\"", "'",
|
||||
).Replace(knowledge.Title)
|
||||
filename := safeName + ".md"
|
||||
return io.NopCloser(strings.NewReader(content)), filename, nil
|
||||
}
|
||||
```
|
||||
|
||||
- No new routes, handlers, or services required.
|
||||
- All existing permission checks, logging, and error middleware remain intact.
|
||||
- The handler sets `Content-Disposition: attachment; filename="{safeName}.md"` via the existing streaming logic.
|
||||
|
||||
#### Non-ASCII filenames in `Content-Disposition`
|
||||
|
||||
The existing handler writes the filename unquoted and without RFC 5987 encoding (`filename*=UTF-8''...`). This is a **known limitation inherited from the existing file-type download path** and is out of scope for this change. Most modern browsers (Chrome, Edge, Firefox) tolerate unquoted UTF-8 values in practice. A follow-up issue should address RFC 5987 encoding for all download types.
|
||||
|
||||
### Frontend — `frontend/src/components/doc-content.vue`
|
||||
|
||||
Two changes are required:
|
||||
|
||||
**1. Add download button to the `manual` UI block:**
|
||||
|
||||
```vue
|
||||
<div v-else-if="details.type === 'manual'" class="manual_box">
|
||||
<span class="label">{{ $t('knowledgeBase.documentTitle') }}</span>
|
||||
<div class="manual_title_box">
|
||||
<span class="manual_title">{{ details.title }}</span>
|
||||
</div>
|
||||
<!-- NEW: download button, same styling as file type -->
|
||||
<div class="icon_box" @click="downloadFile()">
|
||||
<img class="download_box" src="@/assets/img/download.svg" alt="">
|
||||
</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
**2. Fix `link.download` attribute in `downloadFile()` for manual type:**
|
||||
|
||||
The existing `downloadFile()` function sets `link.setAttribute("download", props.details.title)` without an extension. Browsers use the `download` attribute — not `Content-Disposition` — as the saved filename when `URL.createObjectURL` is used. For `manual` type, append `.md`:
|
||||
|
||||
```ts
|
||||
const filename = details.value.type === 'manual'
|
||||
? details.value.title + '.md'
|
||||
: details.value.title
|
||||
link.setAttribute('download', filename)
|
||||
```
|
||||
|
||||
`downloadFile()` otherwise calls `downKnowledgeDetails(id)` → `GET /api/v1/knowledge/:id/download` unchanged.
|
||||
|
||||
---
|
||||
|
||||
## Data Flow
|
||||
|
||||
```
|
||||
User clicks download
|
||||
→ downloadFile() [frontend, sets link.download = "{title}.md" for manual type]
|
||||
→ GET /api/v1/knowledge/:id/download
|
||||
→ DownloadKnowledgeFile handler [validates access]
|
||||
→ GetKnowledgeFile service
|
||||
→ if manual: sanitize title, read Metadata.content (empty string if nil)
|
||||
→ return (ReadCloser, "{safeName}.md", nil)
|
||||
→ handler streams response with Content-Disposition header
|
||||
→ browser saves "{title}.md"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Side Effects
|
||||
|
||||
`PreviewKnowledgeFile` also calls `GetKnowledgeFile` internally. After this change, preview requests for `manual` items will also succeed (returning the Markdown content with `text/markdown` MIME type via the existing `mimeTypeByExt(".md")` lookup). This is a desirable side effect and requires no additional changes.
|
||||
|
||||
---
|
||||
|
||||
## Error Handling
|
||||
|
||||
| Scenario | Behaviour |
|
||||
|----------|-----------|
|
||||
| `ManualMetadata()` parse error | Returns internal server error (existing error middleware handles it) |
|
||||
| `ManualMetadata()` returns `nil` (empty Metadata column) | Treated as empty content; returns a valid empty `.md` file |
|
||||
| Empty content (`""`) | Returns a valid empty `.md` file — not an error |
|
||||
| Non-existent knowledge ID | Existing 404 handling unchanged |
|
||||
| Insufficient permissions | Existing auth middleware unchanged |
|
||||
|
||||
---
|
||||
|
||||
## Out of Scope
|
||||
|
||||
- RFC 5987 encoding for non-ASCII `Content-Disposition` filenames (affects all download types, tracked separately)
|
||||
- HTML / PDF / plain-text export formats
|
||||
- Batch download of multiple knowledge items
|
||||
- Version history download
|
||||
Reference in New Issue
Block a user