summaryrefslogtreecommitdiffstats
path: root/static/js/index.js
blob: fd9cc061957a1d2a415ad25a6481fd72e19ef39c (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
var $ = require('ep_etherpad-lite/static/js/rjquery').$;
var _ = require('ep_etherpad-lite/static/js/underscore');

var cssFiles = ['ep_headline_buttons/static/css/headline_buttons.css'];
var tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];

function aceEditorCSS(){
    return cssFiles;
};

function postAceInit(ace, pad) {
    $('.headline-button').on('click', function() {
        var data = $(this).data('key');
        if (typeof data !== 'undefined') {
            pad.ace.callWithAce(function (ace) {
                ace.ace_doToggleHeading(data);
            }, data, true);
        }
    });
};

function aceInitialized(hook, context) {
    function doToggleHeading(tag) {
        var rep = this.rep;
        var attrsManager = this.documentAttributeManager;

        if (!(rep.selStart && rep.selEnd)) {
            return;
        }

        var firstLine = rep.selStart[0];
        var lastLine = Math.max(firstLine, rep.selEnd[0] - ((rep.selEnd[1] === 0) ? 1 : 0));
        _(_.range(firstLine, lastLine + 1)).each(function(i) {
            if (attrsManager.getAttributeOnLine(i, 'headline_buttons') == tag) {
                attrsManager.removeAttributeOnLine(i, 'headline_buttons');
            }
            else {
                attrsManager.setAttributeOnLine(i, 'headline_buttons', tag);
            }
        });
    }

    context.editorInfo.ace_doToggleHeading = _(doToggleHeading).bind(context);
}

function collectContentPre(hook, ctx) {
    if ($.inArray(ctx.tname, tags) >= 0) {
        ctx.state.lineAttributes['headline_buttons'] = ctx.tname;
    }
};

function aceRegisterBlockElements() {
    return tags;
};

function aceDomLineProcessLineAttributes(hook, ctx) {
    if (typeof ctx.cls !== 'undefined') {
        var headline_type = ctx.cls.match(/(?:^| )headline_buttons:(\S+)/);
        if (headline_type) {
            return [{
                processedMarker: true,
                preHtml: '<' + headline_type[1] + '>',
                postHtml: '</' + headline_type[1] + '>'
            }];
        }
    }
};

function aceAttribsToClasses(hook, ctx) {
    if (ctx.key == 'headline_buttons' && ctx.value != '') {
        return ['headline_buttons:' + ctx.value];
    }
};

exports.aceEditorCSS = aceEditorCSS;
exports.aceInitialized = aceInitialized;
exports.postAceInit = postAceInit;
exports.collectContentPre = collectContentPre;
exports.aceRegisterBlockElements = aceRegisterBlockElements;
exports.aceDomLineProcessLineAttributes = aceDomLineProcessLineAttributes;
exports.aceAttribsToClasses = aceAttribsToClasses;