From a9c77ae5068d1bc895e4a2bf9e87ef27bfcc3db6 Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Thu, 18 Jan 2024 22:57:54 -0600 Subject: [PATCH] add optionType and slider --- src/features/attributeSelect.tsx | 392 +++++++++++++++++++++---------- 1 file changed, 274 insertions(+), 118 deletions(-) diff --git a/src/features/attributeSelect.tsx b/src/features/attributeSelect.tsx index 3efbcb0..4ec440e 100644 --- a/src/features/attributeSelect.tsx +++ b/src/features/attributeSelect.tsx @@ -1,6 +1,18 @@ import React, { useEffect, useMemo, useRef, useState, ReactText } from "react"; import ReactDOM from "react-dom"; -import { Classes, Button, Tabs, Tab, Card, MenuItem } from "@blueprintjs/core"; +import { + Classes, + Button, + Tabs, + Tab, + Card, + MenuItem, + FormGroup, + Label, + NumericInput, + Slider, + Popover, +} from "@blueprintjs/core"; import { Select } from "@blueprintjs/select"; import createHTMLObserver from "roamjs-components/dom/createHTMLObserver"; import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid"; @@ -17,6 +29,9 @@ import MenuItemSelect from "roamjs-components/components/MenuItemSelect"; import addStyle from "roamjs-components/dom/addStyle"; import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid"; import { render as renderToast } from "roamjs-components/components/Toast"; +import setInputSetting from "roamjs-components/util/setInputSetting"; +import setInputSettings from "roamjs-components/util/setInputSettings"; +import getSettingValueFromTree from "roamjs-components/util/getSettingValueFromTree"; const CONFIG = `roam/js/attribute-select`; @@ -28,6 +43,7 @@ type AttributeButtonPopoverProps = { uid: string; currentValue: string; filterable?: boolean; + isOpen: boolean; }; const AttributeButtonPopover = ({ @@ -37,12 +53,16 @@ const AttributeButtonPopover = ({ uid, currentValue, filterable = false, + isOpen, }: AttributeButtonPopoverProps) => { const AttributeSelect = Select.ofType(); const itemPredicate = (query: string, item: T) => { return String(item).toLowerCase().includes(query.toLowerCase()); }; - + const [sliderValue, setSliderValue] = useState(0); + useEffect(() => { + setSliderValue(Number(currentValue)); + }, [isOpen, currentValue]); return ( ([]); const [currentValue, setCurrentValue] = useState(""); - useEffect(() => { - if (isOpen) { - const configUid = getPageUidByPageTitle(CONFIG); - const attributesNode = getSubTree({ - key: "attributes", - parentUid: configUid, - }); - const attributeUid = getSubTree({ - key: attributeName, - parentUid: attributesNode.uid, - }).uid; - const optionsNode = getSubTree({ - key: "options", - parentUid: attributeUid, - }); + const configUid = getPageUidByPageTitle(CONFIG); + const attributesNode = getSubTree({ + key: "attributes", + parentUid: configUid, + }); + const attributeUid = getSubTree({ + key: attributeName, + parentUid: attributesNode.uid, + }).uid; + const optionType = + getSettingValueFromTree({ + key: "type", + parentUid: attributeUid, + }) || "text"; - const useSmartBlocks = - optionsNode.children.filter((obj) => obj.text.includes("<%")).length > - 0; + useEffect(() => { + if (!isOpen) return; + const optionsNode = getSubTree({ + key: "options", + parentUid: attributeUid, + }); + const rangeNode = getSubTree({ + key: "range", + parentUid: attributeUid, + }); + const useSmartBlocks = + optionsNode.children.filter((obj) => obj.text.includes("<%")).length > 0; + if (optionType === "number") { + setOptions([rangeNode.children[0]?.text, rangeNode.children[1]?.text]); + } + if (optionType === "text") { if (useSmartBlocks && !window.roamjs?.extension?.smartblocks) { renderToast({ content: @@ -129,19 +161,62 @@ const AttributeButton = ({ } else { setOptions(optionsNode.children.map((t) => t.text)); } - const regex = new RegExp(`^${attributeName}::\\s*`); - setCurrentValue(getTextByBlockUid(uid).replace(regex, "").trim()); } + const regex = new RegExp(`^${attributeName}::\\s*`); + setCurrentValue(getTextByBlockUid(uid).replace(regex, "").trim()); }, [isOpen]); - + const [sliderValue, setSliderValue] = useState(0); + useEffect(() => { + setSliderValue(Number(currentValue)); + }, [isOpen, currentValue]); + const min = Number(options[0]) || 0; + const max = Number(options[1]) || 10; return ( - + <> + {optionType === "number" ? ( + setSliderValue(value)} + onRelease={(s) => { + updateBlock({ + text: `${attributeName}:: ${s}`, + uid, + }); + setIsOpen(false); + }} + /> + } + target={ +