|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// padToggle (server side) — emits parallel checkboxes in the User Settings |
| 4 | +// (mySettings) and Pad Wide Settings (padSettings) panels, mirroring native |
| 5 | +// Etherpad behavior. Pad-wide values ride the existing padoptions COLLABROOM |
| 6 | +// rail (stored at pad.padOptions[pluginName] = {enabled: bool}) when the core |
| 7 | +// has the ep_* passthrough patch (Etherpad >= 2.7.4); on older cores the |
| 8 | +// pad-wide block silently no-ops and the user-side cookie toggle alone still |
| 9 | +// works. |
| 10 | +// |
| 11 | +// This module is intended for server-side import only. The companion |
| 12 | +// `pad-toggle.js` provides the client-side init/handleClientMessage hooks. |
| 13 | +// They share the same config; plugin authors should use identical |
| 14 | +// `pluginName`, `settingId`, `l10nId`, and `defaultEnabled` on both sides so |
| 15 | +// the checkbox ids and clientVars block line up. |
| 16 | + |
| 17 | +const PLUGIN_NAME_RE = /^ep_[a-z0-9_]+$/; |
| 18 | + |
| 19 | +let padOptionsPluginPassthrough = false; |
| 20 | +try { |
| 21 | + // The require lands on a leaf module on patched cores (Etherpad >= 2.7.4) |
| 22 | + // and throws on older cores. Server-only: this file is never bundled for |
| 23 | + // the browser, so esbuild's static analysis does not run here. |
| 24 | + // eslint-disable-next-line global-require |
| 25 | + const caps = require('ep_etherpad-lite/node/utils/PluginCapabilities'); |
| 26 | + padOptionsPluginPassthrough = caps && caps.padOptionsPluginPassthrough === true; |
| 27 | +} catch (_e) { /* older core — leave as false */ } |
| 28 | + |
| 29 | +const HTML_ESCAPE_RE = /[&<>"']/g; |
| 30 | +const HTML_ESCAPES = {'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}; |
| 31 | +const escapeHtml = (s) => String(s).replace(HTML_ESCAPE_RE, (c) => HTML_ESCAPES[c]); |
| 32 | + |
| 33 | +const validateConfig = (config) => { |
| 34 | + if (!config || typeof config !== 'object') { |
| 35 | + throw new Error('padToggle requires a config object'); |
| 36 | + } |
| 37 | + const {pluginName, settingId, l10nId, defaultLabel, defaultEnabled = true} = config; |
| 38 | + if (!PLUGIN_NAME_RE.test(pluginName || '')) { |
| 39 | + throw new Error( |
| 40 | + `padToggle pluginName must match /^ep_[a-z0-9_]+$/, got: ${pluginName}`); |
| 41 | + } |
| 42 | + if (!settingId || typeof settingId !== 'string') { |
| 43 | + throw new Error('padToggle requires settingId (string)'); |
| 44 | + } |
| 45 | + if (!l10nId || typeof l10nId !== 'string') { |
| 46 | + throw new Error('padToggle requires l10nId (string) — i18n is mandatory'); |
| 47 | + } |
| 48 | + if (!defaultLabel || typeof defaultLabel !== 'string') { |
| 49 | + throw new Error( |
| 50 | + 'padToggle requires defaultLabel (string) — accessibility fallback ' + |
| 51 | + 'rendered inside <label> so screen readers announce something before ' + |
| 52 | + 'html10n loads. html10n overwrites it at runtime via data-l10n-id.'); |
| 53 | + } |
| 54 | + return {pluginName, settingId, l10nId, defaultLabel, defaultEnabled: !!defaultEnabled}; |
| 55 | +}; |
| 56 | + |
| 57 | +const renderCheckbox = (settingId, l10nId, defaultLabel, idPrefix) => |
| 58 | + `<p>` + |
| 59 | + `<input type="checkbox" id="${idPrefix}options-${settingId}">` + |
| 60 | + `<label for="${idPrefix}options-${settingId}" ` + |
| 61 | + `data-l10n-id="${escapeHtml(l10nId)}">${escapeHtml(defaultLabel)}</label>` + |
| 62 | + `</p>`; |
| 63 | + |
| 64 | +const padToggleServer = (rawConfig) => { |
| 65 | + const {pluginName, settingId, l10nId, defaultLabel, defaultEnabled} = validateConfig(rawConfig); |
| 66 | + let cachedDefaultEnabled = defaultEnabled; |
| 67 | + |
| 68 | + const loadSettings = async (hookName, args) => { |
| 69 | + const ps = (args && args.settings && args.settings[pluginName]) || {}; |
| 70 | + if (typeof ps.defaultEnabled === 'boolean') cachedDefaultEnabled = ps.defaultEnabled; |
| 71 | + }; |
| 72 | + |
| 73 | + const clientVars = async (hookName, ctx) => { |
| 74 | + let initialPadEnabled = cachedDefaultEnabled; |
| 75 | + try { |
| 76 | + const padSettings = ctx && ctx.pad && typeof ctx.pad.getPadSettings === 'function' |
| 77 | + ? ctx.pad.getPadSettings() : null; |
| 78 | + const stored = padSettings && padSettings[pluginName]; |
| 79 | + if (stored && typeof stored.enabled === 'boolean') initialPadEnabled = stored.enabled; |
| 80 | + } catch (_e) { /* leave initialPadEnabled at instance default */ } |
| 81 | + |
| 82 | + return { |
| 83 | + ep_plugin_helpers: { |
| 84 | + padToggle: { |
| 85 | + [pluginName]: { |
| 86 | + padWideSupported: padOptionsPluginPassthrough, |
| 87 | + settingId, |
| 88 | + l10nId, |
| 89 | + defaultEnabled: cachedDefaultEnabled, |
| 90 | + initialPadEnabled, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }; |
| 95 | + }; |
| 96 | + |
| 97 | + const eejsBlock_mySettings = (hookName, args, cb) => { |
| 98 | + args.content += renderCheckbox(settingId, l10nId, defaultLabel, ''); |
| 99 | + return cb(); |
| 100 | + }; |
| 101 | + |
| 102 | + const eejsBlock_padSettings = (hookName, args, cb) => { |
| 103 | + if (!padOptionsPluginPassthrough) return cb(); |
| 104 | + args.content += renderCheckbox(settingId, l10nId, defaultLabel, 'padsettings-'); |
| 105 | + return cb(); |
| 106 | + }; |
| 107 | + |
| 108 | + return {loadSettings, clientVars, eejsBlock_mySettings, eejsBlock_padSettings}; |
| 109 | +}; |
| 110 | + |
| 111 | +module.exports = {padToggle: padToggleServer, createPadToggle: padToggleServer}; |
0 commit comments