-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.go
329 lines (285 loc) · 9.52 KB
/
frontend.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
// +build js
package main
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/gopherjs/eventsource"
"github.com/gopherjs/gopherjs/js"
_ "github.com/shurcooL/frontend/checkbox"
_ "github.com/shurcooL/frontend/select_menu"
"github.com/shurcooL/go/gopherjs_http/jsutil"
"github.com/shurcooL/gtdo/frontend/selectlistview"
"github.com/shurcooL/gtdo/frontend/tableofcontents"
"github.com/shurcooL/gtdo/gtdo"
"github.com/shurcooL/gtdo/page"
"honnef.co/go/js/dom"
)
var document = dom.GetWindow().Document().(dom.HTMLDocument)
var state selection
type selection struct {
valid bool
file string
start int // Start line of selection, or 0 if there's no line selection. First line is 1.
end int // End line of selection. Same as start if it's one line selection.
}
// Hash returns a hash encoding of the selection, without '#'.
func (s selection) Hash() string {
if !s.valid {
return ""
}
hash := s.file
if s.start != 0 {
hash += fmt.Sprintf("-L%d", s.start)
}
if s.start != s.end {
hash += fmt.Sprintf("-L%d", s.end)
}
return hash
}
func main() {
js.Global.Set("MustScrollTo", jsutil.Wrap(MustScrollTo))
js.Global.Set("LineNumber", jsutil.Wrap(LineNumber))
js.Global.Set("HideOutdatedBox", HideOutdatedBox)
switch readyState := document.ReadyState(); readyState {
case "loading":
document.AddEventListener("DOMContentLoaded", false, func(dom.Event) {
go setup()
})
case "interactive", "complete":
setup()
default:
panic(fmt.Errorf("internal error: unexpected document.ReadyState value: %v", readyState))
}
var state page.State
err := json.Unmarshal([]byte(js.Global.Get("StateJSON").String()), &state)
if err != nil {
panic(err)
}
// EventSource updates.
if state.RepoSpec.CloneURL != "" {
u := url.URL{
Path: "/-/events",
RawQuery: url.Values{
"ImportPath": {state.ImportPath},
"Branch": {state.ProcessedRev},
"RepoSpec.VCSType": {state.RepoSpec.VCSType},
"RepoSpec.CloneURL": {state.RepoSpec.CloneURL},
}.Encode(),
}
source := eventsource.New(u.String())
source.AddEventListener("message", false, func(event *js.Object) {
data := event.Get("data").String()
if data == "outdated" {
showOutdatedBox()
}
})
}
}
func setup() {
tableofcontents.Setup()
selectlistview.Setup()
// Jump to desired hash after page finishes loading (and override browser's default hash jumping).
go func() {
// This needs to be in a goroutine or else it "happens too early".
// TODO: See if there's a better event than DOMContentLoaded.
processHashSet()
}()
// Start watching for hashchange events.
dom.GetWindow().AddEventListener("hashchange", false, func(event dom.Event) {
event.PreventDefault()
processHashSet()
})
document.Body().AddEventListener("keydown", false, func(event dom.Event) {
if event.DefaultPrevented() {
return
}
// Ignore when some element other than body has focus (it means the user is typing elsewhere).
if !event.Target().IsEqualNode(document.Body()) {
return
}
switch ke := event.(*dom.KeyboardEvent); {
// 'y' keyboard shortcut to get permalink.
case ke.KeyCode == 'Y' && !ke.Repeat && !ke.CtrlKey && !ke.AltKey && !ke.MetaKey && !ke.ShiftKey:
commitIdEl := document.GetElementByID("commit-id")
if commitIdEl == nil {
return
}
commitId := commitIdEl.GetAttribute("title")
if commitId == "" {
return
}
// Set revision query parameter to full commit id, if it's not already.
query, _ := url.ParseQuery(strings.TrimPrefix(dom.GetWindow().Location().Search, "?"))
if query.Get(gtdo.RevisionQueryParameter) != commitId {
query.Set(gtdo.RevisionQueryParameter, commitId)
// TODO: dom.GetWindow().History().PushState(...)
js.Global.Get("window").Get("history").Call("pushState", nil, nil, "?"+query.Encode()+dom.GetWindow().Location().Hash)
}
ke.PreventDefault()
// Escape.
case ke.KeyCode == 27 && !ke.Repeat && !ke.CtrlKey && !ke.AltKey && !ke.MetaKey && !ke.ShiftKey:
url := windowLocation
url.Fragment = ""
// TODO: dom.GetWindow().History().ReplaceState(...), blocked on https://github.com/dominikh/go-js-dom/issues/41.
js.Global.Get("window").Get("history").Call("replaceState", nil, nil, url.String())
processHashSet()
ke.PreventDefault()
}
})
}
// targetId must point to a valid target.
func MustScrollTo(event dom.Event, targetId string) {
target := document.GetElementByID(targetId).(dom.HTMLElement)
// TODO: dom.GetWindow().History().ReplaceState(...)
js.Global.Get("window").Get("history").Call("replaceState", nil, nil, "#"+targetId)
windowHalfHeight := dom.GetWindow().InnerHeight() * 2 / 5
dom.GetWindow().ScrollTo(dom.GetWindow().ScrollX(), int(offsetTopRoot(target)+target.OffsetHeight())-windowHalfHeight)
processHash(targetId, true)
}
// expandLineSelection expands line selection if shift was held down when clicking a line number,
// and it's in the same file as already highlighted. Otherwise return original targetId unmodified.
func expandLineSelection(event dom.Event, targetId string) string {
me, ok := event.(*dom.MouseEvent)
if !(ok && me.ShiftKey && state.valid && state.start != 0) {
return targetId
}
file, start, end := parseHash(targetId)
if !(file == state.file && start != 0) {
return targetId
}
switch {
case start < state.start:
state.start = start
case end > state.end:
state.end = end
}
return state.Hash()
}
// targetId must point to a valid target.
func LineNumber(event dom.Event, targetId string) {
targetId = expandLineSelection(event, targetId)
// TODO: dom.GetWindow().History().ReplaceState(...)
js.Global.Get("window").Get("history").Call("replaceState", nil, nil, "#"+targetId)
processHash(targetId, true)
}
func showOutdatedBox() {
document.GetElementByID("outdated-box").(dom.HTMLElement).Style().SetProperty("display", "block", "")
}
func HideOutdatedBox() {
document.GetElementByID("outdated-box").(dom.HTMLElement).Style().SetProperty("display", "none", "")
}
func processHashSet() {
// Scroll to hash target.
hash := strings.TrimPrefix(dom.GetWindow().Location().Hash, "#")
parts := strings.Split(hash, "-")
var targetId string
if file, start, _, ok := tryParseFileLineRange(parts); ok {
targetId = fmt.Sprintf("%s-L%d", file, start)
} else {
targetId = hash
}
target, ok := document.GetElementByID(targetId).(dom.HTMLElement)
if ok {
windowHalfHeight := dom.GetWindow().InnerHeight() * 2 / 5
dom.GetWindow().ScrollTo(dom.GetWindow().ScrollX(), int(offsetTopRoot(target)+target.OffsetHeight())-windowHalfHeight)
}
processHash(hash, ok)
}
// valid is true iff the hash points to a valid target.
func processHash(hash string, valid bool) {
// Clear everything.
for _, e := range document.GetElementsByClassName("selection") {
e.(dom.HTMLElement).Style().SetProperty("display", "none", "")
}
if !valid {
state.valid = false
return
}
file, start, end := parseHash(hash)
state.file, state.start, state.end, state.valid = file, start, end, true
if start != 0 {
startElement := document.GetElementByID(fmt.Sprintf("%s-L%d", file, start)).(dom.HTMLElement)
var endElement dom.HTMLElement
if end == start {
endElement = startElement
} else {
endElement = document.GetElementByID(fmt.Sprintf("%s-L%d", file, end)).(dom.HTMLElement)
}
fileHeader := document.GetElementByID(file).(dom.HTMLElement)
fileBackground := fileHeader.ParentElement().GetElementsByClassName("selection")[0].(dom.HTMLElement)
fileBackground.Style().SetProperty("display", "initial", "")
fileBackground.Style().SetProperty("top", fmt.Sprintf("%vpx", startElement.OffsetTop()), "")
fileBackground.Style().SetProperty("height", fmt.Sprintf("%vpx", endElement.OffsetTop()-startElement.OffsetTop()+endElement.OffsetHeight()), "")
}
}
func parseHash(hash string) (file string, start, end int) {
parts := strings.Split(hash, "-")
if file, start, end, ok := tryParseFileLineRange(parts); ok {
return file, start, end
} else if file, line, ok := tryParseFileLine(parts); ok {
return file, line, line
} else {
return hash, 0, 0
}
}
func tryParseFileLine(parts []string) (file string, line int, ok bool) {
if len(parts) < 2 {
return "", 0, false
}
lastPart := parts[len(parts)-1]
if len(lastPart) < 2 || lastPart[0] != 'L' {
return "", 0, false
}
line, err := strconv.Atoi(lastPart[1:])
if err != nil {
return "", 0, false
}
return strings.Join(parts[:len(parts)-1], "-"), line, true
}
func tryParseFileLineRange(parts []string) (file string, start, end int, ok bool) {
if len(parts) < 3 {
return "", 0, 0, false
}
{
secondLastPart := parts[len(parts)-2]
if len(secondLastPart) < 2 || secondLastPart[0] != 'L' {
return "", 0, 0, false
}
var err error
start, err = strconv.Atoi(secondLastPart[1:])
if err != nil {
return "", 0, 0, false
}
}
{
lastPart := parts[len(parts)-1]
if len(lastPart) < 2 || lastPart[0] != 'L' {
return "", 0, 0, false
}
var err error
end, err = strconv.Atoi(lastPart[1:])
if err != nil {
return "", 0, 0, false
}
}
return strings.Join(parts[:len(parts)-2], "-"), start, end, true
}
// offsetTopRoot returns the offset top of element e relative to root element.
func offsetTopRoot(e dom.HTMLElement) float64 {
var offsetTopRoot float64
for ; e != nil; e = e.OffsetParent() {
offsetTopRoot += e.OffsetTop()
}
return offsetTopRoot
}
var windowLocation = func() url.URL {
url, err := url.Parse(dom.GetWindow().Location().Href)
if err != nil {
// We don't expect this can ever happen, so treat it as an internal error if it does.
panic(fmt.Errorf("internal error: parsing window.location.href as URL failed: %v", err))
}
return *url
}()