@preprio/toolkit 0.6.0-alpha.9 → 1.0.0-rc.2
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/README.md +111 -1363
- package/dist/astro/index.cjs +1555 -182
- package/dist/astro/index.d.cts +201 -1
- package/dist/astro/index.d.ts +201 -1
- package/dist/astro/index.js +1557 -29
- package/dist/chunk-A2J7R4LR.js +256 -0
- package/dist/{chunk-5QIWVDP2.js → chunk-DBKZWOXA.js} +1 -1
- package/dist/{chunk-WQBKQG4R.js → chunk-EWXBE3JD.js} +6 -1
- package/dist/chunk-KM2GME62.js +1255 -0
- package/dist/{chunk-J66UOCK6.js → chunk-LJA5K4KM.js} +1680 -579
- package/dist/chunk-UTKH7DOI.js +58 -0
- package/dist/chunk-V7MCHPE7.js +85 -0
- package/dist/chunk-Y7JWV7WV.js +374 -0
- package/dist/codegen/index.cjs +82 -0
- package/dist/codegen/index.d.cts +70 -0
- package/dist/codegen/index.d.ts +70 -0
- package/dist/codegen/index.js +64 -0
- package/dist/index.cjs +3729 -553
- package/dist/index.d.cts +403 -19
- package/dist/index.d.ts +403 -19
- package/dist/index.js +345 -24
- package/dist/live-preview-HCBNBC6C.js +19 -0
- package/dist/nextjs/components.cjs +1872 -754
- package/dist/nextjs/components.d.cts +5 -51
- package/dist/nextjs/components.d.ts +5 -51
- package/dist/nextjs/components.js +8 -299
- package/dist/nextjs/index.cjs +1228 -181
- package/dist/nextjs/index.d.cts +247 -92
- package/dist/nextjs/index.d.ts +247 -92
- package/dist/nextjs/index.js +1629 -111
- package/dist/nextjs/live-action.cjs +298 -0
- package/dist/nextjs/live-action.d.cts +12 -0
- package/dist/nextjs/live-action.d.ts +12 -0
- package/dist/nextjs/live-action.js +68 -0
- package/dist/nextjs/live.cjs +2010 -0
- package/dist/nextjs/live.d.cts +54 -0
- package/dist/nextjs/live.d.ts +54 -0
- package/dist/nextjs/live.js +287 -0
- package/dist/nuxt/chunk-5RBZPRQN.js +350 -0
- package/dist/nuxt/chunk-LUKVPOJ2.js +32 -0
- package/dist/nuxt/index.cjs +15 -16
- package/dist/nuxt/index.d.cts +167 -6
- package/dist/nuxt/index.d.ts +167 -6
- package/dist/nuxt/index.js +402 -15
- package/dist/nuxt/live-preview-KO4IKWTX.js +1234 -0
- package/dist/nuxt/live.cjs +1928 -0
- package/dist/nuxt/live.d.cts +45 -0
- package/dist/nuxt/live.d.ts +45 -0
- package/dist/nuxt/live.js +216 -0
- package/dist/{types-BYH9XqmN.d.cts → pixel-DqU7NvgW.d.cts} +102 -5
- package/dist/{types-BYH9XqmN.d.ts → pixel-DqU7NvgW.d.ts} +102 -5
- package/dist/react/components.cjs +1865 -480
- package/dist/react/components.d.cts +43 -8
- package/dist/react/components.d.ts +43 -8
- package/dist/react/components.js +1865 -480
- package/dist/sveltekit/chunk-5RBZPRQN.js +350 -0
- package/dist/sveltekit/chunk-LUKVPOJ2.js +32 -0
- package/dist/sveltekit/index.cjs +15 -16
- package/dist/sveltekit/index.d.cts +111 -10
- package/dist/sveltekit/index.d.ts +111 -10
- package/dist/sveltekit/index.js +398 -15
- package/dist/sveltekit/live-preview-KO4IKWTX.js +1234 -0
- package/dist/sveltekit/live.cjs +1940 -0
- package/dist/sveltekit/live.d.cts +61 -0
- package/dist/sveltekit/live.d.ts +61 -0
- package/dist/sveltekit/live.js +220 -0
- package/package.json +132 -71
- package/src/astro/PreprToolbar.astro +7 -1
- package/src/astro/PreprTrackingPixel.astro +2 -4
- package/src/nuxt/PreprToolbar.vue +12 -1
- package/src/nuxt/PreprTrackingPixel.vue +2 -4
- package/src/sveltekit/PreprToolbar.svelte +17 -2
- package/src/sveltekit/PreprTrackingPixel.svelte +2 -4
- package/dist/astro/chunk-UN4JADNU.js +0 -173
- package/dist/astro/graphql.cjs +0 -1078
- package/dist/astro/graphql.d.cts +0 -300
- package/dist/astro/graphql.d.ts +0 -300
- package/dist/astro/graphql.js +0 -984
- package/dist/astro/index-XT_z-RYT.d.cts +0 -130
- package/dist/astro/index-XT_z-RYT.d.ts +0 -130
- package/dist/chunk-3NCEVJJ3.js +0 -10
- package/dist/chunk-S5RWVMDJ.js +0 -148
- package/dist/chunk-XL4Y7FGW.js +0 -499
- package/dist/graphql/codegen.cjs +0 -557
- package/dist/graphql/codegen.d.cts +0 -23
- package/dist/graphql/codegen.d.ts +0 -23
- package/dist/graphql/codegen.js +0 -99
- package/dist/graphql.cjs +0 -1020
- package/dist/graphql.d.cts +0 -335
- package/dist/graphql.d.ts +0 -335
- package/dist/graphql.js +0 -448
- package/dist/nextjs/chunk-CMV6ZP6E.js +0 -549
- package/dist/nextjs/graphql.cjs +0 -1836
- package/dist/nextjs/graphql.d.cts +0 -498
- package/dist/nextjs/graphql.d.ts +0 -498
- package/dist/nextjs/graphql.js +0 -1422
- package/dist/nextjs/server-BL-sqZvu.d.cts +0 -156
- package/dist/nextjs/server-BL-sqZvu.d.ts +0 -156
- package/dist/nuxt/chunk-XWOABVMW.js +0 -417
- package/dist/nuxt/graphql.cjs +0 -1085
- package/dist/nuxt/graphql.d.cts +0 -297
- package/dist/nuxt/graphql.d.ts +0 -297
- package/dist/nuxt/graphql.js +0 -985
- package/dist/nuxt/server-8v6OHbsw.d.cts +0 -158
- package/dist/nuxt/server-8v6OHbsw.d.ts +0 -158
- package/dist/pixel-DwW7cXJm.d.cts +0 -49
- package/dist/pixel-DwW7cXJm.d.ts +0 -49
- package/dist/server-Brr1UgtW.d.ts +0 -59
- package/dist/server-DJuS3YU0.d.cts +0 -59
- package/dist/sveltekit/chunk-3S5RA6G5.js +0 -413
- package/dist/sveltekit/graphql.cjs +0 -1091
- package/dist/sveltekit/graphql.d.cts +0 -302
- package/dist/sveltekit/graphql.d.ts +0 -302
- package/dist/sveltekit/graphql.js +0 -991
- package/dist/sveltekit/server-CO847mEV.d.cts +0 -98
- package/dist/sveltekit/server-CO847mEV.d.ts +0 -98
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/core/constants.ts
|
|
2
|
+
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
3
|
+
var COOKIE_TOOLBAR_OPEN = "Prepr-Toolbar-Open";
|
|
4
|
+
var COOKIE_SEGMENT = "Prepr-Segments";
|
|
5
|
+
var COOKIE_VARIANT = "Prepr-ABtesting";
|
|
6
|
+
var COOKIE_UID = "__prepr_uid";
|
|
7
|
+
var COOKIE_HUBSPOT = "hubspotutk";
|
|
8
|
+
var PARAM_SEGMENT = "prepr_preview_segment";
|
|
9
|
+
var PARAM_VARIANT = "prepr_preview_ab";
|
|
10
|
+
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
11
|
+
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
12
|
+
var HEADER_LIVE_RENDER = "x-prepr-live";
|
|
13
|
+
var ATTR_ENCODED = "data-prepr-encoded";
|
|
14
|
+
var ATTR_ID = "data-prepr-id";
|
|
15
|
+
var ATTR_FIELD = "data-prepr-field";
|
|
16
|
+
var ATTR_LOCALE = "data-prepr-locale";
|
|
17
|
+
var ATTR_HREF = "data-prepr-href";
|
|
18
|
+
var ENCODED_SELECTOR = `[${ATTR_ENCODED}]`;
|
|
19
|
+
var TOOLBAR_TAG = "prepr-toolbar";
|
|
20
|
+
var SEGMENT_ALL_OTHER_USERS = "all_other_users";
|
|
21
|
+
var EVENT_INIT = "prepr:initVE";
|
|
22
|
+
var EVENT_REFERENCES = "prepr:references";
|
|
23
|
+
var EVENT_GET_SCROLL_POSITION = "prepr:getScrollPosition";
|
|
24
|
+
var EVENT_SCROLL_TO_FIELD = "prepr:scrollToField";
|
|
25
|
+
var EVENT_ALLOW_NAVIGATION = "prepr:allowNavigation";
|
|
26
|
+
var EVENT_ITEM_CHANGED = "prepr:itemChanged";
|
|
27
|
+
var EVENT_FIELD_CHANGE = "prepr:fieldChange";
|
|
28
|
+
var EVENT_LIST_CHANGE = "prepr:listChange";
|
|
29
|
+
|
|
30
|
+
export {
|
|
31
|
+
COOKIE_PREVIEW_MODE,
|
|
32
|
+
COOKIE_TOOLBAR_OPEN,
|
|
33
|
+
COOKIE_SEGMENT,
|
|
34
|
+
COOKIE_VARIANT,
|
|
35
|
+
COOKIE_UID,
|
|
36
|
+
COOKIE_HUBSPOT,
|
|
37
|
+
PARAM_SEGMENT,
|
|
38
|
+
PARAM_VARIANT,
|
|
39
|
+
PARAM_HIDE_BAR,
|
|
40
|
+
ONE_YEAR_SECONDS,
|
|
41
|
+
HEADER_LIVE_RENDER,
|
|
42
|
+
ATTR_ENCODED,
|
|
43
|
+
ATTR_ID,
|
|
44
|
+
ATTR_FIELD,
|
|
45
|
+
ATTR_LOCALE,
|
|
46
|
+
ATTR_HREF,
|
|
47
|
+
ENCODED_SELECTOR,
|
|
48
|
+
TOOLBAR_TAG,
|
|
49
|
+
SEGMENT_ALL_OTHER_USERS,
|
|
50
|
+
EVENT_INIT,
|
|
51
|
+
EVENT_REFERENCES,
|
|
52
|
+
EVENT_GET_SCROLL_POSITION,
|
|
53
|
+
EVENT_SCROLL_TO_FIELD,
|
|
54
|
+
EVENT_ALLOW_NAVIGATION,
|
|
55
|
+
EVENT_ITEM_CHANGED,
|
|
56
|
+
EVENT_FIELD_CHANGE,
|
|
57
|
+
EVENT_LIST_CHANGE
|
|
58
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// src/core/shared-state.ts
|
|
2
|
+
var KEY = /* @__PURE__ */ Symbol.for("@preprio/toolkit:shared-state");
|
|
3
|
+
function initialState() {
|
|
4
|
+
return {
|
|
5
|
+
trustedParentOrigin: null,
|
|
6
|
+
debugEnabled: void 0,
|
|
7
|
+
strictMode: false,
|
|
8
|
+
liveAnnouncedStrict: void 0
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function sharedState() {
|
|
12
|
+
const scope = globalThis;
|
|
13
|
+
return scope[KEY] ?? (scope[KEY] = initialState());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/core/utils.ts
|
|
17
|
+
function setTrustedParentOrigin(origin) {
|
|
18
|
+
sharedState().trustedParentOrigin = origin;
|
|
19
|
+
}
|
|
20
|
+
function getTrustedParentOrigin() {
|
|
21
|
+
return sharedState().trustedParentOrigin;
|
|
22
|
+
}
|
|
23
|
+
function sendPreprEvent(event, data, options) {
|
|
24
|
+
if (typeof window !== "undefined") {
|
|
25
|
+
const message = {
|
|
26
|
+
name: "prepr_preview_bar",
|
|
27
|
+
event,
|
|
28
|
+
...data
|
|
29
|
+
};
|
|
30
|
+
window.dispatchEvent(
|
|
31
|
+
new CustomEvent("prepr_preview_bar", { detail: message })
|
|
32
|
+
);
|
|
33
|
+
if (window.parent && window.parent !== window) {
|
|
34
|
+
const trustedParentOrigin = getTrustedParentOrigin();
|
|
35
|
+
if (trustedParentOrigin) {
|
|
36
|
+
window.parent.postMessage(message, trustedParentOrigin);
|
|
37
|
+
} else if (options?.allowUntrustedTarget) {
|
|
38
|
+
window.parent.postMessage(message, "*");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
var DebugLogger = class {
|
|
44
|
+
constructor(options) {
|
|
45
|
+
this.options = {
|
|
46
|
+
prefix: "[Prepr]",
|
|
47
|
+
...options
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
// A local `enabled` wins; otherwise defer to the shared flag so scoped
|
|
51
|
+
// loggers created before initDebugLogger() still pick up the setting.
|
|
52
|
+
isEnabled() {
|
|
53
|
+
if (this.options.enabled !== void 0) {
|
|
54
|
+
return this.options.enabled;
|
|
55
|
+
}
|
|
56
|
+
return sharedState().debugEnabled ?? false;
|
|
57
|
+
}
|
|
58
|
+
log(message, ...args) {
|
|
59
|
+
if (!this.isEnabled()) return;
|
|
60
|
+
const prefix = this.options.prefix;
|
|
61
|
+
console.log(`${prefix} ${message}`, ...args);
|
|
62
|
+
}
|
|
63
|
+
warn(message, ...args) {
|
|
64
|
+
if (!this.isEnabled()) return;
|
|
65
|
+
const prefix = this.options.prefix;
|
|
66
|
+
console.warn(`${prefix} ${message}`, ...args);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function initDebugLogger(enabled = false) {
|
|
70
|
+
sharedState().debugEnabled = enabled;
|
|
71
|
+
}
|
|
72
|
+
function createScopedLogger(scopeName) {
|
|
73
|
+
return new DebugLogger({
|
|
74
|
+
prefix: `[Prepr][${scopeName}]`
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export {
|
|
79
|
+
sharedState,
|
|
80
|
+
setTrustedParentOrigin,
|
|
81
|
+
getTrustedParentOrigin,
|
|
82
|
+
sendPreprEvent,
|
|
83
|
+
initDebugLogger,
|
|
84
|
+
createScopedLogger
|
|
85
|
+
};
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EVENT_ALLOW_NAVIGATION,
|
|
3
|
+
EVENT_GET_SCROLL_POSITION,
|
|
4
|
+
EVENT_INIT,
|
|
5
|
+
EVENT_ITEM_CHANGED,
|
|
6
|
+
EVENT_REFERENCES,
|
|
7
|
+
EVENT_SCROLL_TO_FIELD
|
|
8
|
+
} from "./chunk-UTKH7DOI.js";
|
|
9
|
+
import {
|
|
10
|
+
createScopedLogger,
|
|
11
|
+
sendPreprEvent,
|
|
12
|
+
setTrustedParentOrigin
|
|
13
|
+
} from "./chunk-V7MCHPE7.js";
|
|
14
|
+
|
|
15
|
+
// src/core/locales/de.json
|
|
16
|
+
var de_default = {
|
|
17
|
+
common: {
|
|
18
|
+
viewingAs: "Ansicht als:",
|
|
19
|
+
user: "Benutzer",
|
|
20
|
+
off: "Aus",
|
|
21
|
+
on: "An",
|
|
22
|
+
reset: "Zur\xFCcksetzen",
|
|
23
|
+
toolbar: "Symbolleiste"
|
|
24
|
+
},
|
|
25
|
+
adaptiveContent: {
|
|
26
|
+
adaptiveContent: "Adaptive Inhalte",
|
|
27
|
+
enablePreview: "Vorschaumodus",
|
|
28
|
+
segment: "Segment",
|
|
29
|
+
ABVariant: "A/B-Variante",
|
|
30
|
+
chooseSegment: "Segment w\xE4hlen",
|
|
31
|
+
searchSegments: "Segmente suchen",
|
|
32
|
+
none: "Keine Segmente",
|
|
33
|
+
allOtherUsers: "Alle anderen Benutzer",
|
|
34
|
+
offDescription: "Die Website als echter Benutzer ansehen (Cookies verwenden)",
|
|
35
|
+
onDescription: "Mit der Symbolleiste Segmente und A/B-Varianten simulieren"
|
|
36
|
+
},
|
|
37
|
+
editingTools: {
|
|
38
|
+
editingTools: "Bearbeitungswerkzeuge",
|
|
39
|
+
editMode: "Bearbeitungsmodus",
|
|
40
|
+
ariaCloseEditMode: "Bearbeitungsmodus schlie\xDFen"
|
|
41
|
+
},
|
|
42
|
+
visualEditor: {
|
|
43
|
+
edit: "Bearbeiten",
|
|
44
|
+
otherItem: "anderes Element",
|
|
45
|
+
linkedItem: "verkn\xFCpftes Element"
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// src/core/locales/en.json
|
|
50
|
+
var en_default = {
|
|
51
|
+
common: {
|
|
52
|
+
viewingAs: "Viewing as:",
|
|
53
|
+
user: "User",
|
|
54
|
+
off: "Off",
|
|
55
|
+
on: "On",
|
|
56
|
+
reset: "Reset",
|
|
57
|
+
toolbar: "Toolbar"
|
|
58
|
+
},
|
|
59
|
+
adaptiveContent: {
|
|
60
|
+
adaptiveContent: "Adaptive content",
|
|
61
|
+
enablePreview: "Preview mode",
|
|
62
|
+
segment: "Segment",
|
|
63
|
+
ABVariant: "A/B variant",
|
|
64
|
+
chooseSegment: "Choose segment",
|
|
65
|
+
searchSegments: "Search segments",
|
|
66
|
+
none: "No segments",
|
|
67
|
+
allOtherUsers: "All other users",
|
|
68
|
+
offDescription: "View the site as a real user (use cookies)",
|
|
69
|
+
onDescription: "Use the toolbar to simulate segments and A/B variants"
|
|
70
|
+
},
|
|
71
|
+
editingTools: {
|
|
72
|
+
editingTools: "Editing tools",
|
|
73
|
+
editMode: "Edit mode",
|
|
74
|
+
ariaCloseEditMode: "Close edit mode"
|
|
75
|
+
},
|
|
76
|
+
visualEditor: {
|
|
77
|
+
edit: "Edit",
|
|
78
|
+
otherItem: "other item",
|
|
79
|
+
linkedItem: "linked item"
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/core/locales/fr.json
|
|
84
|
+
var fr_default = {
|
|
85
|
+
common: {
|
|
86
|
+
viewingAs: "Affichage en tant que :",
|
|
87
|
+
user: "Utilisateur",
|
|
88
|
+
off: "D\xE9sactiv\xE9",
|
|
89
|
+
on: "Activ\xE9",
|
|
90
|
+
reset: "R\xE9initialiser",
|
|
91
|
+
toolbar: "Barre d'outils"
|
|
92
|
+
},
|
|
93
|
+
adaptiveContent: {
|
|
94
|
+
adaptiveContent: "Contenu adaptatif",
|
|
95
|
+
enablePreview: "Mode aper\xE7u",
|
|
96
|
+
segment: "Segment",
|
|
97
|
+
ABVariant: "Variante A/B",
|
|
98
|
+
chooseSegment: "Choisir un segment",
|
|
99
|
+
searchSegments: "Rechercher des segments",
|
|
100
|
+
none: "Aucun segment",
|
|
101
|
+
allOtherUsers: "Tous les autres utilisateurs",
|
|
102
|
+
offDescription: "Voir le site comme un vrai utilisateur (utiliser les cookies)",
|
|
103
|
+
onDescription: "Utilisez la barre d'outils pour simuler des segments et des variantes A/B"
|
|
104
|
+
},
|
|
105
|
+
editingTools: {
|
|
106
|
+
editingTools: "Outils d'\xE9dition",
|
|
107
|
+
editMode: "Mode \xE9dition",
|
|
108
|
+
ariaCloseEditMode: "Fermer le mode \xE9dition"
|
|
109
|
+
},
|
|
110
|
+
visualEditor: {
|
|
111
|
+
edit: "Modifier",
|
|
112
|
+
otherItem: "autre \xE9l\xE9ment",
|
|
113
|
+
linkedItem: "\xE9l\xE9ment li\xE9"
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/core/locales/nl.json
|
|
118
|
+
var nl_default = {
|
|
119
|
+
common: {
|
|
120
|
+
viewingAs: "Bekijken als:",
|
|
121
|
+
user: "Gebruiker",
|
|
122
|
+
off: "Uit",
|
|
123
|
+
on: "Aan",
|
|
124
|
+
reset: "Resetten",
|
|
125
|
+
toolbar: "Toolbar"
|
|
126
|
+
},
|
|
127
|
+
adaptiveContent: {
|
|
128
|
+
adaptiveContent: "Adaptieve inhoud",
|
|
129
|
+
enablePreview: "Preview modus",
|
|
130
|
+
segment: "Segment",
|
|
131
|
+
ABVariant: "A/B-variant",
|
|
132
|
+
chooseSegment: "Kies segment",
|
|
133
|
+
searchSegments: "Zoek segmenten",
|
|
134
|
+
none: "Geen segmenten",
|
|
135
|
+
allOtherUsers: "Alle andere gebruikers",
|
|
136
|
+
offDescription: "Bekijk de site als een echte gebruiker (gebruik cookies)",
|
|
137
|
+
onDescription: "Gebruik de werkbalk om segmenten en A/B-varianten te simuleren"
|
|
138
|
+
},
|
|
139
|
+
editingTools: {
|
|
140
|
+
editingTools: "Editing Tools",
|
|
141
|
+
editMode: "Edit mode",
|
|
142
|
+
ariaCloseEditMode: "Edit mode sluiten"
|
|
143
|
+
},
|
|
144
|
+
visualEditor: {
|
|
145
|
+
edit: "Bewerken",
|
|
146
|
+
otherItem: "ander item",
|
|
147
|
+
linkedItem: "gekoppeld item"
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// src/core/i18n.ts
|
|
152
|
+
var dictionaries = {
|
|
153
|
+
en: en_default,
|
|
154
|
+
nl: nl_default,
|
|
155
|
+
fr: fr_default,
|
|
156
|
+
de: de_default
|
|
157
|
+
};
|
|
158
|
+
var LOCALES = Object.keys(dictionaries);
|
|
159
|
+
function isLocale(value) {
|
|
160
|
+
return typeof value === "string" && value in dictionaries;
|
|
161
|
+
}
|
|
162
|
+
function getDict(locale) {
|
|
163
|
+
return isLocale(locale) ? dictionaries[locale] : dictionaries.en;
|
|
164
|
+
}
|
|
165
|
+
function getFromPath(obj, path) {
|
|
166
|
+
return path.split(".").reduce(
|
|
167
|
+
(acc, key) => acc && typeof acc === "object" ? acc[key] : void 0,
|
|
168
|
+
obj
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
function format(message, vars) {
|
|
172
|
+
if (!vars) return message;
|
|
173
|
+
return Object.keys(vars).reduce(
|
|
174
|
+
(acc, k) => acc.replace(new RegExp(`\\{${k}\\}`, "g"), String(vars[k])),
|
|
175
|
+
message
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
function t(key, locale, vars) {
|
|
179
|
+
const dict = getDict(locale);
|
|
180
|
+
const msg = getFromPath(dict, key);
|
|
181
|
+
if (typeof msg === "string") return format(msg, vars);
|
|
182
|
+
return key;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// src/core/iframe-bridge.ts
|
|
186
|
+
var debug = createScopedLogger("iframe-bridge");
|
|
187
|
+
var MAX_REFERENCES = 500;
|
|
188
|
+
var MAX_HREF_LENGTH = 8192;
|
|
189
|
+
var MAX_ASSETS = 500;
|
|
190
|
+
var MAX_ASSET_KEY_LENGTH = 2048;
|
|
191
|
+
var MAX_ASSET_FIELD_LENGTH = 512;
|
|
192
|
+
function parseIds(value) {
|
|
193
|
+
if (!Array.isArray(value)) return void 0;
|
|
194
|
+
return value.slice(0, MAX_REFERENCES).filter((id) => typeof id === "string" && id.length > 0);
|
|
195
|
+
}
|
|
196
|
+
function parseReferences(value) {
|
|
197
|
+
if (!Array.isArray(value)) return void 0;
|
|
198
|
+
const list = [];
|
|
199
|
+
for (const entry of value.slice(0, MAX_REFERENCES)) {
|
|
200
|
+
const id = entry?.id;
|
|
201
|
+
const field = entry?.field;
|
|
202
|
+
if (typeof id === "string" && id && typeof field === "string" && field) {
|
|
203
|
+
list.push({ id, field });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return list;
|
|
207
|
+
}
|
|
208
|
+
function boundedString(value, max) {
|
|
209
|
+
return typeof value === "string" && value && value.length <= max ? value : void 0;
|
|
210
|
+
}
|
|
211
|
+
function parseAssets(value) {
|
|
212
|
+
if (!Array.isArray(value)) return void 0;
|
|
213
|
+
const list = [];
|
|
214
|
+
for (const entry of value.slice(0, MAX_ASSETS)) {
|
|
215
|
+
if (!entry || typeof entry !== "object") continue;
|
|
216
|
+
const raw = entry;
|
|
217
|
+
const key = boundedString(raw.key, MAX_ASSET_KEY_LENGTH);
|
|
218
|
+
const field = boundedString(raw.field, MAX_ASSET_FIELD_LENGTH);
|
|
219
|
+
if (!key || !field) continue;
|
|
220
|
+
const entryId = boundedString(raw.entryId, MAX_ASSET_FIELD_LENGTH);
|
|
221
|
+
list.push(entryId ? { key, field, entryId } : { key, field });
|
|
222
|
+
}
|
|
223
|
+
return list;
|
|
224
|
+
}
|
|
225
|
+
var EDITOR_BASE_DOMAIN = "prepr.io";
|
|
226
|
+
function isAllowedEditorOrigin(origin, allowed) {
|
|
227
|
+
if (allowed) {
|
|
228
|
+
if (allowed.includes("*")) return origin !== "null";
|
|
229
|
+
return allowed.includes(origin);
|
|
230
|
+
}
|
|
231
|
+
let url;
|
|
232
|
+
try {
|
|
233
|
+
url = new URL(origin);
|
|
234
|
+
} catch {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
if (url.protocol !== "https:" || url.port !== "") return false;
|
|
238
|
+
const suffix = `.${EDITOR_BASE_DOMAIN}`;
|
|
239
|
+
if (!url.hostname.endsWith(suffix)) return false;
|
|
240
|
+
const label = url.hostname.slice(0, -suffix.length);
|
|
241
|
+
return label.length > 0 && !label.includes(".");
|
|
242
|
+
}
|
|
243
|
+
function createIframeBridge(store, options = {}) {
|
|
244
|
+
const allowedOrigins = options.allowedEditorOrigins;
|
|
245
|
+
const onScrollToField = options.onScrollToField;
|
|
246
|
+
const onHandshake = options.onHandshake;
|
|
247
|
+
const onReferences = options.onReferences;
|
|
248
|
+
const onAssets = options.onAssets;
|
|
249
|
+
const onAllowNavigation = options.onAllowNavigation;
|
|
250
|
+
const onItemChanged = options.onItemChanged;
|
|
251
|
+
let parentOrigin = null;
|
|
252
|
+
const onKeyDown = (event) => {
|
|
253
|
+
const key = event.key.toLowerCase();
|
|
254
|
+
const blocked = (event.ctrlKey || event.metaKey) && ["s", "p", "l"].includes(key);
|
|
255
|
+
if (blocked) event.preventDefault();
|
|
256
|
+
};
|
|
257
|
+
const onMessage = (evt) => {
|
|
258
|
+
if (evt.source !== window.parent) return;
|
|
259
|
+
const data = evt?.data;
|
|
260
|
+
if (data?.event === EVENT_INIT && !parentOrigin) {
|
|
261
|
+
if (!isAllowedEditorOrigin(evt.origin, allowedOrigins)) {
|
|
262
|
+
debug.warn("ignored editor handshake from origin", evt.origin);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
parentOrigin = evt.origin;
|
|
266
|
+
setTrustedParentOrigin(parentOrigin);
|
|
267
|
+
if (data.scrollPosition != null) {
|
|
268
|
+
const top = data.scrollPosition;
|
|
269
|
+
setTimeout(() => window.scrollTo(0, top), 1);
|
|
270
|
+
}
|
|
271
|
+
store?.set({ previewMode: true, editMode: data.editMode ?? true });
|
|
272
|
+
const itemId = typeof data.itemId === "string" && data.itemId ? data.itemId : void 0;
|
|
273
|
+
const referencedIds = parseReferences(data.referencedIds);
|
|
274
|
+
const componentIds = parseIds(data.componentIds);
|
|
275
|
+
const uiLocale = isLocale(data.uiLocale) ? data.uiLocale : void 0;
|
|
276
|
+
const navigationGuard = data.navigationGuard === true;
|
|
277
|
+
const assets = parseAssets(data.assets);
|
|
278
|
+
try {
|
|
279
|
+
onHandshake?.({
|
|
280
|
+
...itemId && { itemId },
|
|
281
|
+
...referencedIds && { referencedIds },
|
|
282
|
+
...componentIds && { componentIds },
|
|
283
|
+
...uiLocale && { uiLocale },
|
|
284
|
+
...navigationGuard && { navigationGuard },
|
|
285
|
+
...assets && { assets }
|
|
286
|
+
});
|
|
287
|
+
} catch (error) {
|
|
288
|
+
debug.warn("handshake handler failed", error);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (!parentOrigin || evt.origin !== parentOrigin) return;
|
|
292
|
+
if (data?.event === EVENT_REFERENCES) {
|
|
293
|
+
const assets = parseAssets(data.assets);
|
|
294
|
+
if (assets) {
|
|
295
|
+
try {
|
|
296
|
+
onAssets?.(assets);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
debug.warn("assets handler failed", error);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
const referencedIds = parseReferences(data.referencedIds);
|
|
302
|
+
if (!referencedIds) return;
|
|
303
|
+
try {
|
|
304
|
+
onReferences?.(referencedIds, parseIds(data.componentIds));
|
|
305
|
+
} catch (error) {
|
|
306
|
+
debug.warn("references handler failed", error);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (data?.event === EVENT_GET_SCROLL_POSITION) {
|
|
310
|
+
const currentScrollY = window.scrollY || document.documentElement.scrollTop;
|
|
311
|
+
sendPreprEvent("getScrollPosition", { value: currentScrollY });
|
|
312
|
+
}
|
|
313
|
+
if (data?.event === EVENT_SCROLL_TO_FIELD) {
|
|
314
|
+
if (typeof data.field !== "string" || !data.field) return;
|
|
315
|
+
if (data.id !== void 0 && typeof data.id !== "string") return;
|
|
316
|
+
const block = typeof data.block === "number" && Number.isInteger(data.block) ? data.block : void 0;
|
|
317
|
+
const entryId = typeof data.entryId === "string" && data.entryId ? data.entryId : void 0;
|
|
318
|
+
const fieldById = typeof data.fieldById === "string" && data.fieldById ? data.fieldById : void 0;
|
|
319
|
+
try {
|
|
320
|
+
onScrollToField?.({
|
|
321
|
+
field: data.field,
|
|
322
|
+
id: data.id,
|
|
323
|
+
block,
|
|
324
|
+
entryId,
|
|
325
|
+
fieldById
|
|
326
|
+
});
|
|
327
|
+
} catch (error) {
|
|
328
|
+
debug.warn("scrollToField handler failed", error);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (data?.event === EVENT_ALLOW_NAVIGATION) {
|
|
332
|
+
const href = data.href;
|
|
333
|
+
if (typeof href !== "string" || !href || href.length > MAX_HREF_LENGTH) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
try {
|
|
337
|
+
onAllowNavigation?.(href);
|
|
338
|
+
} catch (error) {
|
|
339
|
+
debug.warn("allowNavigation handler failed", error);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (data?.event === EVENT_ITEM_CHANGED) {
|
|
343
|
+
if (typeof data.itemId !== "string" || !data.itemId) return;
|
|
344
|
+
try {
|
|
345
|
+
onItemChanged?.(data.itemId);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
debug.warn("itemChanged handler failed", error);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
return {
|
|
352
|
+
start() {
|
|
353
|
+
const features = store?.get().features;
|
|
354
|
+
sendPreprEvent("loaded", features ? { features } : void 0, {
|
|
355
|
+
allowUntrustedTarget: true
|
|
356
|
+
});
|
|
357
|
+
window.addEventListener("keydown", onKeyDown);
|
|
358
|
+
window.addEventListener("message", onMessage);
|
|
359
|
+
},
|
|
360
|
+
stop() {
|
|
361
|
+
window.removeEventListener("keydown", onKeyDown);
|
|
362
|
+
window.removeEventListener("message", onMessage);
|
|
363
|
+
parentOrigin = null;
|
|
364
|
+
setTrustedParentOrigin(null);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export {
|
|
370
|
+
isLocale,
|
|
371
|
+
t,
|
|
372
|
+
isAllowedEditorOrigin,
|
|
373
|
+
createIframeBridge
|
|
374
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/codegen/index.ts
|
|
21
|
+
var codegen_exports = {};
|
|
22
|
+
__export(codegen_exports, {
|
|
23
|
+
addPreprIds: () => addPreprIds,
|
|
24
|
+
createPreprIds: () => createPreprIds,
|
|
25
|
+
preprIds: () => preprIds
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(codegen_exports);
|
|
28
|
+
var import_graphql = require("graphql");
|
|
29
|
+
function field(name) {
|
|
30
|
+
return { kind: import_graphql.Kind.FIELD, name: { kind: import_graphql.Kind.NAME, value: name } };
|
|
31
|
+
}
|
|
32
|
+
function hasResponseKey(set, key) {
|
|
33
|
+
return set.selections.some(
|
|
34
|
+
(s) => s.kind === import_graphql.Kind.FIELD && (s.alias ?? s.name).value === key
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
function addPreprIds(document, schema, options = {}) {
|
|
38
|
+
const typename = options.typename ?? false;
|
|
39
|
+
const roots = new Set(
|
|
40
|
+
[
|
|
41
|
+
schema.getQueryType(),
|
|
42
|
+
schema.getMutationType(),
|
|
43
|
+
schema.getSubscriptionType()
|
|
44
|
+
].filter(Boolean)
|
|
45
|
+
);
|
|
46
|
+
const typeInfo = new import_graphql.TypeInfo(schema);
|
|
47
|
+
return (0, import_graphql.visit)(
|
|
48
|
+
document,
|
|
49
|
+
(0, import_graphql.visitWithTypeInfo)(typeInfo, {
|
|
50
|
+
SelectionSet(node) {
|
|
51
|
+
const type = typeInfo.getParentType();
|
|
52
|
+
if (!type || !(0, import_graphql.isCompositeType)(type) || roots.has(type)) return;
|
|
53
|
+
const added = [];
|
|
54
|
+
if (((0, import_graphql.isObjectType)(type) || (0, import_graphql.isInterfaceType)(type)) && "_id" in type.getFields() && !hasResponseKey(node, "_id")) {
|
|
55
|
+
added.push(field("_id"));
|
|
56
|
+
}
|
|
57
|
+
if (typename && !hasResponseKey(node, "__typename")) {
|
|
58
|
+
added.push(field("__typename"));
|
|
59
|
+
}
|
|
60
|
+
if (!added.length) return;
|
|
61
|
+
return { ...node, selections: [...added, ...node.selections] };
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
function createPreprIds(options = {}) {
|
|
67
|
+
return {
|
|
68
|
+
transform({ documents, schema }) {
|
|
69
|
+
const built = (0, import_graphql.buildASTSchema)(schema, { assumeValidSDL: true });
|
|
70
|
+
return documents.map(
|
|
71
|
+
(file) => file.document ? { ...file, document: addPreprIds(file.document, built, options) } : file
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
var preprIds = createPreprIds();
|
|
77
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
78
|
+
0 && (module.exports = {
|
|
79
|
+
addPreprIds,
|
|
80
|
+
createPreprIds,
|
|
81
|
+
preprIds
|
|
82
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { DocumentNode, GraphQLSchema } from 'graphql';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The slice of a GraphQL Codegen `DocumentFile` this transform touches,
|
|
5
|
+
* written out so the package needs no dependency on `@graphql-codegen/*`.
|
|
6
|
+
*/
|
|
7
|
+
interface CodegenDocumentFile {
|
|
8
|
+
document?: DocumentNode;
|
|
9
|
+
}
|
|
10
|
+
interface PreprIdsOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Also select `__typename` on every object, interface and union below the
|
|
13
|
+
* root. Default `false`: a selected `__typename` changes the types Codegen
|
|
14
|
+
* generates (each union member is typed separately, and objects built by
|
|
15
|
+
* hand must carry one), which can break code that compiles today. Live
|
|
16
|
+
* preview only needs it on dynamic content entries, where you select it
|
|
17
|
+
* already to render them.
|
|
18
|
+
*/
|
|
19
|
+
typename?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Select `_id` on every level of a query that has one, so live preview and
|
|
23
|
+
* click-to-edit can find each item without you selecting it by hand.
|
|
24
|
+
*
|
|
25
|
+
* The schema decides where it goes: only on types that have an `_id`
|
|
26
|
+
* (models and components, not collections like `Pages` or unions; inside a
|
|
27
|
+
* union, on each member). Adding a field never breaks the types Codegen
|
|
28
|
+
* generates. With `typename: true`, `__typename` is added on every object,
|
|
29
|
+
* interface and union below the root too. Fields already selected are left
|
|
30
|
+
* as they are.
|
|
31
|
+
*/
|
|
32
|
+
declare function addPreprIds(document: DocumentNode, schema: GraphQLSchema, options?: PreprIdsOptions): DocumentNode;
|
|
33
|
+
/** A GraphQL Codegen document transform. */
|
|
34
|
+
interface CodegenDocumentTransform {
|
|
35
|
+
transform<File extends CodegenDocumentFile>(options: {
|
|
36
|
+
documents: File[];
|
|
37
|
+
schema: DocumentNode;
|
|
38
|
+
}): File[];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* GraphQL Codegen document transform that runs `addPreprIds` on every
|
|
42
|
+
* document, with options:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* documentTransforms: [createPreprIds({ typename: true })],
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function createPreprIds(options?: PreprIdsOptions): CodegenDocumentTransform;
|
|
49
|
+
/**
|
|
50
|
+
* GraphQL Codegen document transform that runs `addPreprIds` on every
|
|
51
|
+
* document, so the generated documents and types include `_id` wherever the
|
|
52
|
+
* schema has one:
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* // codegen.ts
|
|
56
|
+
* import { preprIds } from '@preprio/toolkit/codegen';
|
|
57
|
+
*
|
|
58
|
+
* generates: {
|
|
59
|
+
* 'src/gql/': {
|
|
60
|
+
* preset: 'client',
|
|
61
|
+
* documentTransforms: [preprIds],
|
|
62
|
+
* },
|
|
63
|
+
* },
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* `createPreprIds({ typename: true })` adds `__typename` as well.
|
|
67
|
+
*/
|
|
68
|
+
declare const preprIds: CodegenDocumentTransform;
|
|
69
|
+
|
|
70
|
+
export { type CodegenDocumentFile, type CodegenDocumentTransform, type PreprIdsOptions, addPreprIds, createPreprIds, preprIds };
|