domma-cms 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -267,3 +267,59 @@ export function _resetWidgetsForTests() {
267
267
  owners.clear();
268
268
  _registered.length = 0;
269
269
  }
270
+
271
+ // ---------------------------------------------------------------------------
272
+ // Boards - a person's several named layouts, shown as tabs
273
+ // ---------------------------------------------------------------------------
274
+
275
+ export const MAX_BOARDS = 12;
276
+ export const BOARD_NAME_MAX = 40;
277
+ const BOARD_ID_RE = /^[a-z0-9][a-z0-9-]{0,39}$/;
278
+
279
+ const boardName = (v, fallback) => String(v ?? '').replace(/[\u0000-\u001f<>]/g, '').replace(/\s+/g, ' ').trim().slice(0, BOARD_NAME_MAX) || fallback;
280
+
281
+ /**
282
+ * A person's boards, cleaned. Accepts the stored v2 shape
283
+ * ({version: 2, active, boards: [{id, name, widgets}]}) and the v1 shape a
284
+ * board-less layout file has ({widgets}), which becomes one board, "Main".
285
+ * There is always at least one board, and `active` always names one of them.
286
+ *
287
+ * @param {*} input
288
+ * @returns {{version: number, active: string, boards: Array<{id: string, name: string, widgets: object[]}>}}
289
+ */
290
+ export function normaliseBoards(input) {
291
+ const src = input && typeof input === 'object' ? input : {};
292
+ const list = Array.isArray(src.boards) ? src.boards : (Array.isArray(src.widgets) || Array.isArray(src) ? [{id: 'main', name: 'Main', widgets: src.widgets || src}] : []);
293
+ const seen = new Set();
294
+ const boards = [];
295
+ for (const b of list) {
296
+ const id = String(b?.id || '').trim();
297
+ if (!BOARD_ID_RE.test(id) || seen.has(id)) continue;
298
+ seen.add(id);
299
+ boards.push({id, name: boardName(b.name, `Board ${boards.length + 1}`), widgets: normaliseLayout(b.widgets).widgets});
300
+ if (boards.length >= MAX_BOARDS) break;
301
+ }
302
+ if (!boards.length) boards.push({id: 'main', name: 'Main', widgets: []});
303
+ const active = boards.some(b => b.id === src.active) ? src.active : boards[0].id;
304
+ return {version: 2, active, boards};
305
+ }
306
+
307
+ /** What is wrong with a submitted set of boards ([] when it is fine). */
308
+ export function boardsProblems(input) {
309
+ if (!input || typeof input !== 'object' || !Array.isArray(input.boards)) return ['boards must be an array'];
310
+ if (!input.boards.length) return ['keep at least one board'];
311
+ if (input.boards.length > MAX_BOARDS) return [`at most ${MAX_BOARDS} boards`];
312
+ const out = [];
313
+ const ids = new Set();
314
+ input.boards.forEach((b, i) => {
315
+ if (!BOARD_ID_RE.test(String(b?.id || ''))) out.push(`boards[${i}].id is not a board id`);
316
+ else if (ids.has(b.id)) out.push(`boards[${i}].id "${b.id}" is used twice`);
317
+ ids.add(b?.id);
318
+ if (b?.name != null && String(b.name).length > BOARD_NAME_MAX) out.push(`boards[${i}].name can be at most ${BOARD_NAME_MAX} characters`);
319
+ out.push(...layoutProblems({widgets: b?.widgets ?? []}).map(p => `boards[${i}]: ${p}`));
320
+ });
321
+ return out;
322
+ }
323
+
324
+ /** The active board of a set. */
325
+ export const activeBoard = (set) => set.boards.find(b => b.id === set.active) || set.boards[0];