How to make an element in a TinyMCE editor treated as a non-editable single item?

回眸只為那壹抹淺笑 提交于 2019-12-31 21:40:31

问题


Within our CMS, our users can edit error messages for forms using TinyMCE. The problem is, some of these messages may require dynamic data, e.g. "Your chosen name 'X' is invalid".

To do this, we are using tokens. Using the above as an example, our user would type in "Your chosen name '{name}' is invalid", and it would get replaced on render.

I'm currently writing a plugin for TinyMCE to better help the user manage these tokens. They would click the icon, select a token from a predefined list, and it would get inserted into the content automatically.

The problem I have is how this token is treated within the content editor. At present, it's just text that can be edited, but what I want to do is make TinyMCE treat it as a single element so it can be removed, moved, but the actual token itself can not be edited.

I've tried using the noneditable plugin by wrapping the token in a span with the mceNonEditable class but this doesn't behave as expected, e.g. you can't backspace delete the token, if the token is at the end of the content you can't type after it, etc.

So to clarify, what I essentially want is for TinyMCE to treat some text like it would an image. It would be a solid block, a.k.a. treated as one character/element.

Is this possible?

Thank you.


回答1:


After trying about 100 different work arounds, we finally found a solution which works (for our needs anyway).

We're inserting a disabled button with all the styles removed. For example,

<input type="button" class="token" value="my token" disabled="disabled" />

And then adding some code to onGetContent() to convert the input to plain text.

This solution, although not very elegant,

  1. Makes the token behave as a single character/item.
  2. Makes it read only.
  3. Is dynamic in the context of the editor so we can display our token text.

Edit: This is what we ended up with.




回答2:


As a duck-tape programmer, I have to ask - can't you use an image? I think TinyMCE used smilies, maybe you can replace those with "elementies" and allow customer to insert them and on save convert to tokens.




回答3:


You could use an image-tag as token. Using a tinymce UI button to insert this image should work if you replace it after the user has submitted the error message form. For example the image tag you insert could look like this

<img  src="../image/token1.gif" class="token1">

This way you only need to replace image-tags with the class token1 after the submit/save.




回答4:


I know this is a bit late, but there's now support for this using the plugin https://www.tiny.cloud/docs/plugins/noneditable/



来源:https://stackoverflow.com/questions/4885396/how-to-make-an-element-in-a-tinymce-editor-treated-as-a-non-editable-single-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!