schlechtenburg/packages/core/lib/components/BlockPicker.tsx

68 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-05-27 13:57:57 +00:00
import {
computed,
ref,
2020-12-27 21:32:43 +00:00
defineComponent,
} from 'vue';
2021-03-08 15:29:35 +00:00
import { useDynamicBlocks } from '../use-dynamic-blocks';
import { BlockDefinition } from '../types';
2020-05-24 20:00:14 +00:00
2020-12-30 20:17:34 +00:00
import { SbButton } from './Button';
import { SbModal } from './Modal';
2020-05-27 13:57:57 +00:00
2020-05-24 20:00:14 +00:00
import './BlockPicker.scss';
2020-05-20 14:21:08 +00:00
2020-12-30 13:34:23 +00:00
export const SbBlockPicker = defineComponent({
2020-05-25 21:10:21 +00:00
name: 'sb-block-picker',
2021-03-08 15:29:35 +00:00
props: {
onPickedBlock: { type: Function, default: () => {} },
},
2020-05-20 14:21:08 +00:00
2021-03-08 15:29:35 +00:00
setup(props) {
2020-05-27 13:57:57 +00:00
const open = ref(false);
2020-05-24 20:00:14 +00:00
const { customBlocks } = useDynamicBlocks();
const blockList = computed(() => Object.keys(customBlocks).map((key) => customBlocks[key]));
2020-05-20 14:21:08 +00:00
2021-03-08 15:29:35 +00:00
const selectBlock = (block: BlockDefinition<any>) => () => {
2020-05-27 13:57:57 +00:00
open.value = false;
2021-03-08 15:29:35 +00:00
props.onPickedBlock({
2020-05-27 13:57:57 +00:00
name: block.name,
2021-03-07 17:47:28 +00:00
id: `${+(new Date())}`,
2020-05-27 13:57:57 +00:00
data: block.getDefaultData(),
});
};
2020-05-25 21:10:21 +00:00
return () => (
2020-05-27 18:36:46 +00:00
<div class="sb-block-picker">
2020-05-27 13:57:57 +00:00
<SbButton
2020-05-27 18:36:46 +00:00
class="sb-block-picker__add-button"
2021-03-08 15:29:35 +00:00
{...{
type: 'button',
onClick: ($event: MouseEvent) => {
open.value = true;
$event.stopPropagation();
},
2020-05-27 13:57:57 +00:00
}}
2021-03-07 21:30:22 +00:00
>+</SbButton>
2020-05-27 13:57:57 +00:00
<SbModal
open={open.value}
2021-02-22 18:13:37 +00:00
onClose={() => {
2020-05-27 13:57:57 +00:00
open.value = false;
}}
2021-03-08 15:29:35 +00:00
{...{ onClick: ($event: MouseEvent) => $event.stopPropagation() }}
2020-05-27 13:57:57 +00:00
>
2021-03-08 15:29:35 +00:00
{...blockList.value.map((block: BlockDefinition<any>) => (
2020-05-27 13:57:57 +00:00
<SbButton
2021-03-08 15:29:35 +00:00
{...{
type: 'button',
onClick: () => selectBlock(block),
}}
2020-05-27 13:57:57 +00:00
>{block.name}</SbButton>
))}
</SbModal>
2020-05-20 14:21:08 +00:00
</div>
);
},
});