-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Custom heading dropdown menu #651
Comments
It looks like you only have to change that view-to-model conversion. You'd like to define a conversion from element to element with custom creator function that returns model's element. Something like this: buildViewConverter().for( data.viewToModel )
.fromElement( 'h1' )
.toElement( viewElement => {
const classNames = [ ...viewElement.getClassNames() ];
const headingType = getHeadingTypeFromClassNames( classNames );
// headingType should be one of defined heading elements like 'headingNews'.
return new ModelElement( headingType );
} ); Obviously you need to implement ps.: If you have any questions in future we're also active on Stack Overflow (on ckeditor5 tag). |
Thank you for your answer, it work great, Just an another little question about that : |
I've checked similar thing in original // Build converter from view to model for data pipeline.
buildViewConverter().for( data.viewToModel )
.fromElement( option.viewElement )
.toElement( viewElement => {
console.log('converting', viewElement);
return new ModelElement( option.modelElement );
} ); I'd check if you don't have |
no I didn't doubled it come from the options array, if I put other elements like that :
the problem is just about h1 because there are two h1 in the array. But may be it's normal behavior ? |
Would it be possible tor you to post all the custom code you have in a jsfiddle? It's hard to guess without seeing all the code together. |
Hi, here it is : https://jsfiddle.net/2tk84ypg/1/ |
OK, it's clearer now. The You should remove Something like (but probably would need some optimizations): for ( const option of options ) {
// Schema. (probably this part needs some optimization too - like own loop
// with only unque elements to not defnie h1 twice).
editor.document.schema.registerItem( option.modelElement, '$block' );
// Build converter from model to view for data and editing pipelines.
buildModelConverter().for( data.modelToView, editing.modelToView )
// ... rest of code
}
// Now build each header element once:
buildViewConverter().for( data.viewToModel )
.fromElement( 'h1' )
// the rest of logic for converting h1 with different classes
buildViewConverter().for( data.viewToModel )
.fromElement( 'h2' )
// the rest of logic for converting h2 with different classes So the reason why |
ok thanks ! i've upadted the code, but it still fired twice, may be I didn't well understood how to write the "//rest of logic" as you though ? |
ps : have removed the |
Sorry... it was just a cache problem I will try to optimize it now. Thank you very much for your help. And thank a lot to all ckeditor 5 devs to your work, I very enjoy the way it go ! Have a good day |
a little optimization : https://jsfiddle.net/2tk84ypg/4/ need again to allow "p" and multi class |
Hi,
with that :
It seems to work (don't know if there is side effect), but the problem is that it need to put the "options" array in two place : in the heading plugin (or in the create function of ckeditor) and in the paragraph plugin. Anyway to make it nicer ? |
Conversion, like most things in the editor, is event-based. And listeners have priorities. So, conceptually speaking, you should be able to use the default @scofalik could you create an example how to do that with |
Thank you. It is what I thought, but when I did a try like that, by simply add a custom "p" in the options array in headingengine.js (without changing anything in paragraph.js plugin) : The buildViewConverter().for( data.viewToModel ) is working, but the paragraph plugin seem to have priority on it and overwrite it (if I disabled the buidViewConverter() method in the paragraph.js plugin it work well) I'll appreciate an example |
I've added "withPriority"
https://jsfiddle.net/2tk84ypg/8/ And it seem to work |
Yep, if it works, then that would be it. I thought that you may need to use a more complicated notation with |
Yes it seems to work but don't know if there could be side effects... wait and see. Anyway, I'll stay with that until I'll know v.5 more in depth... |
some small improvements
|
In #638 (comment) I proposed a config format which would allow everyone to easily configure which elements should be handled/rendered by which heading option. |
Enhancement: Added support for `ConverterDefinition` in the heading options configuration. Closes #72. Closes ckeditor/ckeditor5#651. BREAKING CHANGE: `config.heading.options` format has changed. The valid `HeadingOption` syntax is now compatible with `ConverterDefinition`: `{ model: 'heading1', view: 'h2', title: 'Heading 1' }`.
Hi,
I'm trying to add a custom heading dropdown menu in the toolbar of ckeditor 5 (something like "block/inline styles menu" in ckeditor 4).
In this menu there will be for example these items : "News title", "News paragraph", "Interview Title", "Interview paragraph", "Interview citation", etc...
and it will create html element with class attribute :
<h1 class="news">lorem</h1>, <h1 class="interview">ipsum</h1>, <p class="interview">lorem</p>, etc...
I've "cloned" the heading plug-in, and with the help of this : #481
i've modified headingengine.js like that :
when I select a menu item it work well and create the corresponding html element with the correct class name, and when deselect the heading text and reselect it again the menu change consequently.
but when there is already html content in the editor or when paste this content on it :
the second h1 (in this example) get class="news" attribute
What need to be changed to work correctly ?
May be some thing not far from in the #481 example ?
Sorry my bad english, hope this understandable
The text was updated successfully, but these errors were encountered: