fix: harden CHRNX editor lifecycle
This commit is contained in:
12 files changed
+512
-95
No files matched your search
+20
-16
@@ -7,13 +7,25 @@
|
||||
} )( function () {
|
||||
'use strict';
|
||||
|
||||
const maxParticipants = 50;
|
||||
const maxParticipantLength = 100;
|
||||
|
||||
function getTranslator( wp ) {
|
||||
return wp.i18n && typeof wp.i18n.__ === 'function'
|
||||
? wp.i18n.__
|
||||
: function ( text ) {
|
||||
return text;
|
||||
};
|
||||
}
|
||||
|
||||
function participantsFromText( value ) {
|
||||
return String( value || '' )
|
||||
.split( /\r?\n/ )
|
||||
.map( function ( name ) {
|
||||
return name.trim();
|
||||
return Array.from( name.trim() ).slice( 0, maxParticipantLength ).join( '' );
|
||||
} )
|
||||
.filter( Boolean );
|
||||
.filter( Boolean )
|
||||
.slice( 0, maxParticipants );
|
||||
}
|
||||
|
||||
function participantsToText( value ) {
|
||||
@@ -53,15 +65,7 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
const editor = wp.data.select( 'core/editor' );
|
||||
if (
|
||||
! editor ||
|
||||
typeof editor.getCurrentPostType !== 'function' ||
|
||||
typeof editor.getEditedPostAttribute !== 'function' ||
|
||||
editor.getCurrentPostType() !== 'post'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const __ = getTranslator( wp );
|
||||
|
||||
function PodcastPanel() {
|
||||
const editorState = wp.data.useSelect( function ( select ) {
|
||||
@@ -107,9 +111,9 @@
|
||||
|
||||
return wp.element.createElement(
|
||||
wp.editPost.PluginDocumentSettingPanel,
|
||||
{ name: 'chrnx-podcast', title: 'Podcast episode' },
|
||||
{ name: 'chrnx-podcast', title: __( 'Podcast episode', 'chrnx' ) },
|
||||
wp.element.createElement( wp.components.TextControl, {
|
||||
label: 'Episode number',
|
||||
label: __( 'Episode number', 'chrnx' ),
|
||||
type: 'number',
|
||||
min: 0,
|
||||
value: meta.chrnx_episode_number ?? '',
|
||||
@@ -122,21 +126,21 @@
|
||||
},
|
||||
} ),
|
||||
wp.element.createElement( wp.components.TextControl, {
|
||||
label: 'Duration',
|
||||
label: __( 'Duration', 'chrnx' ),
|
||||
value: meta.chrnx_episode_duration || '',
|
||||
onChange( value ) {
|
||||
setMeta( 'chrnx_episode_duration', value );
|
||||
},
|
||||
} ),
|
||||
wp.element.createElement( wp.components.TextareaControl, {
|
||||
label: 'Participants (one per line)',
|
||||
label: __( 'Participants (one per line)', 'chrnx' ),
|
||||
value: participantsToText( meta.chrnx_episode_participants ),
|
||||
onChange( value ) {
|
||||
setMeta( 'chrnx_episode_participants', participantsFromText( value ) );
|
||||
},
|
||||
} ),
|
||||
wp.element.createElement( wp.components.TextControl, {
|
||||
label: 'Canonical episode URL',
|
||||
label: __( 'Canonical episode URL', 'chrnx' ),
|
||||
type: 'url',
|
||||
value: meta.canonical_episode_url || '',
|
||||
onChange( value ) {
|
||||
|
||||
@@ -13,6 +13,14 @@
|
||||
{ name: 'chrnx_show', restBase: 'show', title: 'Show' },
|
||||
];
|
||||
|
||||
function getTranslator( wp ) {
|
||||
return wp.i18n && typeof wp.i18n.__ === 'function'
|
||||
? wp.i18n.__
|
||||
: function ( text ) {
|
||||
return text;
|
||||
};
|
||||
}
|
||||
|
||||
function orderTerms( terms ) {
|
||||
if ( ! Array.isArray( terms ) ) {
|
||||
return [];
|
||||
@@ -57,15 +65,12 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
const editor = wp.data.select( 'core/editor' );
|
||||
if (
|
||||
! editor ||
|
||||
typeof editor.getCurrentPostType !== 'function' ||
|
||||
typeof editor.getEditedPostAttribute !== 'function' ||
|
||||
editor.getCurrentPostType() !== 'post'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const __ = getTranslator( wp );
|
||||
const titles = {
|
||||
chrnx_topic: __( 'Topic', 'chrnx' ),
|
||||
chrnx_language: __( 'Language', 'chrnx' ),
|
||||
chrnx_show: __( 'Show', 'chrnx' ),
|
||||
};
|
||||
|
||||
const panelDispatch = wp.data.dispatch( 'core/edit-post' );
|
||||
if ( panelDispatch && typeof panelDispatch.removeEditorPanel === 'function' ) {
|
||||
@@ -98,7 +103,7 @@
|
||||
return {
|
||||
selected: currentEditor.getEditedPostAttribute( taxonomy.restBase ),
|
||||
terms: core.getEntityRecords( 'taxonomy', taxonomy.name, {
|
||||
per_page: -1,
|
||||
per_page: 100,
|
||||
orderby: 'name',
|
||||
order: 'asc',
|
||||
} ),
|
||||
@@ -118,7 +123,7 @@
|
||||
}
|
||||
|
||||
const selected = editorState.taxonomies[ index ].selected;
|
||||
const options = [ { label: 'Not set', value: '' } ].concat(
|
||||
const options = [ { label: __( 'Not set', 'chrnx' ), value: '' } ].concat(
|
||||
terms.map( function ( term ) {
|
||||
return { label: term.name, value: String( term.id ) };
|
||||
} ),
|
||||
@@ -129,10 +134,10 @@
|
||||
{
|
||||
key: taxonomy.name,
|
||||
name: 'chrnx-' + taxonomy.restBase,
|
||||
title: taxonomy.title,
|
||||
title: titles[ taxonomy.name ],
|
||||
},
|
||||
wp.element.createElement( wp.components.RadioControl, {
|
||||
label: taxonomy.title,
|
||||
label: titles[ taxonomy.name ],
|
||||
selected: Array.isArray( selected ) && selected.length ? String( selected[ 0 ] ) : '',
|
||||
options,
|
||||
onChange( value ) {
|
||||
|
||||
Reference in new issue
Block a user