|
5 | 5 | [clojure.string :as string] |
6 | 6 | [clojure.walk :as walk] |
7 | 7 | [datascript.core :as d] |
8 | | - [frontend.components.property.value :as pv] |
9 | 8 | [frontend.db :as db] |
| 9 | + [frontend.db.async :as db-async] |
10 | 10 | [frontend.db.model :as db-model] |
11 | 11 | [frontend.handler.common.page :as page-common-handler] |
12 | | - [logseq.db.frontend.property :as db-property] |
| 12 | + [frontend.handler.db-based.page :as db-page-handler] |
13 | 13 | [frontend.handler.db-based.property :as db-property-handler] |
14 | 14 | [frontend.handler.editor :as editor-handler] |
15 | 15 | [frontend.handler.page :as page-handler] |
16 | 16 | [frontend.modules.layout.core] |
17 | | - [frontend.handler.db-based.page :as db-page-handler] |
18 | | - [frontend.db.async :as db-async] |
19 | 17 | [frontend.state :as state] |
20 | 18 | [frontend.util :as util] |
21 | 19 | [logseq.api.block :as api-block] |
|
60 | 58 | blocks (:blocks result)] |
61 | 59 | (when (seq blocks) |
62 | 60 | (p/doseq [block blocks] |
63 | | - (let [id (:block/uuid block) |
64 | | - b (db/entity [:block/uuid id]) |
65 | | - properties (when uuid->properties (uuid->properties id))] |
66 | | - (when (seq properties) |
67 | | - (api-block/db-based-save-block-properties! b properties {:plugin this |
68 | | - :schema schema}))))) |
| 61 | + (let [id (:block/uuid block) |
| 62 | + b (db/entity [:block/uuid id]) |
| 63 | + properties (when uuid->properties (uuid->properties id))] |
| 64 | + (when (seq properties) |
| 65 | + (api-block/db-based-save-block-properties! b properties {:plugin this |
| 66 | + :schema schema}))))) |
69 | 67 | (let [blocks' (map (fn [b] (db/entity [:block/uuid (:block/uuid b)])) blocks)] |
70 | 68 | (sdk-utils/result->js blocks'))))) |
71 | 69 |
|
|
95 | 93 | (defn get-property |
96 | 94 | [k] |
97 | 95 | (this-as this |
98 | | - (p/let [prop (-get-property this k) |
99 | | - prop' (some-> prop |
100 | | - (assoc :type (:logseq.property/type prop)))] |
101 | | - (sdk-utils/result->js prop')))) |
| 96 | + (p/let [prop (-get-property this k) |
| 97 | + prop' (some-> prop |
| 98 | + (assoc :type (:logseq.property/type prop)))] |
| 99 | + (sdk-utils/result->js prop')))) |
102 | 100 |
|
103 | 101 | (defn ->cardinality |
104 | 102 | [input] |
|
117 | 115 | (when-not (contains? valid-types type) |
118 | 116 | (throw (ex-info (str "Invalid type, type should be one of: " valid-types) {:type type}))))) |
119 | 117 |
|
| 118 | +(defn- upsert-property-aux |
| 119 | + [this k schema opts] |
| 120 | + (p/let [k' (api-block/sanitize-user-property-name k) |
| 121 | + property-ident (api-block/get-db-ident-from-property-name k' this) |
| 122 | + property (db/entity property-ident)] |
| 123 | + (if property |
| 124 | + property |
| 125 | + (p/let [_ (api-block/ensure-property-upsert-control this property-ident k') |
| 126 | + schema (or (some-> schema bean/->clj |
| 127 | + (update-keys #(if (contains? #{:hide :public} %) |
| 128 | + (keyword (str (name %) "?")) %))) |
| 129 | + {}) |
| 130 | + _ (when (:type schema) |
| 131 | + (schema-type-check! (keyword (:type schema)))) |
| 132 | + schema (cond-> schema |
| 133 | + (string? (:cardinality schema)) |
| 134 | + (-> (assoc :db/cardinality (->cardinality (:cardinality schema))) |
| 135 | + (dissoc :cardinality)) |
| 136 | + |
| 137 | + (string? (:type schema)) |
| 138 | + (-> (assoc :logseq.property/type (keyword (:type schema))) |
| 139 | + (dissoc :type))) |
| 140 | + p (db-property-handler/upsert-property! property-ident schema |
| 141 | + (assoc opts :property-name k'))] |
| 142 | + (db/entity (:db/id p)))))) |
| 143 | + |
120 | 144 | (defn upsert-property |
121 | 145 | "schema: |
122 | 146 | {:type :default | :number | :date | :datetime | :checkbox | :url | :node | :json | :string |
|
128 | 152 | [k ^js schema ^js opts] |
129 | 153 | (this-as |
130 | 154 | this |
131 | | - (when-not (string/blank? k) |
132 | | - (p/let [opts (or (some-> opts bean/->clj) {}) |
133 | | - k' (api-block/sanitize-user-property-name k) |
134 | | - property-ident (api-block/get-db-ident-from-property-name k' this) |
135 | | - _ (api-block/ensure-property-upsert-control this property-ident k') |
136 | | - schema (or (some-> schema (bean/->clj) |
137 | | - (update-keys #(if (contains? #{:hide :public} %) |
138 | | - (keyword (str (name %) "?")) %))) {}) |
139 | | - _ (when (:type schema) |
140 | | - (schema-type-check! (keyword (:type schema)))) |
141 | | - schema (cond-> schema |
142 | | - (string? (:cardinality schema)) |
143 | | - (-> (assoc :db/cardinality (->cardinality (:cardinality schema))) |
144 | | - (dissoc :cardinality)) |
145 | | - |
146 | | - (string? (:type schema)) |
147 | | - (-> (assoc :logseq.property/type (keyword (:type schema))) |
148 | | - (dissoc :type))) |
149 | | - p (db-property-handler/upsert-property! property-ident schema |
150 | | - (assoc opts :property-name k')) |
151 | | - p (db/entity (:db/id p))] |
152 | | - (sdk-utils/result->js p))))) |
| 155 | + (when-not (string/blank? k) |
| 156 | + (p/let [opts' (or (some-> opts bean/->clj) {}) |
| 157 | + schema' (or (some-> schema bean/->clj) {}) |
| 158 | + property (upsert-property-aux this k schema' opts')] |
| 159 | + (sdk-utils/result->js property))))) |
153 | 160 |
|
154 | 161 | (defn remove-property |
155 | 162 | [k] |
156 | 163 | (this-as |
157 | 164 | this |
158 | | - (p/let [property (-get-property this k)] |
159 | | - (when-let [uuid (and (api-block/plugin-property-key? (:db/ident property)) |
160 | | - (:block/uuid property))] |
161 | | - (page-common-handler/<delete! uuid nil nil))))) |
| 165 | + (p/let [property (-get-property this k)] |
| 166 | + (when-let [uuid (and (api-block/plugin-property-key? (:db/ident property)) |
| 167 | + (:block/uuid property))] |
| 168 | + (page-common-handler/<delete! uuid nil nil))))) |
162 | 169 |
|
163 | 170 | (defn upsert-block-property |
164 | 171 | [this block key' value schema] |
|
204 | 211 | (when tag |
205 | 212 | (sdk-utils/result->js tag)))) |
206 | 213 |
|
207 | | -(defn add-tag-class-property [tag-id property-id-or-name & {:keys [schema]}] |
208 | | - (p/let [repo (state/get-current-repo) |
209 | | - tag-entity (db-async/<get-block repo tag-id {:children? false}) |
210 | | - class-tag? (some-> tag-entity (ldb/class?)) |
211 | | - _ (when-not class-tag? (throw (ex-info (str tag-id " should be class tag") {:value tag-entity}))) |
212 | | - property (db-async/<get-block repo property-id-or-name {:children? false}) |
213 | | - property? (some-> property (ldb/property?)) |
214 | | - property-title (or (:block/title property) property-id-or-name)] |
215 | | - (p/let [property' |
216 | | - (if property? |
217 | | - (do |
218 | | - (when (and (not (ldb/public-built-in-property? property)) |
219 | | - (ldb/built-in? property)) |
220 | | - (throw (ex-info "This is a private built-in property that can't be used." {:value property}))) |
221 | | - property) |
222 | | - ;; new property entered or converting page to property |
223 | | - (if (db-property/valid-property-name? property-title) |
224 | | - (p/let [type (or (:type schema) :default) |
225 | | - schema (assoc schema :logseq.property/type type) |
226 | | - opts (cond-> {:property-name property-title} |
227 | | - (and (not property?) (ldb/internal-page? property)) |
228 | | - (assoc :properties {:db/id (:db/id property)})) |
229 | | - result (db-property-handler/upsert-property! nil schema opts) |
230 | | - property (db/entity (:db/id result)) |
231 | | - _ (when class-tag? |
232 | | - (pv/<add-property! tag-entity (:db/ident property) |
233 | | - "" {:class-schema? true :exit-edit? false}))] |
234 | | - property) |
235 | | - (throw (ex-info "This is an invalid property name." {:value property-title}))))] |
236 | | - (sdk-utils/result->js property') |
237 | | - ))) |
238 | | - |
239 | | -(defn remove-tag-class-property [tag-id property-id-or-name] |
240 | | - (throw (ex-info "TODO: implement remove-tag-class-property" {tag-id property-id-or-name}))) |
| 214 | +(defn tag-add-property [tag-id property-id-or-name] |
| 215 | + (p/let [tag (db/get-page tag-id) |
| 216 | + property (db/get-page property-id-or-name)] |
| 217 | + (when-not (ldb/class? tag) (throw (ex-info "Not a valid tag" {:tag tag-id}))) |
| 218 | + (when-not (ldb/property? property) (throw (ex-info "Not a valid property" {:property property-id-or-name}))) |
| 219 | + (when (and (not (ldb/public-built-in-property? property)) |
| 220 | + (ldb/built-in? property)) |
| 221 | + (throw (ex-info "This is a private built-in property that can't be used." {:value property}))) |
| 222 | + (p/do! |
| 223 | + (db-property-handler/class-add-property! (:db/id tag) (:db/ident property)) |
| 224 | + (sdk-utils/result->js (db/get-page tag-id))))) |
| 225 | + |
| 226 | +(defn tag-remove-property [tag-id property-id-or-name] |
| 227 | + (p/let [tag (db/get-page tag-id) |
| 228 | + property (db/get-page property-id-or-name)] |
| 229 | + (when-not (ldb/class? tag) (throw (ex-info "Not a valid tag" {:tag tag-id}))) |
| 230 | + (when-not (ldb/property? property) (throw (ex-info "Not a valid property" {:property property-id-or-name}))) |
| 231 | + (p/do! |
| 232 | + (db-property-handler/class-remove-property! (:db/id tag) (:db/ident property)) |
| 233 | + (sdk-utils/result->js (db/get-page tag-id))))) |
241 | 234 |
|
242 | 235 | (defn add-block-tag [id-or-name tag-id] |
243 | 236 | (p/let [repo (state/get-current-repo) |
|
0 commit comments