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

Commit c3dcc24

Browse files
fix: do not loose range selection focus on color input
1 parent 3cd2c3c commit c3dcc24

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

webcomponents/inline-editor/src/components/actions/color-actions/color-actions.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {DeckdeckgoPalette} from '@deckdeckgo/color';
44

55
import {hexToRgb} from '@deckdeckgo/utils';
66

7-
import {clearTheSelection, getSelection} from '../../../utils/selection.utils';
7+
import {getSelection} from '../../../utils/selection.utils';
88
import {findStyleNode, getAnchorNode} from '../../../utils/node.utils';
99

1010
import {ExecCommandAction} from '../../../interfaces/interfaces';
@@ -33,12 +33,17 @@ export class ColorActions {
3333
@Event()
3434
private execCommand: EventEmitter<ExecCommandAction>;
3535

36+
private range: Range | undefined;
37+
3638
async componentWillLoad() {
3739
await this.initColor();
3840
}
3941

4042
private async initColor() {
4143
const selection: Selection | undefined = await getSelection();
44+
45+
this.range = selection?.getRangeAt(0);
46+
4247
const container: HTMLElement | undefined = getAnchorNode(selection);
4348

4449
if (!container) {
@@ -67,21 +72,19 @@ export class ColorActions {
6772
return;
6873
}
6974

70-
if (selection.rangeCount <= 0 || !document) {
71-
return;
72-
}
75+
selection?.removeAllRanges();
76+
selection?.addRange(this.range);
7377

74-
const range: Range | undefined = selection.getRangeAt(0);
78+
const observer: MutationObserver = new MutationObserver( (_mutations: MutationRecord[]) => {
79+
observer.disconnect();
7580

76-
const text: string = range?.toString();
77-
78-
if (!text || text.length <= 0) {
79-
return;
80-
}
81+
// No node were added so the style was modified
82+
this.range = selection?.getRangeAt(0);
83+
});
8184

82-
await clearTheSelection();
85+
const anchorNode: HTMLElement | undefined = getAnchorNode(selection);
8386

84-
selection?.addRange(range);
87+
observer.observe(anchorNode, {childList: true});
8588

8689
this.execCommand.emit({
8790
cmd: 'style',

webcomponents/inline-editor/src/components/inline-editor/deckdeckgo-inline-editor.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ export class DeckdeckgoInlineEditor {
388388
}
389389

390390
private async handleSelectionChange(_$event: UIEvent) {
391+
if (this.toolbarActions === ToolbarActions.COLOR || this.toolbarActions === ToolbarActions.BACKGROUND_COLOR) {
392+
return;
393+
}
394+
391395
if (document && document.activeElement && !this.isContainer(document.activeElement)) {
392396
if (document.activeElement.nodeName.toLowerCase() !== 'deckgo-inline-editor') {
393397
await this.reset(false);
@@ -831,15 +835,15 @@ export class DeckdeckgoInlineEditor {
831835
}
832836

833837
private onAttributesChangesInitStyle() {
834-
const anchoreNode: HTMLElement | undefined = getAnchorNode(this.selection);
838+
const anchorNode: HTMLElement | undefined = getAnchorNode(this.selection);
835839

836840
const observer: MutationObserver = new MutationObserver(async () => {
837841
observer.disconnect();
838842

839-
await this.initStyleForNode(anchoreNode);
843+
await this.initStyleForNode(anchorNode);
840844
});
841845

842-
observer.observe(anchoreNode, {attributes: true});
846+
observer.observe(anchorNode, {attributes: true});
843847
}
844848

845849
render() {

0 commit comments

Comments
 (0)