2021-03-07 17:47:28 +00:00
|
|
|
import {
|
2021-03-08 15:29:35 +00:00
|
|
|
ref,
|
2021-03-07 17:47:28 +00:00
|
|
|
Ref,
|
|
|
|
reactive,
|
|
|
|
inject,
|
|
|
|
provide,
|
|
|
|
onUnmounted,
|
|
|
|
} from 'vue';
|
|
|
|
import {
|
2022-03-11 16:50:50 +00:00
|
|
|
ITreeNode,
|
|
|
|
IBlockData,
|
2021-03-08 15:29:35 +00:00
|
|
|
} from './types';
|
2022-09-05 19:12:20 +00:00
|
|
|
import { useDynamicBlocks } from './use-dynamic-blocks';
|
|
|
|
import { SbMode } from './mode';
|
2021-03-07 17:47:28 +00:00
|
|
|
|
2021-03-08 15:29:35 +00:00
|
|
|
export const SymBlockTree= Symbol('Schlechtenburg block tree');
|
|
|
|
export const SymBlockTreeRegister = Symbol('Schlechtenburg block tree register');
|
|
|
|
export const SymBlockTreeUnregister = Symbol('Schlechtenburg block tree unregister');
|
2021-03-08 16:45:21 +00:00
|
|
|
|
2021-03-07 17:47:28 +00:00
|
|
|
export function useBlockTree() {
|
2022-03-11 16:50:50 +00:00
|
|
|
const blockTree: Ref<ITreeNode|null> = inject(SymBlockTree, ref(null));
|
2022-09-05 19:12:20 +00:00
|
|
|
const registerWithParent = inject(SymBlockTreeRegister, (_b: ITreeNode, _i: number) => {});
|
|
|
|
const unregisterWithParent = inject(SymBlockTreeUnregister, (_b: ITreeNode) => {});
|
2021-03-07 17:47:28 +00:00
|
|
|
|
2022-03-11 16:50:50 +00:00
|
|
|
const self: ITreeNode = reactive({
|
2021-03-07 17:47:28 +00:00
|
|
|
id: '',
|
|
|
|
name: '',
|
|
|
|
icon: '',
|
|
|
|
children: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Provide a registration function to child blocks
|
2022-09-05 19:12:20 +00:00
|
|
|
provide(SymBlockTreeRegister, (block: ITreeNode, index:number = -1) => {
|
2022-03-11 16:50:50 +00:00
|
|
|
if (self.children.find((child: ITreeNode) => child.id === block.id)) {
|
2021-03-07 17:47:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:12:20 +00:00
|
|
|
if (index < 0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const normalizedIndex = index < 0 ? 0 : index;
|
|
|
|
|
|
|
|
self.children =[
|
|
|
|
...self.children.slice(0, normalizedIndex),
|
2021-03-07 17:47:28 +00:00
|
|
|
block,
|
2022-09-05 19:12:20 +00:00
|
|
|
...self.children.slice(normalizedIndex),
|
2021-03-07 17:47:28 +00:00
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
// Provide an unregistration function to child blocks
|
2022-03-11 16:50:50 +00:00
|
|
|
provide(SymBlockTreeUnregister, ({ id }: ITreeNode) => {
|
|
|
|
self.children = self.children.filter((child: ITreeNode) => child.id !== id);
|
2021-03-07 17:47:28 +00:00
|
|
|
});
|
|
|
|
|
2022-09-05 19:12:20 +00:00
|
|
|
const { mode } = useDynamicBlocks();
|
|
|
|
|
|
|
|
const register = (block: IBlockData<any>, index: number = 0) => {
|
2021-03-07 17:47:28 +00:00
|
|
|
if (!block.id) {
|
|
|
|
throw new Error(`Cannot register a block without an id: ${JSON.stringify(block)}`);
|
|
|
|
}
|
|
|
|
|
2022-09-05 19:12:20 +00:00
|
|
|
if (mode.value !== SbMode.Edit) {
|
|
|
|
console.warn('Ignoring block tree registration requests outside of edit mode.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-07 17:47:28 +00:00
|
|
|
self.id = block.id;
|
|
|
|
self.name = block.name;
|
|
|
|
|
|
|
|
// Register ourselves at the parent block
|
2022-09-05 19:12:20 +00:00
|
|
|
registerWithParent(self, index);
|
2021-03-07 17:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unregister from parent when we get destroyed
|
|
|
|
onUnmounted(() => {
|
|
|
|
if (self.id) {
|
|
|
|
unregisterWithParent(self);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
blockTree,
|
|
|
|
register,
|
|
|
|
};
|
|
|
|
}
|