@plaidev/karte-action-sdk 1.1.179-28041859.8b5339d0 → 1.1.180-28042066.2d66783f

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.
@@ -275,10 +275,6 @@ const NOOP = (_args) => { }; // eslint-disable-line @typescript-eslint/no-unused
275
275
  const isPreview = () => {
276
276
  return true;
277
277
  };
278
- // prettier-ignore
279
- /** @internal */
280
- const actionId = ALL_ACTION_ID
281
- ;
282
278
  /** @internal */
283
279
  const setPreviousFocus = () => {
284
280
  const previously_focused = typeof document !== 'undefined' && document.activeElement;
@@ -1035,6 +1031,47 @@ function cloneToJson(data) {
1035
1031
  }
1036
1032
  }
1037
1033
 
1034
+ // prettier-ignore
1035
+ /** @internal */
1036
+ const actionId = ALL_ACTION_ID
1037
+ ;
1038
+ /** @internal */
1039
+ const ACTION_DESTROY_EVENT = `KARTE-ACTION-DESTROY-${actionId}`;
1040
+ /** @internal */
1041
+ const ACTION_SHOW_EVENT = `KARTE-ACTION-SHOW-${actionId}`;
1042
+ /** @internal */
1043
+ const ACTION_CLOSE_EVENT = `KARTE-ACTION-CLOSE-${actionId}`;
1044
+ /** @internal */
1045
+ const ACTION_CHANGE_STATE_EVENT = `KARTE-ACTION-CHANGE-STATE-${actionId}`;
1046
+ /** @internal */
1047
+ const send_event = (event_name, values) => {
1048
+ const setting = getSetting();
1049
+ setting?.send?.(event_name, values);
1050
+ };
1051
+ /** @internal */
1052
+ const initialize = (setting) => {
1053
+ const newSetting = setSetting(setting);
1054
+ if (newSetting.initialState) {
1055
+ setState$1(newSetting.initialState);
1056
+ }
1057
+ setOpened(true);
1058
+ setClosed(false); // deprecated
1059
+ return () => finalize();
1060
+ };
1061
+ /** @internal */
1062
+ const finalize = () => {
1063
+ resetSetting();
1064
+ };
1065
+ /**
1066
+ * Dispatch the event to destroy KARTE action
1067
+ *
1068
+ * @internal
1069
+ */
1070
+ function dispatchDestroyEvent() {
1071
+ const event = new CustomEvent(ACTION_DESTROY_EVENT);
1072
+ window.dispatchEvent(event);
1073
+ }
1074
+
1038
1075
  /**
1039
1076
  * モーダル(ポップアップ)のロジックを管理する
1040
1077
  */
@@ -1130,20 +1167,6 @@ function showOnTime(props, show = NOOP) {
1130
1167
  : null;
1131
1168
  }
1132
1169
 
1133
- /** @internal */
1134
- const ACTION_DESTROY_EVENT = `KARTE-ACTION-DESTROY-${actionId}`;
1135
- /** @internal */
1136
- const ACTION_SHOW_EVENT = `KARTE-ACTION-SHOW-${actionId}`;
1137
- /** @internal */
1138
- const ACTION_CLOSE_EVENT = `KARTE-ACTION-CLOSE-${actionId}`;
1139
- /** @internal */
1140
- const ACTION_CHANGE_STATE_EVENT = `KARTE-ACTION-CHANGE-STATE-${actionId}`;
1141
- /** @internal */
1142
- const send_event = (event_name, values) => {
1143
- const setting = getSetting();
1144
- setting?.send?.(event_name, values);
1145
- };
1146
-
1147
1170
  /**
1148
1171
  * アクションテーブルに関連するコードの管理
1149
1172
  */
@@ -1199,6 +1222,7 @@ function collection$1(config) {
1199
1222
  },
1200
1223
  };
1201
1224
  }
1225
+ /** @internal */
1202
1226
  function request(url, data, cb) {
1203
1227
  console.debug('[debug] request', isPreview(), url, JSON.stringify(data));
1204
1228
  fetch(url, {
@@ -1226,6 +1250,7 @@ const loadActionTableRows = async (config, api_key, endpoint) => {
1226
1250
  const loadActionTableQuery = async (config, api_key, endpoint) => {
1227
1251
  return new Promise((resolve, reject) => collection$1({ endpoint, api_key, table: config.query.table_name }).getByQuery(config.query.query_name, config.query.params, null, (err, data) => (err ? reject(err) : resolve(data))));
1228
1252
  };
1253
+ /** @internal */
1229
1254
  const loadActionTable = async (config, api_key, endpoint) => {
1230
1255
  console.debug('[debug] loadActionTable', isPreview(), api_key, endpoint, JSON.stringify(config));
1231
1256
  const results = config.map(c => c.preview_value)
@@ -1238,13 +1263,23 @@ const loadActionTable = async (config, api_key, endpoint) => {
1238
1263
  return acc;
1239
1264
  }, {});
1240
1265
  };
1266
+ /** @internal */
1267
+ async function setupActionTable(localVariablesQuery, apiKey) {
1268
+ const result = await loadActionTable(localVariablesQuery, apiKey);
1269
+ let success = false;
1270
+ if (Object.keys(result).length === localVariablesQuery.length) {
1271
+ setVariables(result);
1272
+ success = true;
1273
+ }
1274
+ return {
1275
+ success,
1276
+ };
1277
+ }
1241
1278
 
1242
1279
  /**
1243
- * Edgeが起動するアクションに関連するコードを管理する
1244
- *
1245
- * アクションのCreate, Destroyの状態はここで管理する。
1280
+ * モーダル(ポップアップ)に関連するコードの管理
1246
1281
  *
1247
- * FIXME: モーダルなどの機能ごとのコードを分離したい
1282
+ * アクションのShow, Close, ChangeStateの状態はここで管理する。
1248
1283
  */
1249
1284
  /** @internal */
1250
1285
  const handleState = (event) => {
@@ -1253,36 +1288,183 @@ const handleState = (event) => {
1253
1288
  setState$1(event.detail.to, { disableInPreview: event.detail.disableInPreview });
1254
1289
  }
1255
1290
  };
1256
- /** @internal */
1257
- const initialize = (setting) => {
1258
- const newSetting = setSetting(setting);
1259
- if (newSetting.initialState) {
1260
- setState$1(newSetting.initialState);
1291
+ /**
1292
+ * アクションが表示 (show) された後にフックする関数
1293
+ *
1294
+ * @param fn - 呼び出されるフック関数
1295
+ *
1296
+ * @public
1297
+ */
1298
+ function onShow(fn) {
1299
+ let { onShowHandlers } = getInternalHandlers();
1300
+ if (!onShowHandlers) {
1301
+ onShowHandlers = [];
1261
1302
  }
1262
- setOpened(true);
1263
- setClosed(false); // deprecated
1264
- return () => finalize();
1265
- };
1266
- /** @internal */
1267
- const finalize = () => {
1268
- resetSetting();
1269
- };
1303
+ onShowHandlers.push(fn);
1304
+ setInternalHandlers({ onShowHandlers });
1305
+ }
1270
1306
  /**
1271
- * アクションを作成する
1307
+ * アクションがクローズ (close) される前にフックする関数
1272
1308
  *
1273
- * @param App - Svelte コンポーネントのエントリポイント
1274
- * @param options - {@link ActionOptions | オプション}
1309
+ * @param fn - 呼び出されるフック関数
1275
1310
  *
1276
- * @returns アクションを破棄する関数
1311
+ * @public
1312
+ */
1313
+ function onClose(fn) {
1314
+ let { onCloseHandlers } = getInternalHandlers();
1315
+ if (!onCloseHandlers) {
1316
+ onCloseHandlers = [];
1317
+ }
1318
+ onCloseHandlers.push(fn);
1319
+ setInternalHandlers({ onCloseHandlers });
1320
+ }
1321
+ /**
1322
+ * アクションのステートが変更された (changeState) 後にフックする関数
1323
+ *
1324
+ * @param fn - 呼び出されるフック関数
1277
1325
  *
1278
1326
  * @public
1279
1327
  */
1280
- function create(App, options = {
1328
+ function onChangeState(fn) {
1329
+ let { onChangeStateHandlers } = getInternalHandlers();
1330
+ if (!onChangeStateHandlers) {
1331
+ onChangeStateHandlers = [];
1332
+ }
1333
+ onChangeStateHandlers.push(fn);
1334
+ setInternalHandlers({ onChangeStateHandlers });
1335
+ }
1336
+ /**
1337
+ * アクションを表示する
1338
+ *
1339
+ * @public
1340
+ */
1341
+ function showAction() {
1342
+ const event = new CustomEvent(ACTION_SHOW_EVENT, { detail: { trigger: 'custom' } });
1343
+ window.dispatchEvent(event);
1344
+ }
1345
+ /**
1346
+ * アクションを閉じる
1347
+ *
1348
+ * @param trigger - 閉じた時のトリガー。デフォルト `'none'`
1349
+ *
1350
+ * @public
1351
+ */
1352
+ function closeAction(trigger = 'none') {
1353
+ const event = new CustomEvent(ACTION_CLOSE_EVENT, { detail: { trigger } });
1354
+ window.dispatchEvent(event);
1355
+ }
1356
+ /**
1357
+ * アクションに CSS を適用する
1358
+ *
1359
+ * @param css - 適用する CSS
1360
+ *
1361
+ * @returns 適用された style 要素を返す Promise
1362
+ *
1363
+ * @public
1364
+ */
1365
+ async function applyCss(css) {
1366
+ return new Promise((resolve, reject) => {
1367
+ const style = document.createElement('style');
1368
+ style.textContent = css;
1369
+ const shadowRoot = getActionRoot();
1370
+ if (!shadowRoot)
1371
+ return;
1372
+ shadowRoot.append(style);
1373
+ style.addEventListener('load', () => resolve(style));
1374
+ style.addEventListener('error', () => reject(style));
1375
+ });
1376
+ }
1377
+ async function fixFontFaceIssue(href, cssRules) {
1378
+ const css = new CSSStyleSheet();
1379
+ // @ts-ignore
1380
+ await css.replace(cssRules);
1381
+ const rules = [];
1382
+ const fixedRules = [];
1383
+ Array.from(css.cssRules).forEach(cssRule => {
1384
+ if (cssRule.type !== 5) {
1385
+ rules.push(cssRule.cssText);
1386
+ }
1387
+ // type 5 is @font-face
1388
+ const split = href.split('/');
1389
+ const stylePath = split.slice(0, split.length - 1).join('/');
1390
+ const cssText = cssRule.cssText;
1391
+ const newCssText = cssText.replace(
1392
+ // relative paths
1393
+ /url\s*\(\s*['"]?(?!((\/)|((?:https?:)?\/\/)|(?:data:?:)))([^'")]+)['"]?\s*\)/g, `url("${stylePath}/$4")`);
1394
+ if (cssText === newCssText)
1395
+ return;
1396
+ fixedRules.push(newCssText);
1397
+ });
1398
+ return [rules.join('\n'), fixedRules.join('\n')];
1399
+ }
1400
+ /**
1401
+ * アクションにグローバルなスタイルを読み込む
1402
+ *
1403
+ * @param href - style ファイルのリンク URL
1404
+ *
1405
+ * @public
1406
+ */
1407
+ async function loadStyle(href) {
1408
+ const sr = getActionRoot();
1409
+ if (!sr)
1410
+ return;
1411
+ let cssRules = '';
1412
+ try {
1413
+ const res = await fetch(href);
1414
+ cssRules = await res.text();
1415
+ }
1416
+ catch (e) {
1417
+ // pass
1418
+ }
1419
+ if (!cssRules)
1420
+ return;
1421
+ // Chromeのバグで、Shadow Rootの@font-faceを絶対パスで指定する必要がある
1422
+ // @see https://stackoverflow.com/a/63717709
1423
+ const [rules, fontFaceRules] = await fixFontFaceIssue(href, cssRules);
1424
+ const css = new CSSStyleSheet();
1425
+ // @ts-ignore
1426
+ await css.replace(rules);
1427
+ const fontFaceCss = new CSSStyleSheet();
1428
+ // @ts-ignore
1429
+ await fontFaceCss.replace(fontFaceRules);
1430
+ // @ts-ignore
1431
+ sr.adoptedStyleSheets = [...sr.adoptedStyleSheets, css, fontFaceCss];
1432
+ // Chromeのバグで、ページとShadow Rootの両方に、@font-faceを設定する必要がある
1433
+ // @see https://stackoverflow.com/a/63717709
1434
+ // @ts-ignore
1435
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, css, fontFaceCss];
1436
+ }
1437
+ // @internal
1438
+ function getCssVariables(data) {
1439
+ return Object.entries(data)
1440
+ .filter(([key, value]) => {
1441
+ return ['string', 'number'].includes(typeof value) && key.startsWith('--');
1442
+ })
1443
+ .map(([key, value]) => `${key}:${value}`)
1444
+ .join(';');
1445
+ }
1446
+ /**
1447
+ * アクションのルートの DOM 要素を取得する
1448
+ *
1449
+ * @returns アクションがルートの DOM 要素 を持つ場合は DOM 要素を返します。ない場合は `null` を返します
1450
+ *
1451
+ * @public
1452
+ */
1453
+ function getActionRoot() {
1454
+ const root = document.querySelector(`.${KARTE_ACTION_ROOT}[data-${KARTE_ACTION_RID}='${actionId}']`);
1455
+ if (!root?.shadowRoot) {
1456
+ return null;
1457
+ }
1458
+ return root.shadowRoot;
1459
+ }
1460
+ /** @internal */
1461
+ function createModal(App, options = {
1281
1462
  send: () => { },
1282
1463
  publish: () => { },
1283
1464
  props: {},
1284
1465
  variables: {},
1285
1466
  localVariablesQuery: undefined,
1467
+ karteTemplate: {},
1286
1468
  }) {
1287
1469
  let app = null;
1288
1470
  const data = {
@@ -1295,19 +1477,6 @@ function create(App, options = {
1295
1477
  publish: options.publish,
1296
1478
  data,
1297
1479
  };
1298
- const handleDestroy = () => {
1299
- const { onDestroyHandlers } = getInternalHandlers();
1300
- onDestroyHandlers?.forEach(h => {
1301
- const actionHookLog = { name: 'onDestroy' };
1302
- console.info(`${ACTION_HOOK_LABEL}:${JSON.stringify(actionHookLog)}`);
1303
- h(actionProps);
1304
- });
1305
- // 旧Widget APIの内部で利用するため、実行ログは出力しない
1306
- const { onDestroyHandlers: onDestroyWidgetHandlers } = getWidgetHandlers();
1307
- if (onDestroyWidgetHandlers) {
1308
- onDestroyWidgetHandlers.forEach(h => h(actionProps));
1309
- }
1310
- };
1311
1480
  const close = (trigger = 'none') => {
1312
1481
  if (!app) {
1313
1482
  return NOOP;
@@ -1399,39 +1568,20 @@ function create(App, options = {
1399
1568
  };
1400
1569
  // ここからメインの処理
1401
1570
  initialize({ send: options.send, initialState: data.initial_state });
1402
- setSystem({
1403
- // @ts-ignore
1404
- apiKey: data.api_key || null,
1405
- shortenId: data.shorten_id || null,
1406
- campaignId: data.campaign_id || null,
1407
- });
1408
1571
  // ActionTable APIへの非同期リクエスト
1409
1572
  let actionTablePromise = null;
1410
1573
  if (options.localVariablesQuery && data.api_key) {
1411
- actionTablePromise = (async () => {
1412
- const result = await loadActionTable(options.localVariablesQuery, data.api_key);
1413
- let success = false;
1414
- if (Object.keys(result).length === options.localVariablesQuery.length) {
1415
- setVariables(result);
1416
- success = true;
1417
- }
1418
- return {
1419
- success,
1420
- };
1421
- })();
1574
+ actionTablePromise = setupActionTable(options.localVariablesQuery, data.api_key);
1422
1575
  }
1423
1576
  // NOTE: onCreateより前にListenする必要がある
1424
1577
  window.addEventListener(ACTION_SHOW_EVENT, handleShow);
1425
1578
  window.addEventListener(ACTION_CLOSE_EVENT, handleClose);
1426
1579
  window.addEventListener(ACTION_CHANGE_STATE_EVENT, handleState);
1427
- window.addEventListener(ACTION_DESTROY_EVENT, handleDestroy);
1428
- window.addEventListener('beforeunload', dispatchDestroyEvent, false);
1429
1580
  let showTriggerCleanups = [];
1430
1581
  let closeTriggerCleanups = [];
1431
1582
  {
1432
1583
  autoShow();
1433
1584
  }
1434
- const loggerCleanup = listenConsoleLogger() ;
1435
1585
  // 旧Widget API IFの処理
1436
1586
  const { onCreateHandlers } = getInternalHandlers();
1437
1587
  if (onCreateHandlers) {
@@ -1440,7 +1590,6 @@ function create(App, options = {
1440
1590
  console.debug(`${ACTION_HOOK_LABEL}: onCreate`);
1441
1591
  });
1442
1592
  }
1443
- // カスタムスクリプトの処理
1444
1593
  if (options.onCreate) {
1445
1594
  options.onCreate(actionProps);
1446
1595
  }
@@ -1452,9 +1601,6 @@ function create(App, options = {
1452
1601
  window.removeEventListener(ACTION_SHOW_EVENT, handleShow);
1453
1602
  window.removeEventListener(ACTION_CLOSE_EVENT, handleClose);
1454
1603
  window.removeEventListener(ACTION_CHANGE_STATE_EVENT, handleState);
1455
- window.removeEventListener(ACTION_DESTROY_EVENT, handleDestroy);
1456
- window.removeEventListener('beforeunload', dispatchDestroyEvent, false);
1457
- loggerCleanup();
1458
1604
  };
1459
1605
  return appCleanup;
1460
1606
  }
@@ -1482,53 +1628,13 @@ function ensureActionRoot(useShadow = true) {
1482
1628
  }
1483
1629
  }
1484
1630
  /**
1485
- * Dispatch the event to destroy KARTE action
1486
- *
1487
- * @internal
1488
- */
1489
- function dispatchDestroyEvent() {
1490
- const event = new CustomEvent(ACTION_DESTROY_EVENT);
1491
- window.dispatchEvent(event);
1492
- }
1493
- /**
1494
- * アクションの破棄する
1495
- *
1496
- * @public
1497
- */
1498
- function destroyAction() {
1499
- setDestroyed(true);
1500
- dispatchDestroyEvent();
1501
- }
1502
- /**
1503
- * アクションが作成 (create) される前にフックする関数
1504
- *
1505
- * @param fn - 呼び出されるフック関数
1506
- *
1507
- * @public
1508
- */
1509
- function onCreate(fn) {
1510
- let { onCreateHandlers } = getInternalHandlers();
1511
- if (!onCreateHandlers) {
1512
- onCreateHandlers = [];
1513
- }
1514
- onCreateHandlers.push(fn);
1515
- setInternalHandlers({ onCreateHandlers });
1516
- }
1517
- /**
1518
- * アクションが破棄 (destroy) される前にフックする関数
1631
+ * 非推奨
1519
1632
  *
1520
- * @param fn - 呼び出されるフック関数
1633
+ * @deprecated 非推奨
1521
1634
  *
1522
- * @public
1635
+ * @internal
1523
1636
  */
1524
- function onDestroy(fn) {
1525
- let { onDestroyHandlers } = getInternalHandlers();
1526
- if (!onDestroyHandlers) {
1527
- onDestroyHandlers = [];
1528
- }
1529
- onDestroyHandlers.push(fn);
1530
- setInternalHandlers({ onDestroyHandlers });
1531
- }
1637
+ const show = showAction;
1532
1638
  /**
1533
1639
  * 非推奨
1534
1640
  *
@@ -1536,7 +1642,7 @@ function onDestroy(fn) {
1536
1642
  *
1537
1643
  * @internal
1538
1644
  */
1539
- const showModal = create;
1645
+ const close = closeAction;
1540
1646
  /**
1541
1647
  * 非推奨
1542
1648
  *
@@ -1594,17 +1700,6 @@ function createApp(App, options = {
1594
1700
  },
1595
1701
  };
1596
1702
  }
1597
- /**
1598
- * 非推奨
1599
- *
1600
- * @deprecated 非推奨
1601
- *
1602
- * @internal
1603
- */
1604
- function destroy() {
1605
- setDestroyed(true);
1606
- dispatchDestroyEvent();
1607
- }
1608
1703
  /**
1609
1704
  * 非推奨
1610
1705
  *
@@ -1641,6 +1736,43 @@ function createFog({ color = '#000', opacity = '50%', zIndex = 999, onclick, })
1641
1736
  /**
1642
1737
  * スクリプト接客が利用するコードの管理
1643
1738
  */
1739
+ /** @internal */
1740
+ async function runScript$1(options = {
1741
+ send: () => { },
1742
+ publish: () => { },
1743
+ props: {},
1744
+ variables: {},
1745
+ localVariablesQuery: undefined,
1746
+ karteTemplate: {},
1747
+ }) {
1748
+ if (!options.onCreate)
1749
+ return;
1750
+ const data = {
1751
+ ...options.props,
1752
+ ...options.variables,
1753
+ ...getVariables(),
1754
+ };
1755
+ const actionProps = {
1756
+ send: options.send,
1757
+ publish: options.publish,
1758
+ data,
1759
+ };
1760
+ initialize({ send: options.send, initialState: data.initial_state });
1761
+ const { success } = await setupActionTable(options.localVariablesQuery, data.api_key);
1762
+ if (!success)
1763
+ return;
1764
+ options.send('script_fired');
1765
+ // 旧Widget API IFの処理
1766
+ const { onCreateHandlers } = getInternalHandlers();
1767
+ if (onCreateHandlers) {
1768
+ onCreateHandlers.forEach(h => {
1769
+ h({ send: options.send, publish: options.publish, data });
1770
+ console.debug(`${ACTION_HOOK_LABEL}: onCreate`);
1771
+ });
1772
+ }
1773
+ options.onCreate(actionProps);
1774
+ finalize();
1775
+ }
1644
1776
  /**
1645
1777
  * ES Modules に対応していない JavaScript をページに読み込む
1646
1778
  *
@@ -1701,178 +1833,112 @@ async function loadGlobalStyle(href) {
1701
1833
  }
1702
1834
 
1703
1835
  /**
1704
- * モーダル(ポップアップ)に関連するコードの管理
1836
+ * Edgeが起動するアクションに関連するコードを管理する
1705
1837
  *
1706
- * アクションのShow, Close, ChangeStateの状態はここで管理する。
1838
+ * アクションのCreate, Destroyの状態はここで管理する。
1707
1839
  */
1708
1840
  /**
1709
- * アクションが表示 (show) された後にフックする関数
1710
- *
1711
- * @param fn - 呼び出されるフック関数
1841
+ * アクションを作成する
1712
1842
  *
1713
- * @public
1714
- */
1715
- function onShow(fn) {
1716
- let { onShowHandlers } = getInternalHandlers();
1717
- if (!onShowHandlers) {
1718
- onShowHandlers = [];
1719
- }
1720
- onShowHandlers.push(fn);
1721
- setInternalHandlers({ onShowHandlers });
1722
- }
1723
- /**
1724
- * アクションがクローズ (close) される前にフックする関数
1843
+ * @param App - Svelte コンポーネントのエントリポイント
1844
+ * @param options - {@link ActionOptions | オプション}
1725
1845
  *
1726
- * @param fn - 呼び出されるフック関数
1846
+ * @returns アクションを破棄する関数
1727
1847
  *
1728
1848
  * @public
1729
1849
  */
1730
- function onClose(fn) {
1731
- let { onCloseHandlers } = getInternalHandlers();
1732
- if (!onCloseHandlers) {
1733
- onCloseHandlers = [];
1850
+ function create(App, options = {
1851
+ send: () => { },
1852
+ publish: () => { },
1853
+ props: {},
1854
+ variables: {},
1855
+ localVariablesQuery: undefined,
1856
+ karteTemplate: {},
1857
+ }) {
1858
+ const data = {
1859
+ ...options.props,
1860
+ ...options.variables,
1861
+ ...getVariables(),
1862
+ };
1863
+ const actionProps = {
1864
+ send: options.send,
1865
+ publish: options.publish,
1866
+ data,
1867
+ };
1868
+ const handleDestroy = () => {
1869
+ const { onDestroyHandlers } = getInternalHandlers();
1870
+ onDestroyHandlers?.forEach(h => {
1871
+ const actionHookLog = { name: 'onDestroy' };
1872
+ console.info(`${ACTION_HOOK_LABEL}:${JSON.stringify(actionHookLog)}`);
1873
+ h(actionProps);
1874
+ });
1875
+ // 旧Widget APIの内部で利用するため、実行ログは出力しない
1876
+ const { onDestroyHandlers: onDestroyWidgetHandlers } = getWidgetHandlers();
1877
+ if (onDestroyWidgetHandlers) {
1878
+ onDestroyWidgetHandlers.forEach(h => h(actionProps));
1879
+ }
1880
+ };
1881
+ // ここからメインの処理
1882
+ setSystem({
1883
+ // @ts-ignore
1884
+ apiKey: data.api_key || null,
1885
+ shortenId: data.shorten_id || null,
1886
+ campaignId: data.campaign_id || null,
1887
+ });
1888
+ const loggerCleanup = listenConsoleLogger() ;
1889
+ window.addEventListener(ACTION_DESTROY_EVENT, handleDestroy);
1890
+ window.addEventListener('beforeunload', dispatchDestroyEvent, false);
1891
+ let modalCleanup = NOOP;
1892
+ if (options.karteTemplate?.template_type === 'script') {
1893
+ runScript$1(options);
1734
1894
  }
1735
- onCloseHandlers.push(fn);
1736
- setInternalHandlers({ onCloseHandlers });
1737
- }
1738
- /**
1739
- * アクションのステートが変更された (changeState) 後にフックする関数
1740
- *
1741
- * @param fn - 呼び出されるフック関数
1742
- *
1743
- * @public
1744
- */
1745
- function onChangeState(fn) {
1746
- let { onChangeStateHandlers } = getInternalHandlers();
1747
- if (!onChangeStateHandlers) {
1748
- onChangeStateHandlers = [];
1895
+ else {
1896
+ modalCleanup = createModal(App, options);
1749
1897
  }
1750
- onChangeStateHandlers.push(fn);
1751
- setInternalHandlers({ onChangeStateHandlers });
1752
- }
1753
- /**
1754
- * アクションを表示する
1755
- *
1756
- * @public
1757
- */
1758
- function showAction() {
1759
- const event = new CustomEvent(ACTION_SHOW_EVENT, { detail: { trigger: 'custom' } });
1760
- window.dispatchEvent(event);
1761
- }
1762
- /**
1763
- * アクションを閉じる
1764
- *
1765
- * @param trigger - 閉じた時のトリガー。デフォルト `'none'`
1766
- *
1767
- * @public
1768
- */
1769
- function closeAction(trigger = 'none') {
1770
- const event = new CustomEvent(ACTION_CLOSE_EVENT, { detail: { trigger } });
1771
- window.dispatchEvent(event);
1898
+ return () => {
1899
+ loggerCleanup();
1900
+ modalCleanup();
1901
+ window.removeEventListener(ACTION_DESTROY_EVENT, handleDestroy);
1902
+ };
1772
1903
  }
1773
1904
  /**
1774
- * アクションに CSS を適用する
1775
- *
1776
- * @param css - 適用する CSS
1777
- *
1778
- * @returns 適用された style 要素を返す Promise
1905
+ * アクションの破棄する
1779
1906
  *
1780
1907
  * @public
1781
1908
  */
1782
- async function applyCss(css) {
1783
- return new Promise((resolve, reject) => {
1784
- const style = document.createElement('style');
1785
- style.textContent = css;
1786
- const shadowRoot = getActionRoot();
1787
- if (!shadowRoot)
1788
- return;
1789
- shadowRoot.append(style);
1790
- style.addEventListener('load', () => resolve(style));
1791
- style.addEventListener('error', () => reject(style));
1792
- });
1793
- }
1794
- async function fixFontFaceIssue(href, cssRules) {
1795
- const css = new CSSStyleSheet();
1796
- // @ts-ignore
1797
- await css.replace(cssRules);
1798
- const rules = [];
1799
- const fixedRules = [];
1800
- Array.from(css.cssRules).forEach(cssRule => {
1801
- if (cssRule.type !== 5) {
1802
- rules.push(cssRule.cssText);
1803
- }
1804
- // type 5 is @font-face
1805
- const split = href.split('/');
1806
- const stylePath = split.slice(0, split.length - 1).join('/');
1807
- const cssText = cssRule.cssText;
1808
- const newCssText = cssText.replace(
1809
- // relative paths
1810
- /url\s*\(\s*['"]?(?!((\/)|((?:https?:)?\/\/)|(?:data:?:)))([^'")]+)['"]?\s*\)/g, `url("${stylePath}/$4")`);
1811
- if (cssText === newCssText)
1812
- return;
1813
- fixedRules.push(newCssText);
1814
- });
1815
- return [rules.join('\n'), fixedRules.join('\n')];
1909
+ function destroyAction() {
1910
+ setDestroyed(true);
1911
+ dispatchDestroyEvent();
1816
1912
  }
1817
1913
  /**
1818
- * アクションにグローバルなスタイルを読み込む
1914
+ * アクションが作成 (create) される前にフックする関数
1819
1915
  *
1820
- * @param href - style ファイルのリンク URL
1916
+ * @param fn - 呼び出されるフック関数
1821
1917
  *
1822
1918
  * @public
1823
1919
  */
1824
- async function loadStyle(href) {
1825
- const sr = getActionRoot();
1826
- if (!sr)
1827
- return;
1828
- let cssRules = '';
1829
- try {
1830
- const res = await fetch(href);
1831
- cssRules = await res.text();
1832
- }
1833
- catch (e) {
1834
- // pass
1920
+ function onCreate(fn) {
1921
+ let { onCreateHandlers } = getInternalHandlers();
1922
+ if (!onCreateHandlers) {
1923
+ onCreateHandlers = [];
1835
1924
  }
1836
- if (!cssRules)
1837
- return;
1838
- // Chromeのバグで、Shadow Rootの@font-faceを絶対パスで指定する必要がある
1839
- // @see https://stackoverflow.com/a/63717709
1840
- const [rules, fontFaceRules] = await fixFontFaceIssue(href, cssRules);
1841
- const css = new CSSStyleSheet();
1842
- // @ts-ignore
1843
- await css.replace(rules);
1844
- const fontFaceCss = new CSSStyleSheet();
1845
- // @ts-ignore
1846
- await fontFaceCss.replace(fontFaceRules);
1847
- // @ts-ignore
1848
- sr.adoptedStyleSheets = [...sr.adoptedStyleSheets, css, fontFaceCss];
1849
- // Chromeのバグで、ページとShadow Rootの両方に、@font-faceを設定する必要がある
1850
- // @see https://stackoverflow.com/a/63717709
1851
- // @ts-ignore
1852
- document.adoptedStyleSheets = [...document.adoptedStyleSheets, css, fontFaceCss];
1853
- }
1854
- // @internal
1855
- function getCssVariables(data) {
1856
- return Object.entries(data)
1857
- .filter(([key, value]) => {
1858
- return ['string', 'number'].includes(typeof value) && key.startsWith('--');
1859
- })
1860
- .map(([key, value]) => `${key}:${value}`)
1861
- .join(';');
1925
+ onCreateHandlers.push(fn);
1926
+ setInternalHandlers({ onCreateHandlers });
1862
1927
  }
1863
1928
  /**
1864
- * アクションのルートの DOM 要素を取得する
1929
+ * アクションが破棄 (destroy) される前にフックする関数
1865
1930
  *
1866
- * @returns アクションがルートの DOM 要素 を持つ場合は DOM 要素を返します。ない場合は `null` を返します
1931
+ * @param fn - 呼び出されるフック関数
1867
1932
  *
1868
1933
  * @public
1869
1934
  */
1870
- function getActionRoot() {
1871
- const root = document.querySelector(`.${KARTE_ACTION_ROOT}[data-${KARTE_ACTION_RID}='${actionId}']`);
1872
- if (!root?.shadowRoot) {
1873
- return null;
1935
+ function onDestroy(fn) {
1936
+ let { onDestroyHandlers } = getInternalHandlers();
1937
+ if (!onDestroyHandlers) {
1938
+ onDestroyHandlers = [];
1874
1939
  }
1875
- return root.shadowRoot;
1940
+ onDestroyHandlers.push(fn);
1941
+ setInternalHandlers({ onDestroyHandlers });
1876
1942
  }
1877
1943
  // -------- The following codes are deprecated --------
1878
1944
  /**
@@ -1882,7 +1948,7 @@ function getActionRoot() {
1882
1948
  *
1883
1949
  * @internal
1884
1950
  */
1885
- const show = showAction;
1951
+ const showModal = create;
1886
1952
  /**
1887
1953
  * 非推奨
1888
1954
  *
@@ -1890,7 +1956,10 @@ const show = showAction;
1890
1956
  *
1891
1957
  * @internal
1892
1958
  */
1893
- const close = closeAction;
1959
+ function destroy() {
1960
+ setDestroyed(true);
1961
+ dispatchDestroyEvent();
1962
+ }
1894
1963
 
1895
1964
  /**
1896
1965
  * エディタv1のWidget API 互換のインターフェース
@@ -2729,13 +2798,13 @@ function customAnimation(node, { transform, animationStyle, delay = 0, duration
2729
2798
  }
2730
2799
  }
2731
2800
 
2732
- /* src/components/BackgroundOverray.svelte generated by Svelte v3.53.1 */
2801
+ /* src/components/BackgroundOverlay.svelte generated by Svelte v3.53.1 */
2733
2802
 
2734
2803
  function add_css$s(target) {
2735
2804
  append_styles(target, "svelte-1d4fta", ".background.svelte-1d4fta{position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0, 0, 0, 0.3);z-index:2147483646}");
2736
2805
  }
2737
2806
 
2738
- // (9:0) {#if backgroundOverray}
2807
+ // (9:0) {#if backgroundOverlay}
2739
2808
  function create_if_block$7(ctx) {
2740
2809
  let div;
2741
2810
  let mounted;
@@ -2773,7 +2842,7 @@ function create_if_block$7(ctx) {
2773
2842
 
2774
2843
  function create_fragment$v(ctx) {
2775
2844
  let if_block_anchor;
2776
- let if_block = /*backgroundOverray*/ ctx[0] && create_if_block$7(ctx);
2845
+ let if_block = /*backgroundOverlay*/ ctx[0] && create_if_block$7(ctx);
2777
2846
 
2778
2847
  return {
2779
2848
  c() {
@@ -2789,7 +2858,7 @@ function create_fragment$v(ctx) {
2789
2858
  insert_hydration(target, if_block_anchor, anchor);
2790
2859
  },
2791
2860
  p(ctx, [dirty]) {
2792
- if (/*backgroundOverray*/ ctx[0]) {
2861
+ if (/*backgroundOverlay*/ ctx[0]) {
2793
2862
  if (if_block) {
2794
2863
  if_block.p(ctx, dirty);
2795
2864
  } else {
@@ -2812,21 +2881,21 @@ function create_fragment$v(ctx) {
2812
2881
  }
2813
2882
 
2814
2883
  function instance$v($$self, $$props, $$invalidate) {
2815
- let { backgroundOverray = false } = $$props;
2884
+ let { backgroundOverlay = false } = $$props;
2816
2885
  const dispatch = createEventDispatcher();
2817
2886
  const click_handler = () => dispatch('click');
2818
2887
 
2819
2888
  $$self.$$set = $$props => {
2820
- if ('backgroundOverray' in $$props) $$invalidate(0, backgroundOverray = $$props.backgroundOverray);
2889
+ if ('backgroundOverlay' in $$props) $$invalidate(0, backgroundOverlay = $$props.backgroundOverlay);
2821
2890
  };
2822
2891
 
2823
- return [backgroundOverray, dispatch, click_handler];
2892
+ return [backgroundOverlay, dispatch, click_handler];
2824
2893
  }
2825
2894
 
2826
- class BackgroundOverray extends SvelteComponent {
2895
+ class BackgroundOverlay extends SvelteComponent {
2827
2896
  constructor(options) {
2828
2897
  super();
2829
- init(this, options, instance$v, create_fragment$v, safe_not_equal, { backgroundOverray: 0 }, add_css$s);
2898
+ init(this, options, instance$v, create_fragment$v, safe_not_equal, { backgroundOverlay: 0 }, add_css$s);
2830
2899
  }
2831
2900
  }
2832
2901
 
@@ -3651,20 +3720,20 @@ function create_default_slot$6(ctx) {
3651
3720
  }
3652
3721
 
3653
3722
  function create_fragment$t(ctx) {
3654
- let backgroundoverray;
3723
+ let backgroundoverlay;
3655
3724
  let t;
3656
3725
  let if_block_anchor;
3657
3726
  let current;
3658
3727
  let mounted;
3659
3728
  let dispose;
3660
3729
 
3661
- backgroundoverray = new BackgroundOverray({
3730
+ backgroundoverlay = new BackgroundOverlay({
3662
3731
  props: {
3663
- backgroundOverray: /*backgroundOverray*/ ctx[10]
3732
+ backgroundOverlay: /*backgroundOverlay*/ ctx[10]
3664
3733
  }
3665
3734
  });
3666
3735
 
3667
- backgroundoverray.$on("click", function () {
3736
+ backgroundoverlay.$on("click", function () {
3668
3737
  if (is_function(/*backgroundClick*/ ctx[18])) /*backgroundClick*/ ctx[18].apply(this, arguments);
3669
3738
  });
3670
3739
 
@@ -3672,19 +3741,19 @@ function create_fragment$t(ctx) {
3672
3741
 
3673
3742
  return {
3674
3743
  c() {
3675
- create_component(backgroundoverray.$$.fragment);
3744
+ create_component(backgroundoverlay.$$.fragment);
3676
3745
  t = space();
3677
3746
  if (if_block) if_block.c();
3678
3747
  if_block_anchor = empty();
3679
3748
  },
3680
3749
  l(nodes) {
3681
- claim_component(backgroundoverray.$$.fragment, nodes);
3750
+ claim_component(backgroundoverlay.$$.fragment, nodes);
3682
3751
  t = claim_space(nodes);
3683
3752
  if (if_block) if_block.l(nodes);
3684
3753
  if_block_anchor = empty();
3685
3754
  },
3686
3755
  m(target, anchor) {
3687
- mount_component(backgroundoverray, target, anchor);
3756
+ mount_component(backgroundoverlay, target, anchor);
3688
3757
  insert_hydration(target, t, anchor);
3689
3758
  if (if_block) if_block.m(target, anchor);
3690
3759
  insert_hydration(target, if_block_anchor, anchor);
@@ -3700,9 +3769,9 @@ function create_fragment$t(ctx) {
3700
3769
  },
3701
3770
  p(new_ctx, [dirty]) {
3702
3771
  ctx = new_ctx;
3703
- const backgroundoverray_changes = {};
3704
- if (dirty & /*backgroundOverray*/ 1024) backgroundoverray_changes.backgroundOverray = /*backgroundOverray*/ ctx[10];
3705
- backgroundoverray.$set(backgroundoverray_changes);
3772
+ const backgroundoverlay_changes = {};
3773
+ if (dirty & /*backgroundOverlay*/ 1024) backgroundoverlay_changes.backgroundOverlay = /*backgroundOverlay*/ ctx[10];
3774
+ backgroundoverlay.$set(backgroundoverlay_changes);
3706
3775
 
3707
3776
  if (/*visible*/ ctx[12]) {
3708
3777
  if (if_block) {
@@ -3729,17 +3798,17 @@ function create_fragment$t(ctx) {
3729
3798
  },
3730
3799
  i(local) {
3731
3800
  if (current) return;
3732
- transition_in(backgroundoverray.$$.fragment, local);
3801
+ transition_in(backgroundoverlay.$$.fragment, local);
3733
3802
  transition_in(if_block);
3734
3803
  current = true;
3735
3804
  },
3736
3805
  o(local) {
3737
- transition_out(backgroundoverray.$$.fragment, local);
3806
+ transition_out(backgroundoverlay.$$.fragment, local);
3738
3807
  transition_out(if_block);
3739
3808
  current = false;
3740
3809
  },
3741
3810
  d(detaching) {
3742
- destroy_component(backgroundoverray, detaching);
3811
+ destroy_component(backgroundoverlay, detaching);
3743
3812
  if (detaching) detach(t);
3744
3813
  if (if_block) if_block.d(detaching);
3745
3814
  if (detaching) detach(if_block_anchor);
@@ -3776,7 +3845,7 @@ function instance$t($$self, $$props, $$invalidate) {
3776
3845
  let { closeEventValue = null } = $$props;
3777
3846
  let { closeButtonColor = '#000000' } = $$props;
3778
3847
  let { _closeStyle = '' } = $$props;
3779
- let backgroundOverray = DefaultModalPlacement.backgroundOverlay;
3848
+ let backgroundOverlay = DefaultModalPlacement.backgroundOverlay;
3780
3849
  let backgroundClickFunction = DefaultModalPlacement.backgroundClick;
3781
3850
  let modal;
3782
3851
 
@@ -3827,7 +3896,7 @@ function instance$t($$self, $$props, $$invalidate) {
3827
3896
  if ($$self.$$.dirty & /*placement*/ 2097152) {
3828
3897
  {
3829
3898
  {
3830
- $$invalidate(10, backgroundOverray = false);
3899
+ $$invalidate(10, backgroundOverlay = false);
3831
3900
  }
3832
3901
  }
3833
3902
  }
@@ -3902,7 +3971,7 @@ function instance$t($$self, $$props, $$invalidate) {
3902
3971
  closeEventValue,
3903
3972
  closeButtonColor,
3904
3973
  _closeStyle,
3905
- backgroundOverray,
3974
+ backgroundOverlay,
3906
3975
  modal,
3907
3976
  visible,
3908
3977
  handle_keydown,