import { computed, defineComponent, onMounted, type PropType, ref, TransitionGroup, } from 'vue'; import { ColorPicker } from 'vue3-colorpicker'; import { DefaultComponentProps } from '../_default'; import { BCMSButton, BCMSIcon } from '..'; import { useTranslation } from '../../translations'; import InputWrapper from './_input'; const component = defineComponent({ components: { ColorPicker, }, props: { ...DefaultComponentProps, value: { type: String, required: true, }, invalidText: String, placeholder: String, label: String, helperText: String, view: { type: String as PropType<'prop' | 'entry'>, default: 'prop', }, allowCustom: Boolean, allowGlobal: { type: Boolean, default: true, }, allowCustomForce: Boolean, allowCreateColor: Boolean, propPath: String, }, emits: { change: (_value: string) => { return true; }, }, setup(props, ctx) { const translations = useTranslation(); const store = window.bcms.vue.store; const throwable = window.bcms.util.throwable; const selectedColor = ref({ id: '', value: '#249681', }); const hexColorBuffer = ref(selectedColor.value.value.slice(1)); const colors = computed(() => { return store.getters.color_find((e) => e.global); }); const colorWheelVisible = ref(false); async function createColor() { if (colors.value.find((e) => e.value === selectedColor.value.value)) { window.bcms.notification.warning( translations.input.color.error.duplicateValue ); return; } await throwable(async () => { return await window.bcms.sdk.color.create({ label: selectedColor.value.value, value: selectedColor.value.value, global: true, }); }); } async function deleteColor(id: string) { await throwable(async () => { return await window.bcms.sdk.color.deleteById(id); }); } onMounted(async () => { await window.bcms.util.throwable(async () => { await window.bcms.sdk.color.getAll(); }); if (props.value.startsWith('#')) { selectedColor.value = { id: '', value: props.value, }; } else { const color = store.getters.color_findOne((e) => e._id === props.value); if (color) { selectedColor.value = { id: color._id, value: color.value, }; } } if (props.view === 'entry' && props.value) { colorWheelVisible.value = true; } }); return () => (
{props.allowCustom && (
{props.allowCustomForce || colorWheelVisible.value ? ( <> { if (value !== selectedColor.value.value) { selectedColor.value = { id: '', value, }; hexColorBuffer.value = selectedColor.value.value.slice(1); ctx.emit('change', value); } }} />
# { const target = event.target as HTMLInputElement; target.value = target.value.replace( /[^0-9a-f]+/g, '' ); if (target.value.length > 6) { target.value = target.value.substring( target.value.length - 6 ); } if ( target.value.length === 6 && target.value !== hexColorBuffer.value && window.bcms.util.color.check(target.value) ) { hexColorBuffer.value = target.value; selectedColor.value = { id: '', value: `#${hexColorBuffer.value}`, }; } }} // onKeypress={(event) => { // const target = event.target as HTMLInputElement; // if ( // event.key === 'Enter' && // target.value.length === 6 && // window.bcms.util.color.check(target.value) // ) { // createColor(); // } // }} />
{props.allowCreateColor && (
)}
) : ( props.view === 'entry' && ( { colorWheelVisible.value = true; }} > {translations.input.color.actions.chooseOtherColor} ) )}
)} {props.allowGlobal && (
{colors.value.length === 0 ? (
{translations.input.color.actions.addColors}
) : (
{colors.value.map((color, index) => { return ( ); })}
)}
)}
); }, }); export default component;