-
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.
[BlockToolAdapter] Improve native inputs handling (#78)
* Improve native inputs handling * Fix types issue * Update packages/dom-adapters/src/utils/findHardLineBoundary.ts Co-authored-by: Peter <[email protected]> * Add comments, return white-space: pre; for contenteditable --------- Co-authored-by: Peter <[email protected]>
- Loading branch information
Showing
9 changed files
with
196 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Finds nearest next carriage return symbol from passed position | ||
* | ||
* @param text - string to search in | ||
* @param position - search starting position | ||
*/ | ||
export function findNextHardLineBoundary(text: string, position: number): number { | ||
const nextLineBoundary = /\n/g; | ||
|
||
nextLineBoundary.lastIndex = position; | ||
|
||
const match = nextLineBoundary.exec(text); | ||
|
||
return match ? match.index : text.length; | ||
} | ||
|
||
/** | ||
* Finds nearest previous caret symbol before passed position | ||
* | ||
* @param text - sting to search in | ||
* @param position - search finish position | ||
*/ | ||
export function findPreviousHardLineBoundary(text: string, position: number): number { | ||
const previousLineBoundary = /\n/g; | ||
|
||
let match = previousLineBoundary.exec(text); | ||
|
||
while (match) { | ||
const newMatch = previousLineBoundary.exec(text); | ||
|
||
if (!newMatch || newMatch.index >= position) { | ||
break; | ||
} | ||
|
||
match = newMatch; | ||
} | ||
|
||
return match && match.index < position ? match.index : 0; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const APOSTROPHE_AND_CURLY_QUOTES = "['\u2018\u2019]"; | ||
const PUNCTUATION = '.,!?:;"\\(\\){}\\[\\]<>@*~\\/\\-#$&|^%+='; | ||
const WHITESPACE = '\\s'; | ||
|
||
const WHITESPACE_AND_PUNCTUATION = `[${WHITESPACE}${PUNCTUATION}]`; | ||
|
||
/** | ||
* Finds the nearest next word boundary from the passed position | ||
* | ||
* @param text - string to search in | ||
* @param position - search starting position | ||
*/ | ||
export function findNextWordBoundary(text: string, position: number): number { | ||
const nextWordBoundary = new RegExp( | ||
/** | ||
* Match whitespace or punctuation | ||
* or an apostrophe or curly quotes followed by a whitespace character or punctuation | ||
*/ | ||
`(${WHITESPACE_AND_PUNCTUATION}|${APOSTROPHE_AND_CURLY_QUOTES}(?=${WHITESPACE_AND_PUNCTUATION}))`, | ||
'g' | ||
); | ||
|
||
/** | ||
* Start searching from the next character to allow word deletion with one non-word character before the word | ||
*/ | ||
nextWordBoundary.lastIndex = position + 1; | ||
|
||
const match = nextWordBoundary.exec(text); | ||
|
||
return match ? match.index : text.length; | ||
} | ||
|
||
/** | ||
* Finds the nearest previous word boundary before the passed position | ||
* | ||
* @param text - string to search in | ||
* @param position - search finish position | ||
*/ | ||
export function findPreviousWordBoundary(text: string, position: number): number { | ||
const previousWordBoundary = new RegExp( | ||
/** | ||
* Match whitespace or punctuation, | ||
* or an apostrophe or curly quotes preceded by whitespace or punctuation | ||
*/ | ||
`(${WHITESPACE_AND_PUNCTUATION}|(?<=${WHITESPACE_AND_PUNCTUATION})${APOSTROPHE_AND_CURLY_QUOTES})`, | ||
'g' | ||
); | ||
|
||
let match = previousWordBoundary.exec(text); | ||
|
||
while (match) { | ||
const newMatch = previousWordBoundary.exec(text); | ||
|
||
if (!newMatch || newMatch.index >= position) { | ||
break; | ||
} | ||
|
||
match = newMatch; | ||
} | ||
|
||
return match && match.index < position ? match.index : 0; | ||
} |
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
dada291
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.
Coverage report for
./packages/model
Test suite run success
389 tests passing in 24 suites.
Report generated by 🧪jest coverage report action from dada291