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

create a custom button to insert a line of html code #17189

Open
cnaught opened this issue Sep 30, 2024 · 4 comments
Open

create a custom button to insert a line of html code #17189

cnaught opened this issue Sep 30, 2024 · 4 comments
Labels
type:question This issue asks a question (how to...).

Comments

@cnaught
Copy link

cnaught commented Sep 30, 2024

I have a custom button that I add to all of our posts.

Click Here

Is there an easy way to create a custom button that goes on the main toolbar that I can click and it inserts this code without having to use the HTML Embed or doing it through the Source code?

Another question would be is there a way to add the option to edit a class of a link to the link toolbar that comes up when you click on a link?

Thanks!

@cnaught cnaught added the type:question This issue asks a question (how to...). label Sep 30, 2024
@godai78
Copy link
Contributor

godai78 commented Oct 1, 2024

Have you checked out this tutorial? Seems like it might help.

https://ckeditor.com/docs/ckeditor5/latest/framework/tutorials/creating-simple-plugin-timestamp.html

@cnaught
Copy link
Author

cnaught commented Oct 1, 2024

Have you checked out this tutorial? Seems like it might help.

https://ckeditor.com/docs/ckeditor5/latest/framework/tutorials/creating-simple-plugin-timestamp.html

Thank you. I got the button working to insert text. But how do I make it so that it inserted html code into the source, rather than as text?

I'm assuming this line of code needs to be modified?

          // Change the model using the model writer.
          editor.model.change( writer => {

              // Insert the text at the user's current position.
              editor.model.insertContent( writer.createText( '<a href="#">Button</a>' ) );
          } );

@pszczesniak
Copy link
Contributor

Your suspicions are correct, however you can't pass HTML directly here. Take a look at this example:

editor.model.change( writer => {
	// Insert the text at the user's current position.
	const range = editor.model.insertContent( writer.createText( 'some text' ) );
	writer.setSelection( range );
	editor.execute( 'link', 'YOUR_URL' );
} );

this will create

<a href="YOUR_URL">some text</a>

This can be helpful to understand how CKEditor operates on View and Model: https://ckeditor.com/docs/ckeditor5/latest/framework/tutorials/crash-course/model-and-schema.html

@cnaught
Copy link
Author

cnaught commented Oct 2, 2024

this will create

<a href="YOUR_URL">some text</a>

This can be helpful to understand how CKEditor operates on View and Model: https://ckeditor.com/docs/ckeditor5/latest/framework/tutorials/crash-course/model-and-schema.html

I got that to work inserting a simple a href tag, and read through the documentation.

I played around with a few options, but I couldn't get what I needed. I am trying to do is insert some HTML like the following:

<a class="btn btn-warning text-white" target="_blank" rel="noopener noreferrer" href="#"><strong>Get This Deal</strong></a>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question This issue asks a question (how to...).
Projects
None yet
Development

No branches or pull requests

3 participants