Skip to content

Commit 95480e4

Browse files
committed
feat(renderer): Add dynamic max width calculation for editor content
Implement character width measurement using DOM probe Calculate and update max width based on longest line in code Apply min-width to code content element for proper scrolling Adjust gutter position and diff highlighting colors in styles
1 parent 375feae commit 95480e4

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

anycode-base/src/renderer/Renderer.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class Renderer {
4444
private completionRenderer: CompletionRenderer;
4545

4646
private visualRows: VisualRow[] = [];
47+
48+
private maxWidth: number = 0;
49+
private charWidth: number = 0;
4750

4851
constructor(
4952
container: HTMLDivElement,
@@ -164,6 +167,8 @@ export class Renderer {
164167
if (search && search.isActive()) {
165168
this.searchRenderer.updateSearchHighlights(search);
166169
}
170+
171+
this.updateMaxWidth(code);
167172
}
168173

169174
/**
@@ -598,6 +603,8 @@ export class Renderer {
598603
this.renderSelection(code, selection!);
599604
}
600605
}
606+
607+
this.updateMaxWidth(code);
601608
}
602609

603610
private ensureSpacers(container: HTMLElement) {
@@ -824,4 +831,46 @@ export class Renderer {
824831
public clearAllDiffs(): void {
825832
this.diffRenderer.clearAllDiffs();
826833
}
834+
835+
private getCharWidth(): number {
836+
if (this.charWidth > 0) return this.charWidth;
837+
838+
const probe = document.createElement('span');
839+
probe.textContent = 'M';
840+
probe.style.position = 'absolute';
841+
probe.style.visibility = 'hidden';
842+
probe.style.whiteSpace = 'pre';
843+
probe.style.pointerEvents = 'none';
844+
845+
const computed = window.getComputedStyle(this.codeContent);
846+
probe.style.fontFamily = computed.fontFamily;
847+
probe.style.fontSize = computed.fontSize;
848+
probe.style.fontWeight = computed.fontWeight;
849+
probe.style.letterSpacing = computed.letterSpacing;
850+
851+
this.codeContent.appendChild(probe);
852+
const measured = probe.getBoundingClientRect().width;
853+
probe.remove();
854+
855+
this.charWidth = measured > 0 ? measured : 8;
856+
return this.charWidth;
857+
}
858+
859+
private updateMaxWidth(code: Code): void {
860+
const charW = this.getCharWidth();
861+
const totalLines = code.linesLength();
862+
let maxW = 0;
863+
864+
for (let i = 0; i < totalLines; i++) {
865+
const width = code.lineLength(i) * charW;
866+
if (width > maxW) {
867+
maxW = width;
868+
}
869+
}
870+
871+
this.maxWidth = maxW;
872+
if (this.maxWidth > 0) {
873+
this.codeContent.style.minWidth = `${this.maxWidth + 100}px`;
874+
}
875+
}
827876
}

anycode-base/src/styles.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
.anyeditor .gutter {
3232
position: sticky;
33-
left: 31px;
33+
left: 32px;
3434
user-select: none;
3535
pointer-events: auto;
3636
background: inherit;
@@ -94,7 +94,7 @@
9494
}
9595

9696
.diff-added {
97-
background-color: rgba(76, 175, 79, 0.311);
97+
background-color: rgba(76, 175, 79, 0.206);
9898
position: relative;
9999
}
100100

@@ -105,7 +105,7 @@
105105
top: 0;
106106
bottom: 0;
107107
width: 3px;
108-
background-color: rgba(76, 175, 80, 0.5);
108+
background-color: rgba(76, 175, 79, 0.405);
109109
}
110110

111111
.gutter .ln.diff-deleted {

0 commit comments

Comments
 (0)