From 1280220485f7ac33c6a4a195b2a8acf19911029c Mon Sep 17 00:00:00 2001
From: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
Date: Tue, 12 Dec 2023 12:41:50 +0900
Subject: [PATCH] Add filter hook to disable the preview button (#16)
---
.distignore | 1 +
.markdownlint.json | 6 ++++++
README.md | 17 +++++++++++++++++
enable-responsive-image.php | 1 +
src/constants.ts | 2 ++
src/index.tsx | 3 ++-
src/types.ts | 1 +
7 files changed, 30 insertions(+), 1 deletion(-)
create mode 100644 .markdownlint.json
diff --git a/.distignore b/.distignore
index dbd8f08..9de8cfd 100644
--- a/.distignore
+++ b/.distignore
@@ -9,6 +9,7 @@
.editorconfig
.eslintignore
.eslintrc.js
+.markdownlint.json
.gitignore
.npmrc
.nvmrc
diff --git a/.markdownlint.json b/.markdownlint.json
new file mode 100644
index 0000000..3e0ed55
--- /dev/null
+++ b/.markdownlint.json
@@ -0,0 +1,6 @@
+{
+ "default": true,
+ "MD013": { "line_length": 9999 },
+ "MD024": false,
+ "no-hard-tabs": false
+}
diff --git a/README.md b/README.md
index d0526b9..60fe617 100644
--- a/README.md
+++ b/README.md
@@ -41,6 +41,23 @@ function custom_enable_responsive_image_default_media_value( $default_media_valu
add_filter( 'enable_responsive_image_default_media_value', 'custom_enable_responsive_image_default_media_value' );
```
+### `enable_responsive_image_show_preview_button( $show_preview_button )`
+
+Filters whether the preview button is displayed on the block toolbar.
+
+#### Parameters
+
+- `$show_preview_button`
+
+ *(boolean)* Whether the preview button is displayed on the block toolbar. Default is `true`.
+
+#### Example
+
+```php
+// Disable the preview button.
+add_filter( 'enable_responsive_image_show_preview_button', '__return_false' );
+```
+
## Resources
### Image for screenshot
diff --git a/enable-responsive-image.php b/enable-responsive-image.php
index 4a49f1a..5f6d1ec 100644
--- a/enable-responsive-image.php
+++ b/enable-responsive-image.php
@@ -35,6 +35,7 @@ function enable_responsive_image_enqueue_block_editor_assets() {
'enableResponsiveImage',
array(
'defaultMediaValue' => (int) apply_filters( 'enable_responsive_image_default_media_value', 600 ),
+ 'showPreviewButton' => (bool) apply_filters( 'enable_responsive_image_show_preview_button', true ),
)
);
diff --git a/src/constants.ts b/src/constants.ts
index e17338c..355fccd 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -9,6 +9,8 @@ export const DEFAULT_MEDIA_VALUE = isNaN(
? 600
: parseInt( window?.enableResponsiveImage?.defaultMediaValue );
+export const SHOW_PREVIEW_BUTTON = window?.enableResponsiveImage?.showPreviewButton === '1';
+
export const MEDIA_TYPES = [
{
label: __( 'max-width', 'enable-responsive-image' ),
diff --git a/src/index.tsx b/src/index.tsx
index 3194793..9d3ea5a 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -16,6 +16,7 @@ import BlockEditPreview from './block-edit-preview';
import SourceList from './source-list';
import './editor.scss';
import './store';
+import { SHOW_PREVIEW_BUTTON } from './constants';
import type { BlockAttributes } from './types';
const addSourceAttributes = ( settings: { [ key: string ]: any } ) => {
@@ -70,7 +71,7 @@ const withInspectorControl =
) : (
) }
- { url && sources?.length > 0 && (
+ { url && sources?.length > 0 && SHOW_PREVIEW_BUTTON && (