-
Notifications
You must be signed in to change notification settings - Fork 0
feat: support comment mode #16
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
renjie-run
wants to merge
2
commits into
main
Choose a base branch
from
ai-page-comment
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import { POST_MESSAGE_TYPE } from './constants'; | ||
|
|
||
| const countSameTagSiblingsBefore = (element) => { | ||
| let count = 0; | ||
| let sibling = element.previousElementSibling; | ||
| while (sibling) { | ||
| if (sibling.tagName === element.tagName) count += 1; | ||
| sibling = sibling.previousElementSibling; | ||
| } | ||
| return count; | ||
| }; | ||
|
|
||
| const generateSelector = (element) => { | ||
| if (!element || element === document.body) return null; | ||
|
|
||
| const parts = []; | ||
| let current = element; | ||
|
|
||
| while (current && current !== document.body) { | ||
| const tag = current.tagName.toLowerCase(); | ||
| const index = countSameTagSiblingsBefore(current) + 1; | ||
| parts.unshift(`${tag}:nth-of-type(${index})`); | ||
| current = current.parentElement; | ||
| } | ||
|
|
||
| return 'body > ' + parts.join(' > '); | ||
| }; | ||
|
|
||
| const getHtmlHint = (element, maxLen = 180) => { | ||
| const html = element.outerHTML || ''; | ||
| return html.length > maxLen ? html.slice(0, maxLen) : html; | ||
| }; | ||
|
|
||
| const getCurrentText = (element, maxLen = 160) => { | ||
| const text = (element.textContent || '').replace(/\s+/g, ' ').trim(); | ||
| return text.length > maxLen ? text.slice(0, maxLen) : text; | ||
| }; | ||
|
|
||
| const computeStyle = (element) => { | ||
| const style = window.getComputedStyle(element); | ||
| return { | ||
| backgroundColor: style.backgroundColor, | ||
| color: style.color, | ||
| fontSize: style.fontSize, | ||
| fontWeight: style.fontWeight, | ||
| fontFamily: style.fontFamily, | ||
| lineHeight: style.lineHeight, | ||
| borderRadius: style.borderRadius, | ||
| paddingBottom: style.paddingBottom, | ||
| paddingLeft: style.paddingLeft, | ||
| paddingRight: style.paddingRight, | ||
| paddingTop: style.paddingTop, | ||
| marginBottom: style.marginBottom, | ||
| marginLeft: style.marginLeft, | ||
| marginRight: style.marginRight, | ||
| marginTop: style.marginTop, | ||
| textAlign: style.textAlign, | ||
| display: style.display, | ||
| width: style.width, | ||
| height: style.height, | ||
| }; | ||
| }; | ||
|
|
||
| const generateLabel = (element) => { | ||
| const tag = element.tagName.toLowerCase(); | ||
| const classes = element.classList.length > 0 | ||
| ? '.' + Array.from(element.classList).join('.') | ||
| : ''; | ||
| return `${tag}${classes}`; | ||
| }; | ||
|
|
||
| export class CommentModeAdapter { | ||
| constructor() { | ||
| this.isActive = false; | ||
| this._handleEvent = this._handleEvent.bind(this); | ||
| this._handleScroll = this._handleScroll.bind(this); | ||
| this.mouseEvents = ['click', 'dblclick', 'mousedown', 'mouseup', 'mousemove', 'mouseover', 'mouseout', 'mouseenter', 'mouseleave', 'contextmenu']; | ||
| this.hoverTarget = null; | ||
| this.selectedTarget = null; | ||
| this._scrollRAF = null; | ||
| this._hoverRAF = null; | ||
| } | ||
|
|
||
| enable() { | ||
| if (this.isActive) return; | ||
| this.isActive = true; | ||
| this.mouseEvents.forEach(eventType => { | ||
| window.addEventListener(eventType, this._handleEvent, true); | ||
| }); | ||
| window.addEventListener('scroll', this._handleScroll, true); | ||
| this.addCommentModeStyle(); | ||
| } | ||
|
|
||
| disable() { | ||
| if (!this.isActive) return; | ||
| this.isActive = false; | ||
| this.mouseEvents.forEach(eventType => { | ||
| window.removeEventListener(eventType, this._handleEvent, true); | ||
| }); | ||
| window.removeEventListener('scroll', this._handleScroll, true); | ||
| this.hoverTarget = null; | ||
| this.selectedTarget = null; | ||
| if (this._scrollRAF !== null) cancelAnimationFrame(this._scrollRAF); | ||
| if (this._hoverRAF !== null) cancelAnimationFrame(this._hoverRAF); | ||
| this._scrollRAF = null; | ||
| this._hoverRAF = null; | ||
| this.removeCommentStyle(); | ||
| } | ||
|
|
||
| _handleScroll() { | ||
| if (!this.isActive) return; | ||
|
|
||
| if (this._scrollRAF !== null) return; | ||
| this._scrollRAF = requestAnimationFrame(() => { | ||
| this._scrollRAF = null; | ||
|
|
||
| if (this.selectedTarget && document.body.contains(this.selectedTarget)) { | ||
| const data = this.buildElementData(this.selectedTarget); | ||
| window.parent.postMessage({ type: POST_MESSAGE_TYPE.HTML_PAGE_COMMENT_MODE_ELEMENT_POSITION_UPDATE, data, targetType: 'selected' }, '*'); | ||
| } | ||
|
|
||
| if (this.hoverTarget && document.body.contains(this.hoverTarget)) { | ||
| const data = this.buildElementData(this.hoverTarget); | ||
| window.parent.postMessage({ type: POST_MESSAGE_TYPE.HTML_PAGE_COMMENT_MODE_ELEMENT_POSITION_UPDATE, data, targetType: 'hover' }, '*'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| addCommentModeStyle() { | ||
| let style = document.getElementById('ai-comment-cursor-style'); | ||
| if (!style) { | ||
| style = document.createElement('style'); | ||
| style.id = 'ai-comment-cursor-style'; | ||
| style.innerHTML = '* { cursor: crosshair !important; }'; | ||
| document.head.appendChild(style); | ||
| } | ||
| } | ||
|
|
||
| removeCommentStyle() { | ||
| const style = document.getElementById('ai-comment-cursor-style'); | ||
| if (style) style.remove(); | ||
| } | ||
|
|
||
| _handleEvent(event) { | ||
| if (!this.isActive) return; | ||
|
|
||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| event.stopImmediatePropagation(); | ||
|
|
||
| const target = event.target; | ||
| const isBodyOrHtml = target === document.body || target === document.documentElement; | ||
|
|
||
| if ((event.type === 'mouseout' || event.type === 'mouseleave') && event.relatedTarget === null) { | ||
| const hadHoverTarget = this.hoverTarget !== null; | ||
| const hadPendingHover = this._hoverRAF !== null; | ||
| this.hoverTarget = null; | ||
|
|
||
| if (hadPendingHover) { | ||
| cancelAnimationFrame(this._hoverRAF); | ||
| this._hoverRAF = null; | ||
| } | ||
|
|
||
| if (hadHoverTarget || hadPendingHover) { | ||
| window.parent.postMessage({ type: POST_MESSAGE_TYPE.HTML_PAGE_COMMENT_MODE_ELEMENT_HOVER, data: null }, '*'); | ||
| } | ||
| } else if (event.type === 'mouseover') { | ||
| this.hoverTarget = isBodyOrHtml ? null : target; | ||
|
|
||
| if (this._hoverRAF !== null) return; | ||
| this._hoverRAF = requestAnimationFrame(() => { | ||
| this._hoverRAF = null; | ||
| const currentTarget = this.hoverTarget; | ||
| const data = currentTarget ? this.buildElementData(currentTarget) : null; | ||
| window.parent.postMessage({ type: POST_MESSAGE_TYPE.HTML_PAGE_COMMENT_MODE_ELEMENT_HOVER, data }, '*'); | ||
| }); | ||
| } else if (event.type === 'click') { | ||
| this.selectedTarget = isBodyOrHtml ? null : target; | ||
| const data = isBodyOrHtml ? null : this.buildElementData(target); | ||
| window.parent.postMessage({ type: POST_MESSAGE_TYPE.HTML_PAGE_COMMENT_MODE_ELEMENT_SELECTED, data }, '*'); | ||
| } | ||
| } | ||
|
|
||
| buildElementData(target) { | ||
| const rect = target.getBoundingClientRect(); | ||
| const selector = generateSelector(target) || null; | ||
| return { | ||
| selector, | ||
| currentText: getCurrentText(target), | ||
| htmlHint: getHtmlHint(target), | ||
| computedStyle: computeStyle(target), | ||
| label: generateLabel(target), | ||
| elementPosition: { | ||
| left: rect.left, | ||
| top: rect.top, | ||
| width: rect.width, | ||
| height: rect.height, | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| destroy() { | ||
| this.disable(); | ||
| } | ||
| } | ||
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,11 @@ | ||
| export const POST_MESSAGE_TYPE = { | ||
| HTML_PAGE_REQUEST: 'HTML_PAGE_REQUEST', | ||
| HTML_PAGE_RESPONSE: 'HTML_PAGE_RESPONSE', | ||
| HTML_PAGE_EVENT: 'HTML_PAGE_EVENT', | ||
| HTML_PAGE_ENABLE_COMMENT_MODE: 'HTML_PAGE_ENABLE_COMMENT_MODE', | ||
| HTML_PAGE_DISABLE_COMMENT_MODE: 'HTML_PAGE_DISABLE_COMMENT_MODE', | ||
| HTML_PAGE_COMMENT_MODE_ELEMENT_HOVER: 'HTML_PAGE_COMMENT_MODE_ELEMENT_HOVER', | ||
| HTML_PAGE_COMMENT_MODE_ELEMENT_SELECTED: 'HTML_PAGE_COMMENT_MODE_ELEMENT_SELECTED', | ||
| HTML_PAGE_COMMENT_MODE_ELEMENT_POSITION_UPDATE: 'HTML_PAGE_COMMENT_MODE_ELEMENT_POSITION_UPDATE', | ||
| WINDOW_EVENT: 'WINDOW_EVENT', | ||
| }; |
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.
Uh oh!
There was an error while loading. Please reload this page.