This repository has been archived by the owner on Aug 8, 2018. It is now read-only.
forked from NabeelZanabeet/rich-content
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[editor] feat(heading-switch-button): implemented and added to the to…
…olbars instead of drop-down
- Loading branch information
Showing
7 changed files
with
145 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ages/editor/src/RichContentEditor/Toolbars/buttons/inline-styling/HeadingSwitchButton.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { RichUtils } from '@wix/draft-js'; | ||
import { HEADING, getSelectionStyles } from 'wix-rich-content-common'; | ||
import createInlineStyleSwitchButton from './createInlineStyleSwitchButton'; | ||
import { TitleIcon, TitleOneIcon, TitleTwoIcon } from '../../../../../statics/icons'; | ||
|
||
const styles = Object.keys(HEADING).map(k => HEADING[k]).filter(style => style !== HEADING.TITLE); | ||
const HeadingSwitchButton = createInlineStyleSwitchButton({ | ||
Icons: [TitleIcon, TitleOneIcon, TitleTwoIcon], | ||
styles, | ||
onStyleChange: ({ getEditorState, setEditorState, styles, nextStyle }) => { | ||
let editorState = getEditorState(); | ||
// toggle off all exclusive inline styles | ||
const selectionStyles = getSelectionStyles(styles, editorState); | ||
selectionStyles.forEach(appliedStyle => { | ||
editorState = RichUtils.toggleInlineStyle(editorState, appliedStyle); | ||
}); | ||
// apply new style | ||
if (nextStyle !== HEADING.NORMAL) { | ||
editorState = RichUtils.toggleInlineStyle(editorState, nextStyle); | ||
} | ||
setEditorState(editorState); | ||
}, | ||
getSelectionStyle: ({ editorState, styles }) => { | ||
const selection = editorState.getSelection(); | ||
if (selection.getEndOffset() - selection.getStartOffset() > 0) { | ||
const selectionStyles = getSelectionStyles(styles, editorState); | ||
|
||
if (selectionStyles.length !== 1) { | ||
return HEADING.NORMAL; | ||
} | ||
return selectionStyles[0]; | ||
} | ||
}, | ||
tooltipTextKey: 'TitleButton_tooltip' | ||
}); | ||
|
||
export default HeadingSwitchButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...r/src/RichContentEditor/Toolbars/buttons/inline-styling/createInlineStyleSwitchButton.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import TextButton from '../TextButton'; | ||
|
||
export default ({ onStyleChange, getSelectionStyle, styles, Icons, InactiveIcon = null, tooltipTextKey }) => | ||
class InlineStyleSwitchButton extends Component { | ||
static propTypes = { | ||
getEditorState: PropTypes.func.isRequired, | ||
setEditorState: PropTypes.func.isRequired, | ||
theme: PropTypes.object.isRequired, | ||
isVisible: PropTypes.bool, | ||
isMobile: PropTypes.bool, | ||
t: PropTypes.func, | ||
tabIndex: PropTypes.number, | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
styleIndex: undefined, | ||
}; | ||
} | ||
|
||
get selectionStyle() { | ||
const { getEditorState } = this.props; | ||
const editorState = getEditorState(); | ||
return getSelectionStyle({ editorState, styles }); | ||
} | ||
|
||
get Icon() { | ||
const { styleIndex } = this.state; | ||
if (styleIndex !== undefined) { | ||
return Icons[styleIndex]; | ||
} else { | ||
return InactiveIcon ? InactiveIcon : Icons[0]; | ||
} | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
if (this.props.isVisible === false && nextProps.isVisible === true) { | ||
const { selectionStyle } = this; | ||
const styleFoundIndex = styles.findIndex(b => b === selectionStyle); | ||
const styleIndex = styleFoundIndex > -1 ? styleFoundIndex : undefined; | ||
this.setState({ styleIndex }); | ||
} | ||
} | ||
|
||
getNextStyleIndex() { | ||
const style = this.selectionStyle; | ||
if (style) { | ||
const styleIndex = styles.findIndex(s => s === style); | ||
return (styleIndex + 1) % styles.length; | ||
} | ||
} | ||
|
||
setStyle = () => { | ||
const { getEditorState, setEditorState } = this.props; | ||
const newStyleIndex = this.getNextStyleIndex(); | ||
if (newStyleIndex === undefined) { | ||
return; | ||
} | ||
const nextStyle = styles[newStyleIndex]; | ||
onStyleChange({ getEditorState, setEditorState, styles, nextStyle }); | ||
this.setState({ styleIndex: newStyleIndex }); | ||
}; | ||
|
||
styleIsActive = () => { | ||
const { style } = this; | ||
return typeof style !== 'undefined' && style === this.selectionStyle; | ||
}; | ||
|
||
render() { | ||
const { Icon } = this; | ||
const { theme, isMobile, t, tabIndex } = this.props; | ||
const tooltipText = t(tooltipTextKey); | ||
const textForHooks = tooltipText.replace(/\s+/, ''); // TODO: data-hooks | ||
const dataHookText = `textBlockStyleButton_${textForHooks}`; | ||
|
||
return ( | ||
<TextButton | ||
icon={Icon} | ||
theme={theme} | ||
isMobile={isMobile} | ||
isActive={this.styleIsActive} | ||
onClick={this.setStyle} | ||
tooltipText={tooltipText} | ||
dataHook={dataHookText} | ||
tabIndex={tabIndex} | ||
/> | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters