-
Notifications
You must be signed in to change notification settings - Fork 25
feat: add a "git blame" esque hover over for Markdown text #3961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
404Wolf
wants to merge
16
commits into
main
Choose a base branch
from
wolf/git-blame-hover
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b1b49c8
add git blame esque hovers
404Wolf 457f483
lazy fetch
404Wolf e4dc530
general clean ups
404Wolf 5405d68
linter
404Wolf b1ac32f
Merge branch 'main' into wolf/git-blame-hover
404Wolf cbbdfdd
Merge branch 'main' into wolf/git-blame-hover
404Wolf a24c75b
flush every alarm
404Wolf 50071a4
clean up
404Wolf 621fd02
Merge branch 'main' into wolf/git-blame-hover
404Wolf 16e061d
add helper
404Wolf 244818a
Merge branch 'main' into wolf/git-blame-hover
404Wolf 34cd8a2
rename and make feature flag
404Wolf 8d1bb9d
Merge branch 'main' into wolf/git-blame-hover
404Wolf 74af52d
use solid primitive
404Wolf fdce201
remove needless index
404Wolf 93985f7
make biome happy
404Wolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
109 changes: 109 additions & 0 deletions
109
js/app/packages/core/component/LexicalMarkdown/plugins/blame-tooltip/BlameTooltip.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { UserIcon } from '@core/component/UserIcon'; | ||
| import { ENABLE_GIT_BLAME } from '@core/constant/featureFlags'; | ||
| import { macroIdToEmail, tryMacroId, useDisplayNameParts } from '@core/user'; | ||
| import { formatRelativeTimestamp } from '@entity'; | ||
| import { syncServiceClient } from '@service-sync/client'; | ||
| import { createScheduled, debounce } from '@solid-primitives/scheduled'; | ||
| import { createQuery } from '@tanstack/solid-query'; | ||
| import { | ||
| createEffect, | ||
| createMemo, | ||
| createSignal, | ||
| onCleanup, | ||
| Show, | ||
| untrack, | ||
| } from 'solid-js'; | ||
| import type { Store } from 'solid-js/store'; | ||
| import type { BlameTooltipState } from './blameTooltipPlugin'; | ||
|
|
||
| const FETCH_DELAY_MS = 400; | ||
| const SHOW_DELAY_MS = 500; | ||
|
|
||
| function UserLine(props: { userId: string; editedAt: Date }) { | ||
| const macroId = tryMacroId(props.userId); | ||
| const { firstName } = useDisplayNameParts(macroId); | ||
| const name = () => | ||
| firstName() || (macroId ? macroIdToEmail(macroId) : props.userId); | ||
|
|
||
| return ( | ||
| <span class="inline-flex items-center gap-1"> | ||
| <UserIcon id={props.userId} size="sm" suppressClick showTooltip={false} /> | ||
| {name()}, {formatRelativeTimestamp(props.editedAt)} | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| export function BlameTooltip(props: { | ||
| state: Store<BlameTooltipState>; | ||
| documentId: string; | ||
| }) { | ||
| const [visible, setVisible] = createSignal(false); | ||
| let shownAtX = 0; | ||
| let shownAtY = 0; | ||
| let showTimer: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| // Only commits to a new nodeId after FETCH_DELAY_MS of hover stillness. | ||
| const scheduled = createScheduled((fn) => debounce(fn, FETCH_DELAY_MS)); | ||
| const queriedNodeId = createMemo<string | null>((prev) => { | ||
| const active = props.state.hovering ? props.state.nodeId : null; | ||
| return scheduled() ? active : prev; | ||
| }, null); | ||
|
|
||
| const hide = () => { | ||
| if (showTimer) clearTimeout(showTimer); | ||
| showTimer = null; | ||
| setVisible(false); | ||
| }; | ||
|
|
||
| const query = createQuery(() => ({ | ||
| queryKey: ['blame', props.documentId, queriedNodeId()], | ||
| queryFn: async () => { | ||
| const res = await syncServiceClient.getNodeBlame({ | ||
| documentId: props.documentId, | ||
| nodeId: queriedNodeId()!, | ||
| }); | ||
| return res.isOk() ? res.value : null; | ||
| }, | ||
| enabled: ENABLE_GIT_BLAME() && queriedNodeId() !== null, | ||
| staleTime: Infinity, | ||
| })); | ||
|
|
||
| // Drive visibility based on cursor stillness (x/y). | ||
| createEffect(() => { | ||
| const { hovering, x, y } = props.state; | ||
|
|
||
| if (!hovering) return hide(); | ||
|
|
||
| // After shown: any pointer move dismisses. | ||
| if (untrack(visible)) { | ||
| if (x !== shownAtX || y !== shownAtY) hide(); | ||
| return; | ||
| } | ||
|
|
||
| // Pre-show: each move restarts the show timer. | ||
| if (showTimer) clearTimeout(showTimer); | ||
| showTimer = setTimeout(() => { | ||
| shownAtX = x; | ||
| shownAtY = y; | ||
| setVisible(true); | ||
| }, SHOW_DELAY_MS); | ||
| }); | ||
|
|
||
| onCleanup(hide); | ||
|
|
||
| return ( | ||
| <Show when={visible() && query.data?.userId ? query.data : null}> | ||
| {(b) => ( | ||
| <div | ||
| class="fixed z-50 text-xs text-ink-secondary/70 pointer-events-none" | ||
| style={{ | ||
| left: `${props.state.x + 12}px`, | ||
| top: `${props.state.y + 12}px`, | ||
| }} | ||
| > | ||
| <UserLine userId={b().userId!} editedAt={b().editedAt} /> | ||
| </div> | ||
| )} | ||
| </Show> | ||
| ); | ||
| } |
108 changes: 108 additions & 0 deletions
108
js/app/packages/core/component/LexicalMarkdown/plugins/blame-tooltip/blameTooltipPlugin.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { isTouchDevice } from '@core/mobile/isTouchDevice'; | ||
| import { mergeRegister } from '@lexical/utils'; | ||
| import { $getId } from '@lexical-core'; | ||
| import { | ||
| $getNearestNodeFromDOMNode, | ||
| $isTextNode, | ||
| type LexicalEditor, | ||
| type LexicalNode, | ||
| } from 'lexical'; | ||
| import { createStore } from 'solid-js/store'; | ||
|
|
||
| export type BlameTooltipState = { | ||
| hovering: boolean; | ||
| x: number; | ||
| y: number; | ||
| nodeId: string | null; | ||
| }; | ||
|
|
||
| export function createBlameTooltipStore() { | ||
| return createStore<BlameTooltipState>({ | ||
| hovering: false, | ||
| x: 0, | ||
| y: 0, | ||
| nodeId: null, | ||
| }); | ||
| } | ||
|
|
||
| type BlameTooltipPluginProps = { | ||
| setState: (state: Partial<BlameTooltipState>) => void; | ||
| }; | ||
|
|
||
| /** | ||
| * Given a DOM element under the cursor, find the stable Lexical id of the | ||
| * nearest ancestor text-bearing node. Must be called inside `editor.read()` | ||
| * so the Lexical state is readable. | ||
| * | ||
| * Returns null when the cursor isn't over a text node, or when no ancestor | ||
| * has been assigned a stable id yet (typically transient nodes). | ||
| */ | ||
| function $resolveHoveredNodeId(target: HTMLElement): string | null { | ||
| const node = $getNearestNodeFromDOMNode(target); | ||
| if (!node || !$isTextNode(node)) return null; | ||
|
|
||
| let cursor: LexicalNode | null = node; | ||
| while (cursor) { | ||
| const id = $getId(cursor); | ||
| if (id) return id; | ||
| cursor = cursor.getParent(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function registerBlameTooltipPlugin( | ||
| editor: LexicalEditor, | ||
| props: BlameTooltipPluginProps | ||
| ) { | ||
| const handlePointerMove = (e: MouseEvent) => { | ||
| if (isTouchDevice()) return; | ||
| const target = e.target; | ||
| if (!(target instanceof HTMLElement)) { | ||
| props.setState({ hovering: false, nodeId: null }); | ||
| return; | ||
| } | ||
|
|
||
| // Suppress while the user has a text selection — that's when the | ||
| // formatting popup shows and the tooltip would compete with it. | ||
| const sel = window.getSelection(); | ||
| if (sel && !sel.isCollapsed && sel.toString().length > 0) { | ||
| props.setState({ hovering: false, nodeId: null }); | ||
| return; | ||
| } | ||
|
|
||
| const nodeId = editor.read(() => $resolveHoveredNodeId(target)); | ||
| if (nodeId === null) { | ||
| props.setState({ hovering: false, nodeId: null }); | ||
| return; | ||
| } | ||
| props.setState({ | ||
| hovering: true, | ||
| x: e.clientX, | ||
| y: e.clientY, | ||
| nodeId, | ||
| }); | ||
| }; | ||
|
|
||
| const dismiss = () => { | ||
| props.setState({ hovering: false, nodeId: null }); | ||
| }; | ||
|
|
||
| return mergeRegister( | ||
| editor.registerRootListener((root, prevRoot) => { | ||
| if (root) { | ||
| root.addEventListener('pointermove', handlePointerMove); | ||
| root.addEventListener('pointerleave', dismiss); | ||
| root.addEventListener('pointerdown', dismiss); | ||
| } | ||
| if (prevRoot) { | ||
| prevRoot.removeEventListener('pointermove', handlePointerMove); | ||
| prevRoot.removeEventListener('pointerleave', dismiss); | ||
| prevRoot.removeEventListener('pointerdown', dismiss); | ||
| } | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| export function blameTooltipPlugin(props: BlameTooltipPluginProps) { | ||
| return (editor: LexicalEditor) => registerBlameTooltipPlugin(editor, props); | ||
| } |
2 changes: 2 additions & 0 deletions
2
js/app/packages/core/component/LexicalMarkdown/plugins/blame-tooltip/index.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './BlameTooltip'; | ||
| export * from './blameTooltipPlugin'; |
1 change: 1 addition & 0 deletions
1
js/app/packages/core/component/LexicalMarkdown/plugins/index.ts
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
rust/sync-service/database/user-peer-mapping/migrations/0002_add_blame.sql
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| CREATE TABLE blame ( | ||
| document_id TEXT NOT NULL, | ||
| node_id TEXT NOT NULL, | ||
| peer_id TEXT NOT NULL, | ||
| timestamp_ms INTEGER NOT NULL, | ||
| PRIMARY KEY (document_id, node_id) | ||
| ); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migration is not idempotent.
The
CREATE TABLElacksIF NOT EXISTS, so re-running this migration will fail. Per the SQL coding guidelines, migrations should be idempotent where possible.🔄 Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Coding guidelines