From 727868728204f1a4b46e94a86e7ea524be8a3741 Mon Sep 17 00:00:00 2001
From: Leon Hudak <33522493+leohhhn@users.noreply.github.com>
Date: Sat, 7 Dec 2024 22:57:14 +0100
Subject: [PATCH] docs: hyperlink buttons demo (#3245)
## Description
Adds a `r/docs` that showcases how "buttons" can be added to realm
renders.
Contributors' checklist...
- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
---------
Co-authored-by: Morgan
---
examples/gno.land/r/docs/buttons/buttons.gno | 44 +++++++++++++++++++
.../gno.land/r/docs/buttons/buttons_test.gno | 14 ++++++
examples/gno.land/r/docs/buttons/gno.mod | 1 +
examples/gno.land/r/docs/docs.gno | 1 +
4 files changed, 60 insertions(+)
create mode 100644 examples/gno.land/r/docs/buttons/buttons.gno
create mode 100644 examples/gno.land/r/docs/buttons/buttons_test.gno
create mode 100644 examples/gno.land/r/docs/buttons/gno.mod
diff --git a/examples/gno.land/r/docs/buttons/buttons.gno b/examples/gno.land/r/docs/buttons/buttons.gno
new file mode 100644
index 00000000000..cb050b1bc38
--- /dev/null
+++ b/examples/gno.land/r/docs/buttons/buttons.gno
@@ -0,0 +1,44 @@
+package buttons
+
+import (
+ "std"
+
+ "gno.land/p/demo/ufmt"
+ "gno.land/p/moul/txlink"
+)
+
+var (
+ motd = "The Initial Message\n\n"
+ lastCaller std.Address
+)
+
+func UpdateMOTD(newmotd string) {
+ motd = newmotd
+ lastCaller = std.PrevRealm().Addr()
+}
+
+func Render(path string) string {
+ if path == "motd" {
+ out := "# Message of the Day:\n\n"
+ out += "---\n\n"
+ out += "# " + motd + "\n\n"
+ out += "---\n\n"
+ link := txlink.Call("UpdateMOTD", "newmotd", "Message!") // "/r/docs/buttons$help&func=UpdateMOTD&newmotd=Message!"
+ out += ufmt.Sprintf("Click **[here](%s)** to update the Message of The Day!\n\n", link)
+ out += "[Go back to home page](/r/docs/buttons)\n\n"
+ out += "Last updated by " + lastCaller.String()
+
+ return out
+ }
+
+ out := `# Buttons
+
+Users can create simple hyperlink buttons to view specific realm pages and
+do specific realm actions, such as calling a specific function with some arguments.
+
+The foundation for this functionality are markdown links; for example, you can
+click...
+` + "\n## [here](/r/docs/buttons:motd)\n" + `...to view this realm's message of the day.`
+
+ return out
+}
diff --git a/examples/gno.land/r/docs/buttons/buttons_test.gno b/examples/gno.land/r/docs/buttons/buttons_test.gno
new file mode 100644
index 00000000000..2903fa1a858
--- /dev/null
+++ b/examples/gno.land/r/docs/buttons/buttons_test.gno
@@ -0,0 +1,14 @@
+package buttons
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestRenderMotdLink(t *testing.T) {
+ res := Render("motd")
+ const wantLink = "/r/docs/buttons$help&func=UpdateMOTD&newmotd=Message!"
+ if !strings.Contains(res, wantLink) {
+ t.Fatalf("%s\ndoes not contain correct help page link: %s", res, wantLink)
+ }
+}
diff --git a/examples/gno.land/r/docs/buttons/gno.mod b/examples/gno.land/r/docs/buttons/gno.mod
new file mode 100644
index 00000000000..43cc2d773da
--- /dev/null
+++ b/examples/gno.land/r/docs/buttons/gno.mod
@@ -0,0 +1 @@
+module gno.land/r/docs/buttons
diff --git a/examples/gno.land/r/docs/docs.gno b/examples/gno.land/r/docs/docs.gno
index 57d020cd737..28bac4171b5 100644
--- a/examples/gno.land/r/docs/docs.gno
+++ b/examples/gno.land/r/docs/docs.gno
@@ -11,6 +11,7 @@ Explore various examples to learn more about Gno functionality and usage.
- [Hello World](/r/docs/hello) - A simple introductory example.
- [Adder](/r/docs/adder) - An interactive example to update a number with transactions.
- [Source](/r/docs/source) - View realm source code.
+- [Buttons](/r/docs/buttons) - Add buttons to your realm's render.
- [AVL Pager](/r/docs/avl_pager) - Paginate through AVL tree items.
- [Img Embed](/r/docs/img_embed) - Demonstrates how to embed an image.
- ...