-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from hadynz/popup-as-menu
Render suggestions in Menu. Support multiple suggestions for a keyword
- Loading branch information
Showing
11 changed files
with
75 additions
and
68 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
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
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
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,36 +1,29 @@ | ||
import tippy from 'tippy.js'; | ||
import 'tippy.js/dist/tippy.css'; | ||
import { App, Menu, MenuItem } from 'obsidian'; | ||
|
||
import './suggestionsPopup.css'; | ||
type SuggestionsModalProps = { | ||
app: App; | ||
mouseEvent: MouseEvent; | ||
suggestions: string[]; | ||
onClick: (replaceText: string) => void; | ||
}; | ||
|
||
type SuggestionsPopupProps = { | ||
target: HTMLElement; | ||
text: string; | ||
onClick: () => void; | ||
const item = (icon, title, click) => { | ||
return (item: MenuItem) => item.setIcon(icon).setTitle(title).onClick(click); | ||
}; | ||
|
||
export class SuggestionsPopup { | ||
public show(props: SuggestionsPopupProps): void { | ||
const { text, onClick, target } = props; | ||
export const showSuggestionsModal = (props: SuggestionsModalProps): void => { | ||
const { app, mouseEvent, suggestions, onClick } = props; | ||
|
||
const menu = new Menu(app); | ||
|
||
const button = document.createElement('button'); | ||
button.innerText = text; | ||
button.title = 'Suggestion'; | ||
button.onclick = () => { | ||
onClick(); | ||
tippyInstance.hide(); | ||
}; | ||
suggestions.forEach((replaceText) => { | ||
menu.addItem( | ||
item('pencil', `Replace with ${replaceText}`, () => { | ||
onClick(replaceText); | ||
}) | ||
); | ||
}); | ||
|
||
const tippyInstance = tippy(target, { | ||
content: button, | ||
trigger: 'click', | ||
theme: 'obsidian', | ||
interactive: true, | ||
appendTo: document.body, | ||
allowHTML: true, | ||
onHidden: () => { | ||
tippyInstance.destroy(); | ||
}, | ||
}); | ||
} | ||
} | ||
menu.addSeparator(); | ||
menu.showAtMouseEvent(mouseEvent); | ||
}; |
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
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
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
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
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
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
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