forked from ether/ep_inline_toolbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (79 loc) · 2.92 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
const eejs = require('ep_etherpad-lite/node/eejs/');
const settings = require('ep_etherpad-lite/node/utils/Settings');
const _ = require('ep_etherpad-lite/static/js/underscore');
let inlineMenuItems;
let inlineButtons = [];
let createInlineToolbar = function () {
const toolbar = this.toolbar;
if (toolbar) {
const availableButtons = toolbar.availableButtons;
inlineButtons = [];
inlineMenuItems.forEach((inlineBlock) => {
if (Array.isArray(inlineBlock)) {
const buttons = [];
inlineBlock.forEach((buttonName) => {
let buttonType = null;
let buttonTitle = null;
let localizationId = null;
if (_.isObject(buttonName)) {
const objKey = Object.keys(buttonName)[0];
const keySettings = buttonName[objKey];
buttonType = keySettings.buttonType;
buttonTitle = keySettings.title;
localizationId = keySettings.localizationId;
buttonName = objKey;
}
if (availableButtons[buttonName]) {
let buttonItem = availableButtons[buttonName];
if (availableButtons[buttonName].attributes) {
buttonItem = availableButtons[buttonName].attributes;
}
if (localizationId) {
buttonItem.localizationId = localizationId;
buttonTitle = localizationId;
}
buttonItem = toolbar.button(buttonItem);
let buttonHtml = buttonItem.render();
if (buttonType === 'link') {
buttonHtml = buttonHtml
.replace('<button', '<span')
.replace('</button>', '</span>')
.replace('data-type="button"', 'data-type="link"');
}
if (buttonTitle) {
buttonHtml = buttonHtml.replace('</span>', `${buttonTitle}</span>`);
}
buttons.push(buttonHtml);
}
});
inlineButtons.push(buttons);
}
});
}
};
exports.loadSettings = (hookName, context, cb) => {
inlineMenuItems = context.settings.toolbar.custom_inline;
return cb();
};
exports.clientVars = async (hook, context) => {
// tell the client which year we are in
await createInlineToolbar();
return {ep_custom_inline_toolbar: settings.ep_custom_inline_toolbar, inlineButtons};
};
exports.padInitToolbar = (hookName, context) => {
createInlineToolbar = createInlineToolbar.bind(context);
};
exports.eejsBlock_body = (hookName, args, cb) => {
args.content += eejs.require('ep_custom_inline_toolbar/templates/menuButtons.ejs');
return cb();
};
exports.eejsBlock_mySettings = (hookName, args, cb) => {
args.content += eejs.require('ep_custom_inline_toolbar/templates/settings.ejs');
return cb();
};
// not used
exports.eejsBlock_styles = (hookName, args, cb) => {
args.content += eejs.require('ep_custom_inline_toolbar/templates/styles.html', {}, module);
return cb();
};