diff options
author | Wu Cheng-Han | 2015-07-16 22:46:06 +0800 |
---|---|---|
committer | Wu Cheng-Han | 2015-07-16 22:46:06 +0800 |
commit | d14c5bdc9cabf632262067466ef02374cee496cd (patch) | |
tree | 63baaaa8c0205f55cb30f7e353e0727f14543851 /public/js | |
parent | 57253d28a7ad2037ba4d5da69d69f653e651c49b (diff) |
Added document max length limit, enforceMaxLength on change and show modal when reach the limit.
Diffstat (limited to '')
-rw-r--r-- | public/js/index.js | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/public/js/index.js b/public/js/index.js index c90255e4..4068d59b 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -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) { |