Skip to content

Commit d9eb425

Browse files
authored
Fixup overtype modification sync errors (#102)
2 parents f1df5b8 + aaca3ff commit d9eb425

8 files changed

+84
-60
lines changed

src/lib/enhancers/github/GitHubEditEnhancer.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
StrippedLocation,
77
} from "@/lib/enhancer"
88
import { logger } from "@/lib/logger"
9-
import { modifyDOM } from "../modifyDOM"
9+
import { fixupOvertype, modifyDOM } from "../overtype-misc"
1010
import { commonGitHubOptions, prepareGitHubHighlighter } from "./github-common"
1111

1212
const GH_EDIT = "GH_EDIT" as const
@@ -71,13 +71,12 @@ export class GitHubEditEnhancer implements CommentEnhancer<GitHubEditSpot> {
7171
): OverTypeInstance {
7272
prepareGitHubHighlighter()
7373
const overtypeContainer = modifyDOM(textArea)
74-
const overtype = new OverType(overtypeContainer, {
75-
...commonGitHubOptions,
76-
padding: spot.isIssue ? "var(--base-size-16)" : "var(--base-size-8)",
77-
})[0]!
78-
if (!spot.isIssue) {
79-
// TODO: autoheight not working
80-
}
74+
const overtype = fixupOvertype(
75+
new OverType(overtypeContainer, {
76+
...commonGitHubOptions,
77+
padding: spot.isIssue ? "var(--base-size-16)" : "var(--base-size-8)",
78+
})
79+
)
8180
return overtype
8281
}
8382

src/lib/enhancers/github/GitHubIssueAppendEnhancer.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
StrippedLocation,
99
} from "@/lib/enhancer"
1010
import { logger } from "@/lib/logger"
11-
import { modifyDOM } from "../modifyDOM"
11+
import { fixupOvertype, modifyDOM } from "../overtype-misc"
1212
import { commonGitHubOptions, prepareGitHubHighlighter } from "./github-common"
1313

1414
const GH_ISSUE_APPEND = "GH_ISSUE_APPEND" as const
@@ -78,11 +78,13 @@ export class GitHubIssueAppendEnhancer
7878
): OverTypeInstance {
7979
prepareGitHubHighlighter()
8080
const overtypeContainer = modifyDOM(textArea)
81-
return new OverType(overtypeContainer, {
82-
...commonGitHubOptions,
83-
minHeight: "100px",
84-
placeholder: "Use Markdown to format your comment",
85-
})[0]!
81+
return fixupOvertype(
82+
new OverType(overtypeContainer, {
83+
...commonGitHubOptions,
84+
minHeight: "100px",
85+
placeholder: "Use Markdown to format your comment",
86+
})
87+
)
8688
}
8789

8890
tableUpperDecoration(spot: GitHubIssueAppendSpot): React.ReactNode {

src/lib/enhancers/github/GitHubIssueCreateEnhancer.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
StrippedLocation,
88
} from "../../enhancer"
99
import { logger } from "../../logger"
10-
import { modifyDOM } from "../modifyDOM"
10+
import { fixupOvertype, modifyDOM } from "../overtype-misc"
1111
import { commonGitHubOptions, prepareGitHubHighlighter } from "./github-common"
1212

1313
const GH_ISSUE_CREATE = "GH_ISSUE_CREATE" as const
@@ -68,11 +68,13 @@ export class GitHubIssueCreateEnhancer
6868
): OverTypeInstance {
6969
prepareGitHubHighlighter()
7070
const overtypeContainer = modifyDOM(textArea)
71-
return new OverType(overtypeContainer, {
72-
...commonGitHubOptions,
73-
minHeight: "400px",
74-
placeholder: "Type your description here...",
75-
})[0]!
71+
return fixupOvertype(
72+
new OverType(overtypeContainer, {
73+
...commonGitHubOptions,
74+
minHeight: "400px",
75+
placeholder: "Type your description here...",
76+
})
77+
)
7678
}
7779

7880
tableUpperDecoration(spot: GitHubIssueCreateSpot): React.ReactNode {

src/lib/enhancers/github/GitHubPrAppendEnhancer.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
StrippedLocation,
99
} from "@/lib/enhancer"
1010
import { logger } from "@/lib/logger"
11-
import { modifyDOM } from "../modifyDOM"
11+
import { fixupOvertype, modifyDOM } from "../overtype-misc"
1212
import { commonGitHubOptions, prepareGitHubHighlighter } from "./github-common"
1313

1414
const GH_PR_APPEND = "GH_PR_APPEND" as const
@@ -69,18 +69,14 @@ export class GitHubPrAppendEnhancer
6969
): OverTypeInstance {
7070
prepareGitHubHighlighter()
7171
const overtypeContainer = modifyDOM(textArea)
72-
const overtype = new OverType(overtypeContainer, {
73-
...commonGitHubOptions,
74-
minHeight: "102px",
75-
padding: "var(--base-size-8)",
76-
placeholder: "Add your comment here...",
77-
})[0]!
78-
const listenForEmpty = new MutationObserver(() => {
79-
if (textArea.value === "") {
80-
overtype.updatePreview()
81-
}
82-
})
83-
listenForEmpty.observe(textArea, { attributes: true, characterData: true })
72+
const overtype = fixupOvertype(
73+
new OverType(overtypeContainer, {
74+
...commonGitHubOptions,
75+
minHeight: "102px",
76+
padding: "var(--base-size-8)",
77+
placeholder: "Add your comment here...",
78+
})
79+
)
8480
return overtype
8581
}
8682

src/lib/enhancers/github/GitHubPrCreateEnhancer.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
StrippedLocation,
88
} from "../../enhancer"
99
import { logger } from "../../logger"
10-
import { modifyDOM } from "../modifyDOM"
10+
import { fixupOvertype, modifyDOM } from "../overtype-misc"
1111
import { commonGitHubOptions, prepareGitHubHighlighter } from "./github-common"
1212

1313
const GH_PR_CREATE = "GH_PR_CREATE" as const
@@ -83,11 +83,13 @@ export class GitHubPrCreateEnhancer
8383
): OverTypeInstance {
8484
prepareGitHubHighlighter()
8585
const overtypeContainer = modifyDOM(textArea)
86-
return new OverType(overtypeContainer, {
87-
...commonGitHubOptions,
88-
minHeight: "250px",
89-
placeholder: "Type your description here...",
90-
})[0]!
86+
return fixupOvertype(
87+
new OverType(overtypeContainer, {
88+
...commonGitHubOptions,
89+
minHeight: "250px",
90+
placeholder: "Type your description here...",
91+
})
92+
)
9193
}
9294

9395
tableUpperDecoration(spot: GitHubPrCreateSpot): React.ReactNode {

src/lib/enhancers/github/github-common.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@ export const commonGitHubOptions: Options = {
1111

1212
export function prepareGitHubHighlighter() {
1313
oncePerRefresh("github-highlighter", () => {
14+
const textColor = "rgb(31, 35, 40)"
15+
const headingColor = "rgb(174, 52, 151)"
16+
OverType.setTheme({
17+
colors: {
18+
blockquote: "rgb(89, 99, 110)",
19+
code: "#59636e",
20+
codeBg: "#f6f8fa",
21+
cursor: "#000000",
22+
em: "rgb(126, 123, 255)",
23+
h1: headingColor,
24+
h2: headingColor,
25+
h3: headingColor,
26+
hr: "#5a7a9b",
27+
link: "rgb(9, 105, 218)",
28+
selection: "rgba(0, 123, 255, 0.3)",
29+
strong: "rgb(45, 1, 142)",
30+
syntaxMarker: textColor,
31+
text: textColor,
32+
},
33+
name: "custom-github",
34+
})
1435
OverType.setCodeHighlighter(githubHighlighter)
1536
})
1637
}

src/lib/enhancers/modifyDOM.ts renamed to src/lib/enhancers/overtype-misc.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
import type { OverTypeInstance } from "overtype"
2+
3+
// Assert that OverType returned exactly one instance and return it
4+
export function fixupOvertype(instances: OverTypeInstance[]): OverTypeInstance {
5+
if (instances.length !== 1) {
6+
throw new Error(
7+
`Expected OverType to return exactly 1 instance, got ${instances.length}`
8+
)
9+
}
10+
const overtype = instances[0]!
11+
// this works, but we're now updating twice as often as we need to, because
12+
// overtype has a built-in update which usually works but not always (#101)
13+
// and we're doing this which does always work
14+
const updateOnChange = new MutationObserver(() => {
15+
overtype.updatePreview()
16+
})
17+
updateOnChange.observe(overtype.textarea, {
18+
attributes: true,
19+
characterData: true,
20+
})
21+
return overtype
22+
}
23+
124
// Modify the DOM to trick overtype into adopting it instead of recreating it
225
export function modifyDOM(overtypeInput: HTMLTextAreaElement): HTMLElement {
326
overtypeInput.classList.add("overtype-input")

src/lib/registries.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,6 @@ export class EnhancerRegistry {
3232
this.register(new GitHubIssueCreateEnhancer())
3333
this.register(new GitHubPrAppendEnhancer())
3434
this.register(new GitHubPrCreateEnhancer())
35-
const textColor = "rgb(31, 35, 40)"
36-
const headingColor = "rgb(174, 52, 151)"
37-
OverType.setTheme({
38-
colors: {
39-
blockquote: "rgb(89, 99, 110)",
40-
code: "#59636e",
41-
codeBg: "#f6f8fa",
42-
cursor: "#000000",
43-
em: "rgb(126, 123, 255)",
44-
h1: headingColor,
45-
h2: headingColor,
46-
h3: headingColor,
47-
hr: "#5a7a9b",
48-
link: "rgb(9, 105, 218)",
49-
selection: "rgba(0, 123, 255, 0.3)",
50-
strong: "rgb(45, 1, 142)",
51-
syntaxMarker: textColor,
52-
text: textColor,
53-
},
54-
name: "custom-github",
55-
})
5635
}
5736

5837
private register<T extends CommentSpot>(enhancer: CommentEnhancer<T>): void {

0 commit comments

Comments
 (0)