Skip to content

Commit 9d33cb5

Browse files
authored
ENG-1398 Bug: is in canvas query builder condition only supports legacy canvas schema (#870)
* Enhance conditionToDatalog utility with canvas page target retrieval - Introduced getCanvasPageTargets function to dynamically fetch canvas page names based on configuration. - Replaced hardcoded canvas page title retrieval with the new function in targetOptions. - Simplified shape clause handling by utilizing getCanvasMembershipShapeClauses. This update improves flexibility and maintainability of the query builder's canvas integration. * format
1 parent b51c8ce commit 9d33cb5

2 files changed

Lines changed: 134 additions & 44 deletions

File tree

apps/roam/src/utils/conditionToDatalog.ts

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,27 @@ import { Condition } from "./types";
1313
import gatherDatalogVariablesFromClause from "./gatherDatalogVariablesFromClause";
1414
import getCurrentPageUid from "roamjs-components/dom/getCurrentPageUid";
1515
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
16-
import getPageTitlesStartingWithPrefix from "roamjs-components/queries/getPageTitlesStartingWithPrefix";
1716
import extractRef from "roamjs-components/util/extractRef";
1817
import getCurrentUserDisplayName from "roamjs-components/queries/getCurrentUserDisplayName";
1918
import getPageTitleByBlockUid from "roamjs-components/queries/getPageTitleByBlockUid";
19+
import { getFormattedConfigTree } from "./discourseConfigRef";
20+
import { getCanvasMembershipShapeClauses } from "./isInCanvasDatalog";
2021

2122
type ConditionToDatalog = (condition: Condition) => DatalogClause[];
2223

2324
const INPUT_REGEX = /^:in /;
25+
const DEFAULT_CANVAS_PAGE_FORMAT = "Canvas/*";
26+
27+
const getCanvasPageTargets = (): string[] => {
28+
const { canvasPageFormat } = getFormattedConfigTree();
29+
const canvasFormat = canvasPageFormat.value || DEFAULT_CANVAS_PAGE_FORMAT;
30+
const formatRegex = new RegExp(
31+
`^${canvasFormat
32+
.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
33+
.replace(/\\\*/g, ".+")}$`,
34+
);
35+
return getAllPageNames().filter((title) => formatRegex.test(title));
36+
};
2437

2538
const isRegex = (str: string) => /^\/.+\/(i)?$/.test(str);
2639
const regexRePatternValue = (str: string) => {
@@ -917,49 +930,9 @@ const translator: Record<string, Translator> = {
917930
variable: { type: "variable", value: `${target}-Canvas-RQB` },
918931
},
919932
},
920-
{
921-
type: "fn-expr",
922-
fn: "get",
923-
arguments: [
924-
{ type: "variable", value: `${target}-Canvas-RQB` },
925-
{ type: "constant", value: ":tldraw" },
926-
],
927-
binding: {
928-
type: "bind-rel",
929-
args: [
930-
{ type: "variable", value: `${target}-TLDraw-Key` },
931-
{ type: "variable", value: `${target}-TLDraw-Value` },
932-
],
933-
},
934-
},
935-
{
936-
type: "fn-expr",
937-
fn: "get",
938-
arguments: [
939-
{ type: "variable", value: `${target}-TLDraw-Value` },
940-
{ type: "constant", value: ":props" },
941-
],
942-
binding: {
943-
type: "bind-scalar",
944-
variable: { type: "variable", value: `${target}-Shape-Props` },
945-
},
946-
},
947-
{
948-
type: "fn-expr",
949-
fn: "get",
950-
arguments: [
951-
{ type: "variable", value: `${target}-Shape-Props` },
952-
{ type: "constant", value: ":uid" },
953-
],
954-
binding: {
955-
type: "bind-scalar",
956-
variable: { type: "variable", value: `${source}-uid` },
957-
},
958-
},
933+
...getCanvasMembershipShapeClauses({ source, target }),
959934
],
960-
targetOptions: () =>
961-
// TODO - use roam depot setting
962-
getPageTitlesStartingWithPrefix("Canvas/").concat(["{current}"]),
935+
targetOptions: () => getCanvasPageTargets().concat(["{current}"]),
963936
placeholder: "Enter a page name",
964937
},
965938
"has block reference": {
@@ -1035,7 +1008,7 @@ const conditionToDatalog: ConditionToDatalog = (con) => {
10351008
type: "or-join-clause",
10361009
clauses,
10371010
variables: Object.entries(variableSet)
1038-
.filter(([_, v]) => v === clauses.length)
1011+
.filter(([, v]) => v === clauses.length)
10391012
.map(([value]) => ({
10401013
type: "variable",
10411014
value,
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import type { DatalogAndClause, DatalogClause } from "roamjs-components/types";
2+
3+
const getLegacyCanvasLookupClauses = ({
4+
target,
5+
}: {
6+
target: string;
7+
}): DatalogClause[] =>
8+
[
9+
{
10+
type: "fn-expr",
11+
fn: "get",
12+
arguments: [
13+
{ type: "variable", value: `${target}-Canvas-RQB` },
14+
{ type: "constant", value: ":tldraw" },
15+
],
16+
binding: {
17+
type: "bind-rel",
18+
args: [
19+
{ type: "variable", value: `${target}-TLDraw-Key` },
20+
{ type: "variable", value: `${target}-TLDraw-Value` },
21+
],
22+
},
23+
},
24+
] as DatalogClause[];
25+
26+
// tldraw 2.x schema
27+
const getCanvas2LookupClauses = ({
28+
target,
29+
}: {
30+
target: string;
31+
}): DatalogClause[] =>
32+
[
33+
{
34+
type: "fn-expr",
35+
fn: "get",
36+
arguments: [
37+
{ type: "variable", value: `${target}-Canvas-RQB` },
38+
{ type: "constant", value: ":tldraw" },
39+
],
40+
binding: {
41+
type: "bind-scalar",
42+
variable: { type: "variable", value: `${target}-TLDraw` },
43+
},
44+
},
45+
{
46+
type: "fn-expr",
47+
fn: "get",
48+
arguments: [
49+
{ type: "variable", value: `${target}-TLDraw` },
50+
{ type: "constant", value: ":store" },
51+
],
52+
binding: {
53+
type: "bind-rel",
54+
args: [
55+
{ type: "variable", value: `${target}-TLDraw-Key` },
56+
{ type: "variable", value: `${target}-TLDraw-Value` },
57+
],
58+
},
59+
},
60+
] as DatalogClause[];
61+
62+
export const getCanvasMembershipShapeClauses = ({
63+
source,
64+
target,
65+
}: {
66+
source: string;
67+
target: string;
68+
}): DatalogClause[] => {
69+
const sourceUidVar = `${source}-uid`;
70+
const canvasRqbVar = `${target}-Canvas-RQB`;
71+
const tldrawValueVar = `${target}-TLDraw-Value`;
72+
const shapePropsVar = `${target}-Shape-Props`;
73+
74+
return [
75+
{
76+
type: "or-join-clause",
77+
variables: [
78+
{ type: "variable", value: canvasRqbVar },
79+
{ type: "variable", value: tldrawValueVar },
80+
],
81+
clauses: [
82+
{
83+
type: "and-clause",
84+
clauses: getLegacyCanvasLookupClauses({ target }),
85+
} as DatalogAndClause,
86+
{
87+
type: "and-clause",
88+
clauses: getCanvas2LookupClauses({ target }),
89+
} as DatalogAndClause,
90+
],
91+
} as DatalogClause,
92+
{
93+
type: "fn-expr",
94+
fn: "get",
95+
arguments: [
96+
{ type: "variable", value: tldrawValueVar },
97+
{ type: "constant", value: ":props" },
98+
],
99+
binding: {
100+
type: "bind-scalar",
101+
variable: { type: "variable", value: shapePropsVar },
102+
},
103+
} as DatalogClause,
104+
{
105+
type: "fn-expr",
106+
fn: "get",
107+
arguments: [
108+
{ type: "variable", value: shapePropsVar },
109+
{ type: "constant", value: ":uid" },
110+
],
111+
binding: {
112+
type: "bind-scalar",
113+
variable: { type: "variable", value: sourceUidVar },
114+
},
115+
} as DatalogClause,
116+
];
117+
};

0 commit comments

Comments
 (0)