@plaidev/karte-action-sdk 1.1.268-29083022.42c3d847 → 1.1.268
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/hydrate/index.es.js +1367 -1368
- package/dist/index.es.js +1380 -1381
- package/dist/svelte5/hydrate/index.es.js +323 -382
- package/dist/svelte5/index.es.js +369 -353
- package/dist/svelte5/index.front2.es.js +369 -353
- package/package.json +1 -1
@@ -1352,58 +1352,6 @@ 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
|
-
}
|
1407
1355
|
|
1408
1356
|
/**
|
1409
1357
|
* アクションテーブルに関連するコードの管理
|
@@ -1667,32 +1615,6 @@ function initActionTable(localVariablesQuery) {
|
|
1667
1615
|
setVariables(initValues);
|
1668
1616
|
}
|
1669
1617
|
|
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
|
-
|
1696
1618
|
/**
|
1697
1619
|
* モーダル(ポップアップ)に関連するコードの管理
|
1698
1620
|
*
|
@@ -1710,57 +1632,12 @@ const handleState = (event) => {
|
|
1710
1632
|
});
|
1711
1633
|
}
|
1712
1634
|
};
|
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
|
-
}
|
1758
1635
|
/**
|
1759
1636
|
* アクションを表示する
|
1760
1637
|
*
|
1761
1638
|
* @public
|
1762
1639
|
*/
|
1763
|
-
function showAction() {
|
1640
|
+
function showAction$1() {
|
1764
1641
|
const event = new CustomEvent(ACTION_SHOW_EVENT, { detail: { trigger: 'custom' } });
|
1765
1642
|
window.dispatchEvent(event);
|
1766
1643
|
}
|
@@ -1771,114 +1648,10 @@ function showAction() {
|
|
1771
1648
|
*
|
1772
1649
|
* @public
|
1773
1650
|
*/
|
1774
|
-
function closeAction(trigger = 'none') {
|
1651
|
+
function closeAction$1(trigger = 'none') {
|
1775
1652
|
const event = new CustomEvent(ACTION_CLOSE_EVENT, { detail: { trigger } });
|
1776
1653
|
window.dispatchEvent(event);
|
1777
1654
|
}
|
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
|
-
}
|
1882
1655
|
/** @internal */
|
1883
1656
|
function createModal(App, options = {
|
1884
1657
|
send: () => { },
|
@@ -1946,7 +1719,7 @@ function createModal(App, options = {
|
|
1946
1719
|
if (app) {
|
1947
1720
|
return;
|
1948
1721
|
}
|
1949
|
-
if (
|
1722
|
+
if (trigger === 'custom') {
|
1950
1723
|
return;
|
1951
1724
|
}
|
1952
1725
|
if (trigger === 'custom' && (options.props.show_on_scroll || options.props.show_on_time)) {
|
@@ -1973,9 +1746,9 @@ function createModal(App, options = {
|
|
1973
1746
|
if (app) {
|
1974
1747
|
return;
|
1975
1748
|
}
|
1976
|
-
const target = ensureActionRoot(isOnSite());
|
1977
1749
|
const props = {
|
1978
|
-
target,
|
1750
|
+
target: ensureActionRoot$1(!true),
|
1751
|
+
hydrate: true,
|
1979
1752
|
props: {
|
1980
1753
|
send: options.send,
|
1981
1754
|
publish: options.publish,
|
@@ -2010,43 +1783,17 @@ function createModal(App, options = {
|
|
2010
1783
|
},
|
2011
1784
|
},
|
2012
1785
|
};
|
2013
|
-
app =
|
2014
|
-
|
1786
|
+
app = svelte.hydrate(App, props)
|
1787
|
+
|
2015
1788
|
;
|
2016
1789
|
};
|
2017
1790
|
const handleShow = (event) => {
|
2018
1791
|
const trigger = event?.detail?.trigger ? event.detail.trigger : 'none';
|
2019
1792
|
show(trigger);
|
2020
1793
|
};
|
2021
|
-
const currying = (conditionFn, options) => (fn) => conditionFn(options.props, fn);
|
2022
1794
|
const autoShow = () => {
|
2023
1795
|
return show('auto');
|
2024
1796
|
};
|
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
|
-
};
|
2050
1797
|
// ここからメインの処理
|
2051
1798
|
initialize({ send: options.send, initialState: data.initial_state });
|
2052
1799
|
// ActionTable APIへの非同期リクエスト
|
@@ -2061,13 +1808,9 @@ function createModal(App, options = {
|
|
2061
1808
|
window.addEventListener(ACTION_CHANGE_STATE_EVENT, handleState);
|
2062
1809
|
let showTriggerCleanups = [];
|
2063
1810
|
let closeTriggerCleanups = [];
|
2064
|
-
|
1811
|
+
{
|
2065
1812
|
autoShow();
|
2066
1813
|
}
|
2067
|
-
else {
|
2068
|
-
showTriggerCleanups = listenShowTrigger();
|
2069
|
-
closeTriggerCleanups = listenCloseTrigger();
|
2070
|
-
}
|
2071
1814
|
// 旧Widget API IFの処理
|
2072
1815
|
const { onCreateHandlers } = getInternalHandlers();
|
2073
1816
|
if (onCreateHandlers) {
|
@@ -2096,7 +1839,7 @@ function createModal(App, options = {
|
|
2096
1839
|
return appCleanup;
|
2097
1840
|
}
|
2098
1841
|
/** @internal */
|
2099
|
-
function ensureActionRoot(useShadow = true) {
|
1842
|
+
function ensureActionRoot$1(useShadow = true) {
|
2100
1843
|
const systemConfig = getSystem();
|
2101
1844
|
const rootAttrs = {
|
2102
1845
|
class: `${KARTE_ACTION_ROOT} ${KARTE_MODAL_ROOT}`,
|
@@ -2119,128 +1862,19 @@ function ensureActionRoot(useShadow = true) {
|
|
2119
1862
|
return el;
|
2120
1863
|
}
|
2121
1864
|
}
|
1865
|
+
|
2122
1866
|
/**
|
2123
|
-
*
|
2124
|
-
*
|
2125
|
-
* @deprecated 非推奨
|
2126
|
-
*
|
2127
|
-
* @internal
|
2128
|
-
*/
|
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
|
1867
|
+
* スクリプト接客が利用するコードの管理
|
2152
1868
|
*/
|
2153
|
-
|
1869
|
+
/** @internal */
|
1870
|
+
async function runScript$1(options = {
|
2154
1871
|
send: () => { },
|
2155
1872
|
publish: () => { },
|
2156
1873
|
props: {},
|
2157
1874
|
variables: {},
|
2158
1875
|
localVariablesQuery: undefined,
|
2159
|
-
|
2160
|
-
|
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 },
|
1876
|
+
karteTemplate: {},
|
1877
|
+
context: { api_key: '', collection_endpoint: undefined },
|
2244
1878
|
}) {
|
2245
1879
|
if (!options.onCreate)
|
2246
1880
|
return;
|
@@ -2396,7 +2030,7 @@ function create(App, options) {
|
|
2396
2030
|
runScript$1(options);
|
2397
2031
|
}
|
2398
2032
|
else {
|
2399
|
-
modalCleanup = createModal(App, options)
|
2033
|
+
modalCleanup = createModal(App, options);
|
2400
2034
|
}
|
2401
2035
|
return () => {
|
2402
2036
|
loggerCleanup();
|
@@ -2464,6 +2098,313 @@ function destroy() {
|
|
2464
2098
|
dispatchDestroyEvent();
|
2465
2099
|
}
|
2466
2100
|
|
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
|
+
|
2467
2408
|
const USER_ID_VARIABLE_NAME = '__karte_form_identify_user_id';
|
2468
2409
|
const MAX_LENGTH_FREE_ANSWER = 2000;
|
2469
2410
|
function createAnswerValue(value) {
|