-
Notifications
You must be signed in to change notification settings - Fork 5
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
FEATURE: Select styles from a dropdown #9
Draft
Sebobo
wants to merge
4
commits into
breadlesscode:master
Choose a base branch
from
Sebobo:feature/dropdown-selection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
43b8401
FEATURE: Add configurable dropdowns for selecting styles
Sebobo 0bba02a
TASK: Add notes about new feature to readme
Sebobo 611d3e3
TASK: Bump minimal neos-ui requirement
Sebobo 8896e92
TASK: Reenable the button manifest to be able to use both
markusguenther File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
85 changes: 85 additions & 0 deletions
85
Resources/Private/UiPlugin/src/Commands/SelectFormatCommand.js
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,85 @@ | ||
import {Command} from 'ckeditor5-exports'; | ||
|
||
export default class SelectFormatCommand extends Command { | ||
attributePrefix = 'textFormat-'; | ||
|
||
constructor(editor, attributeKeys) { | ||
super(editor); | ||
this.attributeKeys = attributeKeys; | ||
} | ||
|
||
execute(options = {}) { | ||
const editor = this.editor; | ||
const model = editor.model; | ||
const document = model.document; | ||
const selection = document.selection; | ||
|
||
editor.model.change((writer) => { | ||
for (const attributeSuffix of Object.keys(options)) { | ||
const attributeName = this.attributePrefix + attributeSuffix; | ||
const selectedClass = options[attributeSuffix]; | ||
const ranges = model.schema.getValidRanges(selection.getRanges(), attributeName); | ||
|
||
if (selection.isCollapsed) { | ||
const position = selection.getFirstPosition(); | ||
|
||
// When selection is inside text with `highlight` attribute. | ||
if (selection.hasAttribute(attributeName)) { | ||
// Find the full highlighted range. | ||
const isSameHighlight = value => { | ||
return value.item.hasAttribute(attributeName) && value.item.getAttribute(attributeName) === this.value; | ||
}; | ||
|
||
const highlightStart = position.getLastMatchingPosition(isSameHighlight, {direction: 'backward'}); | ||
const highlightEnd = position.getLastMatchingPosition(isSameHighlight); | ||
const highlightRange = writer.createRange(highlightStart, highlightEnd); | ||
|
||
// Then depending on current value... | ||
if (!selectedClass || this.value === selectedClass) { | ||
// ...remove attribute when passing highlighter different then current or executing "eraser". | ||
writer.removeAttribute(attributeName, highlightRange); | ||
writer.removeSelectionAttribute(attributeName); | ||
} else { | ||
// ...update `highlight` value. | ||
writer.setAttribute(attributeName, selectedClass, highlightRange); | ||
writer.setSelectionAttribute(attributeName, selectedClass); | ||
} | ||
} else if (selectedClass) { | ||
writer.setSelectionAttribute(attributeName, selectedClass); | ||
} | ||
} else { | ||
for (const range of ranges) { | ||
if (selectedClass) { | ||
writer.setAttribute(attributeName, selectedClass, range); | ||
} else { | ||
writer.removeAttribute(attributeName, range); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
refresh() { | ||
const {model} = this.editor; | ||
const {selection} = model.document; | ||
|
||
this.value = {}; | ||
const attributes = selection.getAttributes(); | ||
|
||
for (let attribute of attributes) { | ||
if (attribute[0].indexOf(this.attributePrefix) === 0) { | ||
const suffix = attribute[0].substr(this.attributePrefix.length); | ||
this.value[suffix] = attribute[1]; | ||
} | ||
} | ||
|
||
this.isEnabled = true; | ||
for (let attributeName of this.attributeKeys) { | ||
if (this.isEnabled) { | ||
return; | ||
} | ||
this.isEnabled = this.isEnabled || model.schema.checkAttributeInSelection(selection, this.attributePrefix + attributeName); | ||
} | ||
} | ||
} |
File renamed without changes.
12 changes: 12 additions & 0 deletions
12
Resources/Private/UiPlugin/src/Components/SelectFormatButton.css
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,12 @@ | ||
.selectFormatButton__flyout { | ||
--dialog-width: 460px; | ||
|
||
background-color: var(--colors-ContrastDarker); | ||
position: fixed; | ||
z-index: var(--zIndex-SecondaryToolbar-LinkIconButtonFlyout); | ||
width: var(--dialog-width); | ||
left: 50%; | ||
margin-left: calc(var(--dialog-width) * -.5); | ||
border: var(--spacing-Half) solid var(--colors-ContrastDarker); | ||
} | ||
|
118 changes: 118 additions & 0 deletions
118
Resources/Private/UiPlugin/src/Components/SelectFormatButtonComponent.js
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,118 @@ | ||
import React, {PureComponent} from 'react'; | ||
import {connect} from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
import {$get, $transform} from 'plow-js'; | ||
import {selectors} from '@neos-project/neos-ui-redux-store'; | ||
import {IconButton, SelectBox} from '@neos-project/react-ui-components'; | ||
import {neos} from '@neos-project/neos-ui-decorators'; | ||
import {executeCommand} from '@neos-project/neos-ui-ckeditor5-bindings'; | ||
import style from './SelectFormatButton.css'; | ||
|
||
export default (commandName, configuration) => { | ||
@connect($transform({ | ||
formattingUnderCursor: selectors.UI.ContentCanvas.formattingUnderCursor | ||
})) | ||
@neos(globalRegistry => ({ | ||
i18nRegistry: globalRegistry.get('i18n') | ||
})) | ||
class SelectFormatButtonComponent extends PureComponent { | ||
static propTypes = { | ||
i18nRegistry: PropTypes.object, | ||
tooltip: PropTypes.string, | ||
formatOptions: PropTypes.object, | ||
formattingUnderCursor: PropTypes.objectOf(PropTypes.oneOfType([ | ||
PropTypes.number, | ||
PropTypes.bool, | ||
PropTypes.string, | ||
PropTypes.object | ||
])), | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
isOpen: false, | ||
attributes: props.formattingUnderCursor[commandName], | ||
}; | ||
} | ||
|
||
componentWillReceiveProps = (nextProps) => { | ||
// If the new selection doesn't have any formatting close the dialog | ||
if (!$get(commandName, nextProps.formattingUnderCursor)) { | ||
this.setState({isOpen: false}); | ||
} else { | ||
this.setState({ | ||
isOpen: this.state.isOpen, | ||
attributes: nextProps.formattingUnderCursor[commandName], | ||
}); | ||
} | ||
}; | ||
|
||
handleClick = () => { | ||
this.setState({isOpen: !this.state.isOpen}); | ||
}; | ||
|
||
handleRemoveFormatClick = () => { | ||
this.setState({ | ||
attributes: Object.keys(configuration.formatting.attributes).reduce((map, attribute) => { | ||
map[attribute] = ''; | ||
return map; | ||
}, {}) | ||
}, this.handleSelectionChange); | ||
}; | ||
|
||
handleSelectionChange = () => { | ||
executeCommand(commandName, this.state.attributes); | ||
}; | ||
|
||
handleAttributeChange = (attributeName, value) => { | ||
this.setState({ | ||
attributes: { | ||
...this.state.attributes, | ||
[attributeName]: value, | ||
} | ||
}, this.handleSelectionChange); | ||
}; | ||
|
||
render() { | ||
const {icon, id, tooltip, i18nRegistry} = this.props; | ||
const {isOpen} = this.state; | ||
const {attributes} = configuration.formatting; | ||
|
||
return ( | ||
<div> | ||
<IconButton | ||
icon={icon} | ||
id={id} | ||
isActive={isOpen} | ||
onClick={this.handleClick} | ||
title={i18nRegistry.translate(tooltip)} | ||
/> | ||
{isOpen ? ( | ||
<div className={style.selectFormatButton__flyout}> | ||
{Object.keys(attributes).map((attributeName) => { | ||
const attribute = attributes[attributeName]; | ||
return ( | ||
<div key={attributeName}> | ||
<label htmlFor={"selectFormat-" + attributeName}>{i18nRegistry.translate(attribute.label)}</label> | ||
<SelectBox id={"selectFormat-" + attributeName} | ||
placeholder={i18nRegistry.translate(attribute.placeholder)} | ||
placeholderIcon={i18nRegistry.translate(attribute.placeholderIcon)} | ||
options={attribute.options} | ||
value={this.state.attributes[attributeName]} | ||
onValueChange={(value) => this.handleAttributeChange(attributeName, value)} | ||
optionValueField="model"/> | ||
</div> | ||
); | ||
})} | ||
<IconButton icon="eraser" hoverStyle="brand" | ||
onClick={this.handleRemoveFormatClick}/> | ||
</div> | ||
) : null} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
return SelectFormatButtonComponent; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you creating a group of SelectBoxes instead of a singel SelectBox? Is there a reason for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for each Attribute (in the example above
color
andsize
). There can be multiple attributes/styles in one styling button.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly