Skip to content

Commit c0adaad

Browse files
++
1 parent fea2906 commit c0adaad

File tree

2 files changed

+86
-27
lines changed

2 files changed

+86
-27
lines changed

RoamTypes.d.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ declare global {
22
interface Window {
33
roamAlphaAPI?: {
44
q: (query: string, ...args: (string | number)[]) => [number][]
5-
pull: (keys: string, dbid: number) => null | RoamBlock | RoamNode
5+
pull: (keys: string, dbid: number) => RoamNode | null
66
}
77
}
88
}
@@ -27,12 +27,28 @@ export interface RoamBlock extends RoamDBRef {
2727
":edit/time": number
2828
}
2929

30+
type RoamAttrItem = [":block/uid", string]
31+
32+
type RoamAttr = {
33+
":source": RoamAttrItem
34+
":value": RoamAttrItem | string
35+
}
36+
3037
export interface RoamNode extends RoamBlock {
3138
":node/title": string
3239
":attrs/lookup"?: RoamDBRef[]
33-
":entity/attrs"?: {
34-
":source": [":block/uid", string]
35-
":value": [":block/uid", string]
36-
}[]
40+
":entity/attrs"?: [
41+
RoamAttr,
42+
RoamAttr,
43+
RoamAttr,
44+
][]
3745
":page/permissions"?: { ":public": null | any }
3846
}
47+
48+
export type RoamId =
49+
| number
50+
| string
51+
| {
52+
":block/uid"?: string
53+
":db/id"?: number
54+
}

index.js

+65-22
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const q = (query, arg1, ...args) =>
1515

1616
/**
1717
* @param {string} query
18-
* @param {?(number|string)} arg1
18+
* @param {number|string|null|undefined} arg1
1919
* @param {(number|string)[]} args
2020
* @returns {?number}
2121
*/
@@ -29,7 +29,6 @@ const q1 = (query, arg1, ...args) => {
2929
* @deprecated
3030
* @param {string} props
3131
* @param {number | null | undefined} dbid
32-
* @returns {import('./RoamTypes').RoamNode | import('./RoamTypes').RoamBlock | null | undefined}
3332
*/
3433
const PULL_DEPRECATED = (props, dbid) =>
3534
dbid == null
@@ -38,14 +37,27 @@ const PULL_DEPRECATED = (props, dbid) =>
3837

3938
/**
4039
* 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
4342
*/
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+
}
4961

5062
const getStuffThatRefsToId = (/**@type {?number} */ dbid) =>
5163
q("[:find ?e :in $ ?a :where [?e :block/refs ?a]]", dbid)
@@ -56,12 +68,12 @@ const getIdForTitle = (/**@type {?string} */ title) =>
5668
const getParentId = (/**@type {?number} */ dbid) =>
5769
q1("[:find ?e :in $ ?a :where [?e :block/children ?a]]", dbid)
5870

59-
const getIdFromUid = (/**@type {?string} */ uid) =>
71+
const getIdFromUid = (/**@type {string | null | undefined} */ uid) =>
6072
q1("[:find ?e :in $ ?a :where [?e :block/uid ?a]]", uid)
6173

6274
const getCurrentPageUid = () => window.location.hash.split("/").reverse()[0]
6375

64-
const getCurrentPage = () => get(getIdFromUid(getCurrentPageUid()))
76+
const getCurrentPage = () => get(getCurrentPageUid())
6577

6678
const getStuffThatRefsTo = (/**@type {string} */ title) =>
6779
getStuffThatRefsToId(getIdForTitle(title))
@@ -110,18 +122,40 @@ const uidFromElement = (/**@type {Element} */ element) =>
110122
const observer = new MutationObserver((mutationsList, observer) => {
111123
for (const mutation of mutationsList) {
112124
if (mutation.type === "childList") {
113-
114125
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+
})
125159
})
126160

127161
// mutation.removedNodes
@@ -131,6 +165,15 @@ const observer = new MutationObserver((mutationsList, observer) => {
131165
}
132166
})
133167

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+
134177
const roam_onInit = () => {
135178
if (!window.roamAlphaAPI) {
136179
setTimeout(roam_onInit, 100)

0 commit comments

Comments
 (0)