Skip to content

Commit 68096df

Browse files
authored
feat(image): move selection to the image title on toolbar item click (#725)
1 parent 8b65ebd commit 68096df

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/bundle/toolbar/custom/ToolbarImagePopup.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type ToolbarImagePopuProps = Omit<ToolbarBaseProps<never>, 'editor'> & {
1919
onSuccessUpload?: (res: BatchUploadResult) => void;
2020
hide: () => void;
2121
anchorElement: HTMLElement | null;
22-
} & Pick<ImageFormProps, 'onSubmit'>;
22+
} & Pick<ImageFormProps, 'onSubmit' | 'imageTitle'>;
2323

2424
export const ToolbarImagePopup: React.FC<ToolbarImagePopuProps> = ({
2525
className,
@@ -30,6 +30,7 @@ export const ToolbarImagePopup: React.FC<ToolbarImagePopuProps> = ({
3030
onClick,
3131
uploadImages,
3232
onSuccessUpload,
33+
imageTitle,
3334
}) => {
3435
const toaster = useToaster();
3536
const [loading, showLoading, hideLoading] = useBooleanState(false);
@@ -79,6 +80,7 @@ export const ToolbarImagePopup: React.FC<ToolbarImagePopuProps> = ({
7980
onClick?.('addImage');
8081
}}
8182
loading={loading}
83+
imageTitle={imageTitle}
8284
/>
8385
</Popup>
8486
);

src/bundle/toolbar/markup/MToolbarImagePopup.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {useMemo} from 'react';
2+
13
import isNumber from 'is-number';
24

35
import {IMG_MAX_HEIGHT, type ImageItem, getImageDimensions, insertImages} from '../../../markup';
@@ -26,8 +28,16 @@ export const MToolbarImagePopup: React.FC<MToolbarImagePopupProps> = ({
2628
}) => {
2729
const {uploadHandler, needToSetDimensionsForUploadedImages} = useMarkupToolbarContext();
2830

31+
const selectedString = useMemo(() => {
32+
const {from, to} = editor.cm.state.selection.main;
33+
return editor.cm.state.doc.sliceString(from, to);
34+
// we need to calculate the selection only once
35+
// eslint-disable-next-line react-hooks/exhaustive-deps
36+
}, []);
37+
2938
return (
3039
<ToolbarImagePopup
40+
imageTitle={selectedString}
3141
hide={hide}
3242
anchorElement={anchorElement}
3343
focus={focus}

src/forms/ImageForm.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type ImageFormProps = ClassNameProps & {
3535
onCancel(): void;
3636
onAttach?: (files: File[]) => void;
3737
loading?: boolean;
38+
imageTitle?: string;
3839
};
3940

4041
export const ImageForm: React.FC<ImageFormProps> = ({
@@ -44,12 +45,13 @@ export const ImageForm: React.FC<ImageFormProps> = ({
4445
onSubmit,
4546
onAttach,
4647
loading,
48+
imageTitle: providedName,
4749
}) => {
4850
const [tabId, setTabId] = useState<string>(() =>
4951
isFunction(onAttach) ? ImageTabId.Attach : ImageTabId.Link,
5052
);
5153
const [url, setUrl] = useState('');
52-
const [name, setName] = useState('');
54+
const [name, setName] = useState(providedName ?? '');
5355
const [alt, setAlt] = useState('');
5456
const [width, setWidth] = useState<number | undefined>();
5557
const [height, setHeight] = useState<number | undefined>();

0 commit comments

Comments
 (0)