Add ScrollSync Button to Preview UI (#693)

* Add ScrollSync Button

* Create <ToggleButton /> component && update CSS

* Swap Icons && fix formatting
This commit is contained in:
Eric Jinks
2017-10-27 05:26:42 +10:00
committed by Benaiah Mischenko
parent c60f73a0d8
commit b2d93ef729
3 changed files with 41 additions and 12 deletions

View File

@ -3,15 +3,21 @@
height: 100%; height: 100%;
} }
.nc-entryEditor-previewToggle { .nc-entryEditor-controlPaneButtons {
position: absolute; position: absolute;
top: 8px; top: 8px;
right: 40px; right: 20px;
z-index: 1000; z-index: 1000;
opacity: 0.8; opacity: 0.8;
display: flex;
justify-content: flex-end;
} }
.nc-entryEditor-previewToggleShow { .nc-entryEditor-toggleButton {
margin-left: 5px;
}
.nc-entryEditor-toggleButtonShow {
right: 60px; right: 60px;
} }

View File

@ -11,11 +11,13 @@ import Toolbar from './EntryEditorToolbar';
import { StickyContext } from '../UI/Sticky/Sticky'; import { StickyContext } from '../UI/Sticky/Sticky';
const PREVIEW_VISIBLE = 'cms.preview-visible'; const PREVIEW_VISIBLE = 'cms.preview-visible';
const SCROLL_SYNC_ENABLED = 'cms.scroll-sync-enabled';
class EntryEditor extends Component { class EntryEditor extends Component {
state = { state = {
showEventBlocker: false, showEventBlocker: false,
previewVisible: localStorage.getItem(PREVIEW_VISIBLE) !== "false", previewVisible: localStorage.getItem(PREVIEW_VISIBLE) !== "false",
scrollSyncEnabled: localStorage.getItem(SCROLL_SYNC_ENABLED) !== "false",
}; };
handleSplitPaneDragStart = () => { handleSplitPaneDragStart = () => {
@ -37,6 +39,12 @@ class EntryEditor extends Component {
localStorage.setItem(PREVIEW_VISIBLE, newPreviewVisible); localStorage.setItem(PREVIEW_VISIBLE, newPreviewVisible);
}; };
handleToggleScrollSync = () => {
const newScrollSyncEnabled = !this.state.scrollSyncEnabled;
this.setState({ scrollSyncEnabled: newScrollSyncEnabled });
localStorage.setItem(SCROLL_SYNC_ENABLED, newScrollSyncEnabled);
};
render() { render() {
const { const {
collection, collection,
@ -55,15 +63,15 @@ class EntryEditor extends Component {
onCancelEdit, onCancelEdit,
} = this.props; } = this.props;
const { previewVisible, showEventBlocker } = this.state; const { previewVisible, scrollSyncEnabled, showEventBlocker } = this.state;
const collectionPreviewEnabled = collection.getIn(['editor', 'preview'], true); const collectionPreviewEnabled = collection.getIn(['editor', 'preview'], true);
const togglePreviewButton = ( const ToggleButton = ({ icon, onClick }) => (
<Button <Button
className={classnames('nc-entryEditor-previewToggle', { previewVisible: 'nc-entryEditor-previewToggleShow' })} className={classnames('nc-entryEditor-toggleButton', { previewVisible: 'nc-entryEditor-toggleButtonShow' })}
onClick={this.handleTogglePreview} icon={icon}
icon={previewVisible ? 'visibility_off' : 'visibility'} onClick={onClick}
floating floating
mini mini
/> />
@ -71,10 +79,23 @@ class EntryEditor extends Component {
const editor = ( const editor = (
<StickyContext <StickyContext
className={classnames('nc-entryEditor-controlPane', { ['nc-entryEditor-blocker']: showEventBlocker })} className={classnames('nc-entryEditor-controlPane', { 'nc-entryEditor-blocker': showEventBlocker })}
registerListener={fn => this.updateStickyContext = fn} registerListener={fn => this.updateStickyContext = fn}
> >
{ collectionPreviewEnabled ? togglePreviewButton : null } { collectionPreviewEnabled ? (
<div className="nc-entryEditor-controlPaneButtons">
{ previewVisible && (
<ToggleButton
icon={scrollSyncEnabled ? 'sync' : 'sync_disabled'}
onClick={this.handleToggleScrollSync}
/>
) }
<ToggleButton
icon={previewVisible ? 'visibility_off' : 'visibility'}
onClick={this.handleTogglePreview}
/>
</div>
) : null }
<ControlPane <ControlPane
collection={collection} collection={collection}
entry={entry} entry={entry}
@ -92,7 +113,7 @@ class EntryEditor extends Component {
); );
const editorWithPreview = ( const editorWithPreview = (
<ScrollSync> <ScrollSync enabled={this.state.scrollSyncEnabled}>
<div className="nc-entryEditor-container"> <div className="nc-entryEditor-container">
<SplitPane <SplitPane
defaultSize="50%" defaultSize="50%"
@ -101,7 +122,7 @@ class EntryEditor extends Component {
onChange={this.updateStickyContext} onChange={this.updateStickyContext}
> >
<ScrollSyncPane>{editor}</ScrollSyncPane> <ScrollSyncPane>{editor}</ScrollSyncPane>
<div className={classnames('nc-entryEditor-previewPane', { ['nc-entryEditor-blocker']: showEventBlocker })}> <div className={classnames('nc-entryEditor-previewPane', { 'nc-entryEditor-blocker': showEventBlocker })}>
<PreviewPane <PreviewPane
collection={collection} collection={collection}
entry={entry} entry={entry}

View File

@ -6,6 +6,7 @@ export default class ScrollSync extends Component {
static propTypes = { static propTypes = {
children: PropTypes.element.isRequired, children: PropTypes.element.isRequired,
enabled: PropTypes.bool.isRequired,
}; };
static childContextTypes = { static childContextTypes = {
@ -51,6 +52,7 @@ export default class ScrollSync extends Component {
}; };
handlePaneScroll = (node) => { handlePaneScroll = (node) => {
if (!this.props.enabled) return false;
// const node = evt.target // const node = evt.target
window.requestAnimationFrame(() => { window.requestAnimationFrame(() => {
this.syncScrollPositions(node); this.syncScrollPositions(node);