Skip to content

Commit

Permalink
implement and use Content.getWebURL; mostly fixes #1279, although som…
Browse files Browse the repository at this point in the history
…e issues with navigation remain
  • Loading branch information
kkoreilly committed Nov 20, 2024
1 parent 2f7022c commit 339a97c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
8 changes: 6 additions & 2 deletions content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,15 @@ func (ct *Content) SetSource(source fs.FS) *Content {
}
return nil
}))
if url := ct.getWebURL(); url != "" {
ct.Open(url)
return ct
}
if root, ok := ct.pagesByURL[""]; ok {
ct.Open(root.URL)
} else {
ct.Open(ct.pages[0].URL)
return ct
}
ct.Open(ct.pages[0].URL)
return ct
}

Expand Down
23 changes: 20 additions & 3 deletions content/url_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
package content

import (
"fmt"
"net/url"
"strings"
"syscall/js"

"cogentcore.org/core/base/errors"
)

// firstContent is the first [Content] used for [Content.getWebURL] or [Content.saveWebURL],
Expand All @@ -18,6 +20,22 @@ var firstContent *Content

var documentData = js.Global().Get("document").Get("documentElement").Get("dataset")

// getWebURL returns the current relative web URL that should be passed to [Content.Open]
// on startup and in [Content.handleWebPopState].
func (ct *Content) getWebURL() string {
if firstContent == nil {
firstContent = ct
}
if firstContent != ct {
return ""
}
full, base, err := getURL()
if errors.Log(err) != nil {
return ""
}
return strings.TrimPrefix(full.String(), base.String())
}

// saveWebURL saves the current page URL to the user's address bar and history.
func (ct *Content) saveWebURL() {
if firstContent == nil {
Expand All @@ -36,8 +54,7 @@ func (ct *Content) saveWebURL() {
// handleWebPopState adds a JS event listener to handle user navigation in the browser.
func (ct *Content) handleWebPopState() {
js.Global().Get("window").Call("addEventListener", "popstate", js.FuncOf(func(this js.Value, args []js.Value) any {
full, base, err := getURL()
fmt.Println(full, base, err)
ct.Open(ct.getWebURL())
return nil
}))
}
Expand Down
1 change: 1 addition & 0 deletions content/url_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

package content

func (ct *Content) getWebURL() string { return "" }
func (ct *Content) saveWebURL() {}
func (ct *Content) handleWebPopState() {}

0 comments on commit 339a97c

Please sign in to comment.