| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369 |
- (ns frontend.state
- "Provides main application state, fns associated to set and state based rum
- cursors"
- (:require [cljs-bean.core :as bean]
- [cljs.core.async :as async :refer [<! >!]]
- [cljs.spec.alpha :as s]
- [clojure.string :as string]
- [dommy.core :as dom]
- [electron.ipc :as ipc]
- [frontend.colors :as colors]
- [frontend.mobile.util :as mobile-util]
- [frontend.spec.storage :as storage-spec]
- [frontend.storage :as storage]
- [frontend.util :as util]
- [frontend.util.cursor :as cursor]
- [goog.dom :as gdom]
- [goog.object :as gobj]
- [logseq.common.config :as common-config]
- [medley.core :as medley]
- [promesa.core :as p]
- [rum.core :as rum]))
- (defonce *profile-state
- (atom {}))
- (defonce *editor-editing-ref (atom nil))
- ;; Stores main application state
- (defonce ^:large-vars/data-var state
- (let [document-mode? (or (storage/get :document/mode?) false)
- current-graph (let [url-graph (:graph (util/parse-params))
- graph (or url-graph (storage/get :git/current-repo))]
- (when graph (ipc/ipc "setCurrentGraph" graph))
- graph)]
- (atom
- {:route-match nil
- :today nil
- :system/events (async/chan 1000)
- :file/unlinked-dirs #{}
- :reactive/custom-queries (async/chan 1000)
- :notification/show? false
- :notification/content nil
- :repo/loading-files? {}
- :nfs/user-granted? {}
- :nfs/refreshing? nil
- :instrument/disabled? (storage/get "instrument-disabled")
- ;; TODO: how to detect the network reliably?
- :network/online? true
- :indexeddb/support? true
- :me nil
- :git/current-repo current-graph
- :draw? false
- :db/restoring? nil
- :journals-length 3
- :search/q ""
- :search/mode nil ; nil -> global mode, :graph -> add graph filter, etc.
- :search/result nil
- :search/graph-filters []
- :search/engines {}
- ;; modals
- :modal/dropdowns {}
- :modal/id nil
- :modal/label ""
- :modal/show? false
- :modal/panel-content nil
- :modal/payload nil
- :modal/fullscreen? false
- :modal/close-btn? nil
- :modal/close-backdrop? true
- :modal/subsets []
- ;; ui
- :ui/viewport {}
- ;; left sidebar
- :ui/navigation-item-collapsed? {}
- :ui/recent-pages (or (storage/get :ui/recent-pages) {})
- ;; right sidebar
- :ui/handbooks-open? false
- :ui/help-open? false
- :ui/fullscreen? false
- :ui/settings-open? false
- :ui/sidebar-open? false
- :ui/sidebar-width "40%"
- :ui/left-sidebar-open? (boolean (storage/get "ls-left-sidebar-open?"))
- :ui/theme (or (storage/get :ui/theme) "light")
- :ui/system-theme? ((fnil identity (or util/mac? util/win32? false)) (storage/get :ui/system-theme?))
- :ui/custom-theme (or (storage/get :ui/custom-theme) {:light {:mode "light"} :dark {:mode "dark"}})
- :ui/wide-mode? (storage/get :ui/wide-mode)
- :ui/radix-color (storage/get :ui/radix-color)
- ;; ui/collapsed-blocks is to separate the collapse/expand state from db for:
- ;; 1. right sidebar
- ;; 2. zoom-in view
- ;; 3. queries
- ;; 4. references
- ;; graph => {:block-id bool}
- :ui/collapsed-blocks {}
- :ui/sidebar-collapsed-blocks {}
- :ui/root-component nil
- :ui/file-component nil
- :ui/developer-mode? (or (= (storage/get "developer-mode") "true")
- false)
- ;; remember scroll positions of visited paths
- :ui/paths-scroll-positions (atom {})
- :ui/main-container-scroll-top (atom nil)
- :ui/shortcut-tooltip? (if (false? (storage/get :ui/shortcut-tooltip?))
- false
- true)
- :ui/scrolling? (atom false)
- :ui/new-property-input-id nil
- :document/mode? document-mode?
- :config {}
- :block/component-editing-mode? false
- :editor/start-pos (atom nil)
- :editor/op (atom nil)
- :editor/hidden-editors #{} ;; page names
- :editor/draw-mode? false
- :editor/action (atom nil)
- :editor/action-data nil
- ;; With label or other data
- :editor/last-saved-cursor nil
- :editor/ref->editing? (atom {})
- :editor/editing-prev-node (atom nil)
- :editor/editing-parent-node (atom nil)
- :editor/in-composition? false
- :editor/content (atom {})
- :editor/block (atom nil)
- :editor/block-dom-id (atom nil)
- :editor/set-timestamp-block (atom nil) ;; click rendered block timestamp-cp to set timestamp
- :editor/last-input-time (atom {})
- :editor/document-mode? document-mode?
- :editor/args (atom nil)
- :editor/on-paste? (atom false)
- :editor/last-key-code (atom nil)
- :editor/block-op-type nil ;; :cut, :copy
- ;; Stores deleted refed blocks, indexed by repo
- :editor/last-replace-ref-content-tx nil
- ;; for audio record
- :editor/record-status "NONE"
- :editor/code-block-context {}
- :db/properties-changed-pages {}
- :editor/cursor-range (atom nil)
- :editor/new-created-blocks (atom #{})
- :selection/mode (atom false)
- ;; Warning: blocks order is determined when setting this attribute
- :selection/blocks (atom [])
- :selection/start-block (atom nil)
- ;; either :up or :down, defaults to down
- ;; used to determine selection direction when two or more blocks are selected
- :selection/direction (atom :down)
- :selection/selected-all? (atom false)
- :custom-context-menu/show? false
- :custom-context-menu/links nil
- :custom-context-menu/position nil
- ;; pages or blocks in the right sidebar
- ;; It is a list of `[repo db-id block-type block-data]` 4-tuple
- :sidebar/blocks '()
- :preferred-language (storage/get :preferred-language)
- ;; electron
- :electron/auto-updater-downloaded false
- :electron/updater-pending? false
- :electron/updater {}
- :electron/user-cfgs nil
- :electron/server nil
- :electron/window-maximized? false
- :electron/window-fullscreen? false
- ;; assets
- :assets/alias-enabled? (or (storage/get :assets/alias-enabled?) false)
- :assets/alias-dirs (or (storage/get :assets/alias-dirs) [])
- ;; mobile
- :mobile/container-urls nil
- :mobile/show-action-bar? false
- :mobile/actioned-block nil
- :mobile/show-toolbar? false
- :mobile/show-recording-bar? false
- :mobile/show-tabbar? false
- ;;; Used to monitor mobile app status,
- ;;; value spec:
- ;;; {:is-active? bool, :timestamp int}
- :mobile/app-state-change (atom nil)
- ;; plugin
- :plugin/enabled (and (util/electron?)
- ;; true false :theme-only
- ((fnil identity true) (storage/get ::storage-spec/lsp-core-enabled)))
- :plugin/preferences nil
- :plugin/indicator-text nil
- :plugin/installed-plugins {}
- :plugin/installed-themes []
- :plugin/installed-slash-commands {}
- :plugin/installed-ui-items {}
- :plugin/installed-resources {}
- :plugin/installed-hooks {}
- :plugin/installed-services {}
- :plugin/simple-commands {}
- :plugin/selected-theme nil
- :plugin/selected-unpacked-pkg nil
- :plugin/marketplace-pkgs nil
- :plugin/marketplace-stats nil
- :plugin/installing nil
- :plugin/active-readme nil
- :plugin/updates-auto-checking? false
- :plugin/updates-pending {}
- :plugin/updates-coming {}
- :plugin/updates-downloading? false
- :plugin/updates-unchecked #{}
- :plugin/navs-settings? true
- :plugin/focused-settings nil ;; plugin id
- ;; pdf
- :pdf/system-win? false
- :pdf/current nil
- :pdf/ref-highlight nil
- :pdf/block-highlight-colored? (or (storage/get "ls-pdf-hl-block-is-colored") true)
- :pdf/auto-open-ctx-menu? (not= false (storage/get "ls-pdf-auto-open-ctx-menu"))
- ;; all notification contents as k-v pairs
- :notification/contents {}
- :graph/syncing? false
- ;; graph -> state
- :graph/parsing-state {}
- :copy/export-block-text-indent-style (or (storage/get :copy/export-block-text-indent-style)
- "dashes")
- :copy/export-block-text-remove-options (or (storage/get :copy/export-block-text-remove-options)
- #{})
- :copy/export-block-text-other-options (or (storage/get :copy/export-block-text-other-options)
- {})
- :date-picker/date nil
- :youtube/players {}
- ;; command palette
- :command-palette/commands (atom [])
- :view/components {}
- :srs/mode? false
- :srs/cards-due-count nil
- :reactive/query-dbs {}
- ;; login, userinfo, token, ...
- :auth/refresh-token (storage/get "refresh-token")
- :auth/access-token nil
- :auth/id-token nil
- ;; file-sync
- :file-sync/jstour-inst nil
- :file-sync/onboarding-state (or (storage/get :file-sync/onboarding-state)
- {:welcome false})
- :file-sync/remote-graphs {:loading false :graphs nil}
- :file-sync/set-remote-graph-password-result {}
- ;; graph-uuid -> {:graphs-txid {}
- ;; :file-sync/sync-manager {}
- ;; :file-sync/sync-state {}
- ;; ;; {file-path -> payload}
- ;; :file-sync/progress {}
- ;; :file-sync/start-time {}
- ;; :file-sync/last-synced-at {}}
- :file-sync/graph-state {:current-graph-uuid nil}
- ;; graph-uuid -> ...
- ;; graph-url -> {:in-transaction? Boolean :txs []}
- :rtc/remote-batch-tx-state {}
- :user/info {:UserGroups (storage/get :user-groups)}
- :encryption/graph-parsing? false
- :ui/loading? {}
- :feature/enable-sync? (storage/get :logseq-sync-enabled)
- :feature/enable-sync-diff-merge? ((fnil identity true) (storage/get :logseq-sync-diff-merge-enabled))
- :file/rename-event-chan (async/chan 100)
- :ui/find-in-page nil
- :graph/importing nil
- :graph/importing-state {}
- :graph/loading? nil
- :handbook/route-chan (async/chan (async/sliding-buffer 1))
- :whiteboard/onboarding-whiteboard? (or (storage/get :ls-onboarding-whiteboard?) false)
- :whiteboard/onboarding-tour? (or (storage/get :whiteboard-onboarding-tour?) false)
- :whiteboard/last-persisted-at {}
- :whiteboard/pending-tx-data {}
- :history/page-only-mode? false
- :history/tx-before-editor-cursor (atom nil)
- ;; db tx-id -> editor cursor
- :history/tx->editor-cursor (atom {})
- :system/info {}
- ;; Whether block is selected
- :ui/select-query-cache (atom {})})))
- ;; Block ast state
- ;; ===============
- ;; block uuid -> {content(String) -> ast}
- (def blocks-ast-cache (atom {}))
- (defn add-block-ast-cache!
- [block-uuid content ast]
- (when (and block-uuid content ast)
- (let [new-value (assoc-in @blocks-ast-cache [block-uuid content] ast)
- new-value (if (> (count new-value) 10000)
- (into {} (take 5000 new-value))
- new-value)]
- (reset! blocks-ast-cache new-value))))
- (defn get-block-ast
- [block-uuid content]
- (when (and block-uuid content)
- (get-in @blocks-ast-cache [block-uuid content])))
- ;; User configuration getters under :config (and sometimes :me)
- ;; ========================================
- ;; TODO: Refactor default config values to be data driven. Currently they are all
- ;; buried in getters
- ;; TODO: Refactor our access to be more data driven. Currently each getter
- ;; (re-)fetches get-current-repo needlessly
- ;; TODO: Add consistent validation. Only a few config options validate at get time
- (def default-config
- "Default config for a repo-specific, user config"
- {:feature/enable-search-remove-accents? true
- :default-arweave-gateway "https://arweave.net"
- :ui/auto-expand-block-refs? true
- ;; For flushing the settings of old versions. Don't bump this value.
- ;; There are only two kinds of graph, one is not upgraded (:legacy) and one is upgraded (:triple-lowbar)
- ;; For not upgraded graphs, the config will have no key `:file/name-format`
- ;; Then the default value is applied
- :file/name-format :legacy})
- ;; State that most user config is dependent on
- (declare get-current-repo sub set-state!)
- (defn merge-configs
- "Merges user configs in given orders. All values are overridden except for maps
- which are merged."
- [& configs]
- (->> configs
- (filter map?)
- (apply merge-with
- (fn merge-config [current new]
- (if (and (map? current) (map? new))
- (merge current new)
- new)))))
- (defn get-global-config
- []
- (get-in @state [:config ::global-config]))
- (defn get-global-config-str-content
- []
- (get-in @state [:config ::global-config-str-content]))
- (defn get-graph-config
- ([] (get-graph-config (get-current-repo)))
- ([repo-url] (get-in @state [:config repo-url])))
- (defn get-config
- "User config for the given repo or current repo if none given. All config fetching
- should be done through this fn in order to get global config and config defaults"
- ([]
- (get-config (get-current-repo)))
- ([repo-url]
- (merge-configs
- default-config
- (get-global-config)
- (get-graph-config repo-url))))
- (defonce publishing? (atom nil))
- (defn publishing-enable-editing?
- []
- (and @publishing? (:publishing/enable-editing? (get-config))))
- (defn enable-editing?
- []
- (or (not @publishing?) (:publishing/enable-editing? (get-config))))
- (defn get-arweave-gateway
- []
- (:arweave/gateway (get-config)))
- (defonce built-in-macros
- {"img" "[:img.$4 {:src \"$1\" :style {:width $2 :height $3}}]"})
- (defn get-macros
- []
- (merge
- built-in-macros
- (:macros (get-config))))
- (defn set-assets-alias-enabled!
- [v]
- (set-state! :assets/alias-enabled? (boolean v))
- (storage/set :assets/alias-enabled? (boolean v)))
- (defn set-assets-alias-dirs!
- [dirs]
- (when dirs
- (set-state! :assets/alias-dirs dirs)
- (storage/set :assets/alias-dirs dirs)))
- (defn get-custom-css-link
- []
- (:custom-css-url (get-config)))
- (defn get-custom-js-link
- []
- (:custom-js-url (get-config)))
- (defn get-default-journal-template
- []
- (when-let [template (get-in (get-config) [:default-templates :journals])]
- (when-not (string/blank? template)
- (string/trim template))))
- (defn all-pages-public?
- []
- (let [value (:publishing/all-pages-public? (get-config))
- value (if (some? value) value (:all-pages-public? (get-config)))]
- (true? value)))
- (defn get-default-home
- []
- (:default-home (get-config)))
- (defn custom-home-page?
- []
- (some? (:page (get-default-home))))
- (defn get-preferred-format
- ([]
- (get-preferred-format (get-current-repo)))
- ([repo-url]
- (keyword
- (or
- (common-config/get-preferred-format (get-config repo-url))
- (get-in @state [:me :preferred_format] "markdown")))))
- (defn markdown?
- []
- (= (keyword (get-preferred-format))
- :markdown))
- (defn get-pages-directory
- []
- (or
- (when-let [repo (get-current-repo)]
- (:pages-directory (get-config repo)))
- "pages"))
- (defn get-journals-directory
- []
- (or
- (when-let [repo (get-current-repo)]
- (:journals-directory (get-config repo)))
- "journals"))
- (defn get-whiteboards-directory
- []
- (or
- (when-let [repo (get-current-repo)]
- (:whiteboards-directory (get-config repo)))
- "whiteboards"))
- (defn org-mode-file-link?
- [repo]
- (:org-mode/insert-file-link? (get-config repo)))
- (defn get-journal-file-name-format
- []
- (when-let [repo (get-current-repo)]
- (:journal/file-name-format (get-config repo))))
- (defn get-preferred-workflow
- []
- (keyword
- (or
- (when-let [workflow (:preferred-workflow (get-config))]
- (let [workflow (name workflow)]
- (if (util/safe-re-find #"now|NOW" workflow)
- :now
- :todo)))
- (get-in @state [:me :preferred_workflow] :now))))
- (defn get-preferred-todo
- []
- (if (= (get-preferred-workflow) :now)
- "LATER"
- "TODO"))
- (defn get-filename-format
- ([] (get-filename-format (get-current-repo)))
- ([repo]
- (:file/name-format (get-config repo))))
- (defn get-date-formatter
- []
- (common-config/get-date-formatter (get-config)))
- (defn shortcuts []
- (:shortcuts (get-config)))
- (defn get-commands
- []
- (:commands (get-config)))
- (defn get-scheduled-future-days
- []
- (let [days (:scheduled/future-days (get-config))]
- (or (when (int? days) days) 7)))
- (defn get-start-of-week
- []
- (or (:start-of-week (get-config))
- (get-in @state [:me :settings :start-of-week])
- 6))
- (defn get-ref-open-blocks-level
- []
- (or
- (when-let [value (:ref/default-open-blocks-level (get-config))]
- (when (integer? value)
- value))
- 2))
- (defn get-linked-references-collapsed-threshold
- []
- (or
- (when-let [value (:ref/linked-references-collapsed-threshold (get-config))]
- (when (integer? value)
- value))
- 100))
- (defn get-export-bullet-indentation
- []
- (case (get (get-config) :export/bullet-indentation :tab)
- :eight-spaces
- " "
- :four-spaces
- " "
- :two-spaces
- " "
- :tab
- "\t"))
- (defn enable-search-remove-accents?
- []
- (:feature/enable-search-remove-accents? (get-config)))
- ;; State cursor fns for use with rum components
- ;; ============================================
- (declare document-mode?)
- (defn sub
- "Creates a rum cursor, https://github.com/tonsky/rum#cursors, for use in rum components.
- Similar to re-frame subscriptions"
- [ks & {:keys [path-in-sub-atom]}]
- (let [ks-coll? (coll? ks)
- get-fn (if ks-coll? get-in get)
- s (get-fn @state ks)
- s-atom? (util/atom? s)
- path-coll?-in-sub-atom (coll? path-in-sub-atom)]
- (cond
- (and s-atom? path-in-sub-atom path-coll?-in-sub-atom)
- (util/react (rum/cursor-in s path-in-sub-atom))
- (and s-atom? path-in-sub-atom)
- (util/react (rum/cursor s path-in-sub-atom))
- s-atom? (util/react s)
- ks-coll? (util/react (rum/cursor-in state ks))
- :else (util/react (rum/cursor state ks)))))
- (defn sub-editing?
- [block-ref]
- (when block-ref
- (when (nil? (get @(:editor/ref->editing? @state) block-ref))
- (swap! (:editor/ref->editing? @state) assoc block-ref (atom false)))
- (rum/react
- (get @(:editor/ref->editing? @state) block-ref))))
- (defn sub-config
- "Sub equivalent to get-config which should handle all sub user-config access"
- ([] (sub-config (get-current-repo)))
- ([repo]
- (let [config (sub :config)]
- (merge-configs default-config
- (get config ::global-config)
- (get config repo)))))
- (defn enable-grammarly?
- []
- (true? (:feature/enable-grammarly? (sub-config))))
- (defn scheduled-deadlines-disabled?
- []
- (true? (:feature/disable-scheduled-and-deadline-query? (sub-config))))
- (defn enable-timetracking?
- []
- (not (false? (:feature/enable-timetracking? (sub-config)))))
- (defn enable-fold-button-right?
- []
- (let [_ (sub :ui/viewport)]
- (and (util/mobile?)
- (util/sm-breakpoint?))))
- (defn enable-journals?
- ([]
- (enable-journals? (get-current-repo)))
- ([repo]
- (not (false? (:feature/enable-journals? (sub-config repo))))))
- (defn enable-flashcards?
- ([]
- (enable-flashcards? (get-current-repo)))
- ([repo]
- (not (false? (:feature/enable-flashcards? (sub-config repo))))))
- (defn enable-sync?
- []
- (sub :feature/enable-sync?))
- (defn enable-sync-diff-merge?
- []
- (sub :feature/enable-sync-diff-merge?))
- (defn enable-whiteboards?
- ([]
- (enable-whiteboards? (get-current-repo)))
- ([repo]
- (not (false? (:feature/enable-whiteboards? (sub-config repo))))))
- (defn enable-rtc?
- [repo]
- (:feature/enable-rtc? (sub-config repo)))
- (defn enable-git-auto-push?
- [repo]
- (not (false? (:git-auto-push (sub-config repo)))))
- (defn graph-settings
- []
- (:graph/settings (sub-config)))
- ;; Enable by default
- (defn show-brackets?
- []
- (not (false? (:ui/show-brackets? (sub-config)))))
- (defn sub-default-home-page
- []
- (get-in (sub-config) [:default-home :page] ""))
- (defn- get-selected-block-ids
- [blocks]
- (->> blocks
- (remove nil?)
- (keep #(when-let [id (dom/attr % "blockid")]
- (uuid id)))
- (distinct)))
- (defn sub-block-selected?
- [block-uuid]
- (let [*cache (:ui/select-query-cache @state)
- keys [::select-block block-uuid]
- atom (or (get @*cache keys)
- (let [result (rum/derived-atom
- [(:selection/blocks @state)]
- keys
- (fn [s]
- (contains? (set (get-selected-block-ids s)) block-uuid)))]
- (swap! *cache assoc keys result)
- result))]
- (rum/react atom)))
- (defn block-content-max-length
- [repo]
- (or (:block/content-max-length (sub-config repo)) 10000))
- (defn mobile?
- []
- (or (util/mobile?) (mobile-util/native-platform?)))
- (defn enable-tooltip?
- []
- (if (mobile?)
- false
- (get (sub-config) :ui/enable-tooltip? true)))
- (defn show-command-doc?
- []
- (get (sub-config) :ui/show-command-doc? true))
- (defn logical-outdenting?
- []
- (:editor/logical-outdenting? (sub-config)))
- (defn show-full-blocks?
- []
- (:ui/show-full-blocks? (sub-config)))
- (defn preferred-pasting-file?
- []
- (:editor/preferred-pasting-file? (sub-config)))
- (defn auto-expand-block-refs?
- []
- (:ui/auto-expand-block-refs? (sub-config)))
- (defn doc-mode-enter-for-new-line?
- []
- (and (document-mode?)
- (not (:shortcut/doc-mode-enter-for-new-block? (get-config)))))
- (defn user-groups
- []
- (set (sub [:user/info :UserGroups])))
- ;; State mutation helpers
- ;; ======================
- (defn set-state!
- [path value & {:keys [path-in-sub-atom]}]
- (swap! *profile-state update path inc)
- (let [path-coll? (coll? path)
- get-fn (if path-coll? get-in get)
- s (get-fn @state path)
- s-atom? (util/atom? s)
- path-coll?-in-sub-atom (coll? path-in-sub-atom)]
- (cond
- (and s-atom? path-in-sub-atom path-coll?-in-sub-atom)
- (let [old-v (get-in @s path-in-sub-atom)]
- (when (not= old-v value)
- (swap! s assoc-in path-in-sub-atom value)))
- (and s-atom? path-in-sub-atom)
- (let [old-v (get @s path-in-sub-atom)]
- (when (not= old-v value)
- (swap! s assoc path-in-sub-atom value)))
- s-atom?
- (when (not= @s value)
- (reset! s value))
- path-coll?
- (when (not= s value)
- (swap! state assoc-in path value))
- :else
- (when (not= s value)
- (swap! state assoc path value))))
- nil)
- (defn update-state!
- [path f & {:keys [path-in-sub-atom]}]
- (swap! *profile-state update path inc)
- (let [path-coll? (coll? path)
- get-fn (if path-coll? get-in get)
- s (get-fn @state path)
- s-atom? (util/atom? s)
- path-coll?-in-sub-atom (coll? path-in-sub-atom)]
- (cond
- (and s-atom? path-in-sub-atom path-coll?-in-sub-atom)
- (swap! s update-in path-in-sub-atom f)
- (and s-atom? path-in-sub-atom)
- (swap! s update path-in-sub-atom f)
- s-atom? (swap! s f)
- path-coll? (swap! state update-in path f)
- :else (swap! state update path f)))
- nil)
- ;; State getters and setters
- ;; =========================
- ;; These fns handle any key except :config.
- ;; Some state is also stored in local storage and/or sent to electron's main process
- (defn get-route-match
- []
- (:route-match @state))
- (defn get-current-route
- []
- (get-in (get-route-match) [:data :name]))
- (defn home?
- []
- (= :home (get-current-route)))
- (defn whiteboard-dashboard?
- []
- (= :whiteboards (get-current-route)))
- (defn setups-picker?
- []
- (= :repo-add (get-current-route)))
- (defn get-current-page
- []
- (when (contains? #{:whiteboard :page} (get-current-route)) ; TODO: move /whiteboard to /page
- (get-in (get-route-match)
- [:path-params :name])))
- (defn whiteboard-route?
- []
- (= :whiteboard (get-current-route)))
- (defn get-current-whiteboard
- []
- (when (whiteboard-route?)
- (get-in (get-route-match)
- [:path-params :name])))
- (defn route-has-p?
- []
- (get-in (get-route-match) [:query-params :p]))
- (defn get-current-repo
- "Returns the current repo URL, or else open demo graph"
- []
- (or (:git/current-repo @state)
- "Logseq demo"))
- (defn get-remote-graphs
- []
- (get-in @state [:file-sync/remote-graphs :graphs]))
- (defn get-remote-graph-info-by-uuid
- [uuid]
- (when-let [graphs (seq (get-in @state [:file-sync/remote-graphs :graphs]))]
- (some #(when (= (:GraphUUID %) (str uuid)) %) graphs)))
- (defn get-remote-graph-usage
- []
- (when-let [graphs (seq (get-in @state [:file-sync/remote-graphs :graphs]))]
- (->> graphs
- (map #(hash-map :uuid (:GraphUUID %)
- :name (:GraphName %)
- :used-gbs (/ (:GraphStorageUsage %) 1024 1024 1024)
- :limit-gbs (/ (:GraphStorageLimit %) 1024 1024 1024)
- :used-percent (/ (:GraphStorageUsage %) (:GraphStorageLimit %) 0.01)))
- (map #(assoc % :free-gbs (- (:limit-gbs %) (:used-gbs %))))
- (vec))))
- (defn delete-remote-graph!
- [repo]
- (swap! state update-in [:file-sync/remote-graphs :graphs]
- (fn [repos]
- (remove #(and
- (:GraphUUID repo)
- (:GraphUUID %)
- (= (:GraphUUID repo) (:GraphUUID %))) repos))))
- (defn add-remote-graph!
- [repo]
- (swap! state update-in [:file-sync/remote-graphs :graphs]
- (fn [repos]
- (->> (conj repos repo)
- (distinct)))))
- (defn get-repos
- []
- (get-in @state [:me :repos]))
- (defn set-repos!
- [repos]
- (set-state! [:me :repos] repos))
- (defn add-repo!
- [repo]
- (when (not (string/blank? repo))
- (update-state! [:me :repos]
- (fn [repos]
- (->> (conj repos repo)
- (distinct))))))
- (defn set-current-repo!
- [repo]
- (swap! state assoc :git/current-repo repo)
- (if repo
- (storage/set :git/current-repo repo)
- (storage/remove :git/current-repo))
- (ipc/ipc "setCurrentGraph" repo))
- (defn set-preferred-format!
- [format]
- (swap! state assoc-in [:me :preferred_format] (name format)))
- (defn set-preferred-workflow!
- [workflow]
- (swap! state assoc-in [:me :preferred_workflow] (name workflow)))
- (defn set-preferred-language!
- [language]
- (set-state! :preferred-language (name language))
- (storage/set :preferred-language (name language)))
- (defn delete-repo!
- [repo]
- (swap! state update-in [:me :repos]
- (fn [repos]
- (->> (remove #(or (= (:url repo) (:url %))
- (and
- (:GraphUUID repo)
- (:GraphUUID %)
- (= (:GraphUUID repo) (:GraphUUID %)))) repos)
- (util/distinct-by :url)))))
- (defn set-timestamp-block!
- [value]
- (set-state! :editor/set-timestamp-block value))
- (defn get-timestamp-block
- []
- @(:editor/set-timestamp-block @state))
- (defn get-edit-block
- []
- @(get @state :editor/block))
- (defn set-edit-content!
- ([input-id value] (set-edit-content! input-id value true))
- ([input-id value set-input-value?]
- (when input-id
- (when set-input-value?
- (when-let [input (gdom/getElement input-id)]
- (util/set-change-value input value)))
- (set-state! :editor/content value :path-in-sub-atom
- (or (:block/uuid (get-edit-block)) input-id)))))
- (defn get-edit-input-id
- []
- (when-not (exists? js/process)
- (or
- (when-let [node @*editor-editing-ref]
- (some-> (dom/sel1 node "textarea")
- (gobj/get "id")))
- (try
- (when-let [elem js/document.activeElement]
- (when (util/input? elem)
- (let [id (gobj/get elem "id")]
- (when (string/starts-with? id "edit-block-")
- id))))
- (catch :default _e)))))
- (defn get-input
- []
- (when-let [id (get-edit-input-id)]
- (gdom/getElement id)))
- (defn get-edit-block-node
- []
- @*editor-editing-ref)
- (defn editing?
- []
- (let [input (get-input)]
- (and input (= input (.-activeElement js/document)))))
- (defn get-edit-content
- []
- (when-let [id (:block/uuid (get-edit-block))]
- (get @(:editor/content @state) id)))
- (defn sub-edit-content
- ([]
- (sub-edit-content (:block/uuid (get-edit-block))))
- ([block-id]
- (when block-id
- (sub :editor/content {:path-in-sub-atom block-id}))))
- (defn get-cursor-range
- []
- @(:editor/cursor-range @state))
- (defn set-cursor-range!
- [range]
- (set-state! :editor/cursor-range range))
- (defn set-search-mode!
- [value]
- (set-state! :search/mode value))
- (defn set-editor-action!
- [value]
- (set-state! :editor/action value))
- (defn set-editor-action-data!
- [value]
- (set-state! :editor/action-data value))
- (defn get-editor-action
- []
- @(:editor/action @state))
- (defn get-editor-action-data
- []
- (:editor/action-data @state))
- (defn get-editor-show-page-search?
- []
- (= (get-editor-action) :page-search))
- (defn get-editor-show-page-search-hashtag?
- []
- (= (get-editor-action) :page-search-hashtag))
- (defn get-editor-show-block-search?
- []
- (= (get-editor-action) :block-search))
- (defn set-editor-show-input!
- [value]
- (if value
- (do
- (set-editor-action-data! (assoc (get-editor-action-data) :options value))
- (set-editor-action! :input))
- (do
- (set-editor-action! nil)
- (set-editor-action-data! nil))))
- (defn get-editor-show-input
- []
- (when (= (get-editor-action) :input)
- (get @state :editor/action-data)))
- (defn set-editor-show-commands!
- []
- (when-not (get-editor-action) (set-editor-action! :commands)))
- (defn set-editor-show-block-commands!
- []
- (when-not (get-editor-action) (set-editor-action! :block-commands)))
- (defn clear-editor-action!
- []
- (set-state! :editor/action nil))
- (defn get-edit-pos
- []
- (when-let [input (get-input)]
- (util/get-selection-start input)))
- (defn get-selection-start-block
- []
- @(get @state :selection/start-block))
- (defn set-selection-start-block!
- [start-block]
- (set-state! :selection/start-block start-block))
- (defn set-selection-blocks!
- ([blocks]
- (set-selection-blocks! blocks :down))
- ([blocks direction]
- (when (seq blocks)
- (let [blocks (vec (util/sort-by-height (remove nil? blocks)))]
- (set-state! :selection/mode true)
- (set-state! :selection/blocks blocks)
- (set-state! :selection/direction direction)))))
- (defn into-selection-mode!
- []
- (set-state! :selection/mode true))
- (defn clear-selection!
- []
- (set-state! :selection/mode false)
- (set-state! :selection/blocks nil)
- (set-state! :selection/direction :down)
- (set-state! :selection/start-block nil)
- (set-state! :selection/selected-all? false))
- (defn get-selection-blocks
- []
- (let [blocks (util/sort-by-height (bean/->clj (dom/by-class "ls-block selected")))]
- (set-state! :selecton/blocks blocks)
- blocks))
- (defn get-selection-block-ids
- []
- (get-selected-block-ids (get-selection-blocks)))
- (defn get-selection-start-block-or-first
- []
- (or (get-selection-start-block)
- (some-> (first (get-selection-blocks))
- (gobj/get "id"))))
- (defn in-selection-mode?
- []
- @(:selection/mode @state))
- (defn selection?
- "True sense of selection mode with valid selected block"
- []
- (and (in-selection-mode?) (seq (get-selection-blocks))))
- (defn conj-selection-block!
- [block-or-blocks direction]
- (let [selection-blocks (get-selection-blocks)
- blocks (-> (if (sequential? block-or-blocks)
- (apply conj selection-blocks block-or-blocks)
- (conj selection-blocks block-or-blocks))
- distinct
- util/sort-by-height
- vec)]
- (set-state! :selection/mode true)
- (set-state! :selection/blocks blocks)
- (set-state! :selection/direction direction)))
- (defn drop-selection-block!
- [block]
- (set-state! :selection/mode true)
- (set-state! :selection/blocks (-> (remove #(= block %) (get-selection-blocks))
- util/sort-by-height
- vec)))
- (defn drop-last-selection-block!
- []
- (let [direction @(:selection/direction @state)
- up? (= direction :up)
- blocks @(:selection/blocks @state)
- last-block (if up?
- (first blocks)
- (peek (vec blocks)))
- blocks' (-> (if up?
- (rest blocks)
- (pop (vec blocks)))
- util/sort-by-height
- vec)]
- (set-state! :selection/mode true)
- (set-state! :selection/blocks blocks')
- last-block))
- (defn get-selection-direction
- []
- @(:selection/direction @state))
- (defn show-custom-context-menu!
- [links position]
- (swap! state assoc
- :custom-context-menu/show? true
- :custom-context-menu/links links
- :custom-context-menu/position position))
- (defn hide-custom-context-menu!
- []
- (swap! state assoc
- :custom-context-menu/show? false
- :custom-context-menu/links nil
- :custom-context-menu/position nil))
- (defn toggle-navigation-item-collapsed!
- [item]
- (update-state! [:ui/navigation-item-collapsed? item] not))
- (defn toggle-sidebar-open?!
- []
- (swap! state update :ui/sidebar-open? not))
- (defn open-right-sidebar!
- []
- (swap! state assoc :ui/sidebar-open? true))
- (defn hide-right-sidebar!
- []
- (swap! state assoc :ui/sidebar-open? false))
- (defn sidebar-add-block!
- [repo db-id block-type]
- (when (not (util/sm-breakpoint?))
- (when db-id
- (update-state! :sidebar/blocks (fn [blocks]
- (->> (remove #(= (second %) db-id) blocks)
- (cons [repo db-id block-type])
- (distinct))))
- (set-state! [:ui/sidebar-collapsed-blocks db-id] false)
- (open-right-sidebar!)
- (when-let [elem (gdom/getElementByClass "sidebar-item-list")]
- (util/scroll-to elem 0)))))
- (defn sidebar-move-block!
- [from to]
- (update-state! :sidebar/blocks (fn [blocks]
- (let [to (if (> from to) (inc to) to)]
- (if (not= to from)
- (let [item (nth blocks from)
- blocks (keep-indexed #(when (not= %1 from) %2) blocks)
- [l r] (split-at to blocks)]
- (concat l [item] r))
- blocks)))))
- (defn sidebar-remove-block!
- [idx]
- (update-state! :sidebar/blocks (fn [blocks]
- (if (string? idx)
- (remove #(= (second %) idx) blocks)
- (util/drop-nth idx blocks))))
- (when (empty? (:sidebar/blocks @state))
- (hide-right-sidebar!)))
- (defn sidebar-remove-rest!
- [db-id]
- (update-state! :sidebar/blocks (fn [blocks]
- (remove #(not= (second %) db-id) blocks)))
- (set-state! [:ui/sidebar-collapsed-blocks db-id] false))
- (defn sidebar-replace-block!
- [old-sidebar-key new-sidebar-key]
- (update-state! :sidebar/blocks (fn [blocks]
- (map #(if (= % old-sidebar-key)
- new-sidebar-key
- %) blocks))))
- (defn sidebar-block-exists?
- [idx]
- (some #(= (second %) idx) (:sidebar/blocks @state)))
- (defn clear-sidebar-blocks!
- []
- (set-state! :sidebar/blocks '()))
- (defn sidebar-block-toggle-collapse!
- [db-id]
- (when db-id
- (update-state! [:ui/sidebar-collapsed-blocks db-id] not)))
- (defn sidebar-block-collapse-rest!
- [db-id]
- (let [items (disj (set (map second (:sidebar/blocks @state))) db-id)]
- (doseq [item items] (set-state! [:ui/sidebar-collapsed-blocks item] true))))
- (defn sidebar-block-set-collapsed-all!
- [collapsed?]
- (let [items (map second (:sidebar/blocks @state))]
- (doseq [item items]
- (set-state! [:ui/sidebar-collapsed-blocks item] collapsed?))))
- (defn get-current-edit-block-and-position
- []
- (let [edit-input-id (get-edit-input-id)
- edit-block (get-edit-block)
- block-element (when edit-input-id (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block")))
- container (when block-element
- (util/get-block-container block-element))]
- (when container
- {:last-edit-block edit-block
- :container (gobj/get container "id")
- :pos @(:editor/start-pos @state)
- :end-pos (or (cursor/pos (gdom/getElement edit-input-id))
- (count (:block/content edit-block)))})))
- (defn clear-edit!
- []
- (when-let [prev-ref @*editor-editing-ref]
- (reset! (get @(:editor/ref->editing? @state) prev-ref) false))
- (reset! *editor-editing-ref nil)
- (set-state! :editor/editing-prev-node nil)
- (set-state! :editor/editing-parent-node nil)
- (set-state! :editor/cursor-range nil)
- (swap! state merge {:editor/last-saved-cursor nil})
- (set-state! :editor/content {})
- (set-state! :editor/block nil)
- (set-state! :ui/select-query-cache {}))
- (defn into-code-editor-mode!
- []
- (when-let [prev-ref @*editor-editing-ref]
- (reset! (get @(:editor/ref->editing? @state) prev-ref) false))
- (reset! *editor-editing-ref nil)
- (set-state! :editor/cursor-range nil)
- (swap! state merge {:editor/code-mode? true}))
- (defn set-editor-last-pos!
- [new-pos]
- (set-state! [:editor/last-saved-cursor (:block/uuid (get-edit-block))] new-pos))
- (defn clear-editor-last-pos!
- []
- (set-state! :editor/last-saved-cursor nil))
- (defn get-editor-last-pos
- []
- (get-in @state [:editor/last-saved-cursor (:block/uuid (get-edit-block))]))
- (defn set-block-content-and-last-pos!
- [edit-input-id content new-pos]
- (when edit-input-id
- (set-edit-content! edit-input-id content)
- (set-state! [:editor/last-saved-cursor (:block/uuid (get-edit-block))] new-pos)))
- (defn set-theme-mode!
- [mode]
- (when (mobile-util/native-platform?)
- (if (= mode "light")
- (util/set-theme-light)
- (util/set-theme-dark)))
- (set-state! :ui/theme mode)
- (storage/set :ui/theme mode))
- (defn sync-system-theme!
- []
- (let [system-dark? (.-matches (js/window.matchMedia "(prefers-color-scheme: dark)"))]
- (set-theme-mode! (if system-dark? "dark" "light"))
- (set-state! :ui/system-theme? true)
- (storage/set :ui/system-theme? true)))
- (defn use-theme-mode!
- [theme-mode]
- (if (= theme-mode "system")
- (sync-system-theme!)
- (do
- (set-theme-mode! theme-mode)
- (set-state! :ui/system-theme? false)
- (storage/set :ui/system-theme? false))))
- (defn- toggle-theme
- [theme]
- (if (= theme "dark") "light" "dark"))
- (defn toggle-theme!
- []
- (use-theme-mode! (toggle-theme (:ui/theme @state))))
- (defn set-custom-theme!
- ([custom-theme]
- (set-custom-theme! nil custom-theme))
- ([mode theme]
- (set-state! (if mode [:ui/custom-theme (keyword mode)] :ui/custom-theme) theme)
- (storage/set :ui/custom-theme (:ui/custom-theme @state))))
- (defn restore-mobile-theme!
- "Restore mobile theme setting from local storage"
- []
- (let [mode (or (storage/get :ui/theme) "light")
- system-theme? (storage/get :ui/system-theme?)]
- (when (and (not system-theme?)
- (mobile-util/native-platform?))
- (if (= mode "light")
- (util/set-theme-light)
- (util/set-theme-dark)))))
- (defn set-editing-block-dom-id!
- [block-dom-id]
- (set-state! :editor/block-dom-id block-dom-id))
- (defn get-editing-block-dom-id
- []
- @(:editor/block-dom-id @state))
- (defn set-root-component!
- [component]
- (set-state! :ui/root-component component))
- (defn get-root-component
- []
- (get @state :ui/root-component))
- (defn load-app-user-cfgs
- ([] (load-app-user-cfgs false))
- ([refresh?]
- (when (util/electron?)
- (p/let [cfgs (if (or refresh? (nil? (:electron/user-cfgs @state)))
- (ipc/ipc :userAppCfgs)
- (:electron/user-cfgs @state))
- cfgs (if (object? cfgs) (bean/->clj cfgs) cfgs)]
- (set-state! :electron/user-cfgs cfgs)))))
- (defn setup-electron-updater!
- []
- (when (util/electron?)
- (js/window.apis.setUpdatesCallback
- (fn [_ args]
- (let [data (bean/->clj args)
- pending? (not= (:type data) "completed")]
- (set-state! :electron/updater-pending? pending?)
- (when pending? (set-state! :electron/updater data))
- nil)))))
- (defn set-file-component!
- [component]
- (set-state! :ui/file-component component))
- (defn clear-file-component!
- []
- (set-state! :ui/file-component nil))
- (defn set-journals-length!
- [value]
- (when value
- (set-state! :journals-length value)))
- (defn save-scroll-position!
- ([value]
- (save-scroll-position! value js/window.location.hash))
- ([value path]
- (set-state! :ui/paths-scroll-positions value :path-in-sub-atom path)))
- (defn save-main-container-position!
- [value]
- (when (not= value @(:ui/main-container-scroll-top @state))
- (set-state! :ui/main-container-scroll-top value)))
- (defn get-saved-scroll-position
- ([]
- (get-saved-scroll-position js/window.location.hash))
- ([path]
- (get @(get @state :ui/paths-scroll-positions) path 0)))
- (defn set-today!
- [value]
- (set-state! :today value))
- (defn get-me
- []
- (:me @state))
- (defn set-db-restoring!
- [value]
- (set-state! :db/restoring? value))
- (defn set-indexedb-support!
- [value]
- (set-state! :indexeddb/support? value))
- (defn modal-opened?
- []
- (:modal/show? @state))
- (declare set-modal!)
- (declare close-modal!)
- (defn get-sub-modals
- []
- (:modal/subsets @state))
- (defn set-sub-modal!
- ([panel-content]
- (set-sub-modal! panel-content
- {:close-btn? true}))
- ([panel-content {:keys [id label payload close-btn? close-backdrop? show? center?] :as opts}]
- (if (not (modal-opened?))
- (set-modal! panel-content opts)
- (let [modals (:modal/subsets @state)
- idx (and id (first (keep-indexed #(when (= (:modal/id %2) id) %1)
- modals)))
- input (medley/filter-vals
- #(not (nil? %1))
- {:modal/id id
- :modal/label (or label (if center? "ls-modal-align-center" ""))
- :modal/payload payload
- :modal/show? (if (boolean? show?) show? true)
- :modal/panel-content panel-content
- :modal/close-btn? close-btn?
- :modal/close-backdrop? (if (boolean? close-backdrop?) close-backdrop? true)})]
- (swap! state update-in
- [:modal/subsets (or idx (count modals))]
- merge input)
- (:modal/subsets @state)))))
- (defn close-sub-modal!
- ([] (close-sub-modal! nil))
- ([all?-a-id]
- (if (true? all?-a-id)
- (swap! state assoc :modal/subsets [])
- (let [id all?-a-id
- mid (:modal/id @state)
- modals (:modal/subsets @state)]
- (if (and id (not (string/blank? mid)) (= id mid))
- (close-modal!)
- (when-let [idx (if id (first (keep-indexed #(when (= (:modal/id %2) id) %1) modals))
- (dec (count modals)))]
- (swap! state assoc :modal/subsets (into [] (medley/remove-nth idx modals)))))))
- (:modal/subsets @state)))
- (defn set-modal!
- ([modal-panel-content]
- (set-modal! modal-panel-content
- {:fullscreen? false
- :close-btn? true}))
- ([modal-panel-content {:keys [id label payload fullscreen? close-btn? close-backdrop? center? panel?
- container-overflow-visible? style]}]
- (let [opened? (modal-opened?)
- style (if container-overflow-visible?
- (merge style {:overflow "visible"})
- style)]
- (when opened?
- (close-modal!))
- (when (seq (get-sub-modals))
- (close-sub-modal! true))
- (async/go
- (when opened?
- (<! (async/timeout 100)))
- (swap! state assoc
- :modal/id id
- :modal/label (or label (if center? "ls-modal-align-center" ""))
- :modal/show? (boolean modal-panel-content)
- :modal/panel-content modal-panel-content
- :modal/payload payload
- :modal/fullscreen? fullscreen?
- :modal/panel? (if (boolean? panel?) panel? true)
- :modal/close-btn? close-btn?
- :modal/close-backdrop? (if (boolean? close-backdrop?) close-backdrop? true)
- :modal/style style)))
- nil))
- (defn close-dropdowns!
- []
- (let [close-fns (vals (:modal/dropdowns @state))]
- (doseq [f close-fns]
- (try (f) (catch :default _e nil)))))
- (defn close-modal!
- []
- (when-not (or (editing?) (:error/multiple-tabs-access-opfs? @state))
- (close-dropdowns!)
- (if (seq (get-sub-modals))
- (close-sub-modal!)
- (swap! state assoc
- :modal/id nil
- :modal/label ""
- :modal/payload nil
- :modal/show? false
- :modal/fullscreen? false
- :modal/panel-content nil
- :modal/dropdowns {}
- :ui/open-select nil))))
- (defn get-reactive-custom-queries-chan
- []
- (:reactive/custom-queries @state))
- (defn get-left-sidebar-open?
- []
- (get-in @state [:ui/left-sidebar-open?]))
- (defn set-left-sidebar-open!
- [value]
- (storage/set "ls-left-sidebar-open?" (boolean value))
- (set-state! :ui/left-sidebar-open? value))
- (defn toggle-left-sidebar!
- []
- (set-left-sidebar-open!
- (not (get-left-sidebar-open?))))
- (defn set-developer-mode!
- [value]
- (set-state! :ui/developer-mode? value)
- (storage/set "developer-mode" (str value)))
- (defn developer-mode?
- []
- (:ui/developer-mode? @state))
- (defn get-notification-contents
- []
- (get @state :notification/contents))
- (defn document-mode?
- []
- (get @state :document/mode?))
- (defn toggle-document-mode!
- []
- (let [mode (document-mode?)]
- (set-state! :document/mode? (not mode))
- (storage/set :document/mode? (not mode))))
- (defn shortcut-tooltip-enabled?
- []
- (get @state :ui/shortcut-tooltip?))
- (defn toggle-shortcut-tooltip!
- []
- (let [mode (shortcut-tooltip-enabled?)]
- (set-state! :ui/shortcut-tooltip? (not mode))
- (storage/set :ui/shortcut-tooltip? (not mode))))
- (defn set-config!
- [repo-url value]
- (when value (set-state! [:config repo-url] value)))
- (defn set-global-config!
- [value str-content]
- ;; Placed under :config so cursors can work seamlessly
- (when value
- (set-config! ::global-config value)
- (set-config! ::global-config-str-content str-content)))
- (defn get-wide-mode?
- []
- (:ui/wide-mode? @state))
- (defn toggle-wide-mode!
- []
- (update-state! :ui/wide-mode? not))
- (defn set-online!
- [value]
- (set-state! :network/online? value))
- (defn get-plugins-slash-commands
- []
- (mapcat seq (flatten (vals (:plugin/installed-slash-commands @state)))))
- (defn get-plugins-commands-with-type
- [type]
- (->> (apply concat (vals (:plugin/simple-commands @state)))
- (filterv #(= (keyword (first %)) (keyword type)))))
- (defn get-plugins-ui-items-with-type
- [type]
- (->> (apply concat (vals (:plugin/installed-ui-items @state)))
- (filterv #(= (keyword (first %)) (keyword type)))))
- (defn get-plugin-resources-with-type
- [pid type]
- (when-let [pid (and type (keyword pid))]
- (get-in @state [:plugin/installed-resources pid (keyword type)])))
- (defn get-plugin-resource
- [pid type key]
- (when-let [resources (get-plugin-resources-with-type pid type)]
- (get resources key)))
- (defn upt-plugin-resource
- [pid type key attr val]
- (when-let [resource (get-plugin-resource pid type key)]
- (let [resource (assoc resource (keyword attr) val)]
- (set-state!
- [:plugin/installed-resources (keyword pid) (keyword type) key] resource)
- resource)))
- (defn get-plugin-services
- [pid type]
- (when-let [installed (and pid (:plugin/installed-services @state))]
- (some->> (seq (get installed (keyword pid)))
- (filterv #(= type (:type %))))))
- (defn install-plugin-service
- ([pid type name] (install-plugin-service pid type name nil))
- ([pid type name opts]
- (when-let [pid (and pid type name (keyword pid))]
- (let [exists (get-plugin-services pid type)]
- (when-let [service (and (or (not exists) (not (some #(= name (:name %)) exists)))
- {:pid pid :type type :name name :opts opts})]
- (update-state! [:plugin/installed-services pid] #(conj (vec %) service))
- ;; search engines state for results
- (when (= type :search)
- (set-state! [:search/engines (str pid name)] service)))))))
- (defn uninstall-plugin-service
- [pid type-or-all]
- (when-let [pid (keyword pid)]
- (when-let [installed (get (:plugin/installed-services @state) pid)]
- (let [remove-all? (or (true? type-or-all) (nil? type-or-all))
- remains (if remove-all? nil (filterv #(not= type-or-all (:type %)) installed))
- removed (if remove-all? installed (filterv #(= type-or-all (:type %)) installed))]
- (set-state! [:plugin/installed-services pid] remains)
- ;; search engines state for results
- (when-let [removed' (seq (filter #(= :search (:type %)) removed))]
- (update-state! :search/engines #(apply dissoc % (mapv (fn [{:keys [pid name]}] (str pid name)) removed'))))))))
- (defn get-all-plugin-services-with-type
- [type]
- (when-let [installed (vals (:plugin/installed-services @state))]
- (mapcat (fn [s] (filter #(= (keyword type) (:type %)) s)) installed)))
- (defn get-all-plugin-search-engines
- []
- (:search/engines @state))
- (defn update-plugin-search-engine
- [pid name f]
- (when-let [pid (keyword pid)]
- (set-state! :search/engines
- (update-vals (get-all-plugin-search-engines)
- #(if (and (= pid (:pid %)) (= name (:name %)))
- (f %) %)))))
- (defn reset-plugin-search-engines
- []
- (when-let [engines (get-all-plugin-search-engines)]
- (set-state! :search/engines
- (update-vals engines #(assoc % :result nil)))))
- (defn install-plugin-hook
- ([pid hook] (install-plugin-hook pid hook true))
- ([pid hook opts]
- (when-let [pid (keyword pid)]
- (set-state!
- [:plugin/installed-hooks hook]
- (assoc
- ((fnil identity {}) (get-in @state [:plugin/installed-hooks hook]))
- pid opts)) true)))
- (defn uninstall-plugin-hook
- [pid hook-or-all]
- (when-let [pid (keyword pid)]
- (if (nil? hook-or-all)
- (swap! state update :plugin/installed-hooks #(update-vals % (fn [ids] (dissoc ids pid))))
- (when-let [coll (get-in @state [:plugin/installed-hooks hook-or-all])]
- (set-state! [:plugin/installed-hooks hook-or-all] (dissoc coll pid))))
- true))
- (defn slot-hook-exist?
- [uuid]
- (when-let [type (and uuid (string/replace (str uuid) "-" "_"))]
- (when-let [hooks (sub :plugin/installed-hooks)]
- (contains? hooks (str "hook:editor:slot_" type)))))
- (defn active-tldraw-app
- []
- (when-let [tldraw-el (.querySelector js/document.body ".logseq-tldraw[data-tlapp]")]
- (gobj/get js/window.tlapps (.. tldraw-el -dataset -tlapp))))
- (defn tldraw-editing-logseq-block?
- []
- (when-let [app (active-tldraw-app)]
- (and (= 1 (.. app -selectedShapesArray -length))
- (= (.. app -editingShape) (.. app -selectedShapesArray (at 0))))))
- (defn set-graph-syncing?
- [value]
- (set-state! :graph/syncing? value))
- (defn set-editor-in-composition!
- [value]
- (set-state! :editor/in-composition? value))
- (defn editor-in-composition?
- []
- (:editor/in-composition? @state))
- (defn set-loading-files!
- [repo value]
- (when repo
- (set-state! [:repo/loading-files? repo] value)))
- (defn loading-files?
- [repo]
- (get-in @state [:repo/loading-files? repo]))
- (defn set-editor-last-input-time!
- [repo time]
- (set-state! :editor/last-input-time time :path-in-sub-atom repo))
- (defn input-idle?
- [repo & {:keys [diff]
- :or {diff 1000}}]
- (when repo
- (let [last-input-time (get @(get @state :editor/last-input-time) repo)]
- (or
- (nil? last-input-time)
- (let [now (util/time-ms)]
- (>= (- now last-input-time) diff))
- ;; not in editing mode
- ;; Is this a good idea to put whiteboard check here?
- (not (get-edit-input-id))))))
- (defn set-nfs-refreshing!
- [value]
- (set-state! :nfs/refreshing? value))
- (defn nfs-refreshing?
- []
- (:nfs/refreshing? @state))
- (defn set-search-result!
- [value]
- (set-state! :search/result value))
- (defn clear-search-result!
- []
- (set-search-result! nil))
- (defn add-graph-search-filter!
- [q]
- (when-not (string/blank? q)
- (update-state! :search/graph-filters
- (fn [value]
- (vec (distinct (conj value q)))))))
- (defn remove-search-filter!
- [q]
- (when-not (string/blank? q)
- (update-state! :search/graph-filters
- (fn [value]
- (remove #{q} value)))))
- (defn clear-search-filters!
- []
- (set-state! :search/graph-filters []))
- (defn get-search-mode
- []
- (:search/mode @state))
- (defn toggle!
- [path]
- (update-state! path not))
- (defn toggle-settings!
- []
- (toggle! :ui/settings-open?))
- (defn settings-open?
- []
- (:ui/settings-open? @state))
- (defn close-settings!
- []
- (set-state! :ui/settings-open? false))
- (defn open-settings!
- ([] (open-settings! true))
- ([active-tab] (set-state! :ui/settings-open? active-tab)))
- (defn set-editor-op!
- [value]
- (set-state! :editor/op value))
- (defn get-editor-op
- []
- @(:editor/op @state))
- (defn get-events-chan
- []
- (:system/events @state))
- (defn pub-event!
- {:malli/schema [:=> [:cat vector?] :any]}
- [payload]
- (let [d (p/deferred)
- chan (get-events-chan)]
- (async/put! chan [payload d])
- d))
- (defn get-export-block-text-indent-style []
- (:copy/export-block-text-indent-style @state))
- (defn set-export-block-text-indent-style!
- [v]
- (set-state! :copy/export-block-text-indent-style v)
- (storage/set :copy/export-block-text-indent-style v))
- (defn get-recent-pages
- []
- (get-in @state [:ui/recent-pages (get-current-repo)]))
- (defn set-recent-pages!
- [v]
- (set-state! [:ui/recent-pages (get-current-repo)] v)
- (storage/set :ui/recent-pages (:ui/recent-pages @state)))
- (defn get-export-block-text-remove-options []
- (:copy/export-block-text-remove-options @state))
- (defn update-export-block-text-remove-options!
- [e k]
- (let [f (if (util/echecked? e) conj disj)]
- (update-state! :copy/export-block-text-remove-options
- #(f % k))
- (storage/set :copy/export-block-text-remove-options
- (get-export-block-text-remove-options))))
- (defn get-export-block-text-other-options []
- (:copy/export-block-text-other-options @state))
- (defn update-export-block-text-other-options!
- [k v]
- (update-state! :copy/export-block-text-other-options #(assoc % k v)))
- (defn set-editor-args!
- [args]
- (set-state! :editor/args args))
- (defn editing-whiteboard-portal?
- []
- (and (active-tldraw-app) (tldraw-editing-logseq-block?)))
- (defn block-component-editing?
- []
- (and (:block/component-editing-mode? @state)
- (not (editing-whiteboard-portal?))))
- (defn set-block-component-editing-mode!
- [value]
- (set-state! :block/component-editing-mode? value))
- (defn get-editor-args
- []
- @(:editor/args @state))
- (defn set-page-blocks-cp!
- [value]
- (set-state! [:view/components :page-blocks] value))
- (defn get-page-blocks-cp
- []
- (get-in @state [:view/components :page-blocks]))
- ;; To avoid circular dependencies
- (defn set-component!
- [k value]
- (set-state! [:view/components k] value))
- (defn get-component
- [k]
- (get-in @state [:view/components k]))
- (defn exit-editing-and-set-selected-blocks!
- ([blocks]
- (exit-editing-and-set-selected-blocks! blocks :down))
- ([blocks direction]
- (clear-edit!)
- (set-selection-blocks! blocks direction)))
- (defn set-editing-ref!
- [ref]
- (let [prev-ref @*editor-editing-ref]
- (when (and prev-ref (not= ref prev-ref))
- (reset! (get @(:editor/ref->editing? @state) prev-ref) false)))
- (if-let [*state (get @(:editor/ref->editing? @state) ref)]
- (reset! *state true)
- (swap! (:editor/ref->editing? @state) assoc ref (atom true)))
- (reset! *editor-editing-ref ref)
- (when ref
- (when-let [prev (.-previousSibling ref)]
- (set-state! :editor/editing-prev-node prev))
- (when-let [parent (util/rec-get-node (.-parentNode ref) "ls-block")]
- (set-state! :editor/editing-parent-node parent))))
- (defn set-editing!
- [edit-input-id content block cursor-range & {:keys [move-cursor? ref]
- :or {move-cursor? true}}]
- (when-not (exists? js/process)
- (if (> (count content)
- (block-content-max-length (get-current-repo)))
- (let [elements (array-seq (js/document.getElementsByClassName (str "id" (:block/uuid block))))]
- (when (first elements)
- (util/scroll-to-element (gobj/get (first elements) "id")))
- (exit-editing-and-set-selected-blocks! elements))
- (let [edit-input-id (if ref
- (or (some-> (gobj/get ref "id") (string/replace "ls-block" "edit-block"))
- edit-input-id)
- edit-input-id)]
- (when (and edit-input-id block
- (or
- (publishing-enable-editing?)
- (not @publishing?)))
- (let [block-element (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block"))
- container (util/get-block-container block-element)
- block (if container
- (assoc block
- :block.temp/container (gobj/get container "id"))
- block)
- content (string/trim (or content ""))]
- (when ref (set-editing-ref! ref))
- (set-state! :editor/block block)
- (set-state! :editor/content content :path-in-sub-atom (:block/uuid block))
- (set-state! :editor/last-key-code nil)
- (set-state! :editor/set-timestamp-block nil)
- (set-state! :editor/cursor-range cursor-range)
- (when-let [input (gdom/getElement edit-input-id)]
- (let [pos (count cursor-range)]
- (when content
- (util/set-change-value input content))
- (when move-cursor?
- (cursor/move-cursor-to input pos))
- (when (or (util/mobile?) (mobile-util/native-platform?))
- (set-state! :mobile/show-action-bar? false))))))))))
- (defn action-bar-open?
- []
- (:mobile/show-action-bar? @state))
- (defn get-git-auto-commit-enabled?
- []
- (false? (sub [:electron/user-cfgs :git/disable-auto-commit?])))
- (defn get-git-commit-on-close-enabled?
- []
- (sub [:electron/user-cfgs :git/commit-on-close?]))
- (defn set-last-key-code!
- [key-code]
- (set-state! :editor/last-key-code key-code))
- (defn get-last-key-code
- []
- @(:editor/last-key-code @state))
- (defn set-block-op-type!
- [op-type]
- (set-state! :editor/block-op-type op-type))
- (defn get-block-op-type
- []
- (:editor/block-op-type @state))
- (defn feature-http-server-enabled?
- []
- (boolean (storage/get ::storage-spec/http-server-enabled)))
- (defn get-plugin-by-id
- [id]
- (when-let [id (and id (keyword id))]
- (get-in @state [:plugin/installed-plugins id])))
- (defn get-enabled?-installed-plugins
- ([theme?] (get-enabled?-installed-plugins theme? true false false))
- ([theme? enabled? include-unpacked? include-all?]
- (filterv
- #(and (if include-unpacked? true (:iir %))
- (if-not (boolean? enabled?) true (= (not enabled?) (boolean (get-in % [:settings :disabled]))))
- (or include-all? (if (boolean? theme?) (= (boolean theme?) (:theme %)) true)))
- (vals (:plugin/installed-plugins @state)))))
- (defn lsp-enabled?-or-theme
- []
- (:plugin/enabled @state))
- (def lsp-enabled?
- (lsp-enabled?-or-theme))
- (defn consume-updates-from-coming-plugin!
- [payload updated?]
- (when-let [id (keyword (:id payload))]
- (let [prev-pending? (boolean (seq (:plugin/updates-pending @state)))]
- (println "Updates: consumed pending - " id)
- (swap! state update :plugin/updates-pending dissoc id)
- (if updated?
- (if-let [error (:error-code payload)]
- (swap! state update-in [:plugin/updates-coming id] assoc :error-code error)
- (swap! state update :plugin/updates-coming dissoc id))
- (swap! state update :plugin/updates-coming assoc id payload))
- (pub-event! [:plugin/consume-updates id prev-pending? updated?]))))
- (defn coming-update-new-version?
- [pkg]
- (and pkg (:latest-version pkg)))
- (defn plugin-update-available?
- [id]
- (when-let [pkg (and id (get (:plugin/updates-coming @state) (keyword id)))]
- (coming-update-new-version? pkg)))
- (defn all-available-coming-updates
- ([] (all-available-coming-updates (:plugin/updates-coming @state)))
- ([updates] (when-let [updates (vals updates)]
- (filterv #(coming-update-new-version? %) updates))))
- (defn get-next-selected-coming-update
- []
- (when-let [updates (all-available-coming-updates)]
- (let [unchecked (:plugin/updates-unchecked @state)]
- (first (filter #(and (not (and (seq unchecked) (contains? unchecked (:id %))))
- (not (:error-code %))) updates)))))
- (defn set-unchecked-update
- [id unchecked?]
- (swap! state update :plugin/updates-unchecked (if unchecked? conj disj) id))
- (defn reset-unchecked-update
- []
- (swap! state assoc :plugin/updates-unchecked #{}))
- (defn reset-all-updates-state
- []
- (swap! state assoc
- :plugin/updates-auto-checking? false
- :plugin/updates-pending {}
- :plugin/updates-coming {}
- :plugin/updates-downloading? false))
- (defn sub-right-sidebar-blocks
- []
- (when-let [current-repo (get-current-repo)]
- (->> (sub :sidebar/blocks)
- (filter #(= (first %) current-repo)))))
- (defn toggle-collapsed-block!
- [block-id]
- (let [current-repo (get-current-repo)]
- (update-state! [:ui/collapsed-blocks current-repo block-id] not)))
- (defn set-collapsed-block!
- [block-id value]
- (let [current-repo (get-current-repo)]
- (set-state! [:ui/collapsed-blocks current-repo block-id] value)))
- (defn sub-collapsed
- [block-id]
- (sub [:ui/collapsed-blocks (get-current-repo) block-id]))
- (defn get-modal-id
- []
- (:modal/id @state))
- (defn edit-in-query-or-refs-component
- []
- (let [config (last (get-editor-args))]
- {:custom-query? (:custom-query? config)
- :ref? (:ref? config)}))
- (defn set-auth-id-token
- [id-token]
- (set-state! :auth/id-token id-token))
- (defn set-auth-refresh-token
- [refresh-token]
- (set-state! :auth/refresh-token refresh-token))
- (defn set-auth-access-token
- [access-token]
- (set-state! :auth/access-token access-token))
- (defn get-auth-id-token []
- (sub :auth/id-token))
- (defn get-auth-refresh-token []
- (:auth/refresh-token @state))
- (defn set-file-sync-manager [graph-uuid v]
- (when (and graph-uuid v)
- (set-state! [:file-sync/graph-state graph-uuid :file-sync/sync-manager] v)))
- (defn get-file-sync-manager [graph-uuid]
- (get-in @state [:file-sync/graph-state graph-uuid :file-sync/sync-manager]))
- (defn clear-file-sync-state! [graph-uuid]
- (set-state! [:file-sync/graph-state graph-uuid] nil))
- (defn clear-file-sync-progress! [graph-uuid]
- (set-state! [:file-sync/graph-state
- graph-uuid
- :file-sync/progress]
- nil))
- (defn set-file-sync-state [graph-uuid v]
- (when v (s/assert :frontend.fs.sync/sync-state v))
- (set-state! [:file-sync/graph-state graph-uuid :file-sync/sync-state] v))
- (defn get-current-file-sync-graph-uuid
- []
- (get-in @state [:file-sync/graph-state :current-graph-uuid]))
- (defn sub-current-file-sync-graph-uuid
- []
- (sub [:file-sync/graph-state :current-graph-uuid]))
- (defn get-file-sync-state
- ([]
- (get-file-sync-state (get-current-file-sync-graph-uuid)))
- ([graph-uuid]
- (get-in @state [:file-sync/graph-state graph-uuid :file-sync/sync-state])))
- (defn sub-file-sync-state
- [graph-uuid]
- (sub [:file-sync/graph-state graph-uuid :file-sync/sync-state]))
- (defn reset-parsing-state!
- []
- (set-state! [:graph/parsing-state (get-current-repo)] {}))
- (defn set-parsing-state!
- [m]
- (update-state! [:graph/parsing-state (get-current-repo)]
- (if (fn? m) m
- (fn [old-value] (merge old-value m)))))
- (defn http-proxy-enabled-or-val? []
- (when-let [{:keys [type protocol host port] :as agent-opts} (sub [:electron/user-cfgs :settings/agent])]
- (when (and (not (contains? #{"system"} type))
- (every? not-empty (vals agent-opts)))
- (str protocol "://" host ":" port))))
- (defn set-mobile-app-state-change
- [is-active?]
- (set-state! :mobile/app-state-change
- {:is-active? is-active?
- :timestamp (inst-ms (js/Date.))}))
- (defn get-sync-graph-by-id
- [graph-uuid]
- (when graph-uuid
- (let [graph (first (filter #(= graph-uuid (:GraphUUID %))
- (get-repos)))]
- (when (:url graph)
- graph))))
- (defn unlinked-dir?
- [dir]
- (contains? (:file/unlinked-dirs @state) dir))
- (defn get-file-rename-event-chan
- []
- (:file/rename-event-chan @state))
- (defn offer-file-rename-event-chan!
- [v]
- {:pre [(map? v)
- (= #{:repo :old-path :new-path} (set (keys v)))]}
- (async/offer! (get-file-rename-event-chan) v))
- (defn set-onboarding-whiteboard!
- [v]
- (set-state! :whiteboard/onboarding-whiteboard? v)
- (storage/set :ls-onboarding-whiteboard? v))
- (defn get-onboarding-whiteboard?
- []
- (get-in @state [:whiteboard/onboarding-whiteboard?]))
- (defn get-local-container-root-url
- []
- (when (mobile-util/native-ios?)
- (get-in @state [:mobile/container-urls :localContainerUrl])))
- (defn get-icloud-container-root-url
- []
- (when (mobile-util/native-ios?)
- (get-in @state [:mobile/container-urls :iCloudContainerUrl])))
- (defn get-current-pdf
- []
- (:pdf/current @state))
- (defn nfs-user-granted?
- [repo]
- (get-in @state [:nfs/user-granted? repo]))
- (defn set-current-pdf!
- [inflated-file]
- (let [settle-file! #(set-state! :pdf/current inflated-file)]
- (if-not (get-current-pdf)
- (settle-file!)
- (when (apply not= (map :identity [inflated-file (get-current-pdf)]))
- (set-state! :pdf/current nil)
- (js/setTimeout #(settle-file!) 16)))))
- (defn focus-whiteboard-shape
- ([shape-id]
- (focus-whiteboard-shape (active-tldraw-app) shape-id))
- ([tln shape-id]
- (when-let [^js api (gobj/get tln "api")]
- (when (and shape-id (parse-uuid shape-id))
- (. api selectShapes shape-id)
- (. api zoomToSelection)))))
- (defn set-user-info!
- [info]
- (when info
- (set-state! :user/info info)
- (let [groups (:UserGroups info)]
- (when (seq groups)
- (storage/set :user-groups groups)))))
- (defn get-user-info []
- (sub :user/info))
- (defn clear-user-info!
- []
- (storage/remove :user-groups))
- (defn sub-block-unloaded?
- [repo block-uuid]
- (rum/react
- (rum/derived-atom [(rum/cursor-in state [repo :restore/unloaded-blocks])] [::block-unloaded repo block-uuid]
- (fn [s]
- (contains? s (str block-uuid))))))
- (defn sub-page-unloaded?
- [repo page-name]
- (rum/react
- (rum/derived-atom [(rum/cursor-in state [repo :unloaded-pages])] [::page-unloaded repo page-name]
- (fn [s]
- (contains? s page-name)))))
- (defn get-color-accent []
- (get @state :ui/radix-color))
- (defn set-color-accent! [color]
- (swap! state assoc :ui/radix-color color)
- (storage/set :ui/radix-color color)
- (util/set-android-theme))
- (defn unset-color-accent! []
- (swap! state assoc :ui/radix-color nil)
- (storage/remove :ui/radix-color)
- (util/set-android-theme))
- (defn cycle-color! []
- (let [current-color (get-color-accent)
- next-color (->> (cons nil colors/color-list)
- (drop-while #(not= % current-color))
- (second))]
- (if next-color
- (set-color-accent! next-color)
- (unset-color-accent!))))
- (defn handbook-open?
- []
- (:ui/handbooks-open? @state))
- (defn get-handbook-route-chan
- []
- (:handbook/route-chan @state))
- (defn open-handbook-pane!
- [k]
- (when-not (handbook-open?)
- (set-state! :ui/handbooks-open? true))
- (js/setTimeout #(async/go
- (>! (get-handbook-route-chan) k))))
- (defn set-page-properties-changed!
- [page-name]
- (when-not (string/blank? page-name)
- (update-state! [:db/properties-changed-pages page-name] #(inc %))))
- (defn sub-page-properties-changed
- [page-name]
- (when-not (string/blank? page-name)
- (sub [:db/properties-changed-pages page-name])))
- (defn update-tx-after-cursor-state!
- []
- (let [editor-cursor (get-current-edit-block-and-position)
- max-tx-id (apply max (keys @(:history/tx->editor-cursor @state)))]
- (when (and max-tx-id (nil? (:after (get @(:history/tx->editor-cursor @state) max-tx-id))))
- (update-state! :history/tx->editor-cursor
- (fn [m] (assoc-in m [max-tx-id :after] editor-cursor))))))
|