-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpad-select.js
More file actions
205 lines (181 loc) · 7.19 KB
/
Copy pathpad-select.js
File metadata and controls
205 lines (181 loc) · 7.19 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
// padSelect (client side) — wires up the parallel User Settings / Pad Wide
// Settings <select> dropdowns that pad-select-server.js renders. Reads the
// helper's clientVars block, persists the per-user choice in padcookie, and
// forwards pad-wide changes through pad.changePadOption() so they ride the
// existing padoptions COLLABROOM broadcast.
//
// No top-level requires that touch server-only modules — esbuild bundles
// this into the browser pad bundle.
const PLUGIN_NAME_RE = /^ep_[a-z0-9_]+$/;
const validateConfig = (config) => {
if (!config || typeof config !== 'object') {
throw new Error('padSelect requires a config object');
}
const {pluginName, settingId, options, defaultValue} = config;
if (!PLUGIN_NAME_RE.test(pluginName || '')) {
throw new Error(`padSelect pluginName must match /^ep_[a-z0-9_]+$/, got: ${pluginName}`);
}
if (!settingId || typeof settingId !== 'string') {
throw new Error('padSelect requires settingId (string)');
}
if (!Array.isArray(options) || options.length < 2) {
throw new Error('padSelect requires options array of at least 2 entries');
}
if (typeof defaultValue === 'undefined' || defaultValue === null) {
throw new Error('padSelect requires defaultValue');
}
return {pluginName, settingId, options, defaultValue};
};
const padSelectClient = (rawConfig) => {
const {pluginName, settingId, options, defaultValue} = validateConfig(rawConfig);
const userSelectId = `options-${settingId}`;
const padSelectId = `padsettings-options-${settingId}`;
let onChangeCallback = () => {};
let lastEffective = null;
const getPad = () => {
if (typeof window === 'undefined') return null;
try {
// eslint-disable-next-line global-require
const m = require('ep_etherpad-lite/static/js/pad');
if (m && m.pad) return m.pad;
} catch (_e) { /* fall through */ }
return window.pad || (window.top && window.top.pad) || null;
};
const getCookie = () => {
try {
// eslint-disable-next-line global-require
return require('ep_etherpad-lite/static/js/pad_cookie').padcookie;
} catch (_e) { return null; }
};
const getClientVars = () => {
if (typeof window === 'undefined') return null;
return window.clientVars || (window.top && window.top.clientVars) || null;
};
const block = () => {
const cv = getClientVars();
return cv && cv.ep_plugin_helpers && cv.ep_plugin_helpers.padSelect &&
cv.ep_plugin_helpers.padSelect[pluginName] &&
cv.ep_plugin_helpers.padSelect[pluginName][settingId];
};
const isSupportedClient = () => !!(block() && block().padWideSupported);
// Coerce a stored value back to one of the configured option values
// (cookies serialize numbers as strings). Returns undefined if no match.
const coerce = (raw) => {
if (raw == null) return undefined;
const found = options.find((o) => String(o.value) === String(raw));
return found ? found.value : undefined;
};
const readPadValue = () => {
const pad = getPad();
if (!pad || typeof pad.getPadOptions !== 'function') return undefined;
const opts = pad.getPadOptions();
const v = opts && opts[pluginName];
return v ? coerce(v[settingId]) : undefined;
};
const readUserValue = () => {
const cookie = getCookie();
if (!cookie) return undefined;
return coerce(cookie.getPref(settingId));
};
const isEnforced = () => {
const pad = getPad();
return !!(pad && typeof pad.isPadSettingsEnforcedForMe === 'function' &&
pad.isPadSettingsEnforcedForMe());
};
const getEffective = () => {
if (isEnforced()) {
const padVal = readPadValue();
return padVal != null ? padVal : defaultValue;
}
const userVal = readUserValue();
if (userVal != null) return userVal;
const padVal = readPadValue();
return padVal != null ? padVal : defaultValue;
};
const refreshUI = () => {
const $u = window.$(`#${userSelectId}`);
const $p = window.$(`#${padSelectId}`);
const eff = getEffective();
const padVal = readPadValue();
if ($u.length) {
$u.val(String(eff));
$u.prop('disabled', isEnforced());
}
if ($p.length && padVal != null) {
$p.val(String(padVal));
}
if (eff !== lastEffective) {
lastEffective = eff;
try { onChangeCallback(eff); } catch (e) { console.error(e); }
}
};
const init = (opts = {}) => {
onChangeCallback = typeof opts.onChange === 'function' ? opts.onChange : () => {};
const pad = getPad();
const cookie = getCookie();
const $u = window.$(`#${userSelectId}`);
const $p = window.$(`#${padSelectId}`);
if ($u.length) {
$u.val(String(getEffective()));
$u.prop('disabled', isEnforced());
$u.on('change', () => {
if (isEnforced()) {
$u.val(String(getEffective()));
return;
}
const v = coerce($u.val());
if (v == null) return;
if (cookie) cookie.setPref(settingId, v);
refreshUI();
});
}
if ($p.length && pad && typeof pad.changePadOption === 'function') {
const initial = readPadValue();
if (initial != null) $p.val(String(initial));
$p.on('change', () => {
const v = coerce($p.val());
if (v == null) return;
pad.changePadOption(pluginName, {[settingId]: v});
refreshUI();
});
} else if (!isSupportedClient()) {
if (typeof console !== 'undefined' && !init._warned) {
// See pad-toggle.js for the rationale: distinguish missing patch
// (Etherpad < 3.0.0) from missing runtime flag
// (settings.enablePluginPadOptions). Falls back to a generic line
// on servers that don't ship the granular capability fields.
const b = block() || {};
let reason;
if (b.patchPresent != null || b.runtimeEnabled != null) {
if (!b.patchPresent) {
reason = 'server lacks ep_* passthrough patch (Etherpad < 3.0.0)';
} else if (!b.runtimeEnabled) {
reason = 'settings.enablePluginPadOptions is false — set to true ' +
'in settings.json to enable pad-wide options';
} else {
reason = 'pad-wide block not rendered (eejsBlock_padSettings missing)';
}
} else {
reason = 'server lacks ep_* passthrough patch (Etherpad < 3.0.0) ' +
'or runtime flag settings.enablePluginPadOptions is false';
}
console.warn(
`[ep_plugin_helpers.padSelect ${pluginName}] pad-wide settings ` +
`unavailable — ${reason}. Per-user cookie picker still works.`);
init._warned = true;
}
}
lastEffective = getEffective();
try { onChangeCallback(lastEffective); } catch (e) { console.error(e); }
return {getValue: () => lastEffective, refresh: refreshUI};
};
// Plugin re-exports this so the helper sees pad-wide broadcasts and
// refreshes local state when another user changes the pad-wide value.
const handleClientMessage_CLIENT_MESSAGE = (hookName, ctx) => {
if (!ctx || !ctx.payload) return;
if (ctx.payload.type === 'padoptions') refreshUI();
};
return {init, handleClientMessage_CLIENT_MESSAGE};
};
module.exports = {padSelect: padSelectClient, createPadSelect: padSelectClient};