PromucFlow_constructor/app/client/src/components/editorComponents/DynamicAutocompleteInput.tsx

108 lines
3.1 KiB
TypeScript
Raw Normal View History

2019-12-30 07:35:16 +00:00
import React, { Component } from "react";
2019-12-06 13:16:08 +00:00
import { connect } from "react-redux";
import { AppState } from "reducers";
import styled from "styled-components";
2019-12-30 07:35:16 +00:00
import CodeMirror, { EditorConfiguration } from "codemirror";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/monokai.css";
import "codemirror/addon/hint/show-hint";
import "codemirror/addon/hint/show-hint.css";
import "codemirror/addon/hint/javascript-hint";
2019-12-06 13:16:08 +00:00
import {
getNameBindingsWithData,
2019-12-06 13:16:08 +00:00
NameBindingsWithData,
} from "selectors/nameBindingsWithDataSelector";
2019-12-30 07:35:16 +00:00
require("codemirror/mode/javascript/javascript");
2019-12-06 13:16:08 +00:00
2019-12-30 07:35:16 +00:00
const Wrapper = styled.div<{ height?: number; theme?: "LIGHT" | "DARK" }>`
border: ${props => props.theme !== "DARK" && "1px solid #d0d7dd"};
border-radius: 4px;
2019-12-06 13:16:08 +00:00
display: flex;
flex: 1;
flex-direction: column;
position: relative;
text-transform: none;
2019-12-30 07:35:16 +00:00
min-height: 32px;
height: ${props => (props.height ? `${props.height}px` : "32px")};
2019-12-06 13:16:08 +00:00
`;
interface ReduxStateProps {
dynamicData: NameBindingsWithData;
}
2019-12-30 07:35:16 +00:00
type Props = ReduxStateProps & {
input: {
value: string;
onChange?: (value: string) => void;
};
theme?: "LIGHT" | "DARK";
2019-12-06 13:16:08 +00:00
};
2019-12-30 07:35:16 +00:00
class DynamicAutocompleteInput extends Component<Props> {
textArea = React.createRef<HTMLTextAreaElement>();
editor: any;
2019-12-06 13:16:08 +00:00
componentDidMount(): void {
2019-12-30 07:35:16 +00:00
if (this.textArea.current) {
const options: EditorConfiguration = {};
if (this.props.theme === "DARK") options.theme = "monokai";
if (!this.props.input.onChange) options.readOnly = true;
this.editor = CodeMirror.fromTextArea(this.textArea.current, {
mode: { name: "javascript", globalVars: true },
value: this.props.input.value,
tabSize: 2,
indentWithTabs: true,
lineWrapping: true,
extraKeys: { "Ctrl-Space": "autocomplete" },
showHint: true,
...options,
});
this.editor.on("change", this.handleChange);
this.editor.on("keyup", this.handleAutocompleteVisibility);
this.editor.setOption("hintOptions", {
completeSingle: false,
globalScope: this.props.dynamicData,
2019-12-06 13:16:08 +00:00
});
}
2019-12-30 07:35:16 +00:00
}
2019-12-06 13:16:08 +00:00
2019-12-30 07:35:16 +00:00
componentDidUpdate(): void {
if (this.editor) {
const editorValue = this.editor.getValue();
const inputValue = this.props.input.value;
if (inputValue && inputValue !== editorValue) {
this.editor.setValue(inputValue);
2019-12-06 13:16:08 +00:00
}
}
2019-12-30 07:35:16 +00:00
}
handleChange = () => {
const value = this.editor.getValue();
if (this.props.input.onChange) {
this.props.input.onChange(value);
2019-12-06 13:16:08 +00:00
}
};
2019-12-30 07:35:16 +00:00
handleAutocompleteVisibility = (cm: any, event: any) => {
if (!cm.state.completionActive && event.keyCode !== 13) {
cm.showHint(cm);
2019-12-06 13:16:08 +00:00
}
};
render() {
2019-12-30 07:35:16 +00:00
const { input, theme } = this.props;
const height = this.editor ? this.editor.doc.height + 20 : null;
2019-12-06 13:16:08 +00:00
return (
2019-12-30 07:35:16 +00:00
<Wrapper height={height} theme={theme}>
<textarea ref={this.textArea} defaultValue={input.value} />
2019-12-06 13:16:08 +00:00
</Wrapper>
);
}
}
const mapStateToProps = (state: AppState): ReduxStateProps => ({
dynamicData: getNameBindingsWithData(state),
2019-12-06 13:16:08 +00:00
});
export default connect(mapStateToProps)(DynamicAutocompleteInput);