Skip to content

Commit

Permalink
Merge pull request #385 from eweintraub/custom-color-swatch
Browse files Browse the repository at this point in the history
Custom color swatch in Rich Text Control
  • Loading branch information
AJIXuMuK committed Oct 9, 2019
2 parents fcadf4f + 1d76beb commit 0fb0fa7
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/controls/richText/RichText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export class RichText extends React.Component<IRichTextProps, IRichTextState> {

constructor(props: IRichTextProps) {
super(props);

telemetry.track('ReactRichText', {
className: !!props.className
});
Expand Down Expand Up @@ -579,7 +578,8 @@ id="DropDownStyles"
editor={this.getEditor()}
isOpen={this.state.morePaneVisible}
onClose={this.handleClosePanel}
onLink={this.showInsertLinkDialog} />
onLink={this.showInsertLinkDialog}
customColors={this.props.customColors}/>

{
this.renderLinkDialog()
Expand Down
6 changes: 6 additions & 0 deletions src/controls/richText/RichText.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ISwatchColor } from './SwatchColorPickerGroup.types';
export interface IRichTextProps {
/**
* CSS class to apply to the rich text editor.
Expand Down Expand Up @@ -27,6 +28,11 @@ export interface IRichTextProps {
*/
styleOptions?: StyleOptions;

/**
* Additional colors to include in swatch
*/
customColors?: ISwatchColor[];

/**
* Callback issued when the rich text changes.
* Returns the text that will be inserted in the rich text control.
Expand Down
13 changes: 9 additions & 4 deletions src/controls/richText/RichTextPropertyPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,19 @@ export default class RichTextPropertyPane extends React.Component<IRichTextPrope
private renderColorStylesGroup = (): JSX.Element => {
const color: string = this.state.formats.color || ThemeColorHelper.GetThemeColor(styles.NeutralPrimary);
const backgroundColor: string = this.state.formats.background || "rgba(0, 0, 0, 0)";

/**
* Add custom colors if passed as a property
*/
let fontColorGroups = ["themeColors","standardColors"];
if(this.props.customColors) fontColorGroups.push('customColors');

return (
<div className={styles.propertyPaneGroupField}>
<div className="ms-CustomFieldHost">
<div className={styles.controlsInOneRow}>
<RteColorPicker colorPickerGroups={[
"themeColors",
"standardColors"
]}
<RteColorPicker colorPickerGroups={fontColorGroups} // changed to variable
customColors={this.props.customColors}
buttonLabel={strings.FontColorLabel}
id="fontColor-propertyPaneButton"
defaultButtonLabel={strings.AutomaticFontColor}
Expand Down
2 changes: 2 additions & 0 deletions src/controls/richText/RichTextPropertyPane.types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Quill } from 'react-quill';
import { ISwatchColor } from './SwatchColorPickerGroup.types';

export interface IRichTextPropertyPaneProps {
className?: string;
editor: Quill;
isOpen: boolean;
customColors?: ISwatchColor[];
onClose: () => void;
onLink: () => void;
}
Expand Down
13 changes: 11 additions & 2 deletions src/controls/richText/RteColorPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default class RteColorPicker extends React.Component<IRteColorPickerProps
*/
public render(): React.ReactElement<IRteColorPickerProps> {
const { buttonLabel, defaultButtonLabel, fillThemeColor, id, previewColor } = this.props;

return (
<div>
<div ref={(ref) => this.wrapperRef = ref}>
Expand All @@ -36,7 +35,8 @@ export default class RteColorPicker extends React.Component<IRteColorPickerProps
<DefaultButton className={styles.colorPickerButton}
aria-describedby={id}
onClick={() => this.handleColorChanged(previewColor)}>
<svg className={`${styles.previewSvg} ${previewColor === "rgba(0, 0, 0, 0)" ? styles.border : ""}`}
{/* Added border to white */}
<svg className={`${styles.previewSvg} ${(previewColor === "rgba(0, 0, 0, 0)" || previewColor === "#ffffff") ? styles.border : ""}`}
fill={previewColor}
viewBox="0 0 20 20">
<rect className={styles.previewRectangle}
Expand Down Expand Up @@ -110,6 +110,12 @@ export default class RteColorPicker extends React.Component<IRteColorPickerProps
case "highlightColors":
groupName = strings.HighlightColorsGroupName;
break;
case "standardColors":
groupName = strings.StandardColorsGroupName;
break;
case "customColors":
groupName = strings.CustomColorsGroupName;
break;
default:
groupName = strings.HighlightColorsGroupName;
break;
Expand Down Expand Up @@ -241,6 +247,9 @@ export default class RteColorPicker extends React.Component<IRteColorPickerProps
}
];
break;
case 'customColors':
groupColors = this.props.customColors;
break;
default:
groupColors = [
{
Expand Down
3 changes: 3 additions & 0 deletions src/controls/richText/RteColorPicker.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ISwatchColor } from './SwatchColorPickerGroup.types';

export interface IRteColorPickerProps {
id: string;
buttonLabel: string;
Expand All @@ -8,6 +10,7 @@ export interface IRteColorPickerProps {
previewColor: string;
selectedColor: string;
switchToDefaultColor: () => void;
customColors?: ISwatchColor[];
}

export interface IRteColorPickerState {
Expand Down
1 change: 0 additions & 1 deletion src/controls/richText/SwatchColorPickerGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { chunk } from '@microsoft/sp-lodash-subset';
export default class SwatchColorPickerGroup extends React.Component<ISwatchColorPickerGroupProps, ISwatchColorPickerGroupState> {
public render(): React.ReactElement<ISwatchColorPickerGroupProps> {
const colorRows = chunk(this.props.groupColors, 5);

return (
<div>
<Label htmlFor={this.props.groupText}
Expand Down
1 change: 1 addition & 0 deletions src/loc/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ define([], () => {
ThemeColorsGroupName: "Theme colors",
HighlightColorsGroupName: "Highlight colors",
StandardColorsGroupName: "Standard colors",
CustomColorsGroupName: "Custom Colors",
ThemeColorDarker: "Theme darker",
ThemeColorDark: "Theme dark",
ThemeColorDarkAlt: "Theme dark alternate",
Expand Down
2 changes: 2 additions & 0 deletions src/loc/mystrings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ declare interface IControlStrings {
ThemeColorsGroupName: string;
HighlightColorsGroupName: string;
StandardColorsGroupName: string;
CustomColorsGroupName: string;
ColorsGroupName: string;
ThemeColorDarker: string;
ThemeColorDark: string;
ThemeColorDarkAlt: string;
Expand Down

0 comments on commit 0fb0fa7

Please sign in to comment.