Ace: New Feature: Provide support for max length option

Created on 24 Aug 2017  ·  3Comments  ·  Source: ajaxorg/ace

Sometimes is important set a limit of the script.

Most helpful comment

To ensure the resulting length is the one that gets limited, and to allow large pastes by trimming the end, try this edit:

var doc = editor.session.doc
doc.applyAnyDelta = doc.applyAnyDelta || doc.applyDelta
doc.applyDelta = function(delta) {
    let joinedLines = delta.lines.join("\n")

    if (delta.action == "insert" && this.$maxLength
        && this.getValue().length + joinedLines.length > this.$maxLength) {

        let newPasteLength = this.$maxLength - this.getValue().length
        if(newPasteLength > 0) {
            delta.lines = joinedLines.substr(0, newPasteLength).split("\n")
            if(delta.lines.length == 1 && delta.start.row == delta.end.row) {
                delta.end = {
                    row: delta.start.row,
                    column: delta.start.column+newPasteLength
                }
            } else {
                delta.end = {
                    row: delta.start.row+delta.lines.length,
                    column: delta.lines[delta.lines.length-1].length
                }
            }
        } else return false;
    }
    return this.applyAnyDelta(delta);
}
doc.$maxLength = 20

Might be beneficial to optimize it more for production, but I hope this helps someone anyway!

Also, I ran into the same use case as @christianbs :)

All 3 comments

You can use the following code to disable inserting if editor value is longer than some limit

var doc = editor.session.doc
doc.applyAnyDelta = doc.applyAnyDelta || doc.applyDelta
doc.applyDelta = function(delta) {
   if (delta.action == "insert" && this.$maxLength 
       && this.getValue().length > this.$maxLength) {
       return false;
   }
   return this.applyAnyDelta(delta);
}
doc.$maxLength = 20

Could you please tell in which cases do you need this, to help us understand whether this needs to be a part of default set of options or an extension.

Of course!! So, in our application, the script is persisted at database and there's a limit to this column. I think this should be very common because the editor actually is very used in web applications. On the other hand, the script above should solve my problem... thanks

To ensure the resulting length is the one that gets limited, and to allow large pastes by trimming the end, try this edit:

var doc = editor.session.doc
doc.applyAnyDelta = doc.applyAnyDelta || doc.applyDelta
doc.applyDelta = function(delta) {
    let joinedLines = delta.lines.join("\n")

    if (delta.action == "insert" && this.$maxLength
        && this.getValue().length + joinedLines.length > this.$maxLength) {

        let newPasteLength = this.$maxLength - this.getValue().length
        if(newPasteLength > 0) {
            delta.lines = joinedLines.substr(0, newPasteLength).split("\n")
            if(delta.lines.length == 1 && delta.start.row == delta.end.row) {
                delta.end = {
                    row: delta.start.row,
                    column: delta.start.column+newPasteLength
                }
            } else {
                delta.end = {
                    row: delta.start.row+delta.lines.length,
                    column: delta.lines[delta.lines.length-1].length
                }
            }
        } else return false;
    }
    return this.applyAnyDelta(delta);
}
doc.$maxLength = 20

Might be beneficial to optimize it more for production, but I hope this helps someone anyway!

Also, I ran into the same use case as @christianbs :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

velara3 picture velara3  ·  5Comments

ketysek picture ketysek  ·  3Comments

mafar picture mafar  ·  4Comments

akosyakov picture akosyakov  ·  3Comments

bostondevin picture bostondevin  ·  5Comments