Skip to content

Commit

Permalink
Merge branch 'fix/update-gdd' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Oct 20, 2023
2 parents b121eff + 7dfa853 commit d946c76
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 22 deletions.
2 changes: 1 addition & 1 deletion apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"formik": "^2.2.9",
"formik-mui": "^5.0.0-alpha.0",
"got": "^11.8.2",
"graphics-data-definition": "0.1.1-nightly-fix-npm-repo-20230214-163658-dbb8098.0",
"graphics-data-definition": "1.1.1",
"koa": "^2.13.4",
"koa-bodyparser": "^4.4.0",
"lodash": "^4.17.21",
Expand Down
18 changes: 15 additions & 3 deletions apps/app/src/react/components/inputs/DurationInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export const DurationInput: React.FC<
allowUndefined: true
emptyPlaceholder?: string
label?: React.ReactNode
focusTooltip?: string
disabled?: boolean
fullWidth?: boolean
width?: string
tooltip?: string
endAdornment?: React.ReactNode
}
| {
Expand All @@ -24,9 +26,11 @@ export const DurationInput: React.FC<
emptyPlaceholder?: string
defaultValue: number
label?: React.ReactNode
focusTooltip?: string
disabled?: boolean
fullWidth?: boolean
width?: string
tooltip?: string
endAdornment?: React.ReactNode
}
| {
Expand All @@ -38,9 +42,11 @@ export const DurationInput: React.FC<
emptyPlaceholder?: string
defaultValue: number | null
label?: React.ReactNode
focusTooltip?: string
disabled?: boolean
fullWidth?: boolean
width?: string
tooltip?: string
endAdornment?: React.ReactNode
}
> = (props) => {
Expand Down Expand Up @@ -76,7 +82,9 @@ export const DurationInput: React.FC<
props.width,
undefined,
onIncrement,
props.endAdornment
props.endAdornment,
props.tooltip,
props.focusTooltip
)
} else if (props.allowNull) {
return ParsedValueInput<number | null>(
Expand All @@ -94,7 +102,9 @@ export const DurationInput: React.FC<
props.width,
undefined,
onIncrement,
props.endAdornment
props.endAdornment,
props.tooltip,
props.focusTooltip
)
} else {
return ParsedValueInput<number>(
Expand All @@ -112,7 +122,9 @@ export const DurationInput: React.FC<
props.width,
undefined,
onIncrement,
props.endAdornment
props.endAdornment,
props.tooltip,
props.focusTooltip
)
}
}
98 changes: 98 additions & 0 deletions apps/app/src/react/components/inputs/PercentageInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React, { useCallback } from 'react'
import { ParsedValueInput } from './parsedValueInput'

export const PercentageInput: React.FC<
(
| {
currentValue: number | undefined
onChange: (newValue: number | undefined) => void
allowUndefined: true
}
| {
currentValue: number
onChange: (newValue: number) => void
allowUndefined: false
defaultValue: number
}
) & {
indeterminate?: boolean
emptyPlaceholder?: string
label?: React.ReactNode
focusTooltip?: string
disabled?: boolean
fullWidth?: boolean
width?: string
tooltip?: string
endAdornment?: React.ReactNode
}
> = (props) => {
const onIncrement = useCallback((value: number | undefined | null, inc: number) => {
value = (value ?? 0) + inc / 100
value = Math.round(value * 100000) / 100000 // fix any rounding errors
value = Math.max(0, value)
return value
}, [])

if (props.allowUndefined) {
return ParsedValueInput<number | undefined>(
props.currentValue,
props.indeterminate,
props.onChange,
undefined,
(str: string) => parsePercentage(str) ?? undefined,
formatPercentage,
props.label,
props.emptyPlaceholder,
'text',
props.disabled,
props.fullWidth,
props.width,
undefined,
onIncrement,
props.endAdornment,
props.tooltip,
props.focusTooltip
)
} else {
return ParsedValueInput<number>(
props.currentValue,
props.indeterminate,
props.onChange,
props.defaultValue,
(str: string) => parsePercentage(str) ?? undefined,
formatPercentage,
props.label,
props.emptyPlaceholder,
'text',
props.disabled,
props.fullWidth,
props.width,
undefined,
onIncrement,
props.endAdornment,
props.tooltip,
props.focusTooltip
)
}
}

function parsePercentage(str: string): number | undefined {
str = str + ''
if (str === '') return undefined
str = str.replace(/,/, '.')
const m = str.match(/([\d,.]+)/)
if (m) {
const percentage = parseFloat(m[1])
if (!Number.isNaN(percentage)) {
return percentage / 100
}
}

return 0
}
function formatPercentage(inputValue: number | undefined): string {
if (inputValue === undefined) return ''

const percentage = Math.round(inputValue * 100 * 100000) / 100000 // fix any rounding errors
return `${percentage}%`
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export const ResourceData: React.FC<{ resource: ResourceAny }> = ({ resource })
<>
<DataRow label="Added" value={moment(resource.added).format(TIMESTAMP_FORMAT)} />
<DataRow label="Modified" value={moment(resource.modified).format(TIMESTAMP_FORMAT)} />
{resource.additionalInfo &&
resource.additionalInfo.map((info, index) => (
<DataRow key={index} label={info.label} value={info.value} />
))}
</>
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { GDDTypeDurationMs } from 'graphics-data-definition'
import React from 'react'
import { getEditPropertyMeta, PropertyProps } from '../lib'
import { EditProperty } from '../util'
import { DurationInput } from '../../../../inputs/DurationInput'

export const gddTypeIntegerMs: React.FC<PropertyProps<GDDTypeDurationMs>> = (props) => {
const data = props.data || ''
const { label, description } = getEditPropertyMeta(props)
return (
<EditProperty className="gdd-edit-data__gddType__integer-duration-ms" {...props}>
<DurationInput
label={label}
focusTooltip={description}
fullWidth
currentValue={data}
onChange={(v) => {
props.setData(v)
props.onSave()
}}
allowUndefined={true}
/>
</EditProperty>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { GDDTypeDurationMs } from 'graphics-data-definition'
import React from 'react'
import { getEditPropertyMeta, PropertyProps } from '../lib'
import { EditProperty } from '../util'
import { PercentageInput } from '../../../../inputs/PercentageInput'

export const gddTypePercentage: React.FC<PropertyProps<GDDTypeDurationMs>> = (props) => {
const data = props.data || ''
const { label, description } = getEditPropertyMeta(props)
return (
<EditProperty className="gdd-edit-data__gddType__integer-duration-ms" {...props}>
<PercentageInput
label={label}
focusTooltip={description}
fullWidth
currentValue={data}
onChange={(v) => {
props.setData(v)
props.onSave()
}}
allowUndefined={true}
/>
</EditProperty>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export const gddTypeColorRRGGBB: React.FC<PropertyProps<GDDTypeColorRRGGBB>> = (
}}
onBlur={props.onSave}
/>
<input
type="text"
value={data}
onChange={(e) => {
props.setData(e.target.value)
}}
onBlur={props.onSave}
/>
</WithLabel>
</EditProperty>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { GDDTypeColorRRGGBBAA } from 'graphics-data-definition'
import React, { useCallback, useEffect, useState } from 'react'
import { PropertyProps } from '../lib'
import { EditProperty, WithLabel } from '../util'

export const gddTypeColorRRGGBBAA: React.FC<PropertyProps<GDDTypeColorRRGGBBAA>> = (props) => {
return <RRGGBBAA {...props} />
}
export const RRGGBBAA: React.FC<PropertyProps<GDDTypeColorRRGGBBAA>> = (props) => {
const [rgbValue, setRgbValue] = useState(props.data || '')
const [aValue, setAValue] = useState(props.data || '')
const [rgbaValue, setRgbaValue] = useState(props.data || '')

const storedValue = props.data || ''
useEffect(() => {
let r = 0
let g = 0
let b = 0
let a = 0

const m = storedValue.match(/(\w\w)(\w\w)(\w\w)(\w\w)/)
if (m) {
r = parseInt(m[1], 16)
g = parseInt(m[2], 16)
b = parseInt(m[3], 16)
a = parseInt(m[4], 16)
} else {
const m = storedValue.match(/(\w\w)(\w\w)(\w\w)/)
if (m) {
r = parseInt(m[1], 16)
g = parseInt(m[2], 16)
b = parseInt(m[3], 16)
a = 255
}
}

const rgbStr =
'#' + r.toString(16).padStart(2, '0') + g.toString(16).padStart(2, '0') + b.toString(16).padStart(2, '0')
const aStr = a.toString(16).padStart(2, '0')

setRgbValue(rgbStr)
setAValue(aStr)
setRgbaValue(rgbStr + aStr)
}, [storedValue])

const setRGB = useCallback(
(rgb: string) => {
props.setData(rgb + aValue)
},
[props, aValue]
)
const setRGBA = useCallback((rgba: string) => {
setRgbaValue(rgba)
}, [])
const saveRGBA = useCallback(() => {
props.setData(rgbaValue)
props.onSave()
}, [props, rgbaValue])

return (
<EditProperty className="gdd-edit-data__gddType__string-color-rrggbbaa" {...props}>
<WithLabel {...props}>
<input
type="color"
value={rgbValue}
onChange={(e) => {
setRGB(e.target.value)
}}
onBlur={props.onSave}
/>
<input
type="text"
value={rgbaValue}
onChange={(e) => {
setRGBA(e.target.value)
}}
onBlur={saveRGBA}
/>
</WithLabel>
</EditProperty>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import { assertNever } from '@shared/lib'
import { gddTypeMultiLine } from './GDDTypes/string-multi-line'
import { gddTypeSelect } from './GDDTypes/select'
import { gddTypeColorRRGGBB } from './GDDTypes/string-color-rrggbb'
import { gddTypeColorRRGGBBAA } from './GDDTypes/string-color-rrggbbaa'
import { gddTypeIntegerMs } from './GDDTypes/integer-duration-ms'
import { gddTypePercentage } from './GDDTypes/number-percentage'

export const componentAny: React.FC<ComponentAnyProps<GDDSchemaProperty>> = (props) => {
const schema = props.schema
Expand All @@ -34,17 +37,18 @@ export const componentAny: React.FC<ComponentAnyProps<GDDSchemaProperty>> = (pro
else if (gddSchema.gddType === 'multi-line') return gddTypeMultiLine(innerProps)
else if (gddSchema.gddType === 'select') return gddTypeSelect(innerProps)
else if (gddSchema.gddType === 'color-rrggbb') return gddTypeColorRRGGBB(innerProps)
else if (gddSchema.gddType === 'color-rrggbbaa') return gddTypeColorRRGGBBAA(innerProps)
// else if (gddSchema.gddType === 'file-path') return gddTypeFilePath(innerProps)
// else if (gddSchema.gddType === 'file-path/image-path') return gddTypeFilePathImagePath(innerProps)
// else assertNever(gddSchema)
} else if (gddSchema.type === 'integer') {
if (gddSchema.gddType === 'select') return gddTypeSelect(innerProps)
// else assertNever(gddSchema)
else if (gddSchema.gddType === 'duration-ms') return gddTypeIntegerMs(innerProps)
else assertNever(gddSchema)
} else if (gddSchema.type === 'number') {
if (gddSchema.gddType === 'select') return gddTypeSelect(innerProps)
// else if (gddSchema.gddType === 'duration-ms') return gddTypeDurationMS(innerProps)
// else if (gddSchema.gddType === 'percentage') return gddTypePercentage(innerProps)
// else assertNever(gddSchema)
else if (gddSchema.gddType === 'percentage') return gddTypePercentage(innerProps)
else assertNever(gddSchema)
} else {
assertNever(gddSchema)
}
Expand Down
Loading

0 comments on commit d946c76

Please sign in to comment.