Skip to content

Commit

Permalink
input-rule: create new code block with (
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuebert committed Jul 29, 2023
1 parent ed69945 commit cf1f245
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions editor2/src/main/maria/editor/prosemirror/input_rules.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns maria.editor.prosemirror.input-rules
(:require ["prosemirror-commands" :as pm.cmd]
(:require ["prosemirror-state" :as pm.state]
["prosemirror-commands" :as pm.cmd]
["prosemirror-inputrules" :as rules]
["prosemirror-model" :as model]
[applied-science.js-interop :as j]
Expand All @@ -10,22 +11,22 @@
;; invokes a command.



(defn toggle-mark-tr [state mark-name & [attrs]]
(let [sel (.-selection state)
mark-type (j/get-in schema [:marks (name mark-name)])
empty (.-empty sel)
$cursor (.-$cursor sel)
the-mark (.create mark-type attrs)
the-node (or (.-node sel) (.-parent (.-$from sel)))
tr (.-tr state)]
(when (and $cursor
(not (or (and empty (not $cursor))
(not (.-inlineContent the-node))
(not (.allowsMarks (.-type the-node) #js [the-mark])))))
(if (.isInSet mark-type (or (.-storedMarks state) (.marks $cursor)))
(.removeStoredMark tr mark-type)
(.addStoredMark tr the-mark)))))
(js
(defn toggle-mark-tr [state mark-name attrs]
(let [sel (.-selection state)
mark-type (j/get-in schema [:marks (name mark-name)])
empty (.-empty sel)
$cursor (.-$cursor sel)
the-mark (.create mark-type attrs)
the-node (or (.-node sel) (.-parent (.-$from sel)))
tr (.-tr state)]
(when (and $cursor
(not (or (and empty (not $cursor))
(not (.-inlineContent the-node))
(not (.allowsMarks (.-type the-node) #js [the-mark])))))
(if (.isInSet mark-type (or (.-storedMarks state) (.marks $cursor)))
(.removeStoredMark tr mark-type)
(.addStoredMark tr the-mark))))))

(js
(def maria-rules*
Expand All @@ -37,7 +38,7 @@
[~@rules/smartQuotes
(rules/InputRule. #"[^`\\]+`$"
(fn [state match start end]
(toggle-mark-tr state :code)))
(toggle-mark-tr state :code nil)))
rules/ellipsis
rules/emDash

Expand All @@ -60,9 +61,26 @@
;; type ``` for a code block
(rules/textblockTypeInputRule #"^```$" code_block)

(rules/InputRule. #"^\($"
(fn [^js state match start end]
(let [^js $start (.. state -doc (resolve start))]
(when (.. $start
(node -1)
(canReplaceWith
(.index $start -1)
(.indexAfter $start -1)
code_block))
(doto (.-tr state)
(.delete start end)
(.setBlockType start end code_block nil)
(.insertText "()" start)
(as-> tr
(.setSelection tr (.create pm.state/TextSelection (.-doc tr) (inc start)))))))))

;; type #, ##, ### etc. for h1, h2, h3 etc.
(rules/textblockTypeInputRule #"^(#{1,6})\s$" heading
(fn [[_ match]] {:level (count match)}))]))


(def maria-rules
(rules/inputRules {:rules maria-rules*})))

0 comments on commit cf1f245

Please # to comment.