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

Multiple editor widths #12874

Closed
manake opened this issue Dec 14, 2018 · 7 comments
Closed

Multiple editor widths #12874

manake opened this issue Dec 14, 2018 · 7 comments
Labels
[Feature] Custom Editor Styles Functionality for adding custom editor styles [Type] Developer Documentation Documentation for developers [Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@manake
Copy link

manake commented Dec 14, 2018

This is already asked by like 10 people but there is no solution.

Sorry for asking this again but no working solution was ever provided anywhere and all questions are being ignored so far or contain wrong answers.

This is frequently suggested but this just doesn't work:

body.gutenberg-editor-page.post-type-post .edit-post-visual-editor .editor-post-title__block, body.gutenberg-editor-page.post-type-post .edit-post-visual-editor .editor-block-list__block { max-width: 500px; }

Can someone please provide the official copy/paste solution (exact steps that we have to take to define width: 500px for post and width: 1000px for page)? What would I have to paste into my style-editor.css or functions.php?

@swissspidy swissspidy added [Type] Help Request Help with setup, implementation, or "How do I?" questions. [Feature] Custom Editor Styles Functionality for adding custom editor styles labels Dec 16, 2018
@designsimply designsimply added the [Type] Developer Documentation Documentation for developers label Jan 9, 2019
@designsimply
Copy link
Member

designsimply commented Jan 10, 2019

To answer your question with some context, you are asking to change editor width based on body classes and that cannot currently be done with the add_theme_support('editor-styles'); method because of the exact reason you mentioned at #12904: .editor-styles-wrapper is injected in order to simplify adding editor styles while still avoiding other specificity problems such as inadvertently affecting the UI bits (toolbars, movers, etc.). This is noted in the documentation about Editor styles (near the top) and that specificity change was made in #9008 in an attempt to simplify things, for example, by allowing a theme author to add a rule such as h2 { color: red; } without having to know any more complex selectors and without having to worry about affecting h2 used elsewhere in the admin area. This is already being discussed in another open issue at #10067 (comment) which I would look to as a good/relevant discussion.

To answer your question with a potential solution, you can currently work around this problem by enqueueing your custom editor width styles a bit differently. Here is an example:

Change test to a name of your choice and add this to functions.php in your theme:

function add_theme_scripts() {
        wp_enqueue_style( 'test', get_template_directory_uri() . '/test.css', array(), wp_get_theme()->get( 'Version' ), 'all');
}
add_action( 'enqueue_block_editor_assets', 'add_theme_scripts' );

Add a test.css file, or a file name of your choice matching what you picked above, to your theme with the following content:

.wp-block {
  max-width: 1000px;
}

.post-type-post .wp-block {
  max-width: 500px;
}

Tested with WordPress 5.0.3-RC1-44445 and the Twenty Nineteen 1.2 theme.

Caveat! Because I used the Twenty Nineteen 1.2 theme in my test and because that theme already had more specific editor width styling, I had to remove those styles in order to allow mine to work. Of course, if you were developing your own theme, you would likely not have encoded conflicting editor styles into your theme without knowing but it is something to watch out for when making edits to another theme you may not be as familiar with. Here are the styles I removed from Twenty Nineteen for my test:

.wp-block {
  width: calc(100vw - (2 * 1rem));
  max-width: 100%;
}

@media only screen and (min-width: 768px) {
  .wp-block {
    width: calc(8 * (100vw / 12));
  }
}

@media only screen and (min-width: 1168px) {
  .wp-block {
    width: calc(6 * (100vw / 12 ));
  }
}

I acknowledge my code may not be perfect! I typically work in testing and triage and love themes but am not a full-time developer. If anyone sees area for improvement, I'd love feedback. 😊

Regarding documenting exact steps or a copy/paste solution, I wouldn't want to do that just yet because how to solve the specificity issues are still under discussion in places like #10067 (comment) (also see #9008 (comment)) and also because I would consider my solution above to be a temporary workaround and would want whatever comes out of the currently open issues to supersede the sample code I've posted here.

Regarding the bulleted list of issues you referenced, sometimes the trouble with looking at past solutions in a repository is that the code has since changed. For example, the solution proposed in #8420 no longer applies after the changes in #9008. A better approach is to include a clear set of steps to reproduce the problem and include code examples whenever possible exactly like you did in #12904. That issue is ace!

@manake if this explanation and example are sufficient for you for now, would you be okay if I closed this issue in favor of consolidating to other already-open issues such as #10067 where they are discussing this in conjunction with some other specificity problems?

@designsimply
Copy link
Member

@samikeijonen I got the idea to use enqueue_block_editor_assets from you at #10956 (comment) 🙂 and was wondering if you might be willing to have a quick look at my example above to let me know whether it's decent or not for the use case of setting different editor widths for posts vs pages.

@samikeijonen
Copy link
Contributor

@designsimply Sorry I missed your comment. I don't see issues on your example but as always, it might depend on theme you're working on.

@designsimply
Copy link
Member

@samikeijonen thanks for taking a look!

@designsimply
Copy link
Member

@manake were you able to try out the solution I proposed?

@thomasdebruin
Copy link

thomasdebruin commented Apr 11, 2019

I needed custom editor width depending on page template (so not post-type).
What worked for me is:

/*
    Append gutenberg editor custom styles
*/

function add_theme_scripts() {
    wp_enqueue_style( 'editor-styles', get_template_directory_uri() . '/dist/styles/editor-styles.css', array(), wp_get_theme()->get( 'Version' ), 'all');
}
add_action( 'enqueue_block_editor_assets',  __NAMESPACE__ .'\\add_theme_scripts' );

function custom_admin_body_class( $classes ) {
    global $post;
    $cls = '';
    if ($post) {
        $cls = str_replace(".php","",get_page_template_slug());
    }
    return "$classes $cls";
}
add_filter( 'admin_body_class', __NAMESPACE__ .'\\custom_admin_body_class' );`

**editor-styles.css**
`.wp-block {
  max-width: 692px;
}

body.template-overview .wp-block {
  max-width: 1200px;
}

I think this is a flexible solution (you could easily add post-types or other contextual classes to determine your editor width or other properties). But my itnuition says this should be done in a more clean way, fi:

add_theme_support( 'wp-block-styles' );
add_editor_style(  Assets\asset_path('styles/editor-styles.css') );

This css doen't seem to apply when using the body class selector, probably because gutenberg loads this in another context, so maybe the 'page-template' class needs to be applied with another hook to another wrapper on the block editor (like the editor-styles-wrapper), but currently I lack the knowledge how to do this.

@youknowriad
Copy link
Contributor

It seems that this has already some good replies. Let us know if the proposed solutions don't work for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Custom Editor Styles Functionality for adding custom editor styles [Type] Developer Documentation Documentation for developers [Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

6 participants