data_helper.cljs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. (ns frontend.modules.shortcut.data-helper
  2. (:require [frontend.modules.shortcut.config :as config]
  3. [lambdaisland.glogi :as log]
  4. [frontend.util :as util]
  5. [clojure.string :as str]
  6. [frontend.state :as state]))
  7. (defn binding-map []
  8. (->> (vals config/default-config)
  9. (apply merge)
  10. (map (fn [[k {:keys [binding]}]]
  11. {k (or (state/get-shortcut k) binding)}))
  12. (into {})))
  13. (defn- mod-key [shortcut]
  14. (str/replace shortcut #"(?i)mod"
  15. (if util/mac? "meta" "ctrl")))
  16. (defn shortcut-binding
  17. [id]
  18. (let [shortcut (get (binding-map) id)]
  19. (cond
  20. (nil? shortcut)
  21. (log/error :shortcut/binding-not-found {:id id})
  22. (false? shortcut)
  23. (log/debug :shortcut/disabled {:id id})
  24. :else
  25. (->>
  26. (if (string? shortcut)
  27. [shortcut]
  28. shortcut)
  29. (mapv mod-key)))))
  30. ;; returns a vector to preserve order
  31. (defn binding-by-category [name]
  32. (let [dict (->> (vals config/default-config)
  33. (apply merge)
  34. (map (fn [[k {:keys [i18n]}]]
  35. {k {:binding (get (binding-map) k)
  36. :i18n i18n}}))
  37. (into {}))]
  38. (->> (config/category name)
  39. (mapv (fn [k] [k (k dict)])))))
  40. (defn shortcut-map
  41. ([handler-id]
  42. (shortcut-map handler-id nil))
  43. ([handler-id state]
  44. (let [raw (get config/default-config handler-id)
  45. handler-m (->> raw
  46. (map (fn [[k {:keys [fn]}]]
  47. {k fn}))
  48. (into {}))
  49. before (-> raw meta :before)]
  50. (cond->> handler-m
  51. state (reduce-kv (fn [r k handle-fn]
  52. (assoc r k (partial handle-fn state)))
  53. {})
  54. before (reduce-kv (fn [r k v]
  55. (assoc r k (before v)))
  56. {})))))
  57. (defn- decorate-namespace [k]
  58. (let [n (name k)
  59. ns (namespace k)]
  60. (keyword (str "shortcut." ns) n)))
  61. (defn shortcut-dict
  62. "All docs for EN are generated from :desc field of shortcut default-config map.
  63. For all other languages, need manual translation in dict file.
  64. Eg: editor/insert-link would be shortcut.editor/insert-link in dict file"
  65. []
  66. {:en
  67. (->> (vals config/default-config)
  68. (apply merge)
  69. (map (fn [[k {:keys [desc]}]]
  70. {(decorate-namespace k) desc}))
  71. (into {}))})