From d6c8b6fb3ffc8195cd1abcbb93fe8b7228d9cfea Mon Sep 17 00:00:00 2001 From: Tony Arcangelini Date: Tue, 13 Oct 2026 13:06:39 +0200 Subject: [PATCH 01/20] Comments: Add a settings screen with a live form preview --- pnpm-lock.yaml | 145 +++---- projects/packages/comments/.gitattributes | 3 + projects/packages/comments/.gitignore | 2 + projects/packages/comments/.phan/config.php | 1 + .../changelog/update-comments-settings-screen | 4 + projects/packages/comments/composer.json | 4 +- projects/packages/comments/eslint.config.mjs | 23 +- projects/packages/comments/package.json | 44 +- .../comments/packages/init/package.json | 12 + .../comments/packages/init/src/index.ts | 12 + .../comments/routes/settings/package.json | 24 ++ .../comments/routes/settings/stage.tsx | 12 + .../packages/comments/src/settings/api.ts | 21 + .../src/settings/class-settings-page.php | 392 ++++++++++++++++++ .../comments/src/settings/components.tsx | 95 +++++ .../settings/preview/comment-form-preview.tsx | 261 ++++++++++++ .../comments/src/settings/preview/shadow.tsx | 37 ++ .../comments/src/settings/preview/styles.ts | 117 ++++++ .../comments/src/settings/script-data.ts | 12 + .../sections/comment-form-section.tsx | 167 ++++++++ .../settings/sections/features-section.tsx | 100 +++++ .../src/settings/sections/related-section.tsx | 100 +++++ .../comments/src/settings/settings-screen.tsx | 70 ++++ .../packages/comments/src/settings/style.scss | 48 +++ .../packages/comments/src/settings/types.ts | 47 +++ .../comments/src/settings/use-settings.ts | 72 ++++ projects/packages/comments/tsconfig.json | 3 +- .../packages/comments/tsconfig.settings.json | 4 + .../_inc/client/discussion/comments.jsx | 28 +- .../jetpack/_inc/client/discussion/index.jsx | 38 +- .../changelog/update-comments-settings-screen | 4 + projects/plugins/jetpack/load-jetpack.php | 5 + .../changelog/update-comments-settings-screen | 5 + .../changelog/update-comments-settings-screen | 5 + 34 files changed, 1784 insertions(+), 133 deletions(-) create mode 100644 projects/packages/comments/changelog/update-comments-settings-screen create mode 100644 projects/packages/comments/packages/init/package.json create mode 100644 projects/packages/comments/packages/init/src/index.ts create mode 100644 projects/packages/comments/routes/settings/package.json create mode 100644 projects/packages/comments/routes/settings/stage.tsx create mode 100644 projects/packages/comments/src/settings/api.ts create mode 100644 projects/packages/comments/src/settings/class-settings-page.php create mode 100644 projects/packages/comments/src/settings/components.tsx create mode 100644 projects/packages/comments/src/settings/preview/comment-form-preview.tsx create mode 100644 projects/packages/comments/src/settings/preview/shadow.tsx create mode 100644 projects/packages/comments/src/settings/preview/styles.ts create mode 100644 projects/packages/comments/src/settings/script-data.ts create mode 100644 projects/packages/comments/src/settings/sections/comment-form-section.tsx create mode 100644 projects/packages/comments/src/settings/sections/features-section.tsx create mode 100644 projects/packages/comments/src/settings/sections/related-section.tsx create mode 100644 projects/packages/comments/src/settings/settings-screen.tsx create mode 100644 projects/packages/comments/src/settings/style.scss create mode 100644 projects/packages/comments/src/settings/types.ts create mode 100644 projects/packages/comments/src/settings/use-settings.ts create mode 100644 projects/packages/comments/tsconfig.settings.json create mode 100644 projects/plugins/jetpack/changelog/update-comments-settings-screen create mode 100644 projects/plugins/mu-wpcom-plugin/changelog/update-comments-settings-screen create mode 100644 projects/plugins/wpcomsh/changelog/update-comments-settings-screen diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2efea0aa4984..96f9abb0805c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2343,36 +2343,69 @@ importers: projects/packages/comments: dependencies: + '@automattic/jetpack-base-styles': + specifier: workspace:* + version: link:../../js-packages/base-styles + '@automattic/jetpack-components': + specifier: workspace:* + version: link:../../js-packages/components + '@automattic/jetpack-script-data': + specifier: workspace:* + version: link:../../js-packages/script-data + '@jetpack-comments/init': + specifier: link:packages/init + version: link:packages/init '@preact/signals': specifier: 1.2.2 version: 1.2.2(preact@10.28.2) + '@wordpress/api-fetch': + specifier: 7.57.0 + version: 7.57.0 '@wordpress/block-editor': specifier: 18.1.0 - version: 18.1.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + version: 18.1.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/block-library': specifier: 11.2.0 - version: 11.2.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + version: 11.2.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/blocks': specifier: 16.2.0 - version: 16.2.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + version: 16.2.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/components': specifier: 42.0.0 - version: 42.0.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + version: 42.0.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/data': specifier: 10.57.0 - version: 10.57.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + version: 10.57.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/dataviews': + specifier: 20.0.0 + version: 20.0.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/element': specifier: 9.0.0 - version: 9.0.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + version: 9.0.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/format-library': specifier: 5.57.0 - version: 5.57.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + version: 5.57.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/i18n': specifier: 6.30.0 version: 6.30.0 + '@wordpress/icons': + specifier: 18.0.0 + version: 18.0.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/notices': + specifier: 5.57.0 + version: 5.57.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/rich-text': specifier: 7.57.0 - version: 7.57.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + version: 7.57.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/route': + specifier: 0.23.0 + version: 0.23.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/theme': + specifier: 2.3.0 + version: 2.3.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/ui': + specifier: 0.24.0 + version: 0.24.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) clsx: specifier: 2.1.1 version: 2.1.1 @@ -2389,6 +2422,9 @@ importers: '@automattic/jetpack-webpack-config': specifier: workspace:* version: link:../../js-packages/webpack-config + '@automattic/jetpack-wp-build-polyfills': + specifier: workspace:* + version: link:../wp-build-polyfills '@babel/core': specifier: 7.29.7 version: 7.29.7 @@ -2401,6 +2437,9 @@ importers: '@types/react': specifier: 18.3.31 version: 18.3.31 + '@types/react-dom': + specifier: 18.3.7 + version: 18.3.7(@types/react@18.3.31) '@typescript/native-preview': specifier: 7.0.0-dev.20260707.2 version: 7.0.0-dev.20260707.2 @@ -2410,6 +2449,9 @@ importers: '@wordpress/browserslist-config': specifier: 6.57.0 version: 6.57.0 + '@wordpress/build': + specifier: 0.25.0 + version: 0.25.0(patch_hash=151ae1c0c3786b862724512e48d8cfcb4b34faf4cac98e352a450abd4741e022)(@wordpress/theme@2.3.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)) autoprefixer: specifier: 10.4.20 version: 10.4.20(postcss@8.5.23) @@ -2419,6 +2461,9 @@ importers: browserslist: specifier: ^4.24.0 version: 4.28.9 + concurrently: + specifier: 10.0.5 + version: 10.0.5 postcss: specifier: 8.5.23 version: 8.5.23 @@ -23780,72 +23825,6 @@ snapshots: - supports-color - vite - '@wordpress/block-library@11.2.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': - dependencies: - '@arraypress/waveform-player': 1.29.1 - '@wordpress/a11y': 4.57.0 - '@wordpress/api-fetch': 7.57.0 - '@wordpress/autop': 4.57.0 - '@wordpress/base-styles': 13.3.0 - '@wordpress/blob': 4.57.0 - '@wordpress/block-editor': 18.1.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/blocks': 16.2.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/components': 42.0.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/compose': 8.10.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/core-data': 8.2.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/data': 10.57.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/date': 5.57.0 - '@wordpress/deprecated': 4.57.0 - '@wordpress/dom': 4.57.0 - '@wordpress/element': 9.0.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/escape-html': 3.57.0 - '@wordpress/global-styles-engine': 1.24.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/hooks': 4.57.0 - '@wordpress/html-entities': 4.57.0 - '@wordpress/i18n': 6.30.0 - '@wordpress/icons': 18.0.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/interactivity': 7.0.0 - '@wordpress/interactivity-router': 2.57.0 - '@wordpress/kebab-case': 1.4.0 - '@wordpress/keycodes': 4.57.0(@types/react@18.3.31) - '@wordpress/latex-to-mathml': 1.25.0 - '@wordpress/notices': 5.57.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/patterns': 2.57.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/primitives': 4.57.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/private-apis': 2.0.0 - '@wordpress/react-inert-value': 1.0.0(@types/react@18.3.31)(react@18.3.1) - '@wordpress/rich-text': 7.57.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/server-side-render': 6.33.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/shortcode': 4.57.0 - '@wordpress/ui': 0.24.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/upload-media': 0.42.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/url': 4.57.0 - '@wordpress/wordcount': 4.57.0 - change-case: 4.1.2 - clsx: 2.1.1 - colord: 2.10.0 - fast-average-color: 9.6.0 - fast-deep-equal: 3.1.3 - html-react-parser: 5.2.17(@types/react@18.3.31)(react@18.3.1) - memize: 2.1.1 - react: 18.3.1 - react-dom: 18.3.1(react@18.3.1) - remove-accents: 0.5.0 - uuid: 14.0.3 - optionalDependencies: - '@types/react': 18.3.31 - transitivePeerDependencies: - - '@date-fns/tz' - - '@emotion/is-prop-valid' - - '@types/react-dom' - - esbuild - - lightningcss - - postcss - - preact-render-to-string - - stylelint - - supports-color - - vite - '@wordpress/block-library@11.2.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': dependencies: '@arraypress/waveform-player': 1.29.1 @@ -27115,22 +27094,22 @@ snapshots: - supports-color - vite - '@wordpress/format-library@5.57.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': + '@wordpress/format-library@5.57.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)': dependencies: '@wordpress/a11y': 4.57.0 '@wordpress/base-styles': 13.3.0 - '@wordpress/block-editor': 18.1.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/components': 42.0.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/compose': 8.10.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/data': 10.57.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/element': 9.0.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/block-editor': 18.1.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/components': 42.0.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/compose': 8.10.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/data': 10.57.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/element': 9.0.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/html-entities': 4.57.0 '@wordpress/i18n': 6.30.0 - '@wordpress/icons': 18.0.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/icons': 18.0.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/latex-to-mathml': 1.25.0 '@wordpress/private-apis': 2.0.0 - '@wordpress/rich-text': 7.57.0(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - '@wordpress/ui': 0.24.0(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/rich-text': 7.57.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) + '@wordpress/ui': 0.24.0(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(postcss@8.5.23)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@wordpress/url': 4.57.0 react: 18.3.1 react-dom: 18.3.1(react@18.3.1) diff --git a/projects/packages/comments/.gitattributes b/projects/packages/comments/.gitattributes index fee265d785c7..43036461f784 100644 --- a/projects/packages/comments/.gitattributes +++ b/projects/packages/comments/.gitattributes @@ -9,3 +9,6 @@ src/**/*.scss production-exclude src/**/*.tsx production-exclude src/**/*.ts production-exclude /tools/** production-exclude +packages/** production-exclude +routes/** production-exclude +/tsconfig.settings.json production-exclude diff --git a/projects/packages/comments/.gitignore b/projects/packages/comments/.gitignore index f8206b742cb3..63aadb065b42 100644 --- a/projects/packages/comments/.gitignore +++ b/projects/packages/comments/.gitignore @@ -2,3 +2,5 @@ vendor/ node_modules/ build/ .cache/ +build-module +/routes/**/.content-entry.js diff --git a/projects/packages/comments/.phan/config.php b/projects/packages/comments/.phan/config.php index 9aaa272fc971..085fde222c06 100644 --- a/projects/packages/comments/.phan/config.php +++ b/projects/packages/comments/.phan/config.php @@ -16,6 +16,7 @@ '+stubs' => array( 'wpcom' ), 'parse_file_list' => array( __DIR__ . '/../../../plugins/jetpack/_inc/lib/core-api/load-wpcom-endpoints.php', // function wpcom_rest_api_v2_load_plugin + __DIR__ . '/../../../plugins/jetpack/src/class-jetpack-modules-overrides.php', // class Jetpack_Modules_Overrides ), ) ); diff --git a/projects/packages/comments/changelog/update-comments-settings-screen b/projects/packages/comments/changelog/update-comments-settings-screen new file mode 100644 index 000000000000..3034cbac8a4d --- /dev/null +++ b/projects/packages/comments/changelog/update-comments-settings-screen @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add a Jetpack > Comments settings screen with a live preview of the comment form. diff --git a/projects/packages/comments/composer.json b/projects/packages/comments/composer.json index 21fad3184587..4bfa01bc6c38 100644 --- a/projects/packages/comments/composer.json +++ b/projects/packages/comments/composer.json @@ -5,11 +5,13 @@ "license": "GPL-2.0-or-later", "require": { "php": ">=7.4", + "automattic/jetpack-admin-ui": "@dev", "automattic/jetpack-assets": "@dev", "automattic/jetpack-image-cdn": "@dev", "automattic/jetpack-connection": "@dev", "automattic/jetpack-ip": "@dev", - "automattic/jetpack-status": "@dev" + "automattic/jetpack-status": "@dev", + "automattic/jetpack-wp-build-polyfills": "@dev" }, "suggest": { "automattic/jetpack-autoloader": "Allow for better interoperability with other plugins that use this package." diff --git a/projects/packages/comments/eslint.config.mjs b/projects/packages/comments/eslint.config.mjs index 2d053148a5d2..1426e70c35f2 100644 --- a/projects/packages/comments/eslint.config.mjs +++ b/projects/packages/comments/eslint.config.mjs @@ -1,11 +1,18 @@ import { makeBaseConfig, defineConfig } from 'jetpack-js-tools/eslintrc/base.mjs'; import makeReactConfig from 'jetpack-js-tools/eslintrc/react.mjs'; -// The form is Preact; only the block editor is React. React's rules, such as its camelCase -// SVG attributes, would misread the Preact code. -export default defineConfig( makeBaseConfig( import.meta.url, { react: false } ), { - files: [ 'src/editor/**' ], - extends: [ makeReactConfig( import.meta.url ) ], - // The editor speaks core's strings, translated by core's language packs under the default domain. - rules: { '@wordpress/i18n-text-domain': 'off' }, -} ); +// The form is Preact; only the block editor and the settings screen are React. React's rules, +// such as its camelCase SVG attributes, would misread the Preact code. +export default defineConfig( + makeBaseConfig( import.meta.url, { react: false } ), + { + files: [ 'src/editor/**' ], + extends: [ makeReactConfig( import.meta.url ) ], + // The editor speaks core's strings, translated by core's language packs under the default domain. + rules: { '@wordpress/i18n-text-domain': 'off' }, + }, + { + files: [ 'src/settings/**', 'routes/**' ], + extends: [ makeReactConfig( import.meta.url ) ], + } +); diff --git a/projects/packages/comments/package.json b/projects/packages/comments/package.json index 49ea86fa75aa..1fe8e42f06be 100644 --- a/projects/packages/comments/package.json +++ b/projects/packages/comments/package.json @@ -16,26 +16,42 @@ "author": "Automattic", "type": "module", "scripts": { - "build": "pnpm run clean && pnpm run build-js", + "build": "pnpm run clean && concurrently 'pnpm:build-js' 'pnpm:build:wp-build' && pnpm run build:stamp-textdomains && pnpm run build:boot-asset", + "build:boot-asset": "provide-boot-asset-file", + "build:stamp-textdomains": "stamp-textdomains", + "build:strip-unminified-prod": "strip-unminified-prod", + "build:wp-build": "wp-build", "build-js": "webpack --config webpack.config.js", - "build-production": "pnpm run clean && pnpm run build-production-js && pnpm run validate", + "build-production": "pnpm run clean && concurrently 'pnpm:build-production-js' 'pnpm:build:wp-build' && pnpm run build:stamp-textdomains && pnpm run build:strip-unminified-prod && pnpm run build:boot-asset && pnpm run validate", "build-production-js": "NODE_ENV=production BABEL_ENV=production pnpm run build-js", "clean": "rm -rf build/", - "typecheck": "tsgo --noEmit", - "validate": "pnpm exec validate-es build/", - "watch": "pnpm run build-js --watch" + "typecheck": "tsgo --noEmit && tsgo --noEmit -p tsconfig.settings.json", + "validate": "pnpm exec validate-es --no-error-on-unmatched-pattern build/", + "watch": "concurrently 'pnpm:build-js --watch' 'pnpm:watch:wp-build'", + "watch:wp-build": "wp-build --watch" }, "dependencies": { + "@automattic/jetpack-base-styles": "workspace:*", + "@automattic/jetpack-components": "workspace:*", + "@automattic/jetpack-script-data": "workspace:*", + "@jetpack-comments/init": "link:packages/init", "@preact/signals": "1.2.2", + "@wordpress/api-fetch": "7.57.0", "@wordpress/block-editor": "18.1.0", "@wordpress/block-library": "11.2.0", "@wordpress/blocks": "16.2.0", "@wordpress/components": "42.0.0", "@wordpress/data": "10.57.0", + "@wordpress/dataviews": "20.0.0", "@wordpress/element": "9.0.0", "@wordpress/format-library": "5.57.0", "@wordpress/i18n": "6.30.0", + "@wordpress/icons": "18.0.0", + "@wordpress/notices": "5.57.0", "@wordpress/rich-text": "7.57.0", + "@wordpress/route": "0.23.0", + "@wordpress/theme": "2.3.0", + "@wordpress/ui": "0.24.0", "clsx": "2.1.1", "preact": "10.28.2", "react": "18.3.1", @@ -43,21 +59,39 @@ }, "devDependencies": { "@automattic/jetpack-webpack-config": "workspace:*", + "@automattic/jetpack-wp-build-polyfills": "workspace:*", "@babel/core": "7.29.7", "@babel/plugin-transform-react-jsx": "7.29.7", "@babel/runtime": "7.29.7", "@types/react": "18.3.31", + "@types/react-dom": "18.3.7", "@typescript/native-preview": "7.0.0-dev.20260707.2", "@wordpress/base-styles": "13.3.0", "@wordpress/browserslist-config": "6.57.0", + "@wordpress/build": "0.25.0", "autoprefixer": "10.4.20", "babel-plugin-transform-rename-properties": "0.1.0", "browserslist": "^4.24.0", + "concurrently": "10.0.5", "postcss": "8.5.23", "postcss-loader": "8.2.0", "sass-embedded": "1.97.3", "sass-loader": "16.0.5", "webpack": "5.111.1", "webpack-cli": "7.2.1" + }, + "wpPlugin": { + "name": "jetpack_comments", + "scriptGlobal": "jetpackComments", + "packageNamespace": "jetpack-comments", + "handlePrefix": "jetpack-comments", + "pages": [ + { + "id": "jetpack-comments-settings", + "init": [ + "@jetpack-comments/init" + ] + } + ] } } diff --git a/projects/packages/comments/packages/init/package.json b/projects/packages/comments/packages/init/package.json new file mode 100644 index 000000000000..b143d1bb5c95 --- /dev/null +++ b/projects/packages/comments/packages/init/package.json @@ -0,0 +1,12 @@ +{ + "private": true, + "name": "@automattic/jetpack-comments-init", + "version": "0.1.0", + "type": "module", + "wpScript": true, + "module": "build-module/index.mjs", + "wpScriptModuleExports": "./build-module/index.mjs", + "dependencies": { + "@automattic/jetpack-wp-build-polyfills": "workspace:*" + } +} diff --git a/projects/packages/comments/packages/init/src/index.ts b/projects/packages/comments/packages/init/src/index.ts new file mode 100644 index 000000000000..fe674d673634 --- /dev/null +++ b/projects/packages/comments/packages/init/src/index.ts @@ -0,0 +1,12 @@ +import { loadI18nCatalogs } from '@automattic/jetpack-wp-build-polyfills/src/js/load-i18n-catalogs'; + +/** + * Provide the Jetpack config and load the translation catalogs before the app renders. + */ +export async function init(): Promise< void > { + // wp-build has no `jetpackConfig` external, so @automattic/jetpack-config reads this global. + ( globalThis as typeof globalThis & { jetpackConfig?: object } ).jetpackConfig = { + consumer_slug: 'jetpack-comments', + }; + await loadI18nCatalogs( 'jetpack-comments', import.meta.url ); +} diff --git a/projects/packages/comments/routes/settings/package.json b/projects/packages/comments/routes/settings/package.json new file mode 100644 index 000000000000..b294ca04dde8 --- /dev/null +++ b/projects/packages/comments/routes/settings/package.json @@ -0,0 +1,24 @@ +{ + "version": "1.0.0", + "private": true, + "dependencies": { + "@automattic/jetpack-components": "workspace:*", + "@automattic/jetpack-script-data": "workspace:*", + "@types/react": "18.3.31", + "@wordpress/api-fetch": "7.57.0", + "@wordpress/components": "42.0.0", + "@wordpress/data": "10.57.0", + "@wordpress/dataviews": "20.0.0", + "@wordpress/element": "9.0.0", + "@wordpress/i18n": "6.30.0", + "@wordpress/icons": "18.0.0", + "@wordpress/notices": "5.57.0", + "@wordpress/route": "0.23.0", + "@wordpress/theme": "2.3.0", + "@wordpress/ui": "0.24.0" + }, + "route": { + "path": "/", + "page": "jetpack-comments-settings" + } +} diff --git a/projects/packages/comments/routes/settings/stage.tsx b/projects/packages/comments/routes/settings/stage.tsx new file mode 100644 index 000000000000..851134fd8b7f --- /dev/null +++ b/projects/packages/comments/routes/settings/stage.tsx @@ -0,0 +1,12 @@ +/* @jsxImportSource react */ +import { SettingsScreen } from '../../src/settings/settings-screen'; +import '../../src/settings/style.scss'; + +/** + * wp-build stage for Jetpack > Comments. + * + * @return The settings screen. + */ +const Stage = () => ; + +export { Stage as stage }; diff --git a/projects/packages/comments/src/settings/api.ts b/projects/packages/comments/src/settings/api.ts new file mode 100644 index 000000000000..dcdd3ca4b284 --- /dev/null +++ b/projects/packages/comments/src/settings/api.ts @@ -0,0 +1,21 @@ +import apiFetch from '@wordpress/api-fetch'; +import type { Settings } from './types'; + +/** + * Save settings through Jetpack's settings route, which turns modules on and off by slug too. + * + * @param updates - Settings to change. + * @return The route's response. + */ +export function saveSettings( updates: Partial< Settings > ) { + return apiFetch( { path: '/jetpack/v4/settings', method: 'POST', data: updates } ); +} + +/** + * Read every setting Jetpack's settings route knows, module states included. + * + * @return Settings by key. + */ +export function fetchSettings() { + return apiFetch< Partial< Settings > >( { path: '/jetpack/v4/settings' } ); +} diff --git a/projects/packages/comments/src/settings/class-settings-page.php b/projects/packages/comments/src/settings/class-settings-page.php new file mode 100644 index 000000000000..42c02f74de5a --- /dev/null +++ b/projects/packages/comments/src/settings/class-settings-page.php @@ -0,0 +1,392 @@ + Comments, the comment settings screen. + * + * @package automattic/jetpack-comments + */ + +namespace Automattic\Jetpack\Comments; + +use Automattic\Jetpack\Admin_UI\Admin_Menu; +use Automattic\Jetpack\Assets; +use Automattic\Jetpack\Connection\Manager as Connection_Manager; +use Automattic\Jetpack\Modules; +use Automattic\Jetpack\Status\Host; +use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills; +use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Screen_Id; + +/** + * Registers the screen and hands its wp-build app what only PHP knows. + */ +class Settings_Page { + + /** + * Menu slug. + */ + const SLUG = 'jetpack-comments'; + + /** + * The wp-build page ID, which wp-build's generated enqueue check compares the screen ID against. + */ + const WP_BUILD_PAGE = 'jetpack-comments-settings'; + + private const RENDER_FUNCTION = 'jetpack_comments_jetpack_comments_settings_wp_admin_render_page'; + private const MODULES_FUNCTION = 'jetpack_comments_register_script_modules'; + private const INTERCEPT_FUNCTION = 'jetpack_comments_jetpack_comments_settings_intercept_render'; + + /** + * Modules the screen toggles besides Comments itself, none of which need the Comments module. + */ + const FEATURE_MODULES = array( 'gravatar-hovercards', 'markdown', 'comment-likes' ); + + /** + * The real screen ID while it is aliased. + * + * @var string|null + */ + private static $original_screen_id = null; + + /** + * Hook the screen up. Safe to call more than once. + * + * @return void + */ + public static function init() { + if ( has_action( 'admin_menu', array( __CLASS__, 'maybe_load' ) ) ) { + return; + } + + // Priority 1: the build must load before the menu registers. + add_action( 'admin_menu', array( __CLASS__, 'maybe_load' ), 1 ); + // Priority 999: Admin_Menu processes its queue at 1000. + add_action( 'admin_menu', array( __CLASS__, 'add_menu' ), 999 ); + // Priority 20: past the module's own default. + add_filter( 'jetpack_module_configuration_url_comments', array( __CLASS__, 'configuration_url' ), 20 ); + } + + /** + * Whether this site gets the screen: it runs Jetpack Comments, and is connected as the Comments module requires. + * + * @return bool + */ + public static function is_available() { + return Comments::is_enabled() && ( new Connection_Manager() )->is_connected(); + } + + /** + * Send the module's Configure links here. + * + * @param string $url Default configuration URL. + * @return string + */ + public static function configuration_url( $url ) { + return self::is_available() ? admin_url( 'admin.php?page=' . self::SLUG ) : $url; + } + + /** + * Add the screen to the Jetpack menu, shown whether or not the Comments module is on. + * + * @return void + */ + public static function add_menu() { + if ( ! self::is_available() ) { + return; + } + + $title = __( 'Comments', 'jetpack-comments' ); + + // Atomic builds its own Jetpack menu, which Admin_Menu does not manage. + if ( ( new Host() )->is_woa_site() ) { + add_submenu_page( 'jetpack', $title, $title, 'manage_options', self::SLUG, array( __CLASS__, 'render' ) ); + return; + } + + Admin_Menu::add_menu( $title, $title, 'manage_options', self::SLUG, array( __CLASS__, 'render' ), null, array( 'key' => self::SLUG ) ); + } + + /** + * Render the app, or say why it is missing. + * + * @return void + */ + public static function render() { + if ( function_exists( self::RENDER_FUNCTION ) ) { + call_user_func( self::RENDER_FUNCTION ); + return; + } + + printf( + '

%s

', + esc_html__( 'The Comments settings could not be loaded. Rebuild the jetpack-comments package and reload the page.', 'jetpack-comments' ) + ); + } + + /** + * Load the wp-build app when this request is for the screen. + * + * @return void + */ + public static function maybe_load() { + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only decides which screen to render. + $page = isset( $_GET['page'] ) && is_string( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; + + if ( ! is_admin() || self::SLUG !== $page || ! self::is_available() ) { + return; + } + + $build_index = dirname( __DIR__, 2 ) . '/build/build.php'; + if ( ! file_exists( $build_index ) ) { + return; + } + + $require = static function () use ( $build_index ) { + require_once $build_index; + }; + + // An older wp-build-polyfills under the Jetpack autoloader may predate load_with_alias(). + if ( method_exists( WP_Build_Screen_Id::class, 'load_with_alias' ) ) { + WP_Build_Screen_Id::load_with_alias( array( __CLASS__, 'alias_screen_id' ), array( __CLASS__, 'restore_screen_id' ), $require ); + } else { + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id' ) ); + $require(); + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id' ) ); + } + + // A stale or partial build can't render the page, so don't swap core's scripts for it. + if ( ! function_exists( self::RENDER_FUNCTION ) ) { + remove_action( 'admin_enqueue_scripts', array( __CLASS__, 'alias_screen_id' ) ); + remove_action( 'admin_enqueue_scripts', array( __CLASS__, 'restore_screen_id' ) ); + return; + } + + // The generated file registers these on `wp_default_scripts`, which has already fired by `admin_menu`. + if ( function_exists( self::MODULES_FUNCTION ) ) { + call_user_func( self::MODULES_FUNCTION ); + } + + remove_action( 'admin_init', self::INTERCEPT_FUNCTION ); + + WP_Build_Polyfills::register( + 'jetpack-comments', + array_merge( WP_Build_Polyfills::SCRIPT_HANDLES, WP_Build_Polyfills::MODULE_IDS ) + ); + + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_i18n_loader' ) ); + add_action( 'admin_head', array( __CLASS__, 'print_theme_assets' ) ); + add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'add_script_data' ) ); + } + + /** + * Show wp-build's generated enqueue check the page ID it expects. + * + * @return void + */ + public static function alias_screen_id() { + $screen = get_current_screen(); + if ( ! $screen ) { + return; + } + + self::$original_screen_id = $screen->id; + $screen->id = self::WP_BUILD_PAGE; + } + + /** + * Put the real screen ID back for everything after the generated check. + * + * @return void + */ + public static function restore_screen_id() { + $screen = get_current_screen(); + if ( ! $screen || null === self::$original_screen_id ) { + return; + } + + $screen->id = self::$original_screen_id; + self::$original_screen_id = null; + } + + /** + * Jetpack's i18n loader is registered everywhere but only enqueued on demand; the init module needs it. + * + * @return void + */ + public static function enqueue_i18n_loader() { + if ( wp_script_is( 'wp-jp-i18n-loader', 'registered' ) ) { + wp_enqueue_script( 'wp-jp-i18n-loader' ); + } + } + + /** + * Print the theme's fonts and preset variables for the preview. + * + * The preview's shadow root inherits custom properties from here, but browsers ignore an @font-face declared inside one. + * + * @return void + */ + public static function print_theme_assets() { + if ( ! wp_theme_has_theme_json() ) { + return; + } + + if ( function_exists( 'wp_print_font_faces' ) ) { + wp_print_font_faces(); + } + + printf( "\n", wp_strip_all_tags( wp_get_global_stylesheet( array( 'variables' ) ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS, stripped of tags. + } + + /** + * Hand the first render the settings, plus what the preview needs to look like the site. + * + * @param array $data Script data. + * @return array + */ + public static function add_script_data( $data ) { + $data = is_array( $data ) ? $data : array(); + + if ( ! current_user_can( 'manage_options' ) ) { + return $data; + } + + $modules = new Modules(); + $available = array_values( array_filter( self::FEATURE_MODULES, array( $modules, 'is_module' ) ) ); + $css = dirname( __DIR__, 2 ) . '/build/comments.css'; + + $data['jetpack_comments'] = array( + 'settings' => array( + 'comments' => $modules->is_active( 'comments' ), + 'highlander_comment_form_prompt' => (string) get_option( 'highlander_comment_form_prompt', '' ), + 'enable_blocks_comments' => (bool) get_option( 'enable_blocks_comments', true ), + 'gravatar-hovercards' => $modules->is_active( 'gravatar-hovercards' ), + 'comment-likes' => $modules->is_active( 'comment-likes' ), + 'markdown' => $modules->is_active( 'markdown' ), + 'wpcom_publish_comments_with_markdown' => (bool) get_option( 'wpcom_publish_comments_with_markdown' ), + ), + 'modules' => $available, + 'overrides' => self::module_overrides(), + 'discussion' => array( + 'url' => admin_url( 'options-discussion.php' ), + 'requireNameEmail' => (bool) get_option( 'require_name_email' ), + 'commentRegistration' => (bool) get_option( 'comment_registration' ), + 'showAvatars' => (bool) get_option( 'show_avatars' ), + 'cookiesOptIn' => (bool) get_option( 'show_comments_cookies_opt_in' ), + ), + 'newsletter' => array( + 'active' => $modules->is_active( 'subscriptions' ), + // The page's router reads its route, hash included, only from `p`. + 'url' => admin_url( 'admin.php?page=jetpack-newsletter&p=' . rawurlencode( '/?tab=settings#subscriptions' ) ), + // Simple stores an option's "off" as an empty string, Jetpack as 0. + 'subscribeBlog' => ! in_array( get_option( 'stb_enabled', 1 ), array( '', '0', 0 ), true ), + 'subscribeReplies' => ! in_array( get_option( 'stc_enabled', 1 ), array( '', '0', 0 ), true ), + ), + 'preview' => array( + 'styleUrl' => file_exists( $css ) ? add_query_arg( 'ver', (string) filemtime( $css ), Assets::normalize_path( plugins_url( '../../build/comments.css', __FILE__ ) ) ) : '', + 'theme' => self::theme_styles(), + 'defaultAvatar' => Avatars::default_url( 80 ), + ), + ); + + return $data; + } + + /** + * Modules a filter forces on or off, whose toggles the screen locks as Jetpack's dashboard does. + * + * @return object Module slug => 'active' or 'inactive'. An object, so none encodes as `{}`. + */ + private static function module_overrides() { + $overrides = array(); + + // The Jetpack plugin owns overrides; another plugin's autoloader may serve this package without it. + if ( class_exists( 'Jetpack_Modules_Overrides' ) ) { + foreach ( array_merge( array( 'comments' ), self::FEATURE_MODULES ) as $module ) { + $override = \Jetpack_Modules_Overrides::instance()->get_module_override( $module ); + if ( $override ) { + $overrides[ $module ] = $override; + } + } + } + + return (object) $overrides; + } + + /** + * The theme's type, colors, and buttons, as custom properties for the preview. + * + * @return array|null Null for a theme without theme.json, whose global styles are only core's defaults. + */ + public static function theme_styles() { + if ( ! wp_theme_has_theme_json() ) { + return null; + } + + $styles = wp_get_global_styles(); + $map = array( + '--jpc-font-family' => array( 'typography', 'fontFamily' ), + '--jpc-font-size' => array( 'typography', 'fontSize' ), + '--jpc-line-height' => array( 'typography', 'lineHeight' ), + '--jpc-color' => array( 'color', 'text' ), + '--jpc-background' => array( 'color', 'background' ), + '--jpc-link' => array( 'elements', 'link', 'color', 'text' ), + '--jpc-heading-font-family' => array( array( 'elements', 'h3', 'typography', 'fontFamily' ), array( 'elements', 'heading', 'typography', 'fontFamily' ) ), + '--jpc-heading-font-size' => array( array( 'elements', 'h3', 'typography', 'fontSize' ), array( 'elements', 'heading', 'typography', 'fontSize' ) ), + '--jpc-heading-font-weight' => array( array( 'elements', 'h3', 'typography', 'fontWeight' ), array( 'elements', 'heading', 'typography', 'fontWeight' ) ), + '--jpc-heading-line-height' => array( array( 'elements', 'h3', 'typography', 'lineHeight' ), array( 'elements', 'heading', 'typography', 'lineHeight' ) ), + '--jpc-heading-color' => array( array( 'elements', 'h3', 'color', 'text' ), array( 'elements', 'heading', 'color', 'text' ) ), + '--jpc-button-background' => array( 'elements', 'button', 'color', 'background' ), + '--jpc-button-color' => array( 'elements', 'button', 'color', 'text' ), + '--jpc-button-font-family' => array( 'elements', 'button', 'typography', 'fontFamily' ), + '--jpc-button-font-weight' => array( 'elements', 'button', 'typography', 'fontWeight' ), + '--jpc-button-text-transform' => array( 'elements', 'button', 'typography', 'textTransform' ), + '--jpc-button-letter-spacing' => array( 'elements', 'button', 'typography', 'letterSpacing' ), + '--jpc-button-radius' => array( 'elements', 'button', 'border', 'radius' ), + '--jpc-button-border-width' => array( 'elements', 'button', 'border', 'width' ), + '--jpc-button-border-style' => array( 'elements', 'button', 'border', 'style' ), + '--jpc-button-border-color' => array( 'elements', 'button', 'border', 'color' ), + ); + + $tokens = array(); + foreach ( $map as $property => $paths ) { + $paths = is_array( $paths[0] ) ? $paths : array( $paths ); + foreach ( $paths as $path ) { + $value = self::style_value( $styles, $path ); + if ( '' !== $value ) { + $tokens[ $property ] = $value; + break; + } + } + } + + return $tokens; + } + + /** + * One global styles value as CSS, with theme.json's `var:preset|…` shorthand expanded as core expands it. + * + * @param array $styles Global styles. + * @param array $path Keys to the value. + * @return string Empty when unset or not a plain value. + */ + private static function style_value( $styles, $path ) { + $value = $styles; + foreach ( $path as $key ) { + if ( ! is_array( $value ) || ! isset( $value[ $key ] ) ) { + return ''; + } + $value = $value[ $key ]; + } + + if ( ! is_string( $value ) && ! is_numeric( $value ) ) { + return ''; + } + + $value = trim( (string) $value ); + if ( str_starts_with( $value, 'var:' ) ) { + $value = 'var(--wp--' . str_replace( '|', '--', substr( $value, 4 ) ) . ')'; + } + + // A value, never a way out of one. + return preg_match( '/[;{}<>]/', $value ) ? '' : $value; + } +} diff --git a/projects/packages/comments/src/settings/components.tsx b/projects/packages/comments/src/settings/components.tsx new file mode 100644 index 000000000000..e6724ede6ff3 --- /dev/null +++ b/projects/packages/comments/src/settings/components.tsx @@ -0,0 +1,95 @@ +/* @jsxImportSource react */ +import { ToggleControl } from '@wordpress/components'; +import { useCallback } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { Card, Stack, Text } from '@wordpress/ui'; +import type { ModuleOverrides, Settings } from './types'; +import type { DeepPartial, NormalizedField } from '@wordpress/dataviews'; +import type { JSX, ReactNode } from 'react'; + +/** + * One section of the screen, as Newsletter's settings cards: a title, an optional description, then its settings. + * + * @param props - Props. + * @param props.title - Heading. + * @param props.description - What the section is for. + * @param props.children - Settings. + * @return Card. + */ +export function SectionCard( { + title, + description, + children, +}: { + title: string; + description?: ReactNode; + children: ReactNode; +} ): JSX.Element { + return ( + + + }>{ title } + + + + { description && }>{ description } } + { children } + + + + ); +} + +/** + * Why a module's toggle is locked, in the words Jetpack's dashboard uses. + * + * @param override - The module's override, if a filter forces it. + * @return Note, or undefined when nothing forces it. + */ +export function overrideNote( + override: ModuleOverrides[ keyof ModuleOverrides ] +): string | undefined { + if ( override === 'active' ) { + return __( 'This feature has been enabled by a site administrator.', 'jetpack-comments' ); + } + if ( override === 'inactive' ) { + return __( 'This feature has been disabled by a site administrator.', 'jetpack-comments' ); + } + return undefined; +} + +/** + * DataForm `Edit` for a boolean setting, as a toggle. Reads through the field's `getValue`. + * + * @param props - Edit props. + * @param props.data - Settings. + * @param props.field - Field. + * @param props.onChange - Called with the edit. + * @return Control. + */ +export function ToggleEdit( { + data, + field, + onChange, +}: { + data: Settings; + field: NormalizedField< Settings >; + onChange: ( value: DeepPartial< Settings > ) => void; +} ): JSX.Element { + const checked = Boolean( field.getValue( { item: data } ) ); + const handleChange = useCallback( + ( next: boolean ) => onChange( { [ field.id ]: next } as DeepPartial< Settings > ), + [ field.id, onChange ] + ); + + return ( + + ); +} diff --git a/projects/packages/comments/src/settings/preview/comment-form-preview.tsx b/projects/packages/comments/src/settings/preview/comment-form-preview.tsx new file mode 100644 index 000000000000..8b252058b355 --- /dev/null +++ b/projects/packages/comments/src/settings/preview/comment-form-preview.tsx @@ -0,0 +1,261 @@ +/* @jsxImportSource react */ +import { createInterpolateElement, useId } from '@wordpress/element'; +import { __, _x } from '@wordpress/i18n'; +import { Icon, formatBold, formatItalic, link, paragraph, plus } from '@wordpress/icons'; +import { Shadow } from './shadow'; +import { PREVIEW_CSS } from './styles'; +import type { DiscussionSettings, NewsletterSettings, PreviewData } from '../types'; +import type { CSSProperties, JSX } from 'react'; + +interface PreviewProps { + isJetpackForm: boolean; + hasBlocks: boolean; + prompt: string; + discussion: DiscussionSettings; + newsletter: NewsletterSettings; + preview: PreviewData; +} + +// An attribute, not a prop: React 18 and 19 disagree on how `inert` is spelled. +const makeInert = ( node: HTMLDivElement | null ) => node?.setAttribute( 'inert', '' ); + +/** + * The editor's toolbar as it opens over the box: insert, the block switcher, and the inline formats. + * + * @return Toolbar. + */ +function Toolbar(): JSX.Element { + const groups = [ [ plus ], [ paragraph ], [ formatBold, formatItalic, link ] ]; + + return ( +
+ { groups.map( ( icons, index ) => ( + + { icons.map( ( icon, iconIndex ) => ( + + + + ) ) } + + ) ) } +
+ ); +} + +/** + * Jetpack's box as a reader first sees it, in the real form's markup so its stylesheet applies. + * + * @param props - Props. + * @param props.hasBlocks - Whether the editor's toolbar shows. + * @param props.discussion - Core's discussion settings. + * @param props.preview - Preview data. + * @return Form. + */ +function JetpackForm( { + hasBlocks, + discussion, + preview, +}: Pick< PreviewProps, 'hasBlocks' | 'discussion' | 'preview' > ): JSX.Element { + return ( +
+
+ { hasBlocks && } +