Fixed typescript stuff

This commit is contained in:
Benjamin Bädorf 2020-05-27 17:06:14 +02:00
parent b81f0c6673
commit 7156fe11e0
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C
14 changed files with 185 additions and 128 deletions

View file

@ -14,6 +14,7 @@ module.exports = {
rules: { rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'@typescript-eslint/no-empty-function': 0,
}, },
overrides: [ overrides: [
{ {

View file

@ -4,7 +4,7 @@ import {
ref, ref,
} from '@vue/composition-api'; } from '@vue/composition-api';
import Schlechtenburg from '@components/Schlechtenburg'; import Schlechtenburg from '@components/Schlechtenburg';
import { BlockData } from './components/TreeElement'; import { Block } from './components/TreeElement';
import './App.scss'; import './App.scss';
@ -20,7 +20,7 @@ export default defineComponent({
orientation: 'vertical', orientation: 'vertical',
children: [], children: [],
}, },
}) as BlockData; }) as Block;
return () => ( return () => (
<div id="app"> <div id="app">
@ -41,7 +41,7 @@ export default defineComponent({
<Schlechtenburg <Schlechtenburg
vShow={activeTab.value === 'edit'} vShow={activeTab.value === 'edit'}
block={block} block={block}
eventUpdate={(newBlock: BlockData) => { eventUpdate={(newBlock: Block) => {
block.name = newBlock.name; block.name = newBlock.name;
block.blockId = newBlock.blockId; block.blockId = newBlock.blockId;
block.data = newBlock.data; block.data = newBlock.data;

View file

@ -8,7 +8,7 @@ import {
import { import {
model, model,
ActiveBlock, ActiveBlock,
BlockData, Block,
SbMode, SbMode,
Mode, Mode,
BlockDefinition, BlockDefinition,
@ -27,8 +27,8 @@ import './Schlechtenburg.scss';
export interface SchlechtenburgProps { export interface SchlechtenburgProps {
customBlocks: BlockDefinition[]; customBlocks: BlockDefinition[];
eventUpdate: (b?: BlockData) => void; eventUpdate: (b: Block) => void;
block: BlockData; block: Block;
mode: SbMode; mode: SbMode;
} }
@ -39,11 +39,8 @@ export default defineComponent({
props: { props: {
customBlocks: { type: Array as PropType<BlockDefinition[]>, default: () => [] }, customBlocks: { type: Array as PropType<BlockDefinition[]>, default: () => [] },
block: { type: Object as PropType<BlockData>, required: true }, block: { type: Object as PropType<Block>, required: true },
eventUpdate: { eventUpdate: { type: Function, default: () => {} },
type: (Function as unknown) as (b?: BlockData) => void,
default: () => () => undefined,
},
mode: { mode: {
type: String, type: String,
validator(value: string) { validator(value: string) {
@ -53,7 +50,7 @@ export default defineComponent({
}, },
}, },
setup(props: SchlechtenburgProps, context) { setup(props: SchlechtenburgProps) {
const mode = ref(props.mode); const mode = ref(props.mode);
provide(Mode, mode); provide(Mode, mode);

View file

@ -17,10 +17,12 @@ export interface BlockLibraryDefinition {
[name: string]: BlockDefinition; [name: string]: BlockDefinition;
} }
export interface BlockData { export interface BlockData { [name: string]: any }
export interface Block {
name: string; name: string;
blockId: string; blockId: string;
data: { [name: string]: any }; data: BlockData;
} }
export interface BlockProps { export interface BlockProps {
@ -35,10 +37,6 @@ export const model = {
export const blockProps = { export const blockProps = {
blockId: { type: String, required: true }, blockId: { type: String, required: true },
eventUpdate: {
type: (Function as unknown) as (b: any) => void,
default: () => () => undefined,
},
data: { type: Object, default: () => ({}) }, data: { type: Object, default: () => ({}) },
}; };

View file

@ -4,33 +4,34 @@ import {
PropType, PropType,
} from '@vue/composition-api'; } from '@vue/composition-api';
import { import {
BlockData, Block,
useDynamicBlocks, useDynamicBlocks,
useActivation, useActivation,
} from '@components/TreeElement'; } from '@components/TreeElement';
import './Block.scss'; import './Block.scss';
interface BlockProps {
block: Block;
eventUpdate: (b?: Block) => void;
eventInsertBlock: (b?: Block) => void;
eventAppendBlock: (b?: Block) => void;
}
export default defineComponent({ export default defineComponent({
name: 'sb-block', name: 'sb-block',
props: { props: {
block: { type: (null as unknown) as PropType<BlockData>, required: true }, block: {
eventUpdate: { type: (null as unknown) as PropType<Block>,
type: (Function as unknown) as (b?: BlockData) => void, required: true,
default: () => () => undefined,
},
eventInsertBlock: {
type: (Function as unknown) as (b?: BlockData) => void,
default: () => () => undefined,
},
eventAppendBlock: {
type: (Function as unknown) as (b?: BlockData) => void,
default: () => () => undefined,
}, },
eventUpdate: { type: Function, default: () => {} },
eventInsertBlock: { type: Function, default: () => {} },
eventAppendBlock: { type: Function, default: () => {} },
}, },
setup(props, context) { setup(props: BlockProps, context) {
const { isActive, activate } = useActivation(props.block.blockId); const { isActive, activate } = useActivation(props.block.blockId);
const { getBlock } = useDynamicBlocks(); const { getBlock } = useDynamicBlocks();
const classes = computed(() => ({ const classes = computed(() => ({
@ -48,31 +49,26 @@ export default defineComponent({
}); });
}; };
const Block = getBlock(props.block.name) as any; const BlockComponent = getBlock(props.block.name) as any;
return () => <div class={classes.value}> return () => (<div class={classes.value}>
<div class="sb-block__edit-cover"></div> <div class="sb-block__edit-cover"></div>
<div class="sb-block__mover"></div> <div class="sb-block__mover"></div>
<Block <BlockComponent
data={props.block.data} data={props.block.data}
block-id={props.block.blockId} block-id={props.block.blockId}
eventUpdate={onChildUpdate} eventUpdate={onChildUpdate}
eventInsertBlock={props.eventInsertBlock} eventInsertBlock={props.eventInsertBlock}
eventAppendBlock={props.eventAppendBlock} eventAppendBlock={props.eventAppendBlock}
onClick={($event: MouseEvent) => {
$event.stopPropagation();
activate();
}}
{...{ {...{
attrs: context.attrs, attrs: context.attrs,
on: { on: context.listeners,
...context.listeners,
update: onChildUpdate,
},
nativeOn: {
click: ($event: MouseEvent) => {
$event.stopPropagation();
activate();
},
},
}} }}
/> />
</div>; </div>);
}, },
}); });

View file

@ -36,17 +36,17 @@ export default defineComponent({
return () => ( return () => (
<div <div
class="sb-block-picker" class="sb-block-picker"
onClick={($event: MouseEvent) => $event.stopPropagation()}
> >
<SbButton <SbButton
type="button" type="button"
onClick={() => { onClick={($event: MouseEvent) => {
open.value = true; open.value = true;
console.log(open); $event.stopPropagation();
}} }}
>Add a block</SbButton> >Add a block</SbButton>
<SbModal <SbModal
open={open.value} open={open.value}
onClick={($event: MouseEvent) => $event.stopPropagation()}
eventClose={() => { eventClose={() => {
open.value = false; open.value = false;
}} }}

View file

@ -1,11 +1,15 @@
import { import {
defineComponent, defineComponent,
computed, computed,
ref,
} from '@vue/composition-api'; } from '@vue/composition-api';
import './Modal.scss'; import './Modal.scss';
interface ModalProps {
open: boolean;
eventClose: () => void;
}
export default defineComponent({ export default defineComponent({
name: 'sb-modal', name: 'sb-modal',
@ -14,13 +18,10 @@ export default defineComponent({
type: Boolean, type: Boolean,
default: false, default: false,
}, },
eventClose: { eventClose: { type: Function, default: () => {} },
type: (Function as unknown) as () => void,
default: () => () => undefined,
},
}, },
setup(props, context) { setup(props: ModalProps, context) {
const classes = computed(() => ({ const classes = computed(() => ({
'sb-modal': true, 'sb-modal': true,
'sb-modal_open': props.open, 'sb-modal_open': props.open,

View file

@ -0,0 +1,93 @@
import {
defineComponent,
reactive,
ref,
Ref,
watch,
PropType,
} from '@vue/composition-api';
import {
model,
blockProps,
} from '@components/TreeElement';
import SbToolbar from '@internal/Toolbar';
import {
getDefaultData,
ImageData,
ImageProps,
} from './util';
import './style.scss';
export default defineComponent({
name: 'sb-image-display',
model,
props: {
...blockProps,
data: {
type: (null as unknown) as PropType<ImageData>,
default: getDefaultData,
},
},
setup(props: ImageProps, context) {
const localData = reactive({
src: props.data.src,
alt: props.data.alt,
});
const fileInput: Ref<null|HTMLInputElement> = ref(null);
watch(() => props.data, () => {
localData.src = props.data.src;
localData.alt = props.data.alt;
});
const selectImage = () => {
if (fileInput.value) {
fileInput.value.click();
}
};
const onImageSelect = () => {
if (fileInput.value && fileInput.value.files && fileInput.value.files.length) {
context.emit('update', {
src: window.URL.createObjectURL(fileInput.value.files[0]),
});
}
};
return () => (
<div class="sb-image">
<SbToolbar>
Image Edit
<input
type="file"
ref="fileInput"
style="display: none;"
{...{
on: {
input: onImageSelect,
},
}}
/>
</SbToolbar>
{localData.src
? <img src={localData.src} alt={localData.alt} />
: <button
{...{
on: {
click: selectImage,
},
}}
>Select Image</button>
}
</div>
);
},
});

View file

@ -8,7 +8,7 @@ import {
import { import {
model, model,
useActivation, useActivation,
BlockData, Block,
blockProps, blockProps,
} from '@components/TreeElement'; } from '@components/TreeElement';
@ -32,17 +32,14 @@ export default defineComponent({
props: { props: {
...blockProps, ...blockProps,
eventUpdate: { eventUpdate: { type: Function, default: () => {} },
type: (Function as unknown) as (b?: LayoutData) => void,
default: () => () => undefined,
},
data: { data: {
type: (null as unknown) as PropType<LayoutData>, type: (null as unknown) as PropType<LayoutData>,
default: getDefaultData, default: getDefaultData,
}, },
}, },
setup(props: LayoutProps, context) { setup(props: LayoutProps) {
const { activate } = useActivation(props.blockId); const { activate } = useActivation(props.blockId);
const localData: LayoutData = reactive({ const localData: LayoutData = reactive({
@ -66,7 +63,7 @@ export default defineComponent({
}); });
}; };
const onChildUpdate = (child: BlockData, updated: BlockData) => { const onChildUpdate = (child: Block, updated: Block) => {
const index = localData.children.indexOf(child); const index = localData.children.indexOf(child);
props.eventUpdate({ props.eventUpdate({
children: [ children: [
@ -80,7 +77,7 @@ export default defineComponent({
}); });
}; };
const appendBlock = (block: BlockData) => { const appendBlock = (block: Block) => {
props.eventUpdate({ props.eventUpdate({
children: [ children: [
...localData.children, ...localData.children,
@ -90,8 +87,8 @@ export default defineComponent({
activate(block.blockId); activate(block.blockId);
}; };
const insertBlock = (index: number, block: BlockData) => { const insertBlock = (index: number, block: Block) => {
context.emit('update', { props.eventUpdate({
children: [ children: [
...localData.children.slice(0, index + 1), ...localData.children.slice(0, index + 1),
block, block,
@ -118,13 +115,9 @@ export default defineComponent({
<SbBlock <SbBlock
key={child.blockId} key={child.blockId}
block={child} block={child}
{...{ eventUpdate={(updated: Block) => onChildUpdate(child, updated)}
on: { eventInsertBlock={(block: Block) => insertBlock(index, block)}
update: (updated: BlockData) => onChildUpdate(child, updated), eventAppendBlock={appendBlock}
'insert-block': (block: BlockData) => insertBlock(index, block),
'append-block': appendBlock,
},
}}
/> />
))} ))}

View file

@ -3,6 +3,6 @@ import { getDefaultData } from './util';
export default { export default {
name: 'sb-layout', name: 'sb-layout',
getDefaultData, getDefaultData,
edit: () => import('./edit.tsx'), edit: () => import('./edit'),
display: () => import('./display.tsx'), display: () => import('./display'),
}; };

View file

@ -1,11 +1,12 @@
import { import {
BlockProps, BlockProps,
Block,
BlockData, BlockData,
} from '@components/TreeElement'; } from '@components/TreeElement';
export interface LayoutData { export interface LayoutData {
orientation: string; orientation: string;
children: BlockData[]; children: Block[];
} }
export interface LayoutProps extends BlockProps { export interface LayoutProps extends BlockProps {

View file

@ -1,48 +1,36 @@
import { import {
defineComponent, defineComponent,
reactive,
computed, computed,
ref,
Ref,
onMounted,
watch,
PropType, PropType,
} from '@vue/composition-api'; } from '@vue/composition-api';
import { import {
model, model,
blockProps, blockProps,
useActivation, BlockProps,
} from '@components/TreeElement'; } from '@components/TreeElement';
import SbToolbar from '@internal/Toolbar';
import { import {
getDefaultData, getDefaultData,
ParagraphData, ParagraphData,
ParagraphProps,
} from './util'; } from './util';
import './style.scss'; import './style.scss';
interface ParagraphProps extends BlockProps {
data: ParagraphData;
}
export default defineComponent({ export default defineComponent({
name: 'sb-paragraph-edit', name: 'sb-paragraph-display',
model, model,
props: { props: {
...blockProps, ...blockProps,
data: { data: {
type: (null as unknown) as PropType<ParagraphData>, type: Object as PropType<ParagraphData>,
default: getDefaultData, default: getDefaultData,
}, },
eventUpdate: {
type: (Function as unknown) as (b?: ParagraphData) => void,
default: () => () => undefined,
},
eventInsertBlock: {
type: (Function as unknown) as (b?: ParagraphData) => void,
default: () => () => undefined,
},
}, },
setup(props: ParagraphProps, context) { setup(props: ParagraphProps, context) {
@ -51,8 +39,6 @@ export default defineComponent({
[`sb-paragraph_align-${props.data.align}`]: true, [`sb-paragraph_align-${props.data.align}`]: true,
})); }));
return () => ( return () => <p class={classes}>{props.data.value}</p>;
<p class={classes}>{props.data.value}</p>
);
}, },
}); });

View file

@ -11,6 +11,8 @@ import {
import { import {
model, model,
blockProps, blockProps,
BlockProps,
BlockData,
useActivation, useActivation,
} from '@components/TreeElement'; } from '@components/TreeElement';
@ -19,11 +21,16 @@ import SbToolbar from '@internal/Toolbar';
import { import {
getDefaultData, getDefaultData,
ParagraphData, ParagraphData,
ParagraphProps,
} from './util'; } from './util';
import './style.scss'; import './style.scss';
interface ParagraphProps extends BlockProps {
data: ParagraphData;
eventUpdate: (b?: ParagraphData) => void;
eventInsertBlock: (b?: BlockData) => void;
}
export default defineComponent({ export default defineComponent({
name: 'sb-paragraph-edit', name: 'sb-paragraph-edit',
@ -35,22 +42,16 @@ export default defineComponent({
type: (null as unknown) as PropType<ParagraphData>, type: (null as unknown) as PropType<ParagraphData>,
default: getDefaultData, default: getDefaultData,
}, },
eventUpdate: { eventUpdate: { type: Function, default: () => {} },
type: (Function as unknown) as (b?: ParagraphData) => void, eventInsertBlock: { type: Function, default: () => {} },
default: () => () => undefined,
},
eventInsertBlock: {
type: (Function as unknown) as (b?: ParagraphData) => void,
default: () => () => undefined,
},
}, },
setup(props: ParagraphProps, context) { setup(props: ParagraphProps) {
const localData = (reactive({ const localData = (reactive({
value: props.data.value, value: props.data.value,
align: props.data.align, align: props.data.align,
focused: false, focused: false,
}) as any) as { }) as unknown) as {
value: string; value: string;
align: string; align: string;
focused: boolean; focused: boolean;
@ -89,7 +90,10 @@ export default defineComponent({
})); }));
const setAlignment = ($event: Event) => { const setAlignment = ($event: Event) => {
props.eventUpdate({ align: ($event.target as HTMLSelectElement).value }); props.eventUpdate({
value: localData.value,
align: ($event.target as HTMLSelectElement).value,
});
}; };
const onFocus = () => { const onFocus = () => {
@ -100,6 +104,7 @@ export default defineComponent({
localData.focused = false; localData.focused = false;
props.eventUpdate({ props.eventUpdate({
value: localData.value, value: localData.value,
align: localData.align,
}); });
activate(null); activate(null);
}; };
@ -124,11 +129,7 @@ export default defineComponent({
<SbToolbar> <SbToolbar>
<select <select
value={localData.align} value={localData.align}
{...{ onChange={setAlignment}
on: {
change: setAlignment,
},
}}
> >
<option>left</option> <option>left</option>
<option>center</option> <option>center</option>
@ -139,14 +140,10 @@ export default defineComponent({
class="sb-paragraph__input" class="sb-paragraph__input"
ref={inputEl} ref={inputEl}
contenteditable contenteditable
{...{ onInput={onTextUpdate}
on: { onFocus={onFocus}
input: onTextUpdate, onBlur={onBlur}
focus: onFocus, onKeypress={onKeypress}
blur: onBlur,
keypress: onKeypress,
},
}}
></p> ></p>
</div> </div>
); );

View file

@ -1,14 +1,8 @@
import { BlockProps } from '@components/TreeElement';
export interface ParagraphData { export interface ParagraphData {
value: string; value: string;
align: string; align: string;
} }
export interface ParagraphProps extends BlockProps {
data: ParagraphData;
}
export const getDefaultData: () => ParagraphData = () => ({ export const getDefaultData: () => ParagraphData = () => ({
value: '', value: '',
align: 'left', align: 'left',