mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-03-07 21:22:34 +01:00
♻️ Adjust template and widget search data structure (#17142)
This commit is contained in:
parent
87bd6267b7
commit
10dfb0855f
4 changed files with 35 additions and 23 deletions
|
|
@ -1455,7 +1455,7 @@ export class Toolbar {
|
|||
k: inputElement.value,
|
||||
}, (response) => {
|
||||
let searchHTML = "";
|
||||
response.data.blocks.forEach((item: { path: string, content: string }, index: number) => {
|
||||
response.data.templates.forEach((item: { path: string, content: string }, index: number) => {
|
||||
searchHTML += `<div data-value="${item.path}" class="b3-list-item--hide-action b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">
|
||||
<span class="b3-list-item__text">${item.content}</span>`;
|
||||
/// #if !BROWSER
|
||||
|
|
@ -1468,7 +1468,7 @@ export class Toolbar {
|
|||
</span></div>`;
|
||||
});
|
||||
listElement.innerHTML = searchHTML || `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
|
||||
const currentPath = response.data.blocks[0]?.path;
|
||||
const currentPath = response.data.templates[0]?.path;
|
||||
if (previewPath === currentPath) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -1543,7 +1543,7 @@ export class Toolbar {
|
|||
k: "",
|
||||
}, (response) => {
|
||||
let html = "";
|
||||
response.data.blocks.forEach((item: { path: string, content: string }, index: number) => {
|
||||
response.data.templates.forEach((item: { path: string, content: string }, index: number) => {
|
||||
html += `<div data-value="${item.path}" class="b3-list-item--hide-action b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">
|
||||
<span class="b3-list-item__text">${item.content}</span>`;
|
||||
/// #if !BROWSER
|
||||
|
|
@ -1601,8 +1601,8 @@ export class Toolbar {
|
|||
k: inputElement.value,
|
||||
}, (response) => {
|
||||
let searchHTML = "";
|
||||
response.data.blocks.forEach((item: { path: string, content: string, name: string }, index: number) => {
|
||||
searchHTML += `<div data-value="${item.path}" data-content="${item.content}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">
|
||||
response.data.widgets.forEach((item: { content: string, name: string }, index: number) => {
|
||||
searchHTML += `<div data-content="${item.content}" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}">
|
||||
${item.name}
|
||||
<span class="b3-list-item__meta">${item.content}</span>
|
||||
</div>`;
|
||||
|
|
@ -1627,7 +1627,7 @@ export class Toolbar {
|
|||
k: "",
|
||||
}, (response) => {
|
||||
let html = "";
|
||||
response.data.blocks.forEach((item: { content: string, name: string }, index: number) => {
|
||||
response.data.widgets.forEach((item: { content: string, name: string }, index: number) => {
|
||||
html += `<div class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}" data-content="${item.content}">
|
||||
${item.name}
|
||||
<span class="b3-list-item__meta">${item.content}</span>
|
||||
|
|
|
|||
|
|
@ -188,10 +188,10 @@ func searchWidget(c *gin.Context) {
|
|||
}
|
||||
|
||||
keyword := arg["k"].(string)
|
||||
blocks := model.SearchWidget(keyword)
|
||||
widgets := model.SearchWidget(keyword)
|
||||
ret.Data = map[string]interface{}{
|
||||
"blocks": blocks,
|
||||
"k": keyword,
|
||||
"widgets": widgets,
|
||||
"k": keyword,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -223,10 +223,10 @@ func searchTemplate(c *gin.Context) {
|
|||
}
|
||||
|
||||
keyword := arg["k"].(string)
|
||||
blocks := model.SearchTemplate(keyword)
|
||||
templates := model.SearchTemplate(keyword)
|
||||
ret.Data = map[string]interface{}{
|
||||
"blocks": blocks,
|
||||
"k": keyword,
|
||||
"templates": templates,
|
||||
"k": keyword,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -42,6 +42,12 @@ import (
|
|||
"github.com/xrash/smetrics"
|
||||
)
|
||||
|
||||
// TemplateSearchResult 描述了模板搜索结果。
|
||||
type TemplateSearchResult struct {
|
||||
Path string `json:"path"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func RenderGoTemplate(templateContent string) (ret string, err error) {
|
||||
tmpl := template.New("")
|
||||
tplFuncMap := filesys.BuiltInTemplateFuncs()
|
||||
|
|
@ -70,8 +76,8 @@ func RemoveTemplate(p string) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func SearchTemplate(keyword string) (ret []*Block) {
|
||||
ret = []*Block{}
|
||||
func SearchTemplate(keyword string) (ret []*TemplateSearchResult) {
|
||||
ret = []*TemplateSearchResult{}
|
||||
|
||||
templates := filepath.Join(util.DataDir, "templates")
|
||||
if !util.IsPathRegularDirOrSymlinkDir(templates) {
|
||||
|
|
@ -90,7 +96,7 @@ func SearchTemplate(keyword string) (ret []*Block) {
|
|||
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
type result struct {
|
||||
block *Block
|
||||
item *TemplateSearchResult
|
||||
score float64
|
||||
}
|
||||
var results []*result
|
||||
|
|
@ -133,8 +139,8 @@ func SearchTemplate(keyword string) (ret []*Block) {
|
|||
content = strings.TrimSuffix(content, ".md")
|
||||
content = filepath.ToSlash(content)
|
||||
_, content = search.MarkText(content, strings.Join(keywords, search.TermSep), 32, Conf.Search.CaseSensitive)
|
||||
b := &Block{Path: path, Content: content}
|
||||
results = append(results, &result{block: b, score: score})
|
||||
b := &TemplateSearchResult{Path: path, Content: content}
|
||||
results = append(results, &result{item: b, score: score})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
|
@ -159,8 +165,8 @@ func SearchTemplate(keyword string) (ret []*Block) {
|
|||
if hit {
|
||||
content = filepath.ToSlash(content)
|
||||
_, content = search.MarkText(content, strings.Join(keywords, search.TermSep), 32, Conf.Search.CaseSensitive)
|
||||
b := &Block{Path: filepath.Join(templates, group.Name()), Content: content}
|
||||
results = append(results, &result{block: b, score: score})
|
||||
b := &TemplateSearchResult{Path: filepath.Join(templates, group.Name()), Content: content}
|
||||
results = append(results, &result{item: b, score: score})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -169,7 +175,7 @@ func SearchTemplate(keyword string) (ret []*Block) {
|
|||
return results[i].score > results[j].score
|
||||
})
|
||||
for _, r := range results {
|
||||
ret = append(ret, r.block)
|
||||
ret = append(ret, r.item)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,8 +26,14 @@ import (
|
|||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
func SearchWidget(keyword string) (ret []*Block) {
|
||||
ret = []*Block{}
|
||||
// WidgetSearchResult 描述了挂件搜索结果。
|
||||
type WidgetSearchResult struct {
|
||||
Name string `json:"name"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func SearchWidget(keyword string) (ret []*WidgetSearchResult) {
|
||||
ret = []*WidgetSearchResult{}
|
||||
widgetsDir := filepath.Join(util.DataDir, "widgets")
|
||||
entries, err := os.ReadDir(widgetsDir)
|
||||
if err != nil {
|
||||
|
|
@ -55,7 +61,7 @@ func SearchWidget(keyword string) (ret []*Block) {
|
|||
|
||||
widgets = filterWidgets(widgets, k)
|
||||
for _, widget := range widgets {
|
||||
b := &Block{
|
||||
b := &WidgetSearchResult{
|
||||
Name: bazaar.GetPreferredName(widget.Package),
|
||||
Content: widget.Name,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue