Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support audio files #2224

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cljc/athens/parser/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ inline = recur
page-link /
link /
image /
audio /
autolink /
hashtag-braced /
hashtag-naked /
Expand Down Expand Up @@ -98,6 +99,7 @@ strikethrough = <#'~~(?!\\s)'>

link = md-link
image = <'!'> md-link
audio = <'^'> md-link
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did some copy and paste, but I don't fully understand how the interpreter works


<md-link> = <#'\\[(?!\\s)'>
link-text
Expand Down Expand Up @@ -365,6 +367,12 @@ newline = #'\\n'
:src link-target}
link-title (assoc :title link-title))]))

(defn- audio-transform
[& link-parts]
(let [{:keys [link-text link-target link-title]} (link-parts->map link-parts)]
[:url-audio (cond-> {:alt link-text
:src link-target}
link-title (assoc :title link-title))]))

(defn- autolink-transform
[url]
Expand Down Expand Up @@ -399,6 +407,7 @@ newline = #'\\n'
:inline inline-transform
:link link-transform
:image image-transform
:audio audio-transform
:autolink autolink-transform
:component component-transform})

Expand Down
55 changes: 55 additions & 0 deletions src/cljs/athens/electron/audio.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(ns athens.electron.audio
(:require
[athens.common.utils :as common.utils]
[athens.db :as db]
[athens.electron.utils :as electron.utils]
[re-frame.core :as rf]))

(defn save-audio
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the save-image function share a big portion of code, maybe makes sense to refactor the common code to copy files

([item extension]
(save-audio "" "" item extension))
([head tail item extension]
;; TODO: change images-dir to audios-dir, for some reason returns nil even if I added the path and key in the utils.clj local-db.
(let [{:keys [name] audios-dir :images-dir} @(rf/subscribe [:db-picker/selected-db])
_ (prn head tail audios-dir name item extension)
file (.getAsFile item)
audio-filename (.resolve (electron.utils/path)
audios-dir
(str "audio-" name "-" (common.utils/gen-block-uid) "." extension))
reader (js/FileReader.)
new-str (str head "^[](" "file://" audio-filename ")" tail)
cb (fn [e]
(let [audio-data (as->
(.. e -target -result) x
(clojure.string/replace-first x #"data:audio/(wav|mp3);base64," "")
(js/Buffer. x "base64"))]
(when-not (.existsSync (electron.utils/fs) audios-dir)
(.mkdirSync (electron.utils/fs) audios-dir))
(.writeFileSync (electron.utils/fs) audio-filename audio-data)))]
(set! (.. reader -onload) cb)
(.readAsDataURL reader file)
new-str)))

(defn dnd-audio
[target-uid drag-target item extension]
(let [new-str (save-audio item extension)
{:block/keys [order]} (db/get-block [:block/uid target-uid])
parent (db/get-parent [:block/uid target-uid])
block (db/get-block [:block/uid target-uid])
new-block {:block/uid (common.utils/gen-block-uid) :block/order 0 :block/string new-str :block/open true}
tx-data (if (= drag-target :first)
(let [reindex (db/inc-after (:db/id block) -1)
new-children (conj reindex new-block)
new-target-block {:db/id [:block/uid target-uid] :block/children new-children}]
new-target-block)
(let [index (case drag-target
:before (dec order)
:after order)
reindex (db/inc-after (:db/id parent) index)
new-children (conj reindex new-block)
new-parent {:db/id (:db/id parent) :block/children new-children}]
new-parent))]
;; delay because you want to create block *after* the file has been saved to filesystem
;; otherwise, <img> is created too fast, and no image is rendered
;; TODO: this functionality needs to create an event instead and upload the file to work with RTC.
(js/setTimeout #(rf/dispatch [:transact [tx-data]]) 50)))
2 changes: 2 additions & 0 deletions src/cljs/athens/electron/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

(def DB-INDEX "index.transit")
(def IMAGES-DIR-NAME "images")
(def AUDIOS-DIR-NAME "audios")


(defn default-dbs-dir
Expand All @@ -70,6 +71,7 @@
:id base-dir
:base-dir base-dir
:images-dir (.resolve (path) base-dir IMAGES-DIR-NAME)
:audios-dir (.resolve (path) base-dir AUDIOS-DIR-NAME)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with this change, when I tried to access to :audios-dir it returns nil

:db-path (.resolve (path) base-dir DB-INDEX)})


Expand Down
6 changes: 6 additions & 0 deletions src/cljs/athens/parse_renderer.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@
:borderRadius "md"
:alt alt
:src url}])
:url-audio (fn [{url :src alt :alt}]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not working, dunno how if chakra ui supports audio

[:> Box {:class "url-audio"
:as "audio"
:borderRadius "md"
:alt alt
:src url}])
:url-link (fn [{url :url} text]
[:> Button
(merge link-props {:class "url-link"
Expand Down
4 changes: 4 additions & 0 deletions src/cljs/athens/views/blocks/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[athens.common.logging :as log]
[athens.db :as db]
[athens.electron.images :as images]
[athens.electron.audio :as audio]
[athens.electron.utils :as electron.utils]
[athens.events.dragging :as drag.events]
[athens.events.inline-refs :as inline-refs.events]
Expand Down Expand Up @@ -120,13 +121,16 @@
item (first items)
datatype (.. item -type)
img-regex #"(?i)^image/(p?jpeg|gif|png)$"
audio-regex #"(?i)^audio/(p?wav|mp3)$"
valid-text-drop (and (not (nil? drag-target))
(not= source-uid target-uid)
(or (= effect-allowed "link")
(= effect-allowed "move")))
selected-items @(rf/subscribe [::select-subs/items])]

(cond
(re-find audio-regex datatype) (when electron.utils/electron?
(audio/dnd-audio target-uid drag-target item (second (re-find audio-regex datatype))))
(re-find img-regex datatype) (when electron.utils/electron?
(images/dnd-image target-uid drag-target item (second (re-find img-regex datatype))))
(re-find #"text/plain" datatype) (when valid-text-drop
Expand Down