-
-
Notifications
You must be signed in to change notification settings - Fork 146
[Feat]: Word-Level Navigation for Text-Heavy HTML in Live Preview #2244
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
atondapu
wants to merge
1
commit into
phcode-dev:main
Choose a base branch
from
atondapu:main
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -382,12 +382,146 @@ | |
} | ||
|
||
|
||
/** | ||
* Gets the word at the clicked position along with additional information | ||
* @param {Element} element - The element that was clicked | ||
* @param {MouseEvent} event - The click event | ||
* @return {Object|null} - Object containing the word and additional info, or null if not found | ||
*/ | ||
function getClickedWord(element, event) { | ||
|
||
// Try to find the clicked position within the element | ||
const range = document.caretRangeFromPoint(event.clientX, event.clientY); | ||
if (!range) { | ||
return null; | ||
} | ||
|
||
const textNode = range.startContainer; | ||
const offset = range.startOffset; | ||
|
||
// Check if we have a text node | ||
if (textNode.nodeType !== Node.TEXT_NODE) { | ||
|
||
// If the element itself contains text, try to extract a word from it | ||
if (element.textContent && element.textContent.trim()) { | ||
const text = element.textContent.trim(); | ||
|
||
// Simple word extraction - get the first word | ||
const match = text.match(/\b(\w+)\b/); | ||
if (match) { | ||
const word = match[1]; | ||
|
||
// Since we're just getting the first word, it's the first occurrence | ||
return { | ||
word: word, | ||
occurrenceIndex: 0, | ||
context: text.substring(0, Math.min(40, text.length)) | ||
}; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
const nodeText = textNode.textContent; | ||
|
||
// Function to extract a word and its occurrence index | ||
function extractWordAndOccurrence(text, wordStart, wordEnd) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what will happen if we don't do word search? is the text not not always the correct target on click? |
||
const word = text.substring(wordStart, wordEnd); | ||
|
||
// Calculate which occurrence of this word it is | ||
const textBeforeWord = text.substring(0, wordStart); | ||
const regex = new RegExp("\\b" + word + "\\b", "g"); | ||
let occurrenceIndex = 0; | ||
let match; | ||
|
||
while ((match = regex.exec(textBeforeWord)) !== null) { | ||
occurrenceIndex++; | ||
} | ||
|
||
|
||
// Get context around the word (up to 20 chars before and after) | ||
const contextStart = Math.max(0, wordStart - 20); | ||
const contextEnd = Math.min(text.length, wordEnd + 20); | ||
const context = text.substring(contextStart, contextEnd); | ||
|
||
return { | ||
word: word, | ||
occurrenceIndex: occurrenceIndex, | ||
context: context | ||
}; | ||
} | ||
|
||
// If we're at a space or the text is empty, try to find a nearby word | ||
if (nodeText.length === 0 || (offset < nodeText.length && /\s/.test(nodeText[offset]))) { | ||
|
||
// Look for the nearest word | ||
let leftPos = offset - 1; | ||
let rightPos = offset; | ||
|
||
// Check to the left | ||
while (leftPos >= 0 && /\s/.test(nodeText[leftPos])) { | ||
leftPos--; | ||
} | ||
|
||
// Check to the right | ||
while (rightPos < nodeText.length && /\s/.test(nodeText[rightPos])) { | ||
rightPos++; | ||
} | ||
|
||
// If we found a non-space character to the left, extract that word | ||
if (leftPos >= 0) { | ||
let wordStart = leftPos; | ||
while (wordStart > 0 && /\w/.test(nodeText[wordStart - 1])) { | ||
wordStart--; | ||
} | ||
|
||
return extractWordAndOccurrence(nodeText, wordStart, leftPos + 1); | ||
} | ||
|
||
// If we found a non-space character to the right, extract that word | ||
if (rightPos < nodeText.length) { | ||
let wordEnd = rightPos; | ||
while (wordEnd < nodeText.length && /\w/.test(nodeText[wordEnd])) { | ||
wordEnd++; | ||
} | ||
|
||
return extractWordAndOccurrence(nodeText, rightPos, wordEnd); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
// Find word boundaries | ||
let startPos = offset; | ||
let endPos = offset; | ||
|
||
// Move start position to the beginning of the word | ||
while (startPos > 0 && /\w/.test(nodeText[startPos - 1])) { | ||
startPos--; | ||
} | ||
|
||
// Move end position to the end of the word | ||
while (endPos < nodeText.length && /\w/.test(nodeText[endPos])) { | ||
endPos++; | ||
} | ||
|
||
|
||
// Extract the word and its occurrence index | ||
if (endPos > startPos) { | ||
return extractWordAndOccurrence(nodeText, startPos, endPos); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Sends the message containing tagID which is being clicked | ||
* to the editor in order to change the cursor position to | ||
* the HTML tag corresponding to the clicked element. | ||
*/ | ||
function onDocumentClick(event) { | ||
|
||
// Get the user's current selection | ||
const selection = window.getSelection(); | ||
|
||
|
@@ -399,16 +533,32 @@ | |
return; | ||
} | ||
var element = event.target; | ||
|
||
if (element && element.hasAttribute('data-brackets-id')) { | ||
MessageBroker.send({ | ||
|
||
// Get the clicked word and its information | ||
const clickedWordInfo = getClickedWord(element, event); | ||
|
||
// Prepare the message with the clicked word information | ||
const message = { | ||
"tagId": element.getAttribute('data-brackets-id'), | ||
"nodeID": element.id, | ||
"nodeClassList": element.classList, | ||
"nodeName": element.nodeName, | ||
"allSelectors": _getAllInheritedSelectorsInOrder(element), | ||
"contentEditable": element.contentEditable === 'true', | ||
"clicked": true | ||
}); | ||
}; | ||
|
||
// Add word information if available | ||
if (clickedWordInfo) { | ||
message.clickedWord = clickedWordInfo.word; | ||
message.wordContext = clickedWordInfo.context; | ||
message.wordOccurrenceIndex = clickedWordInfo.occurrenceIndex; | ||
} | ||
|
||
MessageBroker.send(message); | ||
} else { | ||
} | ||
} | ||
window.document.addEventListener("click", onDocumentClick); | ||
|
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.
This api doesn't work in Firefox, so maybe we can fallback to caretPositionFromPoint in Firefox(caretPositionFromPoint doesnt work in safari), or disable the feature in Firefox.