Skip to content

Commit c5c8e88

Browse files
authored
feat(ringcentral-c2d): add prefix option for BaseWidget (#47)
* feat(ringcentral-c2d): add prefix option for BaseWidget * update minor version
1 parent 7822f4a commit c5c8e88

5 files changed

Lines changed: 24 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ringcentral-c2d",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "RingCentral Click-to-Call and Click-to-Text library",
55
"homepage": "https://github.com/ringcentral/ringcentral-c2d#readme",
66
"repository": {

src/RingCentralC2D.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class RingCentralC2D {
3636
}
3737

3838
dispose() {
39-
if (this._widget instanceof Emitter && !this._observer.disposed) {
39+
if (this._observer instanceof Emitter && !this._observer.disposed) {
4040
this._observer.off(ObserverEvents.hoverIn, this._onHoverIn);
4141
this._observer.off(ObserverEvents.hoverOut, this._onHoverOut);
4242
this._observer.dispose();

src/widgets/BaseWidget.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Emitter } from '../lib/Emitter';
22

3-
const BUBBLE_EVENTS_CHANNEL = 'rc-c2d-bubble-events';
3+
const BUBBLE_EVENTS_CHANNEL = 'c2d-bubble-events';
44

5-
interface BaseWidgetProps {
5+
export interface BaseWidgetProps {
6+
prefix?: string;
67
bubbleInIframe?: boolean;
78
}
89

@@ -14,15 +15,16 @@ interface BubbleEventMessage {
1415

1516
export class BaseWidget extends Emitter {
1617
_shouldBubbleEvents: boolean;
18+
_bubbleEventsChannel: string;
1719
_selfObserver?: MutationObserver;
1820
_observeNodes: Map<Node, Node> = new Map();
1921

20-
constructor(_props: BaseWidgetProps = {}) {
22+
constructor({ prefix = 'all', bubbleInIframe }: BaseWidgetProps = {}) {
2123
super();
2224

23-
this._shouldBubbleEvents =
24-
!!_props.bubbleInIframe && window !== window.parent;
25-
if (_props.bubbleInIframe) {
25+
this._bubbleEventsChannel = `${prefix}-${BUBBLE_EVENTS_CHANNEL}`;
26+
this._shouldBubbleEvents = !!bubbleInIframe && window !== window.parent;
27+
if (bubbleInIframe) {
2628
this._addBubbleEventListeners();
2729
}
2830
}
@@ -36,7 +38,7 @@ export class BaseWidget extends Emitter {
3638
return true;
3739
}
3840
this._applyBubbleEvent({
39-
channel: BUBBLE_EVENTS_CHANNEL,
41+
channel: this._bubbleEventsChannel,
4042
eventName,
4143
eventArgs: JSON.stringify(eventArgs),
4244
});
@@ -52,7 +54,7 @@ export class BaseWidget extends Emitter {
5254

5355
_onBubbleEvent = (event: MessageEvent<BubbleEventMessage | undefined>) => {
5456
const message = event.data;
55-
if (message?.channel === BUBBLE_EVENTS_CHANNEL) {
57+
if (message?.channel === this._bubbleEventsChannel) {
5658
this._applyBubbleEvent(message);
5759
}
5860
};
@@ -111,10 +113,8 @@ export class BaseWidget extends Emitter {
111113
}
112114

113115
_disconnectObserver() {
114-
if (this._selfObserver) {
115-
this._selfObserver.disconnect();
116-
this._selfObserver = undefined;
117-
}
116+
this._selfObserver?.disconnect();
117+
this._selfObserver = undefined;
118118
}
119119

120120
override dispose() {

src/widgets/BuiltinWidget/BuiltinWidget.interface.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export interface BuiltinWidgetProps {
1+
import type { BaseWidgetProps } from '../BaseWidget';
2+
3+
export interface BuiltinWidgetProps extends BaseWidgetProps {
24
logoIcon?: string;
35
/** As an option to automatically hide the number text when it is hovered. */
46
autoHide?: boolean;

src/widgets/BuiltinWidget/BuiltinWidget.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { type MatchRect } from '../../lib/NodeObserver';
1515
import { convertToInline } from '../../lib/convertToInline';
1616
import type { MatchContextModel } from '../../matchers';
1717
import { BaseWidget } from '../BaseWidget';
18-
import { type TargetItem, type IWidget } from '../Widget.interface';
18+
import { type IWidget, type TargetItem } from '../Widget.interface';
1919

2020
import {
2121
type BuiltinWidgetProps,
@@ -78,8 +78,12 @@ export class BuiltinWidget extends BaseWidget implements IWidget {
7878
return BuiltinWidgetEvents;
7979
}
8080

81-
constructor(protected _props: BuiltinWidgetProps = {}) {
82-
super({ bubbleInIframe: true });
81+
constructor(
82+
protected _props: BuiltinWidgetProps = {
83+
bubbleInIframe: true,
84+
},
85+
) {
86+
super(_props);
8387
this._injectDOM();
8488
}
8589

0 commit comments

Comments
 (0)