Skip to content

Commit 68b347c

Browse files
dimaMachinaCopilot
andauthored
graphiql 5: fix Fixes Uncaught Error: can't access property "offsetNode", hitResult is null on Mozilla (#4044)
* upd * add changeset * upd comments * add `sideEffects` * Update packages/graphiql-react/src/monaco-editor.ts Co-authored-by: Copilot <[email protected]> * Update packages/graphiql-react/src/monaco-editor.ts --------- Co-authored-by: Copilot <[email protected]>
1 parent 19cae3a commit 68b347c

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

.changeset/wise-deers-bake.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphiql/react': patch
3+
'graphiql': patch
4+
---
5+
6+
fix `Fixes Uncaught Error: can't access property "offsetNode", hitResult is null` on Mozilla

packages/graphiql-react/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@graphiql/react",
33
"version": "0.35.1",
44
"sideEffects": [
5+
"dist/monaco-editor.js",
56
"dist/setup-workers/webpack.js",
67
"dist/setup-workers/vite.js"
78
],

packages/graphiql-react/src/monaco-editor.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,40 @@
55

66
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
77
export * from 'monaco-editor';
8+
// @ts-expect-error -- no types
9+
import { MouseTargetFactory } from 'monaco-editor/esm/vs/editor/browser/controller/mouseTarget.js';
10+
11+
/**
12+
* Patch for Firefox compatibility:
13+
*
14+
* Fixes:
15+
* Uncaught Error: can't access property "offsetNode", hitResult is null
16+
*
17+
* Related issues:
18+
* - https://github.com/graphql/graphiql/issues/4041
19+
* - https://github.com/microsoft/monaco-editor/issues/4679
20+
* - https://github.com/microsoft/monaco-editor/issues/4527
21+
*
22+
* The suggested patch https://github.com/microsoft/monaco-editor/issues/4679#issuecomment-2406284453
23+
* no longer works in Mozilla Firefox
24+
*/
25+
if (navigator.userAgent.includes('Firefox/')) {
26+
const originalFn = MouseTargetFactory._doHitTestWithCaretPositionFromPoint;
27+
28+
// @ts-expect-error -- internal override of Monaco method
29+
MouseTargetFactory._doHitTestWithCaretPositionFromPoint = (...args) => {
30+
const [ctx, coords] = args;
31+
const hitResult = ctx.viewDomNode.ownerDocument.caretPositionFromPoint(
32+
coords.clientX,
33+
coords.clientY,
34+
);
35+
if (hitResult) {
36+
// Delegate to original function if hitResult is valid
37+
const result = originalFn(...args);
38+
return result;
39+
}
40+
// We must return an object with `type: 0` to avoid the following error:
41+
// Uncaught Error: can't access property "type", result is undefined
42+
return { type: 0 };
43+
};
44+
}

0 commit comments

Comments
 (0)