@plaidev/karte-action-sdk 1.1.267-29082699.18b12594 → 1.1.268-29083022.42c3d847

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.
@@ -1352,6 +1352,58 @@ function showOnTime(props, show = NOOP) {
1352
1352
  ? onTime(props.show_on_time_count * 1000, () => show())
1353
1353
  : null;
1354
1354
  }
1355
+ /** @internal */
1356
+ function and(fn, ...conditionFns) {
1357
+ return checkAndDo((conditions, idx) => {
1358
+ conditions[idx] = true;
1359
+ return conditions.every(t => t == null || t);
1360
+ }, fn, ...conditionFns);
1361
+ }
1362
+ /** @internal */
1363
+ function or(fn, ...conditionFns) {
1364
+ return checkAndDo((conditions, idx) => {
1365
+ if (conditions.some(t => t)) {
1366
+ return conditions[idx]; // this is true when show_on_scroll_reenter is true
1367
+ }
1368
+ conditions[idx] = true;
1369
+ return true;
1370
+ }, fn, ...conditionFns);
1371
+ }
1372
+ function checkAndDo(checkFn, fn, ...conditionFns) {
1373
+ let initialized = false;
1374
+ const checkBeforeInitialized = [];
1375
+ let haveCondition = true;
1376
+ const conditions = Array(conditionFns.length).fill(null);
1377
+ const checkAndDos = Array(conditionFns.length).fill(() => { });
1378
+ const cleanups = Array(conditionFns.length).fill(null);
1379
+ const generageCheckAndDo = (idx) => () => {
1380
+ if (!initialized) {
1381
+ checkBeforeInitialized.push(idx);
1382
+ return;
1383
+ }
1384
+ if (!haveCondition || !checkFn(conditions, idx))
1385
+ return;
1386
+ fn?.();
1387
+ };
1388
+ conditionFns.forEach((conditionFn, i) => {
1389
+ const checkAndDo = generageCheckAndDo(i);
1390
+ checkAndDos[i] = checkAndDo;
1391
+ const cleanup = conditionFn(checkAndDo);
1392
+ cleanups[i] = cleanup;
1393
+ if (cleanup != null)
1394
+ conditions[i] = false;
1395
+ });
1396
+ haveCondition = conditions.some(c => c !== null);
1397
+ const cleanupAll = () => {
1398
+ cleanups.forEach((cleanup, i) => {
1399
+ cleanup?.();
1400
+ cleanups[i] = null;
1401
+ });
1402
+ };
1403
+ initialized = true;
1404
+ checkBeforeInitialized.forEach(i => checkAndDos[i]());
1405
+ return haveCondition ? cleanupAll : null;
1406
+ }
1355
1407
 
1356
1408
  /**
1357
1409
  * アクションテーブルに関連するコードの管理
@@ -1615,6 +1667,32 @@ function initActionTable(localVariablesQuery) {
1615
1667
  setVariables(initValues);
1616
1668
  }
1617
1669
 
1670
+ /**
1671
+ * モーダル(ポップアップ)に関連するコードの管理
1672
+ *
1673
+ * アクションのShow, Close, ChangeStateの状態はここで管理する。
1674
+ */
1675
+ /**
1676
+ * アクションを表示する
1677
+ *
1678
+ * @public
1679
+ */
1680
+ function showAction$1() {
1681
+ const event = new CustomEvent(ACTION_SHOW_EVENT, { detail: { trigger: 'custom' } });
1682
+ window.dispatchEvent(event);
1683
+ }
1684
+ /**
1685
+ * アクションを閉じる
1686
+ *
1687
+ * @param trigger - 閉じた時のトリガー。デフォルト `'none'`
1688
+ *
1689
+ * @public
1690
+ */
1691
+ function closeAction$1(trigger = 'none') {
1692
+ const event = new CustomEvent(ACTION_CLOSE_EVENT, { detail: { trigger } });
1693
+ window.dispatchEvent(event);
1694
+ }
1695
+
1618
1696
  /**
1619
1697
  * モーダル(ポップアップ)に関連するコードの管理
1620
1698
  *
@@ -1632,12 +1710,57 @@ const handleState = (event) => {
1632
1710
  });
1633
1711
  }
1634
1712
  };
1713
+ /**
1714
+ * アクションが表示 (show) された後にフックする関数
1715
+ *
1716
+ * @param fn - 呼び出されるフック関数
1717
+ *
1718
+ * @public
1719
+ */
1720
+ function onShow(fn) {
1721
+ let { onShowHandlers } = getInternalHandlers();
1722
+ if (!onShowHandlers) {
1723
+ onShowHandlers = [];
1724
+ }
1725
+ onShowHandlers.push(fn);
1726
+ setInternalHandlers({ onShowHandlers });
1727
+ }
1728
+ /**
1729
+ * アクションがクローズ (close) される前にフックする関数
1730
+ *
1731
+ * @param fn - 呼び出されるフック関数
1732
+ *
1733
+ * @public
1734
+ */
1735
+ function onClose(fn) {
1736
+ let { onCloseHandlers } = getInternalHandlers();
1737
+ if (!onCloseHandlers) {
1738
+ onCloseHandlers = [];
1739
+ }
1740
+ onCloseHandlers.push(fn);
1741
+ setInternalHandlers({ onCloseHandlers });
1742
+ }
1743
+ /**
1744
+ * アクションのステートが変更された (changeState) 後にフックする関数
1745
+ *
1746
+ * @param fn - 呼び出されるフック関数
1747
+ *
1748
+ * @public
1749
+ */
1750
+ function onChangeState(fn) {
1751
+ let { onChangeStateHandlers } = getInternalHandlers();
1752
+ if (!onChangeStateHandlers) {
1753
+ onChangeStateHandlers = [];
1754
+ }
1755
+ onChangeStateHandlers.push(fn);
1756
+ setInternalHandlers({ onChangeStateHandlers });
1757
+ }
1635
1758
  /**
1636
1759
  * アクションを表示する
1637
1760
  *
1638
1761
  * @public
1639
1762
  */
1640
- function showAction$1() {
1763
+ function showAction() {
1641
1764
  const event = new CustomEvent(ACTION_SHOW_EVENT, { detail: { trigger: 'custom' } });
1642
1765
  window.dispatchEvent(event);
1643
1766
  }
@@ -1648,10 +1771,114 @@ function showAction$1() {
1648
1771
  *
1649
1772
  * @public
1650
1773
  */
1651
- function closeAction$1(trigger = 'none') {
1774
+ function closeAction(trigger = 'none') {
1652
1775
  const event = new CustomEvent(ACTION_CLOSE_EVENT, { detail: { trigger } });
1653
1776
  window.dispatchEvent(event);
1654
1777
  }
1778
+ /**
1779
+ * アクションに CSS を適用する
1780
+ *
1781
+ * @param css - 適用する CSS
1782
+ *
1783
+ * @returns 適用された style 要素を返す Promise
1784
+ *
1785
+ * @public
1786
+ */
1787
+ async function applyCss(css) {
1788
+ return new Promise((resolve, reject) => {
1789
+ const style = document.createElement('style');
1790
+ style.textContent = css;
1791
+ const shadowRoot = getActionRoot();
1792
+ if (!shadowRoot)
1793
+ return;
1794
+ shadowRoot.append(style);
1795
+ style.addEventListener('load', () => resolve(style));
1796
+ style.addEventListener('error', () => reject(style));
1797
+ });
1798
+ }
1799
+ async function fixFontFaceIssue(href, cssRules) {
1800
+ const css = new CSSStyleSheet();
1801
+ // @ts-ignore
1802
+ await css.replace(cssRules);
1803
+ const rules = [];
1804
+ const fixedRules = [];
1805
+ Array.from(css.cssRules).forEach(cssRule => {
1806
+ if (cssRule.type !== 5) {
1807
+ rules.push(cssRule.cssText);
1808
+ }
1809
+ // type 5 is @font-face
1810
+ const split = href.split('/');
1811
+ const stylePath = split.slice(0, split.length - 1).join('/');
1812
+ const cssText = cssRule.cssText;
1813
+ const newCssText = cssText.replace(
1814
+ // relative paths
1815
+ /url\s*\(\s*['"]?(?!((\/)|((?:https?:)?\/\/)|(?:data:?:)))([^'")]+)['"]?\s*\)/g, `url("${stylePath}/$4")`);
1816
+ if (cssText === newCssText)
1817
+ return;
1818
+ fixedRules.push(newCssText);
1819
+ });
1820
+ return [rules.join('\n'), fixedRules.join('\n')];
1821
+ }
1822
+ /**
1823
+ * アクションにグローバルなスタイルを読み込む
1824
+ *
1825
+ * @param href - style ファイルのリンク URL
1826
+ *
1827
+ * @public
1828
+ */
1829
+ async function loadStyle(href) {
1830
+ const sr = getActionRoot();
1831
+ if (!sr)
1832
+ return;
1833
+ let cssRules = '';
1834
+ try {
1835
+ const res = await fetch(href);
1836
+ cssRules = await res.text();
1837
+ }
1838
+ catch (_) {
1839
+ // pass
1840
+ }
1841
+ if (!cssRules)
1842
+ return;
1843
+ // Chromeのバグで、Shadow Rootの@font-faceを絶対パスで指定する必要がある
1844
+ // @see https://stackoverflow.com/a/63717709
1845
+ const [rules, fontFaceRules] = await fixFontFaceIssue(href, cssRules);
1846
+ const css = new CSSStyleSheet();
1847
+ // @ts-ignore
1848
+ await css.replace(rules);
1849
+ const fontFaceCss = new CSSStyleSheet();
1850
+ // @ts-ignore
1851
+ await fontFaceCss.replace(fontFaceRules);
1852
+ // @ts-ignore
1853
+ sr.adoptedStyleSheets = [...sr.adoptedStyleSheets, css, fontFaceCss];
1854
+ // Chromeのバグで、ページとShadow Rootの両方に、@font-faceを設定する必要がある
1855
+ // @see https://stackoverflow.com/a/63717709
1856
+ // @ts-ignore
1857
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, css, fontFaceCss];
1858
+ }
1859
+ // @internal
1860
+ function getCssVariables(data) {
1861
+ return Object.entries(data)
1862
+ .filter(([key, value]) => {
1863
+ return ['string', 'number'].includes(typeof value) && key.startsWith('--');
1864
+ })
1865
+ .map(([key, value]) => `${key}:${value}`)
1866
+ .join(';');
1867
+ }
1868
+ /**
1869
+ * アクションのルートの DOM 要素を取得する
1870
+ *
1871
+ * @returns アクションがルートの DOM 要素 を持つ場合は DOM 要素を返します。ない場合は `null` を返します
1872
+ *
1873
+ * @public
1874
+ */
1875
+ function getActionRoot() {
1876
+ const root = document.querySelector(`.${KARTE_ACTION_ROOT}[data-${KARTE_ACTION_RID}='${actionId}']`);
1877
+ if (!root?.shadowRoot) {
1878
+ return null;
1879
+ }
1880
+ return root.shadowRoot;
1881
+ }
1655
1882
  /** @internal */
1656
1883
  function createModal(App, options = {
1657
1884
  send: () => { },
@@ -1719,7 +1946,7 @@ function createModal(App, options = {
1719
1946
  if (app) {
1720
1947
  return;
1721
1948
  }
1722
- if (trigger === 'custom') {
1949
+ if (!isOnSite() && trigger === 'custom') {
1723
1950
  return;
1724
1951
  }
1725
1952
  if (trigger === 'custom' && (options.props.show_on_scroll || options.props.show_on_time)) {
@@ -1746,9 +1973,9 @@ function createModal(App, options = {
1746
1973
  if (app) {
1747
1974
  return;
1748
1975
  }
1976
+ const target = ensureActionRoot(isOnSite());
1749
1977
  const props = {
1750
- target: ensureActionRoot$1(!true),
1751
- hydrate: true,
1978
+ target,
1752
1979
  props: {
1753
1980
  send: options.send,
1754
1981
  publish: options.publish,
@@ -1783,17 +2010,43 @@ function createModal(App, options = {
1783
2010
  },
1784
2011
  },
1785
2012
  };
1786
- app = svelte.hydrate(App, props)
1787
-
2013
+ app = // @ts-ignore -- Svelte3 では `mount` は存在しない
2014
+ svelte.mount(App, props)
1788
2015
  ;
1789
2016
  };
1790
2017
  const handleShow = (event) => {
1791
2018
  const trigger = event?.detail?.trigger ? event.detail.trigger : 'none';
1792
2019
  show(trigger);
1793
2020
  };
2021
+ const currying = (conditionFn, options) => (fn) => conditionFn(options.props, fn);
1794
2022
  const autoShow = () => {
1795
2023
  return show('auto');
1796
2024
  };
2025
+ const autoClose = () => {
2026
+ return close('auto');
2027
+ };
2028
+ const listenCloseTrigger = () => {
2029
+ const cleanups = [];
2030
+ const curried = fn => hideOnScroll(options.props, fn, autoShow);
2031
+ if (options.props.hide_and_condition) {
2032
+ cleanups.push(and(autoClose, curried, currying(hideOnTime, options)) || NOOP);
2033
+ }
2034
+ else {
2035
+ cleanups.push(or(autoClose, curried, currying(hideOnTime, options)) || NOOP);
2036
+ }
2037
+ return cleanups;
2038
+ };
2039
+ const listenShowTrigger = () => {
2040
+ const cleanups = [];
2041
+ const curried = fn => showOnScroll(options.props, fn, autoClose);
2042
+ if (options.props.show_and_condition) {
2043
+ cleanups.push(and(autoShow, curried, currying(showOnTime, options)) || NOOP);
2044
+ }
2045
+ else {
2046
+ cleanups.push(or(autoShow, curried, currying(showOnTime, options)) || NOOP);
2047
+ }
2048
+ return cleanups;
2049
+ };
1797
2050
  // ここからメインの処理
1798
2051
  initialize({ send: options.send, initialState: data.initial_state });
1799
2052
  // ActionTable APIへの非同期リクエスト
@@ -1808,9 +2061,13 @@ function createModal(App, options = {
1808
2061
  window.addEventListener(ACTION_CHANGE_STATE_EVENT, handleState);
1809
2062
  let showTriggerCleanups = [];
1810
2063
  let closeTriggerCleanups = [];
1811
- {
2064
+ if (!isOnSite()) {
1812
2065
  autoShow();
1813
2066
  }
2067
+ else {
2068
+ showTriggerCleanups = listenShowTrigger();
2069
+ closeTriggerCleanups = listenCloseTrigger();
2070
+ }
1814
2071
  // 旧Widget API IFの処理
1815
2072
  const { onCreateHandlers } = getInternalHandlers();
1816
2073
  if (onCreateHandlers) {
@@ -1839,7 +2096,7 @@ function createModal(App, options = {
1839
2096
  return appCleanup;
1840
2097
  }
1841
2098
  /** @internal */
1842
- function ensureActionRoot$1(useShadow = true) {
2099
+ function ensureActionRoot(useShadow = true) {
1843
2100
  const systemConfig = getSystem();
1844
2101
  const rootAttrs = {
1845
2102
  class: `${KARTE_ACTION_ROOT} ${KARTE_MODAL_ROOT}`,
@@ -1862,34 +2119,143 @@ function ensureActionRoot$1(useShadow = true) {
1862
2119
  return el;
1863
2120
  }
1864
2121
  }
1865
-
1866
2122
  /**
1867
- * スクリプト接客が利用するコードの管理
2123
+ * 非推奨
2124
+ *
2125
+ * @deprecated 非推奨
2126
+ *
2127
+ * @internal
1868
2128
  */
1869
- /** @internal */
1870
- async function runScript$1(options = {
2129
+ const show = showAction;
2130
+ /**
2131
+ * 非推奨
2132
+ *
2133
+ * @deprecated 非推奨
2134
+ *
2135
+ * @internal
2136
+ */
2137
+ const close = closeAction;
2138
+ /**
2139
+ * 非推奨
2140
+ *
2141
+ * @deprecated 非推奨
2142
+ *
2143
+ * @internal
2144
+ */
2145
+ const ensureModalRoot = ensureActionRoot;
2146
+ /**
2147
+ * 非推奨
2148
+ *
2149
+ * @deprecated 非推奨
2150
+ *
2151
+ * @internal
2152
+ */
2153
+ function createApp(App, options = {
1871
2154
  send: () => { },
1872
2155
  publish: () => { },
1873
2156
  props: {},
1874
2157
  variables: {},
1875
2158
  localVariablesQuery: undefined,
1876
- karteTemplate: {},
1877
- context: { api_key: '', collection_endpoint: undefined },
2159
+ context: { api_key: '' },
1878
2160
  }) {
1879
- if (!options.onCreate)
1880
- return;
1881
- let data = getVariables();
1882
- initialize({ send: options.send, initialState: data.initial_state });
1883
- initActionTable(options.localVariablesQuery);
1884
- const { success } = await setupActionTable(options.localVariablesQuery, data, data.api_key, options.context.collection_endpoint);
1885
- if (!success)
1886
- return;
1887
- // Action Tableの取得結果を反映する
1888
- data = getVariables();
1889
- const actionProps = {
1890
- send: options.send,
1891
- publish: options.publish,
1892
- data,
2161
+ let app = null;
2162
+ const close = () => {
2163
+ if (app) {
2164
+ {
2165
+ // @ts-ignore -- Svelte3 では `unmount` は存在しない
2166
+ svelte.unmount(app);
2167
+ }
2168
+ app = null;
2169
+ }
2170
+ };
2171
+ const appArgs = {
2172
+ target: null,
2173
+ props: {
2174
+ send: options.send,
2175
+ publish: options.publish,
2176
+ close,
2177
+ data: {
2178
+ ...options.props,
2179
+ ...options.variables,
2180
+ },
2181
+ },
2182
+ };
2183
+ const win = ensureActionRoot(isOnSite());
2184
+ appArgs.target = win;
2185
+ return {
2186
+ close,
2187
+ show: () => {
2188
+ if (app) {
2189
+ return;
2190
+ }
2191
+ options.send('message_open');
2192
+ app = // @ts-ignore -- Svelte3 では `mount` は存在しない
2193
+ svelte.mount(App, appArgs)
2194
+ ;
2195
+ },
2196
+ };
2197
+ }
2198
+ /**
2199
+ * 非推奨
2200
+ *
2201
+ * @deprecated 非推奨
2202
+ *
2203
+ * @internal
2204
+ */
2205
+ function createFog({ color = '#000', opacity = '50%', zIndex = 999, onclick, }) {
2206
+ console.log('createFog');
2207
+ const root = ensureModalRoot();
2208
+ if (root.querySelector('.__krt-fog')) {
2209
+ return { fog: null, close: () => { } };
2210
+ }
2211
+ const fog = document.createElement('div');
2212
+ fog.className = '__krt-fog';
2213
+ Object.assign(fog.style, {
2214
+ position: 'fixed',
2215
+ left: 0,
2216
+ top: 0,
2217
+ width: '100%',
2218
+ height: '100%',
2219
+ 'z-index': zIndex,
2220
+ 'background-color': color,
2221
+ opacity,
2222
+ });
2223
+ const close = () => {
2224
+ onclick();
2225
+ fog.remove();
2226
+ };
2227
+ fog.onclick = close;
2228
+ root.appendChild(fog);
2229
+ return { fog, close };
2230
+ }
2231
+
2232
+ /**
2233
+ * スクリプト接客が利用するコードの管理
2234
+ */
2235
+ /** @internal */
2236
+ async function runScript$1(options = {
2237
+ send: () => { },
2238
+ publish: () => { },
2239
+ props: {},
2240
+ variables: {},
2241
+ localVariablesQuery: undefined,
2242
+ karteTemplate: {},
2243
+ context: { api_key: '', collection_endpoint: undefined },
2244
+ }) {
2245
+ if (!options.onCreate)
2246
+ return;
2247
+ let data = getVariables();
2248
+ initialize({ send: options.send, initialState: data.initial_state });
2249
+ initActionTable(options.localVariablesQuery);
2250
+ const { success } = await setupActionTable(options.localVariablesQuery, data, data.api_key, options.context.collection_endpoint);
2251
+ if (!success)
2252
+ return;
2253
+ // Action Tableの取得結果を反映する
2254
+ data = getVariables();
2255
+ const actionProps = {
2256
+ send: options.send,
2257
+ publish: options.publish,
2258
+ data,
1893
2259
  };
1894
2260
  options.send('script_fired');
1895
2261
  // 旧Widget API IFの処理
@@ -2030,7 +2396,7 @@ function create(App, options) {
2030
2396
  runScript$1(options);
2031
2397
  }
2032
2398
  else {
2033
- modalCleanup = createModal(App, options);
2399
+ modalCleanup = createModal(App, options) ;
2034
2400
  }
2035
2401
  return () => {
2036
2402
  loggerCleanup();
@@ -2098,313 +2464,6 @@ function destroy() {
2098
2464
  dispatchDestroyEvent();
2099
2465
  }
2100
2466
 
2101
- /**
2102
- * モーダル(ポップアップ)に関連するコードの管理
2103
- *
2104
- * アクションのShow, Close, ChangeStateの状態はここで管理する。
2105
- */
2106
- /**
2107
- * アクションが表示 (show) された後にフックする関数
2108
- *
2109
- * @param fn - 呼び出されるフック関数
2110
- *
2111
- * @public
2112
- */
2113
- function onShow(fn) {
2114
- let { onShowHandlers } = getInternalHandlers();
2115
- if (!onShowHandlers) {
2116
- onShowHandlers = [];
2117
- }
2118
- onShowHandlers.push(fn);
2119
- setInternalHandlers({ onShowHandlers });
2120
- }
2121
- /**
2122
- * アクションがクローズ (close) される前にフックする関数
2123
- *
2124
- * @param fn - 呼び出されるフック関数
2125
- *
2126
- * @public
2127
- */
2128
- function onClose(fn) {
2129
- let { onCloseHandlers } = getInternalHandlers();
2130
- if (!onCloseHandlers) {
2131
- onCloseHandlers = [];
2132
- }
2133
- onCloseHandlers.push(fn);
2134
- setInternalHandlers({ onCloseHandlers });
2135
- }
2136
- /**
2137
- * アクションのステートが変更された (changeState) 後にフックする関数
2138
- *
2139
- * @param fn - 呼び出されるフック関数
2140
- *
2141
- * @public
2142
- */
2143
- function onChangeState(fn) {
2144
- let { onChangeStateHandlers } = getInternalHandlers();
2145
- if (!onChangeStateHandlers) {
2146
- onChangeStateHandlers = [];
2147
- }
2148
- onChangeStateHandlers.push(fn);
2149
- setInternalHandlers({ onChangeStateHandlers });
2150
- }
2151
- /**
2152
- * アクションを表示する
2153
- *
2154
- * @public
2155
- */
2156
- function showAction() {
2157
- const event = new CustomEvent(ACTION_SHOW_EVENT, { detail: { trigger: 'custom' } });
2158
- window.dispatchEvent(event);
2159
- }
2160
- /**
2161
- * アクションを閉じる
2162
- *
2163
- * @param trigger - 閉じた時のトリガー。デフォルト `'none'`
2164
- *
2165
- * @public
2166
- */
2167
- function closeAction(trigger = 'none') {
2168
- const event = new CustomEvent(ACTION_CLOSE_EVENT, { detail: { trigger } });
2169
- window.dispatchEvent(event);
2170
- }
2171
- /**
2172
- * アクションに CSS を適用する
2173
- *
2174
- * @param css - 適用する CSS
2175
- *
2176
- * @returns 適用された style 要素を返す Promise
2177
- *
2178
- * @public
2179
- */
2180
- async function applyCss(css) {
2181
- return new Promise((resolve, reject) => {
2182
- const style = document.createElement('style');
2183
- style.textContent = css;
2184
- const shadowRoot = getActionRoot();
2185
- if (!shadowRoot)
2186
- return;
2187
- shadowRoot.append(style);
2188
- style.addEventListener('load', () => resolve(style));
2189
- style.addEventListener('error', () => reject(style));
2190
- });
2191
- }
2192
- async function fixFontFaceIssue(href, cssRules) {
2193
- const css = new CSSStyleSheet();
2194
- // @ts-ignore
2195
- await css.replace(cssRules);
2196
- const rules = [];
2197
- const fixedRules = [];
2198
- Array.from(css.cssRules).forEach(cssRule => {
2199
- if (cssRule.type !== 5) {
2200
- rules.push(cssRule.cssText);
2201
- }
2202
- // type 5 is @font-face
2203
- const split = href.split('/');
2204
- const stylePath = split.slice(0, split.length - 1).join('/');
2205
- const cssText = cssRule.cssText;
2206
- const newCssText = cssText.replace(
2207
- // relative paths
2208
- /url\s*\(\s*['"]?(?!((\/)|((?:https?:)?\/\/)|(?:data:?:)))([^'")]+)['"]?\s*\)/g, `url("${stylePath}/$4")`);
2209
- if (cssText === newCssText)
2210
- return;
2211
- fixedRules.push(newCssText);
2212
- });
2213
- return [rules.join('\n'), fixedRules.join('\n')];
2214
- }
2215
- /**
2216
- * アクションにグローバルなスタイルを読み込む
2217
- *
2218
- * @param href - style ファイルのリンク URL
2219
- *
2220
- * @public
2221
- */
2222
- async function loadStyle(href) {
2223
- const sr = getActionRoot();
2224
- if (!sr)
2225
- return;
2226
- let cssRules = '';
2227
- try {
2228
- const res = await fetch(href);
2229
- cssRules = await res.text();
2230
- }
2231
- catch (_) {
2232
- // pass
2233
- }
2234
- if (!cssRules)
2235
- return;
2236
- // Chromeのバグで、Shadow Rootの@font-faceを絶対パスで指定する必要がある
2237
- // @see https://stackoverflow.com/a/63717709
2238
- const [rules, fontFaceRules] = await fixFontFaceIssue(href, cssRules);
2239
- const css = new CSSStyleSheet();
2240
- // @ts-ignore
2241
- await css.replace(rules);
2242
- const fontFaceCss = new CSSStyleSheet();
2243
- // @ts-ignore
2244
- await fontFaceCss.replace(fontFaceRules);
2245
- // @ts-ignore
2246
- sr.adoptedStyleSheets = [...sr.adoptedStyleSheets, css, fontFaceCss];
2247
- // Chromeのバグで、ページとShadow Rootの両方に、@font-faceを設定する必要がある
2248
- // @see https://stackoverflow.com/a/63717709
2249
- // @ts-ignore
2250
- document.adoptedStyleSheets = [...document.adoptedStyleSheets, css, fontFaceCss];
2251
- }
2252
- // @internal
2253
- function getCssVariables(data) {
2254
- return Object.entries(data)
2255
- .filter(([key, value]) => {
2256
- return ['string', 'number'].includes(typeof value) && key.startsWith('--');
2257
- })
2258
- .map(([key, value]) => `${key}:${value}`)
2259
- .join(';');
2260
- }
2261
- /**
2262
- * アクションのルートの DOM 要素を取得する
2263
- *
2264
- * @returns アクションがルートの DOM 要素 を持つ場合は DOM 要素を返します。ない場合は `null` を返します
2265
- *
2266
- * @public
2267
- */
2268
- function getActionRoot() {
2269
- const root = document.querySelector(`.${KARTE_ACTION_ROOT}[data-${KARTE_ACTION_RID}='${actionId}']`);
2270
- if (!root?.shadowRoot) {
2271
- return null;
2272
- }
2273
- return root.shadowRoot;
2274
- }
2275
- /** @internal */
2276
- function ensureActionRoot(useShadow = true) {
2277
- const systemConfig = getSystem();
2278
- const rootAttrs = {
2279
- class: `${KARTE_ACTION_ROOT} ${KARTE_MODAL_ROOT}`,
2280
- [`data-${KARTE_ACTION_RID}`]: actionId,
2281
- [`data-${KARTE_ACTION_SHORTEN_ID}`]: systemConfig.shortenId
2282
- ? systemConfig.shortenId
2283
- : ALL_ACTION_SHORTEN_ID,
2284
- style: { display: 'block' },
2285
- };
2286
- let el = document.querySelector(`.${KARTE_MODAL_ROOT}[data-${KARTE_ACTION_RID}='${actionId}']`);
2287
- if (el == null) {
2288
- el = h('div', rootAttrs);
2289
- document.body.appendChild(el);
2290
- }
2291
- const isShadow = !!document.body.attachShadow && useShadow;
2292
- if (isShadow) {
2293
- return el.shadowRoot ?? el.attachShadow({ mode: 'open' });
2294
- }
2295
- else {
2296
- return el;
2297
- }
2298
- }
2299
- /**
2300
- * 非推奨
2301
- *
2302
- * @deprecated 非推奨
2303
- *
2304
- * @internal
2305
- */
2306
- const show = showAction;
2307
- /**
2308
- * 非推奨
2309
- *
2310
- * @deprecated 非推奨
2311
- *
2312
- * @internal
2313
- */
2314
- const close = closeAction;
2315
- /**
2316
- * 非推奨
2317
- *
2318
- * @deprecated 非推奨
2319
- *
2320
- * @internal
2321
- */
2322
- const ensureModalRoot = ensureActionRoot;
2323
- /**
2324
- * 非推奨
2325
- *
2326
- * @deprecated 非推奨
2327
- *
2328
- * @internal
2329
- */
2330
- function createApp(App, options = {
2331
- send: () => { },
2332
- publish: () => { },
2333
- props: {},
2334
- variables: {},
2335
- localVariablesQuery: undefined,
2336
- context: { api_key: '' },
2337
- }) {
2338
- let app = null;
2339
- const close = () => {
2340
- if (app) {
2341
- {
2342
- // @ts-ignore -- Svelte3 では `unmount` は存在しない
2343
- svelte.unmount(app);
2344
- }
2345
- app = null;
2346
- }
2347
- };
2348
- const appArgs = {
2349
- target: null,
2350
- props: {
2351
- send: options.send,
2352
- publish: options.publish,
2353
- close,
2354
- data: {
2355
- ...options.props,
2356
- ...options.variables,
2357
- },
2358
- },
2359
- };
2360
- const win = ensureModalRoot(true);
2361
- appArgs.target = win;
2362
- return {
2363
- close,
2364
- show: () => {
2365
- if (app) {
2366
- return;
2367
- }
2368
- options.send('message_open');
2369
- app = // @ts-ignore -- Svelte3 では `mount` は存在しない
2370
- svelte.mount(App, appArgs)
2371
- ;
2372
- },
2373
- };
2374
- }
2375
- /**
2376
- * 非推奨
2377
- *
2378
- * @deprecated 非推奨
2379
- *
2380
- * @internal
2381
- */
2382
- function createFog({ color = '#000', opacity = '50%', zIndex = 999, onclick, }) {
2383
- const root = ensureModalRoot();
2384
- if (root.querySelector('.__krt-fog')) {
2385
- return { fog: null, close: () => { } };
2386
- }
2387
- const fog = document.createElement('div');
2388
- fog.className = '__krt-fog';
2389
- Object.assign(fog.style, {
2390
- position: 'fixed',
2391
- left: 0,
2392
- top: 0,
2393
- width: '100%',
2394
- height: '100%',
2395
- 'z-index': zIndex,
2396
- 'background-color': color,
2397
- opacity,
2398
- });
2399
- const close = () => {
2400
- onclick();
2401
- fog.remove();
2402
- };
2403
- fog.onclick = close;
2404
- root.appendChild(fog);
2405
- return { fog, close };
2406
- }
2407
-
2408
2467
  const USER_ID_VARIABLE_NAME = '__karte_form_identify_user_id';
2409
2468
  const MAX_LENGTH_FREE_ANSWER = 2000;
2410
2469
  function createAnswerValue(value) {