chore: abstract types to shared package (#1593)

* chore: update types

* small typing change

* fix typing issue

* typed node

* add extra lint skip

* add changeset

---------

Co-authored-by: Eoghan Murray <eoghan@getthere.ie>
Co-authored-by: Justin Halsall <Juice10@users.noreply.github.com>
This commit is contained in:
David Newell
2024-12-06 13:17:24 +00:00
committed by GitHub
parent bd9eb70629
commit 5a789385a3
36 changed files with 256 additions and 250 deletions

View File

@@ -1,10 +1,3 @@
import type {
serializedNodeWithId,
Mirror,
INode,
DataURLOptions,
} from 'rrweb-snapshot';
export enum EventType {
DomContentLoaded,
Load,
@@ -254,7 +247,7 @@ export type RecordPlugin<TOptions = unknown> = {
) => listenerHandler;
eventProcessor?: <TExtend>(event: eventWithTime) => eventWithTime & TExtend;
getMirror?: (mirrors: {
nodeMirror: Mirror;
nodeMirror: IMirror<Node>;
crossOriginIframeMirror: ICrossOriginIframeMirror;
crossOriginIframeStyleMirror: ICrossOriginIframeMirror;
}) => void;
@@ -615,6 +608,13 @@ export type customElementParam = {
export type customElementCallback = (c: customElementParam) => void;
/**
* @deprecated
*/
interface INode extends Node {
__sn: serializedNodeWithId;
}
export type DeprecatedMirror = {
map: {
[key: number]: INode;
@@ -705,6 +705,147 @@ export type TakeTypedKeyValues<Obj extends object, Type> = Pick<
TakeTypeHelper<Obj, Type>[keyof TakeTypeHelper<Obj, Type>]
>;
export enum NodeType {
Document,
DocumentType,
Element,
Text,
CDATA,
Comment,
}
export type documentNode = {
type: NodeType.Document;
childNodes: serializedNodeWithId[];
compatMode?: string;
};
export type documentTypeNode = {
type: NodeType.DocumentType;
name: string;
publicId: string;
systemId: string;
};
type cssTextKeyAttr = {
_cssText?: string;
};
export type attributes = cssTextKeyAttr & {
[key: string]:
| string
| number // properties e.g. rr_scrollLeft or rr_mediaCurrentTime
| true // e.g. checked on <input type="radio">
| null; // an indication that an attribute was removed (during a mutation)
};
export type legacyAttributes = {
/**
* @deprecated old bug in rrweb was causing these to always be set
* @see https://github.com/rrweb-io/rrweb/pull/651
*/
selected: false;
};
export type mediaAttributes = {
rr_mediaState: 'played' | 'paused';
rr_mediaCurrentTime: number;
/**
* for backwards compatibility this is optional but should always be set
*/
rr_mediaPlaybackRate?: number;
/**
* for backwards compatibility this is optional but should always be set
*/
rr_mediaMuted?: boolean;
/**
* for backwards compatibility this is optional but should always be set
*/
rr_mediaLoop?: boolean;
/**
* for backwards compatibility this is optional but should always be set
*/
rr_mediaVolume?: number;
};
export type elementNode = {
type: NodeType.Element;
tagName: string;
attributes: attributes;
childNodes: serializedNodeWithId[];
isSVG?: true;
needBlock?: boolean;
// This is a custom element or not.
isCustom?: true;
};
export type textNode = {
type: NodeType.Text;
textContent: string;
/**
* @deprecated styles are now always snapshotted against parent <style> element
* style mutations can still happen via an added textNode, but they don't need this attribute for correct replay
*/
isStyle?: true;
};
export type cdataNode = {
type: NodeType.CDATA;
textContent: '';
};
export type commentNode = {
type: NodeType.Comment;
textContent: string;
};
export type serializedNode = (
| documentNode
| documentTypeNode
| elementNode
| textNode
| cdataNode
| commentNode
) & {
rootId?: number;
isShadowHost?: boolean;
isShadow?: boolean;
};
export type serializedNodeWithId = serializedNode & { id: number };
export type serializedElementNodeWithId = Extract<
serializedNodeWithId,
Record<'type', NodeType.Element>
>;
export interface IMirror<TNode> {
getId(n: TNode | undefined | null): number;
getNode(id: number): TNode | null;
getIds(): number[];
getMeta(n: TNode): serializedNodeWithId | null;
removeNodeFromMap(n: TNode): void;
has(id: number): boolean;
hasNode(node: TNode): boolean;
add(n: TNode, meta: serializedNodeWithId): void;
replace(id: number, n: TNode): void;
reset(): void;
}
export type DataURLOptions = Partial<{
type: string;
quality: number;
}>;
// Types for @rrweb/packer
export type PackFn = (event: eventWithTime) => string;
export type UnpackFn = (raw: string) => eventWithTime;