Add responsive player layout, captions and native terminal sizing

This commit is contained in:
2026-10-13 16:31:55 +08:00
parent 4f85e3f420
commit 2041124178
22 changed files with 633 additions and 36 deletions
+34 -9
View File
@@ -1,16 +1,18 @@
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { BlockAlignmentToolbar, BlockControls, InspectorControls, RichText, useBlockProps } from '@wordpress/block-editor';
import { Button, Notice, PanelBody, Placeholder, RangeControl, SelectControl, TextControl, ToggleControl } from '@wordpress/components';
import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { finiteNumber, normalizeSource, normalizeWidth } from './options.js';
import { finiteNumber, normalizeDisplayAlign, normalizeSource, normalizeWidth, terminalDimension } from './options.js';
import Preview from './preview.js';
export default function Edit( { attributes, setAttributes } ) {
const { source, theme, speed, loop, controls, fit, startAt, idleTimeLimit, autoPlay, preload, width } = attributes;
export default function Edit( { attributes, setAttributes, isSelected } ) {
const { source, theme, speed, loop, controls, fit, startAt, idleTimeLimit, autoPlay, preload, width, caption, displayAlign, cols, rows } = attributes;
const [ draftSource, setDraftSource ] = useState( source );
const [ approvedSource, setApprovedSource ] = useState( '' );
const [ urlError, setUrlError ] = useState( false );
const [ attempt, setAttempt ] = useState( 0 );
const [ draftWidth, setDraftWidth ] = useState( width );
useEffect( () => setDraftWidth( width ), [ width ] );
useEffect( () => setDraftSource( source ), [ source ] );
const blockProps = useBlockProps( { className: 'asciiplayer-editor' } );
const applySource = ( event ) => {
@@ -31,16 +33,35 @@ export default function Edit( { attributes, setAttributes } ) {
{ label: 'Solarized Light', value: 'solarized-light' }, { label: 'Tango', value: 'tango' },
];
if ( ! themes.some( ( option ) => option.value === theme ) ) themes.push( { label: theme, value: theme } );
const widthOptions = [ '100%', '80%', '60%', '50%' ].map( ( value ) => ( { label: value, value } ) );
if ( ! widthOptions.some( ( option ) => option.value === width ) ) widthOptions.push( { label: width, value: width } );
const widthPresets = [ '100%', '80%', '60%', '50%', '25%' ];
const widthOptions = [ ...widthPresets.map( ( value ) => ( { label: value, value } ) ), { label: __( 'Custom', 'wp-asciiplayer' ), value: 'custom' } ];
const [ customWidth, setCustomWidth ] = useState( ! widthPresets.includes( width ) );
const widthValid = draftWidth.trim() !== '' && normalizeWidth( draftWidth ) === draftWidth.trim() && parseFloat( draftWidth ) > 0;
const placement = normalizeDisplayAlign( displayAlign );
const fitOptions = [
{ label: __( 'Width', 'wp-asciiplayer' ), value: 'width' },
{ label: __( 'Original size', 'wp-asciiplayer' ), value: 'none' },
];
if ( fit === 'height' || fit === 'both' ) fitOptions.push( { label: fit === 'height' ? __( 'Height (legacy)', 'wp-asciiplayer' ) : __( 'Width and height (legacy)', 'wp-asciiplayer' ), value: fit } );
if ( attributes.isExample ) {
return <div { ...blockProps }><figure className="asciiplayer-example" aria-label={ __( 'Example terminal recording', 'wp-asciiplayer' ) }><div className="asciiplayer-example__terminal"><span>$ printf 'Hello, terminal!\n'</span><br /><span>Hello, terminal!</span><br /><span>$ <span aria-hidden="true">▋</span></span></div><div className="asciiplayer-example__controls" aria-hidden="true">▶ ━━━━━━━━━━━ 0:00 / 0:12</div><figcaption>{ __( 'A terminal recording, right in your post.', 'wp-asciiplayer' ) }</figcaption></figure></div>;
}
return (
<>
<BlockControls group="other"><BlockAlignmentToolbar value={ placement } controls={ [ 'left', 'center', 'right' ] } onChange={ ( value ) => setAttributes( { displayAlign: normalizeDisplayAlign( value ) } ) } /></BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Display', 'wp-asciiplayer' ) }>
<SelectControl label={ __( 'Player width', 'wp-asciiplayer' ) } value={ customWidth || ! widthPresets.includes( width ) ? 'custom' : width } options={ widthOptions } onChange={ ( value ) => { setCustomWidth( value === 'custom' ); if ( value !== 'custom' ) setAttributes( { width: value } ); } } />
{ ( customWidth || ! widthPresets.includes( width ) ) && <TextControl label={ __( 'Custom width', 'wp-asciiplayer' ) } value={ draftWidth } help={ widthValid ? __( 'Use a CSS length, such as 480px or 65%. The player stays within its available space.', 'wp-asciiplayer' ) : __( 'Enter a positive width with a unit, such as 480px or 65%. The last valid width is kept.', 'wp-asciiplayer' ) } aria-invalid={ ! widthValid } onChange={ ( value ) => { setDraftWidth( value ); if ( value.trim() && normalizeWidth( value ) === value.trim() && parseFloat( value ) > 0 ) setAttributes( { width: value.trim() } ); } } /> }
<SelectControl label={ __( 'Display alignment', 'wp-asciiplayer' ) } value={ placement } options={ [ { label: __( 'Left', 'wp-asciiplayer' ), value: 'left' }, { label: __( 'Center', 'wp-asciiplayer' ), value: 'center' }, { label: __( 'Right', 'wp-asciiplayer' ), value: 'right' } ] } onChange={ ( value ) => setAttributes( { displayAlign: value } ) } />
<p>{ __( 'Display width scales the recording without changing its terminal columns or rows. Add a caption directly below the player.', 'wp-asciiplayer' ) }</p>
</PanelBody>
<PanelBody title={ __( 'Terminal size', 'wp-asciiplayer' ) } initialOpen={ false }>
<p>{ __( 'Leave blank to use the size recorded in the cast file, including recorded resize events. Overrides change the terminal grid and can alter or crop the recording’s layout.', 'wp-asciiplayer' ) }</p>
<TextControl label={ __( 'Columns', 'wp-asciiplayer' ) } type="number" min="1" max="1000" step="1" value={ cols ?? '' } placeholder={ __( 'From recording', 'wp-asciiplayer' ) } onChange={ ( value ) => setAttributes( { cols: terminalDimension( value ) } ) } />
<TextControl label={ __( 'Rows', 'wp-asciiplayer' ) } type="number" min="1" max="1000" step="1" value={ rows ?? '' } placeholder={ __( 'From recording', 'wp-asciiplayer' ) } onChange={ ( value ) => setAttributes( { rows: terminalDimension( value ) } ) } />
<Button variant="secondary" onClick={ () => setAttributes( { cols: undefined, rows: undefined } ) }>{ __( 'Use recording size', 'wp-asciiplayer' ) }</Button>
</PanelBody>
<PanelBody title={ __( 'Playback', 'wp-asciiplayer' ) }>
<RangeControl label={ __( 'Playback speed', 'wp-asciiplayer' ) } value={ speed } min={ 0.25 } max={ Math.max( 8, speed ) } step={ 0.25 } onChange={ ( value ) => setAttributes( { speed: finiteNumber( value, 1, 0.01, 100 ) } ) } />
<ToggleControl label={ __( 'Loop playback', 'wp-asciiplayer' ) } checked={ loop } onChange={ ( value ) => setAttributes( { loop: value } ) } />
@@ -57,7 +78,6 @@ export default function Edit( { attributes, setAttributes } ) {
{ label: __( 'Hide', 'wp-asciiplayer' ), value: 'never' },
] } onChange={ ( value ) => setAttributes( { controls: value } ) } />
<SelectControl label={ __( 'Fit', 'wp-asciiplayer' ) } value={ fit } options={ fitOptions } help={ [ 'height', 'both' ].includes( fit ) ? __( 'Height-based fitting requires a fixed container height in your site CSS.', 'wp-asciiplayer' ) : undefined } onChange={ ( value ) => setAttributes( { fit: value } ) } />
<SelectControl label={ __( 'Player width', 'wp-asciiplayer' ) } value={ width } options={ widthOptions } onChange={ ( value ) => setAttributes( { width: value } ) } />
{ attributes.legacyStyle && <p>{ __( 'The custom container style from your shortcode is preserved on the published page.', 'wp-asciiplayer' ) }</p> }
</PanelBody>
</InspectorControls>
@@ -70,15 +90,20 @@ export default function Edit( { attributes, setAttributes } ) {
<p className="asciiplayer-editor__privacy">{ __( 'Applying a URL loads the recording directly from that server, which receives your browser’s request. Published players contact the same server. Use a direct .cast URL; external servers must allow CORS.', 'wp-asciiplayer' ) }</p>
{ urlError && <Notice status="error" isDismissible={ false }>{ __( 'Enter an HTTP(S) recording URL or a path starting with a single /.', 'wp-asciiplayer' ) }</Notice> }
</div>
<figure className={ `asciiplayer-figure asciiplayer-align-${ placement }` } style={ { width: normalizeWidth( width ) } }>
{ approvedSource && approvedSource === source ? (
<div style={ { width: normalizeWidth( width ), maxWidth: '100%' } }>
<>
<Preview source={ source } attributes={ attributes } attempt={ attempt } onRetry={ () => setAttempt( ( current ) => current + 1 ) } />
</div>
</>
) : (
<Placeholder icon="editor-code" label={ __( 'Asciicast Player', 'wp-asciiplayer' ) } instructions={ source ? __( 'Load the saved recording to play and adjust it here.', 'wp-asciiplayer' ) : __( 'Paste a terminal recording URL above, then apply it to see an interactive preview.', 'wp-asciiplayer' ) }>
{ source && <Button variant="secondary" onClick={ () => { setApprovedSource( source ); setAttempt( ( current ) => current + 1 ); } }>{ __( 'Load preview', 'wp-asciiplayer' ) }</Button> }
</Placeholder>
) }
{ ( isSelected || caption ) && <RichText tagName="figcaption" className="wp-element-caption" value={ caption } allowedFormats={ [] } onChange={ ( value ) => setAttributes( { caption: value } ) } placeholder={ __( 'Add a caption…', 'wp-asciiplayer' ) } aria-label={ __( 'Recording caption', 'wp-asciiplayer' ) } /> }
</figure>
{ approvedSource === source && source && <p className="asciiplayer-editor__hint">{ __( 'Use the player controls to play, pause, and seek. Autoplay is disabled only in the editor.', 'wp-asciiplayer' ) }</p> }
{ [ 'height', 'both' ].includes( fit ) && <p className="asciiplayer-editor__hint">{ __( 'Height-based previews use the shortcode’s fixed height when available, or a 320px editor frame. Published height still comes from your container style.', 'wp-asciiplayer' ) }</p> }
</div>
</>
);
+10
View File
@@ -30,10 +30,12 @@
margin-top: 16px;
}
/*! rtl:begin:ignore */
.asciiplayer-editor__player {
min-width: 0;
text-align: left;
}
/*! rtl:end:ignore */
.asciiplayer-editor__status {
display: flex;
@@ -48,3 +50,11 @@
flex-direction: column;
}
}
.asciiplayer-editor > .asciiplayer-figure { margin-top: 16px; }
.asciiplayer-editor .asciiplayer-editor__preview { margin-top: 0; }
.asciiplayer-example { margin: 0; background: #121314; color: #eee; border-radius: 5px; overflow: hidden; }
.asciiplayer-example__terminal { padding: 24px 16px; font: 14px/1.7 monospace; white-space: pre; }
.asciiplayer-example__terminal > span:first-child { color: #a9da76; }
.asciiplayer-example__controls { padding: 8px 16px; background: #222; font: 12px/1.5 monospace; }
.asciiplayer-example figcaption { padding: 8px; background: #fff; color: #333; text-align: center; font-size: 12px; }
+10
View File
@@ -39,6 +39,16 @@ export function normalizeWidth( value ) {
? value.trim() : DEFAULTS.width;
}
/** Presentation placement never changes the terminal's character grid. */
export function normalizeDisplayAlign( value ) {
return [ 'left', 'center', 'right' ].includes( value ) ? value : 'center';
}
export function terminalDimension( value ) {
const number = finiteNumber( value, 0, 1, 1000 );
return Number.isInteger( number ) && number > 0 ? number : undefined;
}
/** A bounded editor frame for old height-fitting shortcodes; never persisted. */
export function previewHeight( attributes ) {
if ( ! [ 'height', 'both' ].includes( attributes.fit ) ) return undefined;
-2
View File
@@ -61,8 +61,6 @@ export default function Preview( { source, attributes, attempt, onRetry } ) {
{ status === 'loading' && <div className="asciiplayer-editor__status" role="status"><Spinner />{ __( 'Loading recording…', 'wp-asciiplayer' ) }</div> }
{ status === 'error' && <Notice status="error" isDismissible={ false }><p>{ problem }</p><Button variant="secondary" onClick={ onRetry }>{ __( 'Retry preview', 'wp-asciiplayer' ) }</Button></Notice> }
<div className="asciiplayer-editor__player" style={ { height: previewHeight( attributes ) } } ref={ playerRef } aria-label={ __( 'Interactive terminal recording preview', 'wp-asciiplayer' ) } />
{ [ 'height', 'both' ].includes( attributes.fit ) && <p className="asciiplayer-editor__hint">{ __( 'Height-based previews use the shortcode’s fixed height when available, or a 320px editor frame. Published height still comes from your container style.', 'wp-asciiplayer' ) }</p> }
<p className="asciiplayer-editor__hint">{ __( 'Use the player controls to play, pause, and seek. Autoplay is disabled only in the editor.', 'wp-asciiplayer' ) }</p>
</div>
);
}
+63 -1
View File
@@ -1,15 +1,77 @@
.wp-block-lamgc-asciiplayer,
.asciiplayer-container {
box-sizing: border-box;
max-width: 100%;
margin-inline: auto;
}
/*! rtl:begin:ignore */
.asciiplayer-container .ap-wrapper {
text-align: left;
}
/*! rtl:end:ignore */
.asciiplayer-container[data-ap-status="error"] .asciiplayer-error {
padding: 1em;
border: 1px solid #b32d2e;
}
/* Keep placement separate from terminal dimensions and from theme floats. */
.asciiplayer-figure {
box-sizing: border-box;
max-width: 100%;
min-width: 0;
margin-block: 0 1em;
}
/*! rtl:begin:ignore */
.asciiplayer-figure.asciiplayer-align-left { margin-left: 0 !important; margin-right: auto !important; }
.asciiplayer-figure.asciiplayer-align-center { margin-left: auto !important; margin-right: auto !important; }
.asciiplayer-figure.asciiplayer-align-right { margin-left: auto !important; margin-right: 0 !important; }
/*! rtl:end:ignore */
.asciiplayer-figure > .asciiplayer-container { width: 100%; }
.asciiplayer-figure > figcaption {
margin-block: 0.5em 0;
font-size: 0.875em;
line-height: 1.5;
text-align: center;
overflow-wrap: anywhere;
}
/* Terminal glyph geometry must inherit the size calculated by the player,
* not a theme's code-block typography. Scope every reset to our mount points. */
:is(.asciiplayer-container, .asciiplayer-editor__player) .ap-term pre.ap-term-text {
border: 0 !important;
border-radius: 0 !important;
box-shadow: none !important;
padding: 0 !important;
margin: 0 !important;
font-family: inherit !important;
font-size: inherit !important;
line-height: var(--term-line-height) !important;
white-space: pre !important;
word-break: normal !important;
overflow-wrap: normal !important;
text-transform: none !important;
}
:is(.asciiplayer-container, .asciiplayer-editor__player) .ap-term .ap-line,
:is(.asciiplayer-container, .asciiplayer-editor__player) .ap-term .ap-line span {
font-family: inherit !important;
font-size: inherit !important;
line-height: inherit !important;
letter-spacing: normal !important;
white-space: pre !important;
text-transform: none !important;
}
:is(.asciiplayer-container, .asciiplayer-editor__player) .ap-control-bar button.ap-button {
border: 0 !important;
box-shadow: none !important;
border-radius: 0;
}
:is(.asciiplayer-container, .asciiplayer-editor__player) .ap-control-bar button.ap-button:focus-visible {
outline: 2px solid currentColor;
outline-offset: 3px;
}