"Implementing an inline widget" tutorial doesn't seem to work as expected
📝 Provide detailed reproduction steps (if any)
- Implement an inline widget (placeholder plugin) using the following official tutorial: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/tutorials/implementing-an-inline-widget.html
- Create a model that includes one of the placeholders when the editor loads:
model = "<p>Hello </p><placeholder name='firstName'></placeholder>"
✔️ Expected result
The editor will load with the placeholder loaded - "Hello {firstName}"
❌ Actual result
The editor loads "Hello" but replaces the placeholder code with <br data-cke-filler="true">
📃 Other details
Also tried using different ways to insert the placeholder into the model, like using <span class="placeholder">{firstName}</span> which just sanitizes the span away and leaves a <p> tag. Everything else about the tutorial works perfectly, meaning the we can manually include placeholders from the dropdown after the editor is loaded, and we can also successfully change their value.
The conclusion that we reached is that schema.register is not working properly, but we don't know how to fix it.
If you'd like to see this fixed sooner, add a 👍 reaction to this post.
Hi, how do you load your data to the editor? Note that there's no <p> tag in the model, paragraphs are represented as <paragraph> elements.
Thank you for your quick response.
We are including this editor in an angular application, so we are saving the model like this, as described in the official documentation.
model = {
editorData: "<p>My Model</p>"
}, and then using in in our component like this: <ckeditor [(ngModel)]="model.editorData" [editor]="Editor" [config]="config"></ckeditor>.
Replacing the <p> tag with <paragraph> does display the paragraph, but it was also working before. Replacing this also does not have any effect on the <placeholder> tag, which is still not displayed.
We have also tried the same thing on the sample index.html file when creating a custom ckeditor build, which by default comes with <p> tags and not <paragraph> tags. Once again, using the paragraph tag does indeed work to display paragraph content, but it's also working with a p tag, and it has no effect on the loading of the placehodler.
A short explanation of what we did:
- Cloned the official ckeditor5-inline-build module
- Implemented the code in the tutorial inside of the cloned module
- Built the new editor module and imported it into our project.
The placeholder plugin works great in both our project as well as the sample index.html file in the custom build, allowing us to add placeholders using the dropdown. However, in both cases, including the <placeholder> tag does not display anything inside the editor.
Here is the sample editor code in the index.html file that comes from cloning the ckeditor5-inline-build
<div id="editor">
<p>This is an instance of the <a
href="https://ckeditor.com/docs/ckeditor5/latest/builds/guides/overview.html#inline-editor">inline
editor build</a>.</p>
<placeholder name="name"></placeholder>
<figure class="image">
<img src="../tests/manual/sample.jpg" alt="Autumn fields" />
</figure>
<placeholder name="name"></placeholder>
<paragraph>Test</paragraph>
<p>You can use this sample to validate whether your <a
href="https://ckeditor.com/docs/ckeditor5/latest/builds/guides/development/custom-builds.html">custom
build</a> works fine.</p>
</div>
As you can see, the default code does not use the <paragraph> tag but the <p> tag. I have added a <paragraph> tag that does indeed display, and two <placeholder> tags that do not display.
Please let us know what further information might help you with this.
Indeed, using <paragraph> in HTML results in a properly inserted paragraph, however, in fact, the editor doesn't know that you wanted to insert the paragraph :) To prevent the content loss, CKEditor 5 converts to paragraphs all elements which are not recognized. As <paragraph> is not a valid HTML element, it's automatically converted to the paragraph, because the editor doesn't know what to do with it.
While creating features and introducing new elements to the editor, it's important to create a proper model <--> view conversion. The editor should know how to translate view element (basically the HTML structure) to the model and vice versa.
Let's see how the conversion is handled in Implementing an inline widget guide:
conversion.for( 'upcast' ).elementToElement( {
view: {
name: 'span',
classes: [ 'placeholder' ]
},
model: ( viewElement, { writer: modelWriter } ) => {
// Extract the "name" from "{name}".
const name = viewElement.getChild( 0 ).data.slice( 1, -1 );
return modelWriter.createElement( 'placeholder', { name } );
}
} );
conversion.for( 'editingDowncast' ).elementToElement( {
model: 'placeholder',
view: ( modelItem, { writer: viewWriter } ) => {
const widgetElement = createPlaceholderView( modelItem, viewWriter );
// Enable widget handling on a placeholder element inside the editing view.
return toWidget( widgetElement, viewWriter );
}
} );
conversion.for( 'dataDowncast' ).elementToElement( {
model: 'placeholder',
view: ( modelItem, { writer: viewWriter } ) => createPlaceholderView( modelItem, viewWriter )
} );
// Helper method for both downcast converters.
function createPlaceholderView( modelItem, viewWriter ) {
const name = modelItem.getAttribute( 'name' );
const placeholderView = viewWriter.createContainerElement( 'span', {
class: 'placeholder'
} );
// Insert the placeholder name (as a text).
const innerText = viewWriter.createText( '{' + name + '}' );
viewWriter.insert( viewWriter.createPositionAt( placeholderView, 0 ), innerText );
return placeholderView;
}
}
As you can see, <placeholder> model element is represented in the view as <span> with placeholder class. So, if you want to load the placeholder in the initial content, you have to use mentioned <span> element, instead of <placeholder> model element.
To fix your code, replace
<placeholder name="name"></placeholder>
with
<span class="placeholder">{name}</span>
Hi Mgsy,
We have finally managed to fix our issue after many hours over a span of one week. Indeed, the code <span class="placeholder">{name}</span> is the one that works. However, that was not our issue entirely, as we've also tried loading the editor with that code before.
The problem was that we were not inserting this code into other editor blocks, like <paragraph>.
Indeed, the documentation mentions this here:
The placeholder feature will be defined as an inline (text-like) element so it will be inserted into other editor blocks, like
, that allow text. The placeholder will have a name attribute. This means that the model containing some text and a placeholder will look like this: Emphasis mine
However, what threw us off was the example provided further down in the documentation here:
model:
foo<placeholder name="time"></placeholder>bar
view:
foo<span class="placeholder">{name}</span>bar
As you can see, this example does not have the <span> tag enclosed inside any other editor block.
Anyways, we're happy to finally fix this issue and we want to thank you for your assistance.