Skip to content

Commit

Permalink
fix: do not save empty link (#1580)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Nov 18, 2024
1 parent 4f59c9f commit 3477f64
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions projects/editor/common/editor-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import type {TuiEditorAttachedFile} from './attached';
import type {TuiEditableIframe} from './iframe';
import type {TuiYoutubeOptions} from './youtube';

export interface TuiSelectionSnapshot {
anchor: number;
head: number;
}

@Directive()
export abstract class AbstractTuiEditor {
public abstract readonly isFocused: boolean;
Expand Down Expand Up @@ -82,6 +87,8 @@ export abstract class AbstractTuiEditor {
public abstract destroy(): void;
public abstract selectClosest(): void;
public abstract focus(): void;
public abstract takeSelectionSnapshot(): void;
public abstract getSelectionSnapshot(): TuiSelectionSnapshot | null;
public abstract setValue(value: string): void;
public abstract setCellColor(color: string): void;
public abstract getOriginTiptapEditor(): Editor | null;
Expand Down
9 changes: 9 additions & 0 deletions projects/editor/components/edit-link/edit-link.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ export class TuiEditLink {
this.url = this.removePrefix(url);
}

protected onBlur(url: string): void {
const range = this.editor?.getSelectionSnapshot();

if (range && !url) {
this.editor?.setTextSelection({from: range.anchor, to: range.head});
this.editor?.toggleLink('');
}
}

protected onClear(): void {
this.url = '';
}
Expand Down
5 changes: 4 additions & 1 deletion projects/editor/components/edit-link/edit-link.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
<ng-container *ngIf="texts$ | async as text">
{{ prefixIsHashMode ? text.anchorExample : text.urlExample }}
</ng-container>
<input [(ngModel)]="url" />
<input
[(ngModel)]="url"
(blur)="onBlur(url)"
/>
</tui-input-inline>
</div>
</label>
Expand Down
1 change: 1 addition & 0 deletions projects/editor/components/toolbar/toolbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export class TuiToolbar {
}

protected onLink(url?: string): void {
this.editor?.takeSelectionSnapshot();
this.editor?.toggleLink(url ?? '');
}

Expand Down
11 changes: 11 additions & 0 deletions projects/editor/directives/tiptap-editor/tiptap-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
TuiEditableIframe,
TuiEditableImage,
TuiEditorAttachedFile,
TuiSelectionSnapshot,
TuiYoutubeOptions,
} from '@taiga-ui/editor/common';
import {
Expand All @@ -29,6 +30,8 @@ export class TuiTiptapEditorService extends AbstractTuiEditor {

protected editor?: Editor;

protected selectionSnapshot: TuiSelectionSnapshot | null = null;

constructor() {
super();

Expand Down Expand Up @@ -429,6 +432,14 @@ export class TuiTiptapEditorService extends AbstractTuiEditor {
public getHTML(): string {
return this.getOriginTiptapEditor()?.getHTML() ?? '';
}

public takeSelectionSnapshot(): void {
this.selectionSnapshot = this.editor?.state.selection.toJSON() ?? null;
}

public getSelectionSnapshot(): TuiSelectionSnapshot | null {
return this.selectionSnapshot;
}
}

export type {TuiEditorTypes};

0 comments on commit 3477f64

Please sign in to comment.