-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
import { Ace, require as acequire } from 'ace-builds'; | ||
import { ReactNode } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
import readApiCall from './readApiCall'; | ||
|
||
const { HoverTooltip } = acequire('ace/tooltip'); | ||
const { TokenIterator } = acequire('ace/token_iterator'); | ||
|
||
const apiHelpComponents: { | ||
[matchText: string]: () => ReactNode; | ||
}[] = { | ||
'Robot.get_value': () => ( | ||
<div> | ||
Documentation for Robot.get_value method. | ||
</div> | ||
), | ||
'Robot': () => ( | ||
<div> | ||
Documentation for Robot object. | ||
</div> | ||
), | ||
}; | ||
|
||
export default function addEditorTooltips(editor: Ace.Editor) { | ||
const tooltip = new HoverTooltip(); | ||
const node = document.createElement('div'); | ||
const root = createRoot(node); | ||
const maxMatchTextLength = Math.max(...Object.keys(apiHelpComponents).map(s => s.length)); | ||
tooltip.setDataProvider((event: any, _editor: Ace.Editor) => { | ||
const pos: Ace.Position = event.getDocumentPosition(); | ||
const range = editor.session.getWordRange(pos.row, pos.column); | ||
const node = document.createElement('div'); | ||
const root = createRoot(node); | ||
root.render( | ||
<span>Hello world!</span> | ||
); | ||
const observer = new MutationObserver((_mutations) => { | ||
if (!document.contains(node)) { | ||
observer.disconnect(); | ||
root.unmount(); | ||
} | ||
}); | ||
observer.observe(document, { | ||
childList: true, | ||
subtree: true, | ||
}); | ||
tooltip.showForRange(editor, range, node, event); | ||
const result = readApiCall(editor.getSession(), range.end, maxMatchTextLength); | ||
if (!result.isInterrupted && result.text in apiHelpComponents) { | ||
root.render(apiHelpComponents[result.text]()); | ||
tooltip.showForRange(editor, range, node, event); | ||
} | ||
}); | ||
tooltip.addToEditor(editor); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters