-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
base: main
Are you sure you want to change the base?
Support audio files #2224
Changes from all commits
c222682
1f25396
51aed82
4718696
5c0056c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
|
||
(def DB-INDEX "index.transit") | ||
(def IMAGES-DIR-NAME "images") | ||
(def AUDIOS-DIR-NAME "audios") | ||
|
||
|
||
(defn default-dbs-dir | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even with this change, when I tried to access to |
||
:db-path (.resolve (path) base-dir DB-INDEX)}) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -265,6 +265,12 @@ | |
:borderRadius "md" | ||
:alt alt | ||
:src url}]) | ||
:url-audio (fn [{url :src alt :alt}] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
There was a problem hiding this comment.
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