diff --git a/eslint.config.mjs b/eslint.config.mjs index d402bfe..26d054c 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -2,8 +2,16 @@ import wordpress from '@wordpress/eslint-plugin'; import globals from 'globals'; export default [ - ...wordpress.configs.custom, - ...wordpress.configs.es5, + ...wordpress.configs.recommended.map( ( config ) => ( { + ...config, + files: [ 'scripts/block.js' ], + } ) ), + + ...wordpress.configs.es5.map( ( config ) => ( { + ...config, + files: [ 'scripts/liveticker.js' ], + } ) ), + ...wordpress.configs.i18n, ...wordpress.configs.jsdoc, { diff --git a/scripts/block.js b/scripts/block.js index fae7c90..fca50a4 100644 --- a/scripts/block.js +++ b/scripts/block.js @@ -3,37 +3,37 @@ * * Gutenberg Block to integrate the liveticker widget without shortcode. */ -( function() { - var __ = wp.i18n.__; - var registerBlockType = wp.blocks.registerBlockType; - var registerStore = wp.data.registerStore; - var withSelect = wp.data.withSelect; - var el = wp.element.createElement; +{ + const __ = wp.i18n.__; + const registerBlockType = wp.blocks.registerBlockType; + const registerStore = wp.data.registerStore; + const withSelect = wp.data.withSelect; + const el = wp.element.createElement; /** * Datastore actions. */ - var actions = { - setTickers: function( tickers ) { + const actions = { + setTickers(tickers) { return { type: 'SET_TICKERS', - tickers: tickers, + tickers, }; }, - getTickers: function( path ) { + getTickers(path) { return { type: 'RECEIVE_TICKERS', - path: path, + path, }; }, }; - registerStore( 'scliveticker/ticker', { - reducer: function( state, action ) { - if ( undefined === state ) { + registerStore('scliveticker/ticker', { + reducer(state, action) { + if (undefined === state) { state = { tickers: null }; } - switch ( action.type ) { + switch (action.type) { case 'SET_TICKERS': state.tickers = action.tickers; return state; @@ -44,35 +44,37 @@ return state; }, - actions: actions, + actions, selectors: { - receiveTickers: function( state ) { + receiveTickers(state) { return state.tickers; }, }, resolvers: { - receiveTickers: function() { - return wp.apiFetch( { path: '/wp/v2/scliveticker_ticker' } ).then( function( tickers ) { - return actions.setTickers( tickers.map( function( t ) { - return { - name: t.name, - slug: t.slug, - }; - } ) ); - } ); + receiveTickers() { + return wp + .apiFetch({ path: '/wp/v2/scliveticker_ticker' }) + .then((tickers) => + actions.setTickers( + tickers.map((t) => { + return { + name: t.name, + slug: t.slug, + }; + }) + ) + ); }, }, - } ); + }); - registerBlockType( 'scliveticker/ticker', { - title: __( 'Liveticker', 'stklcode-liveticker' ), + registerBlockType('scliveticker/ticker', { + title: __('Liveticker', 'stklcode-liveticker'), icon: 'rss', category: 'widgets', - keywords: [ - __( 'Liveticker', 'stklcode-liveticker' ), - ], + keywords: [__('Liveticker', 'stklcode-liveticker')], attributes: { ticker: { type: 'string', @@ -91,125 +93,122 @@ // implicit default: 'desc', left empty here for backwards compatibility of the block }, }, - edit: withSelect( function( select ) { + edit: withSelect((select) => { return { - tickers: select( 'scliveticker/ticker' ).receiveTickers(), + tickers: select('scliveticker/ticker').receiveTickers(), }; - } )( function( props ) { - var label = [ - el( - wp.components.Dashicon, - { icon: 'rss' }, - ), - __( 'Liveticker', 'stklcode-liveticker' ), + })((props) => { + const label = [ + el(wp.components.Dashicon, { icon: 'rss' }), + __('Liveticker', 'stklcode-liveticker'), ]; - var content; - if ( null === props.tickers ) { + let content; + if (null === props.tickers) { // Tickers not yet loaded. content = [ el( 'span', { className: 'components-base-control label' }, - label, + label ), - el( wp.components.Spinner ), + el(wp.components.Spinner), ]; - } else if ( 0 === props.tickers.length ) { + } else if (0 === props.tickers.length) { // No tickers available. content = [ el( 'span', { className: 'components-base-control label' }, - label, + label + ), + el( + 'span', + null, + __('No tickers available', 'stklcode-liveticker') ), - el( 'span', null, __( 'No tickers available', 'stklcode-liveticker' ) ), ]; } else { // Tickers loaded and available. - if ( 0 === props.attributes.ticker.length && props.tickers.length > 0 ) { - props.attributes.ticker = props.tickers[ 0 ].slug; + if ( + 0 === props.attributes.ticker.length && + props.tickers.length > 0 + ) { + props.attributes.ticker = props.tickers[0].slug; } content = [ - el( - wp.components.SelectControl, - { - label: label, - value: props.attributes.ticker, - options: props.tickers.map( function( t ) { - return { - value: t.slug, - label: t.name, - }; - } ), - onChange: function( val ) { - props.setAttributes( { ticker: val } ); - }, + el(wp.components.SelectControl, { + label, + value: props.attributes.ticker, + options: props.tickers.map((t) => { + return { + value: t.slug, + label: t.name, + }; + }), + onChange(val) { + props.setAttributes({ ticker: val }); }, - ), - el( - wp.components.TextControl, - { - label: __( 'Number of Ticks', 'stklcode-liveticker' ), - type: 'number', - min: 1, - step: 1, - disabled: props.attributes.unlimited, - value: props.attributes.limit, - onChange: function( val ) { - props.setAttributes( { limit: Number( val ) } ); - }, + }), + el(wp.components.TextControl, { + label: __('Number of Ticks', 'stklcode-liveticker'), + type: 'number', + min: 1, + step: 1, + disabled: props.attributes.unlimited, + value: props.attributes.limit, + onChange(val) { + props.setAttributes({ limit: Number(val) }); }, - ), - el( - wp.components.CheckboxControl, - { - label: __( 'unlimited', 'stklcode-liveticker' ), - checked: props.attributes.unlimited, - onChange: function( val ) { - props.setAttributes( { unlimited: val } ); - }, + }), + el(wp.components.CheckboxControl, { + label: __('unlimited', 'stklcode-liveticker'), + checked: props.attributes.unlimited, + onChange(val) { + props.setAttributes({ unlimited: val }); }, - ), - el( - wp.components.SelectControl, - { - label: __( 'Output direction', 'stklcode-liveticker' ), - value: props.attributes.sort, - options: [ - { - value: 'desc', - label: __( 'newest first', 'stklcode-liveticker' ), - }, - { - value: 'asc', - label: __( 'oldest first', 'stklcode-liveticker' ), - }, - ], - onChange: function( val ) { - props.setAttributes( { sort: val } ); + }), + el(wp.components.SelectControl, { + label: __('Output direction', 'stklcode-liveticker'), + value: props.attributes.sort, + options: [ + { + value: 'desc', + label: __( + 'newest first', + 'stklcode-liveticker' + ), + }, + { + value: 'asc', + label: __( + 'oldest first', + 'stklcode-liveticker' + ), }, + ], + onChange(val) { + props.setAttributes({ sort: val }); }, - ), + }), ]; } return el( 'div', { className: props.className + ' components-placeholder' }, - content, - ); - } ), - save: function( props ) { - return el( - 'div', - { - className: 'sclt-ajax', - 'data-sclt-ticker': props.attributes.ticker, - 'data-sclt-limit': props.attributes.unlimited ? 0 : props.attributes.limit, - 'data-sclt-last': 0, - 'data-sclt-sort': props.attributes.sort, - }, + content ); + }), + save(props) { + return el('div', { + className: 'sclt-ajax', + 'data-sclt-ticker': props.attributes.ticker, + 'data-sclt-limit': props.attributes.unlimited + ? 0 + : props.attributes.limit, + 'data-sclt-last': 0, + 'data-sclt-sort': props.attributes.sort, + }); }, - } ); -}() ); + }); +} diff --git a/scripts/liveticker.js b/scripts/liveticker.js index 28902dc..371da27 100644 --- a/scripts/liveticker.js +++ b/scripts/liveticker.js @@ -146,7 +146,7 @@ ); } t.updating = false; - } catch ( e ) { + } catch { // eslint-disable-next-line no-console console.warn( 'Liveticker AJAX update failed, stopping automatic updates.' ); } @@ -249,7 +249,7 @@ try { // eslint-disable-next-line no-eval eval( script.innerHTML ); - } catch ( e ) { + } catch { // eslint-disable-next-line no-console console.warn( 'Failed to evaluate embedded script.' ); }