#23 Add bookmarklet for quick linking

Unito
getty ha unito 4 commit da getty/gdritter/add-bookmarklet a getty/master 4 anni fa
6 ha cambiato i file con 36 aggiunte e 5 eliminazioni
  1. 8 2
      lc/app.py
  2. 1 0
      lc/config.py
  3. 15 0
      lc/view.py
  4. 4 1
      tasks.py
  5. 2 2
      templates/add_link.mustache
  6. 6 0
      templates/config.mustache

+ 8 - 2
lc/app.py

@@ -104,7 +104,7 @@ class GetUser(Endpoint):
 
 
 @endpoint("/u/<string:user>/config")
-class UserConfig(Endpoint):
+class GetUserConfig(Endpoint):
     def html(self, user: str):
         u = self.require_authentication(user)
         return render(
@@ -128,8 +128,14 @@ class CreateInvite(Endpoint):
 @endpoint("/u/<string:user>/l")
 class CreateLink(Endpoint):
     def html(self, user: str):
+        url = flask.request.args.get("url", "")
+        name = flask.request.args.get("name", "")
+        defaults = v.AddLinkDefaults(name=name, url=url,)
         return render(
-            "main", v.Page(title="login", content=render("add_link"), user=self.user,)
+            "main",
+            v.Page(
+                title="login", content=render("add_link", defaults), user=self.user,
+            ),
         )
 
     def api_post(self, user: str):

+ 1 - 0
lc/config.py

@@ -8,6 +8,7 @@ import playhouse.sqlite_ext
 db = playhouse.sqlite_ext.SqliteExtDatabase(None)
 per_page = 50
 serializer = itsdangerous.URLSafeTimedSerializer(os.getenv("SECRET_KEY", "TEMP KEY"))
+app_path = os.environ["APP_PATH"]
 app = flask.Flask(__name__)
 app.secret_key = os.getenv("SECRET_KEY", "ARGLBARGL")
 

+ 15 - 0
lc/view.py

@@ -46,6 +46,15 @@ class Config(View):
     username: str
     admin_pane: Optional[AdminPane]
 
+    def bookmarklet_link(self):
+        return (
+            "javascript:(function(){window.open(`"
+            + c.app_path
+            + "/u/"
+            + self.username
+            + "/l?name=${document.title}&url=${document.URL}`);})();"
+        )
+
 
 @dataclass
 class Tag(View):
@@ -83,6 +92,12 @@ class Message(View):
     message: str
 
 
+@dataclass
+class AddLinkDefaults(View):
+    url: Optional[str] = None
+    name: Optional[str] = None
+
+
 @dataclass
 class Page(View):
     title: str

+ 4 - 1
tasks.py

@@ -13,7 +13,10 @@ def run(c, port=8080, host="127.0.0.1"):
     """Run a debug server locally"""
     c.run(
         f"poetry run python -m flask run -p {port} -h {host}",
-        env={"FLASK_APP": "lament-configuration.py"},
+        env={
+            "FLASK_APP": "lament-configuration.py",
+            "APP_PATH": f"http://{host}:{port}",
+        },
     )
 
 

+ 2 - 2
templates/add_link.mustache

@@ -2,11 +2,11 @@
   <form name="login" method="POST">
     <div class="url">
       <label for="url">URL</label>
-      <input name="url" type="text" />
+      <input name="url" type="text" value="{{url}}" />
     </div>
     <div class="name">
       <label for="name">Link Name</label>
-      <input name="name" type="text" />
+      <input name="name" type="text" value="{{name}}" />
     </div>
     <div class="description">
       <label for="description">Description</label>

+ 6 - 0
templates/config.mustache

@@ -20,6 +20,12 @@
     </form>
   </div>
 </div>
+<div class="config-pane">
+  <div class="config">
+  <p>Drag the following link to your bookmark bar to create a bookmarklet:</p>
+  <p><a href="{{bookmarklet_link}}">Add Lament</a></p>
+  </div>
+</div>
 {{#admin_pane}}
   <div class="config-pane">
     <div class="config">