PromucFlow_constructor/app/client/src/pages/Editor/index.tsx

121 lines
3.2 KiB
TypeScript
Raw Normal View History

import React, { Component } from "react";
import { connect } from "react-redux";
import styled from "styled-components";
import Canvas from "./Canvas";
import {
WidgetCardProps,
WidgetProps,
WidgetDynamicProperty,
} from "../../widgets/BaseWidget";
import { AppState } from "../../reducers";
import { EditorReduxState } from "../../reducers/uiReducers/editorReducer";
import WidgetCardsPane from "./WidgetCardsPane";
import EditorHeader from "./EditorHeader";
import CanvasWidgetsNormalizer from "../../normalizers/CanvasWidgetsNormalizer";
import { ContainerWidgetProps } from "../../widgets/ContainerWidget";
import { fetchPage, updateWidget, savePage } from "../../actions/pageActions";
import { RenderModes } from "../../constants/WidgetConstants";
import EditorDragLayer from "./EditorDragLayer";
2019-08-20 13:19:19 +00:00
2019-08-29 11:22:09 +00:00
const CanvasContainer = styled.section`
2019-08-20 13:19:19 +00:00
height: 100%;
width: 100%;
position: relative;
overflow-x: hidden;
overflow-y: auto;
margin: 0px 10px;
2019-08-26 12:41:21 +00:00
&:before {
position: absolute;
2019-09-09 09:08:54 +00:00
top: 0;
right: 0;
bottom: 0;
left: 0;
2019-08-26 12:41:21 +00:00
z-index: 1000;
pointer-events: none;
}
2019-08-20 13:19:19 +00:00
`;
2019-08-26 12:41:21 +00:00
const EditorWrapper = styled.div`
display: flex;
flex-direction: row;
align-items: stretch;
justify-content: flex-start;
width: 100vw;
overflow: hidden;
padding: 10px;
height: calc(100vh - 60px);
2019-08-29 11:22:09 +00:00
`;
type EditorProps = {
layout: ContainerWidgetProps<WidgetProps> | any;
2019-08-29 11:22:09 +00:00
fetchCanvasWidgets: Function;
cards: { [id: string]: WidgetCardProps[] } | any;
updateWidgetProperty: Function;
savePageLayout: Function;
2019-09-02 10:58:11 +00:00
page: string;
currentPageId: string;
currentLayoutId: string;
};
2019-08-26 12:41:21 +00:00
2019-08-29 11:22:09 +00:00
class Editor extends Component<EditorProps> {
2019-08-26 12:41:21 +00:00
componentDidMount() {
this.props.fetchCanvasWidgets(this.props.currentPageId);
2019-08-26 12:41:21 +00:00
}
2019-08-29 11:22:09 +00:00
public render() {
return (
2019-08-26 12:41:21 +00:00
<React.Fragment>
2019-08-20 13:19:19 +00:00
<EditorHeader></EditorHeader>
2019-08-26 12:41:21 +00:00
<EditorWrapper>
<WidgetCardsPane cards={this.props.cards} />
<EditorDragLayer />
2019-08-29 11:22:09 +00:00
<CanvasContainer>
<Canvas
layout={{
...this.props.layout,
onPropertyChange: this.props.updateWidgetProperty,
}}
/>
2019-08-29 11:22:09 +00:00
</CanvasContainer>
2019-08-26 12:41:21 +00:00
</EditorWrapper>
</React.Fragment>
);
}
}
const mapStateToProps = (state: AppState): EditorReduxState => {
const layout = CanvasWidgetsNormalizer.denormalize(
state.ui.editor.pageWidgetId,
state.entities,
);
2019-08-26 12:41:21 +00:00
return {
cards: state.ui.widgetCardsPane.cards,
layout,
pageWidgetId: state.ui.editor.pageWidgetId,
currentPageId: state.ui.editor.currentPageId,
currentLayoutId: state.ui.editor.currentLayoutId,
};
};
2019-08-26 12:41:21 +00:00
const mapDispatchToProps = (dispatch: any) => {
return {
fetchCanvasWidgets: (pageId: string) =>
dispatch(fetchPage(pageId, RenderModes.CANVAS)),
updateWidgetProperty: (
propertyType: WidgetDynamicProperty,
widgetProps: WidgetProps,
payload: any,
) => dispatch(updateWidget(propertyType, widgetProps, payload)),
savePageLayout: (
pageId: string,
layoutId: string,
dsl: ContainerWidgetProps<WidgetProps>,
) => dispatch(savePage(pageId, layoutId, dsl)),
};
};
2019-08-26 12:41:21 +00:00
export default connect(
mapStateToProps,
mapDispatchToProps,
)(Editor);