@data-fair/frame 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ export type ParsedSyncParams = {
2
+ regexp: RegExp;
3
+ append?: string;
4
+ }[];
5
+ export declare function parseSyncParams(syncParamsStr: string): ParsedSyncParams;
6
+ export declare function getChildSrc(srcUrl: URL, parentHref: string, syncParams: ParsedSyncParams | undefined, syncPath: null | string): string;
7
+ export declare function getParentUrl(srcUrl: URL | undefined, childHref: string, currentParentHref: string, syncParams: ParsedSyncParams | undefined, syncPath: null | string): URL;
@@ -0,0 +1,80 @@
1
+ import { getUrlRelativePath } from './url.js';
2
+ const escapeRegExp = (str) => str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
3
+ export function parseSyncParams(syncParamsStr) {
4
+ const syncParams = [];
5
+ for (const syncParam of syncParamsStr.split(',')) {
6
+ const [pattern = '*', append] = syncParam.split(':');
7
+ const regexpStr = '^' + escapeRegExp(pattern).replace(/\\\*/g, '(.*)') + '$';
8
+ syncParams.push({
9
+ regexp: new RegExp(regexpStr),
10
+ append
11
+ });
12
+ }
13
+ return syncParams;
14
+ }
15
+ export function getChildSrc(srcUrl, parentHref, syncParams, syncPath) {
16
+ if (!syncParams && !syncPath)
17
+ return srcUrl.href;
18
+ const parentUrl = new URL(parentHref);
19
+ if (syncParams) {
20
+ parentUrl.searchParams.forEach((value, key) => {
21
+ for (const syncParam of syncParams) {
22
+ if (syncParam.append && !key.startsWith(syncParam.append))
23
+ continue;
24
+ if (key === syncPath)
25
+ continue;
26
+ const childKey = syncParam.append ? key.replace(syncParam.append, '') : key;
27
+ if (!childKey.match(syncParam.regexp))
28
+ continue;
29
+ srcUrl.searchParams.set(childKey, value);
30
+ break;
31
+ }
32
+ });
33
+ }
34
+ if (syncPath !== null) {
35
+ const path = parentUrl.searchParams.get(syncPath);
36
+ if (path) {
37
+ srcUrl.pathname = new URL(path, srcUrl).pathname;
38
+ }
39
+ }
40
+ return srcUrl.href;
41
+ }
42
+ export function getParentUrl(srcUrl, childHref, currentParentHref, syncParams, syncPath) {
43
+ const parentUrl = new URL(currentParentHref);
44
+ const childUrl = new URL(childHref);
45
+ if (syncParams) {
46
+ // remove existing keys that were deleted from the child
47
+ parentUrl.searchParams.forEach((value, key) => {
48
+ for (const syncParam of syncParams) {
49
+ if (syncParam.append && !key.startsWith(syncParam.append))
50
+ continue;
51
+ const childKey = syncParam.append ? key.replace(syncParam.append, '') : key;
52
+ if (!childKey.match(syncParam.regexp))
53
+ continue;
54
+ if (childUrl.searchParams.get(childKey) === null)
55
+ parentUrl.searchParams.delete(key);
56
+ break;
57
+ }
58
+ });
59
+ // apply params from child to parent
60
+ childUrl.searchParams.forEach((value, key) => {
61
+ for (const syncParam of syncParams) {
62
+ if (!key.match(syncParam.regexp))
63
+ continue;
64
+ if (srcUrl?.searchParams.get(key) === value)
65
+ continue;
66
+ const parentKey = syncParam.append ? (syncParam.append + key) : key;
67
+ parentUrl.searchParams.set(parentKey, value);
68
+ break;
69
+ }
70
+ });
71
+ }
72
+ if (syncPath !== null && srcUrl) {
73
+ const path = getUrlRelativePath(srcUrl, childUrl);
74
+ if (path)
75
+ parentUrl.searchParams.set(syncPath, path);
76
+ else
77
+ parentUrl.searchParams.delete(syncPath);
78
+ }
79
+ return parentUrl;
80
+ }
@@ -0,0 +1,2 @@
1
+ export declare function getPathDirectory(path: string): string;
2
+ export declare function getUrlRelativePath(srcUrl: URL, currentUrl: URL): string;
@@ -0,0 +1,17 @@
1
+ export function getPathDirectory(path) {
2
+ if (path.endsWith('/'))
3
+ return path;
4
+ if (path === '')
5
+ return '/';
6
+ return path.slice(0, path.lastIndexOf('/') + 1);
7
+ }
8
+ export function getUrlRelativePath(srcUrl, currentUrl) {
9
+ if (currentUrl.origin !== srcUrl.origin)
10
+ return currentUrl.href;
11
+ if (currentUrl.pathname === srcUrl.pathname)
12
+ return '';
13
+ const srcDirectory = getPathDirectory(srcUrl.pathname);
14
+ if (!currentUrl.pathname.startsWith(srcDirectory))
15
+ return currentUrl.pathname;
16
+ return currentUrl.pathname.replace(srcDirectory, './');
17
+ }
@@ -0,0 +1,5 @@
1
+ import DFrameContent, { type DFrameContentOptions } from '../DFrameContent.js';
2
+ import type { Router } from 'vue-router';
3
+ type VueRouterDFrameContentOptions = Omit<DFrameContentOptions, 'updateSrc'>;
4
+ export declare function vueRouterDFrameContent(router: Router, options?: VueRouterDFrameContentOptions): DFrameContent;
5
+ export default vueRouterDFrameContent;
@@ -0,0 +1,15 @@
1
+ import DFrameContent from '../DFrameContent.js';
2
+ export function vueRouterDFrameContent(router, options) {
3
+ return new DFrameContent({
4
+ updateSrc: (src) => {
5
+ const urlPrefix = window.location.origin + router.options.history.base;
6
+ if (src.startsWith(urlPrefix)) {
7
+ router.replace(src.replace(urlPrefix, ''));
8
+ }
9
+ else {
10
+ window.location.href = src;
11
+ }
12
+ }
13
+ });
14
+ }
15
+ export default vueRouterDFrameContent;
@@ -0,0 +1,10 @@
1
+ import { type StateChangeAdapter } from '../DFrameElement.js';
2
+ import type { Router } from 'vue-router';
3
+ declare class VueRouterDFrameStateChangeAdapter implements StateChangeAdapter {
4
+ router: Router;
5
+ constructor(router: Router);
6
+ stateChange(action: 'push' | 'replace', newUrl: URL): void;
7
+ onStateChange(callback: () => void): void;
8
+ }
9
+ declare const _default: (router: Router) => VueRouterDFrameStateChangeAdapter;
10
+ export default _default;
@@ -0,0 +1,16 @@
1
+ class VueRouterDFrameStateChangeAdapter {
2
+ router;
3
+ constructor(router) {
4
+ this.router = router;
5
+ }
6
+ stateChange(action, newUrl) {
7
+ if (action === 'push')
8
+ this.router.push({ query: Object.fromEntries(newUrl.searchParams) });
9
+ if (action === 'replace')
10
+ this.router.replace({ query: Object.fromEntries(newUrl.searchParams) });
11
+ }
12
+ onStateChange(callback) {
13
+ this.router.afterEach(callback);
14
+ }
15
+ }
16
+ export default (router) => new VueRouterDFrameStateChangeAdapter(router);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/frame",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "An iframe wrapper for powerful web app integrations.",
5
5
  "main": "dist/frame.min.js",
6
6
  "directories": {
@@ -23,7 +23,7 @@
23
23
  "dev-zellij": "zellij --layout .zellij.kdl",
24
24
  "prepare": "husky",
25
25
  "test": "node --test tests/*.js",
26
- "build": "rm -f src/*.d.ts && rm -f src/*.js && tsc -p tsconfig.json && rm -rf dist && vite -c vite.config.d-frame.js build && vite -c vite.config.d-frame-content.js build",
26
+ "build": "rm -f lib/*.d.ts && rm -f lib/*.js && tsc -p tsconfig.json && rm -rf dist && vite -c vite.config.d-frame.js build && vite -c vite.config.d-frame-content.js build",
27
27
  "prepublishOnly": "npm run lint && npm run test && npm run build",
28
28
  "publish:main": "cd doc && npm run build:main && cd .. && gh-pages-multi deploy -v -s doc/.output/public -t main",
29
29
  "publish:latest": "cd doc && npm run build:latest && cd .. && gh-pages-multi deploy -v -s doc/.output/public -t latest"
@@ -37,8 +37,8 @@
37
37
  "license": "MIT",
38
38
  "type": "module",
39
39
  "files": [
40
- "src/*.js",
41
- "src/*.d.ts",
40
+ "lib/**/*.js",
41
+ "lib/**/*.d.ts",
42
42
  "dist/*.js"
43
43
  ]
44
44
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes