@@ -6,13 +6,12 @@ import {
66 Tooltip ,
77 Icon ,
88 ControlGroup ,
9- Checkbox ,
109} from "@blueprintjs/core" ;
11- import React , { useState } from "react" ;
12- import {
13- getDiscourseNodeSetting ,
14- setDiscourseNodeSetting ,
15- } from "./utils/accessors " ;
10+ import React , { useState , useMemo } from "react" ;
11+ import getBasicTreeByParentUid from "roamjs-components/queries/getBasicTreeByParentUid" ;
12+ import getSettingValueFromTree from "roamjs-components/util/getSettingValueFromTree" ;
13+ import setInputSetting from "roamjs-components/util/setInputSetting" ;
14+ import { DiscourseNodeFlagPanel } from "./components/BlockPropSettingPanels " ;
1615
1716export const formatHexColor = ( color : string ) => {
1817 if ( ! color ) return "" ;
@@ -26,31 +25,23 @@ export const formatHexColor = (color: string) => {
2625 return "" ;
2726} ;
2827
29- const DiscourseNodeCanvasSettings = ( { nodeType } : { nodeType : string } ) => {
28+ const DiscourseNodeCanvasSettings = ( { nodeType, uid } : { nodeType : string ; uid : string } ) => {
29+ const tree = useMemo ( ( ) => getBasicTreeByParentUid ( uid ) , [ uid ] ) ;
3030 const [ color , setColor ] = useState < string > ( ( ) => {
31- const storedColor = getDiscourseNodeSetting < string > ( nodeType , [
32- "canvasSettings" ,
33- "color" ,
34- ] ) ! ;
35- return formatHexColor ( storedColor ) ;
31+ const color = getSettingValueFromTree ( { tree, key : "color" } ) ;
32+ return formatHexColor ( color ) ;
3633 } ) ;
3734 const [ alias , setAlias ] = useState < string > ( ( ) =>
38- getDiscourseNodeSetting < string > ( nodeType , [ "canvasSettings" , "alias" ] ) ! ,
35+ getSettingValueFromTree ( { tree , key : "alias" } ) ,
3936 ) ;
4037 const [ queryBuilderAlias , setQueryBuilderAlias ] = useState < string > ( ( ) =>
41- getDiscourseNodeSetting < string > ( nodeType , [
42- "canvasSettings" ,
43- "query-builder-alias" ,
44- ] ) ! ,
38+ getSettingValueFromTree ( { tree, key : "query-builder-alias" } ) ,
4539 ) ;
46- const [ isKeyImage , setIsKeyImage ] = useState ( ( ) =>
47- getDiscourseNodeSetting < boolean > ( nodeType , [ "canvasSettings" , "key-image" ] ) ! ,
40+ const [ isKeyImage , setIsKeyImage ] = useState (
41+ ( ) => getSettingValueFromTree ( { tree , key : "key-image" } ) === "true" ,
4842 ) ;
4943 const [ keyImageOption , setKeyImageOption ] = useState ( ( ) =>
50- getDiscourseNodeSetting < string > ( nodeType , [
51- "canvasSettings" ,
52- "key-image-option" ,
53- ] ) ! ,
44+ getSettingValueFromTree ( { tree, key : "key-image-option" } ) ,
5445 ) ;
5546
5647 return (
@@ -61,14 +52,14 @@ const DiscourseNodeCanvasSettings = ({ nodeType }: { nodeType: string }) => {
6152 < InputGroup
6253 style = { { width : 120 } }
6354 type = { "color" }
64- value = { color || "#000000" }
55+ value = { color }
6556 onChange = { ( e ) => {
6657 setColor ( e . target . value ) ;
67- setDiscourseNodeSetting (
68- nodeType ,
69- [ "canvasSettings" , "color" ] ,
70- e . target . value . replace ( "#" , "" ) , // remove hash to not create roam link
71- ) ;
58+ setInputSetting ( {
59+ blockUid : uid ,
60+ key : "color" ,
61+ value : e . target . value . replace ( "#" , "" ) , // remove hash to not create roam link
62+ } ) ;
7263 } }
7364 />
7465 < Tooltip content = { color ? "Unset" : "Color not set" } >
@@ -77,11 +68,11 @@ const DiscourseNodeCanvasSettings = ({ nodeType }: { nodeType: string }) => {
7768 icon = { color ? "delete" : "info-sign" }
7869 onClick = { ( ) => {
7970 setColor ( "" ) ;
80- setDiscourseNodeSetting (
81- nodeType ,
82- [ "canvasSettings" , "color" ] ,
83- "" ,
84- ) ;
71+ setInputSetting ( {
72+ blockUid : uid ,
73+ key : "color" ,
74+ value : "" ,
75+ } ) ;
8576 } }
8677 />
8778 </ Tooltip >
@@ -93,57 +84,42 @@ const DiscourseNodeCanvasSettings = ({ nodeType }: { nodeType: string }) => {
9384 value = { alias }
9485 onChange = { ( e ) => {
9586 setAlias ( e . target . value ) ;
96- setDiscourseNodeSetting (
97- nodeType ,
98- [ "canvasSettings" , "alias" ] ,
99- e . target . value ,
100- ) ;
87+ setInputSetting ( {
88+ blockUid : uid ,
89+ key : "alias" ,
90+ value : e . target . value ,
91+ } ) ;
10192 } }
10293 />
10394 </ Label >
104- < Checkbox
105- style = { { width : 240 , lineHeight : "normal" } }
106- checked = { isKeyImage }
107- onChange = { ( e ) => {
108- const target = e . target as HTMLInputElement ;
109- setIsKeyImage ( target . checked ) ;
110- if ( target . checked ) {
111- if ( ! keyImageOption ) setKeyImageOption ( "first-image" ) ;
112- setDiscourseNodeSetting (
113- nodeType ,
114- [ "canvasSettings" , "key-image" ] ,
115- true ,
116- ) ;
117- } else {
118- setDiscourseNodeSetting (
119- nodeType ,
120- [ "canvasSettings" , "key-image" ] ,
121- false ,
122- ) ;
123- }
95+ < DiscourseNodeFlagPanel
96+ nodeType = { nodeType }
97+ title = "Key Image"
98+ description = "Add an image to the discourse node"
99+ settingKeys = { [ "canvasSettings" , "key-image" ] }
100+ defaultValue = { false }
101+ onChange = { ( checked ) => {
102+ setIsKeyImage ( checked ) ;
103+ if ( checked && ! keyImageOption ) setKeyImageOption ( "first-image" ) ;
104+ setInputSetting ( {
105+ blockUid : uid ,
106+ key : "key-image" ,
107+ value : checked ? "true" : "false" ,
108+ } ) ;
124109 } }
125- >
126- Key image
127- < Tooltip content = { "Add an image to the discourse node" } >
128- < Icon
129- icon = { "info-sign" }
130- iconSize = { 12 }
131- className = { "ml-2 align-middle opacity-80" }
132- />
133- </ Tooltip >
134- </ Checkbox >
110+ />
135111 < RadioGroup
136112 disabled = { ! isKeyImage }
137113 selectedValue = { keyImageOption || "first-image" }
138114 label = "Key image location"
139115 onChange = { ( e ) => {
140- const target = e . target as HTMLInputElement ;
141- setKeyImageOption ( target . value ) ;
142- setDiscourseNodeSetting (
143- nodeType ,
144- [ "canvasSettings" , "key-image-option" ] ,
145- target . value ,
146- ) ;
116+ const value = ( e . target as HTMLInputElement ) . value ;
117+ setKeyImageOption ( value ) ;
118+ setInputSetting ( {
119+ blockUid : uid ,
120+ key : "key-image-option" ,
121+ value,
122+ } ) ;
147123 } }
148124 >
149125 < Radio label = "First image on page" value = "first-image" />
@@ -164,11 +140,11 @@ const DiscourseNodeCanvasSettings = ({ nodeType }: { nodeType: string }) => {
164140 value = { queryBuilderAlias }
165141 onChange = { ( e ) => {
166142 setQueryBuilderAlias ( e . target . value ) ;
167- setDiscourseNodeSetting (
168- nodeType ,
169- [ "canvasSettings" , "query-builder-alias" ] ,
170- e . target . value ,
171- ) ;
143+ setInputSetting ( {
144+ blockUid : uid ,
145+ key : "query-builder-alias" ,
146+ value : e . target . value ,
147+ } ) ;
172148 } }
173149 />
174150 </ div >
0 commit comments