Repository navigation
Settings: Move the Markdown settings into the settings panel #54891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1f779a6
c11ed67
256d554
b9ec108
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { getRedirectUrl } from '@automattic/jetpack-components'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Link, Stack, SwitchControl, Text } from '@wordpress/ui'; | ||
| import { useId } from 'react'; | ||
| import { PanelModuleToggle } from './panel-module-toggle'; | ||
| import styles from './styles.module.scss'; | ||
| import { useModuleOption } from './use-module-option'; | ||
| import type { ModuleSettingsProps } from './registry'; | ||
|
|
||
| /** | ||
| * The Markdown options. | ||
| * | ||
| * @param props - Component props. | ||
| * @param props.module - The module. | ||
| * @param props.disabled - Whether the site cannot use the module. | ||
| * @return The settings. | ||
| */ | ||
| export function MarkdownSettings( { module: $module, disabled }: ModuleSettingsProps ) { | ||
| const posts = useModuleOption( $module, 'wpcom_publish_posts_with_markdown' ); | ||
| const comments = useModuleOption( $module, 'wpcom_publish_comments_with_markdown' ); | ||
| const optionsDisabled = ! $module.activated || disabled; | ||
| const labelId = useId(); | ||
|
|
||
| return ( | ||
| <Stack direction="column" gap="lg"> | ||
| <Text variant="body-md"> | ||
| { __( | ||
| 'Use Markdown syntax to compose content with links, lists, and other styles. This setting enables Markdown in the Classic Editor as well as within a Classic Editor block.', | ||
| 'jetpack-my-jetpack' | ||
| ) } | ||
| </Text> | ||
| <Link | ||
| href={ getRedirectUrl( 'jetpack-support-markdown' ) } | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| { __( 'Learn more about Markdown', 'jetpack-my-jetpack' ) } | ||
| </Link> | ||
|
|
||
| <Stack | ||
| direction="row" | ||
| align="center" | ||
| justify="space-between" | ||
| className={ styles[ 'settings-panel__toggle' ] } | ||
| > | ||
| <Text variant="body-md" id={ labelId }> | ||
| { __( 'Enable Markdown', 'jetpack-my-jetpack' ) } | ||
| </Text> | ||
| <PanelModuleToggle module={ $module } labelledby={ labelId } disabled={ disabled } /> | ||
| </Stack> | ||
|
|
||
| <SwitchControl | ||
| label={ __( 'Write posts and pages with Markdown', 'jetpack-my-jetpack' ) } | ||
| checked={ posts.value } | ||
| disabled={ optionsDisabled || posts.isSaving } | ||
| onCheckedChange={ posts.setValue } | ||
| /> | ||
| <SwitchControl | ||
| label={ __( 'Let readers use Markdown in comments', 'jetpack-my-jetpack' ) } | ||
| checked={ comments.value } | ||
| disabled={ optionsDisabled || comments.isSaving } | ||
| onCheckedChange={ comments.setValue } | ||
| /> | ||
| </Stack> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import apiFetch from '@wordpress/api-fetch'; | ||
| import { MarkdownSettings } from '../markdown'; | ||
| import type { MyJetpackModule } from '../../../types'; | ||
|
|
||
| jest.mock( '@wordpress/api-fetch', () => jest.fn() ); | ||
| jest.mock( '@automattic/jetpack-shared-stores', () => ( { store: {} } ) ); | ||
| jest.mock( '@wordpress/notices', () => ( { store: 'core/notices' } ) ); | ||
| jest.mock( '@wordpress/data', () => ( { | ||
| useDispatch: () => ( { | ||
| fetchModules: jest.fn(), | ||
| createErrorNotice: jest.fn(), | ||
| createSuccessNotice: jest.fn(), | ||
| } ), | ||
| } ) ); | ||
| jest.mock( '../../../hooks/use-analytics', () => ( { | ||
| __esModule: true, | ||
| default: () => ( { recordEvent: jest.fn() } ), | ||
| } ) ); | ||
| jest.mock( '../../module-toggle', () => ( { | ||
| ModuleToggle: ( { labelledby, disabled }: { labelledby?: string; disabled?: boolean } ) => ( | ||
| <input type="checkbox" aria-labelledby={ labelledby } disabled={ disabled } /> | ||
| ), | ||
| } ) ); | ||
|
|
||
| const build = ( overrides: Partial< MyJetpackModule > = {} ) => | ||
| ( { | ||
| module: 'markdown', | ||
| name: 'Markdown', | ||
| activated: true, | ||
| override: false, | ||
| options: { | ||
| wpcom_publish_posts_with_markdown: { current_value: true }, | ||
| wpcom_publish_comments_with_markdown: { current_value: false }, | ||
| }, | ||
| ...overrides, | ||
| } ) as unknown as MyJetpackModule; | ||
|
|
||
| const postsSwitch = () => screen.getByRole( 'switch', { name: /posts and pages/ } ); | ||
| const commentsSwitch = () => screen.getByRole( 'switch', { name: /comments/ } ); | ||
|
|
||
| beforeEach( () => { | ||
| jest.clearAllMocks(); | ||
| ( apiFetch as unknown as jest.Mock ).mockResolvedValue( {} ); | ||
| } ); | ||
|
|
||
| describe( 'MarkdownSettings', () => { | ||
| it( 'shows each option with its stored value', () => { | ||
| render( <MarkdownSettings module={ build() } disabled={ false } /> ); | ||
|
|
||
| expect( postsSwitch() ).toBeChecked(); | ||
| expect( commentsSwitch() ).not.toBeChecked(); | ||
| } ); | ||
|
|
||
| it( 'saves the comments option on its own', async () => { | ||
| render( <MarkdownSettings module={ build() } disabled={ false } /> ); | ||
| await userEvent.click( commentsSwitch() ); | ||
|
|
||
| expect( apiFetch ).toHaveBeenCalledWith( { | ||
| path: '/jetpack/v4/settings', | ||
| method: 'POST', | ||
| data: { wpcom_publish_comments_with_markdown: true }, | ||
| } ); | ||
| } ); | ||
|
|
||
| it.each( [ | ||
| [ 'the module is off', { activated: false }, false ], | ||
| [ 'the site cannot use it', {}, true ], | ||
| ] )( 'locks both options when %s', ( _name, overrides, disabled ) => { | ||
| render( <MarkdownSettings module={ build( overrides ) } disabled={ disabled } /> ); | ||
|
|
||
| expect( postsSwitch() ).toHaveAttribute( 'aria-disabled', 'true' ); | ||
| expect( commentsSwitch() ).toHaveAttribute( 'aria-disabled', 'true' ); | ||
| } ); | ||
|
|
||
| it( 'locks the module switch when the site cannot use it', () => { | ||
| render( <MarkdownSettings module={ build() } disabled /> ); | ||
|
|
||
| expect( screen.getByRole( 'checkbox', { name: 'Enable Markdown' } ) ).toBeDisabled(); | ||
| } ); | ||
| } ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Significance: patch | ||
| Type: changed | ||
| Comment: Settings panel: Add the Markdown settings, behind the settings consolidation flag. | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,10 +11,14 @@ import { ModuleToggle } from 'components/module-toggle'; | |||||||||||||||||||
| import { FeaturesPointer } from 'components/pointer-card'; | ||||||||||||||||||||
| import SettingsCard from 'components/settings-card'; | ||||||||||||||||||||
| import SettingsGroup from 'components/settings-group'; | ||||||||||||||||||||
| import { isSwitchedInFeatures as _isSwitchedInFeatures } from 'lib/settings-consolidation'; | ||||||||||||||||||||
| import { showMyJetpack } from 'state/initial-state'; | ||||||||||||||||||||
| import { | ||||||||||||||||||||
| isSettingsConsolidationEnabled, | ||||||||||||||||||||
| isSwitchedInFeatures as _isSwitchedInFeatures, | ||||||||||||||||||||
| } from 'lib/settings-consolidation'; | ||||||||||||||||||||
| import { showMyJetpack, userCanManageModules } from 'state/initial-state'; | ||||||||||||||||||||
| import { getModule } from 'state/modules'; | ||||||||||||||||||||
| import { isModuleFound as _isModuleFound, getSearchTerm } from 'state/search'; | ||||||||||||||||||||
| import { MarkdownMoved } from './markdown-moved'; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Card order. | ||||||||||||||||||||
| const COMPOSING_MODULES = [ 'copy-post', 'markdown', 'latex', 'shortcodes', 'blocks' ]; | ||||||||||||||||||||
|
|
@@ -49,11 +53,12 @@ export class Composing extends Component { | |||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| render() { | ||||||||||||||||||||
| const { isModuleFound, isSwitchedInFeatures } = this.props, | ||||||||||||||||||||
| const { isMarkdownMoved, isModuleFound, isSwitchedInFeatures } = this.props, | ||||||||||||||||||||
| isFound = module => ! isSwitchedInFeatures( module ) && isModuleFound( module ), | ||||||||||||||||||||
| foundCopyPost = isFound( 'copy-post' ), | ||||||||||||||||||||
| foundLatex = isFound( 'latex' ), | ||||||||||||||||||||
| foundMarkdown = isFound( 'markdown' ), | ||||||||||||||||||||
| foundMarkdown = ! isMarkdownMoved && isFound( 'markdown' ), | ||||||||||||||||||||
| pointsToMarkdown = isMarkdownMoved && !! this.props.searchTerm && isModuleFound( 'markdown' ), | ||||||||||||||||||||
| foundShortcodes = isFound( 'shortcodes' ), | ||||||||||||||||||||
| foundBlocks = isFound( 'blocks' ), | ||||||||||||||||||||
| foundMyJetpack = this.props.isMyJetpackReachable, | ||||||||||||||||||||
|
|
@@ -64,7 +69,7 @@ export class Composing extends Component { | |||||||||||||||||||
| ) | ||||||||||||||||||||
| : []; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if ( ! foundCard && ! pointedModules.length ) { | ||||||||||||||||||||
| if ( ! foundCard && ! pointedModules.length && ! pointsToMarkdown ) { | ||||||||||||||||||||
| return null; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -262,6 +267,7 @@ export class Composing extends Component { | |||||||||||||||||||
| /> | ||||||||||||||||||||
| ); | ||||||||||||||||||||
| } ) } | ||||||||||||||||||||
| { pointsToMarkdown && <MarkdownMoved getModule={ this.props.module } /> } | ||||||||||||||||||||
| </> | ||||||||||||||||||||
| ); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -272,6 +278,8 @@ export default connect( state => { | |||||||||||||||||||
| module: module_name => getModule( state, module_name ), | ||||||||||||||||||||
| isModuleFound: module_name => _isModuleFound( state, module_name ), | ||||||||||||||||||||
| isSwitchedInFeatures: module_name => _isSwitchedInFeatures( state, module_name ), | ||||||||||||||||||||
| // The panel needs Features, which users who cannot manage modules lack. | ||||||||||||||||||||
| isMarkdownMoved: isSettingsConsolidationEnabled() && userCanManageModules( state ), | ||||||||||||||||||||
|
Comment on lines
+281
to
+282
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] This gate is written twice, here and in jetpack/projects/plugins/jetpack/_inc/client/lib/settings-consolidation/index.js Lines 64 to 71 in 5d39401
isSwitchedInFeatures(), say isInSettingsPanel( state, module ), would hold the gate and its reason in one place. The Comments panel (JETPACK-3720) needs it next, so it can wait for that PR.
Generated by Claude.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||
| isMyJetpackReachable: showMyJetpack( state ), | ||||||||||||||||||||
| searchTerm: getSearchTerm( state ), | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import PointerCard from 'components/pointer-card'; | ||
|
|
||
| /** | ||
| * Search result that sends the Markdown settings to the panel in Features. | ||
| * | ||
| * @param {object} props - Component props. | ||
| * @param {Function} props.getModule - Reads a module by slug. | ||
| * @return {import('react').ReactNode} The card. | ||
| */ | ||
| export const MarkdownMoved = ( { getModule } ) => ( | ||
| <PointerCard | ||
| header={ __( 'Markdown', 'jetpack' ) } | ||
| href={ getModule( 'markdown' )?.configure_url } | ||
| linkText={ __( 'Open the Markdown settings', 'jetpack' ) } | ||
| > | ||
| <p>{ __( 'The Markdown settings are now in Features, in My Jetpack.', 'jetpack' ) }</p> | ||
| </PointerCard> | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] One row of the coupling table in the description doesn't match the server: "Turning the module on keeps the stored option values (they also survive off and on)". This switch POSTs
jetpack/v4/module/markdown/active.register_endpoints()loads the module endpoints file on every REST request (jetpack/projects/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php
Line 31 in 5d39401
jetpack/projects/plugins/jetpack/_inc/lib/class.core-rest-api-endpoints.php
Line 62 in 5d39401
jetpack_activate_module_markdownto setwpcom_publish_posts_with_markdownto true (jetpack/projects/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php
Lines 1834 to 1843 in 5d39401
Related, and older than this PR:
Jetpack::plugin_upgrade()turns the posts option back on at every Jetpack version change, whether or not the module is on (jetpack/projects/plugins/jetpack/class.jetpack.php
Lines 502 to 505 in 5d39401
Generated by Claude.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, thanks. I fixed the table row in the description: activation sets the posts option, and only the comments option survives off and on. Filed the
plugin_upgrade()reset as a follow-up: JETPACK-3729.