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

[ New Inline Format ] Introduce Inline Time Format for improved SEO and accessibility #67751

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/format-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"@wordpress/icons": "*",
"@wordpress/private-apis": "*",
"@wordpress/rich-text": "*",
"@wordpress/url": "*"
"@wordpress/url": "*",
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
"locutus": "2.0.32"
},
"peerDependencies": {
"react": "^18.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/format-library/src/default-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { link } from './link';
import { strikethrough } from './strikethrough';
import { underline } from './underline';
import { textColor } from './text-color';
import { time } from './time';
import { subscript } from './subscript';
import { superscript } from './superscript';
import { keyboard } from './keyboard';
Expand All @@ -25,6 +26,7 @@ export default [
strikethrough,
underline,
textColor,
time,
subscript,
superscript,
keyboard,
Expand Down
98 changes: 98 additions & 0 deletions packages/format-library/src/time/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* External dependencies
*/
import { gmdate, strtotime } from 'locutus/php/datetime';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { toggleFormat } from '@wordpress/rich-text';
import {
RichTextToolbarButton,
RichTextShortcut,
} from '@wordpress/block-editor';
import { postDate as icon } from '@wordpress/icons';

const name = 'core/time';
const title = __( 'Time' );

export const time = {
name,
title,
tagName: 'time',
className: null,
attributes: {
datetime: 'datetime',
},
edit( { isActive, value, onChange, onFocus } ) {
function onClick() {
const dateDescription = value.text
.slice( value.start, value.end )
.trim();

// Exit early if no selection or format is already active
if ( ! dateDescription || isActive ) {
onChange(
toggleFormat( value, {
type: name,
} )
);
return;
}

// Clean up the date string
const dateCleaned = dateDescription
.replace( 'at ', '' ) // Remove "at"
.replace( 'UTC', 'GMT' ); // Replace "UTC" with "GMT"

// Parse the date string using strtotime
const timestamp = strtotime( dateCleaned );
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved

// If parsing fails, toggle format without enhancement
if ( ! timestamp ) {
onChange(
toggleFormat( value, {
type: name,
} )
);
return;
}

// Format the datetime attributes using gmdate
const datetime = gmdate( 'c', timestamp ); // ISO 8601 format
const datetimeISO = gmdate( 'Ymd\\THi', timestamp ); // Compact ISO format

// Apply the format with parsed datetime attributes
onChange(
toggleFormat( value, {
type: name,
attributes: {
datetime,
'data-iso': datetimeISO,
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
style: 'text-decoration: underline; text-decoration-style: dotted',
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
},
} )
);

onFocus();
}

return (
<>
<RichTextShortcut
type="access"
character="d"
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
onUse={ onClick }
/>
<RichTextToolbarButton
icon={ icon }
title={ title }
onClick={ onClick }
isActive={ isActive }
role="menuitemcheckbox"
/>
</>
);
},
};
Loading