Add extension points

This commit is contained in:
yflory 2024-05-15 17:09:28 +02:00
parent baa9fec876
commit 30e16bf0e4
6 changed files with 109 additions and 7 deletions

View File

@ -15,7 +15,8 @@ define([
'/common/outer/local-store.js',
'/customize/pages.js',
'/common/pad-types.js',
], function ($, Config, h, Hash, Constants, Util, TextFit, Msg, AppConfig, LocalStore, Pages, PadTypes) {
'/common/extensions.js'
], function ($, Config, h, Hash, Constants, Util, TextFit, Msg, AppConfig, LocalStore, Pages, PadTypes, Extensions) {
var urlArgs = Config.requireConf.urlArgs;
var checkEarlyAccess = function (x) {
@ -164,9 +165,18 @@ define([
};
let popup = h('div.cp-extensions-popups');
let utils = { h, Util, Hash };
Extensions.getExtensions('HOMEPAGE_POPUP').forEach(ext => {
ext.getContent(utils, content => {
$(popup).append(h('div.cp-extensions-popup', content));
});
});
return [
h('div#cp-main', [
Pages.infopageTopbar(),
popup,
notice,
h('div.container.cp-container', [
h('div.row.cp-home-hero', [

View File

@ -297,6 +297,29 @@
}
}
.cp-extensions-popups {
width: 100%;
.cp-extensions-popup {
background-color: @cp_alertify-bg;
border-radius: @infopages-radius-L;
padding: 10px;
box-shadow: 0 0 15px 0 rgba(0, 0, 0, 0.2);
width: 400px;
max-width: 100%;
color: @cryptpad_text_col;
margin-left: 40px;
}
}
@media (max-width: 700px) {
.cp-extensions-popups {
max-width: 90%;
.cp-extensions-popup {
margin-left: 0;
}
}
}
@media (min-width: 576px) and (max-width: 767px) {
.container {
padding-left: 0;

View File

@ -160,6 +160,22 @@ define([
}
};
// EXTENSION_POINT:ADMIN_CATEGORY
common.getExtensions('ADMIN_CATEGORY').forEach(ext => {
if (!ext || !ext.id || !ext.name || !ext.content) {
return console.error('Invalid extension point', 'ADMIN_CATEGORY', ext);
}
if (categories[ext.id]) {
return console.error('Extension point ID already used', ext);
}
console.error(ext);
categories[ext.id] = {
icon: ext.icon,
name: ext.name,
content: ext.content
};
});
const blocks = sidebar.blocks;
const flushCache = (cb) => {
@ -3835,6 +3851,32 @@ define([
cb(opts);
});
// EXTENSION_POINT:ADMIN_ITEM
let utils = {
h, Util, Hash
};
common.getExtensions('ADMIN_ITEM').forEach(ext => {
if (!ext || !ext.id || typeof(ext.getContent) !== "function") {
return console.error('Invalid extension point', 'ADMIN_CATEGORY', ext);
}
if (sidebar.hasItem(ext.id)) {
return console.error('Extension point ID already used', ext);
}
sidebar.addItem(ext.id, cb => {
ext.getContent(common, blocks, utils, content => {
cb(content);
});
}, {
noTitle: !ext.title,
noHint: !ext.description,
title: ext.title,
hint: ext.description
});
});
sidebar.makeLeftside(categories);
};

View File

@ -1,14 +1,13 @@
define([
'/extensions.js'
'optional!/extensions.js'
], (Extensions) => {
console.error(Extensions);
const ext = {};
if (!Array.isArray(Extensions) || !Extensions.length) { return ext; }
let all = Extensions.slice();
while(all.length) {
let current = all.splice(0, 3);
console.error(current);
let f = current[0];
if (typeof(f) !== "function") {
continue;
@ -18,6 +17,22 @@ define([
if (!Object.keys(lang).length && Object.keys(defaultLang).length) {
lang = defaultLang;
}
lang._getKey = function (key, argArray) {
if (!lang[key]) { return '?'; }
var text = lang[key];
if (typeof(text) === 'string') {
return text.replace(/\{(\d+)\}/g, function (str, p1) {
if (typeof(argArray[p1]) === 'string' || typeof(argArray[p1]) === "number") {
return argArray[p1];
}
return '';
});
} else {
return text;
}
};
let currentExt = f(lang) || {};
Object.keys(currentExt).forEach(key => {
@ -26,5 +41,11 @@ define([
});
}
ext.getExtensions = id => {
let e = ext[id];
if (!Array.isArray(e)) { e = []; }
return e;
};
return ext;
});

View File

@ -253,9 +253,9 @@ define([
options = options || {};
const title = options.noTitle ? undefined : h('label.cp-item-label', {
id: `cp-${app}-${key}`
}, Messages[`${app}_${safeKey}Title`] || key);
}, options.title || Messages[`${app}_${safeKey}Title`] || key);
const hint = options.noHint ? undefined : h('span.cp-sidebarlayout-description',
Messages[`${app}_${safeKey}Hint`] || 'Coming soon...');
options.hint || Messages[`${app}_${safeKey}Hint`] || 'Coming soon...');
if (hint && options.htmlHint) {
hint.innerHTML = Messages[`${app}_${safeKey}Hint`];
}
@ -272,6 +272,10 @@ define([
});
};
sidebar.hasItem = key => {
return !key || !!items[key];
};
sidebar.addCheckboxItem = (data) => {
const key = data.key;
let box = blocks.activeCheckbox(data);
@ -325,7 +329,7 @@ define([
'data-category': key
}, [
icon,
Messages[`${app}_cat_${key}`] || key,
category.name || Messages[`${app}_cat_${key}`] || key,
]);
var $item = $(item).appendTo(container);
Util.onClickEnter($item, function () {

View File

@ -777,6 +777,8 @@ define([
return Util.checkRestrictedApp(app, AppConfig, ea, priv.plan, priv.loggedIn);
};
funcs.getExtensions = Ext.getExtensions;
funcs.mailbox = {};
Object.freeze(funcs);