raonkeditor-react
Version:
React component for RAON K Editor
96 lines (86 loc) • 2.36 kB
JSX
import * as React from 'react';
import {
prefixEventName,
stripPrefix,
ComponentEventAction
} from 'raonkeditor-react';
import Sidebar from './Sidebar';
import RaonkEditor from './RaonkEditor';
const { version, useReducer, useRef } = React;
function App() {
const [ { events, uniqueName }, dispatch ] = useReducer( reducer, {
events: [],
uniqueName: getUniqueName()
} );
const start = useRef( new Date() );
const handleRemountClick = () => {
dispatch( { type: 'reMount', payload: getUniqueName() } );
};
return (
<div>
<section className="container">
<Sidebar events={events} start={start.current} />
<div className="paper flex-grow-3">
<RaonkEditor
debug={true}
key={uniqueName}
id={uniqueName}
mode='edit'
readOnly={false}
config={{ Lang: 'ko-kr', Width: '100%', Height: '250px', DevelopLangage: 'NONE' }}
componentUrl="/raonkeditor/js/raonkeditor.js"
runtimes="agent"
dispatchEvent={dispatch}
/>
<button className="btn" onClick={handleRemountClick}>
{'Re-mount component'}
</button>
</div>
</section>
<footer>{`Running React v${ version }`}</footer>
</div>
);
}
function reducer( state, action ) {
switch ( action.type ) {
case 'reMount':
return {
...state,
uniqueName: action.payload
};
/**
* Event names are prefixed in order to facilitate integration with dispatch from `useReducer`.
* Access them via `ComponentEventAction`.
*/
case ComponentEventAction.namespaceLoaded:
case ComponentEventAction.beforeLoad:
return {
...state,
events: state.events.concat( {
evtName: stripPrefix( action.type ),
componentName: '--',
date: new Date()
} )
};
case ComponentEventAction.loaded:
case ComponentEventAction.creationComplete:
case ComponentEventAction.destroy:
return {
...state,
events: state.events.concat( {
evtName: stripPrefix( action.type ),
componentName: action.payload.eventInfo ? action.payload.eventInfo.componentName : "",
date: new Date()
} )
};
default:
return state;
}
}
function getUniqueName() {
return Math.random()
.toString( 36 )
.replace( /[^a-z]+/g, '' )
.substr( 0, 5 );
}
export default App;