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

Revisions don't render code fields. Drafts do #12

Closed
gbowne-quickbase opened this issue Oct 4, 2023 · 5 comments
Closed

Revisions don't render code fields. Drafts do #12

gbowne-quickbase opened this issue Oct 4, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@gbowne-quickbase
Copy link

Describe the bug

When I look at a previous revision of a page that contains a code field, it just shows as a text area. Really a low priority, but wanted to report for clarity. Thanks!

To reproduce

  1. Create a page with a code field on it. Save it.
  2. Make a revision or a draft. Save it and publish it
  3. Look back at a previous version in the CP.

Expected behaviour

The code field would display as code with formatting, instead of a text block. Drafts seem to function just fine

Screenshots

If applicable, add screenshots to help explain your problem.
Current revision:
image
Previous Revision:
image
Draft:
image

Versions

  • Plugin version: 4.0.10
  • Craft version: 4.5.6.1
@gbowne-quickbase gbowne-quickbase added the bug Something isn't working label Oct 4, 2023
khalwat added a commit that referenced this issue Oct 5, 2023
khalwat added a commit that referenced this issue Oct 5, 2023
@khalwat
Copy link
Contributor

khalwat commented Oct 5, 2023

So this was pretty interesting... when displaying revisions, the field's ::getStaticHtml() method is called instead of ::getInputHtml(). Code Field doesn't implement that method, so it ends up calling the parent Field method:

    /**
     * @inheritdoc
     */
    public function getStaticHtml(mixed $value, ElementInterface $element): string
    {
        // Just return the input HTML with disabled inputs by default
        Craft::$app->getView()->startJsBuffer();
        $inputHtml = $this->getInputHtml($value, $element);
        $inputHtml = preg_replace('/<(?:input|textarea|select)\s[^>]*/i', '$0 disabled', $inputHtml);
        Craft::$app->getView()->clearJsBuffer();

        return $inputHtml;
    }

...which explicitly removes any queue'd JavaScript from rendering, which then causes the Monaco editor to not be instantiated, so the formatting is never applied.

Instead, we now implement our own ::getStaticHtml() which renders the Monaco editor as readOnly so things display as intended.

Addressed in: cbae501 & cdcfda0

Craft CMS 3:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-code-field": "dev-develop-v3 as 3.0.11”,

Then do a composer clear-cache && composer update

…..

Craft CMS 4:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-code-field": "dev-develop-v4 as 4.0.11”,

Then do a composer clear-cache && composer update

@khalwat khalwat closed this as completed Oct 5, 2023
@gbowne-quickbase
Copy link
Author

Thanks for a quick fix, and glad it was "interesting" at least.
P.S. I'm doing your pushups for you in October

@khalwat
Copy link
Contributor

khalwat commented Oct 5, 2023

How many you doing?

@gbowne-quickbase
Copy link
Author

I’m following the pattern of Month+Day so today is 10+6. It’s a good challenge!

@khalwat
Copy link
Contributor

khalwat commented Oct 8, 2023

ooooh sweet! You should carry the torch for me, and finish the entire year out! 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants