From 30e16bf0e4014148fc1fefb9d4ed4ff3c42263f8 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 15 May 2024 17:09:28 +0200 Subject: [PATCH] Add extension points --- customize.dist/pages/index.js | 12 +++++- .../src/less2/pages/page-index.less | 23 ++++++++++ www/admin/inner.js | 42 +++++++++++++++++++ www/common/extensions.js | 27 ++++++++++-- www/common/inner/sidebar-layout.js | 10 +++-- www/common/sframe-common.js | 2 + 6 files changed, 109 insertions(+), 7 deletions(-) diff --git a/customize.dist/pages/index.js b/customize.dist/pages/index.js index 1f2608792..27d510961 100644 --- a/customize.dist/pages/index.js +++ b/customize.dist/pages/index.js @@ -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', [ diff --git a/customize.dist/src/less2/pages/page-index.less b/customize.dist/src/less2/pages/page-index.less index 7873f5f55..859e376b1 100644 --- a/customize.dist/src/less2/pages/page-index.less +++ b/customize.dist/src/less2/pages/page-index.less @@ -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; diff --git a/www/admin/inner.js b/www/admin/inner.js index cd1360d9c..18ac4cb7a 100644 --- a/www/admin/inner.js +++ b/www/admin/inner.js @@ -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); }; diff --git a/www/common/extensions.js b/www/common/extensions.js index 0fad27b03..cfd2ca39e 100644 --- a/www/common/extensions.js +++ b/www/common/extensions.js @@ -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; }); diff --git a/www/common/inner/sidebar-layout.js b/www/common/inner/sidebar-layout.js index f6adcb66c..ec21a0236 100644 --- a/www/common/inner/sidebar-layout.js +++ b/www/common/inner/sidebar-layout.js @@ -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 () { diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index cd893395a..c297b5273 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -777,6 +777,8 @@ define([ return Util.checkRestrictedApp(app, AppConfig, ea, priv.plan, priv.loggedIn); }; + funcs.getExtensions = Ext.getExtensions; + funcs.mailbox = {}; Object.freeze(funcs);