schlechtenburg/packages/docs/lib/ExampleEditor.tsx

66 lines
1.7 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-03-15 19:58:11 +00:00
const displayedElement = computed(() => {
switch (activeTab.value) {
2022-03-20 13:49:44 +00:00
case 'data':
return <pre><code>{ JSON.stringify(block, null, 2) }</code></pre>;
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-03-15 19:58:11 +00:00
/>;
}
});
return () => {
2022-03-17 17:59:51 +00:00
return <div class="example-editor">
<h2 class="example-editor--title">
<span>Try it yourself</span>
<select
class="example-editor--mode"
value={activeTab.value}
onChange={($event: Event) => {
activeTab.value = ($event.target as HTMLSelectElement).value;
}}
>
2022-03-20 13:49:44 +00:00
<option value={SbMode.Edit}>Editor mode</option>
<option value={SbMode.View}>Viewer mode</option>
2022-03-17 17:59:51 +00:00
<option value="data">JSON Data structure</option>
</select>
</h2>
2022-03-15 19:58:11 +00:00
{displayedElement.value}
</div>;
};
},
});