Asynchronous Editor Template


Use this template when your editor's read/write operations are asynchronous (return Promises). Examples include CKEditor, TinyMCE, Quill, and Froala.

All methods must return a Promise.
// Initialize the Asynchronous TextWriter
var myWriter = new INVOX.TextWriterBaseAsync()

/* --------- ESSENTIAL METHODS --------- */

// Allows you to specify the current editor instance.
myWriter.setEditor = function(editorInstance) {
    if (!editorInstance) {
        throw "Parameter editorInstance cannot be null or undefined"
    }
    this.editor = editorInstance
    return Promise.resolve()
}

// Allows you to get the current editor instance.
myWriter.getEditor = function() {
    return Promise.resolve(this.editor)
}

// Allows you to get the editor text content.
myWriter.getText = async function() {
    const currentEditor = await this.getEditor()
    return getTextFromEditor(currentEditor)

    function getTextFromEditor(currentEditor) {
        let text = ""
        // TODO: Here goes your code...
        return Promise.resolve(text)
    }
}

// Allows you to get current selection range.
myWriter.getSelection = async function() {
    const currentEditor = await this.getEditor()
    const { start, end } = await getSelectionFromEditor(currentEditor)
    const range = new INVOX.Range(start, end)
    return Promise.resolve(range)

    function getSelectionFromEditor(currentEditor) {
        let start = 0
        let end = 0
        // TODO: Here goes your code...
        return Promise.resolve({ start, end })
    }
}

// Allows you to set current selection range.
myWriter.setSelection = async function(range) {
    const { start, end } = range
    const currentEditor = await this.getEditor()
    return setSelectionInEditor(currentEditor, start, end)

    function setSelectionInEditor(currentEditor, start, end) {
        // TODO: Here goes your code...
        return Promise.resolve()
    }
}

// Allows you to write in the editor where the caret is.
myWriter.write = async function(text) {
    const currentEditor = await this.getEditor()
    return writeTextInEditor(currentEditor, text)

    function writeTextInEditor(currentEditor, text) {
        // TODO: Here goes your code...
        return Promise.resolve()
    }
}

/* --------- EXTRA METHODS --------- */

// Allows you to apply redo in the editor.
myWriter.redo = async function() {
    const currentEditor = await this.getEditor()
    return applyRedo(currentEditor)

    function applyRedo(currentEditor) {
        // TODO: Here goes your code...
        return Promise.resolve()
    }
}

// Allows you to apply undo in the editor.
myWriter.undo = async function() {
    const currentEditor = await this.getEditor()
    return applyUndo(currentEditor)

    function applyUndo(currentEditor) {
        // TODO: Here goes your code...
        return Promise.resolve()
    }
}

// Allows you to manage the status of redo/undo operations.
myWriter.updateRedoUndoStack = async function() {
    const currentEditor = await this.getEditor()
    return updateRedoUndoStack(currentEditor)

    function updateRedoUndoStack(currentEditor) {
        // TODO: Here goes your code...
        return Promise.resolve()
    }
}

// Allows you to specify the line break format by the editor used.
myWriter.writeNewLine = async function() {
    // By default this function writes "\n".
    // Override if your editor uses a different format:
    // return this.write("<br>")
    return Promise.resolve()
}

// Allows you to specify the paragraph break format by the editor used.
myWriter.writeNewParagraph = async function() {
    // By default this function writes two line breaks "\n\n".
    // Override if your editor uses a different format:
    // return this.write("<br><br>")
    return Promise.resolve()
}