feat: resource

This commit is contained in:
cha0s 2020-06-21 05:20:56 -05:00
parent 82470b9747
commit 063be814ca
11 changed files with 43 additions and 17 deletions

View File

@ -1,23 +1,24 @@
import './app.scss';
import {hot} from 'react-hot-loader';
import React, {useState} from 'react';
import TypeRenderersContext from '~/client/context/typeRenderers';
import {all as allTypeRenderers} from '~/client/type-renderers.scwp';
import Entity from './entity';
import TypeRenderersContext from './context/typeRenderers';
import Resource from './resource';
import {all as allTypeRenderers} from './type-renderers.scwp';
const typeRenderMap = () => Object.values(allTypeRenderers()).reduce((r, M) => {
const {default: {type, Component}} = M;
return {...r, [type]: Component};
}, {});
const Persea = () => {
const App = () => {
const [typeRenderers] = useState(typeRenderMap());
return (
<TypeRenderersContext.Provider value={typeRenderers}>
<Entity />
<Resource uri="/mama-kitty.entity.json" />
</TypeRenderersContext.Provider>
);
};
export default hot(module)(Persea);
export default hot(module)(App);

0
src/client/app.scss Normal file
View File

View File

@ -4,13 +4,8 @@ import {enableMapSet} from 'immer';
import React from 'react';
import {render} from 'react-dom';
import Persea from '~/client/components/persea';
import App from './app';
enableMapSet();
render(
(
<Persea />
),
document.getElementById('root'),
);
render(<App />, document.getElementById('root'));

20
src/client/resource.jsx Normal file
View File

@ -0,0 +1,20 @@
import './app.scss';
import React from 'react';
import {all} from './resources.scwp';
const resources = Object.values(all()).map((M) => M.default);
const Resource = (props) => {
const {
uri,
} = props;
const resource = resources.find((resource) => uri.match(resource.matcher));
const {Component} = resource
? resource
: {Component: () => <div className="unloadable">No loader for `<code>{uri}</code>`.</div>};
return <div className="resource"><Component uri={uri} /></div>;
};
export default Resource;

View File

@ -0,0 +1,3 @@
module.exports = function types(scwp) {
scwp.autoreg('resource');
};

View File

@ -6,12 +6,13 @@ import React from 'react';
import Traits from './traits';
const decorate = compose(
contempo(require('./entity.raw.scss')),
contempo(require('./index.raw.scss')),
);
const json = require('~/../fixtures/rock-projectile.entity.json');
const EntityComponent = () => {
const EntityComponent = (props) => {
const {uri} = props;
const entity = new Entity(json);
const {traits} = json;
return (

View File

@ -15,7 +15,7 @@ import {
import {createSelector} from '@reduxjs/toolkit';
import {deregisterHooks, registerHooks} from 'scwp';
import Value from './types/value.type-renderer';
import Value from '~/client/components/types/value.type-renderer';
const SCROLL_MAG = 80;

View File

@ -0,0 +1,6 @@
import EntityComponent from './component';
export default {
matcher: /\.entity\.json$/,
Component: EntityComponent,
};

View File