Skip to content

Commit f67c919

Browse files
authored
Write editor: caption is editable after reload (RSM-3359) (#48866)
Captions are saved as `<figcaption class="wp-element-caption">` and come back from the server without `contentEditable`. The caption-button click handler only set up an editable figcaption when one didn't already exist, so reloaded posts had captions that were uneditable and left- aligned (missing the `bw-figcaption` class that supplies `text-align: center` and the placeholder). Initialize any pre-existing figcaption at figure init time so it picks up the same class, contentEditable, placeholder, and click-stop listener as a freshly-created one.
1 parent 225e7b9 commit f67c919

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Write editor: caption is editable after reload (RSM-3359)

projects/packages/jetpack-mu-wpcom/src/features/write/style.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,8 +1224,12 @@
12241224
background: rgba(33, 113, 177, 0.85);
12251225
}
12261226

1227-
/* Figcaption */
1228-
.bw-figcaption {
1227+
/* Figcaption.
1228+
* `.wp-element-caption` is the class on saved markup served by the server
1229+
* before JS upgrades it to `.bw-figcaption` — match both so reloaded
1230+
* captions don't flash left-aligned before init runs. */
1231+
.bw-figcaption,
1232+
.bw-content .wp-element-caption {
12291233
text-align: center;
12301234
font-size: 0.875rem;
12311235
color: #999;

projects/packages/jetpack-mu-wpcom/src/features/write/view.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,21 @@ function addDeleteButtons() {
11281128
} );
11291129
controls.appendChild( altBtn );
11301130

1131+
// Captions saved by the editor come back from the server with class
1132+
// `wp-element-caption` and no `contentEditable`, so without this any
1133+
// reloaded caption would be uneditable and left-aligned (missing the
1134+
// `bw-figcaption` style).
1135+
const existingFigcaption = fig.querySelector( 'figcaption' );
1136+
if ( existingFigcaption ) {
1137+
existingFigcaption.classList.add( 'bw-figcaption' );
1138+
existingFigcaption.contentEditable = 'true';
1139+
existingFigcaption.setAttribute(
1140+
'data-placeholder',
1141+
i18n.writeCaption || 'Write a caption...'
1142+
);
1143+
existingFigcaption.addEventListener( 'click', ev => ev.stopPropagation() );
1144+
}
1145+
11311146
// Caption button.
11321147
const capBtn = document.createElement( 'button' );
11331148
capBtn.className = 'bw-img-caption-btn';

0 commit comments

Comments
 (0)