Skip to content

Commit

Permalink
Added a basic search from the navbar
Browse files Browse the repository at this point in the history
  • Loading branch information
interstar committed Jun 16, 2021
1 parent bcbf679 commit d90b077
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 25 deletions.
18 changes: 16 additions & 2 deletions resources/gql_schema.edn
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@
:ip {:type String}
:system_cards {:type (list :Card)}

}}}
}}

:SearchResult
{:description "Result of a search on the server. In Markdown and intended to be rendered to the Transcript"
:fields
{:result_text {:type String}
}}

}


:queries
Expand All @@ -62,6 +70,12 @@
:args {:page_name {:type (non-null String)}
:hash {:type (non-null String)}}
:resolve :resolve-card
}}
}

:text_search
{:type :SearchResult
:description "Search text within the PageStore"
:args {:query_string {:type String}}
:resolve :resolve-text-search}
}
}
15 changes: 15 additions & 0 deletions src/clj_ts/card_server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,20 @@ stroke=\"green\" r=\"20\" stroke-width=\"2\" fill=\"yellow\" />"

;; GraphQL resolvers

(defn resolve-text-search [context arguments value]
(let [{:keys [query_string]} arguments

res (pagestore/text-search (server-state)
(.all-pages (-> (server-state)
:facts-db))
(re-pattern query_string))
out
(str "*Pages containing \" " query_string "\"*\n "
(apply str (map #(str "* [[" % "]]\n") res))) ]

{:result_text out}
))

(defn resolve-card
"Not yet tested"
[context arguments value]
Expand Down Expand Up @@ -460,6 +474,7 @@ stroke=\"green\" r=\"20\" stroke-width=\"2\" fill=\"yellow\" />"
(attach-resolvers {:resolve-source-page resolve-source-page
:resolve-page resolve-page
:resolve-card resolve-card
:resolve-text-search resolve-text-search
})
schema/compile))

Expand Down
83 changes: 62 additions & 21 deletions src/clj_ts/client.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,36 @@
:data new-data}))))


(declare prepend-transcript!)
(declare string->html)

(defn search-text! [query_text]
(let [query (str "{\"query\" : \"query TextSearch {
text_search(query_string:\\\"" query_text "\\\"){ result_text }
}\", \"variables\":null, \"operationName\":\"TextSearch\" }")]
(.send XhrIo
"/clj_ts/graphql"
(fn [e]
(let [status (-> e .-target .getStatusText)
edn (-> e .-target .getResponseText .toString
(#(.parse js/JSON %)) js->clj )
data (-> edn (get "data"))
result (-> data (get "text_search") (get "result_text"))
]
(prepend-transcript! (str "Searching for " query_text) (string->html result))
))
"POST"
query)
))


;; Nav and History

(defn go-new! [p-name]
(load-page! p-name (conj (-> @db :past) (-> @db :current-page)) []))
(do
(load-page! p-name (conj (-> @db :past) (-> @db :current-page)) [])
(swap! db assoc :mode :viewing)
))

(defn forward! [p-name]
(load-page! p-name (conj (-> @db :past) (-> @db :current-page)) (pop (-> @db :future)) )
Expand Down Expand Up @@ -179,9 +204,10 @@
(defn prepend-transcript! [code result]
(do
(swap! db assoc :transcript
(str "> " code "
(str "<p> > " code "
<br/>
" result "
</p>
" (-> @db :transcript)))
(swap! db assoc :mode :transcript)) )

Expand Down Expand Up @@ -264,10 +290,13 @@
)}
"Execute"]



]
] ))))
[:button
{:class "big-btn"
:on-click
(fn []
(search-text! (-> @current str)))}
"Search"]
]]))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand Down Expand Up @@ -549,12 +578,17 @@ NO BOILERPLATE FOR EMBED TYPE " type
(not= "" (string/trim (get card "source_data")))
)

(defn card->html [card]
(-> (get card "server_prepared_data")
(defn string->html [s]
(-> s
(double-comma-table)
(md/md->html)
(auto-links)
(double-bracket-links)))
(double-bracket-links)
))

(defn card->html [card]
(-> (get card "server_prepared_data")
(string->html)))



Expand Down Expand Up @@ -711,6 +745,16 @@ You'll need to edit the page fully to make permanent changes to the code. "]]

)

(defn on-click-for-links [e]
(let [tag (-> e .-target)
classname (.getAttribute tag "class")
data (.getAttribute tag "data")
x (-> @db :dirty)]

(if (= classname "wikilink")
(go-new! data)))
)

(defn one-card [card]
(let [
inner-html
Expand Down Expand Up @@ -775,14 +819,8 @@ You'll need to edit the page fully to make permanent changes to the code. "]]
:display (-> @state2 :toggle)}}
[:div
{:class "card"
:on-click
(fn [e]
(let [tag (-> e .-target)
classname (.getAttribute tag "class")
data (.getAttribute tag "data")
x (-> @db :dirty)]
(if (= classname "wikilink")
(go-new! data))))
:on-click on-click-for-links

}

inner ]]
Expand Down Expand Up @@ -826,10 +864,13 @@ You'll need to edit the page fully to make permanent changes to the code. "]]
])



(defn transcript []
[:div {:class "transcript"}
[:pre
(-> @db :transcript)]
[:div {:class "transcript"
:dangerouslySetInnerHTML {:__html (-> @db :transcript)}
:on-click on-click-for-links

}
])

(defn main-container []
Expand Down
6 changes: 4 additions & 2 deletions src/clj_ts/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@
{:status 200
:headers {"Content-Type" "application/json"}
:body (let [query (extract-query request)
result (execute card-server/pagestore-schema query nil nil)]
(json/write-str result))})
result (execute card-server/pagestore-schema query nil nil)
out (json/write-str result)]
out
)})



Expand Down

0 comments on commit d90b077

Please sign in to comment.