Closed ArtMin96 closed 1 year ago
Patterns need categories
, content
, name
, and title. Categories need
name,
label,
description`.
It's probably worth checking this kind of stuff against Gutenberg itself as this project just passes it straight through.
https://developer.wordpress.org/block-editor/reference-guides/block-api/block-patterns/ Everything there is presented for working with wordpress. But I can't find anything for the standalone editor. It would be good to open the discussion section, in case of questions we will not open a new issue. Thank you
@ArtMin96 Check here, working code.
https://github.com/xpalko/isolated-block-editor/commit/e73177b987b1eaba1794aa24e45bd11aad011b7f
Thanks @xpalko Is there any option so that I don't fork and change the package code?
The current repo supports patterns fine as it just passes the data to Gutenberg - it's not necessary to fork.
Modifying the included index.html and inserting the patterns from xpalko:
<html>
<head>
<title>Plain Text Editor</title>
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<script src="../../build-browser/isolated-block-editor.js"></script>
<link rel="stylesheet" href="../../build-browser/core.css" />
<link rel="stylesheet" href="../../build-browser/isolated-block-editor.css" />
<meta charset="UTF-8">
<style>
body {
margin: 0;
padding: 40px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
width: 80%;
margin: 0 auto;
}
textarea {
width: 100%;
}
.editor {
min-height: 200px;
width: 100%;
}
.block-editor-block-list__layout p {
font-size: 22px;
}
.add-another {
margin-top: 50px;
padding: 1em;
font-weight: 400;
border-radius: 9999px;
background-color: rgba(45,55,72,1);
color: white;
}
.cloner {
display: none;
}
</style>
</head>
<body>
<h1>Gutenberg attached to a plain textarea</h1>
<p>The textarea can be converted into a Gutenberg editor. Any existing content will be converted into blocks. Multiple editors can be added, with a seperate edit history for each.</p>
<div class="editors">
<div class="editor-container cloner">
<p>
<button onClick="toggleEditor( this );">Toggle Gutenberg</button>
</p>
<textarea rows="10"></textarea>
</div>
<div class="editor-container">
<p>
<button onClick="toggleEditor( this );">Toggle Gutenberg</button>
</p>
<textarea rows="10"></textarea>
</div>
</div>
<button class="add-another" onClick="addEditor()">Add another editor</button>
</body>
<script>
const settings = {
editor: {
__experimentalBlockPatterns: [
{
"categories": [
"text"
],
"content": "<!-- wp:group {\"align\":\"full\",\"style\":{\"color\":{\"gradient\":\"linear-gradient(135deg,rgb(238,238,238) 21%,rgb(169,184,195) 100%)\"}}} -->\n<div class=\"wp-block-group alignfull has-background\" style=\"background:linear-gradient(135deg,rgb(238,238,238) 21%,rgb(169,184,195) 100%)\"><div class=\"wp-block-group__inner-container\"><!-- wp:heading {\"level\":1} -->\n<h1>Featured Articles</h1>\n<!-- /wp:heading -->\n\n<!-- wp:columns -->\n<div class=\"wp-block-columns\"><!-- wp:column -->\n<div class=\"wp-block-column\"><!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://picsum.photos/200/300\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:heading -->\n<h2>Article 1</h2>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Eaedem res maneant alio modo. At hoc in eo M. Non potes, nisi retexueris illa. Scrupulum, inquam, abeunti; Quantum Aristoxeni ingenium consumptum videmus in musicis?</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button {\"backgroundColor\":\"red\"} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-red-background-color has-background\">Read more</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons --></div>\n<!-- /wp:column -->\n\n<!-- wp:column -->\n<div class=\"wp-block-column\"><!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://picsum.photos/200/300\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:heading -->\n<h2>Article 2</h2>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Eaedem res maneant alio modo. At hoc in eo M. Non potes, nisi retexueris illa. Scrupulum, inquam, abeunti; Quantum Aristoxeni ingenium consumptum videmus in musicis?</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button {\"backgroundColor\":\"red\"} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-red-background-color has-background\">Read more</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons --></div>\n<!-- /wp:column -->\n\n<!-- wp:column -->\n<div class=\"wp-block-column\"><!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://picsum.photos/200/300\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:heading -->\n<h2>Article 3</h2>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Eaedem res maneant alio modo. At hoc in eo M. Non potes, nisi retexueris illa. Scrupulum, inquam, abeunti; Quantum Aristoxeni ingenium consumptum videmus in musicis?</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button {\"backgroundColor\":\"red\"} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-red-background-color has-background\">Read more</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons --></div>\n<!-- /wp:column --></div>\n<!-- /wp:columns --></div></div>\n<!-- /wp:group -->",
"description": "An example block pattern with a heading.",
"name": "example/myplugin",
"title": "Example block pattern"
}
],
__experimentalBlockPatternCategories: [
{
"label": "Text",
"name": "text"
},
{
"label": "Demo",
"name": "demo"
}
]
}
};
function toggleEditor( button ) {
const textarea = button.parentNode.parentNode.querySelector( 'textarea' );
if ( textarea.style.display === 'none' ) {
wp.detachEditor( textarea );
} else {
wp.attachEditor( textarea, settings );
}
}
function addEditor() {
const editor = document.querySelector( '.cloner' );
const clone = editor.cloneNode( true );
clone.classList.remove( 'cloner' );
editor.parentNode.appendChild( clone );
}
</script>
</html>
@johngodley I did that, but the pattern section is not there
const settings = {
iso: {
//...
},
editor: {
codeEditingEnabled: true,
allowedBlockTypes: true,
canLockBlocks: false,
disableCustomColors: false,
disableCustomGradients: true,
disableCustomFontSizes: false,
disablePostFormats: false,
bodyPlaceholder: this.bodyPlaceholder,
reusableBlocks: [],
richEditingEnabled: true,
hasUploadPermissions: true,
__experimentalBlockPatterns: [
{
"categories": [
"text"
],
"content": "<!-- wp:group {\"align\":\"full\",\"style\":{\"color\":{\"gradient\":\"linear-gradient(135deg,rgb(238,238,238) 21%,rgb(169,184,195) 100%)\"}}} -->\n<div class=\"wp-block-group alignfull has-background\" style=\"background:linear-gradient(135deg,rgb(238,238,238) 21%,rgb(169,184,195) 100%)\"><div class=\"wp-block-group__inner-container\"><!-- wp:heading {\"level\":1} -->\n<h1>Featured Articles</h1>\n<!-- /wp:heading -->\n\n<!-- wp:columns -->\n<div class=\"wp-block-columns\"><!-- wp:column -->\n<div class=\"wp-block-column\"><!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://picsum.photos/200/300\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:heading -->\n<h2>Article 1</h2>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Eaedem res maneant alio modo. At hoc in eo M. Non potes, nisi retexueris illa. Scrupulum, inquam, abeunti; Quantum Aristoxeni ingenium consumptum videmus in musicis?</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button {\"backgroundColor\":\"red\"} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-red-background-color has-background\">Read more</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons --></div>\n<!-- /wp:column -->\n\n<!-- wp:column -->\n<div class=\"wp-block-column\"><!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://picsum.photos/200/300\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:heading -->\n<h2>Article 2</h2>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Eaedem res maneant alio modo. At hoc in eo M. Non potes, nisi retexueris illa. Scrupulum, inquam, abeunti; Quantum Aristoxeni ingenium consumptum videmus in musicis?</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button {\"backgroundColor\":\"red\"} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-red-background-color has-background\">Read more</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons --></div>\n<!-- /wp:column -->\n\n<!-- wp:column -->\n<div class=\"wp-block-column\"><!-- wp:image {\"sizeSlug\":\"large\"} -->\n<figure class=\"wp-block-image size-large\"><img src=\"https://picsum.photos/200/300\" alt=\"\"/></figure>\n<!-- /wp:image -->\n\n<!-- wp:heading -->\n<h2>Article 3</h2>\n<!-- /wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Eaedem res maneant alio modo. At hoc in eo M. Non potes, nisi retexueris illa. Scrupulum, inquam, abeunti; Quantum Aristoxeni ingenium consumptum videmus in musicis?</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:buttons -->\n<div class=\"wp-block-buttons\"><!-- wp:button {\"backgroundColor\":\"red\"} -->\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-red-background-color has-background\">Read more</a></div>\n<!-- /wp:button --></div>\n<!-- /wp:buttons --></div>\n<!-- /wp:column --></div>\n<!-- /wp:columns --></div></div>\n<!-- /wp:group -->",
"description": "An example block pattern with a heading.",
"name": "example/myplugin",
"title": "Example block pattern"
}
],
__experimentalBlockPatternCategories: [
{
"label": "Text",
"name": "text"
},
{
"label": "Demo",
"name": "demo"
}
]
},
};
wp.attachEditor(editor, settings);
Did you try the above example? You should work from that and see what is different with your local project.
oh. Ok. Let me try
This is what I have
Then something is different with your local files. The above code is run directly from this repo
Thank you very much @johngodley and @xpalko. I will try to find the problem.
The problem was here: when I use allowBlocks
, the pattern section does not work. Is this a bug?
const settings = {
iso: {
blocks: {
allowBlocks: blocksArray
},
}
};
I don't think so, no. Your allowed blocks will need to allow whatever blocks are used in the pattern.
ok. Thanks for help
This is how I try to add a custom block pattern, but no result. How can I do this? Thanks