-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to update numeric style value by pressing up / down key (#70
) * refactor: add ui-logic directory * feat: add textual and numeric style value parser * feat: parse more complex css value * feat: allow to update numeric style value by pressing up / down key * test: fix failing test case * test: add test cases for getTextOffset
- Loading branch information
Showing
12 changed files
with
875 additions
and
32 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
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 was deleted.
Oops, something went wrong.
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,71 @@ | ||
import { lexStyleValue, lexToString } from './style-value-lexer' | ||
|
||
export function selectNodeContents(el: Node): void { | ||
// Skip if selection does not exist (in case of testing environment) | ||
if (!('getSelection' in window)) return | ||
|
||
const selection = window.getSelection() | ||
const range = new Range() | ||
range.selectNodeContents(el) | ||
selection.removeAllRanges() | ||
selection.addRange(range) | ||
} | ||
|
||
export function getTextOffset(node: Node, offset?: number): number { | ||
if (node.nodeType === Node.TEXT_NODE) { | ||
return offset !== undefined ? offset : (node as Text).wholeText.length | ||
} | ||
|
||
if ( | ||
node.nodeType === Node.COMMENT_NODE || | ||
node.nodeType === Node.CDATA_SECTION_NODE | ||
) { | ||
// Ignore comments and cdata... | ||
return 0 | ||
} | ||
|
||
const targetChildren = | ||
offset !== undefined | ||
? Array.from(node.childNodes).slice(0, offset) | ||
: Array.from(node.childNodes) | ||
|
||
return targetChildren.reduce((acc, c) => { | ||
return acc + getTextOffset(c) | ||
}, 0) | ||
} | ||
|
||
export function updateStyleValue( | ||
value: string, | ||
position: number, | ||
offset: number | ||
): { | ||
value: string | ||
range?: [number, number] | ||
} { | ||
return lexStyleValue(value).reduce( | ||
(acc, n) => { | ||
if (n.range[0] <= position && position < n.range[1]) { | ||
if (n.type === 'numeric') { | ||
const updated = { | ||
...n, | ||
value: n.value + offset | ||
} | ||
|
||
const str = lexToString(updated) | ||
|
||
return { | ||
value: acc.value + str, | ||
range: [acc.value.length, acc.value.length + str.length] | ||
} | ||
} | ||
} | ||
return { | ||
...acc, | ||
value: acc.value + lexToString(n) | ||
} | ||
}, | ||
{ | ||
value: '' | ||
} | ||
) | ||
} |
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
Oops, something went wrong.