@plaidev/karte-action-sdk 1.1.96 → 1.1.97

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.
@@ -701,7 +701,7 @@ declare function applyCss(css: string): Promise<any>;
701
701
  *
702
702
  * @param {string} href Link of style file
703
703
  */
704
- declare function loadStyle(href: string): Promise<any>;
704
+ declare function loadStyle(href: string): Promise<void>;
705
705
  // -------- The following codes are deprecated --------
706
706
  declare const showModal: typeof create; // @deprecated
707
707
  type ModalOptions<Props, Variables> = ActionOptions<Props, Variables>; // @deprecated
@@ -1333,7 +1333,7 @@ declare namespace widget {
1333
1333
  *
1334
1334
  * @param {string} href Link of style file
1335
1335
  */
1336
- function loadStyle(href: string): Promise<any>;
1336
+ function loadStyle(href: string): Promise<void>;
1337
1337
  // -------- The following codes are deprecated --------
1338
1338
  const showModal: typeof create; // @deprecated
1339
1339
  type ModalOptions<Props, Variables> = ActionOptions<Props, Variables>; // @deprecated
@@ -963,23 +963,63 @@ async function applyCss(css) {
963
963
  style.addEventListener('error', () => reject(style));
964
964
  });
965
965
  }
966
+ async function fixFontFaceIssue(href, cssRules) {
967
+ const css = new CSSStyleSheet();
968
+ // @ts-ignore
969
+ await css.replace(cssRules);
970
+ const rules = [];
971
+ const fixedRules = [];
972
+ Array.from(css.cssRules).forEach(cssRule => {
973
+ if (cssRule.type != 5) {
974
+ rules.push(cssRule.cssText);
975
+ }
976
+ // type 5 is @font-face
977
+ const split = href.split('/');
978
+ const stylePath = split.slice(0, split.length - 1).join('/');
979
+ const cssText = cssRule.cssText;
980
+ const newCssText = cssText.replace(
981
+ // relative paths
982
+ /url\s*\(\s*['"]?(?!((\/)|((?:https?:)?\/\/)|(?:data:?:)))([^'")]+)['"]?\s*\)/g, `url("${stylePath}/$4")`);
983
+ if (cssText === newCssText)
984
+ return;
985
+ fixedRules.push(newCssText);
986
+ });
987
+ return [rules.join('\n'), fixedRules.join('\n')];
988
+ }
966
989
  /**
967
990
  * {@link loadStyle} load global style to KARTE action.
968
991
  *
969
992
  * @param {string} href Link of style file
970
993
  */
971
994
  async function loadStyle(href) {
972
- return new Promise((resolve, reject) => {
973
- const link = document.createElement('link');
974
- link.rel = 'stylesheet';
975
- link.href = href;
976
- const shadowRoot = getActionShadowRoot();
977
- if (!shadowRoot)
978
- return;
979
- shadowRoot.append(link);
980
- link.addEventListener('load', () => resolve(link));
981
- link.addEventListener('error', () => reject(link));
982
- });
995
+ const sr = getActionShadowRoot();
996
+ if (!sr)
997
+ return;
998
+ let cssRules = '';
999
+ try {
1000
+ const res = await fetch(href);
1001
+ cssRules = await res.text();
1002
+ }
1003
+ catch (e) {
1004
+ // pass
1005
+ }
1006
+ if (!cssRules)
1007
+ return;
1008
+ // Chromeのバグで、Shadow Rootの@font-faceを絶対パスで指定する必要がある
1009
+ // @see https://stackoverflow.com/a/63717709
1010
+ const [rules, fontFaceRules] = await fixFontFaceIssue(href, cssRules);
1011
+ const css = new CSSStyleSheet();
1012
+ // @ts-ignore
1013
+ await css.replace(rules);
1014
+ const fontFaceCss = new CSSStyleSheet();
1015
+ // @ts-ignore
1016
+ await fontFaceCss.replace(fontFaceRules);
1017
+ // @ts-ignore
1018
+ sr.adoptedStyleSheets = [...sr.adoptedStyleSheets, css, fontFaceCss];
1019
+ // Chromeのバグで、ページとShadow Rootの両方に、@font-faceを設定する必要がある
1020
+ // @see https://stackoverflow.com/a/63717709
1021
+ // @ts-ignore
1022
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, css, fontFaceCss];
983
1023
  }
984
1024
  // -------- The following codes are deprecated --------
985
1025
  const showModal = create; // @deprecated
@@ -701,7 +701,7 @@ declare function applyCss(css: string): Promise<any>;
701
701
  *
702
702
  * @param {string} href Link of style file
703
703
  */
704
- declare function loadStyle(href: string): Promise<any>;
704
+ declare function loadStyle(href: string): Promise<void>;
705
705
  // -------- The following codes are deprecated --------
706
706
  declare const showModal: typeof create; // @deprecated
707
707
  type ModalOptions<Props, Variables> = ActionOptions<Props, Variables>; // @deprecated
@@ -1333,7 +1333,7 @@ declare namespace widget {
1333
1333
  *
1334
1334
  * @param {string} href Link of style file
1335
1335
  */
1336
- function loadStyle(href: string): Promise<any>;
1336
+ function loadStyle(href: string): Promise<void>;
1337
1337
  // -------- The following codes are deprecated --------
1338
1338
  const showModal: typeof create; // @deprecated
1339
1339
  type ModalOptions<Props, Variables> = ActionOptions<Props, Variables>; // @deprecated
package/dist/index.es.js CHANGED
@@ -1077,23 +1077,63 @@ async function applyCss(css) {
1077
1077
  style.addEventListener('error', () => reject(style));
1078
1078
  });
1079
1079
  }
1080
+ async function fixFontFaceIssue(href, cssRules) {
1081
+ const css = new CSSStyleSheet();
1082
+ // @ts-ignore
1083
+ await css.replace(cssRules);
1084
+ const rules = [];
1085
+ const fixedRules = [];
1086
+ Array.from(css.cssRules).forEach(cssRule => {
1087
+ if (cssRule.type != 5) {
1088
+ rules.push(cssRule.cssText);
1089
+ }
1090
+ // type 5 is @font-face
1091
+ const split = href.split('/');
1092
+ const stylePath = split.slice(0, split.length - 1).join('/');
1093
+ const cssText = cssRule.cssText;
1094
+ const newCssText = cssText.replace(
1095
+ // relative paths
1096
+ /url\s*\(\s*['"]?(?!((\/)|((?:https?:)?\/\/)|(?:data:?:)))([^'")]+)['"]?\s*\)/g, `url("${stylePath}/$4")`);
1097
+ if (cssText === newCssText)
1098
+ return;
1099
+ fixedRules.push(newCssText);
1100
+ });
1101
+ return [rules.join('\n'), fixedRules.join('\n')];
1102
+ }
1080
1103
  /**
1081
1104
  * {@link loadStyle} load global style to KARTE action.
1082
1105
  *
1083
1106
  * @param {string} href Link of style file
1084
1107
  */
1085
1108
  async function loadStyle(href) {
1086
- return new Promise((resolve, reject) => {
1087
- const link = document.createElement('link');
1088
- link.rel = 'stylesheet';
1089
- link.href = href;
1090
- const shadowRoot = getActionShadowRoot();
1091
- if (!shadowRoot)
1092
- return;
1093
- shadowRoot.append(link);
1094
- link.addEventListener('load', () => resolve(link));
1095
- link.addEventListener('error', () => reject(link));
1096
- });
1109
+ const sr = getActionShadowRoot();
1110
+ if (!sr)
1111
+ return;
1112
+ let cssRules = '';
1113
+ try {
1114
+ const res = await fetch(href);
1115
+ cssRules = await res.text();
1116
+ }
1117
+ catch (e) {
1118
+ // pass
1119
+ }
1120
+ if (!cssRules)
1121
+ return;
1122
+ // Chromeのバグで、Shadow Rootの@font-faceを絶対パスで指定する必要がある
1123
+ // @see https://stackoverflow.com/a/63717709
1124
+ const [rules, fontFaceRules] = await fixFontFaceIssue(href, cssRules);
1125
+ const css = new CSSStyleSheet();
1126
+ // @ts-ignore
1127
+ await css.replace(rules);
1128
+ const fontFaceCss = new CSSStyleSheet();
1129
+ // @ts-ignore
1130
+ await fontFaceCss.replace(fontFaceRules);
1131
+ // @ts-ignore
1132
+ sr.adoptedStyleSheets = [...sr.adoptedStyleSheets, css, fontFaceCss];
1133
+ // Chromeのバグで、ページとShadow Rootの両方に、@font-faceを設定する必要がある
1134
+ // @see https://stackoverflow.com/a/63717709
1135
+ // @ts-ignore
1136
+ document.adoptedStyleSheets = [...document.adoptedStyleSheets, css, fontFaceCss];
1097
1137
  }
1098
1138
  // -------- The following codes are deprecated --------
1099
1139
  const showModal = create; // @deprecated
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaidev/karte-action-sdk",
3
- "version": "1.1.96",
3
+ "version": "1.1.97",
4
4
  "author": "Plaid Inc.",
5
5
  "license": "Apache-2.0",
6
6
  "module": "./dist/index.es.js",
@@ -27,8 +27,8 @@
27
27
  "LICENSE"
28
28
  ],
29
29
  "devDependencies": {
30
- "@plaidev/action-compiler": "0.4.201",
31
- "@plaidev/sender-types": "^0.4.140",
30
+ "@plaidev/action-compiler": "workspace:*",
31
+ "@plaidev/sender-types": "workspace:*",
32
32
  "@rollup/plugin-alias": "^4.0.0",
33
33
  "@rollup/plugin-commonjs": "^23.0.1",
34
34
  "@rollup/plugin-json": "^5.0.0",