Compare commits

...

4 Commits

  1. 13
      CHANGELOG.md
  2. 48
      assets/css/app.scss
  3. 17
      assets/js/app.js
  4. 24
      lib/mirage/notes.ex
  5. 28
      lib/mirage_web/live/goto_anything_live.ex
  6. 25
      lib/mirage_web/live/goto_anything_live.html.leex
  7. 12
      lib/mirage_web/live/page_live.ex
  8. 4
      lib/mirage_web/live/page_live.html.leex
  9. 4
      lib/mirage_web/templates/note/_note.html.eex
  10. 2
      mix.exs
  11. 11
      priv/repo/migrations/20210219221843_add_trigram_extension.exs

13
CHANGELOG.md

@ -5,6 +5,19 @@ See [Conventional Commits](Https://conventionalcommits.org) for commit guideline
<!-- changelog -->
## [v0.44.0](https://git.inhji.de/inhji/mirage/compare/v0.43.0...v0.44.0) (2021-02-19)
### Features:
* colorful note headers in index, revert to blue primary color
* full text search
* add psql trigram extension
## [v0.43.0](https://git.inhji.de/inhji/mirage/compare/v0.42.0...v0.43.0) (2021-02-17)

48
assets/css/app.scss

@ -5,7 +5,7 @@
/* === Color Definitions === */
$primary-color: 'green';
$primary-color: 'blue';
$colors: (
light: (
@ -15,7 +15,7 @@ $colors: (
on-surface: material-color('grey', '900'),
background: material-color('grey', '200'),
primary: material-color($primary-color, '700'),
primary-inverse: material-color($primary-color, '700'),
primary-inverse: material-color($primary-color, 'a700'),
on-primary: material-color('grey', '50'),
navigation: material-color('grey', '900'),
on-navigation: material-color('grey', '50')
@ -275,6 +275,50 @@ kbd {
}
}
.notes {
margin-top: 4rem;
.note {
header {
@include color-assign(background-color, primary);
@include color-assign(color, on-primary);
}
}
}
#goto-anything {
position: absolute;
top: 5rem;
left: 15rem;
width: 30rem;
border: 1px solid white;
@include color-assign(background-color, surface);
@include color-assign(border-color, primary);
&.closed {
display: none;
}
form { margin: 0; }
input {
padding: 1rem;
border: none;
width: 100%;
@include color-assign(background-color, surface);
}
.results {
padding: 1rem;
.result {
padding-bottom: 1rem;
&:last-child { padding-bottom: 0; }
}
}
}
/* === Content === */
article {

17
assets/js/app.js

@ -18,8 +18,23 @@ import {Socket} from "phoenix"
import NProgress from "nprogress"
import {LiveSocket} from "phoenix_live_view"
const handledKeys = ["p"]
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})
let liveSocket = new LiveSocket("/live", Socket, {
params: {_csrf_token: csrfToken},
metadata: {
keydown: (e, el) => {
if (handledKeys.includes(e.key))
e.preventDefault()
return {
key: e.key,
ctrlKey: e.ctrlKey
}
}
}
})
// Show progress bar on live navigation and form submits
window.addEventListener("phx:page-loading-start", info => NProgress.start())

24
lib/mirage/notes.ex

@ -11,6 +11,30 @@ defmodule Mirage.Notes do
@note_preloads [:links, :topics, :backlinks]
@topic_preloads [notes: @note_preloads]
defmacro contains(content, search_term) do
quote do
fragment(
"? %> ?",
unquote(content),
unquote(search_term)
)
end
end
def search_notes(query) do
query = String.downcase(query)
q =
from n in Note,
limit: 5,
select: n,
preload: ^@note_preloads,
where: contains(n.content, ^query),
or_where: contains(n.title, ^query)
Repo.all(q)
end
@doc """
Returns the list of notes.

28
lib/mirage_web/live/goto_anything_live.ex

@ -0,0 +1,28 @@
defmodule MirageWeb.GotoAnythingLive do
use MirageWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(%{popup_class: "closed", results: []})}
end
def handle_event("hotkey", %{"key" => "p", "ctrlKey" => true}, socket) do
IO.inspect("Open Popup")
{:noreply, socket |> assign(%{popup_class: "open"})}
end
def handle_event("hotkey", %{"key" => "Escape", "ctrlKey" => false}, socket) do
IO.inspect("Close Popup")
{:noreply, socket |> assign(%{popup_class: "closed"})}
end
def handle_event("hotkey", params, socket) do
IO.inspect(params)
{:noreply, socket}
end
def handle_event("search", %{"key" => _, "value" => query}, socket) do
results = Mirage.Notes.search_notes(query)
{:noreply, socket |> assign(%{results: results})}
end
end

25
lib/mirage_web/live/goto_anything_live.html.leex

@ -0,0 +1,25 @@
<div id="goto-anything" class="<%= @popup_class %>" phx-window-keydown="hotkey">
<form action="#">
<input type="text" placeholder="Search for anything..." autofocus="true"
phx-keyup="search" phx-debounce="100">
</form>
<div class="results">
<%= if Enum.empty?(@results) do %>
<div class="card result">
<strong>No results..</strong>
<div>yet</div>
</div>
<% else %>
<%= for note <- @results do %>
<div class="card result">
<strong><%= note.title %></strong>
<div>
<%= live_redirect "Go to Note", to: Routes.live_path(MirageWeb.Endpoint, MirageWeb.ShowNoteLive, note.id) %>
</div>
</div>
<% end %>
<% end %>
</div>
</div>

12
lib/mirage_web/live/page_live.ex

@ -2,10 +2,16 @@ defmodule MirageWeb.PageLive do
use MirageWeb, :live_view
@impl true
def mount(_params, _session, socket) do
def mount(_params, %{"user_id" => user_id}, socket) do
current_user = user_id && Mirage.Accounts.get_user!(user_id)
home_note = Mirage.Notes.get_note_by_title("@home")
settings = Mirage.Settings.list_settings_as_map()
{:ok, assign(socket, %{_s: settings, home_note: home_note})}
{:ok,
assign(socket, %{
_s: settings,
home_note: home_note,
current_user: current_user
})}
end
end

4
lib/mirage_web/live/page_live.html.leex

@ -1,3 +1,7 @@
<%= if @current_user do %>
<%= live_render @socket, MirageWeb.GotoAnythingLive, id: "goto-anything-wrapper" %>
<% end %>
<header class="hero">
<h1><%= raw @_s.user_title.value %></h1>
<p><%= raw @_s.user_tagline.value %></p>

4
lib/mirage_web/templates/note/_note.html.eex

@ -1,9 +1,9 @@
<article>
<article class="note">
<%= link to: Routes.live_path(@conn, MirageWeb.ShowNoteLive, @note) do %>
<header class="width-full">
<h2 class="title">
<span class="id"><%= "##{@note.id}" %></span> <%= @note.title %>
<%= @note.title %>
</h2>
</header>
<% end %>

2
mix.exs

@ -1,7 +1,7 @@
defmodule Mirage.MixProject do
use Mix.Project
@version "0.43.0"
@version "0.44.0"
def project do
[

11
priv/repo/migrations/20210219221843_add_trigram_extension.exs

@ -0,0 +1,11 @@
defmodule Mirage.Repo.Migrations.AddTrigramExtension do
use Ecto.Migration
def up do
repo().query!("CREATE EXTENSION pg_trgm;")
end
def up do
repo().query!("DROP EXTENSION pg_trgm;")
end
end
Loading…
Cancel
Save