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

489 link editing #490

Merged
merged 4 commits into from
Sep 22, 2023
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
11 changes: 11 additions & 0 deletions .changeset/fresh-peaches-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@jpmorganchase/mosaic-content-editor-plugin': patch
---

### Fix

The insert/edit link dialog no longer closes when its contents are clicked.

### Refactor

The forms used to insert/edit links and insert images in the content editor plugin have been updated to use the latest version of `FormField` and `Input` from Salt-ds.
2 changes: 0 additions & 2 deletions packages/content-editor-plugin/src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { ScrollableSection } from './ScrollableSection/ScrollableSection';
import HorizontalRulePlugin from '../plugins/HorizontalRulePlugin';
import { FloatingToolbarPlugin } from '../plugins/FloatingToolbarPlugin';
import { TableActionMenuPlugin } from '../plugins/TableActionMenuPlugin';
import { InsertLinkDialog } from './Toolbar/InsertLink';

function onError(error: Error) {
console.error(error);
Expand Down Expand Up @@ -137,7 +136,6 @@ const Editor: FC<EditorProps> = ({
<HorizontalRulePlugin />
<FloatingToolbarPlugin />
<TableActionMenuPlugin />
<InsertLinkDialog />
</div>
</div>
</LexicalComposer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ import React, { useState } from 'react';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { Button, Icon } from '@jpmorganchase/mosaic-components';
import { string, object } from 'yup';
import {
ButtonBar,
DialogTitle,
DialogContent,
DialogActions,
FormField,
Input
} from '@salt-ds/lab';
import { Input, FormField, FormFieldLabel, FormFieldHelperText } from '@salt-ds/core';
import { ButtonBar, DialogTitle, DialogContent, DialogActions } from '@salt-ds/lab';

import { ToolbarButton } from './ToolbarButton';
import { Dialog } from '../Dialog';
Expand Down Expand Up @@ -52,11 +46,16 @@ export const InsertImage = () => {

const handleOpenChange = (open: boolean) => {
setIsOpen(open);
if (!open) {
setValues(initialState);
}
};

const handleOpen = () => setIsOpen(true);

const handleClose = () => setIsOpen(false);
const handleClose = () => {
handleOpenChange(false);
};

const processErrors = validationErrors => {
const newErrors = validationErrors.inner.reduce(
Expand All @@ -69,8 +68,8 @@ export const InsertImage = () => {
setErrors(newErrors);
};

const handleChange = (event, value) => {
const { name } = event.target;
const handleChange = event => {
const { name, value } = event.target;
const newValues = { ...values, [name]: value };
validationSchema.validateAt(name, newValues, { abortEarly: false }).then(() => {
setErrors({ ...errors, [name]: undefined });
Expand Down Expand Up @@ -101,22 +100,18 @@ export const InsertImage = () => {
<DialogTitle>Insert Image</DialogTitle>
<DialogContent>
<div className={styles.fullWidth}>
<FormField
label="Url for image"
validationStatus={errors?.url ? 'error' : undefined}
helperText={errors?.url}
>
<FormField validationStatus={errors?.url ? 'error' : undefined}>
<FormFieldLabel>Url for image</FormFieldLabel>
<Input value={values?.url} inputProps={{ name: 'url' }} onChange={handleChange} />
<FormFieldHelperText>{errors?.url}</FormFieldHelperText>
</FormField>
<FormField
label="Alternative Information (alt)"
validationStatus={errors?.alt ? 'error' : undefined}
helperText={
errors?.alt ||
'Provides alternative information for the image if for some reason it cannot be viewed'
}
>
<FormField validationStatus={errors?.alt ? 'error' : undefined}>
<FormFieldLabel>Alternative Information (alt)</FormFieldLabel>
<Input value={values?.alt} inputProps={{ name: 'alt' }} onChange={handleChange} />
<FormFieldHelperText>
{errors?.alt ||
'Provides alternative information for the image if for some reason it cannot be viewed'}
</FormFieldHelperText>
</FormField>
</div>
</DialogContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ import React, { useEffect, useState } from 'react';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { Button, Icon } from '@jpmorganchase/mosaic-components';
import { string, object } from 'yup';
import {
FormField,
Input,
ButtonBar,
DialogTitle,
DialogContent,
DialogActions
} from '@salt-ds/lab';
import { Input, FormField, FormFieldLabel, FormFieldHelperText } from '@salt-ds/core';
import { ButtonBar, DialogTitle, DialogContent, DialogActions } from '@salt-ds/lab';
import { $getSelection, $isRangeSelection } from 'lexical';

import { ToolbarButton } from './ToolbarButton';
Expand All @@ -20,7 +14,7 @@ import styles from './InsertLink.css';

const validationSchema = object({
url: string().required('Url is required'),
text: string().required('Text is required').max(10, 'Text must be fewer than 100 characters')
text: string().required('Text is required').max(100, 'Text must be fewer than 100 characters')
});

export const InsertLinkButton = () => {
Expand All @@ -37,7 +31,8 @@ export const InsertLinkButton = () => {
};

const initialState = {
url: 'https://'
url: 'https://',
text: ''
};

type FormValueState =
Expand Down Expand Up @@ -78,7 +73,7 @@ export const InsertLinkDialog = () => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const textContent = selection.getTextContent();
setValues(values => ({ ...values, text: textContent }));
setValues(prevState => ({ ...prevState, text: textContent }));
}
}
});
Expand All @@ -95,17 +90,16 @@ export const InsertLinkDialog = () => {
setErrors(newErrors);
};

const handleChange = (event, value) => {
const { name } = event.target;
const handleChange = event => {
const { name, value } = event.target;
const newValues = { ...values, [name]: value };
validationSchema.validateAt(name, newValues, { abortEarly: false }).then(() => {
setErrors({ ...errors, [name]: undefined });
}, processErrors);
setValues(newValues);
};

const handleSubmit = async event => {
event.preventDefault();
const handleSubmit = async () => {
validationSchema.validate(values, { abortEarly: false }).then(() => {
const payload: InsertLinkPayload = {
url: values?.url,
Expand All @@ -118,35 +112,29 @@ export const InsertLinkDialog = () => {

return (
<Dialog onOpenChange={handleOpenChange} open={isInsertingLink}>
<form onSubmit={handleSubmit} noValidate>
<DialogTitle>Insert Link</DialogTitle>
<DialogContent>
<div className={styles.fullWidth}>
<FormField
label="Image URL text"
validationStatus={errors?.url ? 'error' : undefined}
helperText={errors?.url}
>
<Input value={values?.url} inputProps={{ name: 'url' }} onChange={handleChange} />
</FormField>
<FormField
label="Link text"
validationStatus={errors?.text ? 'error' : undefined}
helperText={errors?.text}
>
<Input value={values?.text} inputProps={{ name: 'text' }} onChange={handleChange} />
</FormField>
</div>
</DialogContent>
<DialogActions>
<ButtonBar>
<Button onClick={handleClose}>Cancel</Button>
<Button variant="cta" type="submit">
Insert
</Button>
</ButtonBar>
</DialogActions>
</form>
<DialogTitle>Insert Link</DialogTitle>
<DialogContent>
<div className={styles.fullWidth}>
<FormField validationStatus={errors?.url ? 'error' : undefined}>
<FormFieldLabel>Image URL text</FormFieldLabel>
<Input value={values?.url} inputProps={{ name: 'url' }} onChange={handleChange} />
<FormFieldHelperText>{errors?.url}</FormFieldHelperText>
</FormField>
<FormField validationStatus={errors?.text ? 'error' : undefined}>
<FormFieldLabel>Link Text</FormFieldLabel>
<Input value={values?.text} inputProps={{ name: 'text' }} onChange={handleChange} />
<FormFieldHelperText>{errors?.text}</FormFieldHelperText>
</FormField>
</div>
</DialogContent>
<DialogActions>
<ButtonBar>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for ButtonBar in actions right now. Although the actions don't have the full functionality of the old button bar. But if this works well then maybe leave it. I haven't tried it myself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted...will address in a future PR

<Button onClick={handleClose}>Cancel</Button>
<Button variant="cta" onClick={handleSubmit}>
Insert
</Button>
</ButtonBar>
</DialogActions>
</Dialog>
);
};