ghost-portal/packages/ui-app/src/Editor.tsx
Jaco Greeff 0e2265ff70
Some initial pre-eslint cleanups (#1383)
* Some initial cleanups

* Make it lint

* Small s/type/interface/ additions

* Small minor s/type/interface/

* More type -> interface

* Add lint:new rule

* Some small updates

* Cleanup dangling tslint  issues

* Small updates

* ui-signer cleanups

* ui-reactive updated

* Make lint:new run

* Mini ui-params cleanups

* Some additions

* Tiny... and done for now.

* 1950 problems (1241 errors, 709 warnings)

* 1829 problems (1181 errors, 648 warnings)

* 1674 problems (1181 errors, 493 warnings) (no any... for now)

* Small additions.

* Array<T> -> T[] (all)
2019-07-14 22:27:12 +02:00

91 lines
2.0 KiB
TypeScript

// Copyright 2017-2019 @polkadot/ui-app authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BareProps } from '@polkadot/ui-app/types';
import CodeFlask from 'codeflask';
import React from 'react';
import styled from 'styled-components';
import { classes } from '@polkadot/ui-app/util';
type Props = BareProps & {
code: string,
isValid?: boolean,
onEdit: (code: string) => void
};
/**
* @name Editor
* @summary A code editor based on the codeflask npm module
* @description It allows to live-edit code examples and JSON files.
*
* @example
* <BR>
*
* ```javascript
* import {Editor} from '@polkadot/ui-app';
*
* <Editor
* className={string} // optional
* code={string}
* isValid={boolean}, // optional
* onEdit={() => callbackFunction}
* />
* ```
*/
class Editor extends React.Component<Props> {
private id: string = `flask-${Date.now()}`;
private editor: any;
componentDidMount () {
const { onEdit } = this.props;
this.editor = new CodeFlask(`#${this.id}`, {
language: 'js',
lineNumbers: true
});
this.editor.updateCode(this.props.code);
this.editor.editorRoot.addEventListener('keydown', () => {
this.editor.onUpdate(onEdit);
});
}
shouldComponentUpdate (nextProps: Props): boolean {
return (
nextProps.code !== this.props.code
);
}
componentDidUpdate () {
this.editor.updateCode(this.props.code);
}
public render (): React.ReactNode {
const { className, isValid } = this.props;
return (
<div
className={classes('ui-Editor', className, isValid === false ? 'invalid' : '')}
id={this.id}
/>
);
}
}
export default styled(Editor)`
.codeflask {
border: 1px solid rgba(34,36,38,.15);
background: transparent;
}
&.invalid {
.codeflask {
background-color: #fff6f6;
border-color: #e0b4b4;
}
}
`;