Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add prev&next read #941

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 0 additions & 247 deletions conf/app.conf.example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

该文件是完整配置样例,请恢复

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经恢复

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恢复错误,且CI已验证失败

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经修改好。

This file was deleted.

88 changes: 86 additions & 2 deletions controllers/DocumentController.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ type DocumentController struct {
BaseController
}

// Document prev&next
type DocumentTreeFlatten struct {
DocumentId int `json:"id"`
DocumentName string `json:"text"`
// ParentId interface{} `json:"parent"`
Identify string `json:"identify"`
// BookIdentify string `json:"-"`
// Version int64 `json:"version"`
}

// 文档首页
func (c *DocumentController) Index() {
c.Prepare()
Expand Down Expand Up @@ -98,7 +108,6 @@ func (c *DocumentController) Index() {
c.Data["IS_DOCUMENT_INDEX"] = true
c.Data["Model"] = bookResult
c.Data["Result"] = template.HTML(tree)

}

// CheckPassword : Handles password verification for private documents,
Expand Down Expand Up @@ -139,6 +148,8 @@ func (c *DocumentController) Read() {
identify := c.Ctx.Input.Param(":key")
token := c.GetString("token")
id := c.GetString(":id")
logs.Info(identify)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处日志改为Debug比较好,功能稳定后建议删除

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经删除

logs.Info(id)

if identify == "" || id == "" {
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.item_not_exist"))
Expand Down Expand Up @@ -229,10 +240,46 @@ func (c *DocumentController) Read() {

if err != nil && err != orm.ErrNoRows {
logs.Error("生成项目文档树时出错 ->", err)

c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.build_doc_tree_error"))
}

// prev,next
treeJson, err := models.NewDocument().FindDocumentTree2(bookResult.BookId)
// logs.Info(treeJson)
res := getTreeRecursive(treeJson, 0)
// explain
// for i, v := range res {
// logs.Info(i, v.Children)
// }

flat := make([]DocumentTreeFlatten, 0)
Flatten(res, &flat)
// flattened := Flatten(res, &flat)
// for i, v := range *flattened {
var index int
for i, v := range flat {
logs.Info(i, v)
if v.Identify == id {
logs.Info(i, v)
index = i
}
}
// logs.Info(flat[index].Identify)
// logs.Info(flat[index].Identify)

if index == 0 {
c.Data["PrevName"] = "没有了"
} else {
c.Data["PrevPath"] = identify + "/" + flat[index-1].Identify
c.Data["PrevName"] = flat[index-1].DocumentName
}
if index == len(flat)-1 {
c.Data["NextName"] = "没有了"
} else {
c.Data["NextPath"] = identify + "/" + flat[index+1].Identify
c.Data["NextName"] = flat[index+1].DocumentName
}

c.Data["Description"] = utils.AutoSummary(doc.Release, 120)

c.Data["Model"] = bookResult
Expand All @@ -249,6 +296,43 @@ func (c *DocumentController) Read() {
} else if doc.IsOpen == 2 {
c.Data["FoldSetting"] = "empty"
}

// c.Data["json"] = flat
// c.ServeJSON()
}

// 递归得到树状结构体
func getTreeRecursive(list []*models.DocumentTree, parentId int) (res []*models.DocumentTree) {
// res := make([]*models.DocumentTree, 0)
for _, v := range list {
// logs.Info(v)
if v.ParentId == parentId {
v.Children = getTreeRecursive(list, v.DocumentId)
res = append(res, v)
}
}
return res
}

// 递归将树状结构体转换为扁平结构体数组
// func Flatten(list []*models.DocumentTree, flattened *[]DocumentTreeFlatten) (flatten *[]DocumentTreeFlatten) {
func Flatten(list []*models.DocumentTree, flattened *[]DocumentTreeFlatten) {
// Treeslice := make([]*DocumentTreeFlatten, 0)
for _, v := range list {
tree := make([]DocumentTreeFlatten, 1)
tree[0].DocumentId = v.DocumentId
tree[0].DocumentName = v.DocumentName
tree[0].Identify = v.Identify
// logs.Info(v.DocumentName)
*flattened = append(*flattened, tree...)
logs.Info(flattened)
if len(v.Children) > 0 {
// logs.Info(v.Children)
Flatten(v.Children, flattened)
}
}
// return flattened
return
}

// 编辑文档
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/lib/pq v1.10.5
github.com/lifei6671/gocaptcha v0.2.0
github.com/mattn/go-runewidth v0.0.13
github.com/mattn/go-sqlite3 v1.14.15
github.com/mattn/go-sqlite3 v1.14.17
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/russross/blackfriday/v2 v2.1.0
)
Expand All @@ -32,6 +32,8 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -50,4 +52,6 @@ require (
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/sqlite v1.5.5 // indirect
gorm.io/gorm v1.25.9 // indirect
)
Loading