Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit ba6fabb

Browse files
refactor: move selection utils to utils lib (#1377)
* feat: new util functions for selection Signed-off-by: peterpeterparker <[email protected]> * refactor: move selection utils to utils lib Signed-off-by: peterpeterparker <[email protected]> * refactor: move selection utils to utils lib Signed-off-by: peterpeterparker <[email protected]>
1 parent 81ca2df commit ba6fabb

File tree

17 files changed

+144
-100
lines changed

17 files changed

+144
-100
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
# [X.Y.Z](https://github.com/deckgo/deckdeckgo/compare/v4.10.0...v5.0.0) (202A-BB-CC)
44

5+
### Web Components
6+
7+
- drag-resize-rotate: v2.2.1 ([CHANGELOG](https://github.com/deckgo/deckdeckgo/blob/main/webcomponents/drag-resize-rotate/CHANGELOG.md))
8+
- inline-editor: v4.1.1 ([CHANGELOG](https://github.com/deckgo/deckdeckgo/blob/main/webcomponents/inline-editor/CHANGELOG.md)
9+
510
### Others
611

712
- deck-utils: v4.1.1 ([CHANGELOG](https://github.com/deckgo/deckdeckgo/blob/main/utils/deck/CHANGELOG.md))
13+
- utils: v2.2.0 ([CHANGELOG](https://github.com/deckgo/deckdeckgo/blob/main/utils/utils/CHANGELOG.md))
814

915
<a name="5.0.0"></a>
1016

package-lock.json

Lines changed: 38 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/utils/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.2.0 (2021-10-27)
2+
3+
### Features
4+
5+
- new util functions to interact with the selection API
6+
17
# 2.1.0 (2021-10-21)
28

39
### Fix

utils/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@deckdeckgo/utils",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"author": "David Dal Busco",
55
"description": "A collection of utils methods and functions developed for DeckDeckGo",
66
"license": "MIT",

utils/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './utils/utils';
22
export * from './utils/image-utils';
33
export * from './utils/inject-utils';
44
export * from './utils/color-utils';
5+
export * from './utils/selection.utils';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export const clearTheSelection = () => {
2+
if (window && window.getSelection) {
3+
if (window.getSelection()?.empty) {
4+
window.getSelection()?.empty();
5+
} else if (window.getSelection()?.removeAllRanges) {
6+
window.getSelection()?.removeAllRanges();
7+
}
8+
} else if (document && (document as any).selection) {
9+
(document as any).selection.empty();
10+
}
11+
};
12+
13+
export const getSelection = (): Selection | null => {
14+
if (window && window.getSelection) {
15+
return window.getSelection();
16+
} else if (document && document.getSelection) {
17+
return document.getSelection();
18+
} else if (document && (document as any).selection) {
19+
return (document as any).selection.createRange().text;
20+
}
21+
22+
return null;
23+
};
24+
25+
export const getAnchorElement = (selection: Selection | undefined): HTMLElement | null => {
26+
const anchorNode: Node | null | undefined = selection?.anchorNode;
27+
28+
if (!anchorNode) {
29+
return null;
30+
}
31+
32+
return anchorNode.nodeType !== Node.TEXT_NODE && anchorNode.nodeType !== Node.COMMENT_NODE
33+
? (anchorNode as HTMLElement)
34+
: anchorNode.parentElement;
35+
};
36+
37+
// https://stackoverflow.com/a/3866442/5404186
38+
export const moveCursorToEnd = (element: HTMLElement) => {
39+
if (window && document && document.createRange && element) {
40+
const range: Range = document.createRange();
41+
range.selectNodeContents(element);
42+
range.collapse(false);
43+
44+
const selection: Selection | null = getSelection();
45+
selection?.removeAllRanges();
46+
selection?.addRange(range);
47+
}
48+
};

webcomponents/drag-resize-rotate/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.2.1 (2021-10-27)
2+
3+
### Refactor
4+
5+
- move function to utils lib
6+
17
# 2.2.0 (2021-05-29)
28

39
### Build

webcomponents/drag-resize-rotate/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@deckdeckgo/drag-resize-rotate",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "A Web Component to drag, resize and rotate any element",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.js",
@@ -23,9 +23,6 @@
2323
"test.watch": "stencil test --spec --e2e --watchAll",
2424
"generate": "stencil generate"
2525
},
26-
"dependencies": {
27-
"@deckdeckgo/utils": "^2.0.1"
28-
},
2926
"license": "MIT",
3027
"repository": {
3128
"type": "git",
@@ -49,5 +46,8 @@
4946
"talk",
5047
"draggable",
5148
"resizable"
52-
]
49+
],
50+
"dependencies": {
51+
"@deckdeckgo/utils": "^2.2.0"
52+
}
5353
}

webcomponents/drag-resize-rotate/src/components/deckdeckgo-drr.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component, h, Host, Prop, State, Element, Event, EventEmitter, Build, Watch} from '@stencil/core';
22

3-
import {unifyEvent} from '@deckdeckgo/utils';
3+
import {moveCursorToEnd, unifyEvent} from '@deckdeckgo/utils';
44

55
interface ResizeMatrix {
66
a: 0 | 1;
@@ -607,23 +607,10 @@ export class DeckdeckgoDragResizeRotate {
607607
if (element) {
608608
element.focus();
609609

610-
await this.moveCursorToEnd(element);
610+
moveCursorToEnd(element);
611611
}
612612
};
613613

614-
// https://stackoverflow.com/a/3866442/5404186
615-
private async moveCursorToEnd(contentEditableElement: HTMLElement) {
616-
if (window && document && document.createRange && contentEditableElement) {
617-
const range: Range = document.createRange();
618-
range.selectNodeContents(contentEditableElement);
619-
range.collapse(false);
620-
621-
const selection: Selection = window.getSelection();
622-
selection.removeAllRanges();
623-
selection.addRange(range);
624-
}
625-
}
626-
627614
render() {
628615
const widthUnit: string = this.unit === 'percentage' ? '%' : this.unit === 'viewport' ? 'vw' : this.unit;
629616
const heightUnit: string = this.unit === 'percentage' ? '%' : this.unit === 'viewport' ? 'vh' : this.unit;

webcomponents/inline-editor/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 4.1.1
2+
3+
### Refactor
4+
5+
- selection utils extracted to utils lib
6+
17
# 4.1.0 (2021-05-29)
28

39
### Build

0 commit comments

Comments
 (0)