all repos — litestore @ 2eabe215b76a6d83b65a611a32eae0a693472a19

A minimalist nosql document store.

src/admin/js/components/editor.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
(function () {
    "use strict";
    var app = window.LS || (window.LS = {});
    app.editor = {};

    app.editor.config = function (obj) {
        return function (element, isInitialized, context) {
            var e = element;
            if (!isInitialized) {
                var editor = ace.edit(e);
                obj.editor = editor;
                e.style.position = "relative";
                editor.updateMode = function (filename) {
                    var self = this;
                    var ext = "";
                    try {
                        ext = filename.match(/\.([a-z0-9]+)$/)[1];
                    } catch (e) {
                        ext = "";
                    }
                    switch (ext) {
                        case "js":
                            obj.mode = "javascript";
                            break;
                        case "css":
                            obj.mode = "css";
                            break;
                        case "html":
                            obj.mode = "html";
                            break;
                        case "json":
                            obj.mode = "json";
                            break;
                        case "md":
                            obj.mode = "markdown";
                            break;
                        default:
                            obj.mode = "text";
                    }
                    self.getSession().setMode("ace/mode/" + obj.mode);
                };
                editor.setOptions({ maxLines: Infinity });
                editor.setReadOnly(obj.readOnly);
                editor.setShowPrintMargin(false);
                editor.setTheme("ace/theme/github");
                editor.updateMode(obj.id());
                editor.getSession().setUseWrapMode(true);
                editor.getSession().setTabSize(2);
            }
        };
    };

    /**
     * @param {Function} ctrl
     * @param {Object} args
     * @param {string} args.content
     */
    app.editor.view = function (ctrl, args) {
        if (!args.ext || args.ext === "json") {
            args.content = args.content ? JSON.stringify(args.content) : "";
        }
        return m(
            ".editor.panel.panal-default",
            { config: app.editor.config(args) },
            args.content
        );
    };
})();