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 image support (RichText) #705

Merged
merged 1 commit into from
Oct 28, 2020
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
113 changes: 112 additions & 1 deletion src/controls/richText/RichText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class RichText extends React.Component<IRichTextProps, IRichTextState> {
showAlign: true,
showList: true,
showLink: true,
showImage: true,
showMore: true
}
};
Expand All @@ -115,9 +116,11 @@ export class RichText extends React.Component<IRichTextProps, IRichTextState> {
editing: false,
morePaneVisible: false,
hideDialog: true,
hideImageDialog: true,
formats: {},
insertUrl: undefined,
insertUrlText: undefined,
insertImageUrl: undefined,
selectedText: undefined,
selectedUrl: undefined,
wrapperTop: 0
Expand Down Expand Up @@ -392,6 +395,40 @@ export class RichText extends React.Component<IRichTextProps, IRichTextState> {
);
}

/**
* Renders the "Insert Image" dialog
*/
private renderImageDialog = (): JSX.Element => {
return (
<Dialog hidden={this.state.hideImageDialog}
onDismiss={this.closeImageDialog}
dialogContentProps={{
type: DialogType.normal,
title: strings.InsertImageTitle,
}}
modalProps={{
className: styles.insertLinkDialog,
isBlocking: true,
containerClassName: 'ms-dialogMainOverride'
}}>
<TextField label={strings.AddressFieldLabel}
value={this.state.insertImageUrl !== undefined ? this.state.insertImageUrl : ""}
onChanged={(newValue?: string) => {
this.setState({
insertImageUrl: newValue
});
}} />

<DialogFooter className={styles.actions}>
<div className={`ms-Dialog-actionsRight ${styles.actionsRight}`}>
<PrimaryButton className={styles.action} onClick={this.handleInsertImage} text={strings.SaveButtonLabel} disabled={this.checkImageLinkUrl()} />
<DefaultButton className={styles.action} onClick={this.closeImageDialog} text={strings.CancelButtonLabel} />
</div>
</DialogFooter>
</Dialog>
);
}

/**
* Renders the Rich Text Editor
*/
Expand All @@ -409,7 +446,7 @@ export class RichText extends React.Component<IRichTextProps, IRichTextState> {
}

// Okay, we're in edit mode.
const { placeholder, styleOptions: { showStyles, showBold, showItalic, showUnderline, showAlign, showList, showLink, showMore } } = this.props;
const { placeholder, styleOptions: { showStyles, showBold, showItalic, showUnderline, showAlign, showList, showLink, showMore, showImage} } = this.props;

// Get a unique id for the toolbar
const modules = {
Expand Down Expand Up @@ -551,6 +588,20 @@ id="DropDownStyles"
}} />
</TooltipHost>
)
}
{
showImage && (
<TooltipHost content={strings.ImageTitle}
id="image-richtextbutton"
calloutProps={{ gapSpace: 0 }}>
<IconButton checked={this.state.formats!.link !== undefined}
onClick={this.showInsertImageDialog}
aria-describedby="image-richtextbutton"
iconProps={{
iconName: 'PictureFill'
}} />
</TooltipHost>
)
}
{
showMore && (
Expand Down Expand Up @@ -583,6 +634,10 @@ id="DropDownStyles"
{
this.renderLinkDialog()
}
{
this.renderImageDialog()
}

</div>
);
}
Expand Down Expand Up @@ -659,6 +714,29 @@ id="DropDownStyles"
this.setState({ hideDialog: true });
}

/**
* Displays the insert link dialog
*/
private showInsertImageDialog = () => {
const quill = this.getEditor();
const range = quill.getSelection();

this.setState({
hideImageDialog: false,
selectedRange: range
});
}

/**
* Hides the insert image dialog
*/
private closeImageDialog = () => {
this.setState({
hideImageDialog: true,
insertImageUrl: undefined,
});
}

/**
* When user enters the richtext editor, displays the border
*/
Expand Down Expand Up @@ -703,6 +781,28 @@ id="DropDownStyles"
});
}

/**
* Called when user insert an image
*/
private handleInsertImage = () => {
const { insertImageUrl, selectedRange } = this.state;
try {
const quill = this.getEditor();
const cursorPosition: number = selectedRange!.index;
quill.insertEmbed(cursorPosition, 'image', insertImageUrl, "user");
this.setState({
insertImageUrl: undefined,
hideImageDialog: true,
});
} catch {
//Close the image dialog if something went wrong
this.setState({
insertImageUrl: undefined,
hideImageDialog: true,
});
}
}

/**
* Disable Save-button if hyperlink is undefined or empty
* This prevents the user of adding an empty hyperlink
Expand All @@ -714,6 +814,17 @@ id="DropDownStyles"
return true;
}

/**
* Disable Save-button if hyperlink for the imported image is undefined or empty
* This prevents the user of adding an empty image
*/
private checkImageLinkUrl = () => {
if (this.state.insertImageUrl !== undefined && this.state.insertImageUrl != "") {
return false;
}
return true;
}

/**
* Applies a format to the selection
* @param name format name
Expand Down
17 changes: 17 additions & 0 deletions src/controls/richText/RichText.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export interface StyleOptions {
*/
showMore?: boolean;

/**
* Indicates if we should show the Image button.
* @defaultvalue true
*/
showImage?: boolean;

/**
* Indicates if we should show the Styles button (Heading 1, Heading 2, ..., Pull quote)
* @defaultvalue true
Expand Down Expand Up @@ -109,6 +115,12 @@ export interface IRichTextState {
*/
hideDialog: boolean;


/**
* Whether to hide the insert image dialog
*/
hideImageDialog: boolean;

/**
* The URL to insert
*/
Expand All @@ -119,6 +131,11 @@ export interface IRichTextState {
*/
insertUrlText: string;

/**
* The URL of image to insert
*/
insertImageUrl: string;

/**
* Whether the "More" pane is visible
*/
Expand Down
2 changes: 2 additions & 0 deletions src/loc/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ define([], () => {
AlignTitle: "Align",
ListTitle: "List",
LinkTitle: "Hyperlink",
ImageTitle: "Image",
MoreTitle: "More",
FormattingPaneTitle: "Text formatting",
CloseButton: "Close",
InsertLinkTitle: "Insert link",
InsertImageTitle: "Insert image",
AddressFieldLabel: "Address",
TextToDisplayLabel: "Text to display",
SaveButtonLabel: "Save",
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 @@ -73,10 +73,12 @@ declare interface IControlStrings {
AlignTitle: string;
ListTitle: string;
LinkTitle: string;
ImageTitle: string;
MoreTitle: string;
FormattingPaneTitle: string;
CloseButton: string;
InsertLinkTitle: string;
InsertImageTitle: string;
AddressFieldLabel: string;
TextToDisplayLabel: string;
SaveButtonLabel: string;
Expand Down
2 changes: 2 additions & 0 deletions src/loc/nl-nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ define([], () => {
"AlignTitle": "Uitlijnen",
"ListTitle": "Lijst",
"LinkTitle": "Hyperlink",
"ImageTitle": "Afbeelding",
"MoreTitle": "Meer",
"FormattingPaneTitle": "Tekstopmaak",
"CloseButton": "Sluiten",
"InsertLinkTitle": "Koppeling invoegen",
"InsertImageTitle": "Afbeelding invoegen",
"AddressFieldLabel": "Adres",
"TextToDisplayLabel": "Weer te geven tekst",
"SaveButtonLabel": "Opslaan",
Expand Down