Skip to content

Commit 089ecfb

Browse files
authored
refactor(superdoc): type colors field, guard #assignUserColor (SD-2867 phase B) (#3063)
1 parent 5a026fd commit 089ecfb

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

packages/superdoc/src/core/SuperDoc.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ export class SuperDoc extends EventEmitter {
110110
/** @type {Whiteboard | null} */
111111
whiteboard;
112112

113+
/**
114+
* Awareness palette assigned to local users when no explicit color is set.
115+
* Defaults to an empty array so `#assignUserColor` falls back to the
116+
* built-in `DEFAULT_AWARENESS_PALETTE`.
117+
* @type {string[]}
118+
*/
119+
colors = [];
120+
113121
/** @type {Config} */
114122
config = {
115123
superdocId: null,
@@ -279,7 +287,9 @@ export class SuperDoc extends EventEmitter {
279287
this.#log('🦋 [superdoc] Using SuperDoc version:', this.version);
280288

281289
this.superdocId = config.superdocId || uuidv4();
282-
this.colors = this.config.colors;
290+
// Default to an empty palette when no colors are configured so downstream
291+
// assignment logic doesn't have to null-check on every access.
292+
this.colors = this.config.colors ?? [];
283293

284294
// Preprocess document
285295
this.#initDocuments();
@@ -643,15 +653,21 @@ export class SuperDoc extends EventEmitter {
643653
* identity so different users get different colors.
644654
*/
645655
#assignUserColor() {
646-
if (this.config.user.color) return;
656+
// `#init` always populates `this.config.user` (defaults to DEFAULT_USER
657+
// when the consumer didn't pass one). The guard is here for the
658+
// strictNullChecks contract on the public Config.user typedef, which
659+
// must stay optional because consumers should not be required to pass
660+
// a user up front.
661+
const user = this.config.user;
662+
if (!user || user.color) return;
647663

648664
const palette = this.colors.length > 0 ? this.colors : DEFAULT_AWARENESS_PALETTE;
649-
const userKey = this.config.user.email || this.config.user.name || '';
665+
const userKey = user.email || user.name || '';
650666
let hash = 5381;
651667
for (let i = 0; i < userKey.length; i++) {
652668
hash = ((hash << 5) + hash) ^ userKey.charCodeAt(i);
653669
}
654-
this.config.user.color = palette[Math.abs(hash) % palette.length];
670+
user.color = palette[Math.abs(hash) % palette.length];
655671
}
656672

657673
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)