2016-12-12 19:23:52 -02:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-06-16 19:20:36 -03:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-12-12 19:23:52 -02:00
|
|
|
import SplitPane from 'react-split-pane';
|
2017-03-17 15:12:15 -04:00
|
|
|
import Button from 'react-toolbox/lib/button';
|
2017-04-11 18:41:28 -04:00
|
|
|
import classnames from 'classnames';
|
2016-10-04 17:58:26 +02:00
|
|
|
import { ScrollSync, ScrollSyncPane } from '../ScrollSync';
|
2016-09-29 19:02:28 +02:00
|
|
|
import ControlPane from '../ControlPanel/ControlPane';
|
2016-09-30 16:25:15 +02:00
|
|
|
import PreviewPane from '../PreviewPane/PreviewPane';
|
2016-10-13 14:31:44 +02:00
|
|
|
import Toolbar from './EntryEditorToolbar';
|
2017-03-23 17:15:48 -04:00
|
|
|
import { StickyContext } from '../UI/Sticky/Sticky';
|
2016-09-11 23:08:18 +02:00
|
|
|
import styles from './EntryEditor.css';
|
2017-03-21 14:52:33 -04:00
|
|
|
import stickyStyles from '../UI/Sticky/Sticky.css';
|
2016-05-30 16:55:32 -07:00
|
|
|
|
2017-04-05 12:06:12 -04:00
|
|
|
const PREVIEW_VISIBLE = 'cms.preview-visible';
|
2017-03-17 15:12:15 -04:00
|
|
|
|
2016-12-12 19:23:52 -02:00
|
|
|
class EntryEditor extends Component {
|
|
|
|
state = {
|
|
|
|
showEventBlocker: false,
|
2017-04-05 12:06:12 -04:00
|
|
|
previewVisible: localStorage.getItem(PREVIEW_VISIBLE) !== "false",
|
2016-12-12 19:23:52 -02:00
|
|
|
};
|
|
|
|
|
|
|
|
handleSplitPaneDragStart = () => {
|
|
|
|
this.setState({ showEventBlocker: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSplitPaneDragFinished = () => {
|
|
|
|
this.setState({ showEventBlocker: false });
|
|
|
|
};
|
|
|
|
|
2017-01-13 19:30:40 -02:00
|
|
|
handleOnPersist = () => {
|
|
|
|
this.controlPaneRef.validate();
|
|
|
|
this.props.onPersist();
|
|
|
|
};
|
|
|
|
|
2017-03-17 15:12:15 -04:00
|
|
|
handleTogglePreview = () => {
|
2017-04-05 12:06:12 -04:00
|
|
|
const newPreviewVisible = !this.state.previewVisible;
|
|
|
|
this.setState({ previewVisible: newPreviewVisible });
|
|
|
|
localStorage.setItem(PREVIEW_VISIBLE, newPreviewVisible);
|
2017-03-17 15:12:15 -04:00
|
|
|
};
|
|
|
|
|
2016-12-12 19:23:52 -02:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
collection,
|
|
|
|
entry,
|
|
|
|
fields,
|
2016-12-29 17:18:24 -02:00
|
|
|
fieldsMetaData,
|
2017-01-13 19:30:40 -02:00
|
|
|
fieldsErrors,
|
2017-01-10 22:23:22 -02:00
|
|
|
getAsset,
|
2016-12-12 19:23:52 -02:00
|
|
|
onChange,
|
2017-09-02 19:24:37 -06:00
|
|
|
hasChanged,
|
2017-07-21 23:40:33 -07:00
|
|
|
showDelete,
|
|
|
|
onDelete,
|
2017-01-13 19:30:40 -02:00
|
|
|
onValidate,
|
2017-01-10 22:23:22 -02:00
|
|
|
onAddAsset,
|
|
|
|
onRemoveAsset,
|
2016-12-12 19:23:52 -02:00
|
|
|
onCancelEdit,
|
|
|
|
} = this.props;
|
|
|
|
|
2017-04-11 18:41:28 -04:00
|
|
|
const { previewVisible, showEventBlocker } = this.state;
|
2016-12-12 19:23:52 -02:00
|
|
|
|
2017-04-13 00:56:15 +01:00
|
|
|
const collectionPreviewEnabled = collection.getIn(['editor', 'preview'], true);
|
|
|
|
|
|
|
|
const togglePreviewButton = (
|
2017-04-11 18:41:28 -04:00
|
|
|
<Button
|
|
|
|
className={classnames(styles.previewToggle, { previewVisible: styles.previewToggleShow })}
|
|
|
|
onClick={this.handleTogglePreview}
|
|
|
|
icon={previewVisible ? 'visibility_off' : 'visibility'}
|
|
|
|
floating
|
|
|
|
mini
|
|
|
|
/>
|
2017-04-13 00:56:15 +01:00
|
|
|
);
|
|
|
|
|
2017-03-17 15:12:15 -04:00
|
|
|
const editor = (
|
2017-04-12 10:36:08 -04:00
|
|
|
<StickyContext
|
|
|
|
className={classnames(styles.controlPane, { [styles.blocker]: showEventBlocker })}
|
|
|
|
registerListener={fn => this.updateStickyContext = fn}
|
|
|
|
>
|
2017-04-13 00:56:15 +01:00
|
|
|
{ collectionPreviewEnabled ? togglePreviewButton : null }
|
2017-03-17 15:12:15 -04:00
|
|
|
<ControlPane
|
|
|
|
collection={collection}
|
|
|
|
entry={entry}
|
|
|
|
fields={fields}
|
|
|
|
fieldsMetaData={fieldsMetaData}
|
|
|
|
fieldsErrors={fieldsErrors}
|
|
|
|
getAsset={getAsset}
|
|
|
|
onChange={onChange}
|
|
|
|
onValidate={onValidate}
|
|
|
|
onAddAsset={onAddAsset}
|
|
|
|
onRemoveAsset={onRemoveAsset}
|
|
|
|
ref={c => this.controlPaneRef = c} // eslint-disable-line
|
|
|
|
/>
|
2017-03-23 17:15:48 -04:00
|
|
|
</StickyContext>
|
2017-03-17 15:12:15 -04:00
|
|
|
);
|
2016-12-12 19:23:52 -02:00
|
|
|
|
2017-04-04 23:04:35 -04:00
|
|
|
const editorWithPreview = (
|
2017-03-17 15:12:15 -04:00
|
|
|
<ScrollSync>
|
|
|
|
<div className={styles.container}>
|
|
|
|
<SplitPane
|
|
|
|
defaultSize="50%"
|
|
|
|
onDragStarted={this.handleSplitPaneDragStart}
|
|
|
|
onDragFinished={this.handleSplitPaneDragFinished}
|
2017-04-12 10:36:08 -04:00
|
|
|
onChange={this.updateStickyContext}
|
2017-03-17 15:12:15 -04:00
|
|
|
>
|
2017-04-05 12:06:12 -04:00
|
|
|
<ScrollSyncPane>{editor}</ScrollSyncPane>
|
2017-04-11 18:41:28 -04:00
|
|
|
<div className={classnames(styles.previewPane, { [styles.blocker]: showEventBlocker })}>
|
2017-03-17 15:12:15 -04:00
|
|
|
<PreviewPane
|
|
|
|
collection={collection}
|
|
|
|
entry={entry}
|
|
|
|
fields={fields}
|
|
|
|
fieldsMetaData={fieldsMetaData}
|
|
|
|
getAsset={getAsset}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</SplitPane>
|
|
|
|
</div>
|
|
|
|
</ScrollSync>
|
|
|
|
);
|
2017-04-05 12:06:12 -04:00
|
|
|
|
2017-04-14 21:30:49 +01:00
|
|
|
const editorWithoutPreview = (
|
|
|
|
<div className={styles.noPreviewEditorContainer}>
|
|
|
|
{editor}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2017-03-17 15:12:15 -04:00
|
|
|
return (
|
|
|
|
<div className={styles.root}>
|
2017-04-14 21:30:49 +01:00
|
|
|
{ collectionPreviewEnabled && this.state.previewVisible ? editorWithPreview : editorWithoutPreview }
|
2016-12-12 19:23:52 -02:00
|
|
|
<div className={styles.footer}>
|
|
|
|
<Toolbar
|
|
|
|
isPersisting={entry.get('isPersisting')}
|
2017-01-13 19:30:40 -02:00
|
|
|
onPersist={this.handleOnPersist}
|
2016-12-12 19:23:52 -02:00
|
|
|
onCancelEdit={onCancelEdit}
|
2017-07-21 23:40:33 -07:00
|
|
|
onDelete={onDelete}
|
|
|
|
showDelete={showDelete}
|
2017-09-02 19:24:37 -06:00
|
|
|
hasChanged={hasChanged}
|
2016-12-12 19:23:52 -02:00
|
|
|
/>
|
2016-09-12 11:14:21 +02:00
|
|
|
</div>
|
2016-05-30 16:55:32 -07:00
|
|
|
</div>
|
2016-12-12 19:23:52 -02:00
|
|
|
);
|
|
|
|
}
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
2016-06-05 01:52:18 -07:00
|
|
|
|
2016-06-16 19:20:36 -03:00
|
|
|
EntryEditor.propTypes = {
|
|
|
|
collection: ImmutablePropTypes.map.isRequired,
|
|
|
|
entry: ImmutablePropTypes.map.isRequired,
|
2016-10-21 20:42:14 -02:00
|
|
|
fields: ImmutablePropTypes.list.isRequired,
|
2016-12-29 17:18:24 -02:00
|
|
|
fieldsMetaData: ImmutablePropTypes.map.isRequired,
|
2017-01-13 19:30:40 -02:00
|
|
|
fieldsErrors: ImmutablePropTypes.map.isRequired,
|
2017-01-10 22:23:22 -02:00
|
|
|
getAsset: PropTypes.func.isRequired,
|
|
|
|
onAddAsset: PropTypes.func.isRequired,
|
2016-06-16 19:20:36 -03:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-01-13 19:30:40 -02:00
|
|
|
onValidate: PropTypes.func.isRequired,
|
2016-06-16 19:20:36 -03:00
|
|
|
onPersist: PropTypes.func.isRequired,
|
2017-09-02 19:24:37 -06:00
|
|
|
hasChanged: PropTypes.bool.isRequired,
|
2017-07-21 23:40:33 -07:00
|
|
|
showDelete: PropTypes.bool.isRequired,
|
|
|
|
onDelete: PropTypes.func.isRequired,
|
2017-01-10 22:23:22 -02:00
|
|
|
onRemoveAsset: PropTypes.func.isRequired,
|
2016-10-11 18:24:55 +02:00
|
|
|
onCancelEdit: PropTypes.func.isRequired,
|
2016-06-16 19:20:36 -03:00
|
|
|
};
|
2016-12-12 19:23:52 -02:00
|
|
|
|
|
|
|
export default EntryEditor;
|