-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
outline.go
300 lines (263 loc) · 7.45 KB
/
outline.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
// SiYuan - Refactor your thinking
// 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 (
"time"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/emirpasic/gods/stacks/linkedliststack"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
func (tx *Transaction) doMoveOutlineHeading(operation *Operation) (ret *TxErr) {
headingID := operation.ID
previousID := operation.PreviousID
parentID := operation.ParentID
tree, err := tx.loadTree(headingID)
if nil != err {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}
operation.RetData = tree.Root.ID
if headingID == parentID || headingID == previousID {
return
}
heading := treenode.GetNodeInTree(tree, headingID)
if nil == heading {
return &TxErr{code: TxErrCodeBlockNotFound, id: headingID}
}
if ast.NodeDocument != heading.Parent.Type {
// 仅支持文档根节点下第一层标题,不支持容器块内标题
util.PushMsg(Conf.language(240), 5000)
return
}
headings := []*ast.Node{}
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
headings = append(headings, n)
}
return ast.WalkContinue
})
headingChildren := treenode.HeadingChildren(heading)
if "" != previousID {
previousHeading := treenode.GetNodeInTree(tree, previousID)
if nil == previousHeading {
return &TxErr{code: TxErrCodeBlockNotFound, id: previousID}
}
if ast.NodeDocument != previousHeading.Parent.Type {
// 仅支持文档根节点下第一层标题,不支持容器块内标题
util.PushMsg(Conf.language(240), 5000)
return
}
for _, h := range headingChildren {
if h.ID == previousID {
// 不能移动到自己的子标题下
util.PushMsg(Conf.language(241), 5000)
return
}
}
generateOpTypeHistory(tree, HistoryOpOutline)
targetNode := previousHeading
previousHeadingChildren := treenode.HeadingChildren(previousHeading)
if 0 < len(previousHeadingChildren) {
targetNode = previousHeadingChildren[len(previousHeadingChildren)-1]
}
for _, h := range headingChildren {
if h.ID == targetNode.ID {
// 目标节点是当前标题的子节点,不需要移动
return
}
}
diffLevel := heading.HeadingLevel - previousHeading.HeadingLevel
heading.HeadingLevel = previousHeading.HeadingLevel
for i := len(headingChildren) - 1; i >= 0; i-- {
child := headingChildren[i]
if ast.NodeHeading == child.Type {
child.HeadingLevel -= diffLevel
if 6 < child.HeadingLevel {
child.HeadingLevel = 6
}
}
targetNode.InsertAfter(child)
}
targetNode.InsertAfter(heading)
} else if "" != parentID {
parentHeading := treenode.GetNodeInTree(tree, parentID)
if nil == parentHeading {
return &TxErr{code: TxErrCodeBlockNotFound, id: parentID}
}
if ast.NodeDocument != parentHeading.Parent.Type {
// 仅支持文档根节点下第一层标题,不支持容器块内标题
util.PushMsg(Conf.language(240), 5000)
return
}
for _, h := range headingChildren {
if h.ID == parentID {
// 不能移动到自己的子标题下
util.PushMsg(Conf.language(241), 5000)
return
}
}
generateOpTypeHistory(tree, HistoryOpOutline)
targetNode := parentHeading
parentHeadingChildren := treenode.HeadingChildren(parentHeading)
// 找到下方第一个非标题节点
var tmp []*ast.Node
for _, child := range parentHeadingChildren {
if ast.NodeHeading == child.Type {
break
}
tmp = append(tmp, child)
}
parentHeadingChildren = tmp
if 0 < len(parentHeadingChildren) {
for _, child := range parentHeadingChildren {
if child.ID == headingID {
break
}
targetNode = child
}
}
diffLevel := heading.HeadingLevel - parentHeading.HeadingLevel - 1
heading.HeadingLevel = parentHeading.HeadingLevel + 1
if 6 < heading.HeadingLevel {
heading.HeadingLevel = 6
}
for i := len(headingChildren) - 1; i >= 0; i-- {
child := headingChildren[i]
if ast.NodeHeading == child.Type {
child.HeadingLevel -= diffLevel
if 6 < child.HeadingLevel {
child.HeadingLevel = 6
}
}
targetNode.InsertAfter(child)
}
targetNode.InsertAfter(heading)
} else {
generateOpTypeHistory(tree, HistoryOpOutline)
// 移到第一个标题前
var firstHeading *ast.Node
for n := tree.Root.FirstChild; nil != n; n = n.Next {
if ast.NodeHeading == n.Type {
firstHeading = n
break
}
}
if nil == firstHeading || firstHeading.ID == heading.ID {
return
}
diffLevel := heading.HeadingLevel - firstHeading.HeadingLevel
heading.HeadingLevel = firstHeading.HeadingLevel
firstHeading.InsertBefore(heading)
for i := 0; i < len(headingChildren); i++ {
child := headingChildren[i]
if ast.NodeHeading == child.Type {
child.HeadingLevel -= diffLevel
if 6 < child.HeadingLevel {
child.HeadingLevel = 6
}
}
firstHeading.InsertBefore(child)
}
}
if err = tx.writeTree(tree); nil != err {
return
}
return
}
func Outline(rootID string) (ret []*Path, err error) {
time.Sleep(util.FrontendQueueInterval)
WaitForWritingFiles()
ret = []*Path{}
tree, _ := LoadTreeByBlockID(rootID)
if nil == tree {
return
}
ret = outline(tree)
return
}
func outline(tree *parse.Tree) (ret []*Path) {
luteEngine := NewLute()
var headings []*Block
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
n.Box, n.Path = tree.Box, tree.Path
block := &Block{
RootID: tree.Root.ID,
Depth: n.HeadingLevel,
Box: n.Box,
Path: n.Path,
ID: n.ID,
Content: renderOutline(n, luteEngine),
Type: n.Type.String(),
SubType: treenode.SubTypeAbbr(n),
}
headings = append(headings, block)
return ast.WalkSkipChildren
}
return ast.WalkContinue
})
if 1 > len(headings) {
return
}
var blocks []*Block
stack := linkedliststack.New()
for _, h := range headings {
L:
for ; ; stack.Pop() {
cur, ok := stack.Peek()
if !ok {
blocks = append(blocks, h)
stack.Push(h)
break L
}
tip := cur.(*Block)
if tip.Depth < h.Depth {
tip.Children = append(tip.Children, h)
stack.Push(h)
break L
}
tip.Count = len(tip.Children)
}
}
ret = toFlatTree(blocks, 0, "outline", tree)
if 0 < len(ret) {
children := ret[0].Blocks
ret = nil
for _, b := range children {
resetDepth(b, 0)
ret = append(ret, &Path{
ID: b.ID,
Box: b.Box,
Name: b.Content,
NodeType: b.Type,
Type: "outline",
SubType: b.SubType,
Blocks: b.Children,
Depth: 0,
Count: b.Count,
})
}
}
return
}
func resetDepth(b *Block, depth int) {
b.Depth = depth
b.Count = len(b.Children)
for _, c := range b.Children {
resetDepth(c, depth+1)
}
}