Skip to content
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

Html block: Add option to allow preview by default #41066

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

mauriac
Copy link
Member

@mauriac mauriac commented May 13, 2022

A new option has been added to HTML block to allow 'preview' mode by default

What?

fixes: #40913

Why?

See the issue.

How?

In this PR, a new attribute showPreview has been added to HTMLEdit component, and then when the state of this attribute changes the html view mode changes accordingly.

Testing Instructions

Screenshots or screencast

@mauriac mauriac requested a review from amustaque97 May 13, 2022 22:37
@mauriac mauriac added [Block] HTML Affects the the HTML Block [Type] Enhancement A suggestion for improvement. labels May 13, 2022
@@ -42,6 +46,14 @@ export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
];
}, [] );

useEffect( () => {
if ( !! showPreview ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @mauriac for working on this. I'm thinking do we really need if-else condition? While doing review I observed both the variables isPreview and showPreview are in sync if we modify current statement something like this:

-const [ isPreview, setIsPreview ] = useState();
+const [ isPreview, setIsPreview ] = useState(false);

PS: Current default value of isPreview variable is undefined.

Looking forward to hearing from you

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @amustaque97 thank you for your review, but I am a bit lost;

I do not get the suggestion. Please do you mind enlighten me?

The sync between isPreview and showPreview is just a plus, but on second thought let's remove it.

Thank you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sync between isPreview and showPreview is just a plus, but on second thought let's remove it.

What I mean to say is that we can remove if-else block and can write something like this

useEffect(() => {
    setIsPreview( showPreview );
}, [showPreview])

Also, to change line here

-const [ isPreview, setIsPreview ] = useState();
+const [ isPreview, setIsPreview ] = useState(false);

I hope I'm able to explain request changes.

Thank you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mauriac , I saw your changes. I will take a proper look at it in sometime and update my comments here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sync between isPreview and showPreview is just a plus, but on second thought let's remove it.

What I mean to say is that we can remove if-else block and can write something like this

useEffect(() => {
    setIsPreview( showPreview );
}, [showPreview])

Also, to change line here

-const [ isPreview, setIsPreview ] = useState();
+const [ isPreview, setIsPreview ] = useState(false);

I hope I'm able to explain request changes.

Thank you.

Oh yes you are totally right. I got it now, I make some changes, give me your back when you have sometime. thank you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your changes makes more sense to load preview setting once. Also, can we change useEffect block to look like this:

useEffect( () => {
    if ( showPreview ) {
        switchToPreview();
    }
}, [] );

I think PR is good to approve after the above final changes. Let me know what you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your changes makes more sense to load preview setting once. Also, can we change useEffect block to look like this:

useEffect( () => {
    if ( showPreview ) {
        switchToPreview();
    }
}, [] );

I think PR is good to approve after the above final changes. Let me know what you think?

I agree, I applied changes, so what about this change
+const [ isPreview, setIsPreview ] = useState(false); should we still apply it?

@amustaque97
Copy link
Member

This PR has frontend change I would like to involve @jasmussen and other bunch of folks for design/UX review 🙂

@amustaque97 amustaque97 added the Needs Design Feedback Needs general design feedback. label May 14, 2022
@amustaque97 amustaque97 requested a review from Mamaduka May 14, 2022 14:55
@@ -15,12 +16,15 @@ import {
Disabled,
SandBox,
ToolbarGroup,
PanelBody,
ToggleControl,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';

export default function HTMLEdit( { attributes, setAttributes, isSelected } ) {
const [ isPreview, setIsPreview ] = useState();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [ isPreview, setIsPreview ] = useState();
const [ isPreview, setIsPreview ] = useState( showPreview );

Suggestion: We can use attribute value as the initial state. It would be similar to the current effect that only runs on the mount.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Mamaduka thanks for your review, that works perfectly, but we have a new suggestion in the comments below (here) that blocks me to make new commit now.

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, @mauriac.

I left one suggestion, but the otherwise feature works as expected.

@mtias
Copy link
Member

mtias commented May 21, 2022

This shouldn't be an attribute of the block, we should probably look into making preview the default view when the block is unselected.

@mauriac
Copy link
Member Author

mauriac commented May 22, 2022

This shouldn't be an attribute of the block, we should probably look into making preview the default view when the block is unselected.

Hello @mtias thanks for your comment. I think it makes sense, and I should agree with, but the advantage of the new option here is to allow users to choose what will be good for them, so if we remove the attribute, what happen for those who will not want to get the preview mode by default?

Looking forward to hear you. Thanks

@kohheepeace
Copy link

kohheepeace commented Oct 19, 2022

+1 for this feature. And I don't think InspectorControls is necessary.

Also, Instead of using local state like below

const [ isPreview, setIsPreview ] = useState( showPreview );

Call setAttributes directly like below isn't better?

In edit.js

...

export default function HTMLEdit({ attributes, setAttributes, isSelected }) {
  const { isPreview } = attributes;
  ...

  function switchToPreview() {
    setAttributes({ isPreview: true });
  }

  function switchToHTML() {
    setAttributes({ isPreview: false });
  }

  return (
 ...
  );
}

*NOTE: Of course define isPreview attribute in block.json

...
  "attributes": {
   ...
    "isPreview": {
      "type": "boolean",
      "default": false
    }
  },
...

The user's toggled state is saved and remains even if the browser is reloaded.

@kohheepeace
Copy link

@mtias

making preview the default view when the block is unselected.

=> I think this will make editor UX bad (crackling? popping? <- sorry I forget English words to describe this phenomenon 😢 )
Because of code height and preview HTML height will maybe different.

@mauteri
Copy link
Contributor

mauteri commented Jan 1, 2023

This shouldn't be an attribute of the block, we should probably look into making preview the default view when the block is unselected.

PR for on select approach: #46836

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] HTML Affects the the HTML Block Needs Design Feedback Needs general design feedback. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow setting default preview for HTML blocks
6 participants