Skip to content

Commit ef8522c

Browse files
committed
fix
1 parent 61b15b9 commit ef8522c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/cm/rainbowBrackets.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ interface BlockCacheEntry {
8383
function normalizeHexColor(value: unknown): string | null {
8484
if (typeof value !== "string") return null;
8585
const color = value.trim().toLowerCase();
86-
if (/^#([\da-f]{3}|[\da-f]{6})$/i.test(color)) return color;
86+
if (/^#([\da-f]{3}|[\da-f]{6})$/.test(color)) return color;
8787
return null;
8888
}
8989

@@ -298,7 +298,7 @@ export function getRainbowBracketColors(
298298

299299
export function rainbowBrackets(options: RainbowBracketsOptions = {}) {
300300
const colors =
301-
options.colors?.length != null && options.colors.length > 0
301+
options.colors != null && options.colors.length > 0
302302
? [...options.colors]
303303
: getRainbowBracketColors();
304304
const exactScanLimit = Math.max(
@@ -315,14 +315,30 @@ export function rainbowBrackets(options: RainbowBracketsOptions = {}) {
315315
class {
316316
decorations: DecorationSet;
317317
blockCache = new Map<string, BlockCacheEntry>();
318+
raf = 0;
319+
pendingView: EditorView | null = null;
318320

319321
constructor(view: EditorView) {
320322
this.decorations = this.buildDecorations(view);
321323
}
322324

323325
update(update: ViewUpdate) {
324326
if (!update.docChanged && !update.viewportChanged) return;
325-
this.decorations = this.buildDecorations(update.view);
327+
this.scheduleBuild(update.view);
328+
}
329+
330+
scheduleBuild(view: EditorView): void {
331+
this.pendingView = view;
332+
if (this.raf) return;
333+
// Bracket recoloring is cosmetic. Collapse bursts of edits/scroll
334+
// events into a single frame so large pastes don't block repeatedly.
335+
this.raf = requestAnimationFrame(() => {
336+
this.raf = 0;
337+
const pendingView = this.pendingView;
338+
this.pendingView = null;
339+
if (!pendingView) return;
340+
this.decorations = this.buildDecorations(pendingView);
341+
});
326342
}
327343

328344
buildDecorations(view: EditorView): DecorationSet {
@@ -424,6 +440,11 @@ export function rainbowBrackets(options: RainbowBracketsOptions = {}) {
424440
}
425441

426442
destroy(): void {
443+
if (this.raf) {
444+
cancelAnimationFrame(this.raf);
445+
this.raf = 0;
446+
}
447+
this.pendingView = null;
427448
this.blockCache.clear();
428449
}
429450
},

0 commit comments

Comments
 (0)