Skip to content

Commit

Permalink
Merge pull request #56 from magjac/visual-feedback-of-focus
Browse files Browse the repository at this point in the history
Visual feedback of focus
  • Loading branch information
magjac committed Sep 19, 2018
2 parents 0f2f06e + fdb0da4 commit 09175a3
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/FormatDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class FormatDrawer extends React.Component {
handleClick = () => {
this.setColorColorPickerOpen(false);
this.setFillColorColorPickerOpen(false);
this.props.onClick();
};

handleDrawerClose = () => {
Expand Down
17 changes: 15 additions & 2 deletions src/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class Graph extends React.Component {
}

handleClickDiv(d, i, nodes) {
this.props.onFocus();
document.activeElement.blur();
var event = d3_event;
event.preventDefault();
Expand All @@ -267,6 +268,9 @@ class Graph extends React.Component {
}

handleKeyDownDocument(d, i, nodes) {
if (!this.props.hasFocus) {
return;
}
var event = d3_event;
if (event.target.nodeName !== 'BODY') {
return;
Expand Down Expand Up @@ -343,6 +347,7 @@ class Graph extends React.Component {
}

handleClickNode(d, i, nodes) {
this.props.onFocus();
document.activeElement.blur();
var event = d3_event;
event.preventDefault();
Expand All @@ -354,6 +359,7 @@ class Graph extends React.Component {
}

handleDblClickNode(d, i, nodes) {
this.props.onFocus();
document.activeElement.blur();
var event = d3_event;
event.preventDefault();
Expand All @@ -373,6 +379,7 @@ class Graph extends React.Component {
}

handleRightClickNode(d, i, nodes) {
this.props.onFocus();
document.activeElement.blur();
var event = d3_event;
event.preventDefault();
Expand All @@ -394,6 +401,7 @@ class Graph extends React.Component {
}

handleClickEdge(d, i, nodes) {
this.props.onFocus();
document.activeElement.blur();
var event = d3_event;
event.preventDefault();
Expand All @@ -403,6 +411,7 @@ class Graph extends React.Component {
}

handleRightClickDiv(d, i, nodes) {
this.props.onFocus();
document.activeElement.blur();
var event = d3_event;
event.preventDefault();
Expand All @@ -411,6 +420,7 @@ class Graph extends React.Component {
}

handleMouseDownSvg(d, i, nodes) {
this.props.onFocus();
document.activeElement.blur();
var event = d3_event;
if (event.which !== 1) {
Expand Down Expand Up @@ -455,6 +465,7 @@ class Graph extends React.Component {
}

handleMouseUpSvg(d, i, nodes) {
this.props.onFocus();
document.activeElement.blur();
var event = d3_event;
if (event.which === 1 && this.selectArea) {
Expand Down Expand Up @@ -543,8 +554,10 @@ class Graph extends React.Component {
unSelectComponents() {
this.selectRects.remove();
this.selectRects = d3_select(null);
this.selectedComponents = d3_selectAll(null);
this.props.onSelect([]);
if (this.selectedComponents.size() > 0) {
this.selectedComponents = d3_selectAll(null);
this.props.onSelect([]);
}
}

deleteSelectedComponents() {
Expand Down
12 changes: 10 additions & 2 deletions src/InsertPanels.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ class InsertPanels extends React.Component {
expanded: null,
};

handleClick = () => {
this.props.onClick();
};

handleChange = panel => (event, expanded) => {
this.setState({
expanded: expanded ? panel : false,
Expand All @@ -153,9 +157,13 @@ class InsertPanels extends React.Component {
const { expanded } = this.state;

return (
<div className={classes.root}>
<div className={classes.root} onClick={this.handleClick}>
{nodeShapeCategories.map((nodeShapeCategory) =>
<ExpansionPanel key={nodeShapeCategory.name} expanded={expanded === nodeShapeCategory} onChange={this.handleChange(nodeShapeCategory)}>
<ExpansionPanel
key={nodeShapeCategory.name}
expanded={expanded === nodeShapeCategory}
onChange={this.handleChange(nodeShapeCategory)}
>
<ExpansionPanelSummary expandIcon={<ExpandMoreIcon />}>
<Typography className={classes.heading}>{nodeShapeCategory.name}</Typography>
</ExpansionPanelSummary>
Expand Down
2 changes: 2 additions & 0 deletions src/TextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class TextEditor extends React.Component {
onChange={this.handleChange}
onBeforeLoad={this.handleBeforeLoad}
onLoad={this.handleLoad}
onFocus={this.props.onFocus}
onBlur={this.props.onBlur}
name="UNIQUE_ID_OF_DIV"
value={this.props.dotSrc}
// viewport height - app bar - 2 * padding
Expand Down
95 changes: 86 additions & 9 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const styles = theme => ({
}
});

const defaultElevation = 2;
const focusedElevation = 8;

class Index extends React.Component {

constructor(props) {
Expand Down Expand Up @@ -69,6 +72,14 @@ class Index extends React.Component {
};
}

componentDidMount() {
document.onblur = () => {
// Needed when the user clicks outside the document,
// e.g. the browser address bar
this.setFocus(null);
}
}

setPersistentState = (updater) => {
this.setState(updater, function (updater) {
if (typeof updater === 'function') {
Expand Down Expand Up @@ -129,13 +140,15 @@ class Index extends React.Component {
});
}

handleInsertClick = () => {
handleInsertButtonClick = () => {
this.setFocusIf('insertPanelsAreOpen', null, 'InsertPanels')
this.setPersistentState({
insertPanelsAreOpen: !this.state.insertPanelsAreOpen,
});
}

handleNodeFormatClick = () => {
handleNodeFormatButtonClick = () => {
this.setFocusIf('nodeFormatDrawerIsOpen', null, 'NodeFormatDrawer')
this.setPersistentState({
nodeFormatDrawerIsOpen: !this.state.nodeFormatDrawerIsOpen,
edgeFormatDrawerIsOpen: false,
Expand All @@ -146,9 +159,11 @@ class Index extends React.Component {
this.setPersistentState({
nodeFormatDrawerIsOpen: false,
});
this.setFocus(null);
}

handleEdgeFormatClick = () => {
handleEdgeFormatButtonClick = () => {
this.setFocusIf('edgeFormatDrawerIsOpen', null, 'EdgeFormatDrawer')
this.setPersistentState({
edgeFormatDrawerIsOpen: !this.state.edgeFormatDrawerIsOpen,
nodeFormatDrawerIsOpen: false,
Expand All @@ -159,6 +174,7 @@ class Index extends React.Component {
this.setPersistentState({
edgeFormatDrawerIsOpen: false,
});
this.setFocus(null);
}

handleSettingsClick = () => {
Expand Down Expand Up @@ -381,9 +397,65 @@ class Index extends React.Component {
this.redo = redo;
}

handleTextEditorFocus = () => {
this.setFocus('TextEditor');
}

handleTextEditorBlur = () => {
// Needed when the user clicks outside of a pane,
// e.g. the app bar or the background
this.setFocusIfFocusIs('TextEditor', null);
}

handleGraphFocus = () => {
this.setFocus('Graph');
}

handleInsertPanelsClick = () => {
this.setFocus('InsertPanels');
}

handleNodeFormatDrawerClick = () => {
this.setFocusIf('nodeFormatDrawerIsOpen', 'NodeFormatDrawer', null)
}

handleEdgeFormatDrawerClick = () => {
this.setFocus('EdgeFormatDrawer');
this.setFocusIf('edgeFormatDrawerIsOpen', 'EdgeFormatDrawer', null)
}

setFocus = (focusedPane) => {
this.setState((state) => (state.focusedPane !== focusedPane && {
focusedPane: focusedPane,
}) || null);
}

setFocusIfFocusIs = (currentlyFocusedPane, newFocusedPane) => {
this.setState((state) => (state.focusedPane === currentlyFocusedPane && {
focusedPane: newFocusedPane,
}) || null);
}

setFocusIf = (stateProperty, focusedPaneIf, focusedPaneElse) => {
this.setState((state) => {
const focusedPane = state[stateProperty] ? focusedPaneIf: focusedPaneElse;
return (state.focusedPane !== focusedPane && {
focusedPane: focusedPane,
}) || null;
});
}

render() {
const { classes } = this.props;
const editorIsOpen = !this.state.nodeFormatDrawerIsOpen && !this.state.edgeFormatDrawerIsOpen;
const textEditorHasFocus = this.state.focusedPane === 'TextEditor';
const nodeFormatDrawerHasFocus = this.state.focusedPane === 'NodeFormatDrawer';
const edgeFormatDrawerHasFocus = this.state.focusedPane === 'EdgeFormatDrawer';
const insertPanelsHaveFocus = this.state.focusedPane === 'InsertPanels';
const graphHasFocus = this.state.focusedPane === 'Graph';
const leftPaneElevation = textEditorHasFocus || nodeFormatDrawerHasFocus || edgeFormatDrawerHasFocus? focusedElevation : defaultElevation;
const rightPaneElevation = graphHasFocus ? focusedElevation : defaultElevation;
const midPaneElevation = insertPanelsHaveFocus ? focusedElevation : defaultElevation;

var columns;
if (this.state.insertPanelsAreOpen && this.state.graphInitialized) {
Expand All @@ -407,9 +479,9 @@ class Index extends React.Component {
onMenuButtonClick={this.handleMainMenuButtonClick}
onUndoButtonClick={this.handleUndoButtonClick}
onRedoButtonClick={this.handleRedoButtonClick}
onInsertClick={this.handleInsertClick}
onNodeFormatClick={this.handleNodeFormatClick}
onEdgeFormatClick={this.handleEdgeFormatClick}
onInsertClick={this.handleInsertButtonClick}
onNodeFormatClick={this.handleNodeFormatButtonClick}
onEdgeFormatClick={this.handleEdgeFormatButtonClick}
onZoomInButtonClick={this.handleZoomInButtonClick}
onZoomOutButtonClick={this.handleZoomOutButtonClick}
onZoomOutMapButtonClick={this.handleZoomOutMapButtonClick}
Expand Down Expand Up @@ -454,11 +526,12 @@ class Index extends React.Component {
}}
>
<Grid item xs={columns.textEditor}>
<Paper className={classes.paper}>
<Paper elevation={leftPaneElevation} className={classes.paper}>
<FormatDrawer
type='node'
open={this.state.nodeFormatDrawerIsOpen}
defaultAttributes={this.state.defaultNodeAttributes}
onClick={this.handleNodeFormatDrawerClick}
onFormatDrawerClose={this.handleNodeFormatDrawerClose}
onStyleChange={this.handleNodeStyleChange}
onColorChange={this.handleNodeColorChange}
Expand All @@ -468,6 +541,7 @@ class Index extends React.Component {
type='edge'
open={this.state.edgeFormatDrawerIsOpen}
defaultAttributes={this.state.defaultEdgeAttributes}
onClick={this.handleEdgeFormatDrawerClick}
onFormatDrawerClose={this.handleEdgeFormatDrawerClose}
onStyleChange={this.handleEdgeStyleChange}
onColorChange={this.handleEdgeColorChange}
Expand All @@ -494,8 +568,9 @@ class Index extends React.Component {
</Grid>
{this.state.insertPanelsAreOpen && this.state.graphInitialized && (
<Grid item xs={columns.insertPanels}>
<Paper className={classes.paper}>
<Paper elevation={midPaneElevation} className={classes.paper}>
<InsertPanels
onClick={this.handleInsertPanelsClick}
onNodeShapeClick={this.handleNodeShapeClick}
onNodeShapeDragStart={this.handleNodeShapeDragStart}
onNodeShapeDragEnd={this.handleNodeShapeDragEnd}
Expand All @@ -504,8 +579,9 @@ class Index extends React.Component {
</Grid>
)}
<Grid item xs={columns.graph}>
<Paper className={classes.paper}>
<Paper elevation={rightPaneElevation} className={classes.paper}>
<Graph
hasFocus={graphHasFocus}
dotSrc={this.state.dotSrc}
engine={this.state.engine}
fit={this.state.fitGraph}
Expand All @@ -515,6 +591,7 @@ class Index extends React.Component {
tweenPrecision={this.state.tweenPrecision}
defaultNodeAttributes={this.state.defaultNodeAttributes}
defaultEdgeAttributes={this.state.defaultEdgeAttributes}
onFocus={this.handleGraphFocus}
onTextChange={this.handleTextChange}
onHelp={this.handleKeyboardShortcutsClick}
onSelect={this.handleGraphComponentSelect}
Expand Down

0 comments on commit 09175a3

Please sign in to comment.