Skip to content

Commit

Permalink
Leggoooo drafting matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
ks0m1c_dharma committed Jul 27, 2024
1 parent 55da916 commit 55408f6
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 87 deletions.
7 changes: 4 additions & 3 deletions assets/js/hooks/hoverune.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const findParent = (el, attr, stopper) => {
}

function floatHoveRune({clientX, clientY}) {

console.log("sting like a bees")
const selection = window.getSelection()
var getSelectRect = selection.getRangeAt(0).getBoundingClientRect()
const virtualEl = {
Expand Down Expand Up @@ -51,7 +49,7 @@ const forgeBinding = (el, attrs) => attrs.reduce((acc, attr) => {
export default HoveRune = {
mounted() {
const t = this.el
const targetEvents = ['click', 'pointermove', 'pointerdown']
const targetEvents = ['pointerdown', 'pointerup']
targetEvents.forEach(e => window.addEventListener(e, ({ target }) => {
var selection = window.getSelection()
var getSelectRect = selection.getRangeAt(0).getBoundingClientRect();
Expand All @@ -63,8 +61,11 @@ export default HoveRune = {
if (isNode) {
binding = forgeBinding(target, ["node", "node_id", "field", "verse_id"])
binding["selection"] = getSelectText

this.pushEvent("bindHoveRune", {"binding": binding})

console.log(binding)


computePosition(target, hoverune, {placement: 'top-end', middleware: [inline(getSelectRect.x, getSelectRect.y), offset(5)]}).then(({x, y}) => {
hoverune.classList.remove("hidden")
Expand Down
17 changes: 14 additions & 3 deletions lib/vyasa/adapters/binding.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ defmodule Vyasa.Adapters.Binding do
@primary_key {:id, Ecto.UUID, autogenerate: true}
schema "bindings" do
field :w_type, Ecto.Enum, values: [:quote, :timestamp, :null]
field :field_key, :string

field :field_key, {:array, :string}

field :node_id, :string, virtual: true

belongs_to :verse, Verse, foreign_key: :verse_id, type: :binary_id
belongs_to :chapter, Chapter, type: :integer, references: :no, foreign_key: :chapter_no
belongs_to :source, Source, foreign_key: :source_id, type: :binary_id
belongs_to :translation, Translation, foreign_key: :translation_id, type: :binary_id
belongs_to :comment, Comment, foreign_key: :comment_id, type: :binary_id
belongs_to :comment_bind, Comment, foreign_key: :comment_bind_id, type: :binary_id

embeds_one :window, Window, on_replace: :delete do
field(:line_number, :integer)
Expand All @@ -37,7 +40,7 @@ defmodule Vyasa.Adapters.Binding do
end


def bind_comment_changeset(%__MODULE__{} = binding, attrs) do
def windowing_changeset(%__MODULE__{} = binding, attrs) do
binding
|> cast(attrs, [:w_type, :verse_id, :chapter_no, :source_id])
|> typed_window_switch(attrs)
Expand Down Expand Up @@ -98,4 +101,12 @@ defmodule Vyasa.Adapters.Binding do
end
end)
end

def field_lookup(%Verse{}), do: :verse_id
def field_lookup(%Chapter{}), do: :chapter_no
def field_lookup(%Source{}), do: :source_id
def field_lookup(%Translation{}), do: :translation_id
def field_lookup(%Comment{}), do: :comment_id

def field_lookup(_), do: nil
end
131 changes: 131 additions & 0 deletions lib/vyasa/draft.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
defmodule Vyasa.Draft do
@moduledoc """
The Drafting Context for all your marking and binding needs
"""

import Ecto.Query, warn: false
alias Vyasa.Adapters.Binding
alias Vyasa.Sangh.Mark
alias Vyasa.Sangh
alias Vyasa.Repo



def bind_node(%{"selection" => ""} = node) do
bind_node(node, %Binding{})
end

def bind_node(%{"selection" => selection} = node) do
bind_node(Map.delete(node, "selection"), %Binding{:window => %{:quote => selection}})
end

def bind_node(%{"field" => field} = node, bind) do
bind_node(Map.delete(node, "field"), %{bind | field_key: String.split(field, "::") |> Enum.map(&(String.to_existing_atom(&1)))})
end

def bind_node(%{"node" => node, "node_id" => node_id}, %Binding{} = bind) do
n = node
|> String.to_existing_atom()
|> struct()
|> Binding.field_lookup()

%{bind | n => node_id, :node_id => node_id}
end

def create_comment([%Mark{} | _]= marks) do
Sangh.create_comment(%{marks: marks})
end
@doc """
Returns the list of marks.
## Examples
iex> list_marks()
[%Mark{}, ...]
"""
def list_marks do
Repo.all(Mark)
end

@doc """
Gets a single mark.
Raises `Ecto.NoResultsError` if the Mark does not exist.
## Examples
iex> get_mark!(123)
%Mark{}
iex> get_mark!(456)
** (Ecto.NoResultsError)
"""
def get_mark!(id), do: Repo.get!(Mark, id)

@doc """
Creates a mark.
## Examples
iex> create_mark(%{field: value})
{:ok, %Mark{}}
iex> create_mark(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_mark(attrs \\ %{}) do
%Mark{}
|> Mark.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a mark.
## Examples
iex> update_mark(mark, %{field: new_value})
{:ok, %Mark{}}
iex> update_mark(mark, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_mark(%Mark{} = mark, attrs) do
mark
|> Mark.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a mark.
## Examples
iex> delete_mark(mark)
{:ok, %Mark{}}
iex> delete_mark(mark)
{:error, %Ecto.Changeset{}}
"""
def delete_mark(%Mark{} = mark) do
Repo.delete(mark)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking mark changes.
## Examples
iex> change_mark(mark)
%Ecto.Changeset{data: %Mark{}}
"""
def change_mark(%Mark{} = mark, attrs \\ %{}) do
Mark.changeset(mark, attrs)
end
end
4 changes: 2 additions & 2 deletions lib/vyasa/sangh.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ defmodule Vyasa.Sangh do
## Examples
iex> create_comment(comment, %{field: new_value})
iex> create_comment(%{field: new_value})
{:ok, %Comment{}}
iex> create_comment(comment, %{field: bad_value})
iex> create_comment(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
Expand Down
8 changes: 4 additions & 4 deletions lib/vyasa/sangh/comments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ defmodule Vyasa.Sangh.Comment do
use Ecto.Schema
import Ecto.Changeset
alias EctoLtree.LabelTree, as: Ltree
alias Vyasa.Sangh.{Comment, Session}
alias Vyasa.Adapters.Binding
alias Vyasa.Sangh.{Comment, Session, Mark}

@primary_key {:id, Ecto.UUID, autogenerate: false}
schema "comments" do
Expand All @@ -16,7 +15,8 @@ defmodule Vyasa.Sangh.Comment do
belongs_to :session, Session, references: :id, type: Ecto.UUID
belongs_to :parent, Comment, references: :id, type: Ecto.UUID

has_many :bindings, Binding, references: :id, foreign_key: :comment_bind_id, on_replace: :delete_if_exists
#has_many :marks, Mark, references: :id, foreign_key: :comment_bind_id, on_replace: :delete_if_exists
#has_many :bindings, Binding, references: :id, foreign_key: :comment_bind_id, on_replace: :delete_if_exists

timestamps()
end
Expand All @@ -25,7 +25,7 @@ defmodule Vyasa.Sangh.Comment do
def changeset(%Comment{} = comment, attrs) do
comment
|> cast(attrs, [:id, :body, :path, :chapter_number, :p_type, :session_id, :parent_id, :text_id])
|> cast_assoc(:bindings, with: &Binding.bind_comment_changeset/2)
|> cast_assoc(:bindings, with: &Mark.changeset/2)
|> validate_required([:id, :session_id])
end

Expand Down
30 changes: 30 additions & 0 deletions lib/vyasa/sangh/mark.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Vyasa.Sangh.Mark do
@moduledoc """
Interpretation & bounding of binding
"""

use Ecto.Schema
import Ecto.Changeset

alias Vyasa.Sangh.{Comment}
alias Vyasa.Adapters.Binding

@primary_key {:id, Ecto.UUID, autogenerate: true}
schema "marks" do
field :body, :string
field :order, :integer
field :state, Ecto.Enum, values: [:draft, :bookmark, :live]
field :verse_id, :string, virtual: true

belongs_to :comment, Comment, foreign_key: :comment_id, type: :binary_id
belongs_to :binding, Binding, foreign_key: :binding_id, type: :binary_id

timestamps()
end

def changeset(event, attrs) do
event
|> cast(attrs, [:body, :order, :status, :comment_id, :binding_id])
end

end
43 changes: 43 additions & 0 deletions lib/vyasa_web/components/draft_matrix.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule VyasaWeb.DraftMatrix do
use VyasaWeb, :live_component

@impl true
def update(assigns, socket) do
assigns = Enum.reject(assigns, fn {_k, v} -> is_nil(v) or v == [] end)

{:ok,
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> assign_new(:marks, fn -> [] end)
|> assign_new(:custom_style, fn -> [] end)}
end

@impl true
def handle_event("adjust", adjusted, socket) do
style = Enum.map(adjusted, fn {key, val} ->
"#{key}: #{val}"
end)

{:noreply, assign(socket, custom_style: style)}
end

@doc """
Renders a marginote w action slots for interaction with marginote
"""
@impl true
def render(assigns) do
~H"""
<header class={["absolute z-50 shadow-sm", @actions != [] && "marginote"]} id={@id}
style={["max-width: calc(-24px + 100vw)", @custom_style] |> List.flatten() |> Enum.join(";")}>
<.form for={%{}} phx-submit="create_mark">
<input
name="body"
class="block lg:w-[80%] md:w-96 rounded-lg border border-gray-200 bg-gray-50 p-2 pl-5 text-sm text-gray-800"
placeholder="Write here..."
/>
</.form>
</header>
"""
end
end
2 changes: 1 addition & 1 deletion lib/vyasa_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="flex items-center justify-between border-b border-zinc-100 py-3 text-sm">
<div class="flex items-center gap-4">
<a href="/" class="">
<img src={~p"/images/logo.svg"} width="36" class="mix-blend-normal"/>
<img src={~p"/images/logo.svg"} width="30" class="mix-blend-normal"/>
</a>
<p class="bg-brand/5 text-brand rounded-full px-2 font-medium leading-6">
v<%= Application.spec(:vyasa, :vsn) %>
Expand Down
Loading

0 comments on commit 55408f6

Please sign in to comment.