0

I'm using TinyMCE to edit text. When the user selects some text and presses a button, the selected section should be wrapped with <mark></mark> html tags.

The reason I'm not simply adding a custom text formatting button to the tinymce editor is because I want to trigger other actions to happen with the selected text too (i.e. copy and paste the text elsewhere).

How can I do this?

1 Answer 1

1

You can add your own custom button to the TinyMCE toolbar and trigger JavaScript when its clicked. You can then use the editor APIs to wrap the selected text with the tag and perform other work as needed.

Here is a TinyMCE Fiddle that shows the basic process: http://fiddle.tinymce.com/N1gaab

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Your solution works perfectly for marking within a single element. However, I would also like this to work over multiple elements (e.g. selection starts halfway through paragraph 1 and finishes in paragraph 2 or 3). The inbuilt bold, italics, etc can do this fine, so I assume I need to use something else from the TinyMCE API rather than insertContent(). Do you have any suggestions?
I solved the problem of applying across elements by adding formats: {mark: { inline: 'mark' }} to the Editor init property and then calling this.state.editor.formatter.apply('mark') when the button is pressed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.