-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDiscourseNodeCanvasSettings.tsx
More file actions
135 lines (131 loc) · 3.93 KB
/
DiscourseNodeCanvasSettings.tsx
File metadata and controls
135 lines (131 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {
InputGroup,
Radio,
RadioGroup,
Tooltip,
Icon,
} from "@blueprintjs/core";
import React, { useState, useMemo } from "react";
import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByParentUid";
import getSettingValueFromTree from "roamjs-components/util/getSettingValueFromTree";
import setInputSetting from "roamjs-components/util/setInputSetting";
import {
DiscourseNodeFlagPanel,
DiscourseNodeTextPanel,
} from "./components/BlockPropSettingPanels";
import { setDiscourseNodeSetting } from "~/components/settings/utils/accessors";
export const formatHexColor = (color: string) => {
if (!color) return "";
const COLOR_TEST = /^[0-9a-f]{6}$/i;
if (color.startsWith("#")) {
// handle legacy color format
return color;
} else if (COLOR_TEST.test(color)) {
return "#" + color;
}
return "";
};
const DiscourseNodeCanvasSettings = ({
nodeType,
uid,
}: {
nodeType: string;
uid: string;
}) => {
const tree = useMemo(() => getBasicTreeByParentUid(uid), [uid]);
const alias = getSettingValueFromTree({ tree, key: "alias" });
const [queryBuilderAlias, setQueryBuilderAlias] = useState<string>(() =>
getSettingValueFromTree({ tree, key: "query-builder-alias" }),
);
const [isKeyImage, setIsKeyImage] = useState(
() => getSettingValueFromTree({ tree, key: "key-image" }) === "true",
);
const [keyImageOption, setKeyImageOption] = useState(() =>
getSettingValueFromTree({ tree, key: "key-image-option" }),
);
return (
<div>
<DiscourseNodeTextPanel
nodeType={nodeType}
title="Display alias"
description=""
settingKeys={["canvasSettings", "alias"]}
initialValue={alias}
onChange={(val) => {
void setInputSetting({
blockUid: uid,
key: "alias",
value: val,
});
}}
/>
<DiscourseNodeFlagPanel
nodeType={nodeType}
title="Key image"
description="Add an image to the discourse node"
settingKeys={["canvasSettings", "key-image"]}
initialValue={isKeyImage}
onChange={(checked) => {
setIsKeyImage(checked);
if (checked && !keyImageOption) setKeyImageOption("first-image");
void setInputSetting({
blockUid: uid,
key: "key-image",
value: checked ? "true" : "false",
});
}}
/>
<RadioGroup
disabled={!isKeyImage}
selectedValue={keyImageOption || "first-image"}
label="Key image location"
onChange={(e) => {
const value = (e.target as HTMLInputElement).value;
setKeyImageOption(value);
void setInputSetting({
blockUid: uid,
key: "key-image-option",
value,
});
setDiscourseNodeSetting(
nodeType,
["canvasSettings", "key-image-option"],
value,
);
}}
>
<Radio label="First image on page" value="first-image" />
<Radio value="query-builder">
Query builder reference
<Tooltip content={"Use a query builder alias or block reference"}>
<Icon
icon={"info-sign"}
iconSize={12}
className={"ml-2 align-middle opacity-80"}
/>
</Tooltip>
</Radio>
</RadioGroup>
<InputGroup
style={{ width: 240 }}
disabled={keyImageOption !== "query-builder" || !isKeyImage}
value={queryBuilderAlias}
onChange={(e) => {
const val = e.target.value;
setQueryBuilderAlias(val);
void setInputSetting({
blockUid: uid,
key: "query-builder-alias",
value: val,
});
setDiscourseNodeSetting(
nodeType,
["canvasSettings", "query-builder-alias"],
val,
);
}}
/>
</div>
);
};
export default DiscourseNodeCanvasSettings;