Skip to content

Commit

Permalink
fix: support resize multiple images inside single paragraph
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed May 29, 2024
1 parent b39941c commit d813f6c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions projects/tui-editor/abstract/editor-adapter.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ export abstract class AbstractTuiEditor {
abstract setFileLink(preview: TuiEditorAttachedFile): void;
abstract setYoutubeVideo(options: TuiYoutubeOptions): void;
abstract setIframe(options: TuiEditableIframe): void;
abstract getHTML(): string;
}
19 changes: 16 additions & 3 deletions projects/tui-editor/components/editor/editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EventEmitter,
Inject,
Input,
NgZone,
OnDestroy,
Optional,
Output,
Expand All @@ -24,11 +25,12 @@ import {
TuiBooleanHandler,
TuiFocusableElementAccessor,
TuiStringHandler,
tuiZonefree,
} from '@taiga-ui/cdk';
import {TUI_ANIMATIONS_DEFAULT_DURATION} from '@taiga-ui/core';
import {AbstractTuiEditor} from '@tinkoff/tui-editor/abstract';
import {TuiToolbarComponent} from '@tinkoff/tui-editor/components/toolbar';
import {defaultEditorTools} from '@tinkoff/tui-editor/constants';
import {defaultEditorTools, TUI_EDITOR_RESIZE_EVENT} from '@tinkoff/tui-editor/constants';
import {
TuiTiptapEditorDirective,
TuiTiptapEditorService,
Expand All @@ -48,8 +50,8 @@ import {
TuiSelectionState,
} from '@tinkoff/tui-editor/utils';
import {Editor} from '@tiptap/core';
import {Observable} from 'rxjs';
import {delay, takeUntil} from 'rxjs/operators';
import {fromEvent, Observable} from 'rxjs';
import {delay, takeUntil, throttleTime} from 'rxjs/operators';

import {TUI_EDITOR_PROVIDERS} from './editor.providers';

Expand Down Expand Up @@ -105,6 +107,7 @@ export class TuiEditorComponent
@Inject(TUI_EDITOR_VALUE_TRANSFORMER)
transformer: AbstractTuiValueTransformer<string> | null,
@Inject(TUI_EDITOR_OPTIONS) readonly options: TuiEditorOptions,
@Inject(NgZone) private readonly zone: NgZone,
) {
super(control, cdr, transformer);

Expand All @@ -116,6 +119,7 @@ export class TuiEditorComponent
);

this.patchContentEditableElement();
this.listenResizeEvents();
});
}

Expand Down Expand Up @@ -284,4 +288,13 @@ export class TuiEditorComponent
String(this.options.spellcheck),
);
}

private listenResizeEvents(): void {
this.el?.nativeElement &&
fromEvent(this.el?.nativeElement, TUI_EDITOR_RESIZE_EVENT)
.pipe(throttleTime(0), tuiZonefree(this.zone), takeUntil(this.destroy$))
.subscribe(() =>
this.editorService.valueChange$.next(this.editorService.getHTML()),
);
}
}
1 change: 1 addition & 0 deletions projects/tui-editor/constants/default-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TUI_EDITOR_RESIZE_EVENT = `tui_editor_resize` as const;
1 change: 1 addition & 0 deletions projects/tui-editor/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './default-editor-colors';
export * from './default-editor-tools';
export * from './default-events';
export * from './default-font-options-handler';
export * from './default-html5-media-attributes';
export * from './default-link-options-handler';
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ export class TuiTiptapEditorService extends AbstractTuiEditor {
this.editor = editor;

const update = (): void => {
this.stateChange$.next();

const content = editor.getHTML();
const json = editor.getJSON().content;
const value: string = tuiIsEmptyParagraph(json) ? `` : content;

this.valueChange$.next(value);
this.stateChange$.next();
};

editor.on(`transaction`, update.bind(this));
Expand Down Expand Up @@ -412,4 +411,8 @@ export class TuiTiptapEditorService extends AbstractTuiEditor {
): void {
this.editor.commands.toggleMark(typeOrName, attributes, options);
}

getHTML(): string {
return this.getOriginTiptapEditor().getHTML() ?? ``;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {ChangeDetectionStrategy, Component, Inject} from '@angular/core';
import {ChangeDetectionStrategy, Component, ElementRef, Inject} from '@angular/core';
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import {TuiDestroyService} from '@taiga-ui/cdk';
import {AbstractTuiEditorResizable} from '@tinkoff/tui-editor/components/editor-resizable';
import {TUI_EDITOR_RESIZE_EVENT} from '@tinkoff/tui-editor/constants';

import {
TUI_IFRAME_EDITOR_OPTIONS,
Expand All @@ -24,6 +25,7 @@ export class TuiIframeEditorComponent extends AbstractTuiEditorResizable<TuiEdit
constructor(
@Inject(TUI_IFRAME_EDITOR_OPTIONS) readonly options: TuiEditableIframeOptions,
@Inject(DomSanitizer) private readonly sanitizer: DomSanitizer,
@Inject(ElementRef) private readonly el: ElementRef<HTMLDivElement>,
) {
super();
}
Expand All @@ -42,7 +44,8 @@ export class TuiIframeEditorComponent extends AbstractTuiEditorResizable<TuiEdit
this.attrs.width = this.currentWidth;
this.attrs.height = this.currentHeight;

// force update
this.editor.commands.setContent(this.editor.getJSON());
this.el.nativeElement.dispatchEvent(
new CustomEvent(TUI_EDITOR_RESIZE_EVENT, {bubbles: true}),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import {WINDOW} from '@ng-web-apis/common';
import {TuiDestroyService} from '@taiga-ui/cdk';
import {AbstractTuiEditorResizable} from '@tinkoff/tui-editor/components/editor-resizable';
import {TUI_EDITOR_RESIZE_EVENT} from '@tinkoff/tui-editor/constants';

import {
TUI_EDITOR_MAX_IMAGE_WIDTH,
Expand Down Expand Up @@ -87,8 +88,9 @@ export class TuiImageEditorComponent extends AbstractTuiEditorResizable<TuiEdita
this.currentWidth = Math.max(this.minWidth, Math.min(this.maxWidth, width));
this.attrs.width = this.currentWidth;

// force update
this.editor.commands.setContent(this.editor.getJSON());
this.el.nativeElement.dispatchEvent(
new CustomEvent(TUI_EDITOR_RESIZE_EVENT, {bubbles: true}),
);
}

private selectFakeText(): void {
Expand Down

0 comments on commit d813f6c

Please sign in to comment.