Skip to content

Commit

Permalink
feat: use "nullish" option values when multiple blocks selected
Browse files Browse the repository at this point in the history
In cases where the options of all selected blocks are not the same
  • Loading branch information
dulnan committed Nov 12, 2024
1 parent 95fae34 commit c665390
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 52 deletions.
4 changes: 2 additions & 2 deletions css/partials/options.css
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@
@apply h-50;

&.bk-is-grouped {
@apply h-auto;
@apply h-auto min-h-50;

.bk-blokkli-item-options-checkbox {
@apply h-50 w-full;
Expand All @@ -395,7 +395,7 @@
}

.bk-blokkli-item-options-item {
@apply block;
@apply flex flex-col items-start;
&:not(:last-child) {
@apply border-b border-b-mono-600;
}
Expand Down
8 changes: 0 additions & 8 deletions playground/components/Blokkli/Card/Card.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ const { parentType, options } = defineBlokkli({
label: 'Box',
default: true,
},
easyLanguageVisibility: {
type: 'checkbox',
label: 'Box',
default: true,
},
color: {
type: 'radios',
label: 'Color',
Expand All @@ -77,9 +72,6 @@ const { parentType, options } = defineBlokkli({
addBehaviour: 'no-form',
editTitle: (el) => el.querySelector('h3')?.textContent,
determineVisibleOptions: (ctx) => {
if (ctx.entity.language === 'ls') {
return ['easyLanguageVisibility']
}
if (ctx.props.icon) {
return ['box', 'color']
}
Expand Down
7 changes: 7 additions & 0 deletions playground/components/Blokkli/Widget/Widget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ const { options } = defineBlokkli({
primary: { hex: '#0550e6', label: 'Blue' },
},
},
nestedCheckbox: {
type: 'checkbox',
label: 'Nested Checkbox',
default: true,
group: 'Radios',
},
},
editor: {
editTitle: (el) => el.dataset.widget,
Expand All @@ -230,6 +236,7 @@ const { options } = defineBlokkli({
'paddingRight',
'paddingBottom',
'rows',
'nestedCheckbox',
]
}
return ['showAllOptions']
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/components/Edit/DraggableList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const props = withDefaults(
fieldKey: string
list: FieldListItem[]
entity: EntityContext
language: string
language?: string
tag?: string
isNested: boolean
fieldListType: string
Expand All @@ -123,6 +123,7 @@ const props = withDefaults(
tag: 'div',
allowedFragments: undefined,
dropAlignment: undefined,
language: undefined,
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { $t, state } = useBlokkli()
const props = defineProps<{
label: string
property: string
modelValue: string
modelValue?: string
}>()
const emit = defineEmits(['update:modelValue'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const { $t, state } = useBlokkli()
const props = defineProps<{
label: string
property: string
modelValue: string
modelValue?: string
options: { value: string; label: string }[]
isGrouped?: boolean
}>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { computed } from '#imports'
const props = defineProps<{
label: string
modelValue: string
modelValue?: string
}>()
const emit = defineEmits(['update:modelValue'])
Expand Down
31 changes: 5 additions & 26 deletions src/runtime/components/Edit/Features/Options/Form/Item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const emit = defineEmits<{
const props = defineProps<{
option: BlockOptionDefinition
property: string
mutatedValue: any
uuids: string[]
isGrouped?: boolean
}>()
Expand Down Expand Up @@ -169,34 +170,12 @@ const validateValue = (
}
}
const mutatedValue = computed<string | undefined>(() => {
for (let i = 0; i < props.uuids.length; i++) {
const uuid = props.uuids[i]
const mutatedOptions = state.mutatedOptions[uuid]
if (mutatedOptions) {
const mutatedOption = state.mutatedOptions[uuid]?.[props.property]
if (mutatedOption !== undefined) {
return validateValue(mutatedOption)
}
}
}
return undefined
})
const defaultValue = computed<string>(
() => validateValue(props.option.default) || '',
)
const value = computed<string>({
const value = computed<string | undefined>({
get() {
if (mutatedValue.value === undefined) {
return defaultValue.value
}
return mutatedValue.value
return validateValue(props.mutatedValue)
},
set(value: string) {
emit('update', value)
set(value: string | undefined) {
emit('update', value === undefined ? '' : value)
},
})
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import { Icon } from '#blokkli/components'
const props = withDefaults(
defineProps<{
label: string
modelValue: string
modelValue?: string
min: number
max: number
type?: string
}>(),
{
type: 'text',
modelValue: '0',
},
)
Expand All @@ -42,14 +43,17 @@ const emit = defineEmits(['update:modelValue'])
const text = computed<string>({
get() {
return props.modelValue || '0'
return props.modelValue
},
set(v: string | number | undefined) {
emit('update:modelValue', (v === undefined ? '' : v).toString())
},
})
const numeric = computed(() => {
if (props.modelValue === undefined) {
return 0
}
const v = Number.parseInt(props.modelValue)
if (Number.isNaN(v)) {
return 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const props = defineProps<{
property: string
displayAs?: 'radios' | 'colors' | 'grid' | 'icons'
options: Record<string, PossibleOptionType>
modelValue: string
modelValue?: string
}>()
const emit = defineEmits(['update:modelValue'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { computed } from '#imports'
const props = defineProps<{
label: string
modelValue: string
modelValue?: string
min: number
max: number
step: number
Expand All @@ -20,7 +20,7 @@ const emit = defineEmits(['update:modelValue'])
const text = computed<string>({
get() {
return props.modelValue || ''
return props.modelValue || '0'
},
set(v: string | number | undefined) {
emit('update:modelValue', (v === undefined ? '' : v).toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { computed } from '#imports'
const props = withDefaults(
defineProps<{
label: string
modelValue: string
modelValue?: string
type?: string
}>(),
{
type: 'text',
modelValue: '',
},
)
Expand Down
29 changes: 23 additions & 6 deletions src/runtime/components/Edit/Features/Options/Form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:key="plugin.property"
:option="plugin.option"
:property="plugin.property"
:mutated-value="currentValues[plugin.property]"
:uuids="uuids"
class="bk-blokkli-item-options-item"
:class="{
Expand All @@ -31,6 +32,7 @@
:key="plugin.property"
:option="plugin.option"
:property="plugin.property"
:mutated-value="currentValues[plugin.property]"
:uuids="uuids"
class="bk-blokkli-item-options-item"
:class="{
Expand All @@ -47,7 +49,7 @@
<script lang="ts" setup>
import { ref, computed, useBlokkli, onBeforeUnmount, onMounted } from '#imports'
import { globalOptions } from '#blokkli/definitions'
import { falsy } from '#blokkli/helpers'
import { falsy, onlyUnique } from '#blokkli/helpers'
import OptionsFormItem from './Item.vue'
import OptionsFormGroup from './Group.vue'
import type {
Expand Down Expand Up @@ -220,14 +222,29 @@ function getOptionValue(
* The current mapped values, same as provided by defineBlokkli.
*/
const currentValues = computed(() => {
const uuid = props.uuids[0]
return availableOptions.value.reduce<
Record<string, string | string[] | boolean | number>
>((acc, v) => {
acc[v.property] = getRuntimeOptionValue(
v.option,
getOptionValue(uuid, v.property, v.option.default),
)
// Get all current values.
const values = props.uuids
.map((uuid) => {
return JSON.stringify(
getRuntimeOptionValue(
v.option,
getOptionValue(uuid, v.property, v.option.default),
),
)
})
.filter(onlyUnique)
if (values.length === 1) {
acc[v.property] = getRuntimeOptionValue(
v.option,
getOptionValue(props.uuids[0], v.property, v.option.default),
)
} else {
acc[v.property] = ''
}
return acc
}, {})
})
Expand Down

0 comments on commit c665390

Please sign in to comment.