-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
heading.go
343 lines (300 loc) · 9.65 KB
/
heading.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// SiYuan - Build Your Eternal Digital Garden
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package model
import (
"errors"
"os"
"path"
"path/filepath"
"strings"
"github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
func (tx *Transaction) doFoldHeading(operation *Operation) (ret *TxErr) {
headingID := operation.ID
tree, err := tx.loadTree(headingID)
if nil != err {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}
childrenIDs := []string{} // 这里不能用 nil,否则折叠下方没内容的标题时会内核中断 https://github.com/siyuan-note/siyuan/issues/3643
heading := treenode.GetNodeInTree(tree, headingID)
if nil == heading {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}
children := treenode.HeadingChildren(heading)
for _, child := range children {
childrenIDs = append(childrenIDs, child.ID)
child.SetIALAttr("fold", "1")
child.SetIALAttr("heading-fold", "1")
}
heading.SetIALAttr("fold", "1")
if err = tx.writeTree(tree); nil != err {
return &TxErr{code: TxErrCodeWriteTree, msg: err.Error(), id: headingID}
}
IncSync()
cache.PutBlockIAL(headingID, parse.IAL2Map(heading.KramdownIAL))
for _, child := range children {
cache.PutBlockIAL(child.ID, parse.IAL2Map(child.KramdownIAL))
}
sql.UpsertTreeQueue(tree)
operation.RetData = childrenIDs
return
}
func (tx *Transaction) doUnfoldHeading(operation *Operation) (ret *TxErr) {
headingID := operation.ID
tree, err := tx.loadTree(headingID)
if nil != err {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}
heading := treenode.GetNodeInTree(tree, headingID)
if nil == heading {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}
children := treenode.HeadingChildren(heading)
for _, child := range children {
child.RemoveIALAttr("heading-fold")
child.RemoveIALAttr("fold")
}
heading.RemoveIALAttr("fold")
heading.RemoveIALAttr("heading-fold")
if err = tx.writeTree(tree); nil != err {
return &TxErr{code: TxErrCodeWriteTree, msg: err.Error(), id: headingID}
}
IncSync()
cache.PutBlockIAL(headingID, parse.IAL2Map(heading.KramdownIAL))
for _, child := range children {
cache.PutBlockIAL(child.ID, parse.IAL2Map(child.KramdownIAL))
}
sql.UpsertTreeQueue(tree)
luteEngine := NewLute()
operation.RetData = renderBlockDOMByNodes(children, luteEngine)
return
}
func Doc2Heading(srcID, targetID string, after bool) (srcTreeBox, srcTreePath string, err error) {
WaitForWritingFiles()
srcTree, _ := loadTreeByBlockID(srcID)
if nil == srcTree {
err = ErrBlockNotFound
return
}
subDir := filepath.Join(util.DataDir, srcTree.Box, strings.TrimSuffix(srcTree.Path, ".sy"))
if gulu.File.IsDir(subDir) {
if !util.IsEmptyDir(subDir) {
err = errors.New(Conf.Language(20))
return
}
if removeErr := os.Remove(subDir); nil != removeErr { // 移除空文件夹不会有副作用
logging.LogWarnf("remove empty dir [%s] failed: %s", subDir, removeErr)
}
}
targetTree, _ := loadTreeByBlockID(targetID)
if nil == targetTree {
err = ErrBlockNotFound
return
}
pivot := treenode.GetNodeInTree(targetTree, targetID)
if nil == pivot {
err = ErrBlockNotFound
return
}
if ast.NodeListItem == pivot.Type {
pivot = pivot.LastChild
}
pivotLevel := treenode.HeadingLevel(pivot)
deltaLevel := pivotLevel - treenode.TopHeadingLevel(srcTree) + 1
headingLevel := pivotLevel
if ast.NodeHeading == pivot.Type { // 平级插入
children := treenode.HeadingChildren(pivot)
if after {
if length := len(children); 0 < length {
pivot = children[length-1]
}
}
} else { // 子节点插入
headingLevel++
deltaLevel++
}
if 6 < headingLevel {
headingLevel = 6
}
srcTree.Root.RemoveIALAttr("type")
tagIAL := srcTree.Root.IALAttr("tags")
tags := strings.Split(tagIAL, ",")
srcTree.Root.RemoveIALAttr("tags")
heading := &ast.Node{ID: srcTree.Root.ID, Type: ast.NodeHeading, HeadingLevel: headingLevel, KramdownIAL: srcTree.Root.KramdownIAL}
heading.SetIALAttr("updated", util.CurrentTimeSecondsStr())
heading.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(srcTree.Root.IALAttr("title"))})
heading.Box, heading.Path = targetTree.Box, targetTree.Path
if "" != tagIAL && 0 < len(tags) {
// 带标签的文档块转换为标题块时将标签移动到标题块下方 https://github.com/siyuan-note/siyuan/issues/6550
tagPara := treenode.NewParagraph()
for i, tag := range tags {
if "" == tag {
continue
}
tagPara.AppendChild(&ast.Node{Type: ast.NodeTextMark, TextMarkType: "tag", TextMarkTextContent: tag})
if i < len(tags)-1 {
tagPara.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(" ")})
}
}
if nil != tagPara.FirstChild {
srcTree.Root.PrependChild(tagPara)
}
}
var nodes []*ast.Node
if after {
for c := srcTree.Root.LastChild; nil != c; c = c.Previous {
nodes = append(nodes, c)
}
} else {
for c := srcTree.Root.FirstChild; nil != c; c = c.Next {
nodes = append(nodes, c)
}
}
if !after {
pivot.InsertBefore(heading)
}
for _, n := range nodes {
if ast.NodeHeading == n.Type {
n.HeadingLevel = n.HeadingLevel + deltaLevel
if 6 < n.HeadingLevel {
n.HeadingLevel = 6
}
}
n.Box = targetTree.Box
n.Path = targetTree.Path
if after {
pivot.InsertAfter(n)
} else {
pivot.InsertBefore(n)
}
}
if after {
pivot.InsertAfter(heading)
}
if contentPivot := treenode.GetNodeInTree(targetTree, targetID); nil != contentPivot && ast.NodeParagraph == contentPivot.Type && nil == contentPivot.FirstChild { // 插入到空的段落块下
contentPivot.Unlink()
}
// 推送给前端移除旧文档
evt := util.NewCmdResult("removeDoc", 0, util.PushModeBroadcast)
evt.Data = map[string]interface{}{
"ids": []string{srcTree.Root.ID},
}
util.PushEvent(evt)
srcTreeBox, srcTreePath = srcTree.Box, srcTree.Path // 返回旧的文档块位置,前端后续会删除旧的文档块
targetTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
err = indexWriteJSONQueue(targetTree)
IncSync()
RefreshBacklink(srcTree.ID)
RefreshBacklink(targetTree.ID)
return
}
func Heading2Doc(srcHeadingID, targetBoxID, targetPath string) (srcRootBlockID, newTargetPath string, err error) {
WaitForWritingFiles()
srcTree, _ := loadTreeByBlockID(srcHeadingID)
if nil == srcTree {
err = ErrBlockNotFound
return
}
srcRootBlockID = srcTree.Root.ID
headingBlock, err := getBlock(srcHeadingID, srcTree)
if nil != err {
return
}
if nil == headingBlock {
err = ErrBlockNotFound
return
}
headingNode := treenode.GetNodeInTree(srcTree, srcHeadingID)
if nil == headingNode {
err = ErrBlockNotFound
return
}
box := Conf.Box(targetBoxID)
headingText := getNodeRefText(headingNode)
headingText = util.FilterFileName(headingText)
moveToRoot := "/" == targetPath
toHP := path.Join("/", headingText)
toFolder := "/"
if !moveToRoot {
toBlock := treenode.GetBlockTreeRootByPath(targetBoxID, targetPath)
if nil == toBlock {
err = ErrBlockNotFound
return
}
toHP = path.Join(toBlock.HPath, headingText)
toFolder = path.Join(path.Dir(targetPath), toBlock.ID)
}
newTargetPath = path.Join(toFolder, srcHeadingID+".sy")
if !box.Exist(toFolder) {
if err = box.MkdirAll(toFolder); nil != err {
return
}
}
// 折叠标题转换为文档时需要自动展开下方块 https://github.com/siyuan-note/siyuan/issues/2947
children := treenode.HeadingChildren(headingNode)
for _, child := range children {
child.RemoveIALAttr("heading-fold")
child.RemoveIALAttr("fold")
}
headingNode.RemoveIALAttr("fold")
luteEngine := util.NewLute()
newTree := &parse.Tree{Root: &ast.Node{Type: ast.NodeDocument, ID: srcHeadingID}, Context: &parse.Context{ParseOption: luteEngine.ParseOptions}}
children = treenode.HeadingChildren(headingNode)
for _, c := range children {
newTree.Root.AppendChild(c)
}
newTree.ID = srcHeadingID
newTree.Path = newTargetPath
newTree.HPath = toHP
headingNode.SetIALAttr("type", "doc")
headingNode.SetIALAttr("id", srcHeadingID)
headingNode.SetIALAttr("title", headingText)
newTree.Root.KramdownIAL = headingNode.KramdownIAL
topLevel := treenode.TopHeadingLevel(newTree)
for c := newTree.Root.FirstChild; nil != c; c = c.Next {
if ast.NodeHeading == c.Type {
c.HeadingLevel = c.HeadingLevel - topLevel + 1
if 6 < c.HeadingLevel {
c.HeadingLevel = 6
}
}
}
headingNode.Unlink()
srcTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
if nil == srcTree.Root.FirstChild {
srcTree.Root.AppendChild(treenode.NewParagraph())
}
if err = indexWriteJSONQueue(srcTree); nil != err {
return "", "", err
}
newTree.Box, newTree.Path = targetBoxID, newTargetPath
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
newTree.Root.Spec = "1"
if err = indexWriteJSONQueue(newTree); nil != err {
return "", "", err
}
IncSync()
RefreshBacklink(srcTree.ID)
RefreshBacklink(newTree.ID)
return
}