|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { |
| 3 | + Checkbox, |
| 4 | + InputGroup, |
| 5 | + Label, |
| 6 | + NumericInput, |
| 7 | + HTMLSelect, |
| 8 | + Button, |
| 9 | + Tag, |
| 10 | +} from "@blueprintjs/core"; |
| 11 | +import Description from "roamjs-components/components/Description"; |
| 12 | +import idToTitle from "roamjs-components/util/idToTitle"; |
| 13 | +import { getGlobalSetting, setGlobalSetting } from "../utils/accessors"; |
| 14 | + |
| 15 | +type BaseProps = { |
| 16 | + title: string; |
| 17 | + description: string; |
| 18 | + settingKeys: string[]; |
| 19 | +}; |
| 20 | + |
| 21 | +export const BlockPropTextPanel = ({ |
| 22 | + title, |
| 23 | + description, |
| 24 | + settingKeys, |
| 25 | + defaultValue = "", |
| 26 | + placeholder, |
| 27 | +}: BaseProps & { |
| 28 | + defaultValue?: string; |
| 29 | + placeholder?: string; |
| 30 | +}) => { |
| 31 | + const [value, setValue] = useState( |
| 32 | + () => getGlobalSetting<string>(settingKeys) ?? defaultValue, |
| 33 | + ); |
| 34 | + |
| 35 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 36 | + const newValue = e.target.value; |
| 37 | + setValue(newValue); |
| 38 | + setGlobalSetting(settingKeys, newValue); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <Label> |
| 43 | + {idToTitle(title)} |
| 44 | + <Description description={description} /> |
| 45 | + <InputGroup |
| 46 | + value={value} |
| 47 | + onChange={handleChange} |
| 48 | + placeholder={placeholder || defaultValue} |
| 49 | + /> |
| 50 | + </Label> |
| 51 | + ); |
| 52 | +}; |
| 53 | + |
| 54 | +export const BlockPropFlagPanel = ({ |
| 55 | + title, |
| 56 | + description, |
| 57 | + settingKeys, |
| 58 | + defaultValue = false, |
| 59 | + disabled = false, |
| 60 | + onChange, |
| 61 | +}: BaseProps & { |
| 62 | + defaultValue?: boolean; |
| 63 | + disabled?: boolean; |
| 64 | + onChange?: (checked: boolean) => void; |
| 65 | +}) => { |
| 66 | + const [value, setValue] = useState( |
| 67 | + () => getGlobalSetting<boolean>(settingKeys) ?? defaultValue, |
| 68 | + ); |
| 69 | + |
| 70 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 71 | + const { checked } = e.target; |
| 72 | + setValue(checked); |
| 73 | + setGlobalSetting(settingKeys, checked); |
| 74 | + onChange?.(checked); |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <Checkbox |
| 79 | + checked={value} |
| 80 | + onChange={handleChange} |
| 81 | + disabled={disabled} |
| 82 | + labelElement={ |
| 83 | + <> |
| 84 | + {idToTitle(title)} |
| 85 | + <Description description={description} /> |
| 86 | + </> |
| 87 | + } |
| 88 | + /> |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +export const BlockPropNumberPanel = ({ |
| 93 | + title, |
| 94 | + description, |
| 95 | + settingKeys, |
| 96 | + defaultValue = 0, |
| 97 | + min, |
| 98 | + max, |
| 99 | +}: BaseProps & { |
| 100 | + defaultValue?: number; |
| 101 | + min?: number; |
| 102 | + max?: number; |
| 103 | +}) => { |
| 104 | + const [value, setValue] = useState( |
| 105 | + () => getGlobalSetting<number>(settingKeys) ?? defaultValue, |
| 106 | + ); |
| 107 | + |
| 108 | + const handleChange = (valueAsNumber: number) => { |
| 109 | + setValue(valueAsNumber); |
| 110 | + setGlobalSetting(settingKeys, valueAsNumber); |
| 111 | + }; |
| 112 | + |
| 113 | + return ( |
| 114 | + <Label> |
| 115 | + {idToTitle(title)} |
| 116 | + <Description description={description} /> |
| 117 | + <NumericInput |
| 118 | + value={value} |
| 119 | + onValueChange={handleChange} |
| 120 | + min={min} |
| 121 | + max={max} |
| 122 | + fill |
| 123 | + /> |
| 124 | + </Label> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export const BlockPropSelectPanel = ({ |
| 129 | + title, |
| 130 | + description, |
| 131 | + settingKeys, |
| 132 | + options, |
| 133 | + defaultValue, |
| 134 | +}: BaseProps & { |
| 135 | + options: string[]; |
| 136 | + defaultValue?: string; |
| 137 | +}) => { |
| 138 | + const [value, setValue] = useState( |
| 139 | + () => getGlobalSetting<string>(settingKeys) ?? defaultValue ?? options[0], |
| 140 | + ); |
| 141 | + |
| 142 | + const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => { |
| 143 | + const newValue = e.target.value; |
| 144 | + setValue(newValue); |
| 145 | + setGlobalSetting(settingKeys, newValue); |
| 146 | + }; |
| 147 | + |
| 148 | + return ( |
| 149 | + <Label> |
| 150 | + {idToTitle(title)} |
| 151 | + <Description description={description} /> |
| 152 | + <HTMLSelect value={value} onChange={handleChange} fill options={options} /> |
| 153 | + </Label> |
| 154 | + ); |
| 155 | +}; |
| 156 | + |
| 157 | +export const BlockPropMultiTextPanel = ({ |
| 158 | + title, |
| 159 | + description, |
| 160 | + settingKeys, |
| 161 | + defaultValue = [], |
| 162 | +}: BaseProps & { |
| 163 | + defaultValue?: string[]; |
| 164 | +}) => { |
| 165 | + const [values, setValues] = useState<string[]>( |
| 166 | + () => getGlobalSetting<string[]>(settingKeys) ?? defaultValue, |
| 167 | + ); |
| 168 | + const [inputValue, setInputValue] = useState(""); |
| 169 | + |
| 170 | + const handleAdd = () => { |
| 171 | + if (inputValue.trim() && !values.includes(inputValue.trim())) { |
| 172 | + const newValues = [...values, inputValue.trim()]; |
| 173 | + setValues(newValues); |
| 174 | + setGlobalSetting(settingKeys, newValues); |
| 175 | + setInputValue(""); |
| 176 | + } |
| 177 | + }; |
| 178 | + |
| 179 | + const handleRemove = (index: number) => { |
| 180 | + const newValues = values.filter((_, i) => i !== index); |
| 181 | + setValues(newValues); |
| 182 | + setGlobalSetting(settingKeys, newValues); |
| 183 | + }; |
| 184 | + |
| 185 | + const handleKeyDown = (e: React.KeyboardEvent) => { |
| 186 | + if (e.key === "Enter") { |
| 187 | + e.preventDefault(); |
| 188 | + handleAdd(); |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + return ( |
| 193 | + <Label> |
| 194 | + {idToTitle(title)} |
| 195 | + <Description description={description} /> |
| 196 | + <div className="flex gap-2"> |
| 197 | + <InputGroup |
| 198 | + value={inputValue} |
| 199 | + onChange={(e) => setInputValue(e.target.value)} |
| 200 | + onKeyDown={handleKeyDown} |
| 201 | + placeholder="Add new item..." |
| 202 | + className="flex-grow" |
| 203 | + /> |
| 204 | + <Button icon="plus" onClick={handleAdd} disabled={!inputValue.trim()} /> |
| 205 | + </div> |
| 206 | + {values.length > 0 && ( |
| 207 | + <div className="mt-2 flex flex-wrap gap-1"> |
| 208 | + {values.map((v, i) => ( |
| 209 | + <Tag key={i} onRemove={() => handleRemove(i)} minimal> |
| 210 | + {v} |
| 211 | + </Tag> |
| 212 | + ))} |
| 213 | + </div> |
| 214 | + )} |
| 215 | + </Label> |
| 216 | + ); |
| 217 | +}; |
0 commit comments