From 9c424691ca46c5f087cc6f89a106158e4e87618d Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 21 Apr 2022 09:38:42 +0400 Subject: [PATCH 1/3] Remove onChangeListener function --- .../components/editor-initialization/utils.js | 24 ------------------- 1 file changed, 24 deletions(-) delete mode 100644 packages/edit-post/src/components/editor-initialization/utils.js diff --git a/packages/edit-post/src/components/editor-initialization/utils.js b/packages/edit-post/src/components/editor-initialization/utils.js deleted file mode 100644 index 64bdc2f1b1b73b..00000000000000 --- a/packages/edit-post/src/components/editor-initialization/utils.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Given a selector returns a functions that returns the listener only - * if the returned value from the selector changes. - * - * @param {Function} selector Selector. - * @param {Function} listener Listener. - * @param {boolean} initial Flags whether listener should be invoked on - * initial call. - * - * @return {Function} Listener creator. - */ -export const onChangeListener = ( selector, listener, initial = false ) => { - let previousValue = selector(); - if ( initial ) { - listener( selector() ); - } - return () => { - const selectedValue = selector(); - if ( selectedValue !== previousValue ) { - previousValue = selectedValue; - listener( selectedValue ); - } - }; -}; From 15b338cc538d46f6032d00d3f379736175734eba Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 21 Apr 2022 09:43:10 +0400 Subject: [PATCH 2/3] Remove unused AdminNotices component --- .../src/components/admin-notices/index.js | 110 ------------------ .../components/admin-notices/test/index.js | 63 ---------- 2 files changed, 173 deletions(-) delete mode 100644 packages/edit-post/src/components/admin-notices/index.js delete mode 100644 packages/edit-post/src/components/admin-notices/test/index.js diff --git a/packages/edit-post/src/components/admin-notices/index.js b/packages/edit-post/src/components/admin-notices/index.js deleted file mode 100644 index 1906f65824f2c0..00000000000000 --- a/packages/edit-post/src/components/admin-notices/index.js +++ /dev/null @@ -1,110 +0,0 @@ -/** - * WordPress dependencies - */ -import { Component } from '@wordpress/element'; -import { withDispatch } from '@wordpress/data'; -import { store as noticesStore } from '@wordpress/notices'; - -/** - * Mapping of server-supported notice class names to an equivalent notices - * module status. - * - * @type {Map} - */ -const NOTICE_CLASS_STATUSES = { - 'notice-success': 'success', - updated: 'success', - 'notice-warning': 'warning', - 'notice-error': 'error', - error: 'error', - 'notice-info': 'info', -}; - -/** - * Returns an array of admin notice Elements. - * - * @return {Element[]} Admin notice elements. - */ -function getAdminNotices() { - // The order is reversed to match expectations of rendered order, since a - // NoticesList is itself rendered in reverse order (newest to oldest). - return Array.from( - document.querySelectorAll( '#wpbody-content > .notice' ) - ).reverse(); -} - -/** - * Given an admin notice Element, returns the relevant notice content HTML. - * - * @param {Element} element Admin notice element. - * - * @return {Element} Upgraded notice HTML. - */ -function getNoticeHTML( element ) { - const fragments = []; - - for ( const child of element.childNodes ) { - if ( child.nodeType !== child.ELEMENT_NODE ) { - const value = child.nodeValue.trim(); - if ( value ) { - fragments.push( child.nodeValue ); - } - } else if ( ! child.classList.contains( 'notice-dismiss' ) ) { - fragments.push( child.outerHTML ); - } - } - - return fragments.join( '' ); -} - -/** - * Given an admin notice Element, returns the upgraded status type, or - * undefined if one cannot be determined (i.e. one is not assigned). - * - * @param {Element} element Admin notice element. - * - * @return {?string} Upgraded status type. - */ -function getNoticeStatus( element ) { - for ( const className of element.classList ) { - if ( NOTICE_CLASS_STATUSES.hasOwnProperty( className ) ) { - return NOTICE_CLASS_STATUSES[ className ]; - } - } -} - -export class AdminNotices extends Component { - componentDidMount() { - this.convertNotices(); - } - - convertNotices() { - const { createNotice } = this.props; - getAdminNotices().forEach( ( element ) => { - // Convert and create. - const status = getNoticeStatus( element ); - const content = getNoticeHTML( element ); - const isDismissible = element.classList.contains( - 'is-dismissible' - ); - createNotice( status, content, { - speak: false, - __unstableHTML: true, - isDismissible, - } ); - - // Remove (now-redundant) admin notice element. - element.parentNode.removeChild( element ); - } ); - } - - render() { - return null; - } -} - -export default withDispatch( ( dispatch ) => { - const { createNotice } = dispatch( noticesStore ); - - return { createNotice }; -} )( AdminNotices ); diff --git a/packages/edit-post/src/components/admin-notices/test/index.js b/packages/edit-post/src/components/admin-notices/test/index.js deleted file mode 100644 index 792fe785b78ae0..00000000000000 --- a/packages/edit-post/src/components/admin-notices/test/index.js +++ /dev/null @@ -1,63 +0,0 @@ -/** - * External dependencies - */ -import renderer from 'react-test-renderer'; - -/** - * Internal dependencies - */ -import { AdminNotices } from '../'; - -describe( 'AdminNotices', () => { - beforeEach( () => { - // The superfluous whitespace is intentional in verifying expected - // outputs of (a) non-element first child of the element (whitespace - // text node) and (b) untrimmed content. - document.body.innerHTML = ` -
-
-

My notice text

-

My second line of text

- -
-
Warning
- -
- `; - } ); - - it( 'should upgrade notices', () => { - const createNotice = jest.fn(); - - renderer.create( ); - - expect( createNotice ).toHaveBeenCalledTimes( 2 ); - expect( createNotice.mock.calls[ 0 ] ).toEqual( [ - 'warning', - 'Warning', - { - speak: false, - __unstableHTML: true, - isDismissible: false, - }, - ] ); - expect( createNotice.mock.calls[ 1 ] ).toEqual( [ - 'success', - '

My notice text

My second line of text

', - { - speak: false, - __unstableHTML: true, - isDismissible: true, - }, - ] ); - - // Verify all but `