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

101 lines
2.4 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-12-27 21:32:43 +00:00
shallowReactive,
2020-05-24 15:33:25 +00:00
ref,
PropType,
2020-05-28 20:16:35 +00:00
Ref,
2020-12-27 21:32:43 +00:00
} from 'vue';
2020-05-24 15:33:25 +00:00
import {
2021-03-08 15:29:35 +00:00
BlockData,
2020-05-24 15:33:25 +00:00
BlockDefinition,
2021-03-08 15:29:35 +00:00
BlockLibrary,
TreeNode,
} from '../types';
import { model } from '../block-helpers';
2020-12-30 13:34:23 +00:00
import { Mode, SbMode } from '../mode';
2021-03-08 15:29:35 +00:00
import { SymBlockLibrary} from '../use-dynamic-blocks';
import {
SymBlockTree,
SymBlockTreeRegister,
SymBlockTreeUnregister,
} from '../use-block-tree';
import { SymEditorDimensions, useResizeObserver } from '../use-resize-observer';
import { SymActiveBlock } from '../use-activation';
2020-05-24 15:33:25 +00:00
2021-02-22 18:13:37 +00:00
import { SbMainMenu } from './MainMenu';
2021-03-07 21:30:22 +00:00
import { SbBlockToolbar } from './BlockToolbar';
2020-12-30 20:17:34 +00:00
import { SbBlock } from './Block';
2020-05-24 20:00:14 +00:00
2020-05-27 13:57:57 +00:00
import './Schlechtenburg.scss';
2021-03-08 15:29:35 +00:00
export const SbMain = defineComponent({
name: 'sb-main',
2020-05-20 14:21:08 +00:00
model,
props: {
2021-03-08 15:29:35 +00:00
customBlocks: {
type: Array as PropType<BlockDefinition<any>[]>,
default: () => [],
},
block: {
type: Object as PropType<BlockData<any>>,
required: true,
},
2021-02-22 18:13:37 +00:00
onUpdate: { type: Function, default: () => {} },
2020-05-27 13:57:57 +00:00
mode: {
2020-12-30 01:32:46 +00:00
type: String as PropType<SbMode>,
validator(value: any) {
2020-12-27 21:32:43 +00:00
return Object.values(SbMode).includes(value);
2020-05-27 13:57:57 +00:00
},
2020-12-27 21:32:43 +00:00
default: SbMode.Edit,
2020-05-27 13:57:57 +00:00
},
2020-05-20 14:21:08 +00:00
},
2021-03-08 15:30:56 +00:00
setup(props: any) { // TODO: why does the typing of props not work here?
2020-05-28 20:16:35 +00:00
const el: Ref<null|HTMLElement> = ref(null);
2021-03-08 15:29:35 +00:00
useResizeObserver(el, SymEditorDimensions);
2020-05-28 20:16:35 +00:00
2020-05-27 13:57:57 +00:00
const mode = ref(props.mode);
provide(Mode, mode);
2020-05-24 15:33:25 +00:00
const activeBlock = ref(null);
2021-03-08 15:29:35 +00:00
provide(SymActiveBlock, activeBlock);
2020-05-20 14:21:08 +00:00
2021-03-08 15:29:35 +00:00
const blockTree: Ref<TreeNode|null> = ref(null);
provide(SymBlockTree, blockTree);
provide(SymBlockTreeRegister, (block: TreeNode) => { blockTree.value = block; });
provide(SymBlockTreeUnregister, () => { blockTree.value = null; });
2021-03-07 17:47:28 +00:00
2021-03-08 15:29:35 +00:00
const blockLibrary: BlockLibrary = shallowReactive({
2020-05-24 15:33:25 +00:00
...props.customBlocks.reduce(
2021-03-08 15:29:35 +00:00
(blocks: BlockLibrary, block: BlockDefinition<any>) => ({ ...blocks, [block.name]: block }),
2020-05-24 15:33:25 +00:00
{},
),
});
2020-12-27 21:32:43 +00:00
2021-03-08 15:29:35 +00:00
provide(SymBlockLibrary, blockLibrary);
2020-05-20 14:21:08 +00:00
2020-05-25 21:10:21 +00:00
return () => (
2020-05-28 20:16:35 +00:00
<div
class="sb-main"
ref={el}
>
2021-02-22 23:12:06 +00:00
{
mode.value === SbMode.Edit
2021-03-07 21:30:22 +00:00
? <>
<SbMainMenu block={props.block} />
2021-03-08 15:29:35 +00:00
<SbBlockToolbar />
2021-03-07 21:30:22 +00:00
</>
2021-02-22 23:12:06 +00:00
: null
}
2020-05-27 13:57:57 +00:00
<SbBlock
block={props.block}
2021-02-22 18:13:37 +00:00
onUpdate={props.onUpdate}
2020-05-27 13:57:57 +00:00
/>
</div>
2020-05-20 14:21:08 +00:00
);
},
});