43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
// Rôle : normalise et compacte les mémos courts des toolboxes.
|
|
import { NOTEPAD_ALLOWED_COLORS } from "./drawing.js";
|
|
import { normalizeIsoDate, uid } from "./shared.js";
|
|
|
|
const DEFAULT_MEMO_COLOR = "#f6c453";
|
|
|
|
function normalizeMemoItem(item) {
|
|
const text = String(item?.text || "").trim();
|
|
if (!text) return null;
|
|
const color = NOTEPAD_ALLOWED_COLORS.has(String(item?.color || "").toLowerCase()) ? String(item.color).toLowerCase() : DEFAULT_MEMO_COLOR;
|
|
return {
|
|
id: item?.id || uid("memo"),
|
|
text,
|
|
color,
|
|
updatedAt: normalizeIsoDate(item?.updatedAt)
|
|
};
|
|
}
|
|
|
|
export function normalizeMemosData(data) {
|
|
const source = data && typeof data === "object" ? data : {};
|
|
return {
|
|
tickerEnabled: source.tickerEnabled !== false,
|
|
items: (Array.isArray(source.items) ? source.items : []).map(normalizeMemoItem).filter(Boolean)
|
|
};
|
|
}
|
|
|
|
export function compactMemosDataForStorage(value) {
|
|
const normalized = normalizeMemosData(value);
|
|
const compact = {};
|
|
if (normalized.tickerEnabled === false) compact.tickerEnabled = false;
|
|
if (normalized.items.length) {
|
|
compact.items = normalized.items.map((item) => {
|
|
const compactItem = {
|
|
id: item.id,
|
|
text: item.text,
|
|
color: item.color
|
|
};
|
|
if (item.updatedAt) compactItem.updatedAt = item.updatedAt;
|
|
return compactItem;
|
|
});
|
|
}
|
|
return Object.keys(compact).length ? compact : null;
|
|
}
|