Skip to content

Commit

Permalink
feat(paste): toggle between pasting text or file when they both exist (
Browse files Browse the repository at this point in the history
…logseq#7198)

* feat(paste): toggle between pasting text or file when they both exist
  • Loading branch information
situ2001 authored Nov 9, 2022
1 parent e326e39 commit 1a216aa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
8 changes: 8 additions & 0 deletions src/main/frontend/components/settings.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@
logical-outdenting?
config-handler/toggle-logical-outdenting!))

(defn perferred-pasting-file [t perferred-pasting-file?]
(toggle "preferred_pasting_file"
(t :settings-page/preferred-pasting-file)
perferred-pasting-file?
config-handler/toggle-perferred-pasting-file!))

(defn tooltip-row [t enable-tooltip?]
(toggle "enable_tooltip"
(t :settings-page/enable-tooltip)
Expand Down Expand Up @@ -555,6 +561,7 @@
enable-timetracking? (state/enable-timetracking?)
enable-all-pages-public? (state/all-pages-public?)
logical-outdenting? (state/logical-outdenting?)
perferred-pasting-file? (state/perferred-pasting-file?)
enable-tooltip? (state/enable-tooltip?)
enable-shortcut-tooltip? (state/sub :ui/shortcut-tooltip?)
show-brackets? (state/show-brackets?)
Expand All @@ -568,6 +575,7 @@
(show-brackets-row t show-brackets?)
(when (util/electron?) (switch-spell-check-row t))
(outdenting-row t logical-outdenting?)
(perferred-pasting-file t perferred-pasting-file?)
(when-not (or (util/mobile?) (mobile-util/native-platform?))
(shortcut-tooltip-row t enable-shortcut-tooltip?))
(when-not (or (util/mobile?) (mobile-util/native-platform?))
Expand Down
1 change: 1 addition & 0 deletions src/main/frontend/dicts.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
:settings-page/custom-date-format "Preferred date format"
:settings-page/preferred-file-format "Preferred file format"
:settings-page/preferred-workflow "Preferred workflow"
:settings-page/preferred-pasting-file "Preferred pasting file"
:settings-page/enable-shortcut-tooltip "Enable shortcut tooltip"
:settings-page/enable-timetracking "Timetracking"
:settings-page/enable-tooltip "Tooltips"
Expand Down
4 changes: 4 additions & 0 deletions src/main/frontend/handler/config.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@
(defn toggle-ui-enable-tooltip! []
(let [enable-tooltip? (state/enable-tooltip?)]
(set-config! :ui/enable-tooltip? (not enable-tooltip?))))

(defn toggle-perferred-pasting-file! []
(let [perferred-pasting-file? (state/perferred-pasting-file?)]
(set-config! :editor/perferred-pasting-file? (not perferred-pasting-file?))))
50 changes: 27 additions & 23 deletions src/main/frontend/handler/paste.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,30 @@
(state/set-state! :editor/on-paste? true)
(let [input (state/get-input)]
(if raw-paste?
(utils/getClipText
(fn [clipboard-data]
(when-let [_ (state/get-input)]
(let [text (or (when (gp-util/url? clipboard-data)
(wrap-macro-url clipboard-data))
clipboard-data)]
(paste-text-or-blocks-aux input e text nil))))
(fn [error]
(js/console.error error)))
(let [clipboard-data (gobj/get e "clipboardData")
html (when-not raw-paste? (.getData clipboard-data "text/html"))
text (.getData clipboard-data "text")]
(if-not (and (string/blank? text) (string/blank? html))
(paste-text-or-blocks-aux input e text html)
(when id
(let [_handled
(let [clipboard-data (gobj/get e "clipboardData")
files (.-files clipboard-data)]
(when-let [file (first files)]
(when-let [block (state/get-edit-block)]
(editor-handler/upload-asset id #js[file] (:block/format block)
editor-handler/*asset-uploading? true))))]
(util/stop e))))))))))
(utils/getClipText
(fn [clipboard-data]
(when-let [_ (state/get-input)]
(let [text (or (when (gp-util/url? clipboard-data)
(wrap-macro-url clipboard-data))
clipboard-data)]
(paste-text-or-blocks-aux input e text nil))))
(fn [error]
(js/console.error error)))
(let [clipboard-data (gobj/get e "clipboardData")
html (when-not raw-paste? (.getData clipboard-data "text/html"))
text (.getData clipboard-data "text")
files (.-files clipboard-data)
paste-file-if-exiist (fn []
(when id
(let [_handled
(let [clipboard-data (gobj/get e "clipboardData")
files (.-files clipboard-data)]
(when-let [file (first files)]
(when-let [block (state/get-edit-block)]
(editor-handler/upload-asset id #js[file] (:block/format block)
editor-handler/*asset-uploading? true))))]
(util/stop e))))]
(cond
(and (string/blank? text) (string/blank? html)) (paste-file-if-exiist)
(and (seq files) (state/perferred-pasting-file?)) (paste-file-if-exiist)
:else (paste-text-or-blocks-aux input e text html))))))))
4 changes: 4 additions & 0 deletions src/main/frontend/state.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ Similar to re-frame subscriptions"
[]
(:editor/logical-outdenting? (sub-config)))

(defn perferred-pasting-file?
[]
(:editor/perferred-pasting-file? (sub-config)))

(defn doc-mode-enter-for-new-line?
[]
(and (document-mode?)
Expand Down

0 comments on commit 1a216aa

Please sign in to comment.