From 200bd3616ca0d7273f70be192647b69118f4db28 Mon Sep 17 00:00:00 2001 From: poojabhimani12 <82029773+poojabhimani12@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:31:43 +0530 Subject: [PATCH 001/159] Add Inline comment experimental flag --- lib/experimental/editor-settings.php | 3 ++ lib/experiments-page.php | 12 +++++ .../src/components/block-toolbar/index.js | 4 ++ packages/block-editor/src/components/index.js | 1 + .../src/components/inline-comment/comment.js | 44 +++++++++++++++++++ .../src/components/inline-comment/index.js | 1 + 6 files changed, 65 insertions(+) create mode 100644 packages/block-editor/src/components/inline-comment/comment.js create mode 100644 packages/block-editor/src/components/inline-comment/index.js diff --git a/lib/experimental/editor-settings.php b/lib/experimental/editor-settings.php index f5d40ae8a2110c..d37964d89aca8a 100644 --- a/lib/experimental/editor-settings.php +++ b/lib/experimental/editor-settings.php @@ -28,6 +28,9 @@ function gutenberg_enable_experiments() { if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) { wp_add_inline_script( 'wp-block-library', 'window.__experimentalDisableTinymce = true', 'before' ); } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-inline-comment', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableInlineComment = true', 'before' ); + } } add_action( 'admin_init', 'gutenberg_enable_experiments' ); diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 08bfc283dac2e2..cceaf51c623663 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -127,6 +127,18 @@ function gutenberg_initialize_experiments_settings() { ) ); + add_settings_field( + 'gutenberg-inline-comment', + __( 'Enable Inline Comment', 'gutenberg' ), + 'gutenberg_display_experiment_field', + 'gutenberg-experiments', + 'gutenberg_experiments_section', + array( + 'label' => __( 'Enable Inline Comment', 'gutenberg' ), + 'id' => 'gutenberg-inline-comment', + ) + ); + register_setting( 'gutenberg-experiments', 'gutenberg-experiments' diff --git a/packages/block-editor/src/components/block-toolbar/index.js b/packages/block-editor/src/components/block-toolbar/index.js index 8d28bd001670fc..1c9a0a485edb83 100644 --- a/packages/block-editor/src/components/block-toolbar/index.js +++ b/packages/block-editor/src/components/block-toolbar/index.js @@ -38,6 +38,7 @@ import Shuffle from './shuffle'; import BlockBindingsIndicator from '../block-bindings-toolbar-indicator'; import { useHasBlockToolbar } from './use-has-block-toolbar'; import { canBindBlock } from '../../hooks/use-bindings-attributes'; +import { InlineCommentToolbar } from '../inline-comment'; /** * Renders the block toolbar. * @@ -188,6 +189,9 @@ export function PrivateBlockToolbar( { clientIds={ blockClientIds } hideDragHandle={ hideDragHandle } /> + ) } diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index 5263ca3332b250..4e572045e66df1 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -161,6 +161,7 @@ export { default as __experimentalBlockPatternsList } from './block-patterns-lis export { default as __experimentalPublishDateTimePicker } from './publish-date-time-picker'; export { default as __experimentalInspectorPopoverHeader } from './inspector-popover-header'; export { useBlockEditingMode } from './block-editing-mode'; +export { InlineCommentToolbar as __experimentalInlineComment } from './inline-comment'; /* * State Related Components diff --git a/packages/block-editor/src/components/inline-comment/comment.js b/packages/block-editor/src/components/inline-comment/comment.js new file mode 100644 index 00000000000000..548935f82b9527 --- /dev/null +++ b/packages/block-editor/src/components/inline-comment/comment.js @@ -0,0 +1,44 @@ +/** + * WordPress dependencies + */ +import { Component } from '@wordpress/element'; +import { comment } from '@wordpress/icons'; +import { ToolbarGroup, ToolbarButton } from '@wordpress/components'; +import { displayShortcut } from '@wordpress/keycodes'; +import { __ } from '@wordpress/i18n'; +const isInlineCommentExperimentEnabled = + window?.__experimentalEnableInlineComment; + +class InlineCommentToolbar extends Component { + constructor( props ) { + super( props ); + this.onToggle = this.onToggle.bind( this ); + } + onToggle() { + // eslint-disable-next-line no-console + console.log( 'comment toggled' ); + } + + render() { + return ( + <> + { isInlineCommentExperimentEnabled && ( + <> + + + + + ) } + + ); + } +} +export default InlineCommentToolbar; diff --git a/packages/block-editor/src/components/inline-comment/index.js b/packages/block-editor/src/components/inline-comment/index.js new file mode 100644 index 00000000000000..185aa24f384de2 --- /dev/null +++ b/packages/block-editor/src/components/inline-comment/index.js @@ -0,0 +1 @@ +export { default as InlineCommentToolbar } from './comment'; From 08d520f646b1d3cb7a1c0f1715b1f3d9ea7a47cd Mon Sep 17 00:00:00 2001 From: akashdhawade1991 <68046117+akashdhawade1991@users.noreply.github.com> Date: Tue, 14 May 2024 10:48:58 -0400 Subject: [PATCH 002/159] Experimental block commenting --- lib/experimental/editor-settings.php | 4 +- lib/experiments-page.php | 8 +- .../src/components/block-toolbar/index.js | 4 - packages/block-editor/src/components/index.js | 1 - .../src/components/inline-comment/comment.js | 44 ------ .../src/components/inline-comment/index.js | 1 - packages/format-library/src/comment/index.js | 90 ++++++++++++ .../src/comment/modal/comment-board.js | 138 ++++++++++++++++++ .../format-library/src/comment/style.scss | 14 ++ .../format-library/src/default-formats.js | 2 + packages/format-library/src/style.scss | 1 + 11 files changed, 251 insertions(+), 56 deletions(-) delete mode 100644 packages/block-editor/src/components/inline-comment/comment.js delete mode 100644 packages/block-editor/src/components/inline-comment/index.js create mode 100644 packages/format-library/src/comment/index.js create mode 100644 packages/format-library/src/comment/modal/comment-board.js create mode 100644 packages/format-library/src/comment/style.scss diff --git a/lib/experimental/editor-settings.php b/lib/experimental/editor-settings.php index d37964d89aca8a..17a9d78042c72c 100644 --- a/lib/experimental/editor-settings.php +++ b/lib/experimental/editor-settings.php @@ -28,8 +28,8 @@ function gutenberg_enable_experiments() { if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) { wp_add_inline_script( 'wp-block-library', 'window.__experimentalDisableTinymce = true', 'before' ); } - if ( $gutenberg_experiments && array_key_exists( 'gutenberg-inline-comment', $gutenberg_experiments ) ) { - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableInlineComment = true', 'before' ); + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-comment', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockComment = true', 'before' ); } } diff --git a/lib/experiments-page.php b/lib/experiments-page.php index cceaf51c623663..1838204e8dc262 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -128,14 +128,14 @@ function gutenberg_initialize_experiments_settings() { ); add_settings_field( - 'gutenberg-inline-comment', - __( 'Enable Inline Comment', 'gutenberg' ), + 'gutenberg-block-comment', + __( 'Enable block Comment', 'gutenberg' ), 'gutenberg_display_experiment_field', 'gutenberg-experiments', 'gutenberg_experiments_section', array( - 'label' => __( 'Enable Inline Comment', 'gutenberg' ), - 'id' => 'gutenberg-inline-comment', + 'label' => __( 'Enable Block Comment', 'gutenberg' ), + 'id' => 'gutenberg-block-comment', ) ); diff --git a/packages/block-editor/src/components/block-toolbar/index.js b/packages/block-editor/src/components/block-toolbar/index.js index 1c9a0a485edb83..8d28bd001670fc 100644 --- a/packages/block-editor/src/components/block-toolbar/index.js +++ b/packages/block-editor/src/components/block-toolbar/index.js @@ -38,7 +38,6 @@ import Shuffle from './shuffle'; import BlockBindingsIndicator from '../block-bindings-toolbar-indicator'; import { useHasBlockToolbar } from './use-has-block-toolbar'; import { canBindBlock } from '../../hooks/use-bindings-attributes'; -import { InlineCommentToolbar } from '../inline-comment'; /** * Renders the block toolbar. * @@ -189,9 +188,6 @@ export function PrivateBlockToolbar( { clientIds={ blockClientIds } hideDragHandle={ hideDragHandle } /> - ) } diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index 4e572045e66df1..5263ca3332b250 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -161,7 +161,6 @@ export { default as __experimentalBlockPatternsList } from './block-patterns-lis export { default as __experimentalPublishDateTimePicker } from './publish-date-time-picker'; export { default as __experimentalInspectorPopoverHeader } from './inspector-popover-header'; export { useBlockEditingMode } from './block-editing-mode'; -export { InlineCommentToolbar as __experimentalInlineComment } from './inline-comment'; /* * State Related Components diff --git a/packages/block-editor/src/components/inline-comment/comment.js b/packages/block-editor/src/components/inline-comment/comment.js deleted file mode 100644 index 548935f82b9527..00000000000000 --- a/packages/block-editor/src/components/inline-comment/comment.js +++ /dev/null @@ -1,44 +0,0 @@ -/** - * WordPress dependencies - */ -import { Component } from '@wordpress/element'; -import { comment } from '@wordpress/icons'; -import { ToolbarGroup, ToolbarButton } from '@wordpress/components'; -import { displayShortcut } from '@wordpress/keycodes'; -import { __ } from '@wordpress/i18n'; -const isInlineCommentExperimentEnabled = - window?.__experimentalEnableInlineComment; - -class InlineCommentToolbar extends Component { - constructor( props ) { - super( props ); - this.onToggle = this.onToggle.bind( this ); - } - onToggle() { - // eslint-disable-next-line no-console - console.log( 'comment toggled' ); - } - - render() { - return ( - <> - { isInlineCommentExperimentEnabled && ( - <> - - - - - ) } - - ); - } -} -export default InlineCommentToolbar; diff --git a/packages/block-editor/src/components/inline-comment/index.js b/packages/block-editor/src/components/inline-comment/index.js deleted file mode 100644 index 185aa24f384de2..00000000000000 --- a/packages/block-editor/src/components/inline-comment/index.js +++ /dev/null @@ -1 +0,0 @@ -export { default as InlineCommentToolbar } from './comment'; diff --git a/packages/format-library/src/comment/index.js b/packages/format-library/src/comment/index.js new file mode 100644 index 00000000000000..454e2e992958f0 --- /dev/null +++ b/packages/format-library/src/comment/index.js @@ -0,0 +1,90 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { useState } from '@wordpress/element'; +import { + RichTextToolbarButton, + store as blockEditorStore, +} from '@wordpress/block-editor'; +import { comment as commentIcon } from '@wordpress/icons'; +import { useSelect } from '@wordpress/data'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { addFilter } from '@wordpress/hooks'; + +/** + * Internal dependencies + */ +import CommentBoard from './modal/comment-board'; + +const isBlockCommentExperimentEnabled = + window?.__experimentalEnableBlockComment; + +const name = 'core/comment'; +const title = __( 'Comment' ); + +export const comment = { + name, + title, + tagName: 'comment', + className: null, + attributes: { + comment: 'comment', + }, + edit: Edit, +}; + +function Edit( { isActive, contentRef } ) { + // State to manage the visibility of the comment modal. + const [ isCommentModalVisible, setIsCommentModalVisible ] = + useState( false ); + + // Get the selected block client ID. + const { selectedBlockClientId } = useSelect( ( select ) => { + return { + selectedBlockClientId: + select( blockEditorStore ).getSelectedBlockClientId(), + }; + }, [] ); + + // Function to toggle the comment modal visibility. + const toggleCommentModal = () => { + setIsCommentModalVisible( ( state ) => ! state ); + }; + + return ( + <> + { isBlockCommentExperimentEnabled && ( + + ) } + { isCommentModalVisible && ( + + ) } + + ); +} + +// Add the 'comment' attribute to all blocks. +if ( isBlockCommentExperimentEnabled ) { + addFilter( 'blocks.registerBlockType', 'core/comment', ( settings ) => { + settings.attributes = { + ...settings.attributes, + blockComments: { + type: 'array', + default: [], + }, + }; + + return settings; + } ); +} diff --git a/packages/format-library/src/comment/modal/comment-board.js b/packages/format-library/src/comment/modal/comment-board.js new file mode 100644 index 00000000000000..a275cfaff18903 --- /dev/null +++ b/packages/format-library/src/comment/modal/comment-board.js @@ -0,0 +1,138 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { + Popover, + TextControl, + Button, + __experimentalHStack as HStack, + __experimentalVStack as VStack, +} from '@wordpress/components'; +import { useAnchor } from '@wordpress/rich-text'; +import { commentAuthorAvatar as userIcon, Icon } from '@wordpress/icons'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { useState } from '@wordpress/element'; +import { store as blockEditorStore } from '@wordpress/block-editor'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { dateI18n, format, getSettings } from '@wordpress/date'; + +const CommentBoard = ( { onClose, contentRef, clientId } ) => { + // Get the anchor for the popover. + const popoverAnchor = useAnchor( { + editableContentElement: contentRef.current, + } ); + + // State to manage the comment input. + const [ comment, setComment ] = useState( '' ); + + // Get the current user and block comments. + const { currentUser, blockComments } = useSelect( + ( select ) => { + const { getCurrentUser, getBlockAttributes } = + select( blockEditorStore ); + + return { + currentUser: getCurrentUser()?.name, + blockComments: + getBlockAttributes( clientId )?.blockComments || [], + }; + }, + [ clientId ] + ); + + const { updateBlockAttributes } = useDispatch( blockEditorStore ); + + // Function to save the comment. + const saveComment = () => { + updateBlockAttributes( clientId, { + blockComments: [ + ...blockComments, + { + userName: currentUser, + comment, + date: Date.now(), + }, + ], + } ); + + setComment( '' ); + }; + + // Get the date time format from WordPress settings. + const dateTimeFormat = getSettings().formats.datetime; + + return ( + + + { blockComments.length && + blockComments.map( + ( { userName, inputComment, timestamp } ) => ( + + + + + { userName } + + + +

{ inputComment }

+
+ ) + ) } + + { blockComments.length === 0 && ( + + + { currentUser } + + ) } + setComment( val ) } + placeholder={ __( 'Comment or add others with @' ) } + /> + + + + + )} + + ); +}; + +export default DiscussionsBoard; diff --git a/packages/format-library/src/comment/style.scss b/packages/block-library/src/collab/editor.scss similarity index 59% rename from packages/format-library/src/comment/style.scss rename to packages/block-library/src/collab/editor.scss index 87eeae49931854..35327ce3ba2173 100644 --- a/packages/format-library/src/comment/style.scss +++ b/packages/block-library/src/collab/editor.scss @@ -11,4 +11,13 @@ .comment-board__comment { border-bottom: 1px solid $gray-300; } + + &__resolved { + align-self: end; + } +} + +/* Targeting class starting with .block-editor-collab__ */ +[class*="block-editor-collab__"] { + border: 2px solid #ffea00; } diff --git a/packages/block-library/src/collab/format.js b/packages/block-library/src/collab/format.js new file mode 100644 index 00000000000000..1124f24fc4d132 --- /dev/null +++ b/packages/block-library/src/collab/format.js @@ -0,0 +1,56 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { useState } from '@wordpress/element'; +import { RichTextToolbarButton } from '@wordpress/block-editor'; +import { comment as commentIcon } from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import DiscussionBoard from './discussion-board'; + +const isBlockCommentExperimentEnabled = + window?.__experimentalEnableBlockComment; + +export const formatName = 'core/add-comment'; +const title = __( 'Add comment' ); + +export const format = { + title, + tagName: 'div', + className: null, + attributes: {}, + edit: Edit, +}; + +function Edit( { isActive, contentRef } ) { + // State to manage the visibility of the discussion board. + const [ isDiscussionBoardVisible, setIsDiscussionBoardVisible ] = useState( false ); + + // Function to toggle the visibility of the discussion board. + const toggleDiscussionBoardVisibility = () => { + setIsDiscussionBoardVisible( ( state ) => ! state ); + }; + + return ( + <> + { isBlockCommentExperimentEnabled && + toggleDiscussionBoardVisibility() } + role="menuitemcheckbox" + title={ title } + /> + } + { isDiscussionBoardVisible && + + } + + ); +} diff --git a/packages/block-library/src/collab/index.js b/packages/block-library/src/collab/index.js new file mode 100644 index 00000000000000..71fedc547a41a7 --- /dev/null +++ b/packages/block-library/src/collab/index.js @@ -0,0 +1,25 @@ +/** + * WordPress dependencies + */ +import { commentContent as icon } from '@wordpress/icons'; +import { registerFormatType } from '@wordpress/rich-text'; + +/** + * Internal dependencies + */ +import initBlock from '../utils/init-block'; +import metadata from './block.json'; +import { formatName, format } from './format'; + +const { name } = metadata; + +export { metadata, name }; + +registerFormatType( formatName, format ); + +export const settings = { + icon, + edit: () => {}, +}; + +export const init = () => initBlock({ name, metadata, settings }); diff --git a/packages/block-library/src/collab/index.php b/packages/block-library/src/collab/index.php new file mode 100644 index 00000000000000..3e9ba5534fcc34 --- /dev/null +++ b/packages/block-library/src/collab/index.php @@ -0,0 +1,35 @@ + true ] ); + + foreach ( $post_types as $post_type ) { + // Only register the meta field if the post type supports the editor, custom fields, and revisions. + if ( + post_type_supports( $post_type, 'editor' ) && + post_type_supports( $post_type, 'custom-fields' ) && + post_type_supports( $post_type, 'revisions' ) + ) { + register_post_meta( + $post_type, + 'collab', + [ + 'show_in_rest' => true, + 'single' => true, + 'type' => 'string', + 'revisions_enabled' => true, + ] + ); + } + } +} + +/* + * Most post types are registered at priority 10, so use priority 20 here in + * order to catch them. +*/ +add_action( 'init', 'register_block_collab_post_meta', 20 ); diff --git a/packages/block-library/src/collab/init.js b/packages/block-library/src/collab/init.js new file mode 100644 index 00000000000000..79f0492c2cb2f8 --- /dev/null +++ b/packages/block-library/src/collab/init.js @@ -0,0 +1,6 @@ +/** + * Internal dependencies + */ +import { init } from './'; + +export default init(); diff --git a/packages/block-library/src/editor.scss b/packages/block-library/src/editor.scss index 0bca3e23028c50..5db7f77feaa0ee 100644 --- a/packages/block-library/src/editor.scss +++ b/packages/block-library/src/editor.scss @@ -6,6 +6,7 @@ @import "./buttons/editor.scss"; @import "./categories/editor.scss"; @import "./code/editor.scss"; +@import "./collab/editor.scss"; @import "./columns/editor.scss"; @import "./comments/editor.scss"; @import "./comment-author-avatar/editor.scss"; diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 9cb2f44d05eb9b..6644d1a20ac456 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -29,6 +29,7 @@ import * as calendar from './calendar'; import * as categories from './categories'; import * as classic from './freeform'; import * as code from './code'; +import * as collab from './collab'; import * as column from './column'; import * as columns from './columns'; import * as comments from './comments'; @@ -147,6 +148,7 @@ const getAllBlocks = () => { calendar, categories, code, + collab, column, columns, commentAuthorAvatar, diff --git a/packages/format-library/CHANGELOG.md b/packages/format-library/CHANGELOG.md index f313912c274ba0..ef7f26e376cd22 100644 --- a/packages/format-library/CHANGELOG.md +++ b/packages/format-library/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +## 4.35.0 (2024-05-16) + ## 4.34.0 (2024-05-02) ## 4.33.0 (2024-04-19) diff --git a/packages/format-library/package.json b/packages/format-library/package.json index 94fce380c42caa..9774a1e55d7c58 100644 --- a/packages/format-library/package.json +++ b/packages/format-library/package.json @@ -1,6 +1,6 @@ { "name": "@wordpress/format-library", - "version": "4.34.0", + "version": "4.35.0", "description": "Format library for the WordPress editor.", "author": "The WordPress Contributors", "license": "GPL-2.0-or-later", diff --git a/packages/format-library/src/comment/index.js b/packages/format-library/src/comment/index.js deleted file mode 100644 index 454e2e992958f0..00000000000000 --- a/packages/format-library/src/comment/index.js +++ /dev/null @@ -1,90 +0,0 @@ -/** - * WordPress dependencies - */ -import { __ } from '@wordpress/i18n'; -import { useState } from '@wordpress/element'; -import { - RichTextToolbarButton, - store as blockEditorStore, -} from '@wordpress/block-editor'; -import { comment as commentIcon } from '@wordpress/icons'; -import { useSelect } from '@wordpress/data'; -// eslint-disable-next-line import/no-extraneous-dependencies -import { addFilter } from '@wordpress/hooks'; - -/** - * Internal dependencies - */ -import CommentBoard from './modal/comment-board'; - -const isBlockCommentExperimentEnabled = - window?.__experimentalEnableBlockComment; - -const name = 'core/comment'; -const title = __( 'Comment' ); - -export const comment = { - name, - title, - tagName: 'comment', - className: null, - attributes: { - comment: 'comment', - }, - edit: Edit, -}; - -function Edit( { isActive, contentRef } ) { - // State to manage the visibility of the comment modal. - const [ isCommentModalVisible, setIsCommentModalVisible ] = - useState( false ); - - // Get the selected block client ID. - const { selectedBlockClientId } = useSelect( ( select ) => { - return { - selectedBlockClientId: - select( blockEditorStore ).getSelectedBlockClientId(), - }; - }, [] ); - - // Function to toggle the comment modal visibility. - const toggleCommentModal = () => { - setIsCommentModalVisible( ( state ) => ! state ); - }; - - return ( - <> - { isBlockCommentExperimentEnabled && ( - - ) } - { isCommentModalVisible && ( - - ) } - - ); -} - -// Add the 'comment' attribute to all blocks. -if ( isBlockCommentExperimentEnabled ) { - addFilter( 'blocks.registerBlockType', 'core/comment', ( settings ) => { - settings.attributes = { - ...settings.attributes, - blockComments: { - type: 'array', - default: [], - }, - }; - - return settings; - } ); -} diff --git a/packages/format-library/src/comment/modal/comment-board.js b/packages/format-library/src/comment/modal/comment-board.js deleted file mode 100644 index 93f225faea69b8..00000000000000 --- a/packages/format-library/src/comment/modal/comment-board.js +++ /dev/null @@ -1,129 +0,0 @@ -/** - * WordPress dependencies - */ -import { __ } from '@wordpress/i18n'; -import { - Popover, - TextControl, - Button, - __experimentalHStack as HStack, - __experimentalVStack as VStack, -} from '@wordpress/components'; -import { useAnchor } from '@wordpress/rich-text'; -import { commentAuthorAvatar as userIcon, Icon } from '@wordpress/icons'; -import { useSelect, useDispatch } from '@wordpress/data'; -import { useState } from '@wordpress/element'; -import { store as blockEditorStore } from '@wordpress/block-editor'; -// eslint-disable-next-line import/no-extraneous-dependencies -import { dateI18n, format, getSettings } from '@wordpress/date'; - -const CommentBoard = ( { onClose, contentRef, clientId } ) => { - // Get the anchor for the popover. - const popoverAnchor = useAnchor( { - editableContentElement: contentRef.current, - } ); - - // State to manage the comment input. - const [ inputComment, setInputComment ] = useState( '' ); - - // Fetch current user. - const currentUser = useSelect( ( select ) => { - // eslint-disable-next-line @wordpress/data-no-store-string-literals - const { getCurrentUser } = select( 'core' ); - return getCurrentUser()?.name; - } ); - - // Fetch blockComments. - const blockComments = useSelect( ( select ) => { - const { getBlockAttributes } = select( blockEditorStore ); - return getBlockAttributes( clientId )?.blockComments || []; - } ); - - const { updateBlockAttributes } = useDispatch( blockEditorStore ); - - // Function to save the comment. - const saveComment = () => { - updateBlockAttributes( clientId, { - blockComments: [ - ...blockComments, - { - userName: currentUser, - comment: inputComment, - date: Date.now(), - }, - ], - } ); - - setInputComment( '' ); - }; - - // Get the date time format from WordPress settings. - const dateTimeFormat = getSettings().formats.datetime; - - return ( - - - { blockComments.length && - blockComments.map( ( { userName, comment, timestamp } ) => ( - - - - - { userName } - - - -

{ comment }

-
- ) ) } - - { blockComments.length === 0 && ( - - - { currentUser } - - ) } - setInputComment( val ) } - placeholder={ __( 'Comment or add others with @' ) } - /> - - - - - )} +
+ { showConfirmation && ( + hideConfirmationOverlay() } + className="confirmation-overlay" + > +

+ { __( + 'Are you sure you want to mark this thread as resolved?' + ) } +

+ + +
+ ) } ); }; -export default DiscussionsBoard; +export default DiscussionBoard; diff --git a/packages/block-library/src/collab/format.js b/packages/block-library/src/collab/format.js index 1124f24fc4d132..d2127575e0ee67 100644 --- a/packages/block-library/src/collab/format.js +++ b/packages/block-library/src/collab/format.js @@ -27,7 +27,8 @@ export const format = { function Edit( { isActive, contentRef } ) { // State to manage the visibility of the discussion board. - const [ isDiscussionBoardVisible, setIsDiscussionBoardVisible ] = useState( false ); + const [ isDiscussionBoardVisible, setIsDiscussionBoardVisible ] = + useState( false ); // Function to toggle the visibility of the discussion board. const toggleDiscussionBoardVisibility = () => { @@ -36,7 +37,7 @@ function Edit( { isActive, contentRef } ) { return ( <> - { isBlockCommentExperimentEnabled && + { isBlockCommentExperimentEnabled && ( - } - { isDiscussionBoardVisible && - - } + ) } ); } diff --git a/packages/block-library/src/collab/index.js b/packages/block-library/src/collab/index.js index 71fedc547a41a7..bc6fc052438584 100644 --- a/packages/block-library/src/collab/index.js +++ b/packages/block-library/src/collab/index.js @@ -22,4 +22,4 @@ export const settings = { edit: () => {}, }; -export const init = () => initBlock({ name, metadata, settings }); +export const init = () => initBlock( { name, metadata, settings } ); diff --git a/packages/block-library/src/collab/index.php b/packages/block-library/src/collab/index.php index 3e9ba5534fcc34..64aa489a35283c 100644 --- a/packages/block-library/src/collab/index.php +++ b/packages/block-library/src/collab/index.php @@ -4,8 +4,8 @@ * * @since // TODO: Add version number. */ -function register_block_collab_post_meta() { - $post_types = get_post_types( [ 'show_in_rest' => true ] ); +function register_block_core_collab_post_meta() { + $post_types = get_post_types( array( 'show_in_rest' => true ) ); foreach ( $post_types as $post_type ) { // Only register the meta field if the post type supports the editor, custom fields, and revisions. @@ -17,12 +17,12 @@ function register_block_collab_post_meta() { register_post_meta( $post_type, 'collab', - [ + array( 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'revisions_enabled' => true, - ] + ) ); } } @@ -32,4 +32,4 @@ function register_block_collab_post_meta() { * Most post types are registered at priority 10, so use priority 20 here in * order to catch them. */ -add_action( 'init', 'register_block_collab_post_meta', 20 ); +add_action( 'init', 'register_block_core_collab_post_meta', 20 ); From d82ecc275f3490e250f98b20b54631b67b5ce72a Mon Sep 17 00:00:00 2001 From: akashdhawade1991 <68046117+akashdhawade1991@users.noreply.github.com> Date: Mon, 3 Jun 2024 12:25:10 -0400 Subject: [PATCH 010/159] Address pipeline issues --- lib/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/blocks.php b/lib/blocks.php index 3f346c1fdbdc15..eabb49c2dd39cd 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -50,7 +50,7 @@ function gutenberg_reregister_core_block_types() { 'block.php' => 'core/block', 'calendar.php' => 'core/calendar', 'categories.php' => 'core/categories', - 'collab.php' => 'core/collab', + 'collab.php' => 'core/collab', 'cover.php' => 'core/cover', 'comment-author-avatar.php' => 'core/comment-author-avatar', 'comment-author-name.php' => 'core/comment-author-name', From 871c745f1f12f94098179c6de63b3d49d5bccb30 Mon Sep 17 00:00:00 2001 From: Juhi Bhatt Date: Thu, 6 Jun 2024 17:38:16 +0530 Subject: [PATCH 011/159] Added UI Design changes for Comment Popover section --- .../src/collab/discussion-board.js | 95 +++++++++++-------- packages/block-library/src/collab/editor.scss | 50 +++++++++- 2 files changed, 106 insertions(+), 39 deletions(-) diff --git a/packages/block-library/src/collab/discussion-board.js b/packages/block-library/src/collab/discussion-board.js index 1fef8b9b7fa340..ddc620fda0939a 100644 --- a/packages/block-library/src/collab/discussion-board.js +++ b/packages/block-library/src/collab/discussion-board.js @@ -229,20 +229,10 @@ const DiscussionBoard = ( { contentRef, onClose } ) => { anchor={ popoverAnchor } > - { 0 < currentThread.length && ( -
- showConfirmationOverlay() } - /> -
- ) } + { 0 < currentThread.length && currentThread.map( - ( { userName, comment, timestamp, commentId } ) => ( + ( { userName, comment, timestamp, commentId }, index ) => ( { spacing="1" justify="space-between" > - - - { userName } - - - + +
+ +
+ + +`; + +exports[`AlignmentUI should match snapshot when controls are hidden 1`] = ` +
+
+ +
+
+`; + +exports[`AlignmentUI should match snapshot when controls are visible 1`] = ` +
+
+
+ +
+
+ +
+
+ +
+
+
+`; diff --git a/packages/block-editor/src/components/collab/test/index.js b/packages/block-editor/src/components/collab/test/index.js new file mode 100644 index 00000000000000..178ba294127c31 --- /dev/null +++ b/packages/block-editor/src/components/collab/test/index.js @@ -0,0 +1,183 @@ +/** + * External dependencies + */ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +/** + * WordPress dependencies + */ +import { alignLeft, alignCenter } from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import AlignmentUI from '../ui'; + +describe( 'AlignmentUI', () => { + const alignment = 'left'; + const onChangeSpy = jest.fn(); + + afterEach( () => { + onChangeSpy.mockClear(); + } ); + + test( 'should match snapshot when controls are hidden', () => { + const { container } = render( + + ); + + expect( container ).toMatchSnapshot(); + } ); + + test( 'should match snapshot when controls are visible', () => { + const { container } = render( + + ); + + expect( container ).toMatchSnapshot(); + } ); + + test( 'should expand controls when toggled', async () => { + const user = userEvent.setup(); + + const { unmount } = render( + + ); + + expect( + screen.queryByRole( 'menuitemradio', { + name: /^Align text \w+$/, + } ) + ).not.toBeInTheDocument(); + + await user.click( + screen.getByRole( 'button', { + name: 'Align text', + } ) + ); + + expect( + screen.getAllByRole( 'menuitemradio', { + name: /^Align text \w+$/, + } ) + ).toHaveLength( 3 ); + + // Cancel running effects, like delayed dropdown menu popover positioning. + unmount(); + } ); + + test( 'should call on change with undefined when a control is already active', async () => { + const user = userEvent.setup(); + + render( + + ); + + const activeControl = screen.getByRole( 'button', { + name: /^Align text \w+$/, + pressed: true, + } ); + + await user.click( activeControl ); + + expect( activeControl ).toHaveAttribute( 'align', alignment ); + expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); + expect( onChangeSpy ).toHaveBeenCalledWith( undefined ); + } ); + + test( 'should call on change a new value when the control is not active', async () => { + const user = userEvent.setup(); + + render( + + ); + + const inactiveControl = screen.getByRole( 'button', { + name: 'Align text center', + pressed: false, + } ); + + await user.click( inactiveControl ); + + expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); + expect( onChangeSpy ).toHaveBeenCalledWith( 'center' ); + } ); + + test( 'should allow custom alignment controls to be specified', async () => { + const user = userEvent.setup(); + + const { container } = render( + + ); + + expect( container ).toMatchSnapshot(); + + expect( + screen.getAllByRole( 'button', { + name: /^My custom \w+$/, + } ) + ).toHaveLength( 2 ); + + // Should correctly call on change when right alignment is pressed (active alignment) + const rightControl = screen.getByRole( 'button', { + name: 'My custom right', + } ); + + await user.click( rightControl ); + + expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); + expect( onChangeSpy ).toHaveBeenCalledWith( undefined ); + onChangeSpy.mockClear(); + + // Should correctly call on change when right alignment is pressed (inactive alignment) + const leftControl = screen.getByRole( 'button', { + name: 'My custom left', + } ); + + await user.click( leftControl ); + + expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); + expect( onChangeSpy ).toHaveBeenCalledWith( 'custom-left' ); + } ); +} ); diff --git a/packages/block-library/src/common.scss b/packages/block-library/src/common.scss index 1e2b88f77a3980..c6327692ff02a3 100644 --- a/packages/block-library/src/common.scss +++ b/packages/block-library/src/common.scss @@ -180,7 +180,3 @@ html :where(.is-position-sticky) { /* stylelint-enable length-zero-no-unit */ } } - -span.has-collab-comment { - background-color: #ffea00; -} diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index f894fd7d6b804c..2ea5b7f1dc3315 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -1,18 +1,17 @@ /** * WordPress dependencies */ +// eslint-disable-next-line no-restricted-imports +import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; -import { - comment as commentIcon, - commentAuthorAvatar as userIcon, - Icon, - check as resolvedIcon, -} from '@wordpress/icons'; +import { comment as commentIcon } from '@wordpress/icons'; import { useSelect } from '@wordpress/data'; +import { useState, useEffect, RawHTML } from '@wordpress/element'; import { __experimentalHStack as HStack, __experimentalVStack as VStack, __experimentalText as Text, + Button, } from '@wordpress/components'; import { dateI18n, format, getSettings } from '@wordpress/date'; @@ -20,7 +19,6 @@ import { dateI18n, format, getSettings } from '@wordpress/date'; * Internal dependencies */ import PluginSidebar from '../plugin-sidebar'; -import { store as editorStore } from '../../store'; const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; @@ -29,31 +27,75 @@ const isBlockCommentExperimentEnabled = * Renders the Collab sidebar. */ export default function CollabSidebar() { - const { threads } = useSelect( ( select ) => { - const meta = select( editorStore ).getEditedPostAttribute( 'meta' ); + const postId = useSelect( ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core/editor' ).getCurrentPostId(); + }, [] ); + + const [ threads, setThreads ] = useState( [] ); + const [ showConfirmation, setShowConfirmation ] = useState( false ); + const [ showConfirmationTabId, setShowConfirmationTabId ] = useState( 0 ); + const [ commentConfirmation, setCommentConfirmation ] = useState( false ); + + useEffect( () => { + if ( postId ) { + apiFetch( { + path: + '/wp/v2/comments?post=' + + postId + + '&type=block_comment' + + '&status=any', + method: 'GET', + } ).then( ( response ) => { + setThreads( Array.isArray( response ) ? response : [] ); + } ); + } + }, [ postId ] ); + + const { threads: selectedThreads } = useSelect( () => { return { - threads: meta?.collab ? JSON.parse( meta.collab ) : false, + threads, }; - }, [] ); + }, [ threads ] ); // Check if the experimental flag is enabled. if ( ! isBlockCommentExperimentEnabled ) { return null; // or maybe return some message indicating no threads are available. } + const confirmAndMarkThreadAsResolved = ( threadID ) => { + setCommentConfirmation( false ); + if ( threadID ) { + apiFetch( { + path: '/wp/v2/comments/' + threadID, + method: 'POST', + data: { + status: 'approved', + }, + } ).then( ( response ) => { + if ( 'approved' === response.status ) { + setShowConfirmation( false ); + setCommentConfirmation( true ); + } + } ); + } + }; + // Get the date time format from WordPress settings. const dateTimeFormat = getSettings().formats.datetime; + const resultThreads = selectedThreads.map( ( thread ) => thread ).reverse(); return (
{ // If there are no threads, show a message indicating no threads are available. - ! threads && ( + ( ! Array.isArray( resultThreads ) || + resultThreads.length === 0 ) && ( ) } - { Object.keys( threads ).length > 0 && - Object.values( threads ) - .reverse() - .map( ( thread, index ) => ( - 0 && + resultThreads.reverse().map( ( thread ) => ( + + - - - - - { thread.createdBy } - - - - - { thread.comments.map( ( comment ) => ( + { + + + { thread.author_name } + + + + + + + + { thread.content.rendered } + + + + + { thread.status !== 'approved' && ( + + ) } + { thread.status === 'approved' && ( + // eslint-disable-next-line no-restricted-syntax + + ) } + + + + { commentConfirmation && + thread.id === showConfirmationTabId && ( + + { __( 'Thread marked as resolved.' ) } + + ) } + + { showConfirmation && + thread.id === showConfirmationTabId && ( + setShowConfirmation( false ) + } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="3" > - - - - - { comment.createdBy } - - - - - - { comment.comment } - - - ) ) } - { thread.isResolved && ( - - - - - - { thread.resolvedBy } - - - - - - - - { __( 'Marked as resolved' ) } - +

+ { __( + 'Are you sure you want to mark this thread as resolved?' + ) } +

+ + +
) } -
- ) ) } +
+ ) ) }
); diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index a2ee4fdebfba81..b4ee482cc6c1ad 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -1,12 +1,20 @@ .editor-collab-sidebar { &__activities { - background-color: #f3f5f6; + padding: 16px; + background-color: #f9f9f9; } &__thread { - padding: 20px; - &:not(:last-child) { - border-bottom: 1px solid $gray-300; + position: relative; + padding: 16px; + border-radius: 8px; + border: 1px solid $gray-300; + background-color: $gray-100; + margin-bottom: 16px; + + &__focus { + border: 1.5px solid #3858e9; + background-color: $white; } } @@ -15,16 +23,72 @@ border: 1px solid $gray-300; border-radius: 5px; padding: 10px 15px; - background-color: #fff; + background-color: $white; } &__userName { - font-size: 14px; + font-size: 12px; + font-weight: 400; + line-height: 16px; + text-align: left; + color: $gray-700; text-transform: capitalize; } + &__usertime { + font-size: 12px; + font-weight: 400; + line-height: 16px; + text-align: left; + color: $gray-700; + } + + &__usercomment { + font-size: 13px; + font-weight: 400; + line-height: 20px; + text-align: left; + color: $gray-900; + p { + margin-bottom: 0; + } + } + &__userIcon { - border: 1px solid #c5c5c5; + border-radius: 50%; + } + + &__userstatus { + button { + font-size: 12px; + font-weight: 400; + line-height: 16px; + padding: 0; + height: auto; + box-shadow: none; + outline: none; + } + } + + + &__useroverlay { + background-color: rgba(0, 0, 0, 0.7); + width: 100%; + height: 100%; + text-align: center; + position: absolute; + top: 0; + left: 0; + z-index: 1; + padding: 20px; + border-radius: 8px; + color: $white; + + button { + padding: 8px; + height: 30px; + color: $white; + } } &__resolvedIcon { diff --git a/packages/format-library/src/collab-comment/collab-board/style.scss b/packages/format-library/src/collab-comment/collab-board/style.scss deleted file mode 100644 index 7fa9a024b4f6b2..00000000000000 --- a/packages/format-library/src/collab-comment/collab-board/style.scss +++ /dev/null @@ -1,71 +0,0 @@ -.block-editor-format-toolbar__comment-board { - max-width: 380px; - min-width: auto; - width: 90vw; - - .components-popover__content { - width: auto; - padding: 1rem; - border: 1px solid rgba(0, 0, 0, 0.125); - box-shadow: 2px 2px 8px 2px #e9ecef; - border-radius: 4px; - } - - .comment-board__comment { - border-bottom: 1px solid $gray-300; - } - - &__resolved { - align-self: end; - } - - .comment-board__actions { - width: auto; - } - - .comment-board__userName { - font-size: 16px; - text-transform: capitalize; - } - - .comment-board__userIcon { - border: 1px solid #c5c5c5; - } - - .comment-board__commentText { - font-size: 14px; - margin-top: 5px; - } - .comment-board__dateTime { - font-size: 12px; - } - .block-editor-format-toolbar__comment-input { - .components-text-control__input { - border: 1px solid #ccc; - height: 40px; - &::placeholder { - font-style: italic; - } - &:focus { - border: 0; - } - } - } - .block-editor-format-toolbar__comment-board__resolved { - min-width: auto; - * { - min-width: auto; - } - span.components-checkbox-control__input-container { - display: none; - & + label { - text-decoration: underline; - cursor: pointer; - color: rgb(0, 124, 186); - } - } - } - .block-editor-format-toolbar__comment-board__edit { - margin-right: -8px; - } -} diff --git a/packages/format-library/src/collab-comment/index.js b/packages/format-library/src/collab-comment/index.js deleted file mode 100644 index 621a328edec0bd..00000000000000 --- a/packages/format-library/src/collab-comment/index.js +++ /dev/null @@ -1,117 +0,0 @@ -/** - * WordPress dependencies - */ -import { __ } from '@wordpress/i18n'; -import { useState, useEffect, useLayoutEffect } from '@wordpress/element'; -import { toggleFormat, removeFormat } from '@wordpress/rich-text'; -import { RichTextToolbarButton } from '@wordpress/block-editor'; -import { comment as commentIcon } from '@wordpress/icons'; - -/** - * Internal dependencies - */ -import CollabBoard from './collab-board'; - -const name = 'core/collab-comment'; -const title = __( 'Add Comment' ); - -const isBlockCommentExperimentEnabled = - window?.__experimentalEnableBlockComment; - -function Edit( { isActive, value, onChange, onFocus, contentRef } ) { - // State to manage the visibility of the discussion board. - const [ isDiscussionBoardVisible, setIsDiscussionBoardVisible ] = - useState( false ); - const [ threadId, setThreadId ] = useState(null); - - function onClick() { - const instanceId = Date.now().toString(); - - onChange( - toggleFormat( value, { - type: name, - attributes: { - id: instanceId, - }, - title, - } ) - ); - - setThreadId( instanceId ); - onFocus(); - setIsDiscussionBoardVisible( true ); - } - - // Function to toggle the visibility of the discussion board. - const toggleDiscussionBoardVisibility = () => { - onChange( removeFormat( value, name ) ); - setIsDiscussionBoardVisible( ( state ) => ! state ); - }; - - // close comment board if the comment is not active. - useEffect( () => { - if ( ! isActive ) { - setIsDiscussionBoardVisible( false ); - } - }, [ isActive ] ); - - useLayoutEffect( () => { - const editableContentElement = contentRef.current; - if ( ! editableContentElement ) { - return; - } - - function handleClick( event ) { - const comment = event.target.closest( '[contenteditable] span' ); - if ( - ! comment || - ! isActive - ) { - return; - } - - setIsDiscussionBoardVisible( true ); - const threadID = comment.getAttribute('id'); - setThreadId( threadID ); - } - - editableContentElement.addEventListener( 'click', handleClick ); - - return () => { - editableContentElement.removeEventListener( 'click', handleClick ); - }; - }, [ contentRef, isActive ] ); - - return ( - <> - { isBlockCommentExperimentEnabled && ( - - ) } - - { isBlockCommentExperimentEnabled && isDiscussionBoardVisible && ( - - ) } - - ); -} - -export const collabComment = { - name, - title, - tagName: 'span', - className: 'has-collab-comment', - attributes: { - id: 'id', - }, - edit: Edit, -}; diff --git a/packages/icons/src/index.js b/packages/icons/src/index.js index c5d5976dd2d5f7..6256059365b09b 100644 --- a/packages/icons/src/index.js +++ b/packages/icons/src/index.js @@ -49,6 +49,7 @@ export { default as cloudUpload } from './library/cloud-upload'; export { default as cloud } from './library/cloud'; export { default as code } from './library/code'; export { default as cog } from './library/cog'; +export { default as collabComment } from './library/collab-comment'; export { default as color } from './library/color'; export { default as column } from './library/column'; export { default as columns } from './library/columns'; diff --git a/packages/icons/src/library/collab-comment.js b/packages/icons/src/library/collab-comment.js new file mode 100644 index 00000000000000..dbd62e57a14b6a --- /dev/null +++ b/packages/icons/src/library/collab-comment.js @@ -0,0 +1,12 @@ +/** + * WordPress dependencies + */ +import { SVG, Path } from '@wordpress/primitives'; + +const collabComment = ( + + + +); + +export default collabComment; From f115c3beedaf24f31f063fbd08b5ec2d20d7b9ac Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 22 Aug 2024 19:25:23 +0530 Subject: [PATCH 043/159] made changes for code_sniffer error on rest_api file --- lib/compat/wordpress-6.6/rest-api.php | 22 +++++------ lib/experimental/editor-settings.php | 53 ++++++++++++++------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index e4d56bf31c67eb..4b7aebff8f3bc3 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -166,28 +166,28 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) } } - if( isset( $request['comment_type'] ) && !empty( $request['comment_type'] ) ){ + if ( isset( $request['comment_type'] ) && ! empty( $request['comment_type'] ) ) { $comment_data = array( - 'comment_ID' => $comment->comment_ID, - 'comment_type' => $request['comment_type'], + 'comment_ID' => $comment->comment_ID, + 'comment_type' => $request['comment_type'], ); - - wp_update_comment($comment_data); + + wp_update_comment( $comment_data ); } - $author = get_user_by('login', $response->data['author_name']); + $author = get_user_by( 'login', $response->data['author_name'] ); if ($author) { - $avatar_url = get_avatar_url($author->ID); + $avatar_url = get_avatar_url( $author->ID ); $response->data['author_avatar_urls'] = array( - 'default' => $avatar_url, - '48' => add_query_arg( 's', 48, $avatar_url ), - '96' => add_query_arg( 's', 96, $avatar_url ), + 'default' => $avatar_url, + '48' => add_query_arg( 's', 48, $avatar_url ), + '96' => add_query_arg( 's', 96, $avatar_url ), ); } $comment_id = $comment->comment_ID; $meta_key = 'block_comment'; - $meta_value = get_comment_meta($comment_id, $meta_key, true); + $meta_value = get_comment_meta( $comment_id, $meta_key, true ); if( !empty( $meta_value ) ){ $response->data['meta'] = $meta_value; diff --git a/lib/experimental/editor-settings.php b/lib/experimental/editor-settings.php index 25d7722a256dc9..39f57912860bf5 100644 --- a/lib/experimental/editor-settings.php +++ b/lib/experimental/editor-settings.php @@ -10,32 +10,33 @@ */ function gutenberg_enable_experiments() { $gutenberg_experiments = get_option( 'gutenberg-experiments' ); -if ( $gutenberg_experiments && array_key_exists( 'gutenberg-sync-collaboration', $gutenberg_experiments ) ) { - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableSync = true', 'before' ); -} -if ( $gutenberg_experiments && array_key_exists( 'gutenberg-custom-dataviews', $gutenberg_experiments ) ) { - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalCustomViews = true', 'before' ); -} -if ( $gutenberg_experiments && array_key_exists( 'gutenberg-color-randomizer', $gutenberg_experiments ) ) { - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableColorRandomizer = true', 'before' ); -} -if ( $gutenberg_experiments && array_key_exists( 'gutenberg-grid-interactivity', $gutenberg_experiments ) ) { - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableGridInteractivity = true', 'before' ); -} -if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) { - wp_add_inline_script( 'wp-block-library', 'window.__experimentalDisableTinymce = true', 'before' ); -} -if ( gutenberg_is_experiment_enabled( 'gutenberg-full-page-client-side-navigation' ) ) { - wp_add_inline_script( 'wp-block-library', 'window.__experimentalFullPageClientSideNavigation = true', 'before' ); -} -if ( $gutenberg_experiments && array_key_exists( 'gutenberg-zoomed-out-patterns-tab', $gutenberg_experiments ) ) { - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableZoomedOutPatternsTab = true', 'before' ); -} -if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-comment', $gutenberg_experiments ) ) { - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockComment = true', 'before' ); -} -if ( $gutenberg_experiments && array_key_exists( 'gutenberg-quick-edit-dataviews', $gutenberg_experiments ) ) { - wp_add_inline_script( 'wp-block-editor', 'window.__experimentalQuickEditDataViews = true', 'before' ); + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-sync-collaboration', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableSync = true', 'before' ); + } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-custom-dataviews', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalCustomViews = true', 'before' ); + } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-color-randomizer', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableColorRandomizer = true', 'before' ); + } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-grid-interactivity', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableGridInteractivity = true', 'before' ); + } + if ( gutenberg_is_experiment_enabled( 'gutenberg-no-tinymce' ) ) { + wp_add_inline_script( 'wp-block-library', 'window.__experimentalDisableTinymce = true', 'before' ); + } + if ( gutenberg_is_experiment_enabled( 'gutenberg-full-page-client-side-navigation' ) ) { + wp_add_inline_script( 'wp-block-library', 'window.__experimentalFullPageClientSideNavigation = true', 'before' ); + } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-zoomed-out-patterns-tab', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableZoomedOutPatternsTab = true', 'before' ); + } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-comment', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockComment = true', 'before' ); + } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-quick-edit-dataviews', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalQuickEditDataViews = true', 'before' ); + } } add_action( 'admin_init', 'gutenberg_enable_experiments' ); From f748ad935762ceb5679682b37c2e3c2817cd5f5e Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 22 Aug 2024 19:29:19 +0530 Subject: [PATCH 044/159] made changes for code_sniffer error on rest_api file --- lib/compat/wordpress-6.6/rest-api.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index 4b7aebff8f3bc3..a01a3f0678ebbd 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -168,8 +168,8 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) if ( isset( $request['comment_type'] ) && ! empty( $request['comment_type'] ) ) { $comment_data = array( - 'comment_ID' => $comment->comment_ID, - 'comment_type' => $request['comment_type'], + 'comment_ID' => $comment->comment_ID, + 'comment_type' => $request['comment_type'], ); wp_update_comment( $comment_data ); @@ -179,9 +179,9 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) if ($author) { $avatar_url = get_avatar_url( $author->ID ); $response->data['author_avatar_urls'] = array( - 'default' => $avatar_url, - '48' => add_query_arg( 's', 48, $avatar_url ), - '96' => add_query_arg( 's', 96, $avatar_url ), + 'default' => $avatar_url, + '48' => add_query_arg( 's', 48, $avatar_url ), + '96' => add_query_arg( 's', 96, $avatar_url ), ); } @@ -189,7 +189,7 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) $meta_key = 'block_comment'; $meta_value = get_comment_meta( $comment_id, $meta_key, true ); - if( !empty( $meta_value ) ){ + if ( !empty( $meta_value ) ) { $response->data['meta'] = $meta_value; } From 59e96c440c9707f77679dd6562eb591df86073fa Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 22 Aug 2024 19:33:27 +0530 Subject: [PATCH 045/159] remove readme.md file from collab folder --- .../src/components/collab/README.md | 50 ------------------- 1 file changed, 50 deletions(-) delete mode 100644 packages/block-editor/src/components/collab/README.md diff --git a/packages/block-editor/src/components/collab/README.md b/packages/block-editor/src/components/collab/README.md deleted file mode 100644 index c51da803c77aef..00000000000000 --- a/packages/block-editor/src/components/collab/README.md +++ /dev/null @@ -1,50 +0,0 @@ -## Alignment Control - -The `AlignmentControl` component renders a dropdown menu that displays alignment options for the selected block. - -This component is mostly used for blocks that display text, such as Heading, Paragraph, Post Author, Post Comments, Verse, Quote, Post Title, etc... And the available alignment options are `left`, `center` or `right` alignment. - -![Post Title block alignment options](https://make.wordpress.org/core/files/2020/09/post-title-block-alignment-options.png) - -## Development guidelines - -### Usage - -Renders an alignment toolbar with alignments options. - -```jsx -import { AlignmentControl } from '@wordpress/block-editor'; - -const MyAlignmentToolbar = () => ( - - { - setAttributes( { textAlign: nextAlign } ); - } } - /> - -); -``` - -_Note:_ In this example that we render `AlignmentControl` as a child of the `BlockControls` component. - -### Props - -### `value` - -- **Type:** `String` -- **Default:** `undefined` -- **Options:** `left`, `center`, `right` - -The current value of the alignment setting. You may only choose from the `Options` listed above. - -### `onChange` - -- **Type:** `Function` - -A callback function invoked when the toolbar's alignment value is changed via an interaction with any of the toolbar's buttons. Called with the new alignment value (ie: `left`, `center`, `right`, `undefined`) as the only argument. - -## Related components - -Block Editor components are components that can be used to compose the UI of your block editor. Thus, they can only be used under a [`BlockEditorProvider`](https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/provider/README.md) in the components tree. From 18e5081eac8cbcf57f48127189693f06da55699a Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 22 Aug 2024 19:56:49 +0530 Subject: [PATCH 046/159] remove static email for comment author --- lib/compat/wordpress-6.6/rest-api.php | 1 + .../block-editor/src/components/collab/collab-board/index.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index a01a3f0678ebbd..0d47eb6208de45 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -170,6 +170,7 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) $comment_data = array( 'comment_ID' => $comment->comment_ID, 'comment_type' => $request['comment_type'], + 'comment_approved' => isset( $request['comment_approved'] ) ? $request['comment_approved'] : 0, ); wp_update_comment( $comment_data ); diff --git a/packages/block-editor/src/components/collab/collab-board/index.js b/packages/block-editor/src/components/collab/collab-board/index.js index 6f94cc3494c369..e0c7e5492ff2b5 100644 --- a/packages/block-editor/src/components/collab/collab-board/index.js +++ b/packages/block-editor/src/components/collab/collab-board/index.js @@ -96,11 +96,11 @@ export default function BlockCommentModal( { clientId, onClose, threadId } ) { data: { post: postID, content: newComment.comment, - author_name: currentUser, comment_date: newComment.createdAt, comment_type: 'block_comment', meta: updatedComments, - author_email: 'rishi.shah@multidots.com', + comment_author: currentUser, + comment_approved: 0, }, } ).then( () => { onClose(); From 1ed6d5486cf84a86903e842f433400890e118c40 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Fri, 23 Aug 2024 19:32:19 +0530 Subject: [PATCH 047/159] added edit/delete comment from sidebar feature --- .../block-settings-dropdown.js | 22 +- .../src/components/block-toolbar/index.js | 16 + .../components/collab/collab-board/index.js | 11 +- .../src/components/collab/toolbar.js | 20 + .../src/components/collab-sidebar/index.js | 380 ++++++++++++++++-- .../src/components/collab-sidebar/style.scss | 152 ++++++- .../format-library/src/default-formats.js | 2 - packages/format-library/src/style.scss | 1 - 8 files changed, 540 insertions(+), 64 deletions(-) create mode 100644 packages/block-editor/src/components/collab/toolbar.js diff --git a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js index 2e16c43a7fa418..108bed4cfcffb0 100644 --- a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js +++ b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js @@ -68,6 +68,7 @@ export function BlockSettingsDropdown( { selectedBlockClientIds, openedBlockSettingsMenu, isContentOnly, + blockClassName, } = useSelect( ( select ) => { const { @@ -88,6 +89,11 @@ export function BlockSettingsDropdown( { const parentBlockName = _firstParentClientId && getBlockName( _firstParentClientId ); + const className = + // eslint-disable-next-line @wordpress/data-no-store-string-literals + select( 'core/block-editor' ).getBlock( firstBlockClientId ) + ?.attributes?.className; + return { firstParentClientId: _firstParentClientId, onlyBlock: 1 === getBlockCount( _firstParentClientId ), @@ -104,6 +110,7 @@ export function BlockSettingsDropdown( { openedBlockSettingsMenu: getOpenedBlockSettingsMenu(), isContentOnly: getBlockEditingMode( firstBlockClientId ) === 'contentOnly', + blockClassName: className, }; }, [ firstBlockClientId ] @@ -221,13 +228,14 @@ export function BlockSettingsDropdown( { > { ( { onClose } ) => ( <> - { isBlockCommentExperimentEnabled && ( - - - - ) } + { isBlockCommentExperimentEnabled && + ! blockClassName && ( + + + + ) } <__unstableBlockSettingsMenuFirstItem.Slot fillProps={ { onClose } } diff --git a/packages/block-editor/src/components/block-toolbar/index.js b/packages/block-editor/src/components/block-toolbar/index.js index 6c6acd662ed962..731767f1528211 100644 --- a/packages/block-editor/src/components/block-toolbar/index.js +++ b/packages/block-editor/src/components/block-toolbar/index.js @@ -28,6 +28,7 @@ import BlockControls from '../block-controls'; import __unstableBlockToolbarLastItem from './block-toolbar-last-item'; import BlockSettingsMenu from '../block-settings-menu'; import { BlockLockToolbar } from '../block-lock'; +import BlockCommentToolbar from '../collab/toolbar'; import { BlockGroupToolbar } from '../convert-to-group-buttons'; import BlockEditVisuallyButton from '../block-edit-visually-button'; import { useShowHoveredOrFocusedGestures } from './utils'; @@ -65,6 +66,7 @@ export function PrivateBlockToolbar( { shouldShowVisualToolbar, showParentSelector, isUsingBindings, + blockClassName, } = useSelect( ( select ) => { const { getBlockName, @@ -96,6 +98,12 @@ export function PrivateBlockToolbar( { ( clientId ) => !! getBlockAttributes( clientId )?.metadata?.bindings ); + + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const className = select( 'core/block-editor' ).getBlock( + selectedBlockClientId + )?.attributes?.className; + return { blockClientId: selectedBlockClientId, blockClientIds: selectedBlockClientIds, @@ -115,6 +123,7 @@ export function PrivateBlockToolbar( { selectedBlockClientIds.length === 1 && _isDefaultEditingMode, isUsingBindings: _isUsingBindings, + blockClassName: className, }; }, [] ); @@ -192,6 +201,13 @@ export function PrivateBlockToolbar( { /> ) } + + { blockClassName && ( + + ) } ) } diff --git a/packages/block-editor/src/components/collab/collab-board/index.js b/packages/block-editor/src/components/collab/collab-board/index.js index e0c7e5492ff2b5..2057456691a380 100644 --- a/packages/block-editor/src/components/collab/collab-board/index.js +++ b/packages/block-editor/src/components/collab/collab-board/index.js @@ -102,10 +102,11 @@ export default function BlockCommentModal( { clientId, onClose, threadId } ) { comment_author: currentUser, comment_approved: 0, }, - } ).then( () => { + } ).then( ( response ) => { + threadId = response?.id; + setAttributes( clientId, threadId ); onClose(); } ); - setAttributes( clientId, threadId ); }; // Function to edit the comment. @@ -202,10 +203,10 @@ export default function BlockCommentModal( { clientId, onClose, threadId } ) { ) => ( <> { isEditing === commentId && ( - + ) } { ! isEditing && ( - + { 0 === commentsCount && ( + + + + + ); +} diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 2ea5b7f1dc3315..1e7844cf1f7cc8 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -4,7 +4,6 @@ // eslint-disable-next-line no-restricted-imports import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; -import { comment as commentIcon } from '@wordpress/icons'; import { useSelect } from '@wordpress/data'; import { useState, useEffect, RawHTML } from '@wordpress/element'; import { @@ -12,8 +11,18 @@ import { __experimentalVStack as VStack, __experimentalText as Text, Button, + DropdownMenu, + TextareaControl, + Tooltip, } from '@wordpress/components'; -import { dateI18n, format, getSettings } from '@wordpress/date'; +import { dateI18n, format } from '@wordpress/date'; +import { + comment as commentIcon, + Icon, + check, + published, + moreVertical, +} from '@wordpress/icons'; /** * Internal dependencies @@ -36,6 +45,14 @@ export default function CollabSidebar() { const [ showConfirmation, setShowConfirmation ] = useState( false ); const [ showConfirmationTabId, setShowConfirmationTabId ] = useState( 0 ); const [ commentConfirmation, setCommentConfirmation ] = useState( false ); + const [ deleteCommentShowConfirmation, setDeleteCommentShowConfirmation ] = + useState( false ); + const [ commentDeleteMessage, setCommentDeleteMessage ] = useState( false ); + const [ commentEdit, setCommentEdit ] = useState( false ); + const [ newEditedComment, setNewEditedComment ] = useState( '' ); + const [ commentEditedMessage, setCommentEditedMessage ] = useState( false ); + const [ hasCommentReply, setHasCommentReply ] = useState( false ); + const [ commentReply, setCommentReply ] = useState( '' ); useEffect( () => { if ( postId ) { @@ -44,10 +61,18 @@ export default function CollabSidebar() { '/wp/v2/comments?post=' + postId + '&type=block_comment' + - '&status=any', + '&status=any&per_page=100', method: 'GET', } ).then( ( response ) => { - setThreads( Array.isArray( response ) ? response : [] ); + const filteredComments = response.filter( + ( comment ) => comment.status !== 'trash' + ); + // const hierarchicalComments = buildCommentHierarchy(filteredComments); + // console.log(filteredComments); + // console.log(hierarchicalComments); + setThreads( + Array.isArray( filteredComments ) ? filteredComments : [] + ); } ); } }, [ postId ] ); @@ -81,8 +106,60 @@ export default function CollabSidebar() { } }; + const onEditComment = ( threadID ) => { + if ( threadID ) { + setHasCommentReply( false ); + setCommentEdit( true ); + } + }; + + const confirmEditComment = ( threadID ) => { + if ( threadID ) { + const editedComment = newEditedComment.replace( + /^

|<\/p>$/g, + '' + ); + + apiFetch( { + path: '/wp/v2/comments/' + threadID, + method: 'POST', + data: { + content: editedComment, + }, + } ).then( ( response ) => { + if ( 'trash' !== response.status && '' !== response.id ) { + setCommentEdit( false ); + setCommentEditedMessage( true ); + } + } ); + } + }; + + const confirmDeleteComment = ( threadID ) => { + setDeleteCommentShowConfirmation( false ); + if ( threadID ) { + apiFetch( { + path: '/wp/v2/comments/' + threadID, + method: 'DELETE', + } ).then( ( response ) => { + if ( 'trash' === response.status && '' !== response.id ) { + setCommentDeleteMessage( true ); + } + } ); + } + }; + + const onReplyComment = ( threadID ) => { + if ( threadID ) { + setCommentEdit( false ); + setHasCommentReply( true ); + } + }; + + const confirmReplyComment = () => {}; + // Get the date time format from WordPress settings. - const dateTimeFormat = getSettings().formats.datetime; + const dateTimeFormat = 'h:i A'; const resultThreads = selectedThreads.map( ( thread ) => thread ).reverse(); return ( @@ -137,7 +214,7 @@ export default function CollabSidebar() {

{ __( @@ -238,6 +498,48 @@ export default function CollabSidebar() { ) } + + { deleteCommentShowConfirmation && + thread.id === showConfirmationTabId && ( + + setDeleteCommentShowConfirmation( + false + ) + } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to delete this thread?' + ) } +

+ + + + +
+ ) }
) ) } diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index b4ee482cc6c1ad..4fbabbb22f99da 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -18,14 +18,23 @@ } } - &__comment, - &__resolved { - border: 1px solid $gray-300; - border-radius: 5px; - padding: 10px 15px; - background-color: $white; + + &__editarea { + flex: 1; + + .components-textarea-control__input { + border: 1px solid $gray-300; + border-radius: 8px; + padding: 10px 15px; + background-color: $gray-100; + } + } + + &__commentbtn { + margin-bottom: 10px; } + &__userName { font-size: 12px; font-weight: 400; @@ -49,6 +58,7 @@ line-height: 20px; text-align: left; color: $gray-900; + p { margin-bottom: 0; } @@ -56,6 +66,7 @@ &__userIcon { border-radius: 50%; + flex-shrink: 0; } &__userstatus { @@ -70,7 +81,6 @@ } } - &__useroverlay { background-color: rgba(0, 0, 0, 0.7); width: 100%; @@ -80,13 +90,17 @@ top: 0; left: 0; z-index: 1; - padding: 20px; + padding: 15px; border-radius: 8px; color: $white; + p { + margin-bottom: 15px; + } + button { - padding: 8px; - height: 30px; + height: 24px; + padding: 6px; color: $white; } } @@ -101,6 +115,35 @@ margin-top: 5px; } + &__commentUpdate { + margin-left: auto; + + .components-button { + &.is-compact { + &.has-icon:not(.has-text) { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + flex-shrink: 0; + } + } + } + + .components-dropdown { + &.is-compact { + flex-shrink: 0; + + .components-button { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + } + } + } + } + &__resolvedText { font-style: italic; font-weight: 500 !important; @@ -108,3 +151,92 @@ margin-top: 5px !important; } } + +.block-editor-format-toolbar__comment-board { + z-index: 1000001; + + .components-modal__frame { + max-width: 380px; + min-width: auto; + width: 90vw; + } + + .components-modal__content { + margin-top: 0; + padding: 32px 32px; + + .components-modal__header { + display: none; + } + } + + .comment-board__userIcon { + width: 35px; + height: 35px; + max-width: 100%; + border-radius: 50%; + } + + .comment-board__comment { + border-bottom: 1px solid $gray-300; + } + + &__resolved { + align-self: end; + } + + .comment-board__actions { + width: auto; + } + + .comment-board__userName { + font-size: 16px; + text-transform: capitalize; + } + + .comment-board__commentText { + font-size: 14px; + margin-top: 5px; + } + + .comment-board__dateTime { + font-size: 12px; + } + + .block-editor-format-toolbar__comment-input { + .components-text-control__input { + border: 1px solid #ccc; + height: 40px; + + &::placeholder { + font-style: italic; + } + + &:focus { + border-color: #2271b1; + } + } + } + + .block-editor-format-toolbar__comment-board__resolved { + min-width: auto; + + * { + min-width: auto; + } + + span.components-checkbox-control__input-container { + display: none; + + & + label { + text-decoration: underline; + cursor: pointer; + color: rgb(0, 124, 186); + } + } + } + + .block-editor-format-toolbar__comment-board__edit { + margin-right: -8px; + } +} diff --git a/packages/format-library/src/default-formats.js b/packages/format-library/src/default-formats.js index 022977d8488640..3ccfc92cb40c18 100644 --- a/packages/format-library/src/default-formats.js +++ b/packages/format-library/src/default-formats.js @@ -15,12 +15,10 @@ import { keyboard } from './keyboard'; import { unknown } from './unknown'; import { language } from './language'; import { nonBreakingSpace } from './non-breaking-space'; -import { collabComment } from './collab-comment'; export default [ bold, code, - collabComment, image, italic, link, diff --git a/packages/format-library/src/style.scss b/packages/format-library/src/style.scss index 5606ae34897d7f..e388258b4d5a0e 100644 --- a/packages/format-library/src/style.scss +++ b/packages/format-library/src/style.scss @@ -2,4 +2,3 @@ @import "./link/style.scss"; @import "./text-color/style.scss"; @import "./language/style.scss"; -@import "./collab-comment/collab-board/style.scss"; From 84733c58b7d7dadc9c22c7c2a764b7f15a4df84a Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Fri, 23 Aug 2024 19:41:23 +0530 Subject: [PATCH 048/159] resolve PHP coding standards errors --- lib/compat/wordpress-6.6/rest-api.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index 0d47eb6208de45..b7773d205b2fee 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -168,8 +168,8 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) if ( isset( $request['comment_type'] ) && ! empty( $request['comment_type'] ) ) { $comment_data = array( - 'comment_ID' => $comment->comment_ID, - 'comment_type' => $request['comment_type'], + 'comment_ID' => $comment->comment_ID, + 'comment_type' => $request['comment_type'], 'comment_approved' => isset( $request['comment_approved'] ) ? $request['comment_approved'] : 0, ); @@ -177,8 +177,8 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) } $author = get_user_by( 'login', $response->data['author_name'] ); - if ($author) { - $avatar_url = get_avatar_url( $author->ID ); + if ( $author ) { + $avatar_url = get_avatar_url( $author->ID ); $response->data['author_avatar_urls'] = array( 'default' => $avatar_url, '48' => add_query_arg( 's', 48, $avatar_url ), @@ -187,13 +187,13 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) } $comment_id = $comment->comment_ID; - $meta_key = 'block_comment'; + $meta_key = 'block_comment'; $meta_value = get_comment_meta( $comment_id, $meta_key, true ); - if ( !empty( $meta_value ) ) { + if ( ! empty( $meta_value ) ) { $response->data['meta'] = $meta_value; } - + return $response; } } From 2d5d1621b73253004bc37344bfd794a435212fbc Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Fri, 23 Aug 2024 19:46:12 +0530 Subject: [PATCH 049/159] resolve PHP coding standards errors --- lib/compat/wordpress-6.6/rest-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index b7773d205b2fee..f067deba6b0126 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -161,7 +161,7 @@ function gutenberg_register_wp_rest_themes_template_directory_uri_field() { if ( ! function_exists( 'update_comment_meta_from_rest_request' ) ) { function update_comment_meta_from_rest_request( $response, $comment, $request ) { if ( isset( $request['meta'] ) && is_array( $request['meta'] ) ) { - foreach ( $request['meta'] as $key => $value ) { + foreach ( $request['meta'] as $key => $value ) { //phpcs:ignore update_comment_meta( $comment->comment_ID, 'block_comment', $value ); } } From 8042e22f0677d904bd2147c1a95116e5a3381d4f Mon Sep 17 00:00:00 2001 From: poojabhimani12 <82029773+poojabhimani12@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:24:04 +0530 Subject: [PATCH 050/159] Resolved Conflicts --- lib/compat/wordpress-6.6/rest-api.php | 35 ++++++++++++++++++++++++++- lib/experimental/editor-settings.php | 3 +++ lib/experiments-page.php | 12 +++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index f067deba6b0126..38951c60fd8794 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -33,7 +33,7 @@ function wp_api_template_access_controller( $args, $post_type ) { /** * Adds the post classes to the REST API response. * - * @param array $post The response object data. + * @param array $post The response object data. * * @return array */ @@ -158,6 +158,39 @@ function gutenberg_register_wp_rest_themes_template_directory_uri_field() { } add_action( 'rest_api_init', 'gutenberg_register_wp_rest_themes_template_directory_uri_field' ); +/** + * Preload theme and global styles paths to avoid flash of variation styles in post editor. + * + * @param array $paths REST API paths to preload. + * @param WP_Block_Editor_Context $context Current block editor context. + * @return array Filtered preload paths. + */ +function gutenberg_block_editor_preload_paths_6_6( $paths, $context ) { + if ( 'core/edit-post' === $context->name ) { + $paths[] = '/wp/v2/global-styles/themes/' . get_stylesheet(); + $paths[] = '/wp/v2/themes?context=edit&status=active'; + $paths[] = '/wp/v2/global-styles/' . WP_Theme_JSON_Resolver_Gutenberg::get_user_global_styles_post_id() . '?context=edit'; + } + return $paths; +} +add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_6', 10, 2 ); + +/** + * Updates comment metadata from a REST API request. + * + * This function is hooked to the `rest_prepare_comment` filter and is responsible for updating the comment metadata based on the data provided in the REST API request. + * + * It performs the following tasks: + * - Updates the `block_comment` metadata for the comment based on the `meta` field in the request. + * - Updates the `comment_type` and `comment_approved` fields for the comment based on the corresponding fields in the request. + * - Retrieves the author's avatar URLs and adds them to the response data. + * - Retrieves the `block_comment` metadata for the comment and adds it to the response data. + * + * @param WP_REST_Response $response The response object. + * @param WP_Comment $comment The comment object. + * @param WP_REST_Request $request The request object. + * @return WP_REST_Response The updated response object. + */ if ( ! function_exists( 'update_comment_meta_from_rest_request' ) ) { function update_comment_meta_from_rest_request( $response, $comment, $request ) { if ( isset( $request['meta'] ) && is_array( $request['meta'] ) ) { diff --git a/lib/experimental/editor-settings.php b/lib/experimental/editor-settings.php index 39f57912860bf5..9600c0b85de697 100644 --- a/lib/experimental/editor-settings.php +++ b/lib/experimental/editor-settings.php @@ -37,6 +37,9 @@ function gutenberg_enable_experiments() { if ( $gutenberg_experiments && array_key_exists( 'gutenberg-quick-edit-dataviews', $gutenberg_experiments ) ) { wp_add_inline_script( 'wp-block-editor', 'window.__experimentalQuickEditDataViews = true', 'before' ); } + if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-bindings-ui', $gutenberg_experiments ) ) { + wp_add_inline_script( 'wp-block-editor', 'window.__experimentalBlockBindingsUI = true', 'before' ); + } } add_action( 'admin_init', 'gutenberg_enable_experiments' ); diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 06a26416e2e3f5..503cb5ac9aa41e 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -175,6 +175,18 @@ function gutenberg_initialize_experiments_settings() { ) ); + add_settings_field( + 'gutenberg-block-bindings-ui', + __( 'UI to create block bindings', 'gutenberg' ), + 'gutenberg_display_experiment_field', + 'gutenberg-experiments', + 'gutenberg_experiments_section', + array( + 'label' => __( 'Add UI to create and update block bindings in block inspector controls.', 'gutenberg' ), + 'id' => 'gutenberg-block-bindings-ui', + ) + ); + register_setting( 'gutenberg-experiments', 'gutenberg-experiments' From ceb188ce5706e364e566a68ee1340a5fb707cb96 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Wed, 28 Aug 2024 12:12:24 +0530 Subject: [PATCH 051/159] Add idenrifier to collab sidebar --- packages/editor/src/components/collab-sidebar/constants.js | 1 + packages/editor/src/components/collab-sidebar/index.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 packages/editor/src/components/collab-sidebar/constants.js diff --git a/packages/editor/src/components/collab-sidebar/constants.js b/packages/editor/src/components/collab-sidebar/constants.js new file mode 100644 index 00000000000000..748c2afc26374d --- /dev/null +++ b/packages/editor/src/components/collab-sidebar/constants.js @@ -0,0 +1 @@ +export const collabSidebarName = 'edit-post/collab-sidebar'; diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 1e7844cf1f7cc8..64ea0023227e64 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -28,6 +28,7 @@ import { * Internal dependencies */ import PluginSidebar from '../plugin-sidebar'; +import { collabSidebarName } from './constants'; const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; @@ -164,7 +165,7 @@ export default function CollabSidebar() { return ( From 575361cce0fb0eff3a62c8bc62c984f9497cc0fa Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 11:37:56 +0530 Subject: [PATCH 052/159] Relocate Add Comment button below in more action popover --- .../block-settings-dropdown.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js index 108bed4cfcffb0..b4efc87a269cd8 100644 --- a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js +++ b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js @@ -228,14 +228,6 @@ export function BlockSettingsDropdown( { > { ( { onClose } ) => ( <> - { isBlockCommentExperimentEnabled && - ! blockClassName && ( - - - - ) } <__unstableBlockSettingsMenuFirstItem.Slot fillProps={ { onClose } } @@ -297,6 +289,13 @@ export function BlockSettingsDropdown( { ) } + { isBlockCommentExperimentEnabled && + ! blockClassName && ( + + ) } { canCopyStyles && ! isContentOnly && ( From 8c459825894198df1e938a291e30579f4ae2c8e7 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 11:38:49 +0530 Subject: [PATCH 053/159] Update comment button action to focus new comment form in sidebar on click --- .../collab/block-comment-menu-item.js | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/packages/block-editor/src/components/collab/block-comment-menu-item.js b/packages/block-editor/src/components/collab/block-comment-menu-item.js index c6ea9dcfdec39f..8ff965675db6a7 100644 --- a/packages/block-editor/src/components/collab/block-comment-menu-item.js +++ b/packages/block-editor/src/components/collab/block-comment-menu-item.js @@ -2,37 +2,26 @@ * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { useReducer } from '@wordpress/element'; import { MenuItem } from '@wordpress/components'; import { collabComment } from '@wordpress/icons'; +import { useDispatch } from '@wordpress/data'; -/** - * Internal dependencies - */ -import BlockCommentModal from './collab-board'; +export default function BlockCommentMenuItem( { clientId, onClose } ) { -export default function BlockCommentMenuItem( { clientId } ) { - const [ isModalOpen, toggleModal ] = useReducer( - ( isActive ) => ! isActive, - false - ); + const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); + + const openCollabBoard = () => { + onClose(); + openGeneralSidebar("edit-post/collab-sidebar"); + } return ( - <> - - { __( 'Comment' ) } - - { isModalOpen && ( - - ) } - + + { __( 'Add Comment' ) } + ); } From 2004c041027d5a4ebf6f9a0f448f3ee3fc4d198f Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 11:58:53 +0530 Subject: [PATCH 054/159] Update experimental field lable for commenting functionality --- lib/experiments-page.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 503cb5ac9aa41e..2be55174d5e543 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -165,12 +165,12 @@ function gutenberg_initialize_experiments_settings() { add_settings_field( 'gutenberg-block-comment', - __( 'Enable block Comment', 'gutenberg' ), + __( 'Comments', 'gutenberg' ), 'gutenberg_display_experiment_field', 'gutenberg-experiments', 'gutenberg_experiments_section', array( - 'label' => __( 'Enable Block Comment', 'gutenberg' ), + 'label' => __( 'Enable multi-user commenting on blocks', 'gutenberg' ), 'id' => 'gutenberg-block-comment', ) ); From 8ac8328ab79817c9ee2e487635ad3df51874a011 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 12:03:07 +0530 Subject: [PATCH 055/159] Revert format library changes --- packages/format-library/package.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/format-library/package.json b/packages/format-library/package.json index 7ea25a917549d6..531ec71224ce76 100644 --- a/packages/format-library/package.json +++ b/packages/format-library/package.json @@ -1,6 +1,6 @@ { "name": "@wordpress/format-library", - "version": "5.1.0", + "version": "5.6.0", "description": "Format library for the WordPress editor.", "author": "The WordPress Contributors", "license": "GPL-2.0-or-later", @@ -31,10 +31,7 @@ "@wordpress/block-editor": "file:../block-editor", "@wordpress/components": "file:../components", "@wordpress/compose": "file:../compose", - "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", - "@wordpress/date": "file:../date", - "@wordpress/editor": "file:../editor", "@wordpress/element": "file:../element", "@wordpress/html-entities": "file:../html-entities", "@wordpress/i18n": "file:../i18n", From 868c0955b4afc88c75a4d34fc50f8b376a2399a7 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 29 Aug 2024 12:15:50 +0530 Subject: [PATCH 056/159] made changes as per Github --- lib/compat/wordpress-6.6/rest-api.php | 5 - lib/experiments-page.php | 4 +- .../components/collab/collab-board/index.js | 4 +- .../src/components/collab-sidebar/index.js | 1312 +++++++++++++---- .../src/components/collab-sidebar/style.scss | 319 ++-- 5 files changed, 1187 insertions(+), 457 deletions(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index f067deba6b0126..c86af1dfcc2ec4 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -160,11 +160,6 @@ function gutenberg_register_wp_rest_themes_template_directory_uri_field() { if ( ! function_exists( 'update_comment_meta_from_rest_request' ) ) { function update_comment_meta_from_rest_request( $response, $comment, $request ) { - if ( isset( $request['meta'] ) && is_array( $request['meta'] ) ) { - foreach ( $request['meta'] as $key => $value ) { //phpcs:ignore - update_comment_meta( $comment->comment_ID, 'block_comment', $value ); - } - } if ( isset( $request['comment_type'] ) && ! empty( $request['comment_type'] ) ) { $comment_data = array( diff --git a/lib/experiments-page.php b/lib/experiments-page.php index 06a26416e2e3f5..099a752ddb633b 100644 --- a/lib/experiments-page.php +++ b/lib/experiments-page.php @@ -165,12 +165,12 @@ function gutenberg_initialize_experiments_settings() { add_settings_field( 'gutenberg-block-comment', - __( 'Enable block Comment', 'gutenberg' ), + __( 'Enable multi-user commenting on blocks', 'gutenberg' ), 'gutenberg_display_experiment_field', 'gutenberg-experiments', 'gutenberg_experiments_section', array( - 'label' => __( 'Enable Block Comment', 'gutenberg' ), + 'label' => __( 'Comments', 'gutenberg' ), 'id' => 'gutenberg-block-comment', ) ); diff --git a/packages/block-editor/src/components/collab/collab-board/index.js b/packages/block-editor/src/components/collab/collab-board/index.js index 2057456691a380..065c1f1002e9c1 100644 --- a/packages/block-editor/src/components/collab/collab-board/index.js +++ b/packages/block-editor/src/components/collab/collab-board/index.js @@ -98,15 +98,15 @@ export default function BlockCommentModal( { clientId, onClose, threadId } ) { content: newComment.comment, comment_date: newComment.createdAt, comment_type: 'block_comment', - meta: updatedComments, comment_author: currentUser, comment_approved: 0, }, - } ).then( ( response ) => { + } ).then( (response) => { threadId = response?.id; setAttributes( clientId, threadId ); onClose(); } ); + }; // Function to edit the comment. diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 1e7844cf1f7cc8..c2c0252e118461 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -4,7 +4,7 @@ // eslint-disable-next-line no-restricted-imports import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; -import { useSelect } from '@wordpress/data'; +import { useSelect, useDispatch } from '@wordpress/data'; import { useState, useEffect, RawHTML } from '@wordpress/element'; import { __experimentalHStack as HStack, @@ -14,74 +14,104 @@ import { DropdownMenu, TextareaControl, Tooltip, + TextControl, + CheckboxControl, } from '@wordpress/components'; -import { dateI18n, format } from '@wordpress/date'; +import { dateI18n, format, getSettings } from '@wordpress/date'; import { comment as commentIcon, Icon, check, published, - moreVertical, + moreVertical } from '@wordpress/icons'; /** * Internal dependencies */ import PluginSidebar from '../plugin-sidebar'; +//import BlockCommentModal from '../../../../block-editor/src/components/collab/collab-board'; -const isBlockCommentExperimentEnabled = - window?.__experimentalEnableBlockComment; +const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; /** * Renders the Collab sidebar. */ export default function CollabSidebar() { - const postId = useSelect( ( select ) => { - // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core/editor' ).getCurrentPostId(); - }, [] ); + + const postId = useSelect((select) => { + return select('core/editor').getCurrentPostId(); + }, []); - const [ threads, setThreads ] = useState( [] ); + const [threads, setThreads] = useState([]); const [ showConfirmation, setShowConfirmation ] = useState( false ); const [ showConfirmationTabId, setShowConfirmationTabId ] = useState( 0 ); const [ commentConfirmation, setCommentConfirmation ] = useState( false ); - const [ deleteCommentShowConfirmation, setDeleteCommentShowConfirmation ] = - useState( false ); + const [ deleteCommentShowConfirmation, setDeleteCommentShowConfirmation ] = useState( false ); const [ commentDeleteMessage, setCommentDeleteMessage ] = useState( false ); const [ commentEdit, setCommentEdit ] = useState( false ); const [ newEditedComment, setNewEditedComment ] = useState( '' ); const [ commentEditedMessage, setCommentEditedMessage ] = useState( false ); const [ hasCommentReply, setHasCommentReply ] = useState( false ); const [ commentReply, setCommentReply ] = useState( '' ); + const [ replyMessage, setReplyMessage ] = useState( false ); - useEffect( () => { - if ( postId ) { - apiFetch( { - path: - '/wp/v2/comments?post=' + - postId + - '&type=block_comment' + - '&status=any&per_page=100', + // useEffect(() => { + // if (postId) { + // apiFetch({ + // path: '/wp/v2/comments?post=' + postId + '&type=block_comment' + '&status=any&per_page=100', + // method: 'GET', + // }) + // .then((response) => { + // const filteredComments = response.filter( comment => comment.status !== 'trash' ); + // setThreads(Array.isArray(filteredComments) ? filteredComments : []); + // }); + // } + // }, [postId]); + + useEffect(() => { + if (postId) { + apiFetch({ + path: '/wp/v2/comments?post=' + postId + '&type=block_comment' + '&status=any&per_page=100', method: 'GET', - } ).then( ( response ) => { - const filteredComments = response.filter( - ( comment ) => comment.status !== 'trash' - ); - // const hierarchicalComments = buildCommentHierarchy(filteredComments); - // console.log(filteredComments); - // console.log(hierarchicalComments); - setThreads( - Array.isArray( filteredComments ) ? filteredComments : [] - ); - } ); + }) + .then((data) => { + + // Create a compare to store the references to all objects by id + const compare = {}; + const result = []; + + // Initialize each object with an empty `reply` array + data.forEach(item => { + compare[item.id] = { ...item, reply: [] }; + }); + + // Iterate over the data to build the tree structure + data.forEach(item => { + if (item.parent === 0) { + // If parent is 0, it's a root item, push it to the result array + result.push(compare[item.id]); + } else { + // Otherwise, find its parent and push it to the parent's `reply` array + if (compare[item.parent]) { + if( 'trash' !== item.status ) { + compare[item.parent].reply.push(compare[item.id]); + } + } + } + }); + const filteredComments = result.filter(comment => comment.status !== 'trash'); + setThreads(Array.isArray(filteredComments) ? filteredComments : []); + + }) } - }, [ postId ] ); + }, [postId]); - const { threads: selectedThreads } = useSelect( () => { + const { threads: selectedThreads } = useSelect(() => { return { - threads, + threads: threads, }; - }, [ threads ] ); + }, [threads]); // Check if the experimental flag is enabled. if ( ! isBlockCommentExperimentEnabled ) { @@ -90,19 +120,20 @@ export default function CollabSidebar() { const confirmAndMarkThreadAsResolved = ( threadID ) => { setCommentConfirmation( false ); - if ( threadID ) { - apiFetch( { + if (threadID) { + apiFetch({ path: '/wp/v2/comments/' + threadID, method: 'POST', data: { status: 'approved', - }, - } ).then( ( response ) => { + } + }) + .then((response) => { if ( 'approved' === response.status ) { setShowConfirmation( false ); setCommentConfirmation( true ); } - } ); + }) } }; @@ -114,38 +145,37 @@ export default function CollabSidebar() { }; const confirmEditComment = ( threadID ) => { - if ( threadID ) { - const editedComment = newEditedComment.replace( - /^

|<\/p>$/g, - '' - ); + if (threadID) { + const editedComment = newEditedComment.replace(/^

|<\/p>$/g, ''); - apiFetch( { + apiFetch({ path: '/wp/v2/comments/' + threadID, method: 'POST', data: { content: editedComment, - }, - } ).then( ( response ) => { + } + }) + .then((response) => { if ( 'trash' !== response.status && '' !== response.id ) { setCommentEdit( false ); setCommentEditedMessage( true ); } - } ); + }) } }; const confirmDeleteComment = ( threadID ) => { setDeleteCommentShowConfirmation( false ); - if ( threadID ) { - apiFetch( { + if (threadID) { + apiFetch({ path: '/wp/v2/comments/' + threadID, method: 'DELETE', - } ).then( ( response ) => { + }) + .then((response) => { if ( 'trash' === response.status && '' !== response.id ) { setCommentDeleteMessage( true ); } - } ); + }) } }; @@ -156,11 +186,210 @@ export default function CollabSidebar() { } }; - const confirmReplyComment = () => {}; + const confirmReplyComment = ( threadID ) => { + if (threadID) { + + const newComment = generateReplyComment( commentReply ); + const commentId = newComment?.commentId; + const updatedComments = getUpdatedComments( newComment, commentId ); + + apiFetch({ + path: '/wp/v2/comments/', + method: 'POST', + data: { + parent: threadID, + post: postID, + content: newComment.comment, + comment_date: newComment.createdAt, + comment_type: 'block_comment', + comment_author: currentUser, + comment_approved: 0, + } + }) + .then((response) => { + if ( 'trash' !== response.status && '' !== response.id ) { + setReplyMessage( true ); + } + }) + } + } + + // Helper function to get updated comments structure + const getUpdatedComments = ( newComment, threadKey ) => ( { + ...threads, + [ threadKey ]: { + isResolved: false, + createdAt: + threads?.threadKey?.createdAt || new Date().toISOString(), + createdBy: currentUser, + comments: { + 0: newComment, + }, + }, + } ); + + const generateReplyComment = (comment) => ( { + commentId: Date.now(), + createdBy: currentUser, + comment, + createdAt: new Date().toISOString(), + } ); + + const generateNewComment = () => ( { + commentId: Date.now(), + createdBy: currentUser, + comment: inputComment, + createdAt: new Date().toISOString(), + } ); // Get the date time format from WordPress settings. const dateTimeFormat = 'h:i A'; - const resultThreads = selectedThreads.map( ( thread ) => thread ).reverse(); + const resultThreads = selectedThreads.map(thread => thread).reverse(); + + // State to manage the comment thread. + const [ inputComment, setInputComment ] = useState( '' ); + const [ isResolved, setIsResolved ] = useState( false ); + const [ isEditing, setIsEditing ] = useState( null ); + const curruntUserData = useSelect( ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core' ).getCurrentUser(); + }, [] ); + + let userAvatar = 'https://secure.gravatar.com/avatar/92929292929292929292929292929292?s=48&d=mm&r=g'; + if( curruntUserData?.avatar_urls ) { + userAvatar = curruntUserData?.avatar_urls[ 48 ]; + } + + //const userAvatar = curruntUserData?.avatar_urls[ 48 ] ? curruntUserData?.avatar_urls[ 48 ] : null; + + const currentUser = curruntUserData?.name || null; + + const allThreads = []; + const threadId = 0; + const currentThread = allThreads[ threadId ] ?? {}; + const isCurrentThreadResolved = currentThread.threadIsResolved || false; + const commentsCount = isCurrentThreadResolved + ? 0 + : currentThread.comments?.length || 0; + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const postID = useSelect( ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core/editor' ).getCurrentPostId(); + }, [] ); + + const clientId = useSelect((select) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const { getSelectedBlockClientId } = select('core/block-editor'); + return getSelectedBlockClientId(); + }, []); + + const blockClassName = useSelect((select) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core/block-editor' ).getBlock( select('core/block-editor').getSelectedBlockClientId() )?.attributes?.className; + }, []); + + // Get the dispatch functions to save the comment and update the block attributes. + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); + + // // Function to add a border class to the content reference. + const setAttributes = () => { + updateBlockAttributes( clientId, { + className: `block-editor-collab__${ threadId }`, + } ); + }; + + // Function to save the comment. + const saveComment = () => { + const newComment = generateNewComment(); + apiFetch( { + path: '/wp/v2/comments', + method: 'POST', + data: { + post: postID, + content: newComment.comment, + comment_date: newComment.createdAt, + comment_type: 'block_comment', + comment_author: currentUser, + comment_approved: 0, + }, + } ).then( (response) => { + const threadId = response?.id; + setAttributes( clientId, threadId ); + setInputComment(''); + //onClose(); + } ); + }; + + // Function to edit the comment. + const editComment = ( commentId ) => { + const editedComments = { ...allThreads }; + + if ( + editedComments[ threadId ] && + editedComments[ threadId ].comments + ) { + editedComments[ threadId ].comments.map( ( comment ) => { + if ( comment.commentId === commentId ) { + comment.comment = inputComment; + comment.date = new Date().toISOString(); + } + return comment; + } ); + } + + setInputComment( '' ); + setIsEditing( null ); + }; + + // Function to mark thread as resolved + const markThreadAsResolved = () => { + setIsResolved( true ); + + const updatedComments = { ...allThreads }; + + updatedComments[ threadId ] = { + ...updatedComments[ threadId ], + isResolved: true, + resolvedBy: currentUser, + resolvedAt: new Date().toISOString(), + }; + + onClose(); + }; + + // Function to delete a comment. + const deleteComment = ( commentId ) => { + // Filter out the comment to be deleted. + const currentComments = allThreads[ threadId ].comments.filter( + ( comment ) => comment.commentId !== commentId + ); + + const updatedComments = { ...allThreads }; + + // If there are no comments, delete the thread. + if ( currentComments.length === 0 ) { + delete updatedComments[ threadId ]; + } else { + updatedComments[ threadId ] = { + ...allThreads[ threadId ], + comments: currentComments, + }; + } + }; + + // Function to show the confirmation overlay. + const showConfirmationOverlay = () => setShowConfirmation( true ); + + // Function to hide the confirmation overlay. + const hideConfirmationOverlay = () => setShowConfirmation( false ); + + + // On cancel, remove the border if no comments are present. + const handleCancel = () => { + //onClose(); + }; + return (

+ + { null !== clientId && undefined === blockClassName && ( + + { 0 < commentsCount && ! isCurrentThreadResolved && ( + <> + { currentThread.comments.map( + ( + { + createdBy, + comment, + timestamp, + commentId, + }, + index + ) => ( + <> + { isEditing === commentId && ( + + + + + { currentUser } + + + + setInputComment( val ) + } + placeholder={ __( + 'Add comment' + ) } + className="block-editor-format-toolbar__comment-input" + /> + + + { 0 === thread.parent ? ( { - setShowConfirmationTabId( - thread.id - ); - onEditComment( - thread.id - ); - }, + setShowConfirmationTabId( thread.id ); + onEditComment( thread.id ); + } }, { title: __( 'Delete' ), onClick: () => { - setCommentEdit( - false - ); - setShowConfirmationTabId( - thread.id - ); - setDeleteCommentShowConfirmation( - true - ); - }, + setCommentEdit( false ); + setShowConfirmationTabId( thread.id ); + setDeleteCommentShowConfirmation( true ); + } }, { title: __( 'Reply' ), onClick: () => { - setShowConfirmationTabId( - thread.id - ); - onReplyComment( - thread.id - ); - }, + setShowConfirmationTabId( thread.id ); + onReplyComment( thread.id ); + } + }, + ] } + /> + ) : ( + { + setShowConfirmationTabId( thread.id ); + onEditComment( thread.id ); + } + }, + { + title: __( 'Delete' ), + onClick: () => { + setCommentEdit( false ); + setShowConfirmationTabId( thread.id ); + setDeleteCommentShowConfirmation( true ); + } }, ] } /> + ) } + - ) } - { thread.status === 'approved' && ( + } + { thread.status === 'approved' && - ) } + } - - { commentEdit && - thread.id === showConfirmationTabId && ( - <> - { - setNewEditedComment( - value - ); - } } - /> - - setCommentEdit( false ) - } - > - - - - - - - ) } - - { thread.content.rendered } - - - - - { hasCommentReply && - thread.id === showConfirmationTabId && ( - - + + { commentEdit && thread.id === showConfirmationTabId && ( + <> /g, '') : thread.content.rendered.replace(/<\/?p>/g, '') } onChange={ ( value ) => { - setCommentReply( value ); + setNewEditedComment( value ); } } /> - setHasCommentReply( false ) - } + className="editor-collab-sidebar__commentbtn" + onRequestClose={ () => setCommentEdit( false ) } > - - - ) } - - { commentConfirmation && - thread.id === showConfirmationTabId && ( - - { __( 'Thread marked as resolved.' ) } - - ) } - { commentEditedMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Thread edited successfully.' ) } - - ) } - { commentDeleteMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Thread deleted successfully.' ) } - - ) } + + ) } + + { thread.content.rendered } + + + - { showConfirmation && - thread.id === showConfirmationTabId && ( + { hasCommentReply && thread.id === showConfirmationTabId && ( + + + { + setCommentReply( value ); + } } + /> - setShowConfirmation( false ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" + alignment="left" + spacing="3" + justify="flex-start" + onRequestClose={ () => setHasCommentReply( false ) } > -

- { __( - 'Are you sure you want to mark this thread as resolved?' - ) } -

- +
- ) } +
+
+ ) } + + { commentConfirmation && thread.id === showConfirmationTabId && ( + + { __( 'Thread marked as resolved.' ) } + + ) } + { commentEditedMessage && thread.id === showConfirmationTabId && ( + + { __( 'Thread edited successfully.' ) } + + ) } + { commentDeleteMessage && thread.id === showConfirmationTabId && ( + + { __( 'Thread deleted successfully.' ) } + + ) } + { replyMessage && thread.id === showConfirmationTabId && ( + + { __( 'Reply added successfully.' ) } + + ) } + + { showConfirmation && thread.id === showConfirmationTabId && ( + setShowConfirmation( false ) } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to mark this thread as resolved?' + ) } +

+ + + + +
+ ) } - { deleteCommentShowConfirmation && - thread.id === showConfirmationTabId && ( + { deleteCommentShowConfirmation && thread.id === showConfirmationTabId && ( + setDeleteCommentShowConfirmation( false ) } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to delete this thread?' + ) } +

+ + + + +
+ ) } + + { 0 < thread.reply.length && ( + thread.reply.map((reply) => ( - setDeleteCommentShowConfirmation( - false - ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" + key={ reply.id } + className="editor-collab-sidebar__childThread" + id={reply.id} + spacing="2" > -

- { __( - 'Are you sure you want to delete this thread?' - ) } -

- - - + + +
+ + ) } + + { reply.content.rendered } + +
+ + + { hasCommentReply && reply.id === showConfirmationTabId && ( + + - { __( 'No' ) } - + { + setCommentReply( value ); + } } + /> + setHasCommentReply( false ) } + > + + + + + + + ) } + + { commentConfirmation && reply.id === showConfirmationTabId && ( + + { __( 'Thread marked as resolved.' ) } + + ) } + { commentEditedMessage && reply.id === showConfirmationTabId && ( + + { __( 'Thread edited successfully.' ) } + + ) } + { commentDeleteMessage && reply.id === showConfirmationTabId && ( + + { __( 'Thread deleted successfully.' ) } + + ) } + { replyMessage && reply.id === showConfirmationTabId && ( + + { __( 'Reply added successfully.' ) } + + ) } + + { showConfirmation && reply.id === showConfirmationTabId && ( + setShowConfirmation( false ) } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to mark this thread as resolved?' + ) } +

+ + + + +
+ ) } + + { deleteCommentShowConfirmation && reply.id === showConfirmationTabId && ( + setDeleteCommentShowConfirmation( false ) } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to delete this thread?' + ) } +

+ + + + +
+ ) } + - ) } + + )) + ) } + - ) ) } + )) + } +
); diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index 4fbabbb22f99da..bb4c8fde25893c 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -1,155 +1,174 @@ .editor-collab-sidebar { - &__activities { - padding: 16px; - background-color: #f9f9f9; - } - - &__thread { - position: relative; - padding: 16px; - border-radius: 8px; - border: 1px solid $gray-300; - background-color: $gray-100; - margin-bottom: 16px; - - &__focus { - border: 1.5px solid #3858e9; - background-color: $white; - } - } - - - &__editarea { - flex: 1; - - .components-textarea-control__input { - border: 1px solid $gray-300; - border-radius: 8px; - padding: 10px 15px; - background-color: $gray-100; - } - } - - &__commentbtn { - margin-bottom: 10px; - } - - - &__userName { - font-size: 12px; - font-weight: 400; - line-height: 16px; - text-align: left; - color: $gray-700; - text-transform: capitalize; - } - - &__usertime { - font-size: 12px; - font-weight: 400; - line-height: 16px; - text-align: left; - color: $gray-700; - } - - &__usercomment { - font-size: 13px; - font-weight: 400; - line-height: 20px; - text-align: left; - color: $gray-900; - - p { - margin-bottom: 0; - } - } - - &__userIcon { - border-radius: 50%; - flex-shrink: 0; - } - - &__userstatus { - button { - font-size: 12px; - font-weight: 400; - line-height: 16px; - padding: 0; - height: auto; - box-shadow: none; - outline: none; - } - } - - &__useroverlay { - background-color: rgba(0, 0, 0, 0.7); - width: 100%; - height: 100%; - text-align: center; - position: absolute; - top: 0; - left: 0; - z-index: 1; - padding: 15px; - border-radius: 8px; - color: $white; - - p { - margin-bottom: 15px; - } - - button { - height: 24px; - padding: 6px; - color: $white; - } - } - - &__resolvedIcon { - fill: #008000; - border-radius: 50%; - width: 18px; - height: 18px; - border: 1px solid #008000; - margin-right: 2px; - margin-top: 5px; - } - - &__commentUpdate { - margin-left: auto; - - .components-button { - &.is-compact { - &.has-icon:not(.has-text) { - min-width: 24px; - padding: 0; - width: 24px; - height: 24px; - flex-shrink: 0; - } - } - } - - .components-dropdown { - &.is-compact { - flex-shrink: 0; - - .components-button { - min-width: 24px; - padding: 0; - width: 24px; - height: 24px; - } - } - } - } + &__activities { + padding: 16px; + background-color: #f9f9f9; + } + + &__thread { + position: relative; + padding: 16px; + border-radius: 8px; + border: 1px solid $gray-300; + background-color: $gray-100; + margin-bottom: 16px; + + .components-base-control__field { + margin-bottom: 0 + } + + input[type=text] { + @include input-control; + border-radius: 6px; + padding: 8px 15px; + } + } + + &__activethread { + border: 1.5px solid #3858E9; + background-color: $white; + box-shadow: 0px 5.5px 7.8px -0.3px rgba(0, 0, 0, 0.102); + } + + + &__editarea, + &__replyComment { + flex: 1; + + &__textarea{ + width: 100%; + } + + .components-textarea-control__input { + border: 1px solid $gray-300; + border-radius: 8px; + padding: 10px 15px; + background-color: $gray-100; + } + } + + &__childThread { + margin-top: 15px; + } + + &__userName { + font-size: 12px; + font-weight: 400; + line-height: 16px; + text-align: left; + color: $gray-700; + text-transform: capitalize; + } + + &__usertime { + font-size: 12px; + font-weight: 400; + line-height: 16px; + text-align: left; + color: $gray-700; + } + + &__usercomment { + font-size: 13px; + font-weight: 400; + line-height: 20px; + text-align: left; + color: $gray-900; + + p { + margin-bottom: 0; + } + } + + &__userIcon { + border-radius: 50%; + flex-shrink: 0; + } + + &__userstatus { + button { + font-size: 12px; + font-weight: 400; + line-height: 16px; + padding: 0; + height: auto; + box-shadow: none; + outline: none; + } + } + + &__useroverlay { + background-color: rgba(0, 0, 0, 0.7); + width: 100%; + height: 100%; + text-align: center; + position: absolute; + top: 0; + left: 0; + z-index: 1; + padding: 15px; + border-radius: 8px; + color: $white; + + p { + margin-bottom: 15px; + } + + button { + height: 24px; + padding: 6px; + color: $white; + } + } + + &__resolvedIcon { + fill: #008000; + border-radius: 50%; + width: 18px; + height: 18px; + border: 1px solid #008000; + margin-right: 2px; + margin-top: 5px; + } + + &__commentUpdate { + margin-left: auto; + + .components-button { + &.is-compact { + &.has-icon:not(.has-text) { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + flex-shrink: 0; + } + } + } + + .components-dropdown { + &.is-compact { + flex-shrink: 0; + + .components-button { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + } + } + } + } + + &__resolvedText { + font-style: italic; + font-weight: 500 !important; + color: #808080 !important; + margin-top: 5px !important; + } +} - &__resolvedText { - font-style: italic; - font-weight: 500 !important; - color: #808080 !important; - margin-top: 5px !important; - } +.is-collab-block-selected { + background-color: color-mix( in srgb, currentColor 5%, #182F97 20% ); } .block-editor-format-toolbar__comment-board { From ab73559ebf9b4518db6cff8a392bee95bc71aceb Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 14:26:32 +0530 Subject: [PATCH 057/159] Refactor collab sidebar --- .../components/collab-sidebar/add-comment.js | 430 ++++++++++++++ .../src/components/collab-sidebar/comments.js | 540 ++++++++++++++++++ .../src/components/collab-sidebar/index.js | 481 +--------------- .../src/components/collab-sidebar/style.scss | 319 ++++++----- 4 files changed, 1146 insertions(+), 624 deletions(-) create mode 100644 packages/editor/src/components/collab-sidebar/add-comment.js create mode 100644 packages/editor/src/components/collab-sidebar/comments.js diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js new file mode 100644 index 00000000000000..38e027e2367be4 --- /dev/null +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -0,0 +1,430 @@ +/** + * WordPress dependencies + */ +// eslint-disable-next-line no-restricted-imports +import apiFetch from '@wordpress/api-fetch'; +import { __ } from '@wordpress/i18n'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { useState } from '@wordpress/element'; +import { + __experimentalHStack as HStack, + __experimentalVStack as VStack, + Button, + TextControl, + CheckboxControl, +} from '@wordpress/components'; +import { dateI18n, format } from '@wordpress/date'; +import { + Icon +} from '@wordpress/icons'; + +/** + * Renders the new comment form. + */ +export function AddComment({ threads, setReloadComments }) { + + const generateNewComment = () => ( { + commentId: Date.now(), + createdBy: currentUser, + comment: inputComment, + createdAt: new Date().toISOString(), + } ); + + // Get the date time format from WordPress settings. + const dateTimeFormat = 'h:i A'; + + // State to manage the comment thread. + const [ inputComment, setInputComment ] = useState( '' ); + const [ isResolved, setIsResolved ] = useState( false ); + const [ isEditing, setIsEditing ] = useState( null ); + const curruntUserData = useSelect( ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core' ).getCurrentUser(); + }, [] ); + + let userAvatar = 'https://secure.gravatar.com/avatar/92929292929292929292929292929292?s=48&d=mm&r=g'; + if( curruntUserData?.avatar_urls ) { + userAvatar = curruntUserData?.avatar_urls[ 48 ]; + } + + //const userAvatar = curruntUserData?.avatar_urls[ 48 ] ? curruntUserData?.avatar_urls[ 48 ] : null; + + const currentUser = curruntUserData?.name || null; + + const allThreads = []; + const threadId = 0; + const currentThread = allThreads[ threadId ] ?? {}; + const isCurrentThreadResolved = currentThread.threadIsResolved || false; + const commentsCount = isCurrentThreadResolved + ? 0 + : currentThread.comments?.length || 0; + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const postID = useSelect( ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core/editor' ).getCurrentPostId(); + }, [] ); + + const clientId = useSelect((select) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const { getSelectedBlockClientId } = select('core/block-editor'); + return getSelectedBlockClientId(); + }, []); + + const blockClassName = useSelect((select) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core/block-editor' ).getBlock( select('core/block-editor').getSelectedBlockClientId() )?.attributes?.className; + }, []); + + // Get the dispatch functions to save the comment and update the block attributes. + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); + + // // Function to add a border class to the content reference. + const setAttributes = () => { + updateBlockAttributes( clientId, { + className: `block-editor-collab__${ threadId }`, + } ); + }; + + // Helper function to get updated comments structure + const getUpdatedComments = ( newComment, threadKey ) => ( { + ...threads, + [ threadKey ]: { + isResolved: false, + createdAt: + threads?.threadKey?.createdAt || new Date().toISOString(), + createdBy: currentUser, + comments: { + 0: newComment, + }, + }, + } ); + + + // Function to save the comment. + const saveComment = () => { + + const newComment = generateNewComment(); + const threadId = newComment?.commentId; + // const updatedComments = getUpdatedComments( newComment, threadId ); + + apiFetch( { + path: '/wp/v2/comments', + method: 'POST', + data: { + post: postID, + content: newComment.comment, + comment_date: newComment.createdAt, + comment_type: 'block_comment', + comment_author: currentUser, + comment_approved: 0, + }, + } ).then( (response) => { + const threadId = response?.id; + setAttributes( clientId, threadId ); + setInputComment(''); + setReloadComments( true ); + } ); + + }; + + // Function to edit the comment. + const editComment = ( commentId ) => { + const editedComments = { ...allThreads }; + + if ( + editedComments[ threadId ] && + editedComments[ threadId ].comments + ) { + editedComments[ threadId ].comments.map( ( comment ) => { + if ( comment.commentId === commentId ) { + comment.comment = inputComment; + comment.date = new Date().toISOString(); + } + return comment; + } ); + } + + setInputComment( '' ); + setIsEditing( null ); + }; + + // Function to delete a comment. + const deleteComment = ( commentId ) => { + // Filter out the comment to be deleted. + const currentComments = allThreads[ threadId ].comments.filter( + ( comment ) => comment.commentId !== commentId + ); + + const updatedComments = { ...allThreads }; + + // If there are no comments, delete the thread. + if ( currentComments.length === 0 ) { + delete updatedComments[ threadId ]; + } else { + updatedComments[ threadId ] = { + ...allThreads[ threadId ], + comments: currentComments, + }; + } + }; + + // Function to show the confirmation overlay. + const showConfirmationOverlay = () => setShowConfirmation( true ); + + // Function to hide the confirmation overlay. + const hideConfirmationOverlay = () => setShowConfirmation( false ); + + + // On cancel, remove the border if no comments are present. + const handleCancel = () => { + //onClose(); + }; + + + return ( + <> + { null !== clientId && undefined === blockClassName && ( + + { 0 < commentsCount && ! isCurrentThreadResolved && ( + <> + { currentThread.comments.map( + ( + { + createdBy, + comment, + timestamp, + commentId, + }, + index + ) => ( + <> + { isEditing === commentId && ( + + + + + { currentUser } + + + + setInputComment( val ) + } + placeholder={ __( + 'Add comment' + ) } + className="block-editor-format-toolbar__comment-input" + /> + + + + { + setShowConfirmationTabId( + thread.id + ); + onEditComment( + thread.id + ); + }, + }, + { + title: __( 'Delete' ), + onClick: () => { + setCommentEdit( + false + ); + setShowConfirmationTabId( + thread.id + ); + setDeleteCommentShowConfirmation( + true + ); + }, + }, + { + title: __( 'Reply' ), + onClick: () => { + setShowConfirmationTabId( + thread.id + ); + onReplyComment( + thread.id + ); + }, + }, + ] } + /> + + ) } + { thread.status === 'approved' && ( + + + + ) } + +
+ + + { commentEdit && + thread.id === showConfirmationTabId && ( + <> + { + setNewEditedComment( + value + ); + } } + /> + + setCommentEdit( false ) + } + > + + + + + + + ) } + + { thread.content.rendered } + + + + + { hasCommentReply && + thread.id === showConfirmationTabId && ( + + + { + setCommentReply( value ); + } } + /> + + setHasCommentReply( false ) + } + > + + + + + + + + ) } + + { commentConfirmation && + thread.id === showConfirmationTabId && ( + + { __( 'Thread marked as resolved.' ) } + + ) } + { commentEditedMessage && + thread.id === showConfirmationTabId && ( + + { __( 'Thread edited successfully.' ) } + + ) } + { commentDeleteMessage && + thread.id === showConfirmationTabId && ( + + { __( 'Thread deleted successfully.' ) } + + ) } + + { showConfirmation && + thread.id === showConfirmationTabId && ( + + setShowConfirmation( false ) + } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to mark this thread as resolved?' + ) } +

+ + + + +
+ ) } + + { deleteCommentShowConfirmation && + thread.id === showConfirmationTabId && ( + + setDeleteCommentShowConfirmation( + false + ) + } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to delete this thread?' + ) } +

+ + + + +
+ ) } +
+ ) + ) + } + + ); +} \ No newline at end of file diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 64ea0023227e64..773c134d59c784 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -6,22 +6,8 @@ import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; import { useState, useEffect, RawHTML } from '@wordpress/element'; -import { - __experimentalHStack as HStack, - __experimentalVStack as VStack, - __experimentalText as Text, - Button, - DropdownMenu, - TextareaControl, - Tooltip, -} from '@wordpress/components'; -import { dateI18n, format } from '@wordpress/date'; import { comment as commentIcon, - Icon, - check, - published, - moreVertical, } from '@wordpress/icons'; /** @@ -29,6 +15,8 @@ import { */ import PluginSidebar from '../plugin-sidebar'; import { collabSidebarName } from './constants'; +import { Comments } from './comments'; +import { AddComment } from './add-comment'; const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; @@ -43,17 +31,7 @@ export default function CollabSidebar() { }, [] ); const [ threads, setThreads ] = useState( [] ); - const [ showConfirmation, setShowConfirmation ] = useState( false ); - const [ showConfirmationTabId, setShowConfirmationTabId ] = useState( 0 ); - const [ commentConfirmation, setCommentConfirmation ] = useState( false ); - const [ deleteCommentShowConfirmation, setDeleteCommentShowConfirmation ] = - useState( false ); - const [ commentDeleteMessage, setCommentDeleteMessage ] = useState( false ); - const [ commentEdit, setCommentEdit ] = useState( false ); - const [ newEditedComment, setNewEditedComment ] = useState( '' ); - const [ commentEditedMessage, setCommentEditedMessage ] = useState( false ); - const [ hasCommentReply, setHasCommentReply ] = useState( false ); - const [ commentReply, setCommentReply ] = useState( '' ); + const [ reloadComments, setReloadComments ] = useState( false ); useEffect( () => { if ( postId ) { @@ -68,15 +46,13 @@ export default function CollabSidebar() { const filteredComments = response.filter( ( comment ) => comment.status !== 'trash' ); - // const hierarchicalComments = buildCommentHierarchy(filteredComments); - // console.log(filteredComments); - // console.log(hierarchicalComments); setThreads( Array.isArray( filteredComments ) ? filteredComments : [] ); + setReloadComments( false ); } ); } - }, [ postId ] ); + }, [ postId, reloadComments ] ); const { threads: selectedThreads } = useSelect( () => { return { @@ -89,78 +65,6 @@ export default function CollabSidebar() { return null; // or maybe return some message indicating no threads are available. } - const confirmAndMarkThreadAsResolved = ( threadID ) => { - setCommentConfirmation( false ); - if ( threadID ) { - apiFetch( { - path: '/wp/v2/comments/' + threadID, - method: 'POST', - data: { - status: 'approved', - }, - } ).then( ( response ) => { - if ( 'approved' === response.status ) { - setShowConfirmation( false ); - setCommentConfirmation( true ); - } - } ); - } - }; - - const onEditComment = ( threadID ) => { - if ( threadID ) { - setHasCommentReply( false ); - setCommentEdit( true ); - } - }; - - const confirmEditComment = ( threadID ) => { - if ( threadID ) { - const editedComment = newEditedComment.replace( - /^

|<\/p>$/g, - '' - ); - - apiFetch( { - path: '/wp/v2/comments/' + threadID, - method: 'POST', - data: { - content: editedComment, - }, - } ).then( ( response ) => { - if ( 'trash' !== response.status && '' !== response.id ) { - setCommentEdit( false ); - setCommentEditedMessage( true ); - } - } ); - } - }; - - const confirmDeleteComment = ( threadID ) => { - setDeleteCommentShowConfirmation( false ); - if ( threadID ) { - apiFetch( { - path: '/wp/v2/comments/' + threadID, - method: 'DELETE', - } ).then( ( response ) => { - if ( 'trash' === response.status && '' !== response.id ) { - setCommentDeleteMessage( true ); - } - } ); - } - }; - - const onReplyComment = ( threadID ) => { - if ( threadID ) { - setCommentEdit( false ); - setHasCommentReply( true ); - } - }; - - const confirmReplyComment = () => {}; - - // Get the date time format from WordPress settings. - const dateTimeFormat = 'h:i A'; const resultThreads = selectedThreads.map( ( thread ) => thread ).reverse(); return ( @@ -170,379 +74,8 @@ export default function CollabSidebar() { icon={ commentIcon } >

- { - // If there are no threads, show a message indicating no threads are available. - ( ! Array.isArray( resultThreads ) || - resultThreads.length === 0 ) && ( - - - { __( 'No comments available' ) } - - - ) - } - { Array.isArray( resultThreads ) && - resultThreads.length > 0 && - resultThreads.reverse().map( ( thread ) => ( - - - { - - - { thread.author_name } - - - - - { thread.status !== 'approved' && ( - - - - - { - setShowConfirmationTabId( - thread.id - ); - onEditComment( - thread.id - ); - }, - }, - { - title: __( 'Delete' ), - onClick: () => { - setCommentEdit( - false - ); - setShowConfirmationTabId( - thread.id - ); - setDeleteCommentShowConfirmation( - true - ); - }, - }, - { - title: __( 'Reply' ), - onClick: () => { - setShowConfirmationTabId( - thread.id - ); - onReplyComment( - thread.id - ); - }, - }, - ] } - /> - - ) } - { thread.status === 'approved' && ( - - - - ) } - - - - - { commentEdit && - thread.id === showConfirmationTabId && ( - <> - { - setNewEditedComment( - value - ); - } } - /> - - setCommentEdit( false ) - } - > - - - - - - - ) } - - { thread.content.rendered } - - - - - { hasCommentReply && - thread.id === showConfirmationTabId && ( - - - { - setCommentReply( value ); - } } - /> - - setHasCommentReply( false ) - } - > - - - - - - - - ) } - - { commentConfirmation && - thread.id === showConfirmationTabId && ( - - { __( 'Thread marked as resolved.' ) } - - ) } - { commentEditedMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Thread edited successfully.' ) } - - ) } - { commentDeleteMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Thread deleted successfully.' ) } - - ) } - - { showConfirmation && - thread.id === showConfirmationTabId && ( - - setShowConfirmation( false ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" - > -

- { __( - 'Are you sure you want to mark this thread as resolved?' - ) } -

- - - - -
- ) } - - { deleteCommentShowConfirmation && - thread.id === showConfirmationTabId && ( - - setDeleteCommentShowConfirmation( - false - ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" - > -

- { __( - 'Are you sure you want to delete this thread?' - ) } -

- - - - -
- ) } -
- ) ) } + +
); diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index 4fbabbb22f99da..bb4c8fde25893c 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -1,155 +1,174 @@ .editor-collab-sidebar { - &__activities { - padding: 16px; - background-color: #f9f9f9; - } - - &__thread { - position: relative; - padding: 16px; - border-radius: 8px; - border: 1px solid $gray-300; - background-color: $gray-100; - margin-bottom: 16px; - - &__focus { - border: 1.5px solid #3858e9; - background-color: $white; - } - } - - - &__editarea { - flex: 1; - - .components-textarea-control__input { - border: 1px solid $gray-300; - border-radius: 8px; - padding: 10px 15px; - background-color: $gray-100; - } - } - - &__commentbtn { - margin-bottom: 10px; - } - - - &__userName { - font-size: 12px; - font-weight: 400; - line-height: 16px; - text-align: left; - color: $gray-700; - text-transform: capitalize; - } - - &__usertime { - font-size: 12px; - font-weight: 400; - line-height: 16px; - text-align: left; - color: $gray-700; - } - - &__usercomment { - font-size: 13px; - font-weight: 400; - line-height: 20px; - text-align: left; - color: $gray-900; - - p { - margin-bottom: 0; - } - } - - &__userIcon { - border-radius: 50%; - flex-shrink: 0; - } - - &__userstatus { - button { - font-size: 12px; - font-weight: 400; - line-height: 16px; - padding: 0; - height: auto; - box-shadow: none; - outline: none; - } - } - - &__useroverlay { - background-color: rgba(0, 0, 0, 0.7); - width: 100%; - height: 100%; - text-align: center; - position: absolute; - top: 0; - left: 0; - z-index: 1; - padding: 15px; - border-radius: 8px; - color: $white; - - p { - margin-bottom: 15px; - } - - button { - height: 24px; - padding: 6px; - color: $white; - } - } - - &__resolvedIcon { - fill: #008000; - border-radius: 50%; - width: 18px; - height: 18px; - border: 1px solid #008000; - margin-right: 2px; - margin-top: 5px; - } - - &__commentUpdate { - margin-left: auto; - - .components-button { - &.is-compact { - &.has-icon:not(.has-text) { - min-width: 24px; - padding: 0; - width: 24px; - height: 24px; - flex-shrink: 0; - } - } - } - - .components-dropdown { - &.is-compact { - flex-shrink: 0; - - .components-button { - min-width: 24px; - padding: 0; - width: 24px; - height: 24px; - } - } - } - } + &__activities { + padding: 16px; + background-color: #f9f9f9; + } + + &__thread { + position: relative; + padding: 16px; + border-radius: 8px; + border: 1px solid $gray-300; + background-color: $gray-100; + margin-bottom: 16px; + + .components-base-control__field { + margin-bottom: 0 + } + + input[type=text] { + @include input-control; + border-radius: 6px; + padding: 8px 15px; + } + } + + &__activethread { + border: 1.5px solid #3858E9; + background-color: $white; + box-shadow: 0px 5.5px 7.8px -0.3px rgba(0, 0, 0, 0.102); + } + + + &__editarea, + &__replyComment { + flex: 1; + + &__textarea{ + width: 100%; + } + + .components-textarea-control__input { + border: 1px solid $gray-300; + border-radius: 8px; + padding: 10px 15px; + background-color: $gray-100; + } + } + + &__childThread { + margin-top: 15px; + } + + &__userName { + font-size: 12px; + font-weight: 400; + line-height: 16px; + text-align: left; + color: $gray-700; + text-transform: capitalize; + } + + &__usertime { + font-size: 12px; + font-weight: 400; + line-height: 16px; + text-align: left; + color: $gray-700; + } + + &__usercomment { + font-size: 13px; + font-weight: 400; + line-height: 20px; + text-align: left; + color: $gray-900; + + p { + margin-bottom: 0; + } + } + + &__userIcon { + border-radius: 50%; + flex-shrink: 0; + } + + &__userstatus { + button { + font-size: 12px; + font-weight: 400; + line-height: 16px; + padding: 0; + height: auto; + box-shadow: none; + outline: none; + } + } + + &__useroverlay { + background-color: rgba(0, 0, 0, 0.7); + width: 100%; + height: 100%; + text-align: center; + position: absolute; + top: 0; + left: 0; + z-index: 1; + padding: 15px; + border-radius: 8px; + color: $white; + + p { + margin-bottom: 15px; + } + + button { + height: 24px; + padding: 6px; + color: $white; + } + } + + &__resolvedIcon { + fill: #008000; + border-radius: 50%; + width: 18px; + height: 18px; + border: 1px solid #008000; + margin-right: 2px; + margin-top: 5px; + } + + &__commentUpdate { + margin-left: auto; + + .components-button { + &.is-compact { + &.has-icon:not(.has-text) { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + flex-shrink: 0; + } + } + } + + .components-dropdown { + &.is-compact { + flex-shrink: 0; + + .components-button { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + } + } + } + } + + &__resolvedText { + font-style: italic; + font-weight: 500 !important; + color: #808080 !important; + margin-top: 5px !important; + } +} - &__resolvedText { - font-style: italic; - font-weight: 500 !important; - color: #808080 !important; - margin-top: 5px !important; - } +.is-collab-block-selected { + background-color: color-mix( in srgb, currentColor 5%, #182F97 20% ); } .block-editor-format-toolbar__comment-board { From d5df8cc65541cb16bd6261f98fcfb8dff463a260 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 16:02:42 +0530 Subject: [PATCH 058/159] Refactor collab sidebar --- .../block-settings-dropdown.js | 12 +- .../collab/block-comment-menu-item.js | 7 +- .../components/collab-sidebar/add-comment.js | 557 +++++---- .../src/components/collab-sidebar/comments.js | 1085 ++++++++++++----- .../src/components/collab-sidebar/index.js | 42 +- 5 files changed, 1097 insertions(+), 606 deletions(-) diff --git a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js index b4efc87a269cd8..707fc064d87e13 100644 --- a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js +++ b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js @@ -290,12 +290,12 @@ export function BlockSettingsDropdown( { ) } { isBlockCommentExperimentEnabled && - ! blockClassName && ( - - ) } + ! blockClassName && ( + + ) }
{ canCopyStyles && ! isContentOnly && ( diff --git a/packages/block-editor/src/components/collab/block-comment-menu-item.js b/packages/block-editor/src/components/collab/block-comment-menu-item.js index 8ff965675db6a7..2846d506dfca4d 100644 --- a/packages/block-editor/src/components/collab/block-comment-menu-item.js +++ b/packages/block-editor/src/components/collab/block-comment-menu-item.js @@ -7,13 +7,12 @@ import { collabComment } from '@wordpress/icons'; import { useDispatch } from '@wordpress/data'; export default function BlockCommentMenuItem( { clientId, onClose } ) { - const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); const openCollabBoard = () => { onClose(); - openGeneralSidebar("edit-post/collab-sidebar"); - } + openGeneralSidebar( 'edit-post/collab-sidebar' ); + }; return ( { __( 'Add Comment' ) } - + ); } diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 38e027e2367be4..41c99d0744ec00 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -15,14 +15,20 @@ import { } from '@wordpress/components'; import { dateI18n, format } from '@wordpress/date'; import { - Icon + Icon, + edit as editIcon, + trash as deleteIcon, + commentAuthorAvatar as userIcon, } from '@wordpress/icons'; /** * Renders the new comment form. + * + * @param {Object} root0 The component props. + * @param {Object} root0.threads Comments. + * @param {Function} root0.setReloadComments Function to reload comments. */ -export function AddComment({ threads, setReloadComments }) { - +export function AddComment( { threads, setReloadComments } ) { const generateNewComment = () => ( { commentId: Date.now(), createdBy: currentUser, @@ -35,24 +41,26 @@ export function AddComment({ threads, setReloadComments }) { // State to manage the comment thread. const [ inputComment, setInputComment ] = useState( '' ); - const [ isResolved, setIsResolved ] = useState( false ); const [ isEditing, setIsEditing ] = useState( null ); + const [ showConfirmation, setShowConfirmation ] = useState( false ); + const curruntUserData = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals return select( 'core' ).getCurrentUser(); }, [] ); - let userAvatar = 'https://secure.gravatar.com/avatar/92929292929292929292929292929292?s=48&d=mm&r=g'; - if( curruntUserData?.avatar_urls ) { + let userAvatar = + 'https://secure.gravatar.com/avatar/92929292929292929292929292929292?s=48&d=mm&r=g'; + if ( curruntUserData?.avatar_urls ) { userAvatar = curruntUserData?.avatar_urls[ 48 ]; } - + //const userAvatar = curruntUserData?.avatar_urls[ 48 ] ? curruntUserData?.avatar_urls[ 48 ] : null; - + const currentUser = curruntUserData?.name || null; const allThreads = []; - const threadId = 0; + let threadId = 0; const currentThread = allThreads[ threadId ] ?? {}; const isCurrentThreadResolved = currentThread.threadIsResolved || false; const commentsCount = isCurrentThreadResolved @@ -64,16 +72,18 @@ export function AddComment({ threads, setReloadComments }) { return select( 'core/editor' ).getCurrentPostId(); }, [] ); - const clientId = useSelect((select) => { + const clientId = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals - const { getSelectedBlockClientId } = select('core/block-editor'); - return getSelectedBlockClientId(); - }, []); + const { getSelectedBlockClientId } = select( 'core/block-editor' ); + return getSelectedBlockClientId(); + }, [] ); - const blockClassName = useSelect((select) => { + const blockClassName = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core/block-editor' ).getBlock( select('core/block-editor').getSelectedBlockClientId() )?.attributes?.className; - }, []); + return select( 'core/block-editor' ).getBlock( + select( 'core/block-editor' ).getSelectedBlockClientId() + )?.attributes?.className; + }, [] ); // Get the dispatch functions to save the comment and update the block attributes. // eslint-disable-next-line @wordpress/data-no-store-string-literals @@ -86,27 +96,10 @@ export function AddComment({ threads, setReloadComments }) { } ); }; - // Helper function to get updated comments structure - const getUpdatedComments = ( newComment, threadKey ) => ( { - ...threads, - [ threadKey ]: { - isResolved: false, - createdAt: - threads?.threadKey?.createdAt || new Date().toISOString(), - createdBy: currentUser, - comments: { - 0: newComment, - }, - }, - } ); - - // Function to save the comment. const saveComment = () => { - const newComment = generateNewComment(); - const threadId = newComment?.commentId; - // const updatedComments = getUpdatedComments( newComment, threadId ); + threadId = newComment?.commentId; apiFetch( { path: '/wp/v2/comments', @@ -119,13 +112,12 @@ export function AddComment({ threads, setReloadComments }) { comment_author: currentUser, comment_approved: 0, }, - } ).then( (response) => { - const threadId = response?.id; - setAttributes( clientId, threadId ); - setInputComment(''); + } ).then( ( response ) => { + setAttributes( clientId, response?.id ); + setInputComment( '' ); setReloadComments( true ); + //onClose(); } ); - }; // Function to edit the comment. @@ -148,7 +140,7 @@ export function AddComment({ threads, setReloadComments }) { setInputComment( '' ); setIsEditing( null ); }; - + // Function to delete a comment. const deleteComment = ( commentId ) => { // Filter out the comment to be deleted. @@ -175,256 +167,253 @@ export function AddComment({ threads, setReloadComments }) { // Function to hide the confirmation overlay. const hideConfirmationOverlay = () => setShowConfirmation( false ); - // On cancel, remove the border if no comments are present. const handleCancel = () => { //onClose(); }; - return ( <> - { null !== clientId && undefined === blockClassName && ( - - { 0 < commentsCount && ! isCurrentThreadResolved && ( - <> - { currentThread.comments.map( - ( - { - createdBy, - comment, - timestamp, - commentId, - }, - index - ) => ( - <> - { isEditing === commentId && ( - - - - - { currentUser } - - - - setInputComment( val ) - } - placeholder={ __( - 'Add comment' - ) } - className="block-editor-format-toolbar__comment-input" - /> - - - + + + + { 0 === thread.parent ? ( - - ) } - { thread.status === 'approved' && ( - - - - ) } - -
- { + setShowConfirmationTabId( + thread.id + ); + onEditComment( + thread.id + ); + }, + }, + { + title: __( 'Delete' ), + onClick: () => { + setCommentEdit( + false + ); + setShowConfirmationTabId( + thread.id + ); + setDeleteCommentShowConfirmation( + true + ); + }, + }, + ] } + /> + ) } + + ) } + { thread.status === 'approved' && ( + + + + ) } + + + + - - { commentEdit && - thread.id === showConfirmationTabId && ( - <> - { - setNewEditedComment( - value - ); - } } - /> - - setCommentEdit( false ) - } - > - - - - - - - ) } - - { thread.content.rendered } - - - - - { hasCommentReply && - thread.id === showConfirmationTabId && ( - - + { commentEdit && + thread.id === showConfirmationTabId && ( + <> /g, + '' + ) + : thread.content.rendered.replace( + /<\/?p>/g, + '' + ) + } onChange={ ( value ) => { - setCommentReply( value ); + setNewEditedComment( + value + ); } } /> - setHasCommentReply( false ) + setCommentEdit( false ) } > + + ) } + { thread.content.rendered } + + + + { hasCommentReply && + thread.id === showConfirmationTabId && ( + + + { + setCommentReply( value ); + } } + /> + + setHasCommentReply( false ) + } + > + + + + + + + ) } + + { commentConfirmation && + thread.id === showConfirmationTabId && ( + + { __( 'Thread marked as resolved.' ) } + + ) } + { commentEditedMessage && + thread.id === showConfirmationTabId && ( + + { __( 'Thread edited successfully.' ) } + + ) } + { commentDeleteMessage && + thread.id === showConfirmationTabId && ( + + { __( 'Thread deleted successfully.' ) } + + ) } + { replyMessage && + thread.id === showConfirmationTabId && ( + + { __( 'Reply added successfully.' ) } + + ) } + + { showConfirmation && + thread.id === showConfirmationTabId && ( + + setShowConfirmation( false ) + } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to mark this thread as resolved?' + ) } +

+ + + - ) } +
+ ) } - { commentConfirmation && - thread.id === showConfirmationTabId && ( - - { __( 'Thread marked as resolved.' ) } - - ) } - { commentEditedMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Thread edited successfully.' ) } - - ) } - { commentDeleteMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Thread deleted successfully.' ) } - - ) } + { deleteCommentShowConfirmation && + thread.id === showConfirmationTabId && ( + + setDeleteCommentShowConfirmation( + false + ) + } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" + > +

+ { __( + 'Are you sure you want to delete this thread?' + ) } +

+ + + + +
+ ) } - { showConfirmation && - thread.id === showConfirmationTabId && ( - - setShowConfirmation( false ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" + { 0 < thread?.reply?.length && + thread.reply.map( ( reply ) => ( + + -

- { __( - 'Are you sure you want to mark this thread as resolved?' + { + + + { reply.author_name } + + + + + { reply.status !== 'approved' && ( + + { 0 === reply.parent && ( + { + setShowConfirmationTabId( + reply.id + ); + onEditComment( + reply.id + ); + }, + }, + { + title: __( + 'Delete' + ), + onClick: + () => { + setCommentEdit( + false + ); + setShowConfirmationTabId( + reply.id + ); + setDeleteCommentShowConfirmation( + true + ); + }, + }, + { + title: __( + 'Reply' + ), + onClick: + () => { + setShowConfirmationTabId( + reply.id + ); + onReplyComment( + reply.id + ); + }, + }, + ] } + /> + ) } + + { 0 !== reply.parent && + thread.status !== + 'approved' && ( + { + setShowConfirmationTabId( + reply.id + ); + onEditComment( + reply.id + ); + }, + }, + { + title: __( + 'Delete' + ), + onClick: + () => { + setCommentEdit( + false + ); + setShowConfirmationTabId( + reply.id + ); + setDeleteCommentShowConfirmation( + true + ); + }, + }, + ] } + /> + ) } + ) } -

- - + + +
+ + ) } + + { reply.content.rendered } + +
+ + + { hasCommentReply && + reply.id === showConfirmationTabId && ( + - { __( 'Yes' ) } - - + + +
+
+ + ) } + + { commentConfirmation && + reply.id === showConfirmationTabId && ( + + { __( + 'Thread marked as resolved.' + ) } + + ) } + { commentEditedMessage && + reply.id === showConfirmationTabId && ( + + { __( + 'Thread edited successfully.' + ) } + + ) } + { commentDeleteMessage && + reply.id === showConfirmationTabId && ( + + { __( + 'Thread deleted successfully.' + ) } + + ) } + { replyMessage && + reply.id === showConfirmationTabId && ( + + { __( + 'Reply added successfully.' + ) } + + ) } + + { showConfirmation && + reply.id === showConfirmationTabId && ( + setShowConfirmation( false ) } + className="editor-collab-sidebar__useroverlay confirmation-overlay" + spacing="0" + justify="space-between" > - { __( 'No' ) } - - - - ) } +

+ { __( + 'Are you sure you want to mark this thread as resolved?' + ) } +

+ + + + + + ) } - { deleteCommentShowConfirmation && - thread.id === showConfirmationTabId && ( - - setDeleteCommentShowConfirmation( - false - ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" - > -

- { __( - 'Are you sure you want to delete this thread?' - ) } -

- - - - -
- ) } - - ) - ) - } - - ); -} \ No newline at end of file +

+ { __( + 'Are you sure you want to delete this thread?' + ) } +

+ + + + + + ) } + + ) ) } + + ) ) } + + ); +} diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 773c134d59c784..3fe4d6b863ba46 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -5,10 +5,8 @@ import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; -import { useState, useEffect, RawHTML } from '@wordpress/element'; -import { - comment as commentIcon, -} from '@wordpress/icons'; +import { useState, useEffect } from '@wordpress/element'; +import { comment as commentIcon } from '@wordpress/icons'; /** * Internal dependencies @@ -31,7 +29,7 @@ export default function CollabSidebar() { }, [] ); const [ threads, setThreads ] = useState( [] ); - const [ reloadComments, setReloadComments ] = useState( false ); + const [ reloadComments, setReloadComments ] = useState( false ); useEffect( () => { if ( postId ) { @@ -42,14 +40,35 @@ export default function CollabSidebar() { '&type=block_comment' + '&status=any&per_page=100', method: 'GET', - } ).then( ( response ) => { - const filteredComments = response.filter( + } ).then( ( data ) => { + // Create a compare to store the references to all objects by id + const compare = {}; + const result = []; + + // Initialize each object with an empty `reply` array + data.forEach( ( item ) => { + compare[ item.id ] = { ...item, reply: [] }; + } ); + + // Iterate over the data to build the tree structure + data.forEach( ( item ) => { + if ( item.parent === 0 ) { + // If parent is 0, it's a root item, push it to the result array + result.push( compare[ item.id ] ); + } else if ( + compare[ item.parent ] && + 'trash' !== item.status + ) { + // Otherwise, find its parent and push it to the parent's `reply` array + compare[ item.parent ].reply.push( compare[ item.id ] ); + } + } ); + const filteredComments = result.filter( ( comment ) => comment.status !== 'trash' ); setThreads( Array.isArray( filteredComments ) ? filteredComments : [] ); - setReloadComments( false ); } ); } }, [ postId, reloadComments ] ); @@ -74,8 +93,11 @@ export default function CollabSidebar() { icon={ commentIcon } >
- - + +
); From 533c5d4a0715c6c7e4cf5750466cc04507327873 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 16:25:27 +0530 Subject: [PATCH 059/159] Remove unneccessory snapshot tests --- .../collab/test/__snapshots__/index.js.snap | 169 ---------------- .../src/components/collab/test/index.js | 183 ------------------ 2 files changed, 352 deletions(-) delete mode 100644 packages/block-editor/src/components/collab/test/__snapshots__/index.js.snap delete mode 100644 packages/block-editor/src/components/collab/test/index.js diff --git a/packages/block-editor/src/components/collab/test/__snapshots__/index.js.snap b/packages/block-editor/src/components/collab/test/__snapshots__/index.js.snap deleted file mode 100644 index f2915ead7417b1..00000000000000 --- a/packages/block-editor/src/components/collab/test/__snapshots__/index.js.snap +++ /dev/null @@ -1,169 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`AlignmentUI should allow custom alignment controls to be specified 1`] = ` -
-
-
- -
-
- -
-
-
-`; - -exports[`AlignmentUI should match snapshot when controls are hidden 1`] = ` -
-
- -
-
-`; - -exports[`AlignmentUI should match snapshot when controls are visible 1`] = ` -
-
-
- -
-
- -
-
- -
-
-
-`; diff --git a/packages/block-editor/src/components/collab/test/index.js b/packages/block-editor/src/components/collab/test/index.js deleted file mode 100644 index 178ba294127c31..00000000000000 --- a/packages/block-editor/src/components/collab/test/index.js +++ /dev/null @@ -1,183 +0,0 @@ -/** - * External dependencies - */ -import { render, screen } from '@testing-library/react'; -import userEvent from '@testing-library/user-event'; - -/** - * WordPress dependencies - */ -import { alignLeft, alignCenter } from '@wordpress/icons'; - -/** - * Internal dependencies - */ -import AlignmentUI from '../ui'; - -describe( 'AlignmentUI', () => { - const alignment = 'left'; - const onChangeSpy = jest.fn(); - - afterEach( () => { - onChangeSpy.mockClear(); - } ); - - test( 'should match snapshot when controls are hidden', () => { - const { container } = render( - - ); - - expect( container ).toMatchSnapshot(); - } ); - - test( 'should match snapshot when controls are visible', () => { - const { container } = render( - - ); - - expect( container ).toMatchSnapshot(); - } ); - - test( 'should expand controls when toggled', async () => { - const user = userEvent.setup(); - - const { unmount } = render( - - ); - - expect( - screen.queryByRole( 'menuitemradio', { - name: /^Align text \w+$/, - } ) - ).not.toBeInTheDocument(); - - await user.click( - screen.getByRole( 'button', { - name: 'Align text', - } ) - ); - - expect( - screen.getAllByRole( 'menuitemradio', { - name: /^Align text \w+$/, - } ) - ).toHaveLength( 3 ); - - // Cancel running effects, like delayed dropdown menu popover positioning. - unmount(); - } ); - - test( 'should call on change with undefined when a control is already active', async () => { - const user = userEvent.setup(); - - render( - - ); - - const activeControl = screen.getByRole( 'button', { - name: /^Align text \w+$/, - pressed: true, - } ); - - await user.click( activeControl ); - - expect( activeControl ).toHaveAttribute( 'align', alignment ); - expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); - expect( onChangeSpy ).toHaveBeenCalledWith( undefined ); - } ); - - test( 'should call on change a new value when the control is not active', async () => { - const user = userEvent.setup(); - - render( - - ); - - const inactiveControl = screen.getByRole( 'button', { - name: 'Align text center', - pressed: false, - } ); - - await user.click( inactiveControl ); - - expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); - expect( onChangeSpy ).toHaveBeenCalledWith( 'center' ); - } ); - - test( 'should allow custom alignment controls to be specified', async () => { - const user = userEvent.setup(); - - const { container } = render( - - ); - - expect( container ).toMatchSnapshot(); - - expect( - screen.getAllByRole( 'button', { - name: /^My custom \w+$/, - } ) - ).toHaveLength( 2 ); - - // Should correctly call on change when right alignment is pressed (active alignment) - const rightControl = screen.getByRole( 'button', { - name: 'My custom right', - } ); - - await user.click( rightControl ); - - expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); - expect( onChangeSpy ).toHaveBeenCalledWith( undefined ); - onChangeSpy.mockClear(); - - // Should correctly call on change when right alignment is pressed (inactive alignment) - const leftControl = screen.getByRole( 'button', { - name: 'My custom left', - } ); - - await user.click( leftControl ); - - expect( onChangeSpy ).toHaveBeenCalledTimes( 1 ); - expect( onChangeSpy ).toHaveBeenCalledWith( 'custom-left' ); - } ); -} ); From c0a5dcda2910cd651d4955df45246811df16a9ef Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 16:28:23 +0530 Subject: [PATCH 060/159] Remove collab board component --- .../components/collab/collab-board/index.js | 442 ------------------ 1 file changed, 442 deletions(-) delete mode 100644 packages/block-editor/src/components/collab/collab-board/index.js diff --git a/packages/block-editor/src/components/collab/collab-board/index.js b/packages/block-editor/src/components/collab/collab-board/index.js deleted file mode 100644 index 2057456691a380..00000000000000 --- a/packages/block-editor/src/components/collab/collab-board/index.js +++ /dev/null @@ -1,442 +0,0 @@ -/** - * WordPress dependencies - */ -// eslint-disable-next-line no-restricted-imports -import apiFetch from '@wordpress/api-fetch'; -import { __ } from '@wordpress/i18n'; -import { useState } from '@wordpress/element'; -import { - TextControl, - Button, - CheckboxControl, - __experimentalHStack as HStack, - __experimentalVStack as VStack, - Modal, -} from '@wordpress/components'; -import { dateI18n, format, getSettings } from '@wordpress/date'; -import { - commentAuthorAvatar as userIcon, - Icon, - trash as deleteIcon, - edit as editIcon, -} from '@wordpress/icons'; - -import { useSelect, useDispatch } from '@wordpress/data'; -//import { store as coreStore } from '@wordpress/core-data'; -//import { useEntityProp } from '@wordpress/core-data'; - -export default function BlockCommentModal( { clientId, onClose, threadId } ) { - // State to manage the comment thread. - const [ inputComment, setInputComment ] = useState( '' ); - const [ isResolved, setIsResolved ] = useState( false ); - const [ isEditing, setIsEditing ] = useState( null ); - const [ showConfirmation, setShowConfirmation ] = useState( false ); - const curruntUserData = useSelect( ( select ) => { - // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core' ).getCurrentUser(); - }, [] ); - - const userAvatar = curruntUserData.avatar_urls[ 48 ] || null; - const currentUser = curruntUserData?.name || null; - - const allThreads = []; - const currentThread = allThreads[ threadId ] ?? {}; - const isCurrentThreadResolved = currentThread.threadIsResolved || false; - const commentsCount = isCurrentThreadResolved - ? 0 - : currentThread.comments?.length || 0; - // eslint-disable-next-line @wordpress/data-no-store-string-literals - const postID = useSelect( ( select ) => { - // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core/editor' ).getCurrentPostId(); - }, [] ); - - // Get the dispatch functions to save the comment and update the block attributes. - // eslint-disable-next-line @wordpress/data-no-store-string-literals - const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); - // Helper function to generate a new comment. - const generateNewComment = () => ( { - commentId: Date.now(), - createdBy: currentUser, - comment: inputComment, - createdAt: new Date().toISOString(), - } ); - - // // Function to add a border class to the content reference. - const setAttributes = () => { - updateBlockAttributes( clientId, { - className: `block-editor-collab__${ threadId }`, - } ); - }; - - // Helper function to get updated comments structure - const getUpdatedComments = ( newComment, threadKey ) => ( { - ...allThreads, - [ threadKey ]: { - isResolved, - createdAt: - allThreads?.threadKey?.createdAt || new Date().toISOString(), - createdBy: currentUser, - comments: [ - ...( allThreads[ threadKey ]?.comments || [] ), - newComment, - ], - }, - } ); - - // Function to save the comment. - const saveComment = () => { - const newComment = generateNewComment(); - threadId = newComment?.commentId; - const updatedComments = getUpdatedComments( newComment, threadId ); - - apiFetch( { - path: '/wp/v2/comments', - method: 'POST', - data: { - post: postID, - content: newComment.comment, - comment_date: newComment.createdAt, - comment_type: 'block_comment', - meta: updatedComments, - comment_author: currentUser, - comment_approved: 0, - }, - } ).then( ( response ) => { - threadId = response?.id; - setAttributes( clientId, threadId ); - onClose(); - } ); - }; - - // Function to edit the comment. - const editComment = ( commentId ) => { - const editedComments = { ...allThreads }; - - if ( - editedComments[ threadId ] && - editedComments[ threadId ].comments - ) { - editedComments[ threadId ].comments.map( ( comment ) => { - if ( comment.commentId === commentId ) { - comment.comment = inputComment; - comment.date = new Date().toISOString(); - } - return comment; - } ); - } - - setInputComment( '' ); - setIsEditing( null ); - }; - - // Function to mark thread as resolved - const markThreadAsResolved = () => { - setIsResolved( true ); - - const updatedComments = { ...allThreads }; - - updatedComments[ threadId ] = { - ...updatedComments[ threadId ], - isResolved: true, - resolvedBy: currentUser, - resolvedAt: new Date().toISOString(), - }; - - onClose(); - }; - - // Function to delete a comment. - const deleteComment = ( commentId ) => { - // Filter out the comment to be deleted. - const currentComments = allThreads[ threadId ].comments.filter( - ( comment ) => comment.commentId !== commentId - ); - - const updatedComments = { ...allThreads }; - - // If there are no comments, delete the thread. - if ( currentComments.length === 0 ) { - delete updatedComments[ threadId ]; - } else { - updatedComments[ threadId ] = { - ...allThreads[ threadId ], - comments: currentComments, - }; - } - }; - - // Function to show the confirmation overlay. - const showConfirmationOverlay = () => setShowConfirmation( true ); - - // Function to hide the confirmation overlay. - const hideConfirmationOverlay = () => setShowConfirmation( false ); - - // Function to confirm and mark thread as resolved. - const confirmAndMarkThreadAsResolved = () => { - markThreadAsResolved(); - hideConfirmationOverlay(); - }; - - // On cancel, remove the border if no comments are present. - const handleCancel = () => { - onClose(); - }; - - // Get the date time format from WordPress settings. - const dateTimeFormat = getSettings().formats.datetime; - return ( - <> - - - { 0 < commentsCount && ! isCurrentThreadResolved && ( - <> - { currentThread.comments.map( - ( - { - createdBy, - comment, - timestamp, - commentId, - }, - index - ) => ( - <> - { isEditing === commentId && ( - - - - - { currentUser } - - - - setInputComment( val ) - } - placeholder={ __( - 'Add comment' - ) } - className="block-editor-format-toolbar__comment-input" - /> - - - - - ) } - - ); -} From 56e475b6a1582e73426fab42820e7557d56a6c5d Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 29 Aug 2024 17:38:18 +0530 Subject: [PATCH 061/159] Fix linting errors --- .../components/collab-sidebar/add-comment.js | 9 +- .../src/components/collab-sidebar/style.scss | 334 +++++++++--------- 2 files changed, 168 insertions(+), 175 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 41c99d0744ec00..43ffcd1fd7c4ae 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -116,7 +116,6 @@ export function AddComment( { threads, setReloadComments } ) { setAttributes( clientId, response?.id ); setInputComment( '' ); setReloadComments( true ); - //onClose(); } ); }; @@ -164,13 +163,7 @@ export function AddComment( { threads, setReloadComments } ) { // Function to show the confirmation overlay. const showConfirmationOverlay = () => setShowConfirmation( true ); - // Function to hide the confirmation overlay. - const hideConfirmationOverlay = () => setShowConfirmation( false ); - - // On cancel, remove the border if no comments are present. - const handleCancel = () => { - //onClose(); - }; + const handleCancel = () => {}; return ( <> diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index bb4c8fde25893c..6c8b582ba07314 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -1,174 +1,174 @@ .editor-collab-sidebar { - &__activities { - padding: 16px; - background-color: #f9f9f9; - } - - &__thread { - position: relative; - padding: 16px; - border-radius: 8px; - border: 1px solid $gray-300; - background-color: $gray-100; - margin-bottom: 16px; - - .components-base-control__field { - margin-bottom: 0 - } - - input[type=text] { - @include input-control; - border-radius: 6px; - padding: 8px 15px; - } - } - - &__activethread { - border: 1.5px solid #3858E9; - background-color: $white; - box-shadow: 0px 5.5px 7.8px -0.3px rgba(0, 0, 0, 0.102); - } - - - &__editarea, - &__replyComment { - flex: 1; - - &__textarea{ - width: 100%; - } - - .components-textarea-control__input { - border: 1px solid $gray-300; - border-radius: 8px; - padding: 10px 15px; - background-color: $gray-100; - } - } - - &__childThread { - margin-top: 15px; - } - - &__userName { - font-size: 12px; - font-weight: 400; - line-height: 16px; - text-align: left; - color: $gray-700; - text-transform: capitalize; - } - - &__usertime { - font-size: 12px; - font-weight: 400; - line-height: 16px; - text-align: left; - color: $gray-700; - } - - &__usercomment { - font-size: 13px; - font-weight: 400; - line-height: 20px; - text-align: left; - color: $gray-900; - - p { - margin-bottom: 0; - } - } - - &__userIcon { - border-radius: 50%; - flex-shrink: 0; - } - - &__userstatus { - button { - font-size: 12px; - font-weight: 400; - line-height: 16px; - padding: 0; - height: auto; - box-shadow: none; - outline: none; - } - } - - &__useroverlay { - background-color: rgba(0, 0, 0, 0.7); - width: 100%; - height: 100%; - text-align: center; - position: absolute; - top: 0; - left: 0; - z-index: 1; - padding: 15px; - border-radius: 8px; - color: $white; - - p { - margin-bottom: 15px; - } - - button { - height: 24px; - padding: 6px; - color: $white; - } - } - - &__resolvedIcon { - fill: #008000; - border-radius: 50%; - width: 18px; - height: 18px; - border: 1px solid #008000; - margin-right: 2px; - margin-top: 5px; - } - - &__commentUpdate { - margin-left: auto; - - .components-button { - &.is-compact { - &.has-icon:not(.has-text) { - min-width: 24px; - padding: 0; - width: 24px; - height: 24px; - flex-shrink: 0; - } - } - } - - .components-dropdown { - &.is-compact { - flex-shrink: 0; - - .components-button { - min-width: 24px; - padding: 0; - width: 24px; - height: 24px; - } - } - } - } - - &__resolvedText { - font-style: italic; - font-weight: 500 !important; - color: #808080 !important; - margin-top: 5px !important; - } + &__activities { + padding: 16px; + background-color: #f9f9f9; + } + + &__thread { + position: relative; + padding: 16px; + border-radius: 8px; + border: 1px solid $gray-300; + background-color: $gray-100; + margin-bottom: 16px; + + .components-base-control__field { + margin-bottom: 0; + } + + input[type="text"] { + @include input-control; + border-radius: 6px; + padding: 8px 15px; + } + } + + &__activethread { + border: 1.5px solid #3858e9; + background-color: $white; + box-shadow: 0 5.5px 7.8px -0.3px rgba(0, 0, 0, 0.102); + } + + + &__editarea, + &__replyComment { + flex: 1; + + &__textarea { + width: 100%; + } + + .components-textarea-control__input { + border: 1px solid $gray-300; + border-radius: 8px; + padding: 10px 15px; + background-color: $gray-100; + } + } + + &__childThread { + margin-top: 15px; + } + + &__userName { + font-size: 12px; + font-weight: 400; + line-height: 16px; + text-align: left; + color: $gray-700; + text-transform: capitalize; + } + + &__usertime { + font-size: 12px; + font-weight: 400; + line-height: 16px; + text-align: left; + color: $gray-700; + } + + &__usercomment { + font-size: 13px; + font-weight: 400; + line-height: 20px; + text-align: left; + color: $gray-900; + + p { + margin-bottom: 0; + } + } + + &__userIcon { + border-radius: 50%; + flex-shrink: 0; + } + + &__userstatus { + button { + font-size: 12px; + font-weight: 400; + line-height: 16px; + padding: 0; + height: auto; + box-shadow: none; + outline: none; + } + } + + &__useroverlay { + background-color: rgba(0, 0, 0, 0.7); + width: 100%; + height: 100%; + text-align: center; + position: absolute; + top: 0; + left: 0; + z-index: 1; + padding: 15px; + border-radius: 8px; + color: $white; + + p { + margin-bottom: 15px; + } + + button { + height: 24px; + padding: 6px; + color: $white; + } + } + + &__resolvedIcon { + fill: #008000; + border-radius: 50%; + width: 18px; + height: 18px; + border: 1px solid #008000; + margin-right: 2px; + margin-top: 5px; + } + + &__commentUpdate { + margin-left: auto; + + .components-button { + &.is-compact { + &.has-icon:not(.has-text) { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + flex-shrink: 0; + } + } + } + + .components-dropdown { + &.is-compact { + flex-shrink: 0; + + .components-button { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + } + } + } + } + + &__resolvedText { + font-style: italic; + font-weight: 500 !important; + color: #808080 !important; + margin-top: 5px !important; + } } .is-collab-block-selected { - background-color: color-mix( in srgb, currentColor 5%, #182F97 20% ); + background-color: color-mix(in srgb, currentColor 5%, #182f97 20%); } .block-editor-format-toolbar__comment-board { From 03214303dd904fc19699e74a4748926d004c5a29 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 29 Aug 2024 19:33:12 +0530 Subject: [PATCH 062/159] comment board hide/show on Add comment and cancel button --- .../collab/block-comment-menu-item.js | 14 +- .../src/components/collab/toolbar.js | 2 + .../components/collab-sidebar/add-comment.js | 444 ++++++++++-------- .../src/components/collab-sidebar/index.js | 4 + 4 files changed, 258 insertions(+), 206 deletions(-) diff --git a/packages/block-editor/src/components/collab/block-comment-menu-item.js b/packages/block-editor/src/components/collab/block-comment-menu-item.js index 7a661cd64f74c3..167347f32a8787 100644 --- a/packages/block-editor/src/components/collab/block-comment-menu-item.js +++ b/packages/block-editor/src/components/collab/block-comment-menu-item.js @@ -4,14 +4,26 @@ import { __ } from '@wordpress/i18n'; import { MenuItem } from '@wordpress/components'; import { collabComment } from '@wordpress/icons'; -import { useDispatch } from '@wordpress/data'; +import { useSelect, useDispatch } from '@wordpress/data'; export default function BlockCommentMenuItem( { onClose } ) { // eslint-disable-next-line @wordpress/data-no-store-string-literals const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); + + const clientId = useSelect( ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const { getSelectedBlockClientId } = select( 'core/block-editor' ); + return getSelectedBlockClientId(); + }, [] ); + const openCollabBoard = () => { onClose(); + updateBlockAttributes( clientId, { + showCommentBoard: true, + } ); openGeneralSidebar( 'edit-post/collab-sidebar' ); }; diff --git a/packages/block-editor/src/components/collab/toolbar.js b/packages/block-editor/src/components/collab/toolbar.js index ce7f1742187ff5..e0caf204909358 100644 --- a/packages/block-editor/src/components/collab/toolbar.js +++ b/packages/block-editor/src/components/collab/toolbar.js @@ -4,6 +4,7 @@ import { __ } from '@wordpress/i18n'; import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; import { collabComment } from '@wordpress/icons'; +const openCollabBoard = () => {}; export default function BlockCommentToolbar( {} ) { return ( @@ -11,6 +12,7 @@ export default function BlockCommentToolbar( {} ) { diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index af4ec5cf0d1f2d..9ec6fb66ce5477 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -85,14 +85,23 @@ export function AddComment( { setReloadComments } ) { ?.blockCommentId; }, [] ); + const showAddCommentBoard = useSelect( ( select ) => { + const clientID = + // eslint-disable-next-line @wordpress/data-no-store-string-literals + select( 'core/block-editor' ).getSelectedBlockClientId(); + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core/block-editor' ).getBlock( clientID )?.attributes + ?.showCommentBoard; + }, [] ); + // Get the dispatch functions to save the comment and update the block attributes. // eslint-disable-next-line @wordpress/data-no-store-string-literals const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); // // Function to add a border class to the content reference. - const setAttributes = () => { + const setAttributes = ( commentID ) => { updateBlockAttributes( clientId, { - className: `block-editor-collab__${ threadId }`, + blockCommentId: commentID, } ); }; @@ -113,7 +122,7 @@ export function AddComment( { setReloadComments } ) { comment_approved: 0, }, } ).then( ( response ) => { - setAttributes( clientId, response?.id ); + setAttributes( response?.id ); setInputComment( '' ); setReloadComments( true ); } ); @@ -160,111 +169,39 @@ export function AddComment( { setReloadComments } ) { } }; - const handleCancel = () => {}; + const handleCancel = () => { + updateBlockAttributes( clientId, { + showCommentBoard: false, + } ); + }; return ( <> - { null !== clientId && 0 === blockCommentId && ( - - { 0 < commentsCount && ! isCurrentThreadResolved && ( - <> - { currentThread.comments.map( - ( - { - createdBy, - comment, - timestamp, - commentId, - }, - index - ) => ( - <> - { isEditing === commentId && ( - - - - - { currentUser } - - - - setInputComment( val ) - } - placeholder={ __( - 'Add comment' - ) } - className="block-editor-format-toolbar__comment-input" - /> - - @@ -361,9 +359,6 @@ export function Comments( { threads } ) { spacing="3" justify="flex-start" className="editor-collab-sidebar__commentbtn" - onRequestClose={ () => - setCommentEdit( false ) - } > @@ -461,7 +450,6 @@ export function Comments( { threads } ) { false ); } } - className="is-compact" > { __( 'Cancel' ) } @@ -498,82 +486,20 @@ export function Comments( { threads } ) { { showConfirmation && thread.id === showConfirmationTabId && ( - - setShowConfirmation( false ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" - > -

- { __( - 'Are you sure you want to mark this thread as resolved?' - ) } -

- - - - -
+ confirmAndMarkThreadAsResolved( thread.id ) } + discardAction={ () => setShowConfirmation( false ) } + /> ) } { deleteCommentShowConfirmation && thread.id === showConfirmationTabId && ( - - setDeleteCommentShowConfirmation( - false - ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" - > -

- { __( - 'Are you sure you want to delete this thread?' - ) } -

- - - - -
+ confirmDeleteComment( thread.id ) } + discardAction={ () => setDeleteCommentShowConfirmation( false ) } + /> ) } { 0 < thread?.reply?.length && @@ -629,7 +555,6 @@ export function Comments( { threads } ) { moreVertical } label="Select an action" - className="is-compact" controls={ [ { title: __( @@ -688,7 +613,6 @@ export function Comments( { threads } ) { moreVertical } label="Select an action" - className="is-compact" controls={ [ { title: __( @@ -769,11 +693,6 @@ export function Comments( { threads } ) { spacing="3" justify="flex-start" className="editor-collab-sidebar__commentbtn" - onRequestClose={ () => - setCommentEdit( - false - ) - } > - - -
+ confirmAndMarkThreadAsResolved( reply.id ) } + discardAction={ () => setShowConfirmation( + false + ) } + /> ) } { deleteCommentShowConfirmation && - reply.id === showConfirmationTabId && ( - - setDeleteCommentShowConfirmation( - false - ) - } - className="editor-collab-sidebar__useroverlay confirmation-overlay" - spacing="0" - justify="space-between" - > -

- { __( - 'Are you sure you want to delete this thread?' - ) } -

- - - - -
+ reply.id === showConfirmationTabId && ( + confirmDeleteComment( reply.id ) } + discardAction={ () => setDeleteCommentShowConfirmation( false ) } + /> ) }
) ) } @@ -1019,3 +867,26 @@ export function Comments( { threads } ) { ); } + +function ConfirmNotice({ cofirmMessage, confirmAction, discardAction}) { + return ( + +

+ { cofirmMessage ?? __( 'Are you sure?' ) } +

+ + + + +
+ ); +} diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 59d3a7be4e42aa..4312671231d2df 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -20,12 +20,11 @@ import { AddComment } from './add-comment'; const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; - + export { default as __experimentalBlockAlignmentMatrixControl } const modifyBlockCommentAttributes = ( settings, name ) => { - if ( name && name?.includes( 'core/' ) ) { - if ( - undefined === settings.attributes.blockCommentId || - null === settings.attributes.blockCommentId + if ( + name?.includes( 'core/' ) && + ! settings.attributes.blockCommentId ) { settings.attributes = { ...settings.attributes, @@ -39,7 +38,7 @@ const modifyBlockCommentAttributes = ( settings, name ) => { }, }; } - } + return settings; }; @@ -54,7 +53,12 @@ addFilter( * Renders the Collab sidebar. */ export default function CollabSidebar() { - const [ threads, setThreads ] = useState( [] ); + // Check if the experimental flag is enabled. + if ( ! isBlockCommentExperimentEnabled ) { + return null; // or maybe return some message indicating no threads are available. + } + + const [ threads, setThreads ] = useState( () => [] ); const [ reloadComments, setReloadComments ] = useState( false ); const postId = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals @@ -103,18 +107,7 @@ export default function CollabSidebar() { } }, [ postId, reloadComments ] ); - const { threads: selectedThreads } = useSelect( () => { - return { - threads, - }; - }, [ threads ] ); - - // Check if the experimental flag is enabled. - if ( ! isBlockCommentExperimentEnabled ) { - return null; // or maybe return some message indicating no threads are available. - } - - const resultThreads = selectedThreads.map( ( thread ) => thread ).reverse(); + const resultThreads = threads.map( ( thread ) => thread ).reverse(); return ( Date: Fri, 30 Aug 2024 18:26:37 +0530 Subject: [PATCH 066/159] Create comment header component --- .../src/components/collab-sidebar/comments.js | 410 ++++++------------ 1 file changed, 128 insertions(+), 282 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index bfcdc1af8e55fb..42dc9289e362ee 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -18,17 +18,17 @@ import { TextareaControl, Tooltip, } from '@wordpress/components'; -import { dateI18n, format } from '@wordpress/date'; +import { dateI18n, format, getSettings as getDateSettings } from '@wordpress/date'; import { Icon, check, published, moreVertical } from '@wordpress/icons'; import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; +import { useEntityProp } from '@wordpress/core-data'; export function Comments( { threads } ) { const [ showConfirmation, setShowConfirmation ] = useState( false ); const [ showConfirmationTabId, setShowConfirmationTabId ] = useState( 0 ); const [ commentConfirmation, setCommentConfirmation ] = useState( false ); - const [ deleteCommentShowConfirmation, setDeleteCommentShowConfirmation ] = - useState( false ); + const [ deleteCommentShowConfirmation, setDeleteCommentShowConfirmation ] = useState( false ); const [ commentDeleteMessage, setCommentDeleteMessage ] = useState( false ); const [ commentEdit, setCommentEdit ] = useState( false ); const [ newEditedComment, setNewEditedComment ] = useState( '' ); @@ -37,8 +37,6 @@ export function Comments( { threads } ) { const [ commentReply, setCommentReply ] = useState( '' ); const [ replyMessage, setReplyMessage ] = useState( false ); - // Get the date time format from WordPress settings. - const dateTimeFormat = 'h:i A'; const currentUserData = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals return select( 'core' ).getCurrentUser(); @@ -187,141 +185,27 @@ export function Comments( { threads } ) { id={ thread.id } spacing="2" > - - { - - - { thread.author_name } - - - - - { thread.status !== 'approved' && ( - - - - - { 0 === thread.parent ? ( - { - setShowConfirmationTabId( - thread.id - ); - onEditComment( - thread.id - ); - }, - }, - { - title: __( 'Delete' ), - onClick: () => { - setCommentEdit( - false - ); - setShowConfirmationTabId( - thread.id - ); - setDeleteCommentShowConfirmation( - true - ); - }, - }, - { - title: __( 'Reply' ), - onClick: () => { - setShowConfirmationTabId( - thread.id - ); - onReplyComment( - thread.id - ); - }, - }, - ] } - /> - ) : ( - { - setShowConfirmationTabId( - thread.id - ); - onEditComment( - thread.id - ); - }, - }, - { - title: __( 'Delete' ), - onClick: () => { - setCommentEdit( - false - ); - setShowConfirmationTabId( - thread.id - ); - setDeleteCommentShowConfirmation( - true - ); - }, - }, - ] } - /> - ) } - - ) } - { thread.status === 'approved' && ( - - - - ) } - - + { + setCommentConfirmation( false ); + setShowConfirmation( true ); + setShowConfirmationTabId( thread.id ); + } } + onEdit={ () => { + setShowConfirmationTabId( thread.id ); + onEditComment( thread.id ); + } } + onDelete={ () => { + setCommentEdit( false ); + setShowConfirmationTabId( thread.id ); + setDeleteCommentShowConfirmation( true ); + } } + onReply={ () => { + setShowConfirmationTabId( thread.id ); + onReplyComment( thread.id ); + } } + /> - - { - - - { reply.author_name } - - - - - { reply.status !== 'approved' && ( - - { 0 === reply.parent && ( - { - setShowConfirmationTabId( - reply.id - ); - onEditComment( - reply.id - ); - }, - }, - { - title: __( - 'Delete' - ), - onClick: - () => { - setCommentEdit( - false - ); - setShowConfirmationTabId( - reply.id - ); - setDeleteCommentShowConfirmation( - true - ); - }, - }, - { - title: __( - 'Reply' - ), - onClick: - () => { - setShowConfirmationTabId( - reply.id - ); - onReplyComment( - reply.id - ); - }, - }, - ] } - /> - ) } - - { 0 !== reply.parent && - thread.status !== - 'approved' && ( - { - setShowConfirmationTabId( - reply.id - ); - onEditComment( - reply.id - ); - }, - }, - { - title: __( - 'Delete' - ), - onClick: - () => { - setCommentEdit( - false - ); - setShowConfirmationTabId( - reply.id - ); - setDeleteCommentShowConfirmation( - true - ); - }, - }, - ] } - /> - ) } - - ) } - - + { + setCommentConfirmation( false ); + setShowConfirmation( true ); + setShowConfirmationTabId( reply.id ); + } } + onEdit={ () => { + setShowConfirmationTabId( reply.id ); + onEditComment( reply.id ); + } } + onDelete={ () => { + setCommentEdit( false ); + setShowConfirmationTabId( reply.id ); + setDeleteCommentShowConfirmation( true ); + } } + /> ); } + +function CommentHeader ( { thread, onResolve, onEdit, onDelete, onReply } ){ + const dateSettings = getDateSettings(); + const [ dateTimeFormat = dateSettings.formats.time ] = useEntityProp( + 'root', + 'site', + 'time_format' + ); + + const moreActions = []; + + onEdit && moreActions.push( { + title: __( 'Edit' ), + onClick: () => { onEdit }, + } ); + + onDelete && moreActions.push({ + title: __( 'Delete' ), + onClick: () => { onDelete }, + }); + + 0 === thread.parent && + onReply && moreActions.push( { + title: __( 'Reply' ), + onClick: () => { onReply }, + }); + + return( + + { + + + { thread.author_name } + + + + + { thread.status !== 'approved' && ( + + { onResolve && ( + + + + ) + } + + + ) } + { thread.status === 'approved' && ( + + + + ) } + + + ) +} \ No newline at end of file From 5c70d90123add0d587718a1542192c270ac8c3a9 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Fri, 30 Aug 2024 18:54:03 +0530 Subject: [PATCH 067/159] Fix linting errors --- .../components/collab-sidebar/add-comment.js | 6 +++ .../src/components/collab-sidebar/comments.js | 40 +++++++++++++++---- .../src/components/collab-sidebar/index.js | 1 - 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index cd0f519bc76c34..39b9bfca7edd87 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -227,6 +227,7 @@ export function AddComment( { setReloadComments } ) { spacing="2" > - @@ -662,20 +680,22 @@ function CommentHeader ( { thread, onResolve, onEdit, onDelete, onReply } ){ onEdit && moreActions.push( { title: __( 'Edit' ), - onClick: () => { onEdit }, + onClick: onEdit, } ); onDelete && moreActions.push({ title: __( 'Delete' ), - onClick: () => { onDelete }, + onClick: onDelete, }); 0 === thread.parent && onReply && moreActions.push( { title: __( 'Reply' ), - onClick: () => { onReply }, + onClick: onReply, }); + console.log('moreActions',moreActions); + return( { onResolve && ( - diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 4312671231d2df..22f99c612a07b1 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -20,7 +20,6 @@ import { AddComment } from './add-comment'; const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; - export { default as __experimentalBlockAlignmentMatrixControl } const modifyBlockCommentAttributes = ( settings, name ) => { if ( name?.includes( 'core/' ) && From cc28c6aef1b4c749a85b79f1b2db60447e7e89ef Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Fri, 30 Aug 2024 18:56:20 +0530 Subject: [PATCH 068/159] Remove logs --- packages/editor/src/components/collab-sidebar/comments.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index bb1359bc17b24a..367377423f65f5 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -694,8 +694,6 @@ function CommentHeader ( { thread, onResolve, onEdit, onDelete, onReply } ){ onClick: onReply, }); - console.log('moreActions',moreActions); - return( Date: Fri, 30 Aug 2024 19:13:09 +0530 Subject: [PATCH 069/159] Fix linting errors --- .../block-settings-dropdown.js | 14 +- .../src/components/collab/toolbar.js | 1 - .../components/collab-sidebar/add-comment.js | 319 +++++++++--------- .../src/components/collab-sidebar/comments.js | 185 ++++++---- .../src/components/collab-sidebar/index.js | 29 +- 5 files changed, 283 insertions(+), 265 deletions(-) diff --git a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js index 70dfc152c3b2c6..8fc7e3a3450f44 100644 --- a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js +++ b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js @@ -110,7 +110,7 @@ export function BlockSettingsDropdown( { openedBlockSettingsMenu: getOpenedBlockSettingsMenu(), isContentOnly: getBlockEditingMode( firstBlockClientId ) === 'contentOnly', - blockCommentID: commentID, + blockCommentID: commentID, }; }, [ firstBlockClientId ] @@ -290,12 +290,12 @@ export function BlockSettingsDropdown( { ) } { isBlockCommentExperimentEnabled && - ! blockCommentID && ( - - ) } + ! blockCommentID && ( + + ) } { canCopyStyles && ! isContentOnly && ( diff --git a/packages/block-editor/src/components/collab/toolbar.js b/packages/block-editor/src/components/collab/toolbar.js index e368c7aa29e62d..d28dff3a7df705 100644 --- a/packages/block-editor/src/components/collab/toolbar.js +++ b/packages/block-editor/src/components/collab/toolbar.js @@ -7,7 +7,6 @@ import { collabComment } from '@wordpress/icons'; import { useSelect, useDispatch } from '@wordpress/data'; export default function BlockCommentToolbar( {} ) { - // eslint-disable-next-line @wordpress/data-no-store-string-literals const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 39b9bfca7edd87..21fc07e25d3d13 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -181,202 +181,189 @@ export function AddComment( { setReloadComments } ) { null !== clientId && 0 === blockCommentId && ( - { 0 < commentsCount && ! isCurrentThreadResolved && currentThread.comments.map( - ( - { - createdBy, - comment, - timestamp, - commentId, - }, - index - ) => ( - <> - { isEditing === commentId && ( - - - - - { currentUser } - - - - setInputComment( - val - ) - } - placeholder={ __( - 'Add comment' - ) } - className="block-editor-format-toolbar__comment-input" - /> - - - @@ -668,7 +712,7 @@ function ConfirmNotice({ cofirmMessage, confirmAction, discardAction}) { ); } -function CommentHeader ( { thread, onResolve, onEdit, onDelete, onReply } ){ +function CommentHeader( { thread, onResolve, onEdit, onDelete, onReply } ) { const dateSettings = getDateSettings(); const [ dateTimeFormat = dateSettings.formats.time ] = useEntityProp( 'root', @@ -678,28 +722,27 @@ function CommentHeader ( { thread, onResolve, onEdit, onDelete, onReply } ){ const moreActions = []; - onEdit && moreActions.push( { - title: __( 'Edit' ), - onClick: onEdit, - } ); - - onDelete && moreActions.push({ - title: __( 'Delete' ), - onClick: onDelete, - }); - + onEdit && + moreActions.push( { + title: __( 'Edit' ), + onClick: onEdit, + } ); + + onDelete && + moreActions.push( { + title: __( 'Delete' ), + onClick: onDelete, + } ); + 0 === thread.parent && - onReply && moreActions.push( { - title: __( 'Reply' ), - onClick: onReply, - }); + onReply && + moreActions.push( { + title: __( 'Reply' ), + onClick: onReply, + } ); - return( - + return ( + { thread.status !== 'approved' && ( - + { onResolve && ( - - ) - } + ) } - ) } - ) -} \ No newline at end of file + ); +} diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 22f99c612a07b1..bfd7800eb48e11 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -21,22 +21,19 @@ import { AddComment } from './add-comment'; const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; const modifyBlockCommentAttributes = ( settings, name ) => { - if ( - name?.includes( 'core/' ) && - ! settings.attributes.blockCommentId - ) { - settings.attributes = { - ...settings.attributes, - blockCommentId: { - type: 'number', - default: 0, - }, - showCommentBoard: { - type: 'boolean', - default: false, - }, - }; - } + if ( name?.includes( 'core/' ) && ! settings.attributes.blockCommentId ) { + settings.attributes = { + ...settings.attributes, + blockCommentId: { + type: 'number', + default: 0, + }, + showCommentBoard: { + type: 'boolean', + default: false, + }, + }; + } return settings; }; From a5e12e19ab6bf650a0d8751ee2be4b492bd0c960 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Fri, 30 Aug 2024 19:21:04 +0530 Subject: [PATCH 070/159] made changes as per PR feedback --- lib/compat/wordpress-6.6/rest-api.php | 19 +-- lib/compat/wordpress-6.7/rest-api.php | 43 ++++++ package-lock.json | 6 - .../src/components/collab-sidebar/comments.js | 8 +- .../src/components/collab-sidebar/style.scss | 140 ++---------------- 5 files changed, 63 insertions(+), 153 deletions(-) diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index d54f2e21fc73bc..bcd404039b2f7d 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -191,8 +191,8 @@ function gutenberg_block_editor_preload_paths_6_6( $paths, $context ) { * @param WP_REST_Request $request The request object. * @return WP_REST_Response The updated response object. */ -if ( ! function_exists( 'update_comment_meta_from_rest_request' ) ) { - function update_comment_meta_from_rest_request( $response, $comment, $request ) { +if ( ! function_exists( 'update_comment_meta_from_rest_request_6_6' ) ) { + function update_comment_meta_from_rest_request_6_6( $response, $comment, $request ) { if ( isset( $request['comment_type'] ) && ! empty( $request['comment_type'] ) ) { $comment_data = array( @@ -204,9 +204,8 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) wp_update_comment( $comment_data ); } - $author = get_user_by( 'login', $response->data['author_name'] ); - if ( $author ) { - $avatar_url = get_avatar_url( $author->ID ); + if ( $response->data['author'] ) { + $avatar_url = get_avatar_url( $response->data['author'] ); $response->data['author_avatar_urls'] = array( 'default' => $avatar_url, '48' => add_query_arg( 's', 48, $avatar_url ), @@ -214,15 +213,7 @@ function update_comment_meta_from_rest_request( $response, $comment, $request ) ); } - $comment_id = $comment->comment_ID; - $meta_key = 'block_comment'; - $meta_value = get_comment_meta( $comment_id, $meta_key, true ); - - if ( ! empty( $meta_value ) ) { - $response->data['meta'] = $meta_value; - } - return $response; } } -add_filter( 'rest_prepare_comment', 'update_comment_meta_from_rest_request', 10, 3 ); +add_filter( 'rest_prepare_comment', 'update_comment_meta_from_rest_request_6_6', 10, 3 ); diff --git a/lib/compat/wordpress-6.7/rest-api.php b/lib/compat/wordpress-6.7/rest-api.php index c5e2927198da0c..1fa4ac8cb04085 100644 --- a/lib/compat/wordpress-6.7/rest-api.php +++ b/lib/compat/wordpress-6.7/rest-api.php @@ -114,3 +114,46 @@ function gutenberg_override_default_rest_server() { return 'Gutenberg_REST_Server'; } add_filter( 'wp_rest_server_class', 'gutenberg_override_default_rest_server', 1 ); + +/** + * Updates comment metadata from a REST API request. + * + * This function is hooked to the `rest_prepare_comment` filter and is responsible for updating the comment metadata based on the data provided in the REST API request. + * + * It performs the following tasks: + * - Updates the `block_comment` metadata for the comment based on the `meta` field in the request. + * - Updates the `comment_type` and `comment_approved` fields for the comment based on the corresponding fields in the request. + * - Retrieves the author's avatar URLs and adds them to the response data. + * - Retrieves the `block_comment` metadata for the comment and adds it to the response data. + * + * @param WP_REST_Response $response The response object. + * @param WP_Comment $comment The comment object. + * @param WP_REST_Request $request The request object. + * @return WP_REST_Response The updated response object. + */ +if ( ! function_exists( 'update_comment_meta_from_rest_request_6_7' ) ) { + function update_comment_meta_from_rest_request_6_7( $response, $comment, $request ) { + + if ( isset( $request['comment_type'] ) && ! empty( $request['comment_type'] ) ) { + $comment_data = array( + 'comment_ID' => $comment->comment_ID, + 'comment_type' => $request['comment_type'], + 'comment_approved' => isset( $request['comment_approved'] ) ? $request['comment_approved'] : 0, + ); + + wp_update_comment( $comment_data ); + } + + if ( $response->data['author'] ) { + $avatar_url = get_avatar_url( $response->data['author'] ); + $response->data['author_avatar_urls'] = array( + 'default' => $avatar_url, + '48' => add_query_arg( 's', 48, $avatar_url ), + '96' => add_query_arg( 's', 96, $avatar_url ), + ); + } + + return $response; + } +} +add_filter( 'rest_prepare_comment', 'update_comment_meta_from_rest_request_6_7', 10, 3 ); diff --git a/package-lock.json b/package-lock.json index b910ce57d5e44c..30ffaa01be8526 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53655,10 +53655,7 @@ "@wordpress/block-editor": "file:../block-editor", "@wordpress/components": "file:../components", "@wordpress/compose": "file:../compose", - "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", - "@wordpress/date": "file:../date", - "@wordpress/editor": "file:../editor", "@wordpress/element": "file:../element", "@wordpress/html-entities": "file:../html-entities", "@wordpress/i18n": "file:../i18n", @@ -68213,10 +68210,7 @@ "@wordpress/block-editor": "file:../block-editor", "@wordpress/components": "file:../components", "@wordpress/compose": "file:../compose", - "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", - "@wordpress/date": "file:../date", - "@wordpress/editor": "file:../editor", "@wordpress/element": "file:../element", "@wordpress/html-entities": "file:../html-entities", "@wordpress/i18n": "file:../i18n", diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index dfbd497a9b0d4a..8d69c555d85fbc 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -223,7 +223,7 @@ export function Comments( { threads } ) { Date: Fri, 30 Aug 2024 19:28:37 +0530 Subject: [PATCH 071/159] resolve style.scss conflict --- .../src/components/collab-sidebar/style.scss | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index ca91a7ecf5836e..03c6f12701feea 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -116,7 +116,6 @@ &__commentUpdate { margin-left: auto; -<<<<<<< HEAD button { &.is-compact { &.has-icon:not(.has-text) { @@ -126,30 +125,14 @@ height: 24px; flex-shrink: 0; } -======= - .components-button { - &.has-icon:not(.has-text) { - min-width: 24px; - padding: 0; - width: 24px; - height: 24px; - flex-shrink: 0; ->>>>>>> b0ad9d5ae1070b9480a9c8353569d8596dc84764 } } } &__commentDropdown { flex-shrink: 0; -<<<<<<< HEAD button { &.has-icon { -======= - .components-dropdown { - flex-shrink: 0; - - .components-button { ->>>>>>> b0ad9d5ae1070b9480a9c8353569d8596dc84764 min-width: 24px; padding: 0; width: 24px; From c9d50d89964f574285a349d049c8fcccbf134b56 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Mon, 2 Sep 2024 15:28:32 +0530 Subject: [PATCH 072/159] Refactor collab components --- package-lock.json | 14 +- package.json | 2 +- .../components/collab-sidebar/add-comment.js | 372 ++--------- .../src/components/collab-sidebar/comments.js | 617 +++++++----------- .../src/components/collab-sidebar/index.js | 10 +- 5 files changed, 301 insertions(+), 714 deletions(-) diff --git a/package-lock.json b/package-lock.json index 30ffaa01be8526..ab03c05a836094 100644 --- a/package-lock.json +++ b/package-lock.json @@ -243,7 +243,7 @@ "strip-json-comments": "5.0.0", "style-loader": "3.2.1", "terser-webpack-plugin": "5.3.9", - "typescript": "5.5.3", + "typescript": "5.1.5", "uglify-js": "3.13.7", "uuid": "9.0.1", "webdriverio": "8.16.20", @@ -48686,9 +48686,9 @@ } }, "node_modules/typescript": { - "version": "5.5.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", - "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", + "version": "5.1.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.5.tgz", + "integrity": "sha512-FOH+WN/DQjUvN6WgW+c4Ml3yi0PH+a/8q+kNIfRehv1wLhWONedw85iu+vQ39Wp49IzTJEsZ2lyLXpBF7mkF1g==", "dev": true, "license": "Apache-2.0", "bin": { @@ -93104,9 +93104,9 @@ "dev": true }, "typescript": { - "version": "5.5.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", - "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", + "version": "5.1.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.5.tgz", + "integrity": "sha512-FOH+WN/DQjUvN6WgW+c4Ml3yi0PH+a/8q+kNIfRehv1wLhWONedw85iu+vQ39Wp49IzTJEsZ2lyLXpBF7mkF1g==", "dev": true }, "uc.micro": { diff --git a/package.json b/package.json index 0e495308e44e1e..36580b7d024149 100644 --- a/package.json +++ b/package.json @@ -255,7 +255,7 @@ "strip-json-comments": "5.0.0", "style-loader": "3.2.1", "terser-webpack-plugin": "5.3.9", - "typescript": "5.5.3", + "typescript": "5.1.5", "uglify-js": "3.13.7", "uuid": "9.0.1", "webdriverio": "8.16.20", diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 21fc07e25d3d13..9ef9a9609871f1 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -11,15 +11,7 @@ import { __experimentalVStack as VStack, Button, TextControl, - CheckboxControl, } from '@wordpress/components'; -import { dateI18n, format } from '@wordpress/date'; -import { - Icon, - edit as editIcon, - trash as deleteIcon, - commentAuthorAvatar as userIcon, -} from '@wordpress/icons'; /** * Renders the new comment form. @@ -28,42 +20,27 @@ import { * @param {Function} root0.setReloadComments Function to reload comments. */ export function AddComment( { setReloadComments } ) { - const generateNewComment = () => ( { - commentId: Date.now(), - createdBy: currentUser, - comment: inputComment, - createdAt: new Date().toISOString(), - } ); - - // Get the date time format from WordPress settings. - const dateTimeFormat = 'h:i A'; - // State to manage the comment thread. const [ inputComment, setInputComment ] = useState( '' ); - const [ isEditing, setIsEditing ] = useState( null ); - const curruntUserData = useSelect( ( select ) => { + const currentUserData = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals return select( 'core' ).getCurrentUser(); }, [] ); - let userAvatar = - 'https://secure.gravatar.com/avatar/92929292929292929292929292929292?s=48&d=mm&r=g'; - if ( curruntUserData?.avatar_urls ) { - userAvatar = curruntUserData?.avatar_urls[ 48 ]; + const useDefaultAvatar = () => { + const { avatarURL: defaultAvatarUrl } = useSelect( ( select ) => { + const { getSettings } = select( 'core/block-editor' ); + const { __experimentalDiscussionSettings } = getSettings(); + return __experimentalDiscussionSettings; + } ); + return defaultAvatarUrl; } - //const userAvatar = curruntUserData?.avatar_urls[ 48 ] ? curruntUserData?.avatar_urls[ 48 ] : null; + const userAvatar = currentUserData?.avatar_urls[ 48 ] ?? useDefaultAvatar(); - const currentUser = curruntUserData?.name || null; + const currentUser = currentUserData?.name || null; - const allThreads = []; - let threadId = 0; - const currentThread = allThreads[ threadId ] ?? {}; - const isCurrentThreadResolved = currentThread.threadIsResolved || false; - const commentsCount = isCurrentThreadResolved - ? 0 - : currentThread.comments?.length || 0; // eslint-disable-next-line @wordpress/data-no-store-string-literals const postID = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals @@ -107,8 +84,12 @@ export function AddComment( { setReloadComments } ) { // Function to save the comment. const saveComment = () => { - const newComment = generateNewComment(); - threadId = newComment?.commentId; + const newComment = { + commentId: Date.now(), + createdBy: currentUser, + comment: inputComment, + createdAt: new Date().toISOString(), + }; apiFetch( { path: '/wp/v2/comments', @@ -128,47 +109,6 @@ export function AddComment( { setReloadComments } ) { } ); }; - // Function to edit the comment. - const editComment = ( commentId ) => { - const editedComments = { ...allThreads }; - - if ( - editedComments[ threadId ] && - editedComments[ threadId ].comments - ) { - editedComments[ threadId ].comments.map( ( comment ) => { - if ( comment.commentId === commentId ) { - comment.comment = inputComment; - comment.date = new Date().toISOString(); - } - return comment; - } ); - } - - setInputComment( '' ); - setIsEditing( null ); - }; - - // Function to delete a comment. - const deleteComment = ( commentId ) => { - // Filter out the comment to be deleted. - const currentComments = allThreads[ threadId ].comments.filter( - ( comment ) => comment.commentId !== commentId - ); - - const updatedComments = { ...allThreads }; - - // If there are no comments, delete the thread. - if ( currentComments.length === 0 ) { - delete updatedComments[ threadId ]; - } else { - updatedComments[ threadId ] = { - ...allThreads[ threadId ], - comments: currentComments, - }; - } - }; - const handleCancel = () => { updateBlockAttributes( clientId, { showCommentBoard: false, @@ -180,242 +120,52 @@ export function AddComment( { setReloadComments } ) { { showAddCommentBoard && null !== clientId && 0 === blockCommentId && ( - - { 0 < commentsCount && - ! isCurrentThreadResolved && - currentThread.comments.map( - ( - { - createdBy, - comment, - timestamp, - commentId, - }, - index - ) => ( - <> - { isEditing === commentId && ( - - - - - { currentUser } - - - - setInputComment( val ) - } - placeholder={ __( - 'Add comment' - ) } - className="block-editor-format-toolbar__comment-input" - /> - - - - - - + { + confirmEditComment( thread.id, inputComment ); + setCommentEdit( false ); + } } + onCancel={ onCancel } + /> ) } - { thread.content.rendered } + { ! commentEdit && ( + + { thread.content.rendered } + + ) } { hasCommentReply && thread.id === showConfirmationTabId && ( - - - { - setCommentReply( value ); - } } - /> - - - - - - - - - ) } - - { commentConfirmation && - thread.id === showConfirmationTabId && ( - - { __( 'Thread marked as resolved.' ) } - - ) } - { commentEditedMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Thread edited successfully.' ) } - - ) } - { commentDeleteMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Thread deleted successfully.' ) } - - ) } - { replyMessage && - thread.id === showConfirmationTabId && ( - - { __( 'Reply added successfully.' ) } - + { + confirmReplyComment( + thread.id, + inputComment + ); + setHasCommentReply( + false + ); + } } + onCancel={ () => { + setHasCommentReply( false ); + setShowConfirmation( false ); + } } + /> ) } - { showConfirmation && thread.id === showConfirmationTabId && ( { - setCommentConfirmation( false ); setShowConfirmation( true ); setShowConfirmationTabId( reply.id @@ -437,7 +336,8 @@ export function Comments( { threads } ) { setShowConfirmationTabId( reply.id ); - onEditComment( reply.id ); + setHasCommentReply( false ); + setCommentEdit( true ); } } onDelete={ () => { setCommentEdit( false ); @@ -448,6 +348,7 @@ export function Comments( { threads } ) { true ); } } + status={ thread.status } /> - /g, - '' - ) - : reply.content.rendered.replace( - /<\/?p>/g, - '' - ) - } - onChange={ ( - value - ) => { - setNewEditedComment( - value - ); - } } - /> - - - - - - - - ) } - - { reply.content.rendered } - - - - - { hasCommentReply && - reply.id === showConfirmationTabId && ( - - - { - setCommentReply( - value - ); + { + confirmEditComment( reply.id, inputComment ); + setCommentEdit( false ); } } + onCancel={ onCancel } /> - - - - - - - - - ) } - - { commentConfirmation && - reply.id === showConfirmationTabId && ( - - { __( - 'Thread marked as resolved.' - ) } - - ) } - { commentEditedMessage && - reply.id === showConfirmationTabId && ( - - { __( - 'Thread edited successfully.' - ) } - - ) } - { commentDeleteMessage && - reply.id === showConfirmationTabId && ( - - { __( - 'Thread deleted successfully.' - ) } - - ) } - { replyMessage && - reply.id === showConfirmationTabId && ( - - { __( - 'Reply added successfully.' - ) } - - ) } - - { showConfirmation && - reply.id === showConfirmationTabId && ( - - confirmAndMarkThreadAsResolved( - reply.id - ) - } - discardAction={ () => - setShowConfirmation( false ) - } - /> - ) } - + ) + } + { ! commentEdit && ( + + { reply.content.rendered } + + ) } + + { deleteCommentShowConfirmation && reply.id === showConfirmationTabId && ( ) ) } - ) ) } + ) ) + } + + ); +} + +function EditComment( { thread, onUpdate, onCancel } ) { + const [ inputComment, setInputComment ] = useState( thread.content.rendered.replace( + /<[^>]+>/g, + '' + ) ); + + return ( + <> + { + setInputComment( + value + ); + } } + /> + + + + + + ); } +function AddReply( { thread, onSubmit, onCancel } ) { + const [ inputComment, setInputComment ] = useState( '' ); + + return ( + + + { + setInputComment( value ); + } } + /> + + + + + + + + + ); +} + function ConfirmNotice( { cofirmMessage, confirmAction, discardAction } ) { return ( - { thread.status !== 'approved' && ( + { status !== 'approved' && ( { onResolve && ( @@ -785,7 +622,7 @@ function CommentHeader( { thread, onResolve, onEdit, onDelete, onReply } ) { /> ) } - { thread.status === 'approved' && ( + { status === 'approved' && ( - @@ -464,15 +326,11 @@ function EditComment( { thread, onUpdate, onCancel } ) { ); } -function AddReply( { thread, onSubmit, onCancel } ) { +function AddReply( { onSubmit, onCancel } ) { const [ inputComment, setInputComment ] = useState( '' ); return ( - + - + { __( 'Reply' ) } - @@ -557,26 +408,22 @@ function CommentHeader( { 'time_format' ); - const moreActions = []; - - onEdit && - moreActions.push( { + let moreActions = [ + { title: __( 'Edit' ), onClick: onEdit, - } ); - - onDelete && - moreActions.push( { + }, + { title: __( 'Delete' ), onClick: onDelete, - } ); - - 0 === thread.parent && - onReply && - moreActions.push( { + }, + { title: __( 'Reply' ), onClick: onReply, - } ); + }, + ]; + + moreActions = moreActions.filter( ( item ) => item.onClick ); return ( diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 9ba6857a4b44ca..f7a01de1af4277 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -4,11 +4,11 @@ // eslint-disable-next-line no-restricted-imports import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; -import { useSelect } from '@wordpress/data'; +import { useSelect, useDispatch } from '@wordpress/data'; import { useState, useEffect } from '@wordpress/element'; import { comment as commentIcon } from '@wordpress/icons'; - import { addFilter } from '@wordpress/hooks'; +import { store as noticesStore } from '@wordpress/notices'; /** * Internal dependencies @@ -49,14 +49,145 @@ addFilter( * Renders the Collab sidebar. */ export default function CollabSidebar() { + const { createNotice } = useDispatch( noticesStore ); + const [ threads, setThreads ] = useState( () => [] ); - const [ reloadComments, setReloadComments ] = useState( false ); const postId = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals return select( 'core/editor' ).getCurrentPostId(); }, [] ); - useEffect( () => { + const currentUserData = useSelect( ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core' ).getCurrentUser(); + }, [] ); + + // Get the dispatch functions to save the comment and update the block attributes. + // eslint-disable-next-line @wordpress/data-no-store-string-literals + const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); + + const clientId = useSelect( ( select ) => { + // eslint-disable-next-line @wordpress/data-no-store-string-literals + return select( 'core/block-editor' ).getSelectedBlockClientId(); + }, [] ); + + // Function to save the comment. + const addNewComment = ( comment ) => { + apiFetch( { + path: '/wp/v2/comments', + method: 'POST', + data: { + post: postId, + content: comment, + comment_date: new Date().toISOString(), + comment_type: 'block_comment', + comment_author: currentUserData?.name ?? null, + comment_approved: 0, + }, + } ).then( ( response ) => { + updateBlockAttributes( clientId, { + blockCommentId: response?.id, + } ); + fetchComments(); + } ); + }; + + const onCommentResolve = ( commentId ) => { + apiFetch( { + path: '/wp/v2/comments/' + commentId, + method: 'POST', + data: { + status: 'approved', + }, + } ).then( ( response ) => { + if ( 'approved' === response.status ) { + createNotice( 'snackbar', __( 'Thread marked as resolved.' ), { + type: 'snackbar', + isDismissible: true, + } ); + } + fetchComments(); + } ); + }; + + const onEditComment = ( threadID, comment ) => { + if ( threadID && comment.length > 0 ) { + const editedComment = comment.replace( /^

|<\/p>$/g, '' ); + + apiFetch( { + path: '/wp/v2/comments/' + threadID, + method: 'POST', + data: { + content: editedComment, + }, + } ).then( ( response ) => { + if ( 'trash' !== response.status && '' !== response.id ) { + createNotice( + 'snackbar', + __( 'Thread edited successfully.' ), + { + type: 'snackbar', + isDismissible: true, + } + ); + } + fetchComments(); + } ); + } + }; + + const onAddReply = ( threadID, comment ) => { + if ( threadID ) { + apiFetch( { + path: '/wp/v2/comments/', + method: 'POST', + data: { + parent: threadID, + post: postId, + content: comment, + comment_date: new Date().toISOString(), + comment_type: 'block_comment', + comment_author: currentUserData?.name ?? null, + comment_approved: 0, + }, + } ).then( ( response ) => { + if ( 'trash' !== response.status && '' !== response.id ) { + createNotice( + 'snackbar', + __( 'Reply added successfully.' ), + { + type: 'snackbar', + isDismissible: true, + } + ); + } + fetchComments(); + } ); + } + }; + + const onCommentDelete = ( threadID ) => { + if ( threadID ) { + apiFetch( { + path: '/wp/v2/comments/' + threadID, + method: 'DELETE', + } ).then( ( response ) => { + if ( 'trash' === response.status && '' !== response.id ) { + createNotice( + 'snackbar', + __( 'Thread deleted successfully.' ), + { + type: 'snackbar', + isDismissible: true, + } + ); + } + fetchComments(); + } ); + } + }; + + const fetchComments = () => { if ( postId ) { apiFetch( { path: @@ -96,7 +227,11 @@ export default function CollabSidebar() { ); } ); } - }, [ postId, reloadComments ] ); + }; + + useEffect( () => { + fetchComments(); + }, [ postId ] ); // Check if the experimental flag is enabled. if ( ! isBlockCommentExperimentEnabled ) { @@ -114,9 +249,15 @@ export default function CollabSidebar() {

+ -
); From 15c85e04b68214e19dbfe08a9a83cdad50641eee Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Mon, 2 Sep 2024 18:13:41 +0530 Subject: [PATCH 074/159] remove string literals for data stores --- .../components/collab-sidebar/add-comment.js | 47 ++++++++----------- .../src/components/collab-sidebar/comments.js | 8 ++-- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 2df70d72a0e2eb..bbf2c58662f18f 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -10,6 +10,8 @@ import { Button, TextControl, } from '@wordpress/components'; +import { store as blockEditorStore } from '@wordpress/block-editor'; +import { store as coreStore } from '@wordpress/core-data'; /** * Renders the new comment form. @@ -22,13 +24,12 @@ export function AddComment( { onSubmit } ) { const [ inputComment, setInputComment ] = useState( '' ); const currentUserData = useSelect( ( select ) => { - // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core' ).getCurrentUser(); + return select( coreStore ).getCurrentUser(); }, [] ); const useDefaultAvatar = () => { const { avatarURL: defaultAvatarUrl } = useSelect( ( select ) => { - const { getSettings } = select( 'core/block-editor' ); + const { getSettings } = select( blockEditorStore ); const { __experimentalDiscussionSettings } = getSettings(); return __experimentalDiscussionSettings; } ); @@ -40,33 +41,23 @@ export function AddComment( { onSubmit } ) { const currentUser = currentUserData?.name || null; - const clientId = useSelect( ( select ) => { - // eslint-disable-next-line @wordpress/data-no-store-string-literals - const { getSelectedBlockClientId } = select( 'core/block-editor' ); - return getSelectedBlockClientId(); - }, [] ); - - const blockCommentId = useSelect( ( select ) => { - const clientID = - // eslint-disable-next-line @wordpress/data-no-store-string-literals - select( 'core/block-editor' ).getSelectedBlockClientId(); - // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core/block-editor' ).getBlock( clientID )?.attributes - ?.blockCommentId; - }, [] ); - - const showAddCommentBoard = useSelect( ( select ) => { - const clientID = - // eslint-disable-next-line @wordpress/data-no-store-string-literals - select( 'core/block-editor' ).getSelectedBlockClientId(); - // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core/block-editor' ).getBlock( clientID )?.attributes - ?.showCommentBoard; - }, [] ); + const { clientId, blockCommentId, showAddCommentBoard } = useSelect( + ( select ) => { + const selectedBlock = select( blockEditorStore ).getSelectedBlock(); + const selectedBlockClientID = + select( blockEditorStore ).getSelectedBlockClientId(); + return { + clientId: selectedBlockClientID, + blockCommentId: selectedBlock?.attributes?.blockCommentId, + showAddCommentBoard: + selectedBlock?.attributes?.showCommentBoard, + }; + }, + [] + ); // Get the dispatch functions to save the comment and update the block attributes. - // eslint-disable-next-line @wordpress/data-no-store-string-literals - const { updateBlockAttributes } = useDispatch( 'core/block-editor' ); + const { updateBlockAttributes } = useDispatch( blockEditorStore ); const handleCancel = () => { updateBlockAttributes( clientId, { diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index be4abcdc4f3b3c..1ad75ddd6cd503 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -24,6 +24,7 @@ import { Icon, check, published, moreVertical } from '@wordpress/icons'; import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; import { useEntityProp } from '@wordpress/core-data'; +import { store as blockEditorStore } from '@wordpress/block-editor'; /** * Renders the Comments component. @@ -46,12 +47,9 @@ export function Comments( { const [ actionState, setActionState ] = useState( false ); const blockCommentId = useSelect( ( select ) => { - const clientID = - // eslint-disable-next-line @wordpress/data-no-store-string-literals - select( 'core/block-editor' ).getSelectedBlockClientId(); - // eslint-disable-next-line @wordpress/data-no-store-string-literals + const clientID = select( blockEditorStore ).getSelectedBlockClientId(); return ( - select( 'core/block-editor' ).getBlock( clientID )?.attributes + select( blockEditorStore ).getBlock( clientID )?.attributes ?.blockCommentId ?? false ); }, [] ); From c7be82753515f191083688dd490fc508fb88631d Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Mon, 2 Sep 2024 18:16:02 +0530 Subject: [PATCH 075/159] remove package json file changes --- package-lock.json | 81 +++++++++++++++++++++++++++++++++++++++++------ package.json | 3 +- 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index ab03c05a836094..a5841a06e163ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -216,6 +216,7 @@ "npm-run-all": "4.1.5", "patch-package": "8.0.0", "postcss": "8.4.38", + "postcss-import": "16.1.0", "postcss-loader": "6.2.1", "postcss-local-keyframes": "^0.0.2", "prettier": "npm:wp-prettier@3.0.3", @@ -243,7 +244,7 @@ "strip-json-comments": "5.0.0", "style-loader": "3.2.1", "terser-webpack-plugin": "5.3.9", - "typescript": "5.1.5", + "typescript": "5.5.3", "uglify-js": "3.13.7", "uuid": "9.0.1", "webdriverio": "8.16.20", @@ -41328,6 +41329,29 @@ "postcss": "^8.2.15" } }, + "node_modules/postcss-import": { + "version": "16.1.0", + "resolved": "https://registry.npmjs.org/postcss-import/-/postcss-import-16.1.0.tgz", + "integrity": "sha512-7hsAZ4xGXl4MW+OKEWCnF6T5jqBw80/EE9aXg1r2yyn1RsVEU8EtKXbijEODa+rg7iih4bKf7vlvTGYR4CnPNg==", + "dev": true, + "dependencies": { + "postcss-value-parser": "^4.0.0", + "read-cache": "^1.0.0", + "resolve": "^1.1.7" + }, + "engines": { + "node": ">=18.0.0" + }, + "peerDependencies": { + "postcss": "^8.0.0" + } + }, + "node_modules/postcss-import/node_modules/postcss-value-parser": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz", + "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==", + "dev": true + }, "node_modules/postcss-loader": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/postcss-loader/-/postcss-loader-6.2.1.tgz", @@ -43410,6 +43434,15 @@ "node": "^14.17.0 || ^16.13.0 || >=18.0.0" } }, + "node_modules/read-cache": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/read-cache/-/read-cache-1.0.0.tgz", + "integrity": "sha512-Owdv/Ft7IjOgm/i0xvNDZ1LrRANRfew4b2prF3OWMQLxLfu3bS8FVhCsrSCMK4lR56Y9ya+AThoTpDCTxCmpRA==", + "dev": true, + "dependencies": { + "pify": "^2.3.0" + } + }, "node_modules/read-cmd-shim": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/read-cmd-shim/-/read-cmd-shim-4.0.0.tgz", @@ -48686,9 +48719,9 @@ } }, "node_modules/typescript": { - "version": "5.1.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.5.tgz", - "integrity": "sha512-FOH+WN/DQjUvN6WgW+c4Ml3yi0PH+a/8q+kNIfRehv1wLhWONedw85iu+vQ39Wp49IzTJEsZ2lyLXpBF7mkF1g==", + "version": "5.5.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", + "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", "dev": true, "license": "Apache-2.0", "bin": { @@ -52208,11 +52241,11 @@ "@wordpress/a11y": "file:../a11y", "@wordpress/api-fetch": "file:../api-fetch", "@wordpress/blob": "file:../blob", + "@wordpress/block-serialization-default-parser": "file:../block-serialization-default-parser", "@wordpress/blocks": "file:../blocks", "@wordpress/commands": "file:../commands", "@wordpress/components": "file:../components", "@wordpress/compose": "file:../compose", - "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", "@wordpress/date": "file:../date", "@wordpress/deprecated": "file:../deprecated", @@ -54555,6 +54588,7 @@ "npm-package-json-lint": "^6.4.0", "npm-packlist": "^3.0.0", "postcss": "^8.4.5", + "postcss-import": "^16.1.0", "postcss-loader": "^6.2.1", "prettier": "npm:wp-prettier@3.0.3", "puppeteer-core": "^13.2.0", @@ -67215,11 +67249,11 @@ "@wordpress/a11y": "file:../a11y", "@wordpress/api-fetch": "file:../api-fetch", "@wordpress/blob": "file:../blob", + "@wordpress/block-serialization-default-parser": "file:../block-serialization-default-parser", "@wordpress/blocks": "file:../blocks", "@wordpress/commands": "file:../commands", "@wordpress/components": "file:../components", "@wordpress/compose": "file:../compose", - "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", "@wordpress/date": "file:../date", "@wordpress/deprecated": "file:../deprecated", @@ -68733,6 +68767,7 @@ "npm-package-json-lint": "^6.4.0", "npm-packlist": "^3.0.0", "postcss": "^8.4.5", + "postcss-import": "^16.1.0", "postcss-loader": "^6.2.1", "prettier": "npm:wp-prettier@3.0.3", "puppeteer-core": "^13.2.0", @@ -87490,6 +87525,25 @@ "integrity": "sha512-4VELwssYXDFigPYAZ8vL4yX4mUepF/oCBeeIT4OXsJPYOtvJumyz9WflmJWTfDwCUcpDR+z0zvCWBXgTx35SVw==", "dev": true }, + "postcss-import": { + "version": "16.1.0", + "resolved": "https://registry.npmjs.org/postcss-import/-/postcss-import-16.1.0.tgz", + "integrity": "sha512-7hsAZ4xGXl4MW+OKEWCnF6T5jqBw80/EE9aXg1r2yyn1RsVEU8EtKXbijEODa+rg7iih4bKf7vlvTGYR4CnPNg==", + "dev": true, + "requires": { + "postcss-value-parser": "^4.0.0", + "read-cache": "^1.0.0", + "resolve": "^1.1.7" + }, + "dependencies": { + "postcss-value-parser": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz", + "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==", + "dev": true + } + } + }, "postcss-loader": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/postcss-loader/-/postcss-loader-6.2.1.tgz", @@ -89042,6 +89096,15 @@ } } }, + "read-cache": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/read-cache/-/read-cache-1.0.0.tgz", + "integrity": "sha512-Owdv/Ft7IjOgm/i0xvNDZ1LrRANRfew4b2prF3OWMQLxLfu3bS8FVhCsrSCMK4lR56Y9ya+AThoTpDCTxCmpRA==", + "dev": true, + "requires": { + "pify": "^2.3.0" + } + }, "read-cmd-shim": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/read-cmd-shim/-/read-cmd-shim-4.0.0.tgz", @@ -93104,9 +93167,9 @@ "dev": true }, "typescript": { - "version": "5.1.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.5.tgz", - "integrity": "sha512-FOH+WN/DQjUvN6WgW+c4Ml3yi0PH+a/8q+kNIfRehv1wLhWONedw85iu+vQ39Wp49IzTJEsZ2lyLXpBF7mkF1g==", + "version": "5.5.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", + "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", "dev": true }, "uc.micro": { diff --git a/package.json b/package.json index 36580b7d024149..c0a7853a418178 100644 --- a/package.json +++ b/package.json @@ -228,6 +228,7 @@ "npm-run-all": "4.1.5", "patch-package": "8.0.0", "postcss": "8.4.38", + "postcss-import": "16.1.0", "postcss-loader": "6.2.1", "postcss-local-keyframes": "^0.0.2", "prettier": "npm:wp-prettier@3.0.3", @@ -255,7 +256,7 @@ "strip-json-comments": "5.0.0", "style-loader": "3.2.1", "terser-webpack-plugin": "5.3.9", - "typescript": "5.1.5", + "typescript": "5.5.3", "uglify-js": "3.13.7", "uuid": "9.0.1", "webdriverio": "8.16.20", From bd1a7cebb8074c75f916028ac67276b09a080338 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Mon, 2 Sep 2024 18:24:49 +0530 Subject: [PATCH 076/159] Add docs for components --- .../src/components/collab-sidebar/comments.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 1ad75ddd6cd503..19d3b1ab6a1c74 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -286,6 +286,15 @@ export function Comments( { ); } +/** + * EditComment component. + * + * @param {Object} props - The component props. + * @param {Object} props.thread - The comment thread object. + * @param {Function} props.onUpdate - The function to call when updating the comment. + * @param {Function} props.onCancel - The function to call when canceling the comment update. + * @return {JSX.Element} The EditComment component. + */ function EditComment( { thread, onUpdate, onCancel } ) { const [ inputComment, setInputComment ] = useState( thread.content.rendered.replace( /<[^>]+>/g, '' ) @@ -324,6 +333,14 @@ function EditComment( { thread, onUpdate, onCancel } ) { ); } +/** + * Renders a component to add a reply. + * + * @param {Object} props - The component props. + * @param {Function} props.onSubmit - The function to be called when the reply is submitted. + * @param {Function} props.onCancel - The function to be called when the reply is canceled. + * @return {JSX.Element} The JSX element representing the AddReply component. + */ function AddReply( { onSubmit, onCancel } ) { const [ inputComment, setInputComment ] = useState( '' ); @@ -366,6 +383,15 @@ function AddReply( { onSubmit, onCancel } ) { ); } +/** + * Renders a confirmation notice component. + * + * @param {Object} props - The component props. + * @param {string} props.cofirmMessage - The confirmation message to display. Defaults to 'Are you sure?' if not provided. + * @param {Function} props.confirmAction - The action to perform when the confirm button is clicked. + * @param {Function} props.discardAction - The action to perform when the discard button is clicked. + * @return {JSX.Element} The confirmation notice component. + */ function ConfirmNotice( { cofirmMessage, confirmAction, discardAction } ) { return ( Date: Mon, 2 Sep 2024 18:33:48 +0530 Subject: [PATCH 077/159] remove revert function to show comments in sidebar and show comments blocks according --- .../src/components/collab-sidebar/comments.js | 4 ++-- .../src/components/collab-sidebar/index.js | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 0aec583689a533..cf8d97ad4bbb20 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -183,11 +183,11 @@ export function Comments( { threads } ) { { Array.isArray( threads ) && threads.length > 0 && - threads.reverse().map( ( thread ) => ( + threads.map( ( thread ) => ( thread ).reverse(); + const allBlocks = useSelect( ( select ) => { + return select( 'core/block-editor' ).getBlocks(); + }, [] ); + + const filteredBlocks = allBlocks?.filter(block => + block.attributes.blockCommentId !== 0 + ); + + const blockCommentIds = filteredBlocks?.map(block => block.attributes.blockCommentId); + const resultThreads = threads?.slice().reverse(); + const threadIdMap = new Map(resultThreads?.map(thread => [thread.id, thread])); + const sortedThreads = blockCommentIds + .map(id => threadIdMap.get(id)) + .filter(thread => thread !== undefined); return (
- +
); From d6e01f3c31c7cd8173865945fc4a6f7e6f865941 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Mon, 2 Sep 2024 19:11:59 +0530 Subject: [PATCH 078/159] Remove string literels for gutenberg stores --- .../src/components/collab-sidebar/comments.js | 2 +- .../src/components/collab-sidebar/index.js | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index f1462229bcbe23..33e7a645a4248e 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -484,7 +484,7 @@ function CommentHeader( { { status !== 'approved' && ( - { onResolve && ( + { 0 === thread.parent && onResolve && ( - @@ -368,12 +378,22 @@ function AddReply( { onSubmit, onCancel } ) { > - @@ -406,10 +426,15 @@ function ConfirmNotice( { cofirmMessage, confirmAction, discardAction } ) { __next40pxDefaultSize variant="primary" onClick={ confirmAction } + size="compact" > { __( 'Yes' ) } - @@ -444,22 +469,24 @@ function CommentHeader( { 'time_format' ); - let moreActions = [ - { - title: __( 'Edit' ), - onClick: onEdit, - }, - { - title: __( 'Delete' ), - onClick: onDelete, - }, - { - title: __( 'Reply' ), - onClick: onReply, - }, - ]; + const memorizedMoreActions = useMemo( () => { + return [ + { + title: __( 'Edit' ), + onClick: onEdit, + }, + { + title: __( 'Delete' ), + onClick: onDelete, + }, + { + title: __( 'Reply' ), + onClick: onReply, + }, + ]; + }, [] ); - moreActions = moreActions.filter( ( item ) => item.onClick ); + const moreActions = memorizedMoreActions.filter( ( item ) => item.onClick ); return ( @@ -485,17 +512,13 @@ function CommentHeader( { { status !== 'approved' && ( { 0 === thread.parent && onResolve && ( - - - + + ) } diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index 03c6f12701feea..82bca356115cf4 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -117,14 +117,12 @@ margin-left: auto; button { - &.is-compact { - &.has-icon:not(.has-text) { - min-width: 24px; - padding: 0; - width: 24px; - height: 24px; - flex-shrink: 0; - } + &.has-icon:not(.has-text) { + min-width: 24px; + padding: 0; + width: 24px; + height: 24px; + flex-shrink: 0; } } } diff --git a/packages/editor/src/components/collab-sidebar/utils.js b/packages/editor/src/components/collab-sidebar/utils.js new file mode 100644 index 00000000000000..f073cc35bfbbb5 --- /dev/null +++ b/packages/editor/src/components/collab-sidebar/utils.js @@ -0,0 +1,9 @@ +/** + * Sanitizes a comment string by removing non-printable ASCII characters. + * + * @param {string} str - The comment string to sanitize. + * @return {string} - The sanitized comment string. + */ +export function sanitizeCommentString( str ) { + return str.trim().replace( /[^\x20-\x7E]/g, '' ); +} From e2fd75a28c06d0a12ccc4ab4ebfbd9685c1488e5 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 3 Sep 2024 16:21:24 +0530 Subject: [PATCH 081/159] made changes for feedback --- lib/compat/wordpress-6.6/rest-api.php | 43 --------------------------- lib/compat/wordpress-6.7/rest-api.php | 3 +- lib/experimental/collab.php | 43 --------------------------- lib/load.php | 1 - 4 files changed, 1 insertion(+), 89 deletions(-) delete mode 100644 lib/experimental/collab.php diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index bcd404039b2f7d..fee9c71b86c070 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -174,46 +174,3 @@ function gutenberg_block_editor_preload_paths_6_6( $paths, $context ) { return $paths; } add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_6_6', 10, 2 ); - -/** - * Updates comment metadata from a REST API request. - * - * This function is hooked to the `rest_prepare_comment` filter and is responsible for updating the comment metadata based on the data provided in the REST API request. - * - * It performs the following tasks: - * - Updates the `block_comment` metadata for the comment based on the `meta` field in the request. - * - Updates the `comment_type` and `comment_approved` fields for the comment based on the corresponding fields in the request. - * - Retrieves the author's avatar URLs and adds them to the response data. - * - Retrieves the `block_comment` metadata for the comment and adds it to the response data. - * - * @param WP_REST_Response $response The response object. - * @param WP_Comment $comment The comment object. - * @param WP_REST_Request $request The request object. - * @return WP_REST_Response The updated response object. - */ -if ( ! function_exists( 'update_comment_meta_from_rest_request_6_6' ) ) { - function update_comment_meta_from_rest_request_6_6( $response, $comment, $request ) { - - if ( isset( $request['comment_type'] ) && ! empty( $request['comment_type'] ) ) { - $comment_data = array( - 'comment_ID' => $comment->comment_ID, - 'comment_type' => $request['comment_type'], - 'comment_approved' => isset( $request['comment_approved'] ) ? $request['comment_approved'] : 0, - ); - - wp_update_comment( $comment_data ); - } - - if ( $response->data['author'] ) { - $avatar_url = get_avatar_url( $response->data['author'] ); - $response->data['author_avatar_urls'] = array( - 'default' => $avatar_url, - '48' => add_query_arg( 's', 48, $avatar_url ), - '96' => add_query_arg( 's', 96, $avatar_url ), - ); - } - - return $response; - } -} -add_filter( 'rest_prepare_comment', 'update_comment_meta_from_rest_request_6_6', 10, 3 ); diff --git a/lib/compat/wordpress-6.7/rest-api.php b/lib/compat/wordpress-6.7/rest-api.php index 1fa4ac8cb04085..cd9bd800f02773 100644 --- a/lib/compat/wordpress-6.7/rest-api.php +++ b/lib/compat/wordpress-6.7/rest-api.php @@ -116,7 +116,6 @@ function gutenberg_override_default_rest_server() { add_filter( 'wp_rest_server_class', 'gutenberg_override_default_rest_server', 1 ); /** - * Updates comment metadata from a REST API request. * * This function is hooked to the `rest_prepare_comment` filter and is responsible for updating the comment metadata based on the data provided in the REST API request. * @@ -134,7 +133,7 @@ function gutenberg_override_default_rest_server() { if ( ! function_exists( 'update_comment_meta_from_rest_request_6_7' ) ) { function update_comment_meta_from_rest_request_6_7( $response, $comment, $request ) { - if ( isset( $request['comment_type'] ) && ! empty( $request['comment_type'] ) ) { + if ( ! empty( $request['comment_type'] ) ) { $comment_data = array( 'comment_ID' => $comment->comment_ID, 'comment_type' => $request['comment_type'], diff --git a/lib/experimental/collab.php b/lib/experimental/collab.php deleted file mode 100644 index 279915f2a02eb1..00000000000000 --- a/lib/experimental/collab.php +++ /dev/null @@ -1,43 +0,0 @@ - true ) ); - - foreach ( $post_types as $post_type ) { - // Only register the meta field if the post type supports the editor, custom fields, and revisions. - if ( - post_type_supports( $post_type, 'editor' ) && - post_type_supports( $post_type, 'custom-fields' ) && - post_type_supports( $post_type, 'revisions' ) - ) { - register_post_meta( - $post_type, - 'collab', - array( - 'show_in_rest' => true, - 'single' => true, - 'type' => 'string', - 'revisions_enabled' => true, - ) - ); - } - } - } -} - -/* - * Most post types are registered at priority 10, so use priority 20 here in - * order to catch them. -*/ -add_action( 'init', 'register_post_meta_for_collab_comment', 20 ); diff --git a/lib/load.php b/lib/load.php index 012348fc312914..6ac2bd61f1de49 100644 --- a/lib/load.php +++ b/lib/load.php @@ -13,7 +13,6 @@ require_once __DIR__ . '/init.php'; require_once __DIR__ . '/upgrade.php'; -require_once __DIR__ . '/experimental/collab.php'; /** * Checks whether the Gutenberg experiment is enabled. From 6f03c48d20859afcba68cbf645184bb245bd6e21 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Tue, 3 Sep 2024 16:41:58 +0530 Subject: [PATCH 082/159] Refresh state on every new comment --- .../editor/src/components/collab-sidebar/add-comment.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 6134b692597c23..d64f74dff459cb 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -3,7 +3,7 @@ */ import { __ } from '@wordpress/i18n'; import { useSelect, useDispatch } from '@wordpress/data'; -import { useState } from '@wordpress/element'; +import { useState, useEffect } from '@wordpress/element'; import { __experimentalHStack as HStack, __experimentalVStack as VStack, @@ -61,6 +61,10 @@ export function AddComment( { onSubmit } ) { [] ); + useEffect( () => { + setInputComment( '' ); + }, [ clientId ] ); + // Get the dispatch functions to save the comment and update the block attributes. const { updateBlockAttributes } = useDispatch( blockEditorStore ); @@ -68,6 +72,7 @@ export function AddComment( { onSubmit } ) { updateBlockAttributes( clientId, { showCommentBoard: false, } ); + setInputComment( '' ); }; if ( ! showAddCommentBoard || ! clientId || 0 !== blockCommentId ) { From 2b057cc5d5e3d4193a413103b2d690d4d910f027 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Tue, 3 Sep 2024 16:42:15 +0530 Subject: [PATCH 083/159] Sync file with trunk --- .../block-editor/src/components/block-toolbar/index.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/block-editor/src/components/block-toolbar/index.js b/packages/block-editor/src/components/block-toolbar/index.js index ccc5f9b093ef73..fedce23a67abc7 100644 --- a/packages/block-editor/src/components/block-toolbar/index.js +++ b/packages/block-editor/src/components/block-toolbar/index.js @@ -35,7 +35,6 @@ import { useShowHoveredOrFocusedGestures } from './utils'; import { store as blockEditorStore } from '../../store'; import __unstableBlockNameContext from './block-name-context'; import NavigableToolbar from '../navigable-toolbar'; -import Shuffle from './shuffle'; import { useHasBlockToolbar } from './use-has-block-toolbar'; /** @@ -68,7 +67,6 @@ export function PrivateBlockToolbar( { showParentSelector, isUsingBindings, blockCommentID, - canRemove, } = useSelect( ( select ) => { const { getBlockName, @@ -76,14 +74,11 @@ export function PrivateBlockToolbar( { getBlockParents, getSelectedBlockClientIds, isBlockValid, - getBlockRootClientId, getBlockEditingMode, getBlockAttributes, - canRemoveBlock, } = select( blockEditorStore ); const selectedBlockClientIds = getSelectedBlockClientIds(); const selectedBlockClientId = selectedBlockClientIds[ 0 ]; - const blockRootClientId = getBlockRootClientId( selectedBlockClientId ); const parents = getBlockParents( selectedBlockClientId ); const firstParentClientId = parents[ parents.length - 1 ]; const parentBlockName = getBlockName( firstParentClientId ); @@ -114,7 +109,6 @@ export function PrivateBlockToolbar( { isDefaultEditingMode: _isDefaultEditingMode, blockType: selectedBlockClientId && getBlockType( _blockName ), shouldShowVisualToolbar: isValid && isVisual, - rootClientId: blockRootClientId, toolbarKey: `${ selectedBlockClientId }${ firstParentClientId }`, showParentSelector: parentBlockType && @@ -128,7 +122,6 @@ export function PrivateBlockToolbar( { _isDefaultEditingMode, isUsingBindings: _isUsingBindings, blockCommentID: commentID, - canRemove: canRemoveBlock( selectedBlockClientId ), }; }, [] ); @@ -218,9 +211,6 @@ export function PrivateBlockToolbar( {
) } - { ! isMultiToolbar && canRemove && ( - - ) } { shouldShowVisualToolbar && isMultiToolbar && ( ) } From 59caf1c9a125887f2d94303189fee18a729686f3 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 3 Sep 2024 18:30:03 +0530 Subject: [PATCH 084/159] made changes for css property --- .../editor/src/components/collab-sidebar/style.scss | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index 82bca356115cf4..806d573c3a1597 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -1,16 +1,15 @@ .editor-collab-sidebar { &__activities { - padding: 16px; - background-color: #f9f9f9; + padding: $grid-unit-20; } &__thread { position: relative; - padding: 16px; + padding: $grid-unit-20; border-radius: 8px; border: 1px solid $gray-300; background-color: $gray-100; - margin-bottom: 16px; + margin-bottom: $grid-unit-20; .components-base-control__field { margin-bottom: 0; @@ -21,11 +20,6 @@ border-radius: 6px; padding: 8px 15px; } - - &.is-focused { - background: #fff; - box-shadow: 0 0 10px 1px #00000012; - } } &__activethread { From 4ee210d2eb2e123eb28e7a8958d03418144a53fe Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Tue, 3 Sep 2024 18:36:42 +0530 Subject: [PATCH 085/159] code cleanup --- .../block-settings-dropdown.js | 3 +- .../src/components/collab/toolbar.js | 2 +- .../components/collab-sidebar/add-comment.js | 4 +- .../src/components/collab-sidebar/comments.js | 216 ++++++------------ .../src/components/collab-sidebar/index.js | 29 ++- 5 files changed, 95 insertions(+), 159 deletions(-) diff --git a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js index 8fc7e3a3450f44..899744889097c2 100644 --- a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js +++ b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js @@ -90,8 +90,7 @@ export function BlockSettingsDropdown( { _firstParentClientId && getBlockName( _firstParentClientId ); const commentID = - // eslint-disable-next-line @wordpress/data-no-store-string-literals - select( 'core/block-editor' ).getBlock( firstBlockClientId ) + select( blockEditorStore ).getBlock( firstBlockClientId ) ?.attributes?.blockCommentId; return { diff --git a/packages/block-editor/src/components/collab/toolbar.js b/packages/block-editor/src/components/collab/toolbar.js index d38dc455b8051c..7ca4e537458aee 100644 --- a/packages/block-editor/src/components/collab/toolbar.js +++ b/packages/block-editor/src/components/collab/toolbar.js @@ -6,7 +6,7 @@ import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; import { collabComment } from '@wordpress/icons'; import { useSelect, useDispatch } from '@wordpress/data'; -export default function BlockCommentToolbar( {} ) { +export default function BlockCommentToolbar() { // eslint-disable-next-line @wordpress/data-no-store-string-literals const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index d64f74dff459cb..2ebe169e40e483 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -100,7 +100,7 @@ export function AddComment( { onSubmit } ) { __next40pxDefaultSize __nextHasNoMarginBottom value={ inputComment } - onChange={ ( val ) => setInputComment( val ) } + onChange={ setInputComment } placeholder={ __( 'Add comment' ) } className="block-editor-format-toolbar__comment-input" /> @@ -110,7 +110,7 @@ export function AddComment( { onSubmit } ) { className="block-editor-format-toolbar__cancel-button" variant="tertiary" text={ __( 'Cancel' ) } - onClick={ () => handleCancel() } + onClick={ handleCancel } size="compact" /> @@ -365,9 +293,7 @@ function AddReply( { onSubmit, onCancel } ) { __nextHasNoMarginBottom className="editor-collab-sidebar__replyComment__textarea" value={ inputComment ?? '' } - onChange={ ( value ) => { - setInputComment( value ); - } } + onChange={ setInputComment } /> - { __( 'Reply' ) } + { _x( 'Reply', 'Add reply comment' ) } @@ -513,7 +439,10 @@ function CommentHeader( { { 0 === thread.parent && onResolve && ( @@ -398,15 +410,15 @@ function CommentHeader( { const memorizedMoreActions = useMemo( () => { return [ { - title: __( 'Edit' ), + title: _x( 'Edit', 'Edit comment' ), onClick: onEdit, }, { - title: __( 'Delete' ), + title: _x( 'Delete', 'Delete comment' ), onClick: onDelete, }, { - title: __( 'Reply' ), + title: _x( 'Reply', 'Reply on a comment' ), onClick: onReply, }, ]; @@ -419,6 +431,7 @@ function CommentHeader( { ) } { status === 'approved' && ( + // translators: tooltip for resolved comment diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 39d94adf68c3ba..54e84aac27836d 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -101,6 +101,7 @@ export default function CollabSidebar() { }, } ).then( ( response ) => { if ( response ) { + // translators: Comment resolved successfully createNotice( 'snackbar', __( 'Thread marked as resolved.' ), { type: 'snackbar', isDismissible: true, @@ -124,6 +125,7 @@ export default function CollabSidebar() { if ( response ) { createNotice( 'snackbar', + // translators: Comment edited successfully __( 'Thread edited successfully.' ), { type: 'snackbar', @@ -154,6 +156,7 @@ export default function CollabSidebar() { if ( response ) { createNotice( 'snackbar', + // translators: Reply added successfully __( 'Reply added successfully.' ), { type: 'snackbar', @@ -179,6 +182,7 @@ export default function CollabSidebar() { } ); createNotice( 'snackbar', + // translators: Comment deleted successfully __( 'Thread deleted successfully.' ), { type: 'snackbar', @@ -265,6 +269,7 @@ export default function CollabSidebar() { return ( From 11cf1f1c4c9fcb68b599384edee154346682fade Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Wed, 4 Sep 2024 17:39:37 +0530 Subject: [PATCH 088/159] use entity record function instead api fetch --- .../src/components/collab-sidebar/comments.js | 30 +-- .../src/components/collab-sidebar/index.js | 192 +++++++++--------- 2 files changed, 109 insertions(+), 113 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index ded978a89b90dc..bb450c40037d06 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -65,21 +65,27 @@ export function Comments( { <> { + onResolve={ () => setActionState( { action: 'resolve', id: parentThread?.id ?? thread.id, - } ); - } } - onEdit={ () => { - setActionState( { action: 'edit', id: thread.id } ); - } } - onDelete={ () => { - setActionState( { action: 'delete', id: thread.id } ); - } } - onReply={ () => { - setActionState( { action: 'reply', id: thread.id } ); - } } + } ) + } + onEdit={ () => + setActionState( { action: 'edit', id: thread.id } ) + } + onDelete={ () => + setActionState( { action: 'delete', id: thread.id } ) + } + onReply={ + ! parentThread + ? () => + setActionState( { + action: 'reply', + id: thread.id, + } ) + : undefined + } status={ parentThread?.status ?? thread.status } /> [] ); const postId = useSelect( ( select ) => { @@ -72,125 +78,109 @@ export default function CollabSidebar() { }, [] ); // Function to save the comment. - const addNewComment = ( comment ) => { - apiFetch( { - path: '/wp/v2/comments', - method: 'POST', - data: { - post: postId, - content: comment, - comment_date: new Date().toISOString(), - comment_type: 'block_comment', - comment_author: currentUserData?.name ?? null, - comment_approved: 0, - }, - } ).then( ( response ) => { + const addNewComment = async ( comment ) => { + const savedRecord = await saveEntityRecord( 'root', 'comment', { + post: postId, + content: comment, + comment_date: new Date().toISOString(), + comment_type: 'block_comment', + comment_author: currentUserData?.name ?? null, + comment_approved: 0, + } ); + + if ( savedRecord ) { updateBlockAttributes( clientId, { - blockCommentId: response?.id, + blockCommentId: savedRecord?.id, + } ); + createNotice( 'snackbar', __( 'New comment added.' ), { + type: 'snackbar', + isDismissible: true, } ); fetchComments(); - } ); + } }; - const onCommentResolve = ( commentId ) => { - apiFetch( { - path: '/wp/v2/comments/' + commentId, - method: 'POST', - data: { - status: 'approved', - }, - } ).then( ( response ) => { - if ( response ) { - createNotice( 'snackbar', __( 'Thread marked as resolved.' ), { - type: 'snackbar', - isDismissible: true, - } ); - } - fetchComments(); + const onCommentResolve = async ( commentId ) => { + editEntityRecord( 'root', 'comment', commentId, { + status: 'approved', } ); - }; - const onEditComment = ( threadID, comment ) => { - if ( threadID && comment.length > 0 ) { - const editedComment = removep( comment ); + const savedRecord = await saveEditedEntityRecord( + 'root', + 'comment', + commentId + ); - apiFetch( { - path: '/wp/v2/comments/' + threadID, - method: 'POST', - data: { - content: editedComment, - }, - } ).then( ( response ) => { - if ( response ) { - createNotice( - 'snackbar', - __( 'Thread edited successfully.' ), - { - type: 'snackbar', - isDismissible: true, - } - ); - } - fetchComments(); + if ( savedRecord ) { + createNotice( 'snackbar', __( 'Thread marked as resolved.' ), { + type: 'snackbar', + isDismissible: true, } ); + + fetchComments(); } }; - const onAddReply = ( threadID, comment ) => { - if ( threadID ) { - apiFetch( { - path: '/wp/v2/comments/', - method: 'POST', - data: { - parent: threadID, - post: postId, - content: comment, - comment_date: new Date().toISOString(), - comment_type: 'block_comment', - comment_author: currentUserData?.name ?? null, - comment_approved: 0, - }, - } ).then( ( response ) => { - if ( response ) { - createNotice( - 'snackbar', - __( 'Reply added successfully.' ), - { - type: 'snackbar', - isDismissible: true, - } - ); - } - fetchComments(); + const onEditComment = async ( commentId, comment ) => { + const editedComment = removep( comment ); + + editEntityRecord( 'root', 'comment', commentId, { + content: editedComment, + } ); + + const savedRecord = await saveEditedEntityRecord( + 'root', + 'comment', + commentId + ); + + if ( savedRecord ) { + createNotice( 'snackbar', __( 'Thread edited successfully.' ), { + type: 'snackbar', + isDismissible: true, } ); + + fetchComments(); } }; - const onCommentDelete = ( threadID ) => { - if ( threadID ) { - apiFetch( { - path: '/wp/v2/comments/' + threadID, - method: 'DELETE', - } ).then( ( response ) => { - if ( response ) { - updateBlockAttributes( clientId, { - blockCommentId: undefined, - showCommentBoard: undefined, - } ); - createNotice( - 'snackbar', - __( 'Thread deleted successfully.' ), - { - type: 'snackbar', - isDismissible: true, - } - ); - } - fetchComments(); + const onAddReply = async ( parentCommentId, comment ) => { + const sanitisedComment = removep( comment ); + + const savedRecord = await saveEntityRecord( 'root', 'comment', { + parent: parentCommentId, + post: postId, + content: sanitisedComment, + comment_date: new Date().toISOString(), + comment_type: 'block_comment', + comment_author: currentUserData?.name ?? null, + comment_approved: 0, + } ); + + if ( savedRecord ) { + createNotice( 'snackbar', __( 'Reply added successfully.' ), { + type: 'snackbar', + isDismissible: true, } ); + fetchComments(); } }; + const onCommentDelete = async ( commentId ) => { + await deleteEntityRecord( 'root', 'comment', commentId ); + + updateBlockAttributes( clientId, { + blockCommentId: undefined, + showCommentBoard: undefined, + } ); + + createNotice( 'snackbar', __( 'Thread deleted successfully.' ), { + type: 'snackbar', + isDismissible: true, + } ); + fetchComments(); + }; + const fetchComments = () => { if ( postId ) { apiFetch( { From 33cd398a5301728067465b927d4d7e23afc904f1 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 4 Sep 2024 18:27:09 +0530 Subject: [PATCH 089/159] small fix for translator string --- .../block-editor/src/components/block-settings-menu/index.js | 2 +- packages/block-editor/src/components/collab/toolbar.js | 2 +- packages/editor/src/components/collab-sidebar/add-comment.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/components/block-settings-menu/index.js b/packages/block-editor/src/components/block-settings-menu/index.js index 65b0ea238f5e60..83fe44aedcdcca 100644 --- a/packages/block-editor/src/components/block-settings-menu/index.js +++ b/packages/block-editor/src/components/block-settings-menu/index.js @@ -25,7 +25,7 @@ export function BlockSettingsMenu( { clientIds, ...props } ) { { commentID && ( ) } diff --git a/packages/block-editor/src/components/collab/toolbar.js b/packages/block-editor/src/components/collab/toolbar.js index e818f04470c857..b38ab171601eb7 100644 --- a/packages/block-editor/src/components/collab/toolbar.js +++ b/packages/block-editor/src/components/collab/toolbar.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { __, _x } from '@wordpress/i18n'; -import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; +import { ToolbarButton } from '@wordpress/components'; import { collabComment } from '@wordpress/icons'; import { useSelect, useDispatch } from '@wordpress/data'; diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 6063f9bd0b390d..a0908c288662f8 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { __ } from '@wordpress/i18n'; +import { __, _x } from '@wordpress/i18n'; import { useSelect, useDispatch } from '@wordpress/data'; import { useState, useEffect } from '@wordpress/element'; import { From a4abbec2612e419a70733b2e9a7ee6c7ee5cf172 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Wed, 4 Sep 2024 19:14:48 +0530 Subject: [PATCH 090/159] Fix linting errors --- .../components/block-settings-menu/index.js | 9 +- .../src/components/block-toolbar/index.js | 1 - .../collab/block-comment-menu-item.js | 5 +- .../src/components/collab/toolbar.js | 7 +- .../src/components/collab-sidebar/comments.js | 55 ++++++------ .../src/components/collab-sidebar/index.js | 90 +++++++++---------- 6 files changed, 86 insertions(+), 81 deletions(-) diff --git a/packages/block-editor/src/components/block-settings-menu/index.js b/packages/block-editor/src/components/block-settings-menu/index.js index 65b0ea238f5e60..52a8ba442dc0c5 100644 --- a/packages/block-editor/src/components/block-settings-menu/index.js +++ b/packages/block-editor/src/components/block-settings-menu/index.js @@ -10,18 +10,17 @@ import BlockSettingsDropdown from './block-settings-dropdown'; import BlockCommentToolbar from '../collab/toolbar'; export function BlockSettingsMenu( { clientIds, ...props } ) { - const selectedBlockClientId = clientIds[ 0 ]; const commentID = useSelect( ( select ) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core/block-editor' ).getBlock( selectedBlockClientId ) - ?.attributes?.blockCommentId || null; + return ( + select( 'core/block-editor' ).getBlock( selectedBlockClientId ) + ?.attributes?.blockCommentId || null + ); }, [] ); - return ( - { commentID && ( ) } - ) } diff --git a/packages/block-editor/src/components/collab/block-comment-menu-item.js b/packages/block-editor/src/components/collab/block-comment-menu-item.js index 870d8bbd0b3ac8..24d93cfc9edfc7 100644 --- a/packages/block-editor/src/components/collab/block-comment-menu-item.js +++ b/packages/block-editor/src/components/collab/block-comment-menu-item.js @@ -33,7 +33,10 @@ export default function BlockCommentMenuItem( { onClose } ) { onClick={ openCollabBoard } aria-haspopup="dialog" > - { _x( 'Add Comment', 'Click to open comment sidebar and add comment' ) } + { _x( + 'Add Comment', + 'Click to open comment sidebar and add comment' + ) } ); } diff --git a/packages/block-editor/src/components/collab/toolbar.js b/packages/block-editor/src/components/collab/toolbar.js index e818f04470c857..4f1d360c40acb5 100644 --- a/packages/block-editor/src/components/collab/toolbar.js +++ b/packages/block-editor/src/components/collab/toolbar.js @@ -27,13 +27,14 @@ export default function BlockCommentToolbar() { }; return ( - - ); } diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 892f1a179a3c86..6ff44a31524b91 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -130,9 +130,9 @@ export function Comments( { justify="flex-start" spacing="3" > - { + { // translators: message displayed when there are no comments available - __( 'No comments available' ) + __( 'No comments available' ) } ) @@ -143,10 +143,14 @@ export function Comments( { threads.map( ( thread ) => ( @@ -164,9 +168,11 @@ export function Comments( { { 'resolve' === actionState?.action && thread.id === actionState?.id && ( { onCommentResolve( thread.id ); @@ -180,9 +186,11 @@ export function Comments( { { 'delete' === actionState?.action && thread.id === actionState?.id && ( { onCommentDelete( thread.id ); @@ -208,9 +216,11 @@ export function Comments( { { 'delete' === actionState?.action && reply.id === actionState?.id && ( { onCommentDelete( reply.id ); @@ -251,11 +261,7 @@ function EditComment( { thread, onUpdate, onCancel } ) { value={ inputComment ?? '' } onChange={ setInputComment } /> - + @@ -317,14 +337,24 @@ function AddReply( { onSubmit, onCancel } ) { sanitizeCommentString( inputComment ).length } > - { _x( 'Reply', 'Add reply comment' ) } + { + _x( + 'Reply', + 'Add reply comment' + ) + } @@ -353,7 +383,9 @@ function ConfirmNotice( { cofirmMessage, confirmAction, discardAction } ) { >

{ cofirmMessage ?? // translators: message displayed when confirming an action - __( 'Are you sure?' ) + __( + 'Are you sure?' + ) }

@@ -406,18 +448,27 @@ function CommentHeader( { const memorizedMoreActions = useMemo( () => { return [ { - title: _x( 'Edit', 'Edit comment' ), - onClick: onEdit, + title: _x( + 'Edit', + 'Edit comment' + ), + onClick: onEdit, }, { - title: _x( 'Delete', 'Delete comment' ), - onClick: onDelete, + title: _x( + 'Delete', + 'Delete comment' + ), + onClick: onDelete, }, { - title: _x( 'Reply', 'Reply on a comment' ), - onClick: onReply, + title: _x( + 'Reply', + 'Reply on a comment' + ), + onClick: onReply, }, - ]; + ]; }, [] ); const moreActions = memorizedMoreActions.filter( ( item ) => item.onClick ); @@ -471,7 +522,9 @@ function CommentHeader( { ) } { status === 'approved' && ( // translators: tooltip for resolved comment - + ) } diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 5429e7a8524d02..05a0236b01d904 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -126,7 +126,9 @@ export default function CollabSidebar() { createNotice( 'snackbar', // translators: Comment edited successfully - __( 'Thread edited successfully.' ), + __( + 'Thread edited successfully.' + ), { type: 'snackbar', isDismissible: true, @@ -157,7 +159,9 @@ export default function CollabSidebar() { createNotice( 'snackbar', // translators: Reply added successfully - __( 'Reply added successfully.' ), + __( + 'Reply added successfully.' + ), { type: 'snackbar', isDismissible: true, @@ -183,7 +187,9 @@ export default function CollabSidebar() { createNotice( 'snackbar', // translators: Comment deleted successfully - __( 'Thread deleted successfully.' ), + __( + 'Thread deleted successfully.' + ), { type: 'snackbar', isDismissible: true, @@ -241,10 +247,10 @@ export default function CollabSidebar() { fetchComments(); }, [ postId ] ); - const allBlocks = useSelect( ( select ) => { + const allBlocks = useSelect((select) => { // eslint-disable-next-line @wordpress/data-no-store-string-literals - return select( 'core/block-editor' ).getBlocks(); - }, [] ); + return select('core/block-editor').getBlocks(); + }, []); const filteredBlocks = allBlocks?.filter( ( block ) => block.attributes.blockCommentId !== 0 From 71f7a39cf37f790aea5ac07d95901ba9a575e918 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 5 Sep 2024 14:42:31 +0530 Subject: [PATCH 094/159] Fix linting errors --- packages/block-editor/package.json | 1 - .../components/block-settings-menu/index.js | 11 +- .../collab/block-comment-menu-item.js | 6 +- .../src/components/collab/toolbar.js | 4 +- .../src/components/collab-sidebar/comments.js | 102 ++++++------------ .../src/components/collab-sidebar/index.js | 33 ++---- 6 files changed, 49 insertions(+), 108 deletions(-) diff --git a/packages/block-editor/package.json b/packages/block-editor/package.json index 8d6300da3c582c..d3064c78dbe658 100644 --- a/packages/block-editor/package.json +++ b/packages/block-editor/package.json @@ -44,7 +44,6 @@ "@wordpress/commands": "file:../commands", "@wordpress/components": "file:../components", "@wordpress/compose": "file:../compose", - "@wordpress/core-data": "file:../core-data", "@wordpress/data": "file:../data", "@wordpress/date": "file:../date", "@wordpress/deprecated": "file:../deprecated", diff --git a/packages/block-editor/src/components/block-settings-menu/index.js b/packages/block-editor/src/components/block-settings-menu/index.js index a22dfbb371a904..407c73354f1b36 100644 --- a/packages/block-editor/src/components/block-settings-menu/index.js +++ b/packages/block-editor/src/components/block-settings-menu/index.js @@ -11,14 +11,13 @@ import BlockCommentToolbar from '../collab/toolbar'; export function BlockSettingsMenu( { clientIds, ...props } ) { const selectedBlockClientId = clientIds[ 0 ]; - const commentID = useSelect((select) => { + const commentID = useSelect( ( select ) => { return ( - // eslint-disable-next-line @wordpress/data-no-store-string-literals - select('core/block-editor') - .getBlock(selectedBlockClientId) - ?.attributes?.blockCommentId || null + // eslint-disable-next-line @wordpress/data-no-store-string-literals + select( 'core/block-editor' ).getBlock( selectedBlockClientId ) + ?.attributes?.blockCommentId || null ); - },); + } ); return ( diff --git a/packages/block-editor/src/components/collab/block-comment-menu-item.js b/packages/block-editor/src/components/collab/block-comment-menu-item.js index 6c080742eb6b7d..4c3a307c4afa40 100644 --- a/packages/block-editor/src/components/collab/block-comment-menu-item.js +++ b/packages/block-editor/src/components/collab/block-comment-menu-item.js @@ -34,9 +34,9 @@ export default function BlockCommentMenuItem( { onClose } ) { aria-haspopup="dialog" > { _x( - 'Add Comment', - 'Click to open comment sidebar and add comment' - ) } + 'Add Comment', + 'Click to open comment sidebar and add comment' + ) } ); } diff --git a/packages/block-editor/src/components/collab/toolbar.js b/packages/block-editor/src/components/collab/toolbar.js index 286711d27095ac..5433393ce48cef 100644 --- a/packages/block-editor/src/components/collab/toolbar.js +++ b/packages/block-editor/src/components/collab/toolbar.js @@ -31,9 +31,9 @@ export default function BlockCommentToolbar() { accessibleWhenDisabled icon={ collabComment } label={ _x( - 'Comment', + 'Comment', 'Click to open sidebar and highlight comment board' - ) } + ) } onClick={ openCollabBoard } /> ); diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 4a7afe3e0add17..6ff44a31524b91 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -52,14 +52,13 @@ export function Comments( { } ) { const [ actionState, setActionState ] = useState( false ); - const blockCommentId = useSelect((select) => { - const clientID = select(blockEditorStore).getSelectedBlockClientId(); + const blockCommentId = useSelect( ( select ) => { + const clientID = select( blockEditorStore ).getSelectedBlockClientId(); return ( - select(blockEditorStore) - .getBlock(clientID) - ?.attributes?.blockCommentId ?? false + select( blockEditorStore ).getBlock( clientID )?.attributes + ?.blockCommentId ?? false ); - }, []); + }, [] ); const CommentBoard = ( { thread, parentThread } ) => { return ( @@ -133,9 +132,7 @@ export function Comments( { > { // translators: message displayed when there are no comments available - __( - 'No comments available' - ) + __( 'No comments available' ) } ) @@ -146,13 +143,14 @@ export function Comments( { threads.map( ( thread ) => ( @@ -192,7 +190,7 @@ export function Comments( { // translators: message displayed when deleting a comment __( 'Are you sure you want to delete this thread?' - ) + ) } confirmAction={ () => { onCommentDelete( thread.id ); @@ -275,22 +273,14 @@ function EditComment( { thread, onUpdate, onCancel } ) { 0 === sanitizeCommentString( inputComment ).length } > - { - _x( - 'Update', - 'Update comment' - ) - } + { _x( 'Update', 'Update comment' ) } @@ -335,24 +325,14 @@ function AddReply( { onSubmit, onCancel } ) { sanitizeCommentString( inputComment ).length } > - { - _x( - 'Reply', - 'Add reply comment' - ) - } + { _x( 'Reply', 'Add reply comment' ) } @@ -379,12 +359,11 @@ function ConfirmNotice( { cofirmMessage, confirmAction, discardAction } ) { spacing="0" justify="space-between" > -

{ cofirmMessage ?? - // translators: message displayed when confirming an action - __( - 'Are you sure?' - ) - }

+

+ { cofirmMessage ?? + // translators: message displayed when confirming an action + __( 'Are you sure?' ) } +

@@ -446,27 +415,18 @@ function CommentHeader( { const memorizedMoreActions = useMemo( () => { return [ { - title: _x( - 'Edit', - 'Edit comment' - ), - onClick: onEdit, + title: _x( 'Edit', 'Edit comment' ), + onClick: onEdit, }, { - title: _x( - 'Delete', - 'Delete comment' - ), - onClick: onDelete, + title: _x( 'Delete', 'Delete comment' ), + onClick: onDelete, }, { - title: _x( - 'Reply', - 'Reply on a comment' - ), - onClick: onReply, + title: _x( 'Reply', 'Reply on a comment' ), + onClick: onReply, }, - ]; + ]; }, [] ); const moreActions = memorizedMoreActions.filter( ( item ) => item.onClick ); @@ -520,9 +480,7 @@ function CommentHeader( { ) } { status === 'approved' && ( // translators: tooltip for resolved comment - + ) } diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 166c1beeed5548..8fe6c4187f61f4 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -54,10 +54,7 @@ addFilter( */ export default function CollabSidebar() { const { createNotice } = useDispatch( noticesStore ); - const { - saveEntityRecord, - deleteEntityRecord, - } = useDispatch( coreStore ); + const { saveEntityRecord, deleteEntityRecord } = useDispatch( coreStore ); const [ threads, setThreads ] = useState( () => [] ); const postId = useSelect( ( select ) => { @@ -90,16 +87,10 @@ export default function CollabSidebar() { updateBlockAttributes( clientId, { blockCommentId: savedRecord?.id, } ); - createNotice( - 'snackbar', - __( - 'New comment added.' - ), - { - type: 'snackbar', - isDismissible: true, - } - ); + createNotice( 'snackbar', __( 'New comment added.' ), { + type: 'snackbar', + isDismissible: true, + } ); fetchComments(); } }; @@ -112,16 +103,10 @@ export default function CollabSidebar() { if ( savedRecord ) { // translators: Comment resolved successfully - createNotice( - 'snackbar', - __( - 'Thread marked as resolved.' - ), - { - type: 'snackbar', - isDismissible: true, - } - ); + createNotice( 'snackbar', __( 'Thread marked as resolved.' ), { + type: 'snackbar', + isDismissible: true, + } ); fetchComments(); } From edab788ad5d150ebacf2f49077d47f6238a0b12d Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 5 Sep 2024 15:10:39 +0530 Subject: [PATCH 095/159] Fix style linting issue --- packages/editor/src/components/collab-sidebar/comments.js | 2 +- packages/editor/src/components/collab-sidebar/index.js | 2 +- packages/editor/src/components/collab-sidebar/style.scss | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 6ff44a31524b91..baf9d795a01467 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -427,7 +427,7 @@ function CommentHeader( { onClick: onReply, }, ]; - }, [] ); + }, [ onEdit, onDelete, onReply ] ); const moreActions = memorizedMoreActions.filter( ( item ) => item.onClick ); diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 8fe6c4187f61f4..9af94b57498b99 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -199,7 +199,7 @@ export default function CollabSidebar() { useEffect( () => { fetchComments(); - }, [ postId ] ); + }, [ postId, fetchComments ] ); const allBlocks = useSelect( ( select ) => { return select( blockEditorStore ).getBlocks(); diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index 3c6351da549b8f..491710606db532 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -116,7 +116,7 @@ } } } - + &__comment-dropdown-menu { flex-shrink: 0; From 6e528a130668d13c72d1df98ad59f06459b7ec85 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Thu, 5 Sep 2024 15:36:41 +0530 Subject: [PATCH 096/159] Fix useMemo dependacy --- .../src/components/collab/block-comment-menu-item.js | 4 ++-- .../editor/src/components/collab-sidebar/add-comment.js | 4 ++-- packages/editor/src/components/collab-sidebar/index.js | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/block-editor/src/components/collab/block-comment-menu-item.js b/packages/block-editor/src/components/collab/block-comment-menu-item.js index 4c3a307c4afa40..f0b01f60d7c75d 100644 --- a/packages/block-editor/src/components/collab/block-comment-menu-item.js +++ b/packages/block-editor/src/components/collab/block-comment-menu-item.js @@ -34,8 +34,8 @@ export default function BlockCommentMenuItem( { onClose } ) { aria-haspopup="dialog" > { _x( - 'Add Comment', - 'Click to open comment sidebar and add comment' + 'Comment', + 'Click to open new comment form in comment sidebar' ) } ); diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index a0908c288662f8..ef124f44244b87 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -103,7 +103,7 @@ export function AddComment( { onSubmit } ) { value={ inputComment } onChange={ setInputComment } // translators: placeholder text for comment input - placeholder={ __( 'Add comment' ) } + placeholder={ __( 'Comment' ) } /> diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index b4984b4f271e16..275027941d7818 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -1,10 +1,8 @@ /** * WordPress dependencies */ -// eslint-disable-next-line no-restricted-imports -import apiFetch from '@wordpress/api-fetch'; import { __ } from '@wordpress/i18n'; -import { useSelect, useDispatch } from '@wordpress/data'; +import { useSelect, useDispatch, resolveSelect } from '@wordpress/data'; import { useState, useEffect, useMemo, useCallback } from '@wordpress/element'; import { comment as commentIcon } from '@wordpress/icons'; import { addFilter } from '@wordpress/hooks'; @@ -55,33 +53,29 @@ addFilter( export default function CollabSidebar() { const { createNotice } = useDispatch( noticesStore ); const { saveEntityRecord, deleteEntityRecord } = useDispatch( coreStore ); + const { getEntityRecords } = resolveSelect( coreStore ); const [ threads, setThreads ] = useState( () => [] ); - const postId = useSelect( ( select ) => { - return select( editorStore ).getCurrentPostId(); - }, [] ); - - const currentUserData = useSelect( ( select ) => { - return select( coreStore ).getCurrentUser(); + const { postId, clientId } = useSelect( ( select ) => { + return { + postId: select( editorStore ).getCurrentPostId(), + clientId: select( blockEditorStore ).getSelectedBlockClientId(), + }; }, [] ); // Get the dispatch functions to save the comment and update the block attributes. const { updateBlockAttributes } = useDispatch( blockEditorStore ); - const clientId = useSelect( ( select ) => { - return select( blockEditorStore ).getSelectedBlockClientId(); - }, [] ); - // Function to save the comment. const addNewComment = async ( comment ) => { + const sanitisedComment = removep( comment ); + const savedRecord = await saveEntityRecord( 'root', 'comment', { post: postId, - content: comment, - comment_date: new Date().toISOString(), + content: sanitisedComment, comment_type: 'block_comment', - comment_author: currentUserData?.name ?? null, - comment_approved: 0, } ); + if ( savedRecord ) { updateBlockAttributes( clientId, { blockCommentId: savedRecord?.id, @@ -174,10 +168,7 @@ export default function CollabSidebar() { parent: parentCommentId, post: postId, content: sanitisedComment, - comment_date: new Date().toISOString(), comment_type: 'block_comment', - comment_author: currentUserData?.name ?? null, - comment_approved: 0, } ); if ( savedRecord ) { @@ -225,20 +216,20 @@ export default function CollabSidebar() { fetchComments(); }; - const fetchComments = useCallback( () => { + const fetchComments = useCallback( async () => { if ( postId ) { - apiFetch( { - path: - '/wp/v2/comments?post=' + - postId + - '&type=block_comment' + - '&status=any&per_page=100', - method: 'GET', - } ).then( ( data ) => { - setThreads( Array.isArray( data ) ? data : [] ); + const data = await getEntityRecords( 'root', 'comment', { + post: postId, + type: 'block_comment', + status: 'any', + per_page: 100, } ); + + if ( data ) { + setThreads( Array.isArray( data ) ? data : [] ); + } } - }, [ postId ] ); + }, [ postId, getEntityRecords ] ); useEffect( () => { fetchComments(); From d7bca6e12c9bda48dbff56fc709a64d5e857376d Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Fri, 6 Sep 2024 12:40:38 +0530 Subject: [PATCH 103/159] cleanup code --- .../collab/block-comment-menu-item.js | 1 + .../src/components/collab-sidebar/comments.js | 187 ++++++------------ .../src/components/collab-sidebar/index.js | 101 ++++------ 3 files changed, 97 insertions(+), 192 deletions(-) diff --git a/packages/block-editor/src/components/collab/block-comment-menu-item.js b/packages/block-editor/src/components/collab/block-comment-menu-item.js index 68645c2c8f7224..0cbae405ba5c7f 100644 --- a/packages/block-editor/src/components/collab/block-comment-menu-item.js +++ b/packages/block-editor/src/components/collab/block-comment-menu-item.js @@ -12,6 +12,7 @@ import { useSelect, useDispatch } from '@wordpress/data'; import { store as blockEditorStore } from '../../store'; export default function BlockCommentMenuItem( { onClose } ) { + // eslint-disable-next-line @wordpress/data-no-store-string-literals const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); const { updateBlockAttributes } = useDispatch( blockEditorStore ); diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 8939f4a1a70ba6..70689b1ecd18e4 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -100,13 +100,13 @@ export function Comments( { > { 'edit' === actionState?.action && thread.id === actionState?.id && ( - { + { onEditComment( thread.id, value ); setActionState( false ); } } onCancel={ () => setActionState( false ) } + thread={ thread } /> ) } { ( ! actionState || @@ -115,6 +115,38 @@ export function Comments( { ) } + { 'resolve' === actionState?.action && + thread.id === actionState?.id && ( + { + onCommentResolve( thread.id ); + setActionState( false ); + } } + discardAction={ () => setActionState( false ) } + /> + ) } + { 'delete' === actionState?.action && + thread.id === actionState?.id && ( + { + onCommentDelete( thread.id ); + setActionState( false ); + } } + discardAction={ () => setActionState( false ) } + /> + ) } ); }; @@ -157,49 +189,25 @@ export function Comments( { { 'reply' === actionState?.action && thread.id === actionState?.id && ( - { - onAddReply( thread.id, inputComment ); - setActionState( false ); - } } - onCancel={ () => setActionState( false ) } - /> - ) } - { 'resolve' === actionState?.action && - thread.id === actionState?.id && ( - { - onCommentResolve( thread.id ); - setActionState( false ); - } } - discardAction={ () => - setActionState( false ) - } - /> - ) } - { 'delete' === actionState?.action && - thread.id === actionState?.id && ( - { - onCommentDelete( thread.id ); - setActionState( false ); - } } - discardAction={ () => - setActionState( false ) - } - /> + + { + onAddReply( + inputComment, + thread.id + ); + setActionState( false ); + } } + onCancel={ () => + setActionState( false ) + } + /> + ) } { 0 < thread?.reply?.length && thread.reply.map( ( reply ) => ( @@ -213,24 +221,6 @@ export function Comments( { thread={ reply } parentThread={ thread } /> - { 'delete' === actionState?.action && - reply.id === actionState?.id && ( - { - onCommentDelete( reply.id ); - setActionState( false ); - } } - discardAction={ () => - setActionState( false ) - } - /> - ) } ) ) } @@ -243,14 +233,14 @@ export function Comments( { * EditComment component. * * @param {Object} props - The component props. - * @param {Object} props.thread - The comment thread object. - * @param {Function} props.onUpdate - The function to call when updating the comment. + * @param {Function} props.onSubmit - The function to call when updating the comment. * @param {Function} props.onCancel - The function to call when canceling the comment update. - * @return {JSX.Element} The EditComment component. + * @param {Object} props.thread - The comment thread object. + * @return {JSX.Element} The CommentForm component. */ -function EditComment( { thread, onUpdate, onCancel } ) { +function CommentForm( { onSubmit, onCancel, thread } ) { const [ inputComment, setInputComment ] = useState( - removep( thread.content.rendered ) + removep( thread?.content?.rendered ?? '' ) ); return ( @@ -267,13 +257,15 @@ function EditComment( { thread, onUpdate, onCancel } ) { __next40pxDefaultSize accessibleWhenDisabled variant="primary" - onClick={ () => onUpdate( inputComment ) } + onClick={ () => onSubmit( inputComment ) } size="compact" disabled={ 0 === sanitizeCommentString( inputComment ).length } > - { _x( 'Update', 'Update comment' ) } + { thread + ? _x( 'Update', 'Update comment' ) + : _x( 'Reply', 'Add reply comment' ) } - - - - - - ); -} - /** * Renders a confirmation notice component. * diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 275027941d7818..3da38a6c651c21 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -67,35 +67,40 @@ export default function CollabSidebar() { const { updateBlockAttributes } = useDispatch( blockEditorStore ); // Function to save the comment. - const addNewComment = async ( comment ) => { + const addNewComment = async ( comment, parentCommentId ) => { const sanitisedComment = removep( comment ); - const savedRecord = await saveEntityRecord( 'root', 'comment', { + const args = { post: postId, content: sanitisedComment, comment_type: 'block_comment', - } ); + }; + + if ( parentCommentId ) { + args.parent = parentCommentId; + } + + const savedRecord = await saveEntityRecord( 'root', 'comment', args ); if ( savedRecord ) { updateBlockAttributes( clientId, { blockCommentId: savedRecord?.id, } ); - createNotice( 'snackbar', __( 'New comment added.' ), { - type: 'snackbar', - isDismissible: true, - } ); - fetchComments(); - } else { createNotice( - 'error', - // translators: Error message when comment submission fails - __( - 'Something went wrong. Please try publishing the post, or you may have already submitted your comment earlier.' - ), + 'snackbar', + parentCommentId + ? // translators: Reply added successfully + __( 'Reply added successfully.' ) + : // translators: Comment added successfully + __( 'Comment added successfully.' ), { + type: 'snackbar', isDismissible: true, } ); + fetchComments(); + } else { + onError(); } }; @@ -114,16 +119,7 @@ export default function CollabSidebar() { fetchComments(); } else { - createNotice( - 'error', - // translators: Error message when comment submission fails - __( - 'Something went wrong. Please try publishing the post, or you may have already submitted your comment earlier.' - ), - { - isDismissible: true, - } - ); + onError(); } }; @@ -148,52 +144,21 @@ export default function CollabSidebar() { fetchComments(); } else { - createNotice( - 'error', - // translators: Error message when comment submission fails - __( - 'Something went wrong. Please try publishing the post, or you may have already submitted your comment earlier.' - ), - { - isDismissible: true, - } - ); + onError(); } }; - const onAddReply = async ( parentCommentId, comment ) => { - const sanitisedComment = removep( comment ); - - const savedRecord = await saveEntityRecord( 'root', 'comment', { - parent: parentCommentId, - post: postId, - content: sanitisedComment, - comment_type: 'block_comment', - } ); - - if ( savedRecord ) { - createNotice( - 'snackbar', - // translators: Reply added successfully - __( 'Reply added successfully.' ), - { - type: 'snackbar', - isDismissible: true, - } - ); - fetchComments(); - } else { - createNotice( - 'error', - // translators: Error message when comment submission fails - __( - 'Something went wrong. Please try publishing the post, or you may have already submitted your comment earlier.' - ), - { - isDismissible: true, - } - ); - } + const onError = () => { + createNotice( + 'error', + // translators: Error message when comment submission fails + __( + 'Something went wrong. Please try publishing the post, or you may have already submitted your comment earlier.' + ), + { + isDismissible: true, + } + ); }; const onCommentDelete = async ( commentId ) => { @@ -302,7 +267,7 @@ export default function CollabSidebar() { From 68d203e0f49c808fa1947de924eb8ce9eaff3350 Mon Sep 17 00:00:00 2001 From: sunilprajapati2503 Date: Fri, 6 Sep 2024 15:28:49 +0530 Subject: [PATCH 104/159] Update context for edit button --- packages/editor/src/components/collab-sidebar/comments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 70689b1ecd18e4..c5670d02844ece 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -264,7 +264,7 @@ function CommentForm( { onSubmit, onCancel, thread } ) { } > { thread - ? _x( 'Update', 'Update comment' ) + ? _x( 'Update', 'verb' ) : _x( 'Reply', 'Add reply comment' ) } @@ -307,14 +309,12 @@ function ConfirmNotice( { confirmMessage, confirmAction, discardAction } ) { __next40pxDefaultSize variant="primary" onClick={ confirmAction } - size="compact" > { __( 'Yes' ) } diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 00d2f5d5c2878f..9da9ed91074356 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -130,7 +130,7 @@ export default function CollabSidebar() { if ( savedRecord ) { // translators: Comment resolved successfully - createNotice( 'snackbar', __( 'Thread marked as resolved.' ), { + createNotice( 'snackbar', __( 'Comment marked as resolved.' ), { type: 'snackbar', isDismissible: true, } ); @@ -151,7 +151,7 @@ export default function CollabSidebar() { createNotice( 'snackbar', // translators: Comment edited successfully - __( 'Thread edited successfully.' ), + __( 'Comment edited successfully.' ), { type: 'snackbar', isDismissible: true, @@ -187,7 +187,7 @@ export default function CollabSidebar() { createNotice( 'snackbar', // translators: Comment deleted successfully - __( 'Thread deleted successfully.' ), + __( 'Comment deleted successfully.' ), { type: 'snackbar', isDismissible: true, diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index 861aa0ee07bd0f..f28490d1bcacbf 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -25,10 +25,6 @@ flex: 1; } - &__comment-field-textarea { - width: 100%; - } - &__child-thread { margin-top: 15px; } @@ -85,8 +81,7 @@ } button { - height: 24px; - padding: 6px; + padding: 4px 10px; color: $white; } } From 0d36a58c6742ec6345b635fb02219b615456cf27 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 15 Oct 2024 19:00:59 +0530 Subject: [PATCH 123/159] feedback changes --- lib/compat/wordpress-6.7/rest-api.php | 2 +- lib/compat/wordpress-6.8/rest-api.php | 51 +++++++++++++++++++ lib/load.php | 3 ++ .../block-settings-dropdown.js | 6 +-- ...onToolbar.js => comment-button-toolbar.js} | 0 .../{commentButton.js => comment-button.js} | 0 .../src/components/collab-sidebar/index.js | 29 ++++++----- 7 files changed, 75 insertions(+), 16 deletions(-) create mode 100644 lib/compat/wordpress-6.8/rest-api.php rename packages/editor/src/components/collab-sidebar/{commentButtonToolbar.js => comment-button-toolbar.js} (100%) rename packages/editor/src/components/collab-sidebar/{commentButton.js => comment-button.js} (100%) diff --git a/lib/compat/wordpress-6.7/rest-api.php b/lib/compat/wordpress-6.7/rest-api.php index aa0ab92278b4b0..701c8569428b18 100644 --- a/lib/compat/wordpress-6.7/rest-api.php +++ b/lib/compat/wordpress-6.7/rest-api.php @@ -152,7 +152,7 @@ function update_get_avatar_comment_type( $comment_type ) { $comment_type[] = 'block_comment'; return $comment_type; } - add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type', 10, 1 ); + add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' ); } /** diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php new file mode 100644 index 00000000000000..c9e146c869d83d --- /dev/null +++ b/lib/compat/wordpress-6.8/rest-api.php @@ -0,0 +1,51 @@ + ) } + <__unstableCommentIconFill.Slot + fillProps={ { onClose } } + /> - <__unstableCommentIconFill.Slot - fillProps={ { onClose } } - /> { canCopyStyles && ! isContentOnly && ( { - if ( name?.includes( 'core/' ) && ! settings.attributes.blockCommentId ) { + if ( ! settings.attributes.blockCommentId ) { settings.attributes = { ...settings.attributes, blockCommentId: { @@ -66,13 +66,13 @@ export default function CollabSidebar() { const clientId = useSelect( ( select ) => { const { getSelectedBlockClientId } = select( blockEditorStore ); - setBlockCommentID( - select( blockEditorStore ).getBlock( getSelectedBlockClientId() ) - ?.attributes.blockCommentId - ); return getSelectedBlockClientId(); }, [] ); + const blockDetails = useSelect( ( select ) => { + return clientId ? select( blockEditorStore ).getBlock( clientId ) : null; + }, [clientId] ); + // Get the dispatch functions to save the comment and update the block attributes. const { updateBlockAttributes } = useDispatch( blockEditorStore ); @@ -90,11 +90,13 @@ export default function CollabSidebar() { comment_approved: 0, }; - if ( parentCommentId ) { - args.parent = parentCommentId; - } + // Create a new object, conditionally including the parent property + const updatedArgs = { + ...args, + ...(parentCommentId ? { parent: parentCommentId } : {}), + }; - const savedRecord = await saveEntityRecord( 'root', 'comment', args ); + const savedRecord = await saveEntityRecord( 'root', 'comment', updatedArgs ); if ( savedRecord ) { // If it's a main comment, update the block attributes with the comment id. @@ -212,8 +214,11 @@ export default function CollabSidebar() { }, [ postId, getEntityRecords ] ); useEffect( () => { + if ( blockDetails ) { + setBlockCommentID( blockDetails?.attributes.blockCommentId ); + } fetchComments(); - }, [ postId, fetchComments ] ); + }, [ postId, fetchComments, clientId ] ); const allBlocks = useSelect( ( select ) => { return select( blockEditorStore ).getBlocks(); From 9a6556f00e6601f795b6216e45267bb995966ff1 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 15 Oct 2024 19:16:39 +0530 Subject: [PATCH 124/159] linting error fixes --- .../src/components/collab-sidebar/comments.js | 10 ++-------- .../src/components/collab-sidebar/index.js | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index e2c07c9968d840..836340af393105 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -269,10 +269,7 @@ function CommentForm( { onSubmit, onCancel, thread } ) { ? _x( 'Update', 'verb' ) : _x( 'Reply', 'Add reply comment' ) } - @@ -312,10 +309,7 @@ function ConfirmNotice( { confirmMessage, confirmAction, discardAction } ) { > { __( 'Yes' ) } - diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index dc3f62d1c03b6f..e71f896aa18cef 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -69,9 +69,14 @@ export default function CollabSidebar() { return getSelectedBlockClientId(); }, [] ); - const blockDetails = useSelect( ( select ) => { - return clientId ? select( blockEditorStore ).getBlock( clientId ) : null; - }, [clientId] ); + const blockDetails = useSelect( + ( select ) => { + return clientId + ? select( blockEditorStore ).getBlock( clientId ) + : null; + }, + [ clientId ] + ); // Get the dispatch functions to save the comment and update the block attributes. const { updateBlockAttributes } = useDispatch( blockEditorStore ); @@ -93,10 +98,14 @@ export default function CollabSidebar() { // Create a new object, conditionally including the parent property const updatedArgs = { ...args, - ...(parentCommentId ? { parent: parentCommentId } : {}), + ...( parentCommentId ? { parent: parentCommentId } : {} ), }; - const savedRecord = await saveEntityRecord( 'root', 'comment', updatedArgs ); + const savedRecord = await saveEntityRecord( + 'root', + 'comment', + updatedArgs + ); if ( savedRecord ) { // If it's a main comment, update the block attributes with the comment id. From 02bd950f213cbfe160e7bea49341bef5871efef0 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 16 Oct 2024 18:44:30 +0530 Subject: [PATCH 125/159] made changes for private API and comment controller --- lib/load.php | 1 + .../block-settings-dropdown.js | 4 +++- .../src/components/block-settings-menu/index.js | 6 ++++-- packages/block-editor/src/components/index.js | 3 --- packages/block-editor/src/private-apis.js | 5 ++++- .../collab-sidebar/comment-button-toolbar.js | 15 +++++++++++---- .../components/collab-sidebar/comment-button.js | 14 +++++++++++--- .../editor/src/components/collab-sidebar/index.js | 8 ++++---- 8 files changed, 38 insertions(+), 18 deletions(-) diff --git a/lib/load.php b/lib/load.php index f3a96c2365f0de..5fb67ba459d7bb 100644 --- a/lib/load.php +++ b/lib/load.php @@ -48,6 +48,7 @@ function gutenberg_is_experiment_enabled( $name ) { // WordPress 6.8 compat. require __DIR__ . '/compat/wordpress-6.8/rest-api.php'; + require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php'; // Plugin specific code. require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php'; diff --git a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js index 2b43636cd0c764..e169cb87a7eed8 100644 --- a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js +++ b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js @@ -19,7 +19,7 @@ import { pipe, useCopyToClipboard } from '@wordpress/compose'; * Internal dependencies */ import BlockActions from '../block-actions'; -import __unstableCommentIconFill from '../collab/block-comment-icon-slot'; +import { privateApis as blockEditorPrivateApis } from '../../private-apis'; import BlockHTMLConvertButton from './block-html-convert-button'; import __unstableBlockSettingsMenuFirstItem from './block-settings-menu-first-item'; import BlockSettingsMenuControls from '../block-settings-menu-controls'; @@ -27,6 +27,8 @@ import BlockParentSelectorMenuItem from './block-parent-selector-menu-item'; import { store as blockEditorStore } from '../../store'; import { unlock } from '../../lock-unlock'; +const { __unstableCommentIconFill } = unlock( blockEditorPrivateApis ); + const POPOVER_PROPS = { className: 'block-editor-block-settings-menu__popover', placement: 'bottom-start', diff --git a/packages/block-editor/src/components/block-settings-menu/index.js b/packages/block-editor/src/components/block-settings-menu/index.js index 5856b438c571d9..ca3140ea5af509 100644 --- a/packages/block-editor/src/components/block-settings-menu/index.js +++ b/packages/block-editor/src/components/block-settings-menu/index.js @@ -7,12 +7,14 @@ import { ToolbarGroup, ToolbarItem } from '@wordpress/components'; * Internal dependencies */ import BlockSettingsDropdown from './block-settings-dropdown'; -import __unstableCommentIconToolbarFill from '../collab/block-comment-icon-toolbar-slot'; +//import __unstableCommentIconToolbarFill from '../collab/block-comment-icon-toolbar-slot'; +import { privateApis as blockEditorPrivateApis } from '../../private-apis'; +import { unlock } from '../../lock-unlock'; +const { __unstableCommentIconToolbarFill } = unlock( blockEditorPrivateApis ); export function BlockSettingsMenu( { clientIds, ...props } ) { return ( - <__unstableCommentIconToolbarFill.Slot /> diff --git a/packages/block-editor/src/components/index.js b/packages/block-editor/src/components/index.js index 427deb07d8757c..29bb71b682e970 100644 --- a/packages/block-editor/src/components/index.js +++ b/packages/block-editor/src/components/index.js @@ -104,9 +104,6 @@ export { */ export { default as __unstableBlockSettingsMenuFirstItem } from './block-settings-menu/block-settings-menu-first-item'; -export { default as __unstableCommentIconFill } from './collab/block-comment-icon-slot'; -export { default as __unstableCommentIconToolbarFill } from './collab/block-comment-icon-toolbar-slot'; - export { default as __unstableBlockToolbarLastItem } from './block-toolbar/block-toolbar-last-item'; export { default as __unstableBlockNameContext } from './block-toolbar/block-name-context'; export { default as __unstableInserterMenuExtension } from './inserter-menu-extension'; diff --git a/packages/block-editor/src/private-apis.js b/packages/block-editor/src/private-apis.js index 98dd326a3ebb34..9952b8bb54d0a7 100644 --- a/packages/block-editor/src/private-apis.js +++ b/packages/block-editor/src/private-apis.js @@ -48,7 +48,8 @@ import { PrivatePublishDateTimePicker } from './components/publish-date-time-pic import useSpacingSizes from './components/spacing-sizes-control/hooks/use-spacing-sizes'; import useBlockDisplayTitle from './components/block-title/use-block-display-title'; import TabbedSidebar from './components/tabbed-sidebar'; - +import __unstableCommentIconFill from './components/collab/block-comment-icon-slot'; +import __unstableCommentIconToolbarFill from './components/collab/block-comment-icon-toolbar-slot'; /** * Private @wordpress/block-editor APIs. */ @@ -94,4 +95,6 @@ lock( privateApis, { __unstableBlockStyleVariationOverridesWithConfig, setBackgroundStyleDefaults, sectionRootClientIdKey, + __unstableCommentIconFill, + __unstableCommentIconToolbarFill, } ); diff --git a/packages/editor/src/components/collab-sidebar/comment-button-toolbar.js b/packages/editor/src/components/collab-sidebar/comment-button-toolbar.js index da1e4b761a766e..2e378a7eaabab0 100644 --- a/packages/editor/src/components/collab-sidebar/comment-button-toolbar.js +++ b/packages/editor/src/components/collab-sidebar/comment-button-toolbar.js @@ -4,18 +4,25 @@ import { ToolbarButton } from '@wordpress/components'; import { _x } from '@wordpress/i18n'; import { comment as commentIcon } from '@wordpress/icons'; -import { __unstableCommentIconToolbarFill as CommentIconToolbarFill } from '@wordpress/block-editor'; +import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import { unlock } from '../../lock-unlock'; + +const { __unstableCommentIconToolbarFill } = unlock( blockEditorPrivateApis ); const AddCommentToolbarButton = ( { onClick } ) => { return ( - + <__unstableCommentIconToolbarFill> - + ); }; diff --git a/packages/editor/src/components/collab-sidebar/comment-button.js b/packages/editor/src/components/collab-sidebar/comment-button.js index 198ce909eee04a..3b020661816660 100644 --- a/packages/editor/src/components/collab-sidebar/comment-button.js +++ b/packages/editor/src/components/collab-sidebar/comment-button.js @@ -4,11 +4,19 @@ import { MenuItem } from '@wordpress/components'; import { _x } from '@wordpress/i18n'; import { comment as commentIcon } from '@wordpress/icons'; -import { __unstableCommentIconFill as CommentIconFill } from '@wordpress/block-editor'; + +import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import { unlock } from '../../lock-unlock'; + +const { __unstableCommentIconFill } = unlock( blockEditorPrivateApis ); const AddCommentButton = ( { onClick } ) => { return ( - + <__unstableCommentIconFill> { > { _x( 'Comment', 'Add comment button' ) } - + ); }; diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index e71f896aa18cef..a515f84d8fd7b7 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -127,7 +127,7 @@ export default function CollabSidebar() { isDismissible: true, } ); - fetchComments(); + await fetchComments(); } else { onError(); } @@ -146,7 +146,7 @@ export default function CollabSidebar() { isDismissible: true, } ); - fetchComments(); + await fetchComments(); } else { onError(); } @@ -169,7 +169,7 @@ export default function CollabSidebar() { } ); - fetchComments(); + await fetchComments(); } else { onError(); } @@ -204,7 +204,7 @@ export default function CollabSidebar() { isDismissible: true, } ); - fetchComments(); + await fetchComments(); }; const fetchComments = useCallback( async () => { From 973f559e6e241c6e8478bb670ff46f256bfa8a0c Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 16 Oct 2024 18:47:18 +0530 Subject: [PATCH 126/159] remove comment code and added comment controller file --- ...-gutenberg-rest-comment-controller-6-8.php | 117 ++++++++++++++++++ .../components/block-settings-menu/index.js | 1 - 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php new file mode 100644 index 00000000000000..26e94eeea73bc5 --- /dev/null +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php @@ -0,0 +1,117 @@ + 401 ) + ); + } + + /** + * Filters whether comments can be created via the REST API without authentication. + * + * Enables creating comments for anonymous users. + * + * @since 4.7.0 + * + * @param bool $allow_anonymous Whether to allow anonymous comments to + * be created. Default `false`. + * @param WP_REST_Request $request Request used to generate the + * response. + */ + $allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false, $request ); + + if ( ! $allow_anonymous ) { + return new WP_Error( + 'rest_comment_login_required', + __( 'Sorry, you must be logged in to comment.' ), + array( 'status' => 401 ) + ); + } + } + + // Limit who can set comment `author`, `author_ip` or `status` to anything other than the default. + if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { + return new WP_Error( + 'rest_comment_invalid_author', + /* translators: %s: Request parameter. */ + sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + + if ( isset( $request['author_ip'] ) && ! current_user_can( 'moderate_comments' ) ) { + if ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] ) { + return new WP_Error( + 'rest_comment_invalid_author_ip', + /* translators: %s: Request parameter. */ + sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + } + + if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) { + return new WP_Error( + 'rest_comment_invalid_status', + /* translators: %s: Request parameter. */ + sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'status' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + + if ( empty( $request['post'] ) ) { + return new WP_Error( + 'rest_comment_invalid_post_id', + __( 'Sorry, you are not allowed to create this comment without a post.' ), + array( 'status' => 403 ) + ); + } + + $post = get_post( (int) $request['post'] ); + + if ( ! $post ) { + return new WP_Error( + 'rest_comment_invalid_post_id', + __( 'Sorry, you are not allowed to create this comment without a post.' ), + array( 'status' => 403 ) + ); + } + + if ( 'trash' === $post->post_status ) { + return new WP_Error( + 'rest_comment_trash_post', + __( 'Sorry, you are not allowed to create a comment on this post.' ), + array( 'status' => 403 ) + ); + } + + if ( ! $this->check_read_post_permission( $post, $request ) ) { + return new WP_Error( + 'rest_cannot_read_post', + __( 'Sorry, you are not allowed to read the post for this comment.' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + + return true; + } + +} + +add_action( 'rest_api_init', function () { + $controller = new Gutenberg_REST_Comments_Controller_6_8(); + $controller->register_routes(); +} ); diff --git a/packages/block-editor/src/components/block-settings-menu/index.js b/packages/block-editor/src/components/block-settings-menu/index.js index ca3140ea5af509..542e60f65e2129 100644 --- a/packages/block-editor/src/components/block-settings-menu/index.js +++ b/packages/block-editor/src/components/block-settings-menu/index.js @@ -7,7 +7,6 @@ import { ToolbarGroup, ToolbarItem } from '@wordpress/components'; * Internal dependencies */ import BlockSettingsDropdown from './block-settings-dropdown'; -//import __unstableCommentIconToolbarFill from '../collab/block-comment-icon-toolbar-slot'; import { privateApis as blockEditorPrivateApis } from '../../private-apis'; import { unlock } from '../../lock-unlock'; const { __unstableCommentIconToolbarFill } = unlock( blockEditorPrivateApis ); From ab1395c47dc14c63728effc763574fa4a68ec3d7 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 16 Oct 2024 19:02:25 +0530 Subject: [PATCH 127/159] fix conding standerds warnings and error --- .../class-gutenberg-rest-comment-controller-6-8.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php index 26e94eeea73bc5..c46a2b359b6e8c 100644 --- a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php @@ -7,7 +7,7 @@ */ // Create a new class that extends WP_REST_Comments_Controller -class Gutenberg_REST_Comments_Controller_6_8 extends WP_REST_Comments_Controller { +class Gutenberg_REST_Comment_Controller_6_8 extends WP_REST_Comments_Controller { public function create_item_permissions_check( $request ) { if ( ! is_user_logged_in() ) { @@ -111,7 +111,7 @@ public function create_item_permissions_check( $request ) { } -add_action( 'rest_api_init', function () { - $controller = new Gutenberg_REST_Comments_Controller_6_8(); +add_action('rest_api_init', function() { + $controller = new Gutenberg_REST_Comment_Controller_6_8(); $controller->register_routes(); -} ); +}); From f9f24d93a7995489d647447e6013b179920ecd00 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 16 Oct 2024 19:07:56 +0530 Subject: [PATCH 128/159] fix coding standard warnings and error --- .../class-gutenberg-rest-comment-controller-6-8.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php index c46a2b359b6e8c..38111d6d297dcc 100644 --- a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php @@ -108,10 +108,12 @@ public function create_item_permissions_check( $request ) { return true; } - } -add_action('rest_api_init', function() { - $controller = new Gutenberg_REST_Comment_Controller_6_8(); - $controller->register_routes(); -}); +add_action( + 'rest_api_init', + function() { + $controller = new Gutenberg_REST_Comment_Controller_6_8(); + $controller->register_routes(); + } +); From 63f915f35cfacfce6061300b594456a87d1689e6 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 16 Oct 2024 19:12:49 +0530 Subject: [PATCH 129/159] fix coding standard warnings and error --- .../class-gutenberg-rest-comment-controller-6-8.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php index 38111d6d297dcc..604392bb6a147d 100644 --- a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php @@ -111,9 +111,9 @@ public function create_item_permissions_check( $request ) { } add_action( - 'rest_api_init', - function() { - $controller = new Gutenberg_REST_Comment_Controller_6_8(); - $controller->register_routes(); - } + 'rest_api_init', + function () { + $controller = new Gutenberg_REST_Comment_Controller_6_8(); + $controller->register_routes(); + } ); From 68c5272c5bb1228ac77bc3aebaf48d0d876700c8 Mon Sep 17 00:00:00 2001 From: minaldiwan <77973388+minaldiwan@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:36:32 +0530 Subject: [PATCH 130/159] Implement confirmDialog box for delete and resolve comment box --- .../src/components/collab-sidebar/comments.js | 124 ++++++++---------- 1 file changed, 52 insertions(+), 72 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 836340af393105..5d1bde6e94dbc4 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -10,6 +10,7 @@ import { useState, useMemo, RawHTML } from '@wordpress/element'; import { __experimentalHStack as HStack, __experimentalVStack as VStack, + __experimentalConfirmDialog as ConfirmDialog, Button, DropdownMenu, TextareaControl, @@ -50,6 +51,24 @@ export function Comments( { onCommentResolve, } ) { const [ actionState, setActionState ] = useState( false ); + const [ isConfirmDialogOpen, setIsConfirmDialogOpen ] = useState( false ); + + const handleConfirmDelete = () => { + onCommentDelete(actionState.id); + setActionState(false); + setIsConfirmDialogOpen(false); + }; + + const handleConfirmResolve = () => { + onCommentResolve(actionState.id); + setActionState(false); + setIsConfirmDialogOpen(false); + }; + + const handleCancelDelete = () => { + setActionState(false); + setIsConfirmDialogOpen(false); + }; const blockCommentId = useSelect( ( select ) => { const clientID = select( blockEditorStore ).getSelectedBlockClientId(); @@ -64,18 +83,20 @@ export function Comments( { <> - setActionState( { + onResolve={ () => { + setActionState({ action: 'resolve', id: parentThread?.id ?? thread.id, - } ) - } + }); + setIsConfirmDialogOpen(true); + }} onEdit={ () => setActionState( { action: 'edit', id: thread.id } ) } - onDelete={ () => - setActionState( { action: 'delete', id: thread.id } ) - } + onDelete={ () => { + setActionState({ action: 'delete', id: thread.id }); + setIsConfirmDialogOpen(true); + }} onReply={ ! parentThread ? () => @@ -116,35 +137,33 @@ export function Comments( { { 'resolve' === actionState?.action && thread.id === actionState?.id && ( - { - onCommentResolve( thread.id ); - setActionState( false ); - } } - discardAction={ () => setActionState( false ) } - /> + + { + // translators: message displayed when confirming an action + __('Are you sure you want to mark this comment as resolved?') + } + ) } { 'delete' === actionState?.action && thread.id === actionState?.id && ( - { - onCommentDelete( thread.id ); - setActionState( false ); - } } - discardAction={ () => setActionState( false ) } - /> + + { + // translators: message displayed when confirming an action + __('Are you sure you want to delete this comment?') + } + ) } ); @@ -278,45 +297,6 @@ function CommentForm( { onSubmit, onCancel, thread } ) { ); } -/** - * Renders a confirmation notice component. - * - * @param {Object} props - The component props. - * @param {string} props.confirmMessage - The confirmation message to display. Defaults to 'Are you sure?' if not provided. - * @param {Function} props.confirmAction - The action to perform when the confirm button is clicked. - * @param {Function} props.discardAction - The action to perform when the discard button is clicked. - * @return {JSX.Element} The confirmation notice component. - */ -function ConfirmNotice( { confirmMessage, confirmAction, discardAction } ) { - return ( - -

- { confirmMessage ?? - // translators: message displayed when confirming an action - __( 'Are you sure?' ) } -

- - - - -
- ); -} - /** * Renders the header of a comment in the collaboration sidebar. * From 6b32fcfeb6adabc2e957dde7db0b94129d8b29d2 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 17 Oct 2024 12:55:05 +0530 Subject: [PATCH 131/159] feedback changes --- .../src/components/collab-sidebar/comments.js | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index 836340af393105..02e3da400eca02 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -6,7 +6,7 @@ import clsx from 'clsx'; /** * WordPress dependencies */ -import { useState, useMemo, RawHTML } from '@wordpress/element'; +import { useState, RawHTML } from '@wordpress/element'; import { __experimentalHStack as HStack, __experimentalVStack as VStack, @@ -344,24 +344,22 @@ function CommentHeader( { 'time_format' ); - const memorizedMoreActions = useMemo( () => { - return [ - { - title: _x( 'Edit', 'Edit comment' ), - onClick: onEdit, - }, - { - title: _x( 'Delete', 'Delete comment' ), - onClick: onDelete, - }, - { - title: _x( 'Reply', 'Reply on a comment' ), - onClick: onReply, - }, - ]; - }, [ onEdit, onDelete, onReply ] ); + const actions = [ + { + title: _x( 'Edit', 'Edit comment' ), + onClick: onEdit, + }, + { + title: _x( 'Delete', 'Delete comment' ), + onClick: onDelete, + }, + { + title: _x( 'Reply', 'Reply on a comment' ), + onClick: onReply, + }, + ]; - const moreActions = memorizedMoreActions.filter( ( item ) => item.onClick ); + const moreActions = actions.filter( ( item ) => item.onClick ); return ( From c92de93300561b61739b1eb51dac10583108958f Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 17 Oct 2024 16:06:27 +0530 Subject: [PATCH 132/159] made changes to hide block_comment from admin screen --- lib/compat/wordpress-6.8/rest-api.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php index c9e146c869d83d..8997b12a19f304 100644 --- a/lib/compat/wordpress-6.8/rest-api.php +++ b/lib/compat/wordpress-6.8/rest-api.php @@ -49,3 +49,30 @@ function update_get_avatar_comment_type( $comment_type ) { } add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' ); } + +/** + * Excludes block comments from the admin comments query. + * + * This function modifies the comments query to exclude comments of type 'block_comment' + * when the query is for comments in the WordPress admin. + * + * @param WP_Comment_Query $query The current comments query. + * + * @return void + */ +if ( ! function_exists( 'exclude_block_comments_from_admin' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) { + function exclude_block_comments_from_admin( $query ) { + // Only modify the query if it's for comments + if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) { + $query->set( 'type', '' ); + + add_filter( 'comments_clauses', function( $clauses ) { + global $wpdb; + // Exclude comments of type 'block_comment' + $clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'"; + return $clauses; + }); + } + } + add_action( 'pre_get_comments', 'exclude_block_comments_from_admin' ); +} From 97a6d6e727f7ce8a9e07985dc43bd195030d19d6 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 17 Oct 2024 16:15:01 +0530 Subject: [PATCH 133/159] fix coding standard warnings and error --- lib/compat/wordpress-6.8/rest-api.php | 31 ++++---- .../src/components/collab-sidebar/comments.js | 78 ++++++++++--------- 2 files changed, 58 insertions(+), 51 deletions(-) diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php index 8997b12a19f304..352e0f3a58cd02 100644 --- a/lib/compat/wordpress-6.8/rest-api.php +++ b/lib/compat/wordpress-6.8/rest-api.php @@ -61,18 +61,21 @@ function update_get_avatar_comment_type( $comment_type ) { * @return void */ if ( ! function_exists( 'exclude_block_comments_from_admin' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) { - function exclude_block_comments_from_admin( $query ) { - // Only modify the query if it's for comments - if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) { - $query->set( 'type', '' ); - - add_filter( 'comments_clauses', function( $clauses ) { - global $wpdb; - // Exclude comments of type 'block_comment' - $clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'"; - return $clauses; - }); - } - } - add_action( 'pre_get_comments', 'exclude_block_comments_from_admin' ); + function exclude_block_comments_from_admin( $query ) { + // Only modify the query if it's for comments + if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) { + $query->set( 'type', '' ); + + add_filter( + 'comments_clauses', + function( $clauses ) { + global $wpdb; + // Exclude comments of type 'block_comment' + $clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'"; + return $clauses; + } + ); + } + } + add_action( 'pre_get_comments', 'exclude_block_comments_from_admin' ); } diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index d52f4bc7ded1ec..c0b4bd3d4dc38a 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -54,20 +54,20 @@ export function Comments( { const [ isConfirmDialogOpen, setIsConfirmDialogOpen ] = useState( false ); const handleConfirmDelete = () => { - onCommentDelete(actionState.id); - setActionState(false); - setIsConfirmDialogOpen(false); + onCommentDelete( actionState.id ); + setActionState( false ); + setIsConfirmDialogOpen( false ); }; const handleConfirmResolve = () => { - onCommentResolve(actionState.id); - setActionState(false); - setIsConfirmDialogOpen(false); + onCommentResolve( actionState.id ); + setActionState( false ); + setIsConfirmDialogOpen( false ); }; const handleCancelDelete = () => { - setActionState(false); - setIsConfirmDialogOpen(false); + setActionState( false ); + setIsConfirmDialogOpen( false ); }; const blockCommentId = useSelect( ( select ) => { @@ -84,19 +84,19 @@ export function Comments( { { - setActionState({ + setActionState( { action: 'resolve', id: parentThread?.id ?? thread.id, - }); - setIsConfirmDialogOpen(true); - }} + } ); + setIsConfirmDialogOpen( true ); + } } onEdit={ () => setActionState( { action: 'edit', id: thread.id } ) } onDelete={ () => { - setActionState({ action: 'delete', id: thread.id }); - setIsConfirmDialogOpen(true); - }} + setActionState( { action: 'delete', id: thread.id } ); + setIsConfirmDialogOpen( true ); + } } onReply={ ! parentThread ? () => @@ -138,32 +138,36 @@ export function Comments( { { 'resolve' === actionState?.action && thread.id === actionState?.id && ( - { - // translators: message displayed when confirming an action - __('Are you sure you want to mark this comment as resolved?') - } - + isOpen={ isConfirmDialogOpen } + onConfirm={ handleConfirmResolve } + onCancel={ handleCancelDelete } + confirmButtonText="Yes" + cancelButtonText="No" + > + { + // translators: message displayed when confirming an action + __( + 'Are you sure you want to mark this comment as resolved?' + ) + } + ) } { 'delete' === actionState?.action && thread.id === actionState?.id && ( - { - // translators: message displayed when confirming an action - __('Are you sure you want to delete this comment?') - } - + isOpen={ isConfirmDialogOpen } + onConfirm={ handleConfirmDelete } + onCancel={ handleCancelDelete } + confirmButtonText="Yes" + cancelButtonText="No" + > + { + // translators: message displayed when confirming an action + __( + 'Are you sure you want to delete this comment?' + ) + } + ) } ); From 0136fae3c8c9debec2fb6ede07ef3b1fee1aec13 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 17 Oct 2024 16:17:46 +0530 Subject: [PATCH 134/159] fix coding standard warnings and error --- lib/compat/wordpress-6.8/rest-api.php | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php index 352e0f3a58cd02..841b79714e386d 100644 --- a/lib/compat/wordpress-6.8/rest-api.php +++ b/lib/compat/wordpress-6.8/rest-api.php @@ -61,21 +61,21 @@ function update_get_avatar_comment_type( $comment_type ) { * @return void */ if ( ! function_exists( 'exclude_block_comments_from_admin' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) { - function exclude_block_comments_from_admin( $query ) { - // Only modify the query if it's for comments - if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) { - $query->set( 'type', '' ); + function exclude_block_comments_from_admin( $query ) { + // Only modify the query if it's for comments + if ( isset( $query->query_vars['type'] ) && '' === $query->query_vars['type'] ) { + $query->set( 'type', '' ); - add_filter( - 'comments_clauses', - function( $clauses ) { - global $wpdb; - // Exclude comments of type 'block_comment' - $clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'"; - return $clauses; - } - ); - } - } - add_action( 'pre_get_comments', 'exclude_block_comments_from_admin' ); + add_filter( + 'comments_clauses', + function( $clauses ) { + global $wpdb; + // Exclude comments of type 'block_comment' + $clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'"; + return $clauses; + } + ); + } + } + add_action( 'pre_get_comments', 'exclude_block_comments_from_admin' ); } From 13f53d8c789eb601c2e6a135094b62a93fe78094 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 17 Oct 2024 16:28:00 +0530 Subject: [PATCH 135/159] fix coding standard warnings and error --- lib/compat/wordpress-6.8/rest-api.php | 2 +- packages/editor/src/components/collab-sidebar/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/rest-api.php index 841b79714e386d..df205599223929 100644 --- a/lib/compat/wordpress-6.8/rest-api.php +++ b/lib/compat/wordpress-6.8/rest-api.php @@ -68,7 +68,7 @@ function exclude_block_comments_from_admin( $query ) { add_filter( 'comments_clauses', - function( $clauses ) { + function ( $clauses ) { global $wpdb; // Exclude comments of type 'block_comment' $clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'block_comment'"; diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index a515f84d8fd7b7..fe67210769b29a 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -23,7 +23,7 @@ import AddCommentToolbarButton from './comment-button-toolbar'; const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; -const modifyBlockCommentAttributes = ( settings, name ) => { +const modifyBlockCommentAttributes = ( settings ) => { if ( ! settings.attributes.blockCommentId ) { settings.attributes = { ...settings.attributes, From 4d0ca6e186468d1d000c67aef9564d3cd03e87c8 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 17 Oct 2024 17:15:39 +0530 Subject: [PATCH 136/159] fix coding standard warnings and error --- .../src/components/collab-sidebar/index.js | 2 +- packages/editor/src/store/actions.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index fe67210769b29a..a65836056bcca3 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -53,7 +53,7 @@ export default function CollabSidebar() { const { getEntityRecords } = resolveSelect( coreStore ); // eslint-disable-next-line @wordpress/data-no-store-string-literals - const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); + const { openGeneralSidebar } = useDispatch( editorStore ); const [ blockCommentID, setBlockCommentID ] = useState( null ); const [ showCommentBoard, setShowCommentBoard ] = useState( false ); diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index f8a18fc86e5376..495e491264ec46 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -30,7 +30,10 @@ import { getNotificationArgumentsForSaveFail, getNotificationArgumentsForTrashFail, } from './utils/notice-builder'; +import { privateApis as editorPrivateApis } from '../private-apis'; import { unlock } from '../lock-unlock'; + +const { interfaceStore } = unlock( editorPrivateApis ); /** * Returns an action generator used in signalling that editor has initialized with * the specified post object and editor settings. @@ -192,6 +195,19 @@ export const editPost = .editEntityRecord( 'postType', type, id, edits, options ); }; +/** + * Action that opens an editor sidebar. + * + * @param {?string} name Sidebar name to be opened. + */ +export const openGeneralSidebar = + ( name ) => + ( { registry } ) => { + registry + .dispatch( interfaceStore ) + .enableComplementaryArea( 'core', name ); + }; + /** * Action for saving the current post in the editor. * From c5b76e860546a4d1a9391f7dbbb4543c64f0a5f8 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Thu, 17 Oct 2024 17:33:42 +0530 Subject: [PATCH 137/159] updated doc file --- docs/reference-guides/data/data-core-editor.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/reference-guides/data/data-core-editor.md b/docs/reference-guides/data/data-core-editor.md index ac3413e694877b..0d2de616eae716 100644 --- a/docs/reference-guides/data/data-core-editor.md +++ b/docs/reference-guides/data/data-core-editor.md @@ -1344,6 +1344,14 @@ _Related_ - multiSelect in core/block-editor store. +### openGeneralSidebar + +Action that opens an editor sidebar. + +_Parameters_ + +- _name_ `?string`: Sidebar name to be opened. + ### openPublishSidebar Returns an action object used in signalling that the user opened the publish sidebar. From c8ff6444f081e7878a3657e7907ce76324f388a1 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Fri, 18 Oct 2024 16:00:52 +0530 Subject: [PATCH 138/159] fix delete comment on reply feedback --- .../editor/src/components/collab-sidebar/index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index a65836056bcca3..570029b5a9dc75 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -50,7 +50,7 @@ addFilter( export default function CollabSidebar() { const { createNotice } = useDispatch( noticesStore ); const { saveEntityRecord, deleteEntityRecord } = useDispatch( coreStore ); - const { getEntityRecords } = resolveSelect( coreStore ); + const { getEntityRecords, getEntityRecord } = resolveSelect( coreStore ); // eslint-disable-next-line @wordpress/data-no-store-string-literals const { openGeneralSidebar } = useDispatch( editorStore ); @@ -189,11 +189,18 @@ export default function CollabSidebar() { }; const onCommentDelete = async ( commentId ) => { + const childComment = await getEntityRecord( + 'root', + 'comment', + commentId + ); await deleteEntityRecord( 'root', 'comment', commentId ); - updateBlockAttributes( clientId, { - blockCommentId: undefined, - } ); + if ( childComment && ! childComment.parent ) { + updateBlockAttributes( clientId, { + blockCommentId: undefined, + } ); + } createNotice( 'snackbar', From 45f9553cee6960ac46221dd41b27024299448cca Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Mon, 21 Oct 2024 16:54:09 +0530 Subject: [PATCH 139/159] fix private API import from same API test cases fail errors --- .../components/block-settings-menu/block-settings-dropdown.js | 4 +--- .../block-editor/src/components/block-settings-menu/index.js | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js index e169cb87a7eed8..5b5360cc48a8fe 100644 --- a/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js +++ b/packages/block-editor/src/components/block-settings-menu/block-settings-dropdown.js @@ -19,7 +19,7 @@ import { pipe, useCopyToClipboard } from '@wordpress/compose'; * Internal dependencies */ import BlockActions from '../block-actions'; -import { privateApis as blockEditorPrivateApis } from '../../private-apis'; +import __unstableCommentIconFill from '../../components/collab/block-comment-icon-slot'; import BlockHTMLConvertButton from './block-html-convert-button'; import __unstableBlockSettingsMenuFirstItem from './block-settings-menu-first-item'; import BlockSettingsMenuControls from '../block-settings-menu-controls'; @@ -27,8 +27,6 @@ import BlockParentSelectorMenuItem from './block-parent-selector-menu-item'; import { store as blockEditorStore } from '../../store'; import { unlock } from '../../lock-unlock'; -const { __unstableCommentIconFill } = unlock( blockEditorPrivateApis ); - const POPOVER_PROPS = { className: 'block-editor-block-settings-menu__popover', placement: 'bottom-start', diff --git a/packages/block-editor/src/components/block-settings-menu/index.js b/packages/block-editor/src/components/block-settings-menu/index.js index 542e60f65e2129..50e8abe09d018b 100644 --- a/packages/block-editor/src/components/block-settings-menu/index.js +++ b/packages/block-editor/src/components/block-settings-menu/index.js @@ -7,9 +7,7 @@ import { ToolbarGroup, ToolbarItem } from '@wordpress/components'; * Internal dependencies */ import BlockSettingsDropdown from './block-settings-dropdown'; -import { privateApis as blockEditorPrivateApis } from '../../private-apis'; -import { unlock } from '../../lock-unlock'; -const { __unstableCommentIconToolbarFill } = unlock( blockEditorPrivateApis ); +import __unstableCommentIconToolbarFill from '../../components/collab/block-comment-icon-toolbar-slot'; export function BlockSettingsMenu( { clientIds, ...props } ) { return ( From e3cb45b61af9f4c7ec0bf6ed514b02f76bce3aa4 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Mon, 21 Oct 2024 18:58:13 +0530 Subject: [PATCH 140/159] fix e2e error while testing --- packages/editor/src/components/collab-sidebar/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 570029b5a9dc75..712df725215cc7 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -29,7 +29,6 @@ const modifyBlockCommentAttributes = ( settings ) => { ...settings.attributes, blockCommentId: { type: 'number', - default: 0, }, }; } From c4d4b1d9d0f76575717f373042614f392a1483e8 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Mon, 21 Oct 2024 19:17:01 +0530 Subject: [PATCH 141/159] made conditional changes for default attributes --- packages/editor/src/components/collab-sidebar/add-comment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 699822215498bf..809ee5156d5a6a 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -66,7 +66,7 @@ export function AddComment( { setInputComment( '' ); }; - if ( ! showAddCommentBoard || ! clientId || 0 !== blockCommentId ) { + if ( ! showAddCommentBoard || ! clientId || undefined !== blockCommentId ) { return null; } From 7a6c08540e36e7a4c6f860daf64c0236d8f572ae Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Mon, 21 Oct 2024 19:59:26 +0530 Subject: [PATCH 142/159] made openGeneralSidebar to private --- docs/reference-guides/data/data-core-editor.md | 8 -------- .../src/components/collab-sidebar/index.js | 5 +++-- packages/editor/src/store/actions.js | 16 ---------------- 3 files changed, 3 insertions(+), 26 deletions(-) diff --git a/docs/reference-guides/data/data-core-editor.md b/docs/reference-guides/data/data-core-editor.md index 0d2de616eae716..ac3413e694877b 100644 --- a/docs/reference-guides/data/data-core-editor.md +++ b/docs/reference-guides/data/data-core-editor.md @@ -1344,14 +1344,6 @@ _Related_ - multiSelect in core/block-editor store. -### openGeneralSidebar - -Action that opens an editor sidebar. - -_Parameters_ - -- _name_ `?string`: Sidebar name to be opened. - ### openPublishSidebar Returns an action object used in signalling that the user opened the publish sidebar. diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 712df725215cc7..d0cf8edd0e1c67 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -9,6 +9,7 @@ import { addFilter } from '@wordpress/hooks'; import { store as noticesStore } from '@wordpress/notices'; import { store as coreStore } from '@wordpress/core-data'; import { store as blockEditorStore } from '@wordpress/block-editor'; +import { store as interfaceStore } from '@wordpress/interface'; /** * Internal dependencies @@ -52,7 +53,7 @@ export default function CollabSidebar() { const { getEntityRecords, getEntityRecord } = resolveSelect( coreStore ); // eslint-disable-next-line @wordpress/data-no-store-string-literals - const { openGeneralSidebar } = useDispatch( editorStore ); + const { enableComplementaryArea } = useDispatch( interfaceStore ); const [ blockCommentID, setBlockCommentID ] = useState( null ); const [ showCommentBoard, setShowCommentBoard ] = useState( false ); @@ -82,7 +83,7 @@ export default function CollabSidebar() { const openCollabBoard = () => { setShowCommentBoard( true ); - openGeneralSidebar( 'edit-post/collab-sidebar' ); + enableComplementaryArea( 'core', 'edit-post/collab-sidebar' ); }; // Function to save the comment. diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 803f6cfdefe64d..70be70c6293399 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -30,10 +30,7 @@ import { getNotificationArgumentsForSaveFail, getNotificationArgumentsForTrashFail, } from './utils/notice-builder'; -import { privateApis as editorPrivateApis } from '../private-apis'; import { unlock } from '../lock-unlock'; - -const { interfaceStore } = unlock( editorPrivateApis ); /** * Returns an action generator used in signalling that editor has initialized with * the specified post object and editor settings. @@ -195,19 +192,6 @@ export const editPost = .editEntityRecord( 'postType', type, id, edits, options ); }; -/** - * Action that opens an editor sidebar. - * - * @param {?string} name Sidebar name to be opened. - */ -export const openGeneralSidebar = - ( name ) => - ( { registry } ) => { - registry - .dispatch( interfaceStore ) - .enableComplementaryArea( 'core', name ); - }; - /** * Action for saving the current post in the editor. * From 7ce91e9d9b24d9117c3a26beea0e098418e4a686 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 22 Oct 2024 13:16:41 +0530 Subject: [PATCH 143/159] made changes as per the feedback --- backport-changelog/6.8/7498.md | 3 +-- .../{rest-api.php => block-comments.php} | 0 ...-gutenberg-rest-comment-controller-6-8.php | 21 +++++++++++++++++++ lib/load.php | 2 +- .../src/components/collab-sidebar/index.js | 2 +- 5 files changed, 24 insertions(+), 4 deletions(-) rename lib/compat/wordpress-6.8/{rest-api.php => block-comments.php} (100%) diff --git a/backport-changelog/6.8/7498.md b/backport-changelog/6.8/7498.md index 764f5244a682fd..6c903246166b64 100644 --- a/backport-changelog/6.8/7498.md +++ b/backport-changelog/6.8/7498.md @@ -1,4 +1,3 @@ https://github.com/WordPress/wordpress-develop/pull/7498 -* https://github.com/WordPress/gutenberg/pull/60622 -* https://github.com/WordPress/gutenberg/pull/65181 \ No newline at end of file +* https://github.com/WordPress/gutenberg/pull/60622 \ No newline at end of file diff --git a/lib/compat/wordpress-6.8/rest-api.php b/lib/compat/wordpress-6.8/block-comments.php similarity index 100% rename from lib/compat/wordpress-6.8/rest-api.php rename to lib/compat/wordpress-6.8/block-comments.php diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php index 604392bb6a147d..a5872f7735f8d2 100644 --- a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php @@ -42,6 +42,11 @@ public function create_item_permissions_check( $request ) { } } + // Skip all the checks if comment type is not comment. + if( 'comment' !== $request['comment_type'] ) { + return true; + } + // Limit who can set comment `author`, `author_ip` or `status` to anything other than the default. if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { return new WP_Error( @@ -90,6 +95,14 @@ public function create_item_permissions_check( $request ) { ); } + if ( 'draft' === $post->post_status ) { + return new WP_Error( + 'rest_comment_draft_post', + __( 'Sorry, you are not allowed to create a comment on this post.' ), + array( 'status' => 403 ) + ); + } + if ( 'trash' === $post->post_status ) { return new WP_Error( 'rest_comment_trash_post', @@ -106,6 +119,14 @@ public function create_item_permissions_check( $request ) { ); } + if ( ! comments_open( $post->ID ) ) { + return new WP_Error( + 'rest_comment_closed', + __( 'Sorry, comments are closed for this item.' ), + array( 'status' => 403 ) + ); + } + return true; } } diff --git a/lib/load.php b/lib/load.php index 5fb67ba459d7bb..2c8a0fd0347c92 100644 --- a/lib/load.php +++ b/lib/load.php @@ -47,7 +47,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.7/rest-api.php'; // WordPress 6.8 compat. - require __DIR__ . '/compat/wordpress-6.8/rest-api.php'; + require __DIR__ . '/compat/wordpress-6.8/block-comments.php'; require __DIR__ . '/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php'; // Plugin specific code. diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index d0cf8edd0e1c67..b02b60ac43edb5 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -223,7 +223,7 @@ export default function CollabSidebar() { per_page: 100, } ); - if ( data ) { + if ( data !== undefined ) { setThreads( Array.isArray( data ) ? data : [] ); } } From 4cd5835e5383cf97cafe4eb7501ccd02eebe15cf Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 22 Oct 2024 13:18:06 +0530 Subject: [PATCH 144/159] made changes as per the feedback --- packages/editor/src/components/collab-sidebar/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index b02b60ac43edb5..396d39e5a49932 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -223,7 +223,7 @@ export default function CollabSidebar() { per_page: 100, } ); - if ( data !== undefined ) { + if ( undefined !== data ) { setThreads( Array.isArray( data ) ? data : [] ); } } From 00076ff4941aeea5564d96ea8fb380f2f6cde9d8 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 22 Oct 2024 13:21:06 +0530 Subject: [PATCH 145/159] made changes as per the feedback --- .../class-gutenberg-rest-comment-controller-6-8.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php index a5872f7735f8d2..dad653d6b0f78f 100644 --- a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php @@ -43,7 +43,7 @@ public function create_item_permissions_check( $request ) { } // Skip all the checks if comment type is not comment. - if( 'comment' !== $request['comment_type'] ) { + if ( 'comment' !== $request['comment_type'] ) { return true; } From 514853c7b497152fba3b8220433451af6bf5125c Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 22 Oct 2024 14:44:41 +0530 Subject: [PATCH 146/159] made changes as per the feedback --- lib/compat/wordpress-6.8/block-comments.php | 11 ----------- .../test/__snapshots__/build.js.snap | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/compat/wordpress-6.8/block-comments.php b/lib/compat/wordpress-6.8/block-comments.php index df205599223929..ff9f03b69aa1c3 100644 --- a/lib/compat/wordpress-6.8/block-comments.php +++ b/lib/compat/wordpress-6.8/block-comments.php @@ -1,15 +1,4 @@ array('dependencies' => array('lodash', 'wp-blob'), 'version' => 'e1840d27f0719ffa42d3'), 'fileB.js' => array('dependencies' => array('wp-token-list'), 'version' => '1e60e007d6496c9b46cf')); +" +`; + exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dependency-graph\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` " array('wp-interactivity'), 'version' => '82601013091064d44360'); " @@ -423,6 +428,19 @@ exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dependency-g ] `; +exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dependency-graph\` should produce expected output: External modules should match snapshot 2`] = ` +[ + { + "externalType": "window", + "request": [ + "wp", + "interactivity", + ], + "userRequest": "@wordpress/interactivity", + }, +] +`; + exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dynamic-dependency-graph\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` " array('wp-interactivity'), 'version' => '162f4d352c99e6c37be1'); " From 4b59a869b0babe6ef981f628faff430c68910b5e Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 22 Oct 2024 14:46:53 +0530 Subject: [PATCH 147/159] made changes as per the feedback --- .../test/__snapshots__/build.js.snap | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap index 90e724ea55c9be..8ea40b00d7c2d1 100644 --- a/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap +++ b/packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap @@ -405,11 +405,6 @@ exports[`DependencyExtractionWebpackPlugin scripts Webpack \`combine-assets\` sh ] `; -exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dependency-graph\` should produce expected output: Asset file 'assets.php' should match snapshot 1`] = ` -" array('dependencies' => array('lodash', 'wp-blob'), 'version' => 'e1840d27f0719ffa42d3'), 'fileB.js' => array('dependencies' => array('wp-token-list'), 'version' => '1e60e007d6496c9b46cf')); -" -`; - exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dependency-graph\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` " array('wp-interactivity'), 'version' => '82601013091064d44360'); " @@ -428,19 +423,6 @@ exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dependency-g ] `; -exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dependency-graph\` should produce expected output: External modules should match snapshot 2`] = ` -[ - { - "externalType": "window", - "request": [ - "wp", - "interactivity", - ], - "userRequest": "@wordpress/interactivity", - }, -] -`; - exports[`DependencyExtractionWebpackPlugin scripts Webpack \`cyclic-dynamic-dependency-graph\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = ` " array('wp-interactivity'), 'version' => '162f4d352c99e6c37be1'); " From 45f6ac9068f225a1861910b49a145e2ad6044c7a Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Tue, 22 Oct 2024 15:58:03 +0530 Subject: [PATCH 148/159] made changes as per the feedback --- .../class-gutenberg-rest-comment-controller-6-8.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php index dad653d6b0f78f..981b9dbd840319 100644 --- a/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php +++ b/lib/compat/wordpress-6.8/class-gutenberg-rest-comment-controller-6-8.php @@ -42,11 +42,6 @@ public function create_item_permissions_check( $request ) { } } - // Skip all the checks if comment type is not comment. - if ( 'comment' !== $request['comment_type'] ) { - return true; - } - // Limit who can set comment `author`, `author_ip` or `status` to anything other than the default. if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { return new WP_Error( @@ -95,7 +90,7 @@ public function create_item_permissions_check( $request ) { ); } - if ( 'draft' === $post->post_status ) { + if ( 'draft' === $post->post_status && 'comment' === $request['comment_type'] ) { return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you are not allowed to create a comment on this post.' ), @@ -119,7 +114,7 @@ public function create_item_permissions_check( $request ) { ); } - if ( ! comments_open( $post->ID ) ) { + if ( ! comments_open( $post->ID ) && 'comment' === $request['comment_type'] ) { return new WP_Error( 'rest_comment_closed', __( 'Sorry, comments are closed for this item.' ), From e42e6473bde17b772ecdb6505836df93de409194 Mon Sep 17 00:00:00 2001 From: minaldiwan <77973388+minaldiwan@users.noreply.github.com> Date: Wed, 23 Oct 2024 11:17:00 +0530 Subject: [PATCH 149/159] remove components fields css --- packages/editor/src/components/collab-sidebar/style.scss | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/style.scss b/packages/editor/src/components/collab-sidebar/style.scss index f28490d1bcacbf..2f937e3df9a25b 100644 --- a/packages/editor/src/components/collab-sidebar/style.scss +++ b/packages/editor/src/components/collab-sidebar/style.scss @@ -8,11 +8,6 @@ border: 1px solid $gray-300; background-color: $gray-100; margin-bottom: $grid-unit-20; - - .components-base-control__field { - margin-bottom: 0; - } - } &__active-thread { From 8744c83be3bb5bb1612652ff9d02833e1d2a121c Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 23 Oct 2024 11:22:16 +0530 Subject: [PATCH 150/159] made changes as per the feedback --- backport-changelog/6.8/7488.md | 3 +- lib/compat/wordpress-6.7/rest-api.php | 40 --------------------------- 2 files changed, 1 insertion(+), 42 deletions(-) diff --git a/backport-changelog/6.8/7488.md b/backport-changelog/6.8/7488.md index 4ed6a13dd7d1bd..a588bef0e01796 100644 --- a/backport-changelog/6.8/7488.md +++ b/backport-changelog/6.8/7488.md @@ -1,4 +1,3 @@ https://github.com/WordPress/wordpress-develop/pull/7488 -* https://github.com/WordPress/gutenberg/pull/60622 -* https://github.com/WordPress/gutenberg/pull/65181 \ No newline at end of file +* https://github.com/WordPress/gutenberg/pull/60622 \ No newline at end of file diff --git a/lib/compat/wordpress-6.7/rest-api.php b/lib/compat/wordpress-6.7/rest-api.php index 701c8569428b18..a54d36e18eb4ca 100644 --- a/lib/compat/wordpress-6.7/rest-api.php +++ b/lib/compat/wordpress-6.7/rest-api.php @@ -115,46 +115,6 @@ function gutenberg_override_default_rest_server() { } add_filter( 'wp_rest_server_class', 'gutenberg_override_default_rest_server', 1 ); -/** - * Updates the comment type in the REST API for WordPress version 6.7. - * - * This function is used as a filter callback for the 'rest_pre_insert_comment' hook. - * It checks if the 'comment_type' parameter is set to 'block_comment' in the REST API request, - * and if so, updates the 'comment_type' and 'comment_approved' properties of the prepared comment. - * - * @param array $prepared_comment The prepared comment data. - * @param WP_REST_Request $request The REST API request object. - * @return array The updated prepared comment data. - */ -if ( ! function_exists( 'update_comment_type_in_rest_api_6_7' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) { - function update_comment_type_in_rest_api_6_7( $prepared_comment, $request ) { - if ( ! empty( $request['comment_type'] ) && 'block_comment' === $request['comment_type'] ) { - $prepared_comment['comment_type'] = $request['comment_type']; - $prepared_comment['comment_approved'] = $request['comment_approved']; - } - - return $prepared_comment; - } - add_filter( 'rest_pre_insert_comment', 'update_comment_type_in_rest_api_6_7', 10, 2 ); -} - -/** - * Updates the comment type for avatars in the WordPress REST API. - * - * This function adds the 'block_comment' type to the list of comment types - * for which avatars should be retrieved in the WordPress REST API. - * - * @param array $comment_type The array of comment types. - * @return array The updated array of comment types. - */ -if ( ! function_exists( 'update_get_avatar_comment_type' ) && gutenberg_is_experiment_enabled( 'gutenberg-block-comment' ) ) { - function update_get_avatar_comment_type( $comment_type ) { - $comment_type[] = 'block_comment'; - return $comment_type; - } - add_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' ); -} - /** * Filters the arguments for registering a wp_global_styles post type. * Note when syncing to Core: the capabilities should be updates for `wp_global_styles` in the wp-includes/post.php. From 2e212c1ce0e565d5a496db1f8497f4756afd51ed Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 23 Oct 2024 13:55:54 +0530 Subject: [PATCH 151/159] made changes as per the feedback --- lib/compat/wordpress-6.7/rest-api.php | 1 + .../components/collab-sidebar/add-comment.js | 5 +- .../src/components/collab-sidebar/comments.js | 2 +- .../src/components/collab-sidebar/index.js | 46 ++++++++----------- .../src/components/collab-sidebar/utils.js | 2 +- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/lib/compat/wordpress-6.7/rest-api.php b/lib/compat/wordpress-6.7/rest-api.php index a54d36e18eb4ca..313367594caae0 100644 --- a/lib/compat/wordpress-6.7/rest-api.php +++ b/lib/compat/wordpress-6.7/rest-api.php @@ -115,6 +115,7 @@ function gutenberg_override_default_rest_server() { } add_filter( 'wp_rest_server_class', 'gutenberg_override_default_rest_server', 1 ); + /** * Filters the arguments for registering a wp_global_styles post type. * Note when syncing to Core: the capabilities should be updates for `wp_global_styles` in the wp-includes/post.php. diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 809ee5156d5a6a..5549ddaf4f5da6 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -110,7 +110,10 @@ export function AddComment( { disabled={ 0 === sanitizeCommentString( inputComment ).length } - onClick={ () => onSubmit( inputComment ) } + onClick={ () => { + onSubmit( inputComment ); + setInputComment( '' ); + } } /> diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index c0b4bd3d4dc38a..db9a71acb8de4e 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -343,7 +343,7 @@ function CommentHeader( { }, ]; - const moreActions = actions.filter( ( item ) => item.onClick ); + let moreActions = actions.filter( ( item ) => item.onClick ); return ( diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 396d39e5a49932..86f1e842fcfc3a 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -3,7 +3,7 @@ */ import { __ } from '@wordpress/i18n'; import { useSelect, useDispatch, resolveSelect } from '@wordpress/data'; -import { useState, useEffect, useMemo, useCallback } from '@wordpress/element'; +import { useState, useEffect, useMemo } from '@wordpress/element'; import { comment as commentIcon } from '@wordpress/icons'; import { addFilter } from '@wordpress/hooks'; import { store as noticesStore } from '@wordpress/notices'; @@ -50,20 +50,36 @@ addFilter( export default function CollabSidebar() { const { createNotice } = useDispatch( noticesStore ); const { saveEntityRecord, deleteEntityRecord } = useDispatch( coreStore ); - const { getEntityRecords, getEntityRecord } = resolveSelect( coreStore ); + const { getEntityRecord } = resolveSelect( coreStore ); // eslint-disable-next-line @wordpress/data-no-store-string-literals const { enableComplementaryArea } = useDispatch( interfaceStore ); const [ blockCommentID, setBlockCommentID ] = useState( null ); const [ showCommentBoard, setShowCommentBoard ] = useState( false ); - const [ threads, setThreads ] = useState( () => [] ); const { postId } = useSelect( ( select ) => { return { postId: select( editorStore ).getCurrentPostId(), }; }, [] ); + const threads = useSelect( + ( select ) => { + if ( ! postId ) { + return []; + } + const { getEntityRecords } = select( coreStore ); + const data = getEntityRecords( 'root', 'comment', { + post: postId, + type: 'block_comment', + status: 'any', + per_page: 100, + } ); + return Array.isArray( data ) ? data : []; + }, + [ postId ] + ); + const clientId = useSelect( ( select ) => { const { getSelectedBlockClientId } = select( blockEditorStore ); return getSelectedBlockClientId(); @@ -127,7 +143,6 @@ export default function CollabSidebar() { isDismissible: true, } ); - await fetchComments(); } else { onError(); } @@ -145,8 +160,6 @@ export default function CollabSidebar() { type: 'snackbar', isDismissible: true, } ); - - await fetchComments(); } else { onError(); } @@ -168,8 +181,6 @@ export default function CollabSidebar() { isDismissible: true, } ); - - await fetchComments(); } else { onError(); } @@ -211,30 +222,13 @@ export default function CollabSidebar() { isDismissible: true, } ); - await fetchComments(); }; - const fetchComments = useCallback( async () => { - if ( postId ) { - const data = await getEntityRecords( 'root', 'comment', { - post: postId, - type: 'block_comment', - status: 'any', - per_page: 100, - } ); - - if ( undefined !== data ) { - setThreads( Array.isArray( data ) ? data : [] ); - } - } - }, [ postId, getEntityRecords ] ); - useEffect( () => { if ( blockDetails ) { setBlockCommentID( blockDetails?.attributes.blockCommentId ); } - fetchComments(); - }, [ postId, fetchComments, clientId ] ); + }, [ postId, clientId ] ); const allBlocks = useSelect( ( select ) => { return select( blockEditorStore ).getBlocks(); diff --git a/packages/editor/src/components/collab-sidebar/utils.js b/packages/editor/src/components/collab-sidebar/utils.js index f073cc35bfbbb5..7e73344c5dc0e1 100644 --- a/packages/editor/src/components/collab-sidebar/utils.js +++ b/packages/editor/src/components/collab-sidebar/utils.js @@ -5,5 +5,5 @@ * @return {string} - The sanitized comment string. */ export function sanitizeCommentString( str ) { - return str.trim().replace( /[^\x20-\x7E]/g, '' ); + return str.trim(); } From b42cd97de23682fdc298b73bfd2e0d36b01e4934 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 23 Oct 2024 14:03:29 +0530 Subject: [PATCH 152/159] made changes as per the feedback --- packages/editor/src/components/collab-sidebar/comments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index db9a71acb8de4e..c0b4bd3d4dc38a 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -343,7 +343,7 @@ function CommentHeader( { }, ]; - let moreActions = actions.filter( ( item ) => item.onClick ); + const moreActions = actions.filter( ( item ) => item.onClick ); return ( From e446297df5167f0426ece9559b69279cd57fb7ab Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 23 Oct 2024 15:50:41 +0530 Subject: [PATCH 153/159] made changes as per the feedback --- packages/editor/src/components/collab-sidebar/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 86f1e842fcfc3a..ea8229bc74ba40 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -75,7 +75,7 @@ export default function CollabSidebar() { status: 'any', per_page: 100, } ); - return Array.isArray( data ) ? data : []; + return data ? data : []; }, [ postId ] ); From 19554694f5bd4560d5e70b09cf7e60ee23361fdb Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 23 Oct 2024 15:59:37 +0530 Subject: [PATCH 154/159] made changes as per the feedback --- packages/editor/src/components/collab-sidebar/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index ea8229bc74ba40..f3b0921bd085b7 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -51,8 +51,6 @@ export default function CollabSidebar() { const { createNotice } = useDispatch( noticesStore ); const { saveEntityRecord, deleteEntityRecord } = useDispatch( coreStore ); const { getEntityRecord } = resolveSelect( coreStore ); - - // eslint-disable-next-line @wordpress/data-no-store-string-literals const { enableComplementaryArea } = useDispatch( interfaceStore ); const [ blockCommentID, setBlockCommentID ] = useState( null ); const [ showCommentBoard, setShowCommentBoard ] = useState( false ); From b89ce854800f8df0330323f06981554f499065e6 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 23 Oct 2024 16:16:43 +0530 Subject: [PATCH 155/159] made changes as per the feedback --- packages/editor/src/components/collab-sidebar/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index f3b0921bd085b7..0f5187a2abb81b 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -54,7 +54,7 @@ export default function CollabSidebar() { const { enableComplementaryArea } = useDispatch( interfaceStore ); const [ blockCommentID, setBlockCommentID ] = useState( null ); const [ showCommentBoard, setShowCommentBoard ] = useState( false ); - + const threadsEmptyArray = []; const { postId } = useSelect( ( select ) => { return { postId: select( editorStore ).getCurrentPostId(), @@ -64,7 +64,7 @@ export default function CollabSidebar() { const threads = useSelect( ( select ) => { if ( ! postId ) { - return []; + return threadsEmptyArray; } const { getEntityRecords } = select( coreStore ); const data = getEntityRecords( 'root', 'comment', { @@ -73,7 +73,7 @@ export default function CollabSidebar() { status: 'any', per_page: 100, } ); - return data ? data : []; + return data || threadsEmptyArray; }, [ postId ] ); From 90b038b298151b91da335f4824ebb52df48720a5 Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 23 Oct 2024 16:25:49 +0530 Subject: [PATCH 156/159] define empty array outside the component --- packages/editor/src/components/collab-sidebar/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 0f5187a2abb81b..6fcdaeaf4bd722 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -22,6 +22,8 @@ import { store as editorStore } from '../../store'; import AddCommentButton from './comment-button'; import AddCommentToolbarButton from './comment-button-toolbar'; +const threadsEmptyArray = []; + const isBlockCommentExperimentEnabled = window?.__experimentalEnableBlockComment; const modifyBlockCommentAttributes = ( settings ) => { @@ -54,7 +56,6 @@ export default function CollabSidebar() { const { enableComplementaryArea } = useDispatch( interfaceStore ); const [ blockCommentID, setBlockCommentID ] = useState( null ); const [ showCommentBoard, setShowCommentBoard ] = useState( false ); - const threadsEmptyArray = []; const { postId } = useSelect( ( select ) => { return { postId: select( editorStore ).getCurrentPostId(), From 48c33511ace0907fbfbc83facde27ed718694ebf Mon Sep 17 00:00:00 2001 From: rishishah-multidots Date: Wed, 23 Oct 2024 19:21:33 +0530 Subject: [PATCH 157/159] made changes to compatible with FSE --- .../components/collab-sidebar/add-comment.js | 5 +- .../src/components/collab-sidebar/index.js | 53 ++----------------- 2 files changed, 7 insertions(+), 51 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/add-comment.js b/packages/editor/src/components/collab-sidebar/add-comment.js index 5549ddaf4f5da6..191bb23477f7bd 100644 --- a/packages/editor/src/components/collab-sidebar/add-comment.js +++ b/packages/editor/src/components/collab-sidebar/add-comment.js @@ -55,7 +55,10 @@ export function AddComment( { }; } ); - const userAvatar = currentUser?.avatar_urls[ 48 ] ?? defaultAvatar; + const userAvatar = + currentUser && currentUser.avatar_urls && currentUser.avatar_urls[ 48 ] + ? currentUser.avatar_urls[ 48 ] + : defaultAvatar; useEffect( () => { setInputComment( '' ); diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 6fcdaeaf4bd722..259176853e2aa8 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -3,7 +3,7 @@ */ import { __ } from '@wordpress/i18n'; import { useSelect, useDispatch, resolveSelect } from '@wordpress/data'; -import { useState, useEffect, useMemo } from '@wordpress/element'; +import { useState, useEffect } from '@wordpress/element'; import { comment as commentIcon } from '@wordpress/icons'; import { addFilter } from '@wordpress/hooks'; import { store as noticesStore } from '@wordpress/notices'; @@ -229,53 +229,6 @@ export default function CollabSidebar() { } }, [ postId, clientId ] ); - const allBlocks = useSelect( ( select ) => { - return select( blockEditorStore ).getBlocks(); - }, [] ); - - // Process comments to build the tree structure - const resultComments = useMemo( () => { - // Create a compare to store the references to all objects by id - const compare = {}; - const result = []; - - const filteredComments = threads.filter( - ( comment ) => comment.status !== 'trash' - ); - - // Initialize each object with an empty `reply` array - filteredComments.forEach( ( item ) => { - compare[ item.id ] = { ...item, reply: [] }; - } ); - - // Iterate over the data to build the tree structure - filteredComments.forEach( ( item ) => { - if ( item.parent === 0 ) { - // If parent is 0, it's a root item, push it to the result array - result.push( compare[ item.id ] ); - } else if ( compare[ item.parent ] ) { - // Otherwise, find its parent and push it to the parent's `reply` array - compare[ item.parent ].reply.push( compare[ item.id ] ); - } - } ); - - const filteredBlocks = allBlocks?.filter( - ( block ) => block.attributes.blockCommentId !== 0 - ); - - const blockCommentIds = filteredBlocks?.map( - ( block ) => block.attributes.blockCommentId - ); - const threadIdMap = new Map( - result?.map( ( thread ) => [ thread.id, thread ] ) - ); - const sortedThreads = blockCommentIds - .map( ( id ) => threadIdMap.get( id ) ) - .filter( ( thread ) => thread !== undefined ); - - return sortedThreads; - }, [ threads, allBlocks ] ); - // Check if the experimental flag is enabled. if ( ! isBlockCommentExperimentEnabled ) { return null; // or maybe return some message indicating no threads are available. @@ -298,13 +251,13 @@ export default function CollabSidebar() { >
Date: Wed, 23 Oct 2024 21:05:34 +0530 Subject: [PATCH 158/159] made changes to compatible with FSE --- .../src/components/collab-sidebar/index.js | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 259176853e2aa8..5037abe58f331f 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -3,7 +3,7 @@ */ import { __ } from '@wordpress/i18n'; import { useSelect, useDispatch, resolveSelect } from '@wordpress/data'; -import { useState, useEffect } from '@wordpress/element'; +import { useState, useEffect, useMemo } from '@wordpress/element'; import { comment as commentIcon } from '@wordpress/icons'; import { addFilter } from '@wordpress/hooks'; import { store as noticesStore } from '@wordpress/notices'; @@ -234,6 +234,35 @@ export default function CollabSidebar() { return null; // or maybe return some message indicating no threads are available. } + // Process comments to build the tree structure + const resultComments = useMemo( () => { + // Create a compare to store the references to all objects by id + const compare = {}; + const result = []; + + const filteredComments = threads.filter( + ( comment ) => comment.status !== 'trash' + ); + + // Initialize each object with an empty `reply` array + filteredComments.forEach( ( item ) => { + compare[ item.id ] = { ...item, reply: [] }; + } ); + + // Iterate over the data to build the tree structure + filteredComments.forEach( ( item ) => { + if ( item.parent === 0 ) { + // If parent is 0, it's a root item, push it to the result array + result.push( compare[ item.id ] ); + } else if ( compare[ item.parent ] ) { + // Otherwise, find its parent and push it to the parent's `reply` array + compare[ item.parent ].reply.push( compare[ item.id ] ); + } + } ); + + return result; + }, [ threads ] ); + return ( <> { ! blockCommentID && ( @@ -251,13 +280,13 @@ export default function CollabSidebar() { >
Date: Wed, 23 Oct 2024 21:21:13 +0530 Subject: [PATCH 159/159] fix lint error --- .../src/components/collab-sidebar/index.js | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/index.js b/packages/editor/src/components/collab-sidebar/index.js index 5037abe58f331f..482bf3d4081ed1 100644 --- a/packages/editor/src/components/collab-sidebar/index.js +++ b/packages/editor/src/components/collab-sidebar/index.js @@ -96,6 +96,35 @@ export default function CollabSidebar() { // Get the dispatch functions to save the comment and update the block attributes. const { updateBlockAttributes } = useDispatch( blockEditorStore ); + // Process comments to build the tree structure + const resultComments = useMemo( () => { + // Create a compare to store the references to all objects by id + const compare = {}; + const result = []; + + const filteredComments = threads.filter( + ( comment ) => comment.status !== 'trash' + ); + + // Initialize each object with an empty `reply` array + filteredComments.forEach( ( item ) => { + compare[ item.id ] = { ...item, reply: [] }; + } ); + + // Iterate over the data to build the tree structure + filteredComments.forEach( ( item ) => { + if ( item.parent === 0 ) { + // If parent is 0, it's a root item, push it to the result array + result.push( compare[ item.id ] ); + } else if ( compare[ item.parent ] ) { + // Otherwise, find its parent and push it to the parent's `reply` array + compare[ item.parent ].reply.push( compare[ item.id ] ); + } + } ); + + return result; + }, [ threads ] ); + const openCollabBoard = () => { setShowCommentBoard( true ); enableComplementaryArea( 'core', 'edit-post/collab-sidebar' ); @@ -234,35 +263,6 @@ export default function CollabSidebar() { return null; // or maybe return some message indicating no threads are available. } - // Process comments to build the tree structure - const resultComments = useMemo( () => { - // Create a compare to store the references to all objects by id - const compare = {}; - const result = []; - - const filteredComments = threads.filter( - ( comment ) => comment.status !== 'trash' - ); - - // Initialize each object with an empty `reply` array - filteredComments.forEach( ( item ) => { - compare[ item.id ] = { ...item, reply: [] }; - } ); - - // Iterate over the data to build the tree structure - filteredComments.forEach( ( item ) => { - if ( item.parent === 0 ) { - // If parent is 0, it's a root item, push it to the result array - result.push( compare[ item.id ] ); - } else if ( compare[ item.parent ] ) { - // Otherwise, find its parent and push it to the parent's `reply` array - compare[ item.parent ].reply.push( compare[ item.id ] ); - } - } ); - - return result; - }, [ threads ] ); - return ( <> { ! blockCommentID && (