Skip to content

Commit 301bc9d

Browse files
authored
Merge pull request #530 from prezly/feature/dev-12552-is-it-possible-to-truncate-captions-with-dots-when-not-being
[DEV-12552] Feature - Show pencil next to the caption & truncate if needed
2 parents e90e457 + 68e1e9e commit 301bc9d

File tree

2 files changed

+81
-11
lines changed

2 files changed

+81
-11
lines changed

packages/slate-editor/src/extensions/galleries/components/Gallery/GalleryTile.module.scss

+39
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import "styles/helpers";
12
@import "styles/variables";
23

34
.GalleryTile {
@@ -82,6 +83,7 @@
8283

8384
.Input {
8485
width: 100%;
86+
padding: 0;
8587
appearance: none;
8688
color: $white;
8789
background: none;
@@ -97,6 +99,43 @@
9799
font-weight: 300;
98100
}
99101
}
102+
103+
.Button {
104+
max-width: 100%;
105+
margin: 0;
106+
padding: 0;
107+
border: none;
108+
font-size: 13px;
109+
font-weight: 500;
110+
line-height: 1.2;
111+
overflow: hidden;
112+
113+
&,
114+
&:hover,
115+
&:focus {
116+
color: $white !important;
117+
background: none !important;
118+
}
119+
120+
&.empty {
121+
font-style: italic;
122+
font-weight: 300;
123+
}
124+
125+
> div {
126+
overflow: hidden;
127+
}
128+
129+
span {
130+
@include ellipsis;
131+
132+
opacity: 1;
133+
}
134+
135+
svg {
136+
flex-shrink: 0;
137+
}
138+
}
100139
}
101140

102141
.Actions {

packages/slate-editor/src/extensions/galleries/components/Gallery/GalleryTile.tsx

+42-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import classNames from 'classnames';
2-
import type { CSSProperties } from 'react';
3-
import React, { forwardRef, useState } from 'react';
2+
import type { CSSProperties, ChangeEvent } from 'react';
3+
import React, { forwardRef, useEffect, useState } from 'react';
44

55
import { Button, ImageSizeWarning, ImageWithLoadingPlaceholder } from '#components';
6-
import { Crop, Delete } from '#icons';
6+
import { Crop, Delete, Edit } from '#icons';
77

88
import styles from './GalleryTile.module.scss';
99

@@ -28,7 +28,7 @@ export interface Props {
2828

2929
export const GalleryTile = forwardRef<HTMLDivElement, Props>(function GalleryTile(
3030
{
31-
caption,
31+
caption: originalCaption,
3232
className,
3333
clone = false,
3434
dragging,
@@ -48,7 +48,18 @@ export const GalleryTile = forwardRef<HTMLDivElement, Props>(function GalleryTil
4848
},
4949
ref,
5050
) {
51+
// We have to use an intermediate state otherwise the input keeps
52+
// re-rendering every time the original caption changes and moves
53+
// the caret to the end of the input
54+
const [caption, setCaption] = useState(originalCaption);
5155
const [isHovering, setHovering] = useState(false);
56+
const [isEditingCaption, setEditingCaption] = useState(false);
57+
58+
function handleCaptionChange(event: ChangeEvent<HTMLInputElement>) {
59+
const text = event.currentTarget.value;
60+
setCaption(text);
61+
onCaptionChange(text);
62+
}
5263

5364
function handleShowOverlay() {
5465
setTimeout(() => setHovering(true), 0);
@@ -58,6 +69,10 @@ export const GalleryTile = forwardRef<HTMLDivElement, Props>(function GalleryTil
5869
setHovering(false);
5970
}
6071

72+
useEffect(() => {
73+
setCaption(caption);
74+
}, [caption]);
75+
6176
return (
6277
<div
6378
className={classNames(styles.GalleryTile, className, {
@@ -88,13 +103,29 @@ export const GalleryTile = forwardRef<HTMLDivElement, Props>(function GalleryTil
88103
[styles.visible]: caption !== '',
89104
})}
90105
>
91-
<input
92-
type="text"
93-
className={styles.Input}
94-
onChange={(event) => onCaptionChange?.(event.currentTarget.value)}
95-
value={caption}
96-
placeholder={isInteractive ? 'add caption' : ''}
97-
/>
106+
{isEditingCaption ? (
107+
<input
108+
autoFocus
109+
type="text"
110+
className={styles.Input}
111+
onChange={handleCaptionChange}
112+
onBlur={() => setEditingCaption(false)}
113+
value={caption}
114+
placeholder="add caption"
115+
/>
116+
) : (
117+
<Button
118+
className={classNames(styles.Button, {
119+
[styles.empty]: !caption,
120+
})}
121+
icon={isInteractive ? Edit : undefined}
122+
iconPosition="right"
123+
onClick={() => setEditingCaption(true)}
124+
variant="clear"
125+
>
126+
{caption || 'add caption'}
127+
</Button>
128+
)}
98129
</div>
99130
</div>
100131
)}

0 commit comments

Comments
 (0)