Added document max length limit, enforceMaxLength on change and show modal when reach the limit.
This commit is contained in:
@@ -909,10 +909,13 @@ socket.on('permission', function (data) {
|
||||
permission = data.permission;
|
||||
checkPermission();
|
||||
});
|
||||
var docmaxlength = null;
|
||||
var otk = null;
|
||||
var owner = null;
|
||||
var permission = null;
|
||||
socket.on('refresh', function (data) {
|
||||
docmaxlength = data.docmaxlength;
|
||||
editor.setOption("maxLength", docmaxlength);
|
||||
otk = data.otk;
|
||||
owner = data.owner;
|
||||
permission = data.permission;
|
||||
@@ -1435,10 +1438,30 @@ function buildCursor(user) {
|
||||
}
|
||||
|
||||
//editor actions
|
||||
function enforceMaxLength(cm, change) {
|
||||
var maxLength = cm.getOption("maxLength");
|
||||
if (maxLength && change.update) {
|
||||
var str = change.text.join("\n");
|
||||
var delta = str.length - (cm.indexFromPos(change.to) - cm.indexFromPos(change.from));
|
||||
if (delta <= 0) {
|
||||
return false;
|
||||
}
|
||||
delta = cm.getValue().length + delta - maxLength;
|
||||
if (delta > 0) {
|
||||
str = str.substr(0, str.length - delta);
|
||||
change.update(change.from, change.to, str.split("\n"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
var ignoreEmitEvents = ['setValue', 'ignoreHistory'];
|
||||
editor.on('beforeChange', function (cm, change) {
|
||||
if (debug)
|
||||
console.debug(change);
|
||||
if (enforceMaxLength(cm, change)) {
|
||||
$('.limit-modal').modal('show');
|
||||
}
|
||||
var isIgnoreEmitEvent = (ignoreEmitEvents.indexOf(change.origin) != -1);
|
||||
if (!isIgnoreEmitEvent) {
|
||||
switch (permission) {
|
||||
|
||||
@@ -110,4 +110,23 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- limit modal -->
|
||||
<div class="modal fade limit-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-sm">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
|
||||
</button>
|
||||
<h4 class="modal-title" id="myModalLabel"><i class="fa fa-exclamation-triangle"></i> Reach the limit</h4>
|
||||
</div>
|
||||
<div class="modal-body" style="color:black;">
|
||||
<h5>Sorry, you've reached the max length this note can be.</h5>
|
||||
<strong>Please reduce the content or divided it to more notes, thank you!</strong>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-warning" data-dismiss="modal">OK</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user