开发者

A WYSIWYG Where it's possible to limit the text/height

开发者 https://www.devze.com 2023-04-09 11:07 出处:网络
Ok, so I tried out tinyMCE. After no luck and a lot of research on how to limit the editors content, I\'m looking for other alternatives.

Ok, so I tried out tinyMCE.

After no luck and a lot of research on how to limit the editors content, I'm looking for other alternatives.

These are the needs for the WYSIWYG:

  • Able to have these function/buttons: bold,italic,underline,bull list, table controls
  • Able to limit the input. If I set the editor to 300 width x 500 height, and you type more than the height, it should NOT apply a scroller and you sho开发者_运维知识库uld be unable to write more.
  • Able to set multiple editors in one page

Which WYSIWYG editor can fill my needs?


A simple approach would be to use a div element and put the content of the editor inside that div. The div should have set a width (using css).

<div class="calculation_preview"></div>

And the css should be something like:

div.calculation_preview {
   width: 200px;
   visibility: hidden;
   position: absolute;
   top: 0px;
   left:0px;
}

After each key-stroke, you can measure the height of the div (using the function offsetHeight on the div). If it is too height, remove the last character the user entered.

To restore the previous content, you can declare a javascript variable as for example:

var savedContent = "";

If you have some initial content in your editor, then you should initialize the variable with that content. For each key-stroke, you do the following:

// Get current editor content:
var content = tinyMCE.activeEditor.getContent({format : 'raw'});
// Measure the new height of the content:
var measurer = document.getElementById("calculation_preview");
measurer.innerHTML = content;
if(measurer.offsetHeight > 100) {
   // Content is too big.. restore previous:
   tinyMCE.activeEditor.setContent(savedContent, {format : 'raw'});
} else {
   // Content height is ok .. save to variable:
   savedContent = content;
}


TinyMCE or ck editor should meet your needs.

Regarding the scroll issue, try using jquery and the keypress event along to test the length of the content in the div holding the editor. You could also combine this with a character counter on the screen to dynamically display how many characters a user has left.

charCount = $(this).val().length;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号