Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ScrollSync Button to Preview UI #693

Merged
merged 3 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/components/EntryEditor/EntryEditor.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
height: 100%;
}

.nc-entryEditor-previewToggle {
.nc-entryEditor-controlPaneButtons {
position: absolute;
top: 8px;
right: 40px;
right: 20px;
z-index: 1000;
opacity: 0.8;
display: flex;
justify-content: flex-end;
}

.nc-entryEditor-previewToggleShow {
.nc-entryEditor-toggleButton {
margin-left: 5px;
}

.nc-entryEditor-toggleButtonShow {
right: 60px;
}

Expand Down
39 changes: 30 additions & 9 deletions src/components/EntryEditor/EntryEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import Toolbar from './EntryEditorToolbar';
import { StickyContext } from '../UI/Sticky/Sticky';

const PREVIEW_VISIBLE = 'cms.preview-visible';
const SCROLL_SYNC_ENABLED = 'cms.scroll-sync-enabled';

class EntryEditor extends Component {
state = {
showEventBlocker: false,
previewVisible: localStorage.getItem(PREVIEW_VISIBLE) !== "false",
scrollSyncEnabled: localStorage.getItem(SCROLL_SYNC_ENABLED) !== "false",
};

handleSplitPaneDragStart = () => {
Expand All @@ -37,6 +39,12 @@ class EntryEditor extends Component {
localStorage.setItem(PREVIEW_VISIBLE, newPreviewVisible);
};

handleToggleScrollSync = () => {
const newScrollSyncEnabled = !this.state.scrollSyncEnabled;
this.setState({ scrollSyncEnabled: newScrollSyncEnabled });
localStorage.setItem(SCROLL_SYNC_ENABLED, newScrollSyncEnabled);
};

render() {
const {
collection,
Expand All @@ -55,26 +63,39 @@ class EntryEditor extends Component {
onCancelEdit,
} = this.props;

const { previewVisible, showEventBlocker } = this.state;
const { previewVisible, scrollSyncEnabled, showEventBlocker } = this.state;

const collectionPreviewEnabled = collection.getIn(['editor', 'preview'], true);

const togglePreviewButton = (
const ToggleButton = ({ icon, onClick }) => (
<Button
className={classnames('nc-entryEditor-previewToggle', { previewVisible: 'nc-entryEditor-previewToggleShow' })}
onClick={this.handleTogglePreview}
icon={previewVisible ? 'visibility_off' : 'visibility'}
className={classnames('nc-entryEditor-toggleButton', { previewVisible: 'nc-entryEditor-toggleButtonShow' })}
icon={icon}
onClick={onClick}
floating
mini
/>
);

const editor = (
<StickyContext
className={classnames('nc-entryEditor-controlPane', { ['nc-entryEditor-blocker']: showEventBlocker })}
className={classnames('nc-entryEditor-controlPane', { 'nc-entryEditor-blocker': showEventBlocker })}
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
collection={collection}
entry={entry}
Expand All @@ -92,7 +113,7 @@ class EntryEditor extends Component {
);

const editorWithPreview = (
<ScrollSync>
<ScrollSync enabled={this.state.scrollSyncEnabled}>
<div className="nc-entryEditor-container">
<SplitPane
defaultSize="50%"
Expand All @@ -101,7 +122,7 @@ class EntryEditor extends Component {
onChange={this.updateStickyContext}
>
<ScrollSyncPane>{editor}</ScrollSyncPane>
<div className={classnames('nc-entryEditor-previewPane', { ['nc-entryEditor-blocker']: showEventBlocker })}>
<div className={classnames('nc-entryEditor-previewPane', { 'nc-entryEditor-blocker': showEventBlocker })}>
<PreviewPane
collection={collection}
entry={entry}
Expand Down
2 changes: 2 additions & 0 deletions src/components/ScrollSync/ScrollSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class ScrollSync extends Component {

static propTypes = {
children: PropTypes.element.isRequired,
enabled: PropTypes.bool.isRequired,
};

static childContextTypes = {
Expand Down Expand Up @@ -51,6 +52,7 @@ export default class ScrollSync extends Component {
};

handlePaneScroll = (node) => {
if (!this.props.enabled) return false;
// const node = evt.target
window.requestAnimationFrame(() => {
this.syncScrollPositions(node);
Expand Down