Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 1x 6x 1x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 44x 37x 44x 37x 37x 37x 37x 37x 7x 7x 7x 3644x 19x 7x 7x 37x 47x 37x 14x 7x 37x 3x 3x 3x 3x 37x 3x 37x 3x 37x 2x 1x 1x 1x 37x 2x 2x 2x 2x 37x 3x 37x | import React, { useState, useEffect, FC, useContext } from "react"; import { Select as DefaultSelect, Option, OptGroup, SelectCommonProps, } from "ui/form/Select"; import styled from "styled-components"; import { customEventSelectors, variableSelectors, } from "store/features/entities/entitiesState"; import { groupVariables, NamedVariable, namedVariablesByContext, } from "renderer/lib/variables"; import { CheckIcon, PencilIcon } from "ui/icons/Icons"; import { IMEInput } from "ui/form/IMEInput"; import entitiesActions from "store/features/entities/entitiesActions"; import l10n from "shared/lib/lang/l10n"; import editorActions from "store/features/editor/editorActions"; import { ScriptEditorContext } from "components/script/ScriptEditorContext"; import { UnitsSelectButtonInputOverlay } from "./UnitsSelectButtonInputOverlay"; import { UnitType } from "shared/lib/entities/entitiesTypes"; import { useAppDispatch, useAppSelector } from "store/hooks"; import { SingleValue } from "react-select"; interface VariableSelectProps extends SelectCommonProps { id?: string; name: string; value?: string; entityId: string; allowRename?: boolean; onChange: (newValue: string) => void; units?: UnitType; unitsAllowed?: UnitType[]; onChangeUnits?: (newUnits: UnitType) => void; } export const VariableSelectWrapper = styled.div` position: relative; width: 100%; min-width: 78px; `; const Select: typeof DefaultSelect = styled(DefaultSelect)` .CustomSelect__control { } `; const VariableRenameInput = styled(IMEInput)` &&&& { padding-right: 32px; height: 28px; } `; const VariableRenameButton = styled.button` position: absolute; top: 3px; right: 20px; width: 22px; height: 22px; border: 0; border-radius: ${(props) => Math.max(0, props.theme.borderRadius - 1)}px; display: flex; flex-direction: column; justify-content: center; align-items: center; line-height: 10px; font-size: 12px; font-weight: bold; opacity: 0; transition: opacity 0.3s ease-in-out; background: ${(props) => props.theme.colors.input.background}; border-color: ${(props) => props.theme.colors.input.background}; ${VariableSelectWrapper}:hover & { opacity: 1; } &:focus { opacity: 1; } &:hover { background: rgba(128, 128, 128, 0.3); } &:active { background: rgba(128, 128, 128, 0.4); } svg { width: 12px; height: 12px; fill: #666; } `; const VariableRenameCompleteButton = styled.button` position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; border: 0; border-radius: ${(props) => Math.max(0, props.theme.borderRadius - 1)}px; display: flex; flex-direction: column; justify-content: center; align-items: center; line-height: 10px; font-size: 12px; font-weight: bold; background: transparent; border-color: transparent; &:hover { background: rgba(128, 128, 128, 0.3); } &:active { background: rgba(128, 128, 128, 0.4); } svg { width: 12px; height: 12px; fill: #333; } `; export const VariableToken = styled.span` background: ${(props) => props.theme.colors.token.variable}; box-shadow: 0 0 0px 1px ${(props) => props.theme.colors.token.variable}; border-radius: 5px; color: ${(props) => props.theme.colors.input.background}; `; export const VariableSelect: FC<VariableSelectProps> = ({ value, onChange, entityId, allowRename, units, unitsAllowed, onChangeUnits, ...selectProps }) => { const context = useContext(ScriptEditorContext); const [renameVisible, setRenameVisible] = useState(false); const [editValue, setEditValue] = useState(""); const [renameId, setRenameId] = useState(""); const [variables, setVariables] = useState<NamedVariable[]>([]); const [options, setOptions] = useState<OptGroup[]>([]); const [currentVariable, setCurrentVariable] = useState<NamedVariable>(); const [currentValue, setCurrentValue] = useState<Option>(); const variablesLookup = useAppSelector((state) => variableSelectors.selectEntities(state) ); const customEvent = useAppSelector((state) => customEventSelectors.selectById(state, entityId) ); const dispatch = useAppDispatch(); const valueIsLocal = value && value.startsWith("L"); const valueIsTemp = value && value.startsWith("T"); const canRename = allowRename && !valueIsTemp && context.entityType !== "customEvent"; useEffect(() => { const variables = namedVariablesByContext( context, variablesLookup, customEvent ); const groupedVariables = groupVariables(variables); const groupedOptions: OptGroup[] = groupedVariables.map((g) => { const options = g.variables.map((v) => ({ value: v.id, label: `${v.name}`, })); return { label: g.name, options, }; }); setVariables(variables); setOptions(groupedOptions); }, [entityId, variablesLookup, context, customEvent]); useEffect(() => { setCurrentVariable(variables.find((v) => v.id === value)); }, [variables, value]); useEffect(() => { if (currentVariable) { setCurrentValue({ value: currentVariable.id, label: `$${currentVariable.name}`, }); } }, [currentVariable]); const onRenameStart = () => { if (currentValue) { setEditValue(currentValue.label.replace(/^\$/, "")); setRenameId(currentValue.value); setRenameVisible(true); } }; const onRenameFocus = (e: React.FocusEvent<HTMLInputElement>) => { e.currentTarget.select(); }; const onRename = (e: React.ChangeEvent<HTMLInputElement>) => { setEditValue(e.currentTarget.value); }; const onRenameKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { if (e.key === "Enter") { onRenameFinish(); } else if (e.key === "Escape") { setRenameVisible(false); } }; const onRenameFinish = () => { if (renameId) { Iif (valueIsLocal) { dispatch( entitiesActions.renameVariable({ variableId: `${entityId}__${renameId}`, name: editValue, }) ); } else { dispatch( entitiesActions.renameVariable({ variableId: renameId || "0", name: editValue, }) ); } } setRenameVisible(false); }; const onJumpToVariable = ( e: React.MouseEvent<HTMLDivElement, MouseEvent> ) => { Iif (e.altKey) { Iif ( value && context.entityType !== "customEvent" && Number.isInteger(Number(value)) ) { dispatch(editorActions.selectVariable({ variableId: value })); } } }; return ( <VariableSelectWrapper onClick={onJumpToVariable}> {renameVisible ? ( <VariableRenameInput key={renameId} value={editValue} onChange={onRename} onKeyDown={onRenameKeyDown} onFocus={onRenameFocus} onBlur={onRenameFinish} autoFocus /> ) : ( <Select value={currentValue} options={options} onChange={(newValue: SingleValue<Option>) => { Iif (newValue) { onChange(newValue.value); } }} {...selectProps} /> )} {canRename && (renameVisible ? ( <VariableRenameCompleteButton onClick={onRenameFinish} title={l10n("FIELD_RENAME")} > <CheckIcon /> </VariableRenameCompleteButton> ) : ( <VariableRenameButton onClick={onRenameStart} title={l10n("FIELD_RENAME")} > <PencilIcon /> </VariableRenameButton> ))} {units && ( <UnitsSelectButtonInputOverlay parentValue={(currentValue && currentValue.label) ?? ""} value={units} allowedValues={unitsAllowed} onChange={onChangeUnits} /> )} </VariableSelectWrapper> ); }; |