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

feat(macros/EmbedInteractiveExample): use height from height-data.json #7679

Closed
wants to merge 7 commits into from

Conversation

NiedziolkaMichal
Copy link
Member

@NiedziolkaMichal NiedziolkaMichal commented Nov 26, 2022

This is the second part of PR mdn/bob#839 in BOB, which makes height-data.json be created while building interactive examples. That file contains the height of every interactive example, so that height no longer needs to be provided from content repository and we can also remove the height of each interactive editor type from here.

The height of the editor is now calculated by JS code in interactive-examples.tsx. During build, iframe of example is created, together with data-heights attribute, which contains information about the height of the editor. This is an exemplary value:

[
   {
      "minFrameWidth": 0,
      "height": 723
   },
   {
      "minFrameWidth": 590,
      "height": 654
   }
]

The component will use the last height, for which the value of minFrameWidth was greater or equal to the calculated width of the iframe. This works just like media queries, but we don't use the width of the window in here, which gives incorrect results because of interference from the rest of the UI.

I changed the key of the Prose component to the following because the paragraph under iframe was causing duplicated react key exception(both having null value).

key={section.value.id || `top_level_prose${i}`}

If this gets merged, I will make the next PR to remove the second argument(height class) from every usage of EmbedInteractiveExample in the content repository.

This PR depends on mdn/bob#839 and shouldn't be merged until BOB update is deployed.
Tests are failing because iframe cannot be shown, until #839 is merged

@github-actions github-actions bot added the macros tracking issues related to kumascript macros label Nov 26, 2022
Copy link
Contributor

@caugner caugner left a comment

Choose a reason for hiding this comment

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

Thank you, just some nits (moving complexity out of the macro, add types).

Comment on lines 12 to 22
const heightData = await mdn.fetchInteractiveExampleHeightData();

if ($0.includes('/js/')) {
heightClass = 'is-js-height';
}

if ($1) {
let supportedHeights = ['shorter', 'taller', 'tabbed-shorter', 'tabbed-standard', 'tabbed-taller'];
let heightIsSupported = (supportedHeights.indexOf($1) > -1);

if (heightIsSupported) {
heightClass = 'is-' + $1 + '-height';
} else {
throw new Error(`An unrecognized second size parameter to EmbedInteractiveExample ('${$1}')`);
}
let height;
if (heightData[$0]) {
height = heightData[$0];
} else if ($0.includes('/js/')) {
height = 'js';
} else {
height = 'default';
}
const heightClass = 'is-' + height + '-height';
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's extract this whole section as mdn.getInteractiveExampleHeightClass(path), so that this macro knows nothing about where the height comes from.

Also, let's keep the $1 parameter for now and pass it to this method, to use as a fallback as long as the height-data.json is not available.

Once the height-data.json is available, we can deprecate the 2nd parameter.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have moved the whole section to getInteractiveExampleHeight. Thanks

Since there was no reply for a long time, I have moved the exact height information to height-data.json. Old classes are no longer compatible, so I didn't apply the second suggestion.


// Module level caching for repeat calls to fetchWebExtExamples().
let webExtExamples: any = null;

let interactiveExampleHeightData: object = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let interactiveExampleHeightData: object = null;
type InteractiveExamplesHeight = 'shorter' | 'taller' | 'tabbed-shorter' | 'tabbed-standard' | 'tabbed-taller';
type InteractiveExamplesHeightData = Record<string, InteractiveExamplesHeight>;
let interactiveExampleHeightData: InteractiveExamplesHeightData = null;

Copy link
Member Author

Choose a reason for hiding this comment

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

I added the correct type instead of the object. I have moved it to interactive-example.tsx though, so the whole logic is located in a single place. In the future I will port BOB to TS, so we can remove the types from here.

async fetchInteractiveExampleHeightData() {
if (!interactiveExampleHeightData) {
try {
interactiveExampleHeightData = await got(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
interactiveExampleHeightData = await got(
interactiveExampleHeightData = await got<InteractiveExamplesHeightData>(

Copy link
Member Author

Choose a reason for hiding this comment

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

Added, thank you

@caugner caugner marked this pull request as draft November 28, 2022 19:51
@caugner caugner changed the title Interactive Examples use height from height-data.json feat(macros/EmbedInteractiveExample): use height from height-data.json Nov 28, 2022
@NiedziolkaMichal
Copy link
Member Author

NiedziolkaMichal commented Nov 28, 2022

@caugner All of that sounds good, I will add a commit with those changes later.

I realized now that there is a better way to tackle this problem. Currently, heights are hardcoded in interactive-examples.scss in YARI and distributed across multiple CSS files in BOB. I could make height-data.json send height in pixels instead of classes. A single record would look like that:

    "pages/tabbed/audio.html": {
        "landscape-height": "421px",
        "portrait-height": "548px"
    }

The file would get a bit bigger, but it would allow more complex height adjustments. For JavaScript examples, we could make height calculated by the formula BASE_HEIGHT + amountOfLines * LINE_HEIGHT. It would make examples like Optional Chaining not be half empty.
Would you agree to that?
Edit: Since there was no reply for a long time, I have gone along with this, but JSON looks a bit different. I have updated the first post with the exact details.

@github-actions
Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label Dec 16, 2022
@schalkneethling
Copy link

Hey @NiedziolkaMichal, a friendly ping. This one has a conflict that needs to be resolved. Thanks!

@github-actions github-actions bot removed the merge conflicts 🚧 Please rebase onto or merge the latest main. label Jan 18, 2023
@NiedziolkaMichal
Copy link
Member Author

@schalkneethling It's done :) Tests seem to be failing because height-data.json is not yet available.

@NiedziolkaMichal NiedziolkaMichal requested review from caugner and removed request for fiji-flo January 18, 2023 23:42
@NiedziolkaMichal NiedziolkaMichal marked this pull request as ready for review January 18, 2023 23:42
@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label Jan 23, 2023
@github-actions
Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot removed the merge conflicts 🚧 Please rebase onto or merge the latest main. label Jan 23, 2023
@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label Feb 8, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Feb 8, 2023

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot removed the merge conflicts 🚧 Please rebase onto or merge the latest main. label Feb 25, 2023
@NiedziolkaMichal
Copy link
Member Author

I have rechecked everything and the code is working fine, but https://interactive-examples.mdn.mozilla.net/height-data.json is still returning 404. While building interactive examples, this file exists and is available under the right location, but for some reason, it is not present in the production. I think that maybe "heightData": "./docs/height-data.json" line is missing in the .bobconfigrc in the production environment.

@caugner
Copy link
Contributor

caugner commented Mar 8, 2023

Hi @NiedziolkaMichal, I wonder if we couldn't just fix yari to listen to this height event:

https://github.com/mdn/bob/blob/077d0203a9c45b2a143a734169710c4ab49b4ce9/editor/js/editor-libs/events.js#L85-L92

@NiedziolkaMichal
Copy link
Member Author

I was thinking about it, but there is a significant drawback to this. Individual examples usually load 2-3 seconds after the rest of the content on my PC, where I have high-speed internet. If we send height information through an event, we will have a significant reflow after those few seconds, which would annoy the users.
I was planning to use the event for examples that are not supported by the user's device because there is no way to predict this during the build, while currently a lot of space is being wasted.

@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label Mar 15, 2023
@github-actions
Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@caugner caugner removed their request for review May 2, 2023 08:19
@bsmth bsmth requested a review from LeoMcA May 4, 2023 14:26
@github-actions github-actions bot added the idle label Jun 3, 2023
@github-actions github-actions bot removed the idle label Jun 20, 2023
@github-actions github-actions bot added the idle label Jul 20, 2023
@NiedziolkaMichal NiedziolkaMichal requested a review from a team as a code owner January 2, 2024 10:47
@github-actions github-actions bot removed merge conflicts 🚧 Please rebase onto or merge the latest main. idle labels Jan 2, 2024
@github-actions github-actions bot added the idle label Feb 2, 2024
@github-actions github-actions bot added the merge conflicts 🚧 Please rebase onto or merge the latest main. label Mar 13, 2024
Copy link
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions bot removed the idle label Mar 13, 2024
@fiji-flo
Copy link
Contributor

Sorry for not addressing this in time. I see the issue, but we don't want to set height in JS here. We'll address this later this year.

@fiji-flo fiji-flo closed this Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macros tracking issues related to kumascript macros merge conflicts 🚧 Please rebase onto or merge the latest main.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants