schlechtenburg/src/components/TreeElement.ts

131 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-05-20 14:21:08 +00:00
import {
2020-05-24 15:33:25 +00:00
Ref,
ref,
inject,
reactive,
computed,
2020-05-28 20:16:35 +00:00
watch,
provide,
2020-05-20 14:21:08 +00:00
} from '@vue/composition-api';
2020-05-24 15:33:25 +00:00
export interface BlockDefinition {
2020-05-20 17:12:51 +00:00
name: string;
2020-05-24 20:00:14 +00:00
getDefaultData: any;
2020-05-24 15:33:25 +00:00
edit: () => Promise<any>;
display: () => Promise<any>;
}
export interface BlockLibraryDefinition {
[name: string]: BlockDefinition;
}
2020-05-27 15:06:14 +00:00
export interface BlockData { [name: string]: any }
export interface Block {
2020-05-24 15:33:25 +00:00
name: string;
2020-05-25 21:10:21 +00:00
blockId: string;
2020-05-27 15:06:14 +00:00
data: BlockData;
2020-05-20 14:21:08 +00:00
}
2020-05-24 15:33:25 +00:00
export interface BlockProps {
2020-05-25 21:10:21 +00:00
blockId: string;
2020-05-20 14:21:08 +00:00
data: { [key: string]: any};
2020-05-24 15:33:25 +00:00
}
2020-05-20 14:21:08 +00:00
export const model = {
2020-05-20 17:12:51 +00:00
prop: 'block',
2020-05-24 15:33:25 +00:00
event: 'update',
2020-05-20 14:21:08 +00:00
};
2020-05-20 17:12:51 +00:00
export const blockProps = {
2020-05-25 21:10:21 +00:00
blockId: { type: String, required: true },
2020-05-20 14:21:08 +00:00
data: { type: Object, default: () => ({}) },
};
2020-05-27 13:57:57 +00:00
export enum SbMode {
Edit = 'edit',
Display = 'display',
}
export const Mode = Symbol('Schlechtenburg mode');
export const BlockLibrary = Symbol('Schlechtenburg block library');
2020-05-24 15:33:25 +00:00
export function useDynamicBlocks() {
2020-05-27 13:57:57 +00:00
const mode = inject(Mode, ref(SbMode.Edit));
2020-05-24 15:33:25 +00:00
const customBlocks: BlockLibraryDefinition = inject(BlockLibrary, reactive({}));
2020-05-27 13:57:57 +00:00
const getBlock = (name: string) => customBlocks[name][mode.value];
2020-05-20 14:21:08 +00:00
2020-05-27 13:57:57 +00:00
return {
mode,
customBlocks,
getBlock,
};
2020-05-24 15:33:25 +00:00
}
2020-05-20 14:21:08 +00:00
2020-05-28 20:16:35 +00:00
interface BlockRect {
height: number;
width: number;
left: number;
top: number;
}
export const BlockDimensions = Symbol('Schlechtenburg block dimensions');
export const EditorDimensions = Symbol('Schlechtenburg editor dimensions');
export function useResizeObserver(el: Ref<null|HTMLElement>, symbol: symbol) {
const dimensions: Ref<null|BlockRect> = ref(null);
provide(symbol, dimensions);
const triggerSizeCalculation = () => {
if (!el.value) {
return;
}
const clientRect = el.value.getBoundingClientRect();
dimensions.value = {
width: clientRect.width,
height: clientRect.height,
left: el.value.offsetLeft,
top: el.value.offsetTop,
};
};
const resizeObserver = new ResizeObserver(triggerSizeCalculation);
const mutationObserver = new MutationObserver(triggerSizeCalculation);
watch(el, () => {
if (!el.value) {
return;
}
resizeObserver.observe(el.value);
mutationObserver.observe(el.value, { attributes: true, childList: false, subtree: false });
});
return { triggerSizeCalculation, dimensions };
}
export function useBlockSizing() {
const editorDimensions: Ref<BlockRect|null> = inject(EditorDimensions, ref(null));
const blockDimensions: Ref<BlockRect|null> = inject(BlockDimensions, ref(null));
return { editorDimensions, blockDimensions };
}
2020-05-27 13:57:57 +00:00
export const ActiveBlock = Symbol('Schlechtenburg active block');
2020-05-25 21:10:21 +00:00
export function useActivation(currentBlockId: string) {
const activeBlockId: Ref<string|null> = inject(ActiveBlock, ref(null));
2020-05-24 15:33:25 +00:00
const isActive = computed(() => activeBlockId.value === currentBlockId);
2020-05-25 21:10:21 +00:00
const activate = (blockId?: string|null) => {
2020-05-24 15:33:25 +00:00
activeBlockId.value = blockId !== undefined ? blockId : currentBlockId;
};
2020-05-24 20:39:14 +00:00
const requestActivation = () => {
if (activeBlockId.value) {
return;
}
activate();
};
2020-05-24 15:33:25 +00:00
return {
isActive,
activate,
2020-05-24 20:39:14 +00:00
requestActivation,
2020-05-24 15:33:25 +00:00
};
2020-05-20 14:21:08 +00:00
}