Skip to content

Commit b36fd5c

Browse files
committed
Fix editor jankiness
1 parent 6641029 commit b36fd5c

File tree

2 files changed

+11
-43
lines changed

2 files changed

+11
-43
lines changed

src/editor/EditorPlugin.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class StatusBarEditorPlugin implements PluginValue {
4040
// When selecting text with Shift+Home the userEventType is undefined.
4141
// This is probably a bug in codemirror, for the time being doing an explict check
4242
// for the type allows us to update the stats for the selection.
43-
const userEventTypeUndefined =
44-
tr.annotation(Transaction.userEvent) === undefined;
43+
const userEventTypeUndefined = tr.annotation(Transaction.userEvent) === undefined;
4544

4645
if (
4746
(tr.isUserEvent("select") || userEventTypeUndefined) &&
@@ -171,36 +170,6 @@ function getHeadingRanges(app: App, file: TFile, end: number) {
171170
return ranges;
172171
}
173172

174-
class SectionWidget extends WidgetType {
175-
plugin: BetterWordCount;
176-
range: HeadingRange;
177-
count: number;
178-
selfCount?: number;
179-
180-
constructor(plugin: BetterWordCount, range: HeadingRange, count: number, selfCount?: number) {
181-
super();
182-
this.plugin = plugin;
183-
this.range = range;
184-
this.count = count;
185-
this.selfCount = selfCount;
186-
}
187-
188-
eq(widget: this): boolean {
189-
return this.range.from === widget.range.from && this.range.to === widget.range.to;
190-
}
191-
192-
getDisplayText() {
193-
if (this.selfCount) {
194-
return `${this.selfCount} / ${this.count}`;
195-
}
196-
return this.count.toString();
197-
}
198-
199-
toDOM() {
200-
return createSpan({ cls: "bwc-section-count", text: this.getDisplayText() });
201-
}
202-
}
203-
204173
class SectionWordCountEditorPlugin implements PluginValue {
205174
decorations: DecorationSet;
206175

@@ -244,7 +213,7 @@ class SectionWordCountEditorPlugin implements PluginValue {
244213
for (let i = 0; i < headingRanges.length; i++) {
245214
const heading = headingRanges[i];
246215
const next = headingRanges[i + 1];
247-
const targetPos = heading.heading.position.end.offset;
216+
const targetPos = heading.heading.position.start.offset;
248217

249218
const totalCount = getWordCount(view.state.doc.slice(heading.from, heading.to).toString());
250219
let selfCount: number;
@@ -259,9 +228,11 @@ class SectionWordCountEditorPlugin implements PluginValue {
259228
b.add(
260229
targetPos,
261230
targetPos,
262-
Decoration.widget({
263-
side: 0,
264-
widget: new SectionWidget(plugin, heading, totalCount, selfCount),
231+
Decoration.line({
232+
attributes: {
233+
class: "bwc-section-count",
234+
style: `--word-count: "${selfCount ? `${selfCount} / ${totalCount}` : totalCount.toString()}"`,
235+
},
265236
})
266237
);
267238
}

src/styles.css

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@ details.bwc-sb-item-setting {
2020
margin-bottom: 0px;
2121
}
2222

23-
.bwc-section-count {
23+
.bwc-section-count > span:last-of-type::after {
2424
background: var(--background-secondary);
2525
border-radius: var(--tag-radius);
2626
color: var(--text-muted);
27+
content: var(--word-count);
2728
display: inline-flex;
2829
font-size: var(--font-ui-smaller);
2930
font-weight: var(--font-normal);
3031
line-height: 1;
31-
margin: calc(-1 * var(--size-4-1)) 0 calc(-1 * var(--size-4-1)) var(--size-4-2);
32+
margin: calc(-1 * var(--size-2-3)) 0 calc(-1 * var(--size-2-3)) var(--size-4-2);
3233
padding: var(--size-2-3) var(--size-4-2);
3334
position: relative;
34-
top: calc(-1 * var(--size-2-1));
35-
}
36-
37-
.cm-active .bwc-section-count {
38-
display: none;
35+
top: -3px;
3936
}

0 commit comments

Comments
 (0)