-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: introduce @trezor/dom-utils package
- Loading branch information
Showing
19 changed files
with
113 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@trezor/dom-utils", | ||
"version": "1.0.0", | ||
"private": true, | ||
"license": "See LICENSE.md in repo root", | ||
"sideEffects": false, | ||
"main": "src/index", | ||
"scripts": { | ||
"lint": "eslint '**/*.{ts,tsx,js}'", | ||
"type-check": "tsc --build" | ||
} | ||
} |
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,27 @@ | ||
/** | ||
* Returns string if there is an error, otherwise returns true | ||
*/ | ||
export const copyToClipboard = ( | ||
value: string, | ||
parent: HTMLDivElement | HTMLPreElement | HTMLButtonElement | null, | ||
) => { | ||
try { | ||
const container = parent || document.body; | ||
const el = document.createElement('textarea'); | ||
el.value = value; | ||
el.setAttribute('readonly', ''); | ||
el.style.position = 'absolute'; | ||
el.style.left = '-9999px'; | ||
container.appendChild(el); | ||
el.select(); | ||
el.setSelectionRange(0, 99999); /* For mobile devices */ | ||
const successful = document.execCommand('copy'); | ||
if (!successful) { | ||
throw new Error('Copy command unsuccessful'); | ||
} | ||
container.removeChild(el); | ||
return true; | ||
} catch (error) { | ||
return error.message; | ||
} | ||
}; |
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,12 @@ | ||
export const download = (value: string, filename: string) => { | ||
const element = document.createElement('a'); | ||
element.setAttribute('href', `data:text/plain;charset=utf-8,${encodeURIComponent(value)}`); | ||
element.setAttribute('download', filename); | ||
|
||
element.style.display = 'none'; | ||
document.body.appendChild(element); | ||
|
||
element.click(); | ||
|
||
document.body.removeChild(element); | ||
}; |
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,5 @@ | ||
export { copyToClipboard } from './copyToClipboard'; | ||
export { download } from './download'; | ||
export { moveCaretToEndOfContentEditable } from './moveCaretToEndOfContentEditable'; | ||
export { selectText } from './selectText'; | ||
export { setCaretPosition } from './setCaretPosition'; |
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,19 @@ | ||
/** | ||
* When focusing content editable element, caret appears at the begging of string it contains. | ||
* We need to move it to the end. | ||
* Solution from https://stackoverflow.com/questions/36284973/set-cursor-at-the-end-of-content-editable | ||
*/ | ||
export const moveCaretToEndOfContentEditable = (contentEditableElement: HTMLElement) => { | ||
let range; | ||
let selection; | ||
if (document.createRange) { | ||
range = document.createRange(); // Create a range (a range is a like the selection but invisible) | ||
range.selectNodeContents(contentEditableElement); // Select the entire contents of the element with the range | ||
range.collapse(false); // collapse the range to the end point. false means collapse to end rather than the start | ||
selection = window.getSelection(); // get the selection object (allows you to change selection) | ||
if (selection) { | ||
selection.removeAllRanges(); // remove any selections already made | ||
selection.addRange(range); // make the range you have just created the visible selection | ||
} | ||
} | ||
}; |
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,12 @@ | ||
export const selectText = (element: HTMLElement) => { | ||
const doc = document; | ||
if (window.getSelection) { | ||
const selection = window.getSelection(); | ||
if (selection) { | ||
const range = doc.createRange(); | ||
range.selectNodeContents(element); | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
} | ||
} | ||
}; |
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,11 @@ | ||
/** | ||
* Use this function to position caret in input element | ||
* @param el - input element ref | ||
* @param pos - index of value in el where caret should be positioned | ||
*/ | ||
export const setCaretPosition = (el: HTMLInputElement, pos: number) => { | ||
if (el.setSelectionRange) { | ||
el.focus(); | ||
el.setSelectionRange(pos, pos); | ||
} | ||
}; |
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,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { "outDir": "libDev" }, | ||
"references": [] | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/suite/src/components/suite/modals/confirm/Xpub/index.tsx
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
2 changes: 1 addition & 1 deletion
2
packages/suite/src/components/wallet/CoinmarketTransactionId/index.tsx
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
2 changes: 1 addition & 1 deletion
2
packages/suite/src/hooks/wallet/sign-verify/useCopySignedMessage.ts
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
2 changes: 1 addition & 1 deletion
2
packages/suite/src/hooks/wallet/useCoinmarketSavingsPaymentInfoCopy.ts
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