schlechtenburg/packages/standalone/lib/get-wrapper.tsx

87 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-03-20 13:49:44 +00:00
import {
defineComponent,
ref,
2022-03-21 00:53:25 +00:00
Ref,
2022-03-20 13:49:44 +00:00
PropType,
} from 'vue'
import {
IBlockDefinition,
IBlockData,
SbMain,
SbMode,
OnUpdateBlockCb,
2022-03-21 00:53:25 +00:00
ISbMainProps,
2022-03-20 13:49:44 +00:00
} from '@schlechtenburg/core';
/**
*
*/
2022-03-21 00:53:25 +00:00
export default function getWrapper() {
const refBlock: Ref<IBlockData<any>|null> = ref(null);
const refMode = ref(SbMode.View);
const SchlechtenburgWrapper = defineComponent({
2022-03-20 13:49:44 +00:00
name: 'SchlechtenburgWrapper',
props: {
availableBlocks: {
type: Array as PropType<IBlockDefinition<any>[]>,
default: () => [],
},
block: {
type: Object as PropType<IBlockData<any>>,
required: true,
},
/**
* Called when the block should be updated.
*/
onUpdate: {
type: (null as unknown) as PropType<OnUpdateBlockCb>,
default: () => {},
},
mode: {
type: String as PropType<SbMode>,
validator(value: any) {
return Object.values(SbMode).includes(value);
},
default: SbMode.Edit,
},
},
2022-03-21 00:53:25 +00:00
setup(props: ISbMainProps) {
refBlock.value = { ...(props.block) };
refMode.value = props.mode;
if (!refBlock.value) {
return () => <div class="sb-message sb-message_error">An Error occurred</div>;
}
2022-03-20 13:49:44 +00:00
return () => <SbMain
2022-03-21 00:53:25 +00:00
block={refBlock.value as IBlockData<any>}
2022-03-20 13:49:44 +00:00
availableBlocks={props.availableBlocks}
2022-03-21 00:53:25 +00:00
mode={refMode.value}
onUpdate={(newBlock: IBlockData<any>) => {
refBlock.value = { ...newBlock };
}}
2022-03-20 13:49:44 +00:00
/>
}
});
2022-03-21 00:53:25 +00:00
return {
SchlechtenburgWrapper,
getBlock() {
return refBlock.value;
},
setBlock(block: IBlockData<any>) {
refBlock.value = { ...block };
},
getMode() {
return refMode.value;
},
setMode(mode: SbMode) {
refMode.value = mode;
},
};
2022-03-20 13:49:44 +00:00
}