Skip to content

Commit 57f343c

Browse files
authored
Merge pull request #705 from GetStream/draft-support
feat: export and import draft from/to message input
2 parents 06fb077 + 3334143 commit 57f343c

File tree

5 files changed

+529
-6
lines changed

5 files changed

+529
-6
lines changed

projects/stream-chat-angular/src/lib/channel.service.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ describe('ChannelService', () => {
359359
pinnedMessagesSpy.calls.reset();
360360
typingUsersSpy.calls.reset();
361361
typingUsersInThreadSpy.calls.reset();
362+
const channelSwitchStateSpy = jasmine.createSpy();
363+
service.channelSwitchState$.subscribe(channelSwitchStateSpy);
364+
channelSwitchStateSpy.calls.reset();
362365
service.deselectActiveChannel();
363366

364367
expect(messagesSpy).toHaveBeenCalledWith([]);
@@ -381,6 +384,10 @@ describe('ChannelService', () => {
381384

382385
expect(messagesSpy).not.toHaveBeenCalled();
383386
expect(service.isMessageLoadingInProgress).toBeFalse();
387+
388+
expect(channelSwitchStateSpy.calls.count()).toBe(2);
389+
expect(channelSwitchStateSpy.calls.first().args[0]).toBe('start');
390+
expect(channelSwitchStateSpy.calls.mostRecent().args[0]).toBe('end');
384391
});
385392

386393
it('should tell if user #hasMoreChannels$', async () => {
@@ -457,6 +464,9 @@ describe('ChannelService', () => {
457464
spyOn(newActiveChannel, 'markRead');
458465
const pinnedMessages = generateMockMessages();
459466
newActiveChannel.state.pinnedMessages = pinnedMessages;
467+
const channelSwitchStateSpy = jasmine.createSpy();
468+
service.channelSwitchState$.subscribe(channelSwitchStateSpy);
469+
channelSwitchStateSpy.calls.reset();
460470
service.setAsActiveChannel(newActiveChannel);
461471
result = spy.calls.mostRecent().args[0] as Channel;
462472

@@ -467,6 +477,9 @@ describe('ChannelService', () => {
467477
expect(pinnedMessagesSpy).toHaveBeenCalledWith(pinnedMessages);
468478
expect(typingUsersSpy).toHaveBeenCalledWith([]);
469479
expect(typingUsersInThreadSpy).toHaveBeenCalledWith([]);
480+
expect(channelSwitchStateSpy.calls.count()).toBe(2);
481+
expect(channelSwitchStateSpy.calls.first().args[0]).toBe('start');
482+
expect(channelSwitchStateSpy.calls.mostRecent().args[0]).toBe('end');
470483
});
471484

472485
it('should emit #activeChannelMessages$', async () => {

projects/stream-chat-angular/src/lib/channel.service.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ export class ChannelService {
307307
beforeUpdateMessage?: (
308308
message: StreamMessage
309309
) => StreamMessage | Promise<StreamMessage>;
310+
/**
311+
* Since switching channels changes the state of multiple obserables, this observable can be used to check if all observables are updated.
312+
* - `end` means all observables are in stable state
313+
* - `start` means all observables are in unstable state
314+
*/
315+
channelSwitchState$: Observable<'start' | 'end'>;
310316
/**
311317
* @internal
312318
*/
@@ -357,6 +363,9 @@ export class ChannelService {
357363
private channelQueryStateSubject = new BehaviorSubject<
358364
ChannelQueryState | undefined
359365
>(undefined);
366+
private channelSwitchStateSubject = new BehaviorSubject<'start' | 'end'>(
367+
'end'
368+
);
360369
private channelQuery?:
361370
| ChannelQuery
362371
| ((queryType: ChannelQueryType) => Promise<ChannelQueryResult>);
@@ -511,6 +520,9 @@ export class ChannelService {
511520
this.channelQueryState$ = this.channelQueryStateSubject
512521
.asObservable()
513522
.pipe(shareReplay(1));
523+
this.channelSwitchState$ = this.channelSwitchStateSubject
524+
.asObservable()
525+
.pipe(shareReplay(1));
514526
}
515527

516528
/**
@@ -557,6 +569,7 @@ export class ChannelService {
557569
* @param channel
558570
*/
559571
setAsActiveChannel(channel: Channel) {
572+
this.channelSwitchStateSubject.next('start');
560573
const prevActiveChannel = this.activeChannelSubject.getValue();
561574
if (prevActiveChannel?.cid === channel.cid) {
562575
return;
@@ -585,12 +598,14 @@ export class ChannelService {
585598
);
586599
}
587600
this.setChannelState(channel);
601+
this.channelSwitchStateSubject.next('end');
588602
}
589603

590604
/**
591605
* Deselects the currently active (if any) channel
592606
*/
593607
deselectActiveChannel() {
608+
this.channelSwitchStateSubject.next('start');
594609
const activeChannel = this.activeChannelSubject.getValue();
595610
if (!activeChannel) {
596611
return;
@@ -611,6 +626,7 @@ export class ChannelService {
611626
this.activeChannelUnreadCount = undefined;
612627
this.areReadEventsPaused = false;
613628
this.isMessageLoadingInProgress = false;
629+
this.channelSwitchStateSubject.next('end');
614630
}
615631

616632
/**
@@ -1138,7 +1154,10 @@ export class ChannelService {
11381154
* Selects or deselects the current message to quote reply to
11391155
* @param message The message to select, if called with `undefined`, it deselects the message
11401156
*/
1141-
selectMessageToQuote(message: StreamMessage | undefined) {
1157+
selectMessageToQuote(message: StreamMessage | undefined | MessageResponse) {
1158+
if (message && !this.isStreamMessage(message)) {
1159+
message = this.transformToStreamMessage(message);
1160+
}
11421161
this.messageToQuoteSubject.next(message);
11431162
}
11441163

projects/stream-chat-angular/src/lib/message-input/message-input.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@
171171
[autoFocus]="autoFocus"
172172
[placeholder]="textareaPlaceholder"
173173
[(value)]="textareaValue"
174-
(valueChange)="typingStart$.next()"
174+
(valueChange)="typingStart$.next(); updateMessageDraft()"
175175
(send)="messageSent()"
176-
(userMentions)="mentionedUsers = $event"
176+
(userMentions)="userMentionsChanged($event)"
177177
(pasteFromClipboard)="itemsPasted($event)"
178178
></ng-container>
179179
<ng-container *ngIf="emojiPickerTemplate" data-testid="emoji-picker">

0 commit comments

Comments
 (0)