@kittl/sdk 0.0.9 → 1.0.0-alpha.1

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.
@@ -11,6 +11,10 @@ function unwrapResult(result) {
11
11
  }
12
12
 
13
13
  var FROM_EXTENSION_COMMANDS = {
14
+ /** Capability-owned AI callback registration. */
15
+ AI_GENERATOR_SUBSCRIBE: "ai-generator-subscribe",
16
+ /** Capability-owned AI callback teardown. */
17
+ AI_GENERATOR_UNSUBSCRIBE: "ai-generator-unsubscribe",
14
18
  /** SDK API call */
15
19
  SDK_CALL: "sdk-call",
16
20
  /** AI handler registration */
@@ -59,6 +63,8 @@ var FROM_EXTENSION_COMMANDS = {
59
63
  LOADED: "loaded"
60
64
  };
61
65
  var TO_EXTENSION_COMMANDS = {
66
+ /** Callback payload for generator or pending-set subscriptions. */
67
+ AI_GENERATOR_UPDATE: "ai-generator-update",
62
68
  /** SDK API response */
63
69
  SDK_RESPONSE: "sdk-response",
64
70
  /** File browser fetch content request */
@@ -75,6 +81,10 @@ var TO_EXTENSION_COMMANDS = {
75
81
  APP_STATE_UPDATE: "app-state-update"
76
82
  };
77
83
 
84
+ function isAIGeneratorUpdateMessage(data) {
85
+ return !!data && typeof data === "object" && "command" in data && data.command === "ai-generator-update" && "subscriptionId" in data && typeof data.subscriptionId === "string";
86
+ }
87
+
78
88
  var isSDKResponseMessage = (data) => {
79
89
  return typeof data === "object" && data !== null && "command" in data && data.command === TO_EXTENSION_COMMANDS.SDK_RESPONSE && "messageId" in data && typeof data.messageId === "string";
80
90
  };
@@ -144,7 +154,10 @@ var APPROVED_EXTENSION_COMMUNICATION_DOMAINS = [
144
154
  "https://app.stagingdesigner.com",
145
155
  "https://*.stagingdesigner.com",
146
156
  "http://localhost:3000",
147
- "https://app.kittl.com"
157
+ "https://app.kittl.com",
158
+ // website, for sandboxed standalone dashboard pages
159
+ "http://localhost:3003",
160
+ "https://www.kittl.com"
148
161
  ];
149
162
  var toOrigin = (value) => {
150
163
  if (!value) {
@@ -199,8 +212,15 @@ var getParentDomain = () => {
199
212
  };
200
213
 
201
214
  var pendingRequests = /* @__PURE__ */ new Map();
215
+ var sdkVersion;
216
+ function setSDKVersion(version2) {
217
+ sdkVersion = version2;
218
+ }
202
219
  var postMessageToParent = (message) => {
203
- window.parent.postMessage(message, getParentDomain());
220
+ window.parent.postMessage(
221
+ sdkVersion === void 0 ? message : { ...message, sdkVersion },
222
+ getParentDomain()
223
+ );
204
224
  };
205
225
  var sendMessage = (message) => {
206
226
  postMessageToParent(message);
@@ -223,6 +243,27 @@ var sendLoaded = () => {
223
243
  };
224
244
 
225
245
  var createSDKCallMessage = {
246
+ aiGenerator: (dots, args, messageId) => ({
247
+ command: FROM_EXTENSION_COMMANDS.SDK_CALL,
248
+ dots,
249
+ args,
250
+ messageId,
251
+ apiType: "aiGenerator"
252
+ }),
253
+ mockup: (dots, args, messageId) => ({
254
+ command: FROM_EXTENSION_COMMANDS.SDK_CALL,
255
+ dots,
256
+ args,
257
+ messageId,
258
+ apiType: "mockup"
259
+ }),
260
+ fileBrowser: (dots, args, messageId) => ({
261
+ command: FROM_EXTENSION_COMMANDS.SDK_CALL,
262
+ dots,
263
+ args,
264
+ messageId,
265
+ apiType: "fileBrowser"
266
+ }),
226
267
  ai: (dots, args, messageId) => ({
227
268
  command: FROM_EXTENSION_COMMANDS.SDK_CALL,
228
269
  dots,
@@ -258,6 +299,13 @@ var createSDKCallMessage = {
258
299
  messageId,
259
300
  apiType: "font"
260
301
  }),
302
+ project: (dots, args, messageId) => ({
303
+ command: FROM_EXTENSION_COMMANDS.SDK_CALL,
304
+ dots,
305
+ args,
306
+ messageId,
307
+ apiType: "project"
308
+ }),
261
309
  state: (dots, args, messageId) => ({
262
310
  command: FROM_EXTENSION_COMMANDS.SDK_CALL,
263
311
  dots,
@@ -278,6 +326,27 @@ var createSDKCallMessage = {
278
326
  args,
279
327
  messageId,
280
328
  apiType: "data"
329
+ }),
330
+ brandKit: (dots, args, messageId) => ({
331
+ command: FROM_EXTENSION_COMMANDS.SDK_CALL,
332
+ dots,
333
+ args,
334
+ messageId,
335
+ apiType: "brandKit"
336
+ }),
337
+ openMicroEditor: (dots, args, messageId) => ({
338
+ command: FROM_EXTENSION_COMMANDS.SDK_CALL,
339
+ dots,
340
+ args,
341
+ messageId,
342
+ apiType: "openMicroEditor"
343
+ }),
344
+ openMainEditor: (dots, args, messageId) => ({
345
+ command: FROM_EXTENSION_COMMANDS.SDK_CALL,
346
+ dots,
347
+ args,
348
+ messageId,
349
+ apiType: "openMainEditor"
281
350
  })
282
351
  };
283
352
  function createApiProxy(apiType) {
@@ -294,6 +363,64 @@ function createApiProxy(apiType) {
294
363
  });
295
364
  }
296
365
 
366
+ function createAIGeneratorBridgeApi() {
367
+ const callbacks = /* @__PURE__ */ new Map();
368
+ const proxy = createApiProxy("aiGenerator");
369
+ async function subscribe(request, callback) {
370
+ const subscriptionId = generateMessageId();
371
+ callbacks.set(subscriptionId, callback);
372
+ let response;
373
+ try {
374
+ response = await sendMessage({
375
+ command: "ai-generator-subscribe",
376
+ ...request,
377
+ subscriptionId,
378
+ messageId: generateMessageId()
379
+ });
380
+ } catch (error) {
381
+ callbacks.delete(subscriptionId);
382
+ throw error;
383
+ }
384
+ if (!response.isOk) {
385
+ callbacks.delete(subscriptionId);
386
+ throw new Error(response.error.message);
387
+ }
388
+ return () => {
389
+ callbacks.delete(subscriptionId);
390
+ postMessageToParent({
391
+ command: "ai-generator-unsubscribe",
392
+ subscriptionId
393
+ });
394
+ };
395
+ }
396
+ const methods = {
397
+ registerGenerator: (id, callback) => subscribe({ method: "registerGenerator", args: [id] }, (update) => {
398
+ if (update) callback(update);
399
+ }),
400
+ subscribeToGenerationUpdates: (ids, callback) => subscribe(
401
+ { method: "subscribeToGenerationUpdates", args: [ids] },
402
+ (update) => {
403
+ if (update) callback(update);
404
+ }
405
+ ),
406
+ subscribeToPendingGenerations: (callback) => subscribe(
407
+ { method: "subscribeToPendingGenerations", args: [] },
408
+ () => callback()
409
+ ),
410
+ /** Callers may reuse richer internal submission objects; never send their local abort callbacks. */
411
+ cancelGenerations: (submission) => proxy.cancelGenerations({
412
+ generationIds: submission.generationIds,
413
+ until: submission.until
414
+ })
415
+ };
416
+ return {
417
+ api: new Proxy(proxy, {
418
+ get: (target, property, receiver) => property in methods ? methods[property] : Reflect.get(target, property, receiver)
419
+ }),
420
+ dispatch: (message) => callbacks.get(message.subscriptionId)?.(message.update)
421
+ };
422
+ }
423
+
297
424
  var generationHandlers = /* @__PURE__ */ new Map();
298
425
  var generationSubscriptions = /* @__PURE__ */ new Map();
299
426
  var dispatchAIGenerationUpdate = (subscriptionId, update) => {
@@ -661,7 +788,7 @@ var SandboxedAuthClient = class {
661
788
  }
662
789
  };
663
790
 
664
- var createBatch = async () => {
791
+ var createBatchFor = async (apiType) => {
665
792
  const response = await sendMessage({
666
793
  command: FROM_EXTENSION_COMMANDS.BATCH_CREATE,
667
794
  messageId: generateMessageId()
@@ -670,14 +797,17 @@ var createBatch = async () => {
670
797
  throw response.error;
671
798
  }
672
799
  const { batchKey } = response.result;
673
- const sendDesignCall = (dots, args) => sendMessage({
674
- command: FROM_EXTENSION_COMMANDS.SDK_CALL,
675
- dots,
676
- args,
677
- messageId: generateMessageId(),
678
- apiType: "design",
679
- batchKey
680
- });
800
+ const sendDesignCall = (dots, args) => {
801
+ const message = {
802
+ command: FROM_EXTENSION_COMMANDS.SDK_CALL,
803
+ dots: apiType === "project" ? ["design", "api", ...dots] : dots,
804
+ args,
805
+ messageId: generateMessageId(),
806
+ apiType,
807
+ batchKey
808
+ };
809
+ return sendMessage(message);
810
+ };
681
811
  const commit = async () => {
682
812
  const res = await sendMessage({
683
813
  command: FROM_EXTENSION_COMMANDS.BATCH_COMMIT,
@@ -704,6 +834,8 @@ var createBatch = async () => {
704
834
  };
705
835
  return buildNode([]);
706
836
  };
837
+ var createBatch = () => createBatchFor("design");
838
+ var createProjectBatch = () => createBatchFor("project");
707
839
 
708
840
  var createContextBridgeApi = () => {
709
841
  const contextProxy = createApiProxy("context");
@@ -871,6 +1003,7 @@ var designStateApi = createDesignStateBridgeApi();
871
1003
  var appStateApi = createAppStateBridgeApi();
872
1004
  var { api: contextApi, dispatch: dispatchContextUpdate } = createContextBridgeApi();
873
1005
  var authClient = new SandboxedAuthClient();
1006
+ var aiGeneratorBridge = createAIGeneratorBridgeApi();
874
1007
  var handleIncomingMessage = (event) => {
875
1008
  const parent = window.parent;
876
1009
  if (!parent || parent === window) {
@@ -893,6 +1026,10 @@ var handleIncomingMessage = (event) => {
893
1026
  statelessApi.dispatch(data.type, data.payload);
894
1027
  return;
895
1028
  }
1029
+ if (isAIGeneratorUpdateMessage(data)) {
1030
+ aiGeneratorBridge.dispatch(data);
1031
+ return;
1032
+ }
896
1033
  if (isAIGenerationUpdatePushMessage(data)) {
897
1034
  dispatchAIGenerationUpdate(data.subscriptionId, data.update);
898
1035
  return;
@@ -917,7 +1054,28 @@ var handleIncomingMessage = (event) => {
917
1054
  );
918
1055
  }
919
1056
  };
1057
+ var projectProxy = createApiProxy("project");
1058
+ var projectApi = {
1059
+ design: {
1060
+ state: {
1061
+ getState: projectProxy.design.state.getState,
1062
+ subscribe: designStateApi.subscribe
1063
+ },
1064
+ api: projectProxy.design.api,
1065
+ createBatch: createProjectBatch
1066
+ },
1067
+ app: {
1068
+ state: {
1069
+ getState: projectProxy.app.state.getState,
1070
+ subscribe: appStateApi.subscribe
1071
+ },
1072
+ api: projectProxy.app.api
1073
+ }
1074
+ };
920
1075
  var bridgeHandlers = {
1076
+ aiGenerator: aiGeneratorBridge.api,
1077
+ brandKit: createApiProxy("brandKit"),
1078
+ mockup: createApiProxy("mockup"),
921
1079
  ai: createAIApiProxy(),
922
1080
  context: contextApi,
923
1081
  data: createApiProxy("data"),
@@ -926,12 +1084,17 @@ var bridgeHandlers = {
926
1084
  appStore: appStateApi,
927
1085
  export: createApiProxy("export"),
928
1086
  font: createApiProxy("font"),
1087
+ project: projectApi,
929
1088
  state: createApiProxy("state"),
930
1089
  upload: createApiProxy("upload"),
931
- fileBrowser: createFileBrowserApiProxy(),
1090
+ fileBrowser: { getFiles: createApiProxy("fileBrowser").getFiles },
1091
+ fileBrowserRegistration: createFileBrowserApiProxy(),
1092
+ openMicroEditor: createApiProxy("openMicroEditor"),
1093
+ openMainEditor: createApiProxy("openMainEditor"),
932
1094
  auth: authClient,
933
1095
  stateless: statelessApi,
934
- createBatch
1096
+ createBatch,
1097
+ createProjectBatch
935
1098
  };
936
1099
 
937
1100
  // src/api/dragAndDrop/DragAndDropApi.ts
@@ -966,7 +1129,7 @@ var DragAndDropApi = class {
966
1129
  async dropImage(input) {
967
1130
  let objectId = null;
968
1131
  try {
969
- const offset = unwrapResult(await this.context.get()).dragAndDrop.offset;
1132
+ const offset = unwrapResult(await this.context.get()).iframeOffset;
970
1133
  const params = this.buildDropImageParams(input, offset);
971
1134
  objectId = await this.addDroppedImage(params);
972
1135
  if (input.persistImage) {
@@ -1197,29 +1360,31 @@ var DragAndDropApi = class {
1197
1360
 
1198
1361
  // src/createEditorSDKAPIs.ts
1199
1362
  function createEditorSDKAPIs() {
1363
+ const { project } = bridgeHandlers;
1364
+ const dragAndDrop = new DragAndDropApi({
1365
+ state: project.app.api,
1366
+ context: bridgeHandlers.context,
1367
+ design: project.design.api,
1368
+ upload: bridgeHandlers.upload
1369
+ });
1200
1370
  return {
1201
- ai: bridgeHandlers.ai,
1202
- data: bridgeHandlers.data,
1203
- design: bridgeHandlers.design,
1371
+ aiGenerator: bridgeHandlers.aiGenerator,
1372
+ mockup: bridgeHandlers.mockup,
1373
+ project,
1204
1374
  export: bridgeHandlers.export,
1205
- designStore: bridgeHandlers.designStore,
1206
- appStore: bridgeHandlers.appStore,
1207
- context: bridgeHandlers.context,
1208
- dragAndDrop: new DragAndDropApi({
1209
- state: bridgeHandlers.state,
1210
- context: bridgeHandlers.context,
1211
- design: bridgeHandlers.design,
1212
- upload: bridgeHandlers.upload
1213
- }),
1214
- state: bridgeHandlers.state,
1375
+ helpers: { dragAndDrop },
1376
+ dragAndDrop,
1215
1377
  auth: bridgeHandlers.auth,
1216
1378
  upload: bridgeHandlers.upload,
1217
1379
  stateless: bridgeHandlers.stateless,
1218
1380
  font: bridgeHandlers.font,
1219
- createBatch: bridgeHandlers.createBatch
1381
+ fileBrowser: bridgeHandlers.fileBrowser
1220
1382
  };
1221
1383
  }
1222
1384
 
1385
+ // package.json
1386
+ var version = "1.0.0";
1387
+
1223
1388
  // src/utils/syncHtmlDataThemeFromHostContext.ts
1224
1389
  async function syncHtmlDataThemeFromHostContext(kittl2) {
1225
1390
  if (typeof document === "undefined") {
@@ -1260,12 +1425,17 @@ function createKittlSDK(apis) {
1260
1425
  handleIncomingMessage(event);
1261
1426
  }
1262
1427
  );
1263
- void syncHtmlDataThemeFromHostContext(kittl2).then(() => {
1428
+ const { context } = apis;
1429
+ const themeSynced = context?.get && context.subscribe ? syncHtmlDataThemeFromHostContext({
1430
+ context
1431
+ }) : Promise.resolve();
1432
+ void themeSynced.catch(() => void 0).then(() => {
1264
1433
  kittl2.onLoadedCallback?.();
1265
1434
  sendLoaded();
1266
1435
  });
1267
1436
  };
1268
1437
  if (typeof document !== "undefined" && typeof window !== "undefined") {
1438
+ setSDKVersion(version);
1269
1439
  window.kittl = kittl2;
1270
1440
  if (document.readyState === "loading") {
1271
1441
  document.addEventListener("DOMContentLoaded", initSdk);
@@ -1277,7 +1447,11 @@ function createKittlSDK(apis) {
1277
1447
  }
1278
1448
 
1279
1449
  // src/editorScriptRunner.ts
1280
- var kittl = createKittlSDK(createEditorSDKAPIs());
1450
+ var kittl = createKittlSDK({
1451
+ ...createEditorSDKAPIs(),
1452
+ // nothing is contextually relevant to a hidden script runner yet
1453
+ context: {}
1454
+ });
1281
1455
  export {
1282
1456
  kittl,
1283
1457
  unwrapResult