forked from chantastic/sites
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add select range button to */share page
- Loading branch information
1 parent
0389809
commit 73e6e22
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
chan.dev/src/content/decisions/0012-prefer-range-to-clipboard.md
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,29 @@ | ||
--- | ||
title: "12. Prefer range to clipboard" | ||
status: "Accepted" | ||
date: 2023-08-24 | ||
--- | ||
|
||
## Context | ||
|
||
I've attempted several times to get a clean clipboard item that will paste well into blogging platforms (Medium, dev.to, Hackernoon, Hashnode, etc.). | ||
|
||
While manual copy and paste worked, programatic copy and paste inserted additional whitespace. | ||
|
||
## Decision | ||
|
||
To ensure clean copy and paste, prefered `createRange` over `Clipboard`. Ranges allow one click selection of elements, allowing the user to easily grab a range and use system copy/paste. | ||
|
||
```js | ||
function selectElementContents(el) { | ||
var range = document.createRange(); | ||
range.selectNodeContents(el); | ||
var sel = window.getSelection(); | ||
sel.removeAllRanges(); | ||
sel.addRange(range); | ||
} | ||
``` | ||
|
||
## Consequences | ||
|
||
This will require two steps. But the output is more reliable. |
17 changes: 17 additions & 0 deletions
17
chan.dev/src/content/posts/select-dom-contents-programatically.md
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,17 @@ | ||
--- | ||
title: "Select DOM contents programatically" | ||
references: | ||
- https://developer.mozilla.org/en-US/docs/Web/API/Document/createRange | ||
--- | ||
|
||
```js | ||
function selectContents(domNode) { | ||
let range = document.createRange(); | ||
let sel = window.getSelection(); | ||
|
||
range.selectNodeContents(domNode); | ||
|
||
sel.removeAllRanges(); | ||
sel.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