schlechtenburg/docs/ExampleEditor.tsx

103 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-03-15 19:58:11 +00:00
import {
computed,
defineComponent,
reactive,
ref,
} from 'vue';
import { SbMain, IBlockData, SbMode } from '@schlechtenburg/core';
import SbLayout from '@schlechtenburg/layout';
import SbHeading from '@schlechtenburg/heading';
import SbParagraph from '@schlechtenburg/paragraph';
import SbImage from '@schlechtenburg/image';
2022-03-17 17:59:51 +00:00
import exampleData from './example-data';
import './ExampleEditor.scss';
2022-03-15 19:58:11 +00:00
export default defineComponent({
2022-03-17 17:59:51 +00:00
name: 'ExampleEditor',
2022-03-15 19:58:11 +00:00
setup() {
const activeTab = ref('edit');
2022-03-17 17:59:51 +00:00
const block: IBlockData<any> = reactive({ ...exampleData });
2022-09-05 19:12:20 +00:00
const dateID = +(new Date());
2022-03-15 19:58:11 +00:00
const displayedElement = computed(() => {
switch (activeTab.value) {
2022-03-20 13:49:44 +00:00
case 'data':
2022-09-05 19:12:20 +00:00
return <pre class="example-editor--json"><code>{ JSON.stringify(block, null, 2) }</code></pre>;
2022-03-20 13:49:44 +00:00
default:
2022-03-15 19:58:11 +00:00
return <SbMain
2022-03-17 17:59:51 +00:00
class="example-editor--sb"
2022-03-15 19:58:11 +00:00
block={block}
availableBlocks={[
SbLayout,
SbHeading,
SbImage,
SbParagraph,
]}
2022-03-20 13:49:44 +00:00
mode={activeTab.value as SbMode}
2022-09-05 19:12:20 +00:00
eventUpdate={(data:IBlockData<any>) => {
block.id = data.id;
block.name = data.name;
block.data = data.data;
}}
2022-03-15 19:58:11 +00:00
/>;
}
});
2022-09-05 19:12:20 +00:00
const onModeChange = ($event: Event) => {
activeTab.value = ($event.target as HTMLSelectElement).value;
};
2022-03-15 19:58:11 +00:00
return () => {
2022-03-17 17:59:51 +00:00
return <div class="example-editor">
2022-09-05 19:12:20 +00:00
<div class="example-editor--mode">
<label class={{
'example-editor--mode-tab': true,
'example-editor--mode-tab_checked': activeTab.value === SbMode.Edit,
}}>
<input
type="radio"
name={`example-editor-${dateID}`}
value={SbMode.Edit}
checked={activeTab.value === SbMode.Edit}
onChange={onModeChange}
/>
Editor mode
</label>
<label class={{
'example-editor--mode-tab': true,
'example-editor--mode-tab_checked': activeTab.value === SbMode.View,
}}>
<input
type="radio"
name={`example-editor-${dateID}`}
value={SbMode.View}
checked={activeTab.value === SbMode.View}
onChange={onModeChange}
/>
View mode
</label>
<label class={{
'example-editor--mode-tab': true,
'example-editor--mode-tab_checked': activeTab.value === "data",
}}>
<input
type="radio"
name={`example-editor-${dateID}`}
value="data"
checked={activeTab.value === "data"}
onChange={onModeChange}
/>
JSON Data structure
</label>
</div>
2022-03-15 19:58:11 +00:00
{displayedElement.value}
</div>;
};
},
});