@nookuio/iframe 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/createClient.d.ts +9 -13
- package/dist/createClient.js +50 -23
- package/dist/createClient.mjs +47 -27
- package/dist/editor.d.ts +1 -1
- package/dist/editor.js +15 -34
- package/dist/editor.mjs +11 -27
- package/dist/iframe.d.ts +8 -1
- package/dist/iframe.js +22 -33
- package/dist/iframe.mjs +17 -26
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -11
- package/dist/index.mjs +0 -1
- package/dist/types.d.ts +2 -2
- package/package.json +4 -3
package/dist/createClient.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ContextRequest, ContextResponse,
|
|
1
|
+
import type { ContextRequest, ContextResponse, EventRequest } from './types';
|
|
2
2
|
type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;
|
|
3
3
|
type ClientReturnBase<R, L, RE, LE> = {
|
|
4
4
|
[K in keyof R]: R[K] extends (...args: any[]) => any ? (...args: Parameters<R[K]>) => Promise<Awaited<ReturnType<R[K]>>> : R[K] extends any[] ? Promise<R[K]> : R[K] extends object ? ClientReturnBase<R[K], L, RE, LE> : Promise<R[K]>;
|
|
@@ -12,31 +12,27 @@ type ClientReturn<R, L, RE, LE> = ClientReturnBase<R, L, RE, LE> & {
|
|
|
12
12
|
};
|
|
13
13
|
interface ClientOptions {
|
|
14
14
|
/**
|
|
15
|
-
* Function to send requests to the
|
|
15
|
+
* Function to send requests to the remote process
|
|
16
16
|
*
|
|
17
17
|
* Should return a ContextResponse
|
|
18
18
|
*/
|
|
19
19
|
invoke: (request: ContextRequest) => Promise<ContextResponse>;
|
|
20
20
|
/**
|
|
21
21
|
* Function to handle incoming requests from the remote process
|
|
22
|
+
*
|
|
23
|
+
* Will be called only once at the start of the client
|
|
22
24
|
*/
|
|
23
25
|
handle: (handler: (request: ContextRequest) => Promise<ContextResponse>) => void;
|
|
24
26
|
/**
|
|
25
27
|
* Function to emit events to the remote process
|
|
26
28
|
*/
|
|
27
|
-
emit: (
|
|
28
|
-
/**
|
|
29
|
-
* Function to subscribe to events from the remote process
|
|
30
|
-
*/
|
|
31
|
-
subscribe: (eventName: string, listener: EventListener) => void;
|
|
29
|
+
emit: (request: EventRequest) => void;
|
|
32
30
|
/**
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Remove all the listeners
|
|
31
|
+
* Function to handle events from the remote process
|
|
32
|
+
*
|
|
33
|
+
* Will be called only once at the start of the client
|
|
38
34
|
*/
|
|
39
|
-
|
|
35
|
+
handleEvent: (handleRequest: (request: EventRequest) => void) => void;
|
|
40
36
|
/**
|
|
41
37
|
* Custom function to serialize data
|
|
42
38
|
*
|
package/dist/createClient.js
CHANGED
|
@@ -6,31 +6,30 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.createClient = createClient;
|
|
7
7
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
8
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
-
function defaultSerialize(data) {
|
|
10
|
-
try {
|
|
11
|
-
return JSON.stringify(data);
|
|
12
|
-
} catch (error) {
|
|
13
|
-
return data;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function defaultDeserialize(data) {
|
|
17
|
-
try {
|
|
18
|
-
return JSON.parse(data);
|
|
19
|
-
} catch (error) {
|
|
20
|
-
return data;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
9
|
function createClient(localCtx, options) {
|
|
24
10
|
const {
|
|
25
11
|
invoke,
|
|
26
12
|
handle,
|
|
27
|
-
subscribe,
|
|
28
|
-
removeAllListeners,
|
|
29
13
|
emit,
|
|
30
|
-
|
|
31
|
-
serialize
|
|
32
|
-
deserialize
|
|
14
|
+
handleEvent,
|
|
15
|
+
serialize: _serialize,
|
|
16
|
+
deserialize: _deserialize
|
|
33
17
|
} = options;
|
|
18
|
+
function serialize(data) {
|
|
19
|
+
try {
|
|
20
|
+
return _serialize ? _serialize(data) : JSON.stringify(data);
|
|
21
|
+
} catch {
|
|
22
|
+
return data;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function deserialize(data) {
|
|
26
|
+
try {
|
|
27
|
+
return _deserialize ? _deserialize(data) : JSON.parse(data);
|
|
28
|
+
} catch {
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const listenersCollection = /* @__PURE__ */new Map();
|
|
34
33
|
handle(async request => {
|
|
35
34
|
const value = _lodash.default.get(localCtx, request.key);
|
|
36
35
|
if (value === void 0) return {
|
|
@@ -47,6 +46,11 @@ function createClient(localCtx, options) {
|
|
|
47
46
|
result: serialize(result)
|
|
48
47
|
};
|
|
49
48
|
});
|
|
49
|
+
handleEvent(request => {
|
|
50
|
+
const currentEventListeners = listenersCollection.get(request.eventName);
|
|
51
|
+
if (!currentEventListeners?.length) return;
|
|
52
|
+
currentEventListeners.forEach(listener => listener(...deserialize(request.args || [])));
|
|
53
|
+
});
|
|
50
54
|
const proxyCache = /* @__PURE__ */new Map();
|
|
51
55
|
function createProxyForPath(path) {
|
|
52
56
|
const pathKey = path.join(".");
|
|
@@ -86,10 +90,33 @@ function createClient(localCtx, options) {
|
|
|
86
90
|
const rootProxy = new Proxy({}, {
|
|
87
91
|
get(_target, prop) {
|
|
88
92
|
if (prop === "$context") return localCtx;
|
|
89
|
-
if (prop === "on")
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
+
if (prop === "on") {
|
|
94
|
+
const subscribeHandler = (eventName, listener) => {
|
|
95
|
+
const currentEventListeners = listenersCollection.get(eventName);
|
|
96
|
+
if (!currentEventListeners) {
|
|
97
|
+
listenersCollection.set(eventName, [listener]);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
currentEventListeners.push(listener);
|
|
101
|
+
};
|
|
102
|
+
return subscribeHandler;
|
|
103
|
+
}
|
|
104
|
+
if (prop === "off") {
|
|
105
|
+
const unsubscribeHandler = (eventName, listener) => {
|
|
106
|
+
const index = listenersCollection.get(eventName)?.indexOf(listener);
|
|
107
|
+
if (index === -1 || index === void 0) return;
|
|
108
|
+
listenersCollection.get(eventName)?.splice(index, 1);
|
|
109
|
+
};
|
|
110
|
+
return unsubscribeHandler;
|
|
111
|
+
}
|
|
112
|
+
if (prop === "emit") {
|
|
113
|
+
const emitHandler = request => {
|
|
114
|
+
request.args = request.args?.map(arg => serialize(arg)) || [];
|
|
115
|
+
emit(request);
|
|
116
|
+
};
|
|
117
|
+
return emitHandler;
|
|
118
|
+
}
|
|
119
|
+
if (prop === "removeAllListeners") return () => listenersCollection.clear();
|
|
93
120
|
if (typeof prop === "symbol") return void 0;
|
|
94
121
|
return createProxyForPath([prop]);
|
|
95
122
|
}
|
package/dist/createClient.mjs
CHANGED
|
@@ -1,29 +1,21 @@
|
|
|
1
1
|
import _ from "lodash";
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
export function createClient(localCtx, options) {
|
|
3
|
+
const { invoke, handle, emit, handleEvent, serialize: _serialize, deserialize: _deserialize } = options;
|
|
4
|
+
function serialize(data) {
|
|
5
|
+
try {
|
|
6
|
+
return _serialize ? _serialize(data) : JSON.stringify(data);
|
|
7
|
+
} catch {
|
|
8
|
+
return data;
|
|
9
|
+
}
|
|
7
10
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
function deserialize(data) {
|
|
12
|
+
try {
|
|
13
|
+
return _deserialize ? _deserialize(data) : JSON.parse(data);
|
|
14
|
+
} catch {
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
14
17
|
}
|
|
15
|
-
|
|
16
|
-
export function createClient(localCtx, options) {
|
|
17
|
-
const {
|
|
18
|
-
invoke,
|
|
19
|
-
handle,
|
|
20
|
-
subscribe,
|
|
21
|
-
removeAllListeners,
|
|
22
|
-
emit,
|
|
23
|
-
unsubscribe,
|
|
24
|
-
serialize = defaultSerialize,
|
|
25
|
-
deserialize = defaultDeserialize
|
|
26
|
-
} = options;
|
|
18
|
+
const listenersCollection = /* @__PURE__ */ new Map();
|
|
27
19
|
handle(async (request) => {
|
|
28
20
|
const value = _.get(localCtx, request.key);
|
|
29
21
|
if (value === void 0)
|
|
@@ -41,6 +33,11 @@ export function createClient(localCtx, options) {
|
|
|
41
33
|
result: serialize(result)
|
|
42
34
|
};
|
|
43
35
|
});
|
|
36
|
+
handleEvent((request) => {
|
|
37
|
+
const currentEventListeners = listenersCollection.get(request.eventName);
|
|
38
|
+
if (!currentEventListeners?.length) return;
|
|
39
|
+
currentEventListeners.forEach((listener) => listener(...deserialize(request.args || [])));
|
|
40
|
+
});
|
|
44
41
|
const proxyCache = /* @__PURE__ */ new Map();
|
|
45
42
|
function createProxyForPath(path) {
|
|
46
43
|
const pathKey = path.join(".");
|
|
@@ -82,10 +79,33 @@ export function createClient(localCtx, options) {
|
|
|
82
79
|
{
|
|
83
80
|
get(_target, prop) {
|
|
84
81
|
if (prop === "$context") return localCtx;
|
|
85
|
-
if (prop === "on")
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
82
|
+
if (prop === "on") {
|
|
83
|
+
const subscribeHandler = (eventName, listener) => {
|
|
84
|
+
const currentEventListeners = listenersCollection.get(eventName);
|
|
85
|
+
if (!currentEventListeners) {
|
|
86
|
+
listenersCollection.set(eventName, [listener]);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
currentEventListeners.push(listener);
|
|
90
|
+
};
|
|
91
|
+
return subscribeHandler;
|
|
92
|
+
}
|
|
93
|
+
if (prop === "off") {
|
|
94
|
+
const unsubscribeHandler = (eventName, listener) => {
|
|
95
|
+
const index = listenersCollection.get(eventName)?.indexOf(listener);
|
|
96
|
+
if (index === -1 || index === void 0) return;
|
|
97
|
+
listenersCollection.get(eventName)?.splice(index, 1);
|
|
98
|
+
};
|
|
99
|
+
return unsubscribeHandler;
|
|
100
|
+
}
|
|
101
|
+
if (prop === "emit") {
|
|
102
|
+
const emitHandler = (request) => {
|
|
103
|
+
request.args = request.args?.map((arg) => serialize(arg)) || [];
|
|
104
|
+
emit(request);
|
|
105
|
+
};
|
|
106
|
+
return emitHandler;
|
|
107
|
+
}
|
|
108
|
+
if (prop === "removeAllListeners") return () => listenersCollection.clear();
|
|
89
109
|
if (typeof prop === "symbol") return void 0;
|
|
90
110
|
return createProxyForPath([prop]);
|
|
91
111
|
}
|
package/dist/editor.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { CoreIframeContext, CoreIframeEvents, EditorContext, EditorEvents }
|
|
|
2
2
|
/**
|
|
3
3
|
* This function should be called inside the editor
|
|
4
4
|
*/
|
|
5
|
-
export declare function
|
|
5
|
+
export declare function createEditorClient<IframeContext extends CoreIframeContext, IframeEvents extends CoreIframeEvents>(iframe: string | HTMLIFrameElement): { [K in keyof IframeContext]: IframeContext[K] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K]>) => Promise<Awaited<ReturnType<IframeContext[K]>>> : IframeContext[K] extends any[] ? Promise<IframeContext[K]> : IframeContext[K] extends object ? IframeContext[K] extends infer T ? { [K_1 in keyof T]: IframeContext[K][K_1] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1]>>> : IframeContext[K][K_1] extends any[] ? Promise<IframeContext[K][K_1]> : IframeContext[K][K_1] extends object ? IframeContext[K][K_1] extends infer T_1 ? { [K_2 in keyof T_1]: IframeContext[K][K_1][K_2] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2]>>> : IframeContext[K][K_1][K_2] extends any[] ? Promise<IframeContext[K][K_1][K_2]> : IframeContext[K][K_1][K_2] extends object ? IframeContext[K][K_1][K_2] extends infer T_2 ? { [K_3 in keyof T_2]: IframeContext[K][K_1][K_2][K_3] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2][K_3]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2][K_3]>>> : IframeContext[K][K_1][K_2][K_3] extends any[] ? Promise<IframeContext[K][K_1][K_2][K_3]> : IframeContext[K][K_1][K_2][K_3] extends object ? IframeContext[K][K_1][K_2][K_3] extends infer T_3 ? { [K_4 in keyof T_3]: IframeContext[K][K_1][K_2][K_3][K_4] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2][K_3][K_4]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2][K_3][K_4]>>> : IframeContext[K][K_1][K_2][K_3][K_4] extends any[] ? Promise<IframeContext[K][K_1][K_2][K_3][K_4]> : IframeContext[K][K_1][K_2][K_3][K_4] extends object ? IframeContext[K][K_1][K_2][K_3][K_4] extends infer T_4 ? { [K_5 in keyof T_4]: IframeContext[K][K_1][K_2][K_3][K_4][K_5] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2][K_3][K_4][K_5]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2][K_3][K_4][K_5]>>> : IframeContext[K][K_1][K_2][K_3][K_4][K_5] extends any[] ? Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5]> : IframeContext[K][K_1][K_2][K_3][K_4][K_5] extends object ? IframeContext[K][K_1][K_2][K_3][K_4][K_5] extends infer T_5 ? { [K_6 in keyof T_5]: IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6]>>> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6] extends any[] ? Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6]> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6] extends object ? IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6] extends infer T_6 ? { [K_7 in keyof T_6]: IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]>>> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends any[] ? Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends object ? IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends infer T_7 ? { [K_8 in keyof T_7]: IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]>>> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends any[] ? Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends object ? IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends infer T_8 ? { [K_9 in keyof T_8]: IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]>>> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends any[] ? Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends object ? IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends infer T_9 ? { [K_10 in keyof T_9]: IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends (...args: any[]) => any ? (...args: Parameters<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10]>) => Promise<Awaited<ReturnType<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10]>>> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends any[] ? Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10]> : IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends object ? /*elided*/ any : Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10]>; } : never : Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]>; } : never : Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]>; } : never : Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]>; } : never : Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5][K_6]>; } : never : Promise<IframeContext[K][K_1][K_2][K_3][K_4][K_5]>; } : never : Promise<IframeContext[K][K_1][K_2][K_3][K_4]>; } : never : Promise<IframeContext[K][K_1][K_2][K_3]>; } : never : Promise<IframeContext[K][K_1][K_2]>; } : never : Promise<IframeContext[K][K_1]>; } : never : Promise<IframeContext[K]>; } & {
|
|
6
6
|
$context: EditorContext;
|
|
7
7
|
on: <E extends keyof IframeEvents>(eventName: E, listener: (...args: IframeEvents[E] extends infer T_10 ? T_10 extends IframeEvents[E] ? T_10 extends (...args: infer A) => any ? A : never : never : never) => void) => void;
|
|
8
8
|
off: <E extends keyof IframeEvents>(eventName: E, listener: (...args: IframeEvents[E] extends infer T_10 ? T_10 extends IframeEvents[E] ? T_10 extends (...args: infer A) => any ? A : never : never : never) => void) => void;
|
package/dist/editor.js
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.createEditorClient = createEditorClient;
|
|
7
7
|
var _constants = require("./constants");
|
|
8
8
|
var _createClient = require("./createClient");
|
|
9
9
|
var _telejson = require("telejson");
|
|
10
10
|
const listenersCollection = /* @__PURE__ */new Map();
|
|
11
|
-
function
|
|
11
|
+
function createEditorClient(iframe) {
|
|
12
12
|
let iframeElement;
|
|
13
13
|
function getIframe() {
|
|
14
14
|
if (!iframeElement) {
|
|
@@ -17,17 +17,6 @@ function createIframeClient(iframe) {
|
|
|
17
17
|
return iframeElement;
|
|
18
18
|
}
|
|
19
19
|
const client = (0, _createClient.createClient)({}, {
|
|
20
|
-
emit(eventName, ...args) {
|
|
21
|
-
const iframe2 = getIframe();
|
|
22
|
-
if (!iframe2) return;
|
|
23
|
-
iframe2.contentWindow?.postMessage({
|
|
24
|
-
source: _constants.EDITOR_SOURCE_NAME,
|
|
25
|
-
request: {
|
|
26
|
-
eventName,
|
|
27
|
-
args
|
|
28
|
-
}
|
|
29
|
-
}, "*");
|
|
30
|
-
},
|
|
31
20
|
handle(handler) {
|
|
32
21
|
window.addEventListener("message", async event => {
|
|
33
22
|
if (typeof event.data !== "object") return;
|
|
@@ -69,33 +58,25 @@ function createIframeClient(iframe) {
|
|
|
69
58
|
window.addEventListener("message", messageHandler);
|
|
70
59
|
});
|
|
71
60
|
},
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
if (!
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
},
|
|
80
|
-
unsubscribe(eventName, listener) {
|
|
81
|
-
const index = listenersCollection.get(eventName)?.indexOf(listener);
|
|
82
|
-
if (index === -1 || index === void 0) return;
|
|
83
|
-
listenersCollection.get(eventName)?.splice(index, 1);
|
|
61
|
+
emit(request) {
|
|
62
|
+
const iframe2 = getIframe();
|
|
63
|
+
if (!iframe2) return;
|
|
64
|
+
iframe2.contentWindow?.postMessage({
|
|
65
|
+
source: _constants.EDITOR_SOURCE_NAME,
|
|
66
|
+
request
|
|
67
|
+
}, "*");
|
|
84
68
|
},
|
|
85
|
-
|
|
86
|
-
|
|
69
|
+
handleEvent(handleRequest) {
|
|
70
|
+
window.addEventListener("message", event => {
|
|
71
|
+
if (typeof event.data !== "object") return;
|
|
72
|
+
if (event.data.source !== _constants.IFRAME_SOURCE_NAME || !event.data.request) return;
|
|
73
|
+
handleRequest(event.data.request);
|
|
74
|
+
});
|
|
87
75
|
},
|
|
88
76
|
deserialize: v => (0, _telejson.parse)(v),
|
|
89
77
|
serialize: v => (0, _telejson.stringify)(v, {
|
|
90
78
|
maxDepth: Infinity
|
|
91
79
|
})
|
|
92
80
|
});
|
|
93
|
-
window.addEventListener("message", event => {
|
|
94
|
-
if (typeof event.data !== "object") return;
|
|
95
|
-
if (event.data.source !== _constants.IFRAME_SOURCE_NAME || !event.data.request) return;
|
|
96
|
-
const currentEventListeners = listenersCollection.get(event.data.request.eventName);
|
|
97
|
-
if (!currentEventListeners?.length) return;
|
|
98
|
-
currentEventListeners.forEach(listener => listener(...event.data.request.args));
|
|
99
|
-
});
|
|
100
81
|
return client;
|
|
101
82
|
}
|
package/dist/editor.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { EDITOR_SOURCE_NAME, IFRAME_SOURCE_NAME } from "./constants.mjs";
|
|
|
2
2
|
import { createClient } from "./createClient.mjs";
|
|
3
3
|
import { stringify, parse } from "telejson";
|
|
4
4
|
const listenersCollection = /* @__PURE__ */ new Map();
|
|
5
|
-
export function
|
|
5
|
+
export function createEditorClient(iframe) {
|
|
6
6
|
let iframeElement;
|
|
7
7
|
function getIframe() {
|
|
8
8
|
if (!iframeElement) {
|
|
@@ -13,11 +13,6 @@ export function createIframeClient(iframe) {
|
|
|
13
13
|
const client = createClient(
|
|
14
14
|
{},
|
|
15
15
|
{
|
|
16
|
-
emit(eventName, ...args) {
|
|
17
|
-
const iframe2 = getIframe();
|
|
18
|
-
if (!iframe2) return;
|
|
19
|
-
iframe2.contentWindow?.postMessage({ source: EDITOR_SOURCE_NAME, request: { eventName, args } }, "*");
|
|
20
|
-
},
|
|
21
16
|
handle(handler) {
|
|
22
17
|
window.addEventListener("message", async (event) => {
|
|
23
18
|
if (typeof event.data !== "object") return;
|
|
@@ -54,32 +49,21 @@ export function createIframeClient(iframe) {
|
|
|
54
49
|
window.addEventListener("message", messageHandler);
|
|
55
50
|
});
|
|
56
51
|
},
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
60
|
-
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
currentEventListenersCollection.push(listener);
|
|
64
|
-
},
|
|
65
|
-
unsubscribe(eventName, listener) {
|
|
66
|
-
const index = listenersCollection.get(eventName)?.indexOf(listener);
|
|
67
|
-
if (index === -1 || index === void 0) return;
|
|
68
|
-
listenersCollection.get(eventName)?.splice(index, 1);
|
|
52
|
+
emit(request) {
|
|
53
|
+
const iframe2 = getIframe();
|
|
54
|
+
if (!iframe2) return;
|
|
55
|
+
iframe2.contentWindow?.postMessage({ source: EDITOR_SOURCE_NAME, request }, "*");
|
|
69
56
|
},
|
|
70
|
-
|
|
71
|
-
|
|
57
|
+
handleEvent(handleRequest) {
|
|
58
|
+
window.addEventListener("message", (event) => {
|
|
59
|
+
if (typeof event.data !== "object") return;
|
|
60
|
+
if (event.data.source !== IFRAME_SOURCE_NAME || !event.data.request) return;
|
|
61
|
+
handleRequest(event.data.request);
|
|
62
|
+
});
|
|
72
63
|
},
|
|
73
64
|
deserialize: (v) => parse(v),
|
|
74
65
|
serialize: (v) => stringify(v, { maxDepth: Infinity })
|
|
75
66
|
}
|
|
76
67
|
);
|
|
77
|
-
window.addEventListener("message", (event) => {
|
|
78
|
-
if (typeof event.data !== "object") return;
|
|
79
|
-
if (event.data.source !== IFRAME_SOURCE_NAME || !event.data.request) return;
|
|
80
|
-
const currentEventListeners = listenersCollection.get(event.data.request.eventName);
|
|
81
|
-
if (!currentEventListeners?.length) return;
|
|
82
|
-
currentEventListeners.forEach((listener) => listener(...event.data.request.args));
|
|
83
|
-
});
|
|
84
68
|
return client;
|
|
85
69
|
}
|
package/dist/iframe.d.ts
CHANGED
|
@@ -2,7 +2,14 @@ import type { CoreIframeContext, CoreIframeEvents, EditorEvents } from './types'
|
|
|
2
2
|
/**
|
|
3
3
|
* This function should be called inside the iframe
|
|
4
4
|
*/
|
|
5
|
-
export declare function createIframeClient<IframeContext extends CoreIframeContext, IframeEvents extends CoreIframeEvents>(local: Exclude<IframeContext, CoreIframeContext
|
|
5
|
+
export declare function createIframeClient<IframeContext extends CoreIframeContext, IframeEvents extends CoreIframeEvents>(local: Exclude<IframeContext, CoreIframeContext>, options?: {
|
|
6
|
+
/**
|
|
7
|
+
* If true, the default console methods will be disabled
|
|
8
|
+
*
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
disableDefaultConsole?: boolean;
|
|
12
|
+
}): {} & {
|
|
6
13
|
$context: CoreIframeContext;
|
|
7
14
|
on: <E extends never>(eventName: E, listener: (...args: EditorEvents[E] extends infer T ? T extends EditorEvents[E] ? T extends (...args: infer A) => any ? A : never : never : never) => void) => void;
|
|
8
15
|
off: <E extends never>(eventName: E, listener: (...args: EditorEvents[E] extends infer T ? T extends EditorEvents[E] ? T extends (...args: infer A) => any ? A : never : never : never) => void) => void;
|
package/dist/iframe.js
CHANGED
|
@@ -7,7 +7,6 @@ exports.createIframeClient = createIframeClient;
|
|
|
7
7
|
var _constants = require("./constants");
|
|
8
8
|
var _createClient = require("./createClient");
|
|
9
9
|
var _telejson = require("telejson");
|
|
10
|
-
const listenersCollection = /* @__PURE__ */new Map();
|
|
11
10
|
function getSelector(path, id) {
|
|
12
11
|
return `[data-node-id="${id}"][data-node-path="${path}"]`;
|
|
13
12
|
}
|
|
@@ -37,7 +36,7 @@ function getElementStyles(selector, withPadding) {
|
|
|
37
36
|
if (!styles?.length) return;
|
|
38
37
|
return styles.length === 1 ? styles[0] : styles;
|
|
39
38
|
}
|
|
40
|
-
function createIframeClient(local) {
|
|
39
|
+
function createIframeClient(local, options) {
|
|
41
40
|
const client = (0, _createClient.createClient)({
|
|
42
41
|
document: window.document,
|
|
43
42
|
async editText({
|
|
@@ -142,15 +141,6 @@ function createIframeClient(local) {
|
|
|
142
141
|
}
|
|
143
142
|
}
|
|
144
143
|
}, {
|
|
145
|
-
emit(eventName, ...args) {
|
|
146
|
-
window.parent.postMessage({
|
|
147
|
-
source: _constants.IFRAME_SOURCE_NAME,
|
|
148
|
-
request: {
|
|
149
|
-
eventName,
|
|
150
|
-
args
|
|
151
|
-
}
|
|
152
|
-
}, "*");
|
|
153
|
-
},
|
|
154
144
|
handle(handler) {
|
|
155
145
|
window.addEventListener("message", async event => {
|
|
156
146
|
if (typeof event.data !== "object") return;
|
|
@@ -183,43 +173,42 @@ function createIframeClient(local) {
|
|
|
183
173
|
window.addEventListener("message", messageHandler);
|
|
184
174
|
});
|
|
185
175
|
},
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
currentEventListeners.push(listener);
|
|
193
|
-
},
|
|
194
|
-
unsubscribe(eventName, listener) {
|
|
195
|
-
const index = listenersCollection.get(eventName)?.indexOf(listener);
|
|
196
|
-
if (index === -1 || index === void 0) return;
|
|
197
|
-
listenersCollection.get(eventName)?.splice(index, 1);
|
|
176
|
+
emit(request) {
|
|
177
|
+
window.parent.postMessage({
|
|
178
|
+
source: _constants.IFRAME_SOURCE_NAME,
|
|
179
|
+
request
|
|
180
|
+
}, "*");
|
|
198
181
|
},
|
|
199
|
-
|
|
200
|
-
|
|
182
|
+
handleEvent(handleRequest) {
|
|
183
|
+
window.addEventListener("message", event => {
|
|
184
|
+
if (typeof event.data !== "object") return;
|
|
185
|
+
if (event.data.source !== _constants.EDITOR_SOURCE_NAME || !event.data.request) return;
|
|
186
|
+
handleRequest(event.data.request);
|
|
187
|
+
});
|
|
201
188
|
},
|
|
202
189
|
deserialize: v => (0, _telejson.parse)(v),
|
|
203
190
|
serialize: v => (0, _telejson.stringify)(v, {
|
|
204
191
|
maxDepth: Infinity
|
|
205
192
|
})
|
|
206
193
|
});
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if (!currentEventListeners?.length) return;
|
|
212
|
-
currentEventListeners.forEach(listener => listener(...event.data.request.args));
|
|
213
|
-
});
|
|
194
|
+
const defaultConsoleLog = options?.disableDefaultConsole ? void 0 : console.log.bind(console);
|
|
195
|
+
const defaultConsoleWarn = options?.disableDefaultConsole ? void 0 : console.warn.bind(console);
|
|
196
|
+
const defaultConsoleError = options?.disableDefaultConsole ? void 0 : console.error.bind(console);
|
|
197
|
+
const defaultConsoleInfo = options?.disableDefaultConsole ? void 0 : console.info.bind(console);
|
|
214
198
|
console.log = (...args) => {
|
|
215
199
|
client.emit("console", "log", args);
|
|
200
|
+
defaultConsoleLog?.(...args);
|
|
201
|
+
};
|
|
202
|
+
console.warn = (...args) => {
|
|
203
|
+
defaultConsoleWarn?.(...args);
|
|
216
204
|
};
|
|
217
|
-
console.warn = (...args) => {};
|
|
218
205
|
console.error = (...args) => {
|
|
219
206
|
client.emit("console", "error", args);
|
|
207
|
+
defaultConsoleError?.(...args);
|
|
220
208
|
};
|
|
221
209
|
console.info = (...args) => {
|
|
222
210
|
client.emit("console", "info", args);
|
|
211
|
+
defaultConsoleInfo?.(...args);
|
|
223
212
|
};
|
|
224
213
|
window.addEventListener("resize", async () => client.emit("resize", await client.$context.getPageHeight()));
|
|
225
214
|
const mutationObserver = new MutationObserver(async () => client.emit("resize", await client.$context.getPageHeight()));
|
package/dist/iframe.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { EDITOR_SOURCE_NAME, IFRAME_SOURCE_NAME } from "./constants.mjs";
|
|
2
2
|
import { createClient } from "./createClient.mjs";
|
|
3
3
|
import { stringify, parse } from "telejson";
|
|
4
|
-
const listenersCollection = /* @__PURE__ */ new Map();
|
|
5
4
|
function getSelector(path, id) {
|
|
6
5
|
return `[data-node-id="${id}"][data-node-path="${path}"]`;
|
|
7
6
|
}
|
|
@@ -31,7 +30,7 @@ function getElementStyles(selector, withPadding) {
|
|
|
31
30
|
if (!styles?.length) return;
|
|
32
31
|
return styles.length === 1 ? styles[0] : styles;
|
|
33
32
|
}
|
|
34
|
-
export function createIframeClient(local) {
|
|
33
|
+
export function createIframeClient(local, options) {
|
|
35
34
|
const client = createClient(
|
|
36
35
|
{
|
|
37
36
|
document: window.document,
|
|
@@ -123,9 +122,6 @@ export function createIframeClient(local) {
|
|
|
123
122
|
}
|
|
124
123
|
},
|
|
125
124
|
{
|
|
126
|
-
emit(eventName, ...args) {
|
|
127
|
-
window.parent.postMessage({ source: IFRAME_SOURCE_NAME, request: { eventName, args } }, "*");
|
|
128
|
-
},
|
|
129
125
|
handle(handler) {
|
|
130
126
|
window.addEventListener("message", async (event) => {
|
|
131
127
|
if (typeof event.data !== "object") return;
|
|
@@ -152,43 +148,38 @@ export function createIframeClient(local) {
|
|
|
152
148
|
window.addEventListener("message", messageHandler);
|
|
153
149
|
});
|
|
154
150
|
},
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (!currentEventListeners) {
|
|
158
|
-
listenersCollection.set(eventName, [listener]);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
currentEventListeners.push(listener);
|
|
162
|
-
},
|
|
163
|
-
unsubscribe(eventName, listener) {
|
|
164
|
-
const index = listenersCollection.get(eventName)?.indexOf(listener);
|
|
165
|
-
if (index === -1 || index === void 0) return;
|
|
166
|
-
listenersCollection.get(eventName)?.splice(index, 1);
|
|
151
|
+
emit(request) {
|
|
152
|
+
window.parent.postMessage({ source: IFRAME_SOURCE_NAME, request }, "*");
|
|
167
153
|
},
|
|
168
|
-
|
|
169
|
-
|
|
154
|
+
handleEvent(handleRequest) {
|
|
155
|
+
window.addEventListener("message", (event) => {
|
|
156
|
+
if (typeof event.data !== "object") return;
|
|
157
|
+
if (event.data.source !== EDITOR_SOURCE_NAME || !event.data.request) return;
|
|
158
|
+
handleRequest(event.data.request);
|
|
159
|
+
});
|
|
170
160
|
},
|
|
171
161
|
deserialize: (v) => parse(v),
|
|
172
162
|
serialize: (v) => stringify(v, { maxDepth: Infinity })
|
|
173
163
|
}
|
|
174
164
|
);
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (!currentEventListeners?.length) return;
|
|
180
|
-
currentEventListeners.forEach((listener) => listener(...event.data.request.args));
|
|
181
|
-
});
|
|
165
|
+
const defaultConsoleLog = options?.disableDefaultConsole ? void 0 : console.log.bind(console);
|
|
166
|
+
const defaultConsoleWarn = options?.disableDefaultConsole ? void 0 : console.warn.bind(console);
|
|
167
|
+
const defaultConsoleError = options?.disableDefaultConsole ? void 0 : console.error.bind(console);
|
|
168
|
+
const defaultConsoleInfo = options?.disableDefaultConsole ? void 0 : console.info.bind(console);
|
|
182
169
|
console.log = (...args) => {
|
|
183
170
|
client.emit("console", "log", args);
|
|
171
|
+
defaultConsoleLog?.(...args);
|
|
184
172
|
};
|
|
185
173
|
console.warn = (...args) => {
|
|
174
|
+
defaultConsoleWarn?.(...args);
|
|
186
175
|
};
|
|
187
176
|
console.error = (...args) => {
|
|
188
177
|
client.emit("console", "error", args);
|
|
178
|
+
defaultConsoleError?.(...args);
|
|
189
179
|
};
|
|
190
180
|
console.info = (...args) => {
|
|
191
181
|
client.emit("console", "info", args);
|
|
182
|
+
defaultConsoleInfo?.(...args);
|
|
192
183
|
};
|
|
193
184
|
window.addEventListener("resize", async () => client.emit("resize", await client.$context.getPageHeight()));
|
|
194
185
|
const mutationObserver = new MutationObserver(
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,17 +3,6 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
var _createClient = require("./createClient");
|
|
7
|
-
Object.keys(_createClient).forEach(function (key) {
|
|
8
|
-
if (key === "default" || key === "__esModule") return;
|
|
9
|
-
if (key in exports && exports[key] === _createClient[key]) return;
|
|
10
|
-
Object.defineProperty(exports, key, {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
get: function () {
|
|
13
|
-
return _createClient[key];
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
});
|
|
17
6
|
var _constants = require("./constants");
|
|
18
7
|
Object.keys(_constants).forEach(function (key) {
|
|
19
8
|
if (key === "default" || key === "__esModule") return;
|
package/dist/index.mjs
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -44,14 +44,14 @@ export interface CoreIframeContext {
|
|
|
44
44
|
}
|
|
45
45
|
export interface CoreIframeEvents {
|
|
46
46
|
ready: () => void;
|
|
47
|
-
'route-change': (to: string) => void;
|
|
48
47
|
resize: (height: number) => void;
|
|
48
|
+
console: (type: 'log' | 'error' | 'info' | 'warn', message: any[]) => void;
|
|
49
|
+
'route-change': (to: string) => void;
|
|
49
50
|
'text-update': (options: {
|
|
50
51
|
path: string;
|
|
51
52
|
id: string;
|
|
52
53
|
content: string;
|
|
53
54
|
}) => void;
|
|
54
|
-
console: (type: 'log' | 'error' | 'info' | 'warn', message: any[]) => void;
|
|
55
55
|
}
|
|
56
56
|
export interface IframeContext extends CoreIframeContext {
|
|
57
57
|
navigateTo: (to: string) => void;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nookuio/iframe",
|
|
3
3
|
"description": "",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"exports": {
|
|
@@ -30,13 +30,14 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"telejson": "
|
|
33
|
+
"telejson": "7.2.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {},
|
|
36
36
|
"keywords": [],
|
|
37
37
|
"author": "",
|
|
38
38
|
"license": "ISC",
|
|
39
39
|
"scripts": {
|
|
40
|
-
"build": "unbuild"
|
|
40
|
+
"build": "unbuild",
|
|
41
|
+
"release": "pnpm publish --no-git-checks --access public"
|
|
41
42
|
}
|
|
42
43
|
}
|