|
794 | 794 | {:block/uuid (sdk-utils/uuid-or-throw-error block-uuid) :repo repo}))))
|
795 | 795 |
|
796 | 796 | (def ^:export update_block
|
| 797 | + "Updates block with 3rd arg being a js map with following keys: |
| 798 | + * :mcp - When set behaves as MCP tools expect |
| 799 | + * :properties - A map of properties to update |
| 800 | + * Remaining keys are passed to save-block!" |
797 | 801 | (fn [block-uuid content ^js opts]
|
798 | 802 | (p/let [repo (state/get-current-repo)
|
799 | 803 | db-base? (config/db-based-graph? repo)
|
800 | 804 | block (<pull-block block-uuid)
|
801 | 805 | opts (bean/->clj opts)]
|
802 |
| - (when block |
| 806 | + (if block |
803 | 807 | (p/do!
|
804 | 808 | (when (and db-base? (seq (:properties opts)))
|
805 | 809 | (api-block/save-db-based-block-properties! block (:properties opts)))
|
806 | 810 | (editor-handler/save-block! repo
|
807 | 811 | (sdk-utils/uuid-or-throw-error block-uuid) content
|
808 |
| - (if db-base? (dissoc opts :properties) opts))))))) |
| 812 | + (if db-base? (dissoc opts :properties) opts))) |
| 813 | + (when (:mcp opts) |
| 814 | + (throw (ex-info (str "Block " (pr-str block-uuid) " not found") {}))))))) |
809 | 815 |
|
810 | 816 | (def ^:export move_block
|
811 | 817 | (fn [src-block-uuid target-block-uuid ^js opts]
|
|
1077 | 1083 | (insert_block target content (bean/->js opts)))))))))
|
1078 | 1084 |
|
1079 | 1085 | (defn ^:export append_block_in_page
|
| 1086 | + "Append a block to a page and creates page if it does not exist. |
| 1087 | + If the 'mcp-options' opts key is set, this fn assumes MCP defaults by not creating a page unless explicitly |
| 1088 | + forced to. `opts` arg are options passed to `insert_block` except for |
| 1089 | + key the 'mcp-options', which has the following keys: |
| 1090 | + * :force - When set forces creation of nonexistent page" |
1080 | 1091 | [uuid-or-page-name content ^js opts]
|
1081 | 1092 | (let [current-page? (or (and (nil? content) (nil? opts))
|
1082 | 1093 | (and (nil? opts) (some->> content (instance? js/Object))))
|
1083 | 1094 | opts (if current-page? content opts)
|
| 1095 | + mcp-options (bean/->clj (aget opts "mcp-options")) |
1084 | 1096 | content (if current-page? uuid-or-page-name content)
|
1085 | 1097 | uuid-or-page-name (if current-page?
|
1086 | 1098 | (or (state/get-current-page) (date/today))
|
1087 | 1099 | uuid-or-page-name)]
|
1088 | 1100 | (p/let [_ (<ensure-page-loaded uuid-or-page-name)
|
1089 | 1101 | page? (not (util/uuid-string? uuid-or-page-name))
|
1090 | 1102 | page-not-exist? (and page? (nil? (db-model/get-page uuid-or-page-name)))
|
1091 |
| - _ (and page-not-exist? (page-handler/<create! uuid-or-page-name |
1092 |
| - {:redirect? false |
1093 |
| - :format (state/get-preferred-format)}))] |
1094 |
| - (when-let [block (db-model/get-page uuid-or-page-name)] |
| 1103 | + _ (when (and page-not-exist? |
| 1104 | + (or (nil? mcp-options) |
| 1105 | + (:force mcp-options))) |
| 1106 | + (page-handler/<create! uuid-or-page-name |
| 1107 | + {:redirect? false |
| 1108 | + :format (state/get-preferred-format)}))] |
| 1109 | + (if-let [block (db-model/get-page uuid-or-page-name)] |
1095 | 1110 | (let [target (str (:block/uuid block))]
|
1096 |
| - (insert_block target content opts)))))) |
| 1111 | + (insert_block target content opts)) |
| 1112 | + (when mcp-options |
| 1113 | + (throw (ex-info (str "Page " (pr-str uuid-or-page-name) " not found") {}))))))) |
1097 | 1114 |
|
1098 | 1115 | ;; plugins
|
1099 | 1116 | (defn ^:export validate_external_plugins [urls]
|
|
1264 | 1281 | (defn ^:export search
|
1265 | 1282 | [q' & [opts]]
|
1266 | 1283 | (-> (search-handler/search (state/get-current-repo) q' (if opts (js->clj opts :keywordize-keys true) {}))
|
1267 |
| - (p/then #(bean/->js %)))) |
| 1284 | + (p/then #(bean/->js (sdk-utils/normalize-keyword-for-json %))))) |
1268 | 1285 |
|
1269 | 1286 | ;; helpers
|
1270 | 1287 | (defn ^:export set_focused_settings
|
|
0 commit comments