schlechtenburg/src/components/Schlechtenburg.tsx

71 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-05-20 14:21:08 +00:00
import {
defineComponent,
2020-05-24 15:33:25 +00:00
provide,
2020-05-20 14:21:08 +00:00
reactive,
2020-05-24 15:33:25 +00:00
ref,
PropType,
2020-05-20 14:21:08 +00:00
} from '@vue/composition-api';
2020-05-24 15:33:25 +00:00
import {
model,
ActiveBlock,
2020-05-25 21:10:21 +00:00
BlockData,
2020-05-24 15:33:25 +00:00
BlockDefinition,
BlockLibraryDefinition,
BlockLibrary,
} from '@components/TreeElement';
import SbBlock from '@internal/Block';
2020-05-20 14:21:08 +00:00
2020-05-24 20:00:14 +00:00
import SbLayout from '@user/Layout/index';
import SbParagraph from '@user/Paragraph/index';
import SbImage from '@user/Image/index';
import SbHeading from '@user/Heading/index';
2020-05-25 21:10:21 +00:00
export interface SchlechtenburgProps {
customBlocks: BlockDefinition[];
block: BlockData;
}
2020-05-20 14:21:08 +00:00
export default defineComponent({
name: 'schlechtenburg-main',
model,
props: {
2020-05-24 15:33:25 +00:00
customBlocks: { type: (null as unknown) as PropType<BlockDefinition[]>, default: () => [] },
2020-05-25 21:10:21 +00:00
block: { type: (null as unknown) as PropType<BlockData>, required: true },
2020-05-20 14:21:08 +00:00
},
2020-05-25 21:10:21 +00:00
setup(props, context) {
2020-05-24 15:33:25 +00:00
const activeBlock = ref(null);
provide(ActiveBlock, activeBlock);
2020-05-20 14:21:08 +00:00
2020-05-24 15:33:25 +00:00
const blockLibrary: BlockLibraryDefinition = reactive({
2020-05-24 20:00:14 +00:00
'sb-layout': SbLayout,
'sb-image': SbImage,
'sb-paragraph': SbParagraph,
'sb-heading': SbHeading,
2020-05-24 15:33:25 +00:00
...props.customBlocks.reduce(
(
2020-05-25 21:10:21 +00:00
blocks,
block,
2020-05-24 15:33:25 +00:00
) => ({ ...blocks, [block.name]: block }),
{},
),
});
provide(BlockLibrary, blockLibrary);
2020-05-20 14:21:08 +00:00
2020-05-25 21:10:21 +00:00
return () => (
2020-05-24 15:33:25 +00:00
<SbBlock
2020-05-20 14:21:08 +00:00
class="sb-main"
2020-05-25 21:10:21 +00:00
block={props.block}
2020-05-20 14:21:08 +00:00
{...{
on: {
2020-05-25 21:10:21 +00:00
update: (block: BlockDefinition) => context.emit('update', block),
2020-05-20 14:21:08 +00:00
},
}}
/>
);
},
});