Skip to content

Commit ca4e0ad

Browse files
committed
fix(link-tool): call clear function to reset link-tool state
1 parent 530ec56 commit ca4e0ad

5 files changed

Lines changed: 37 additions & 20 deletions

File tree

‎src/components/inline-tools/inline-tool-link.ts‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,11 @@ export default class LinkInlineTool implements InlineTool {
172172
* Unlink icon pressed
173173
*/
174174
if (parentAnchor) {
175-
/**
176-
* If input is not opened, treat click as explicit unlink action.
177-
* If input is opened (e.g., programmatic close when switching tools), avoid unlinking.
178-
*/
179-
if (!this.inputOpened) {
180-
this.selection.expandToTag(parentAnchor);
181-
this.unlink();
182-
this.closeActions();
183-
this.checkState();
184-
this.toolbar.close();
185-
} else {
186-
/** Only close actions without clearing saved selection to preserve user state */
187-
this.closeActions(false);
188-
this.checkState();
189-
}
175+
this.selection.expandToTag(parentAnchor);
176+
this.unlink();
177+
this.closeActions();
178+
this.checkState();
179+
this.toolbar.close();
190180

191181
return;
192182
}
@@ -270,14 +260,16 @@ export default class LinkInlineTool implements InlineTool {
270260
if (this.selection.isFakeBackgroundEnabled) {
271261
// if actions is broken by other selection We need to save new selection
272262
const currentSelection = new SelectionUtils();
273-
274263
currentSelection.save();
275264

276265
this.selection.restore();
277266
this.selection.removeFakeBackground();
278267

279-
// and recover new selection after removing fake background
280-
currentSelection.restore();
268+
// check if other selection happend
269+
if (!currentSelection.savedSelectionRange.collapsed) {
270+
// and recover new selection after removing fake background
271+
currentSelection.restore();
272+
}
281273
}
282274

283275
this.nodes.input.classList.remove(this.CSS.inputShowed);

‎src/components/modules/toolbar/inline.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
391391
onActivate: () => {
392392
this.toolClicked(instance);
393393
},
394+
onClear: () => {
395+
instance?.clear?.();
396+
},
394397
hint: {
395398
title: toolTitle,
396399
description: shortcutBeautified,

‎src/components/utils/popover/components/popover-item/popover-item.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ export abstract class PopoverItem {
2525
}
2626
}
2727

28+
/**
29+
* Calls instance clear function
30+
*/
31+
public clear(): void {
32+
if (this.params === undefined) {
33+
return;
34+
}
35+
36+
if (!('onClear' in this.params)) {
37+
return;
38+
}
39+
40+
this.params.onClear?.(this.params);
41+
}
42+
2843
/**
2944
* Destroys the instance
3045
*/

‎src/components/utils/popover/popover-inline.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ export class PopoverInline extends PopoverDesktop {
167167
if (item !== this.nestedPopoverTriggerItem) {
168168
/**
169169
* In case tool had special handling for toggling button (like link tool which modifies selection)
170-
* we need to call handleClick on nested popover trigger item
170+
* we need to call clear on nested popover trigger item to restore initial state
171171
*/
172-
this.nestedPopoverTriggerItem?.handleClick();
172+
this.nestedPopoverTriggerItem?.clear();
173173

174174
/**
175175
* Then close the nested popover

‎types/utils/popover/popover-item.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ export interface PopoverItemDefaultBaseParams {
178178
* @param event - event that initiated item activation
179179
*/
180180
onActivate: (item: PopoverItemParams, event?: PointerEvent) => void;
181+
182+
/**
183+
* Popover item clear handler
184+
*
185+
* @param item - item to be cleared
186+
*/
187+
onClear: (item: PopoverItemParams) => void;
181188
}
182189

183190
/**

0 commit comments

Comments
 (0)