@@ -15,7 +15,7 @@ const q = (query, arg1, ...args) =>
15
15
16
16
/**
17
17
* @param {string } query
18
- * @param {?( number|string) } arg1
18
+ * @param {number|string|null|undefined } arg1
19
19
* @param {(number|string)[] } args
20
20
* @returns {?number }
21
21
*/
@@ -29,7 +29,6 @@ const q1 = (query, arg1, ...args) => {
29
29
* @deprecated
30
30
* @param {string } props
31
31
* @param {number | null | undefined } dbid
32
- * @returns {import('./RoamTypes').RoamNode | import('./RoamTypes').RoamBlock | null | undefined }
33
32
*/
34
33
const PULL_DEPRECATED = ( props , dbid ) =>
35
34
dbid == null
@@ -38,14 +37,27 @@ const PULL_DEPRECATED = (props, dbid) =>
38
37
39
38
/**
40
39
* get a node or block by its :db/id
41
- * @param {?number | import('./RoamTypes').RoamDBRef } dbid
42
- * @param {(keyof import('./RoamTypes').RoamNode)[] } props
40
+ * @param { null | undefined | import('./RoamTypes').RoamId } id
41
+ * @param { (keyof import('./RoamTypes').RoamNode)[] } props
43
42
*/
44
- const get = ( dbid , ...props ) =>
45
- PULL_DEPRECATED (
46
- `[${ props . join ( " " ) || "*" } ]` ,
47
- typeof dbid === "object" ? dbid ?. [ ":db/id" ] : dbid ,
48
- )
43
+ const get = ( id , ...props ) => {
44
+ if ( id == null ) return null
45
+ let uid , dbid
46
+ switch ( typeof id ) {
47
+ case "object" :
48
+ ; ( { ":block/uid" : uid , ":db/id" : dbid } = id )
49
+ break
50
+ case "string" :
51
+ uid = id
52
+ break
53
+ case "number" :
54
+ dbid = id
55
+ break
56
+ }
57
+ if ( dbid == null && uid != null ) dbid = getIdFromUid ( uid )
58
+ if ( dbid == null ) return null
59
+ return PULL_DEPRECATED ( `[${ props . join ( " " ) || "*" } ]` , dbid )
60
+ }
49
61
50
62
const getStuffThatRefsToId = ( /**@type {?number } */ dbid ) =>
51
63
q ( "[:find ?e :in $ ?a :where [?e :block/refs ?a]]" , dbid )
@@ -56,12 +68,12 @@ const getIdForTitle = (/**@type {?string} */ title) =>
56
68
const getParentId = ( /**@type {?number } */ dbid ) =>
57
69
q1 ( "[:find ?e :in $ ?a :where [?e :block/children ?a]]" , dbid )
58
70
59
- const getIdFromUid = ( /**@type {? string } */ uid ) =>
71
+ const getIdFromUid = ( /**@type {string | null | undefined } */ uid ) =>
60
72
q1 ( "[:find ?e :in $ ?a :where [?e :block/uid ?a]]" , uid )
61
73
62
74
const getCurrentPageUid = ( ) => window . location . hash . split ( "/" ) . reverse ( ) [ 0 ]
63
75
64
- const getCurrentPage = ( ) => get ( getIdFromUid ( getCurrentPageUid ( ) ) )
76
+ const getCurrentPage = ( ) => get ( getCurrentPageUid ( ) )
65
77
66
78
const getStuffThatRefsTo = ( /**@type {string } */ title ) =>
67
79
getStuffThatRefsToId ( getIdForTitle ( title ) )
@@ -110,18 +122,40 @@ const uidFromElement = (/**@type {Element} */ element) =>
110
122
const observer = new MutationObserver ( ( mutationsList , observer ) => {
111
123
for ( const mutation of mutationsList ) {
112
124
if ( mutation . type === "childList" ) {
113
-
114
125
mutation . addedNodes . forEach ( node => {
115
- if ( node . nodeType === Node . ELEMENT_NODE ) {
116
- const uid = uidFromElement (
117
- /**@type {Element }*/ ( /**@type {any }*/ node ) ,
118
- )
119
- get ( getIdFromUid ( uid ) )
120
- ?. [ ":block/refs" ] ?. map ( ref => get ( ref ) )
121
- . forEach ( refKid => {
122
- console . log ( "mounted block refers to" , refKid )
123
- } )
124
- }
126
+ if ( node . nodeType !== Node . ELEMENT_NODE ) return
127
+ const el = /**@type {Element }*/ ( /**@type {any }*/ node )
128
+ const uid = uidFromElement ( el )
129
+ get ( uid )
130
+ ?. [ ":block/refs" ] ?. map ( ref => get ( ref ) )
131
+ . forEach ( refKid => {
132
+ console . log ( "mounted block refers to" , refKid )
133
+
134
+ // const entity_attrs = refKid?.[":entity/attrs"]?.map(attr =>
135
+ // attr
136
+ // ?.filter(attr => Array.isArray(attr[":value"]))
137
+ // .map(({ ":source": [, source], ":value": value }) => [
138
+ // source,
139
+ // value[1],
140
+ // ])
141
+ // // .filter(([source, value]) => source != value)
142
+ // .map(([source, value]) => [
143
+ // get(source, ":node/title", ":block/string"),
144
+ // get(value, ":node/title", ":block/string"),
145
+ // ]),
146
+ // )
147
+
148
+ const entity_attrs = refKid ?. [ ":entity/attrs" ] ?. map (
149
+ ( [ page , attr , value ] ) => {
150
+ const pageNode = attrToValue ( page [ ":value" ] , ":node/title" , ":block/string" )
151
+ const attrNode = attrToValue ( attr [ ":value" ] , ":node/title" , ":block/string" )
152
+ const valueNode = attrToValue ( value [ ":value" ] , ":node/title" , ":block/string" )
153
+
154
+ return { pageNode, attrNode, valueNode }
155
+ } ,
156
+ )
157
+ console . log ( "and that page has these attributes" , entity_attrs )
158
+ } )
125
159
} )
126
160
127
161
// mutation.removedNodes
@@ -131,6 +165,15 @@ const observer = new MutationObserver((mutationsList, observer) => {
131
165
}
132
166
} )
133
167
168
+ /**
169
+ * @param {string | import("./RoamTypes").RoamAttrItem } attr
170
+ * @param { (keyof import('./RoamTypes').RoamNode)[] } props
171
+ */
172
+ const attrToValue = ( attr , ...props ) =>
173
+ Array . isArray ( attr ) && attr [ 0 ] === ":block/uid"
174
+ ? get ( attr [ 1 ] , ...props )
175
+ : attr
176
+
134
177
const roam_onInit = ( ) => {
135
178
if ( ! window . roamAlphaAPI ) {
136
179
setTimeout ( roam_onInit , 100 )
0 commit comments