Updated some stuff

This commit is contained in:
Benjamin Bädorf 2020-12-30 02:32:46 +01:00
parent 8f42a92dbc
commit 945e851a9f
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C
31 changed files with 403 additions and 386 deletions

View file

@ -1,8 +1,9 @@
import { defineComponent, reactive, ref } from 'vue'; import { defineComponent, reactive, ref } from 'vue';
import Schlechtenburg from '/@components/Schlechtenburg'; import Schlechtenburg from '/@components/Schlechtenburg';
import { Block, SbMode } from '/@components/TreeElement'; import { Block } from '/@/blocks';
import { SbMode } from '/@/mode';
import initialData from './initial-data.json'; import initialData from './initial-data';
import './App.scss'; import './App.scss';
@ -11,38 +12,40 @@ export default defineComponent({
setup() { setup() {
const activeTab = ref('edit'); const activeTab = ref('edit');
const block = reactive(initialData) as Block; const block: Block<any> = reactive(initialData);
return () => ( return () => (
<div id="app"> <div id="app">
<select <select
value={activeTab.value} value={activeTab.value}
onchange={($event: Event) => { activeTab.value = ($event.target as HTMLSelectElement).value; }} onChange={($event: Event) => { activeTab.value = ($event.target as HTMLSelectElement).value; }}
> >
<option>edit</option> <option>edit</option>
<option>display</option> <option>display</option>
<option>json</option> <option>data</option>
</select> </select>
<Schlechtenburg {(() => {
v-show={activeTab.value === 'edit'} switch (activeTab.value) {
case SbMode.Edit:
return <Schlechtenburg
block={block} block={block}
eventUpdate={(newBlock: Block) => { eventUpdate={(newBlock: Block<any>) => {
block.name = newBlock.name;
block.blockId = newBlock.blockId;
block.data = newBlock.data; block.data = newBlock.data;
}} }}
/> key="edit"
mode="edit"
<Schlechtenburg />;
v-show={activeTab.value === 'display'} case SbMode.Edit:
return <Schlechtenburg
block={block} block={block}
mode={SbMode.Display} key="display"
/> mode="display"
/>;
<pre v-show={activeTab.value === 'json'}> case 'data':
<code>{JSON.stringify(block, null, 2)}</code> return <pre><code>{JSON.stringify(block, null, 2)}</code></pre>;
</pre> }
})()}
</div> </div>
); );
}, },

View file

@ -0,0 +1,34 @@
import { Component } from 'vue';
export interface BlockDefinition {
name: string;
getDefaultData: any;
edit: Component;
display: Component;
}
export interface BlockLibraryDefinition {
[name: string]: BlockDefinition;
}
export interface BlockProps<T> {
blockId: string;
data: T;
}
export interface Block<T> extends BlockProps<T> {
name: string;
}
export const model = {
prop: 'block',
event: 'update',
};
export const blockProps = {
blockId: {
type: String,
default: () => `${+(new Date())}`,
},
data: { type: Object, default: () => ({}) },
};

View file

@ -8,16 +8,14 @@ import {
} from 'vue'; } from 'vue';
import { import {
model, model,
ActiveBlock,
Block, Block,
SbMode,
Mode,
EditorDimensions,
BlockDefinition, BlockDefinition,
BlockLibraryDefinition, BlockLibraryDefinition,
BlockLibrary, } from '/@/blocks';
useResizeObserver, import { Mode, SbMode } from '/@/mode';
} from '/@components/TreeElement'; import { BlockLibrary } from '/@/use-dynamic-blocks';
import { EditorDimensions, useResizeObserver } from '/@/use-resize-observer';
import { ActiveBlock } from '/@/use-activation';
import SbBlock from '/@internal/Block'; import SbBlock from '/@internal/Block';
@ -30,8 +28,8 @@ import './Schlechtenburg.scss';
export interface SchlechtenburgProps { export interface SchlechtenburgProps {
customBlocks: BlockDefinition[]; customBlocks: BlockDefinition[];
eventUpdate: (b: Block) => void; eventUpdate: (b: Block<any>) => void;
block: Block; block: Block<any>;
mode: SbMode; mode: SbMode;
} }
@ -42,18 +40,18 @@ export default defineComponent({
props: { props: {
customBlocks: { type: Array as PropType<BlockDefinition[]>, default: () => [] }, customBlocks: { type: Array as PropType<BlockDefinition[]>, default: () => [] },
block: { type: Object as PropType<Block>, required: true }, block: { type: Object as PropType<Block<any>>, required: true },
eventUpdate: { type: Function, default: () => {} }, eventUpdate: { type: Function, default: () => {} },
mode: { mode: {
type: String as SbMode, type: String as PropType<SbMode>,
validator(value: string) { validator(value: any) {
return Object.values(SbMode).includes(value); return Object.values(SbMode).includes(value);
}, },
default: SbMode.Edit, default: SbMode.Edit,
}, },
}, },
setup(props: SchlechtenburgProps) { setup(props) {
const el: Ref<null|HTMLElement> = ref(null); const el: Ref<null|HTMLElement> = ref(null);
useResizeObserver(el, EditorDimensions); useResizeObserver(el, EditorDimensions);
@ -69,10 +67,7 @@ export default defineComponent({
'sb-paragraph': SbParagraph, 'sb-paragraph': SbParagraph,
'sb-heading': SbHeading, 'sb-heading': SbHeading,
...props.customBlocks.reduce( ...props.customBlocks.reduce(
( (blocks: {[name: string]: Block<any>}, block: Block<any>) => ({ ...blocks, [block.name]: block }),
blocks,
block,
) => ({ ...blocks, [block.name]: block }),
{}, {},
), ),
}); });

View file

@ -1,130 +0,0 @@
import {
Ref,
ref,
inject,
reactive,
computed,
watch,
provide,
} from 'vue';
export interface BlockDefinition {
name: string;
getDefaultData: any;
edit: () => Promise<any>;
display: () => Promise<any>;
}
export interface BlockLibraryDefinition {
[name: string]: BlockDefinition;
}
export interface BlockData { [name: string]: any }
export interface Block {
name: string;
blockId: string;
data: BlockData;
}
export interface BlockProps {
blockId: string;
data: { [key: string]: any};
}
export const model = {
prop: 'block',
event: 'update',
};
export const blockProps = {
blockId: { type: String, required: true },
data: { type: Object, default: () => ({}) },
};
export enum SbMode {
Edit = 'edit',
Display = 'display',
}
export const Mode = Symbol('Schlechtenburg mode');
export const BlockLibrary = Symbol('Schlechtenburg block library');
export function useDynamicBlocks() {
const mode = inject(Mode, ref(SbMode.Edit));
const customBlocks: BlockLibraryDefinition = inject(BlockLibrary, reactive({}));
const getBlock = (name: string) => customBlocks[name][mode.value];
return {
mode,
customBlocks,
getBlock,
};
}
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 };
}
export const ActiveBlock = Symbol('Schlechtenburg active block');
export function useActivation(currentBlockId: string) {
const activeBlockId: Ref<string|null> = inject(ActiveBlock, ref(null));
const isActive = computed(() => activeBlockId.value === currentBlockId);
const activate = (blockId?: string|null) => {
activeBlockId.value = blockId !== undefined ? blockId : currentBlockId;
};
const requestActivation = () => {
if (activeBlockId.value) {
return;
}
activate();
};
return {
isActive,
activate,
requestActivation,
};
}

View file

@ -6,14 +6,11 @@ import {
ref, ref,
Ref, Ref,
} from 'vue'; } from 'vue';
import { import { Block } from '/@/blocks';
Block, import { SbMode } from '/@/mode';
useDynamicBlocks, import { useResizeObserver, BlockDimensions } from '/@/use-resize-observer';
useActivation, import { useActivation } from '/@/use-activation';
SbMode, import { useDynamicBlocks } from '/@/use-dynamic-blocks';
BlockDimensions,
useResizeObserver,
} from '/@components/TreeElement';
import SbBlockOrdering from './BlockOrdering'; import SbBlockOrdering from './BlockOrdering';
@ -22,7 +19,7 @@ import './Block.scss';
interface BlockProps { interface BlockProps {
block: Block; block: Block;
eventUpdate: (b?: Block) => void; eventUpdate: (b?: Block) => void;
eventInsertBlock: (b?: Block) => void; eventPrependBlock: (b?: Block) => void;
eventAppendBlock: (b?: Block) => void; eventAppendBlock: (b?: Block) => void;
eventRemoveBlock: () => void; eventRemoveBlock: () => void;
eventMoveUp: () => void; eventMoveUp: () => void;
@ -43,7 +40,7 @@ export default defineComponent({
default: null, default: null,
}, },
eventUpdate: { type: Function, default: () => {} }, eventUpdate: { type: Function, default: () => {} },
eventInsertBlock: { type: Function, default: () => {} }, eventPrependBlock: { type: Function, default: () => {} },
eventAppendBlock: { type: Function, default: () => {} }, eventAppendBlock: { type: Function, default: () => {} },
eventRemoveBlock: { type: Function, default: () => {} }, eventRemoveBlock: { type: Function, default: () => {} },
eventMoveUp: { type: Function, default: () => {} }, eventMoveUp: { type: Function, default: () => {} },
@ -88,31 +85,19 @@ export default defineComponent({
class={classes.value} class={classes.value}
> >
<div class="sb-block__edit-cover"></div> <div class="sb-block__edit-cover"></div>
{props.sortable {context.slots['context-toolbar'] ? context.slots['context-toolbar']() : null}
? <SbBlockOrdering
eventMoveUp={props.eventMoveUp}
eventMoveDown={props.eventMoveDown}
eventRemoveBlock={props.eventRemoveBlock}
sortable={props.sortable}
/>
: null}
<BlockComponent <BlockComponent
data={props.block.data} data={props.block.data}
block-id={props.block.blockId} blockId={props.block.blockId}
eventUpdate={onChildUpdate} eventUpdate={onChildUpdate}
eventInsertBlock={props.eventInsertBlock} eventPrependBlock={props.eventPrependBlock}
eventAppendBlock={props.eventAppendBlock} eventAppendBlock={props.eventAppendBlock}
eventRemoveBlock={props.eventRemoveBlock} eventRemoveBlock={props.eventRemoveBlock}
{...{ onClick={($event: MouseEvent) => {
attrs: context.attrs,
nativeOn: {
click: ($event: MouseEvent) => {
$event.stopPropagation(); $event.stopPropagation();
activate(); activate();
},
...context.listeners,
},
}} }}
{...context.attrs}
/> />
</div>; </div>;
}, },

View file

@ -3,17 +3,15 @@ import {
watch, watch,
reactive, reactive,
computed, computed,
defineAsyncComponent, defineComponent,
} from 'vue'; } from 'vue';
import { import { useBlockSizing } from '/@/use-resize-observer';
useBlockSizing,
} from '/@components/TreeElement';
import SbButton from './Button'; import SbButton from './Button';
import './BlockOrdering.scss'; import './BlockOrdering.scss';
export default defineAsyncComponent({ export default defineComponent({
name: 'sb-block-ordering', name: 'sb-block-ordering',
props: { props: {

View file

@ -6,10 +6,10 @@ import {
import { import {
useDynamicBlocks, useDynamicBlocks,
BlockDefinition, BlockDefinition,
} from '../TreeElement'; } from '/@/use-dynamic-blocks';
import SbButton from './Button'; import SbButton from '/@internal/Button';
import SbModal from './Modal'; import SbModal from '/@internal/Modal';
import './BlockPicker.scss'; import './BlockPicker.scss';

View file

@ -1,6 +1,6 @@
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import { BlockDefinition } from '../TreeElement'; import { BlockDefinition } from '/@/blocks';
import BlockPicker from './BlockPicker'; import BlockPicker from '/@internal/BlockPicker';
import './BlockPlaceholder.scss'; import './BlockPlaceholder.scss';
@ -11,11 +11,7 @@ export default defineComponent({
return () => ( return () => (
<div class="sb-block-placeholder"> <div class="sb-block-placeholder">
<BlockPicker <BlockPicker
{...{ onPickedBlock={(block: BlockDefinition) => context.emit('insert-block', block)}
on: {
'picked-block': (block: BlockDefinition) => context.emit('insert-block', block),
},
}}
/> />
</div> </div>
); );

View file

@ -9,10 +9,9 @@ export default defineComponent({
setup(props, context) { setup(props, context) {
return () => ( return () => (
<button <button
class="sb-button"
{...{ {...{
attrs: context.attrs, ...context.attrs,
on: context.listeners, class: (context.attrs.class || '') + ' sb-button',
}} }}
> >
{context.slots.default()} {context.slots.default()}

View file

@ -11,10 +11,7 @@ export default defineComponent({
<div class="sb-select"> <div class="sb-select">
<select <select
class="sb-select__input" class="sb-select__input"
{...{ {...context.attrs}
attrs: context.attrs,
on: context.listeners,
}}
> >
{context.slots.default()} {context.slots.default()}
</select> </select>

View file

@ -4,14 +4,14 @@ import {
watch, watch,
reactive, reactive,
} from 'vue'; } from 'vue';
import { useBlockSizing } from '/@components/TreeElement'; import { useBlockSizing } from '/@/use-resize-observer';
import './Toolbar.scss'; import './Toolbar.scss';
export default defineComponent({ export default defineComponent({
name: 'sb-toolbar', name: 'sb-toolbar',
setup(props, context) { setup(_, context) {
const styles = reactive({ const styles = reactive({
bottom: '', bottom: '',
left: '', left: '',
@ -38,7 +38,7 @@ export default defineComponent({
style={styles} style={styles}
onClick={($event: MouseEvent) => $event.stopPropagation()} onClick={($event: MouseEvent) => $event.stopPropagation()}
> >
{context.slots.default()} {context.slots?.default?.()}
</div> </div>
); );
}, },

View file

@ -1,8 +1,10 @@
import { defineAsyncComponent, PropType } from 'vue'; import { defineComponent, PropType } from 'vue';
import { import {
model, model,
blockProps, blockProps,
} from '/@components/TreeElement'; } from '/@/blocks';
import SbBlock from '/@internal/block';
import { import {
getDefaultData, getDefaultData,
@ -12,7 +14,7 @@ import {
import './style.scss'; import './style.scss';
export default defineAsyncComponent({ export default defineComponent({
name: 'sb-image-display', name: 'sb-image-display',
model, model,
@ -26,12 +28,13 @@ export default defineAsyncComponent({
}, },
setup(props: ImageProps) { setup(props: ImageProps) {
console.log('img display', props.data); return () => <figure class="sb-image">
<img
return () => <img class="sb-image__content"
class="sb-image"
src={props.data.src} src={props.data.src}
alt={props.data.alt} alt={props.data.alt}
/>; />
<SbBlock block={props.data.description} />
</figure>;
}, },
}); });

View file

@ -1,5 +1,5 @@
import { import {
defineAsyncComponent, defineComponent,
reactive, reactive,
ref, ref,
Ref, Ref,
@ -9,10 +9,11 @@ import {
import { import {
model, model,
blockProps, blockProps,
} from '/@components/TreeElement'; } from '/@/blocks';
import SbToolbar from '/@internal/Toolbar'; import SbToolbar from '/@internal/Toolbar';
import SbButton from '/@internal/Button'; import SbButton from '/@internal/Button';
import SbBlock from '/@internal/Block';
import { import {
getDefaultData, getDefaultData,
@ -22,7 +23,7 @@ import {
import './style.scss'; import './style.scss';
export default defineAsyncComponent({ export default defineComponent({
name: 'sb-image-edit', name: 'sb-image-edit',
model, model,
@ -40,6 +41,7 @@ export default defineAsyncComponent({
const localData = reactive({ const localData = reactive({
src: props.data.src, src: props.data.src,
alt: props.data.alt, alt: props.data.alt,
description: props.data.description,
}); });
const fileInput: Ref<null|HTMLInputElement> = ref(null); const fileInput: Ref<null|HTMLInputElement> = ref(null);
@ -47,6 +49,7 @@ export default defineAsyncComponent({
watch(() => props.data, () => { watch(() => props.data, () => {
localData.src = props.data.src; localData.src = props.data.src;
localData.alt = props.data.alt; localData.alt = props.data.alt;
localData.description = props.data.description;
}); });
const selectImage = () => { const selectImage = () => {
@ -62,6 +65,7 @@ export default defineAsyncComponent({
props.eventUpdate({ props.eventUpdate({
src: reader.result, src: reader.result,
alt: props.data.alt, alt: props.data.alt,
description: props.data.description,
}); });
}); });
@ -69,23 +73,40 @@ export default defineAsyncComponent({
} }
}; };
const onDescriptionUpdate = (description) => {
props.eventUpdate({
...props.data,
description,
});
};
return () => ( return () => (
<div class="sb-image"> <figure class="sb-image">
<SbToolbar> <SbToolbar>
{localData.src {localData.src
? <SbButton onClick={selectImage}>Change Image</SbButton> ? <SbButton onClick={selectImage}>Change Image</SbButton>
: null} : null}
<input <input
type="file" type="file"
ref="fileInput" ref={fileInput}
style="display: none;" style="display: none;"
onInput={onImageSelect} onInput={onImageSelect}
/> />
</SbToolbar> </SbToolbar>
{localData.src {localData.src
? <img src={localData.src} alt={localData.alt} /> ? <>
<img
src={localData.src}
alt={localData.alt}
class="sb-image__content"
/>
<SbBlock
block={localData.description}
eventUpdate={(updated: Block) => onDescriptionUpdate(updated)}
/>
</>
: <SbButton onClick={selectImage}>Select Image</SbButton>} : <SbButton onClick={selectImage}>Select Image</SbButton>}
</div> </figure>
); );
}, },
}); });

View file

@ -1,10 +1,8 @@
.sb-image { .sb-image {
margin: 0;
@at-root { &__content {
& > img,
img#{&} {
width: 100%; width: 100%;
height: auto; height: auto;
} }
} }
}

View file

@ -1,8 +1,13 @@
import { BlockData, BlockProps } from '/@components/TreeElement'; import {
ParagraphData,
getDefaultData as getDefaultParagraphData
} from '/@user/Paragraph/util';
import { BlockData, BlockProps } from '/@/blocks';
export interface ImageData { export interface ImageData {
src: string; src: string;
alt: string; alt: string;
description: ParagraphData;
} }
export interface ImageProps extends BlockProps { export interface ImageProps extends BlockProps {
@ -13,4 +18,5 @@ export interface ImageProps extends BlockProps {
export const getDefaultData: () => ImageData = () => ({ export const getDefaultData: () => ImageData = () => ({
src: '', src: '',
alt: '', alt: '',
description: getDefaultParagraphData(),
}); });

View file

@ -6,7 +6,7 @@ import {
import { import {
model, model,
blockProps, blockProps,
} from '/@components/TreeElement'; } from '/@/blocks';
import SbBlock from '/@internal/Block'; import SbBlock from '/@internal/Block';
@ -37,8 +37,6 @@ export default defineComponent({
[`sb-layout_${props.data.orientation}`]: true, [`sb-layout_${props.data.orientation}`]: true,
})); }));
console.log('layout display', props.data);
return () => ( return () => (
<div class={classes.value}> <div class={classes.value}>
{...props.data.children.map((child) => ( {...props.data.children.map((child) => (

View file

@ -7,15 +7,16 @@ import {
} from 'vue'; } from 'vue';
import { import {
model, model,
useActivation,
Block, Block,
blockProps, blockProps,
} from '/@components/TreeElement'; } from '/@/blocks';
import { useActivation } from '/@/use-activation';
import SbBlock from '/@internal/Block'; import SbBlock from '/@internal/Block';
import SbButton from '/@internal/Button'; import SbButton from '/@internal/Button';
import SbToolbar from '/@internal/Toolbar'; import SbToolbar from '/@internal/Toolbar';
import SbBlockPlaceholder from '/@internal/BlockPlaceholder'; import SbBlockPlaceholder from '/@internal/BlockPlaceholder';
import SbBlockOrdering from '/@internal/BlockOrdering';
import { import {
LayoutData, LayoutData,
@ -58,6 +59,7 @@ export default defineComponent({
})); }));
const toggleOrientation = () => { const toggleOrientation = () => {
console.log('toggle');
props.eventUpdate({ props.eventUpdate({
orientation: localData.orientation === 'vertical' ? 'horizontal' : 'vertical', orientation: localData.orientation === 'vertical' ? 'horizontal' : 'vertical',
}); });
@ -144,18 +146,12 @@ export default defineComponent({
props.eventUpdate({ children: [...localData.children] }); props.eventUpdate({ children: [...localData.children] });
}; };
console.log('Rendering edit');
return () => ( return () => (
<div class={classes.value}> <div class={classes.value}>
<SbToolbar slot="toolbar"> <SbToolbar>
<SbButton <SbButton
type="button" type="button"
{...{ onClick={toggleOrientation}
nativeOn: {
click: toggleOrientation,
},
}}
>{localData.orientation}</SbButton> >{localData.orientation}</SbButton>
</SbToolbar> </SbToolbar>
@ -165,23 +161,23 @@ export default defineComponent({
data-order={index} data-order={index}
block={child} block={child}
eventUpdate={(updated: Block) => onChildUpdate(child, updated)} eventUpdate={(updated: Block) => onChildUpdate(child, updated)}
eventInsertBlock={(block: Block) => insertBlock(index, block)} eventPrependBlock={(block: Block) => insertBlock(index - 1, block)}
eventAppendBlock={appendBlock} eventAppendBlock={(block: Block) => insertBlock(index, block)}
eventRemoveBlock={() => removeBlock(index)} removable
>
{{
'context-toolbar': () =>
<SbBlockOrdering
eventMoveUp={() => moveUp(index)} eventMoveUp={() => moveUp(index)}
eventMoveDown={() => moveDown(index)} eventMoveDown={() => moveDown(index)}
sortable={localData.orientation} eventRemoveBlock={() => removeBlock(index)}
removable sortable={props.sortable}
/> />,
}}
</SbBlock>
))} ))}
<SbBlockPlaceholder <SbBlockPlaceholder onInsertBlock={appendBlock} />
{...{
on: {
'insert-block': appendBlock,
},
}}
/>
</div> </div>
); );
}, },

View file

@ -1,6 +1,5 @@
.sb-layout { .sb-layout {
display: flex; display: flex;
flex-basis: 100%;
&_vertical { &_vertical {
flex-direction: column; flex-direction: column;
@ -10,7 +9,13 @@
flex-direction: row; flex-direction: row;
} }
&__item {
position: relative;
}
> * { > * {
flex-basis: 100%; flex-basis: auto;
flex-grow: 1;
flex-shrink: 1;
} }
} }

View file

@ -2,7 +2,7 @@ import {
BlockProps, BlockProps,
Block, Block,
BlockData, BlockData,
} from '/@components/TreeElement'; } from '/@/blocks';
export interface LayoutData { export interface LayoutData {
orientation: string; orientation: string;

View file

@ -1,5 +1,5 @@
import { import {
defineAsyncComponent, defineComponent,
computed, computed,
PropType, PropType,
} from 'vue'; } from 'vue';
@ -7,7 +7,7 @@ import {
model, model,
blockProps, blockProps,
BlockProps, BlockProps,
} from '/@components/TreeElement'; } from '/@/blocks';
import { import {
getDefaultData, getDefaultData,
@ -20,7 +20,7 @@ interface ParagraphProps extends BlockProps {
data: ParagraphData; data: ParagraphData;
} }
export default defineAsyncComponent({ export default defineComponent({
name: 'sb-paragraph-display', name: 'sb-paragraph-display',
model, model,
@ -39,12 +39,10 @@ export default defineAsyncComponent({
[`sb-paragraph_align-${props.data.align}`]: true, [`sb-paragraph_align-${props.data.align}`]: true,
})); }));
console.log('p display', props.data);
return () => <p return () => <p
class={classes.value} class={classes.value}
{...{ {...{
domProps: { innerHTML: props.data.value }, innerHTML: props.data.value,
}} }}
></p>; ></p>;
}, },

View file

@ -1,5 +1,5 @@
import { import {
defineAsyncComponent, defineComponent,
reactive, reactive,
computed, computed,
ref, ref,
@ -13,8 +13,8 @@ import {
blockProps, blockProps,
BlockProps, BlockProps,
BlockData, BlockData,
useActivation, } from '/@/blocks';
} from '/@components/TreeElement'; import { useActivation } from '/@/use-activation';
import SbToolbar from '/@internal/Toolbar'; import SbToolbar from '/@internal/Toolbar';
import SbSelect from '/@internal/Select'; import SbSelect from '/@internal/Select';
@ -29,11 +29,11 @@ import './style.scss';
interface ParagraphProps extends BlockProps { interface ParagraphProps extends BlockProps {
data: ParagraphData; data: ParagraphData;
eventUpdate: (b?: ParagraphData) => void; eventUpdate: (b?: ParagraphData) => void;
eventInsertBlock: (b?: BlockData) => void; eventAppendBlock: (b?: BlockData) => void;
eventRemoveBlock: () => void; eventRemoveBlock: () => void;
} }
export default defineAsyncComponent({ export default defineComponent({
name: 'sb-paragraph-edit', name: 'sb-paragraph-edit',
model, model,
@ -45,7 +45,7 @@ export default defineAsyncComponent({
default: getDefaultData, default: getDefaultData,
}, },
eventUpdate: { type: Function, default: () => {} }, eventUpdate: { type: Function, default: () => {} },
eventInsertBlock: { type: Function, default: () => {} }, eventAppendBlock: { type: Function, default: () => {} },
eventRemoveBlock: { type: Function, default: () => {} }, eventRemoveBlock: { type: Function, default: () => {} },
}, },
@ -118,9 +118,9 @@ export default defineAsyncComponent({
}; };
const onKeydown = ($event: KeyboardEvent) => { const onKeydown = ($event: KeyboardEvent) => {
if ($event.key === 'Enter' && !$event.shiftKey) { if (props.eventAppendBlock && $event.key === 'Enter' && !$event.shiftKey) {
const blockId = `${+(new Date())}`; const blockId = `${+(new Date())}`;
props.eventInsertBlock({ props.eventAppendBlock({
blockId, blockId,
name: 'sb-paragraph', name: 'sb-paragraph',
data: getDefaultData(), data: getDefaultData(),
@ -133,7 +133,7 @@ export default defineAsyncComponent({
}; };
const onKeyup = ($event: KeyboardEvent) => { const onKeyup = ($event: KeyboardEvent) => {
if ($event.key === 'Backspace' && localData.value === '') { if (props.eventRemoveBlock && $event.key === 'Backspace' && localData.value === '') {
props.eventRemoveBlock(); props.eventRemoveBlock();
} }
}; };
@ -163,4 +163,4 @@ export default defineAsyncComponent({
</div> </div>
); );
}, },
}; });

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,5 @@
export enum SbMode {
Edit = 'edit',
Display = 'display',
}
export const Mode = Symbol('Schlechtenburg mode');

View file

@ -1,44 +0,0 @@
declare module '*.bmp' {
const src: string;
export default src;
}
declare module '*.gif' {
const src: string;
export default src;
}
declare module '*.jpg' {
const src: string;
export default src;
}
declare module '*.jpeg' {
const src: string;
export default src;
}
declare module '*.png' {
const src: string;
export default src;
}
declare module '*.webp' {
const src: string;
export default src;
}
declare module '*.module.css' {
const classes: { readonly [key: string]: string };
export default classes;
}
declare module '*.module.scss' {
const classes: { readonly [key: string]: string };
export default classes;
}
declare module '*.module.sass' {
const classes: { readonly [key: string]: string };
export default classes;
}

View file

@ -1,5 +1,5 @@
declare module '*.vue' { declare module "*.vue" {
import Vue from 'vue'; import { defineComponent } from "vue";
const Component: ReturnType<typeof defineComponent>;
export default Vue; export default Component;
} }

View file

@ -1,36 +0,0 @@
/* eslint no-param-reassign: 0 */
import { GetterTree, MutationTree, ActionTree } from 'vuex';
interface State {
activeBlockId: number|null;
}
const state: State = {
activeBlockId: null,
};
const getters = {
activeBlockId: (s) => s.activeBlockId,
} as GetterTree<State, any>;
const actions = {
setActiveBlock({ commit }, id) {
commit('setActiveBlock', id);
},
} as ActionTree<State, any>;
const mutations = {
setActiveBlock(s, id) {
s.activeBlockId = id;
},
} as MutationTree<State>;
const SchlechtenburgModule = {
namespaced: true,
state,
getters,
mutations,
actions,
};
export default SchlechtenburgModule;

View file

@ -0,0 +1,28 @@
import {
Ref,
ref,
inject,
computed,
} from 'vue';
export const ActiveBlock = Symbol('Schlechtenburg active block');
export function useActivation(currentBlockId: string) {
const activeBlockId: Ref<string|null> = inject(ActiveBlock, ref(null));
const isActive = computed(() => activeBlockId.value === currentBlockId);
const activate = (blockId?: string|null) => {
activeBlockId.value = blockId !== undefined ? blockId : currentBlockId;
};
const requestActivation = () => {
if (activeBlockId.value) {
return;
}
activate();
};
return {
isActive,
activate,
requestActivation,
};
}

View file

@ -0,0 +1,20 @@
import {
ref,
inject,
reactive,
} from 'vue';
import { BlockLibraryDefinition } from '/@/blocks';
import { Mode, SbMode } from '/@/mode';
export const BlockLibrary = Symbol('Schlechtenburg block library');
export function useDynamicBlocks() {
const mode = inject(Mode, ref(SbMode.Edit));
const customBlocks: BlockLibraryDefinition = inject(BlockLibrary, reactive({}));
const getBlock = (name: string) => customBlocks[name][mode.value];
return {
mode,
customBlocks,
getBlock,
};
}

View file

@ -0,0 +1,54 @@
import {
Ref,
ref,
inject,
watch,
provide,
} from 'vue';
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 };
}

64
packages/core/lib/vue-app-env.d.ts vendored Normal file
View file

@ -0,0 +1,64 @@
declare namespace NodeJS {
interface Process{
env: ProcessEnv
}
interface ProcessEnv {
/**
* By default, there are two modes in Vite:
*
* * `development` is used by vite and vite serve
* * `production` is used by vite build
*
* You can overwrite the default mode used for a command by passing the --mode option flag.
*
*/
readonly NODE_ENV: 'development' | 'production'
}
}
declare var process: NodeJS.Process
declare module '*.gif' {
const src: string
export default src
}
declare module '*.jpg' {
const src: string
export default src
}
declare module '*.jpeg' {
const src: string
export default src
}
declare module '*.png' {
const src: string
export default src
}
declare module '*.webp' {
const src: string
export default src
}
declare module '*.svg' {
const src: string;
export default src
}
declare module '*.module.css' {
const classes: { readonly [key: string]: string }
export default classes
}
declare module '*.module.scss' {
const classes: { readonly [key: string]: string }
export default classes
}
declare module '*.module.sass' {
const classes: { readonly [key: string]: string }
export default classes
}

View file

@ -7,7 +7,15 @@
"jsx": "preserve", "jsx": "preserve",
"sourceMap": true, "sourceMap": true,
"lib": [ "esnext", "dom" ], "lib": [ "esnext", "dom" ],
"plugins": [{ "name": "@vuedx/typescript-plugin-vue" }] "plugins": [ { "name": "@vuedx/typescript-plugin-vue" } ],
"resolveJsonModule": true,
// "noErrorTruncation": true,
"paths": {
"/@/*": [ "./lib/*" ],
"/@components/*": [ "./lib/components/*" ],
"/@internal/*": [ "./lib/components/internal/*" ],
"/@user/*": [ "./lib/components/user/*" ]
}
}, },
"include": [ "lib/**/*.ts", "lib/**/*.d.ts", "lib/**/*.tsx", "lib/**/*.vue" ] "include": [ "lib/**/*.ts", "lib/**/*.d.ts", "lib/**/*.tsx", "lib/**/*.vue" ]
} }