@sitecore-feaas/clientside 0.1.53 → 0.1.55
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 +41 -7
- package/build/components/FEAASBase.js +12 -9
- package/build/components/FEAASBase.js.map +1 -1
- package/build/components/FEAASComponent.js +3 -2
- package/build/components/FEAASComponent.js.map +1 -1
- package/build/components/FEAASElement.js +2 -1
- package/build/components/FEAASElement.js.map +1 -1
- package/build/components/FEAASLoader.js +3 -2
- package/build/components/FEAASLoader.js.map +1 -1
- package/build/components/FEAASStylesheet.js +1 -1
- package/build/components/FEAASStylesheet.js.map +1 -1
- package/build/dom/data.js +0 -6
- package/build/dom/data.js.map +1 -1
- package/build/dom/html.js +7 -0
- package/build/dom/html.js.map +1 -1
- package/build/dom/rendering.js +8 -5
- package/build/dom/rendering.js.map +1 -1
- package/build/dom/thumbnail.js +247 -0
- package/build/dom/thumbnail.js.map +1 -0
- package/build/fetch.js +4 -2
- package/build/fetch.js.map +1 -1
- package/build/headless.js +2 -0
- package/build/headless.js.map +1 -1
- package/build/ui/FEAASContext.js +10 -4
- package/build/ui/FEAASContext.js.map +1 -1
- package/build/ui/FEAASEditor.js +7 -6
- package/build/ui/FEAASEditor.js.map +1 -1
- package/build/ui/FEAASExternal.js +12 -10
- package/build/ui/FEAASExternal.js.map +1 -1
- package/build/ui/FEAASPicker.js +10 -4
- package/build/ui/FEAASPicker.js.map +1 -1
- package/build/utils/datapath.js +29 -0
- package/build/utils/datapath.js.map +1 -0
- package/build/utils/promise.js +27 -0
- package/build/utils/promise.js.map +1 -1
- package/dist/browser/headless.cjs +8 -5
- package/dist/browser/headless.cjs.map +4 -4
- package/dist/browser/headless.esm.js +8 -5
- package/dist/browser/headless.esm.js.map +1 -1
- package/dist/browser/index.cjs +8 -5
- package/dist/browser/index.cjs.map +4 -4
- package/dist/browser/index.esm.js +8 -5
- package/dist/browser/index.esm.js.map +1 -1
- package/dist/browser/react.cjs +8 -5
- package/dist/browser/react.cjs.map +4 -4
- package/dist/browser/react.esm.js +8 -5
- package/dist/browser/react.esm.js.map +1 -1
- package/dist/node/headless.cjs +6 -3
- package/dist/node/headless.cjs.map +4 -4
- package/dist/node/headless.esm.js +6 -3
- package/dist/node/headless.esm.js.map +4 -4
- package/dist/node/index.cjs +6 -3
- package/dist/node/index.cjs.map +4 -4
- package/dist/node/index.esm.js +6 -3
- package/dist/node/index.esm.js.map +4 -4
- package/dist/node/react.cjs +6 -3
- package/dist/node/react.cjs.map +4 -4
- package/dist/node/react.esm.js +6 -3
- package/dist/node/react.esm.js.map +4 -4
- package/headless.cjs +6 -3
- package/headless.cjs.map +4 -4
- package/headless.js +6 -3
- package/headless.js.map +4 -4
- package/package.json +2 -1
- package/react.cjs +8 -5
- package/react.cjs.map +4 -4
- package/react.js +8 -5
- package/react.js.map +4 -4
- package/src/components/FEAASBase.tsx +12 -10
- package/src/components/FEAASComponent.tsx +1 -1
- package/src/components/FEAASLoader.tsx +1 -1
- package/src/components/FEAASStylesheet.tsx +1 -1
- package/src/dom/data.ts +0 -8
- package/src/dom/html.ts +8 -0
- package/src/dom/rendering.ts +9 -4
- package/src/dom/thumbnail.ts +293 -0
- package/src/fetch.ts +5 -3
- package/src/headless.ts +2 -0
- package/src/ui/FEAASContext.tsx +9 -3
- package/src/ui/FEAASEditor.tsx +5 -5
- package/src/ui/FEAASExternal.tsx +10 -10
- package/src/ui/FEAASPicker.tsx +10 -3
- package/src/utils/datapath.ts +31 -0
- package/src/utils/promise.ts +29 -2
- package/test/FEAASComponent.react.test.tsx +3 -6
- package/test/FEAASComponent.test.tsx +2 -1
- package/test/FEAASExternal.test.tsx +50 -4
- package/test/cjs/tsconfig.json +9 -0
- package/test/fetch.ts +1 -1
- package/types/components/FEAASBase.d.ts +12 -9
- package/types/components/FEAASBase.d.ts.map +1 -1
- package/types/components/FEAASComponent.d.ts +1 -1
- package/types/components/FEAASComponent.d.ts.map +1 -1
- package/types/components/FEAASLoader.d.ts +1 -1
- package/types/components/FEAASLoader.d.ts.map +1 -1
- package/types/components/FEAASStylesheet.d.ts +1 -1
- package/types/components/FEAASStylesheet.d.ts.map +1 -1
- package/types/dom/data.d.ts +0 -2
- package/types/dom/data.d.ts.map +1 -1
- package/types/dom/html.d.ts +2 -0
- package/types/dom/html.d.ts.map +1 -1
- package/types/dom/rendering.d.ts.map +1 -1
- package/types/dom/thumbnail.d.ts +82 -0
- package/types/dom/thumbnail.d.ts.map +1 -0
- package/types/fetch.d.ts +6 -4
- package/types/fetch.d.ts.map +1 -1
- package/types/headless.d.ts +2 -0
- package/types/headless.d.ts.map +1 -1
- package/types/tsbuildinfo +1 -1
- package/types/tsbuildinfo_ui +1 -1
- package/types/ui/FEAASContext.d.ts +9 -5
- package/types/ui/FEAASContext.d.ts.map +1 -1
- package/types/ui/FEAASEditor.d.ts +2 -2
- package/types/ui/FEAASEditor.d.ts.map +1 -1
- package/types/ui/FEAASExternal.d.ts +2 -2
- package/types/ui/FEAASExternal.d.ts.map +1 -1
- package/types/ui/FEAASPicker.d.ts +5 -5
- package/types/ui/FEAASPicker.d.ts.map +1 -1
- package/types/utils/datapath.d.ts +6 -0
- package/types/utils/datapath.d.ts.map +1 -0
- package/types/utils/promise.d.ts +28 -1
- package/types/utils/promise.d.ts.map +1 -1
|
@@ -27,15 +27,18 @@ export interface ReactPayload {
|
|
|
27
27
|
* Initialization lifecycle:
|
|
28
28
|
*
|
|
29
29
|
* - `set(props)`
|
|
30
|
-
* - `
|
|
31
|
-
* - `
|
|
32
|
-
* - `
|
|
33
|
-
* - `
|
|
34
|
-
* - `
|
|
35
|
-
* -
|
|
36
|
-
* -
|
|
37
|
-
* -
|
|
38
|
-
* - `
|
|
30
|
+
* - `scheduleRender()` - any attribute change causes render to schedule to next frame
|
|
31
|
+
* - `whenRendered.restart()` - rendering promise is reset and will resolve after render
|
|
32
|
+
* - `advance()` - called on next frame
|
|
33
|
+
* - `if (isReadyToLoad()) => boolean` - check if component has all necessary properties to load
|
|
34
|
+
* - `load() => payload` => component is loaded
|
|
35
|
+
* - `onLoad?(payload)` => invoke loaded callback
|
|
36
|
+
* - `advance()` => going further in state machine
|
|
37
|
+
* - `flush()` => flushes all react changes if needed
|
|
38
|
+
* - `update()` => component-specific method (or react) to render changes to dom
|
|
39
|
+
* - `render(payload, props)` => render means react
|
|
40
|
+
* - `whenRendered.resolve()` - rendering promise is resolved after rendering
|
|
41
|
+
* - `whenLoaded.resolved()` - resolved after loading and rendering
|
|
39
42
|
*/
|
|
40
43
|
export class FEAASBase<Props = any, Payload = any> extends WebComponent {
|
|
41
44
|
static observedAttributes: string[]
|
|
@@ -89,7 +92,6 @@ export class FEAASBase<Props = any, Payload = any> extends WebComponent {
|
|
|
89
92
|
disconnectedCallback() {
|
|
90
93
|
this.unmount()
|
|
91
94
|
}
|
|
92
|
-
|
|
93
95
|
/**
|
|
94
96
|
* Web components are initialized depth-first, making child elements render before its parents. This could here
|
|
95
97
|
* attempts to avoid this and inverts the initialization logic, so that children are initialized in the context of its
|
|
@@ -3,7 +3,7 @@ import { renderDOMContent, renderDOMElement } from '../dom/rendering.js'
|
|
|
3
3
|
import { loadStylesheetAllowStale } from '../headless.js'
|
|
4
4
|
import { FEAASBase } from './FEAASBase.js'
|
|
5
5
|
|
|
6
|
-
export function FEAASComponentsProps(element: FEAASBase
|
|
6
|
+
export function FEAASComponentsProps(element: FEAASBase) {
|
|
7
7
|
let data: any
|
|
8
8
|
const src = element.getAttribute('src')
|
|
9
9
|
if (src) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FEAASBase } from './FEAASBase.js'
|
|
2
2
|
|
|
3
|
-
export function FEAASLoaderProps(element: HTMLElement
|
|
3
|
+
export function FEAASLoaderProps(element: HTMLElement) {
|
|
4
4
|
return {
|
|
5
5
|
opacity: element?.getAttribute('opacity') ? parseFloat(element?.getAttribute('opacity')) : 1,
|
|
6
6
|
hidden: element?.getAttribute('hidden') != null
|
|
@@ -2,7 +2,7 @@ import { DEFAULT_CDN_URL, FEAASCDNStylesheetParams, parseStylesheetSource } from
|
|
|
2
2
|
import { loadStylesheet } from '../dom/stylesheet.js'
|
|
3
3
|
import { FEAASBase } from './FEAASBase.js'
|
|
4
4
|
|
|
5
|
-
export function FEAASStylesheetProps(element: FEAASBase
|
|
5
|
+
export function FEAASStylesheetProps(element: FEAASBase) {
|
|
6
6
|
const src = element.getAttribute('src')
|
|
7
7
|
if (src) {
|
|
8
8
|
var { cdn, library, revision } = parseStylesheetSource(src)
|
package/src/dom/data.ts
CHANGED
|
@@ -2,14 +2,6 @@ import { JSONPath } from 'jsonpath-plus'
|
|
|
2
2
|
|
|
3
3
|
export type Datascopes = Record<string, any> | any[]
|
|
4
4
|
|
|
5
|
-
export function cleanCollectionBit(path: string) {
|
|
6
|
-
return path.replace(/(\[[^\]\[]+\]|\.\*)$/g, '')
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function normalizeCollectionScope(path: string) {
|
|
10
|
-
return cleanCollectionBit(path) + '.*'
|
|
11
|
-
}
|
|
12
|
-
|
|
13
5
|
export function queryScopes(scopes: any, datapath: string, singleValue = false, json: any = scopes) {
|
|
14
6
|
var path = datapath
|
|
15
7
|
/* find closest parent scope */
|
package/src/dom/html.ts
CHANGED
|
@@ -62,6 +62,11 @@ class HTMLNode {
|
|
|
62
62
|
get tagName() {
|
|
63
63
|
return this.nodeName?.toUpperCase()
|
|
64
64
|
}
|
|
65
|
+
|
|
66
|
+
get localName() {
|
|
67
|
+
return this.nodeName?.toLowerCase()
|
|
68
|
+
}
|
|
69
|
+
|
|
65
70
|
get innerHTML() {
|
|
66
71
|
return serializeHTML(this.childNodes)
|
|
67
72
|
}
|
|
@@ -71,6 +76,9 @@ class HTMLNode {
|
|
|
71
76
|
get outerHTML() {
|
|
72
77
|
return serializeHTML([this])
|
|
73
78
|
}
|
|
79
|
+
get children() {
|
|
80
|
+
return this.childNodes.filter((c) => c.nodeType == 1)
|
|
81
|
+
}
|
|
74
82
|
|
|
75
83
|
closest(selector: string): HTMLNode | null {
|
|
76
84
|
let currentNode: HTMLNode | undefined = this
|
package/src/dom/rendering.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { FEAASCDNComponentProps } from '../cdn.js'
|
|
2
|
-
import { Datascopes,
|
|
2
|
+
import { Datascopes, queryScopes, serializeCH1RichText } from './data.js'
|
|
3
3
|
import { parseHTML } from './html.js'
|
|
4
4
|
import { loadScript } from './scripts.js'
|
|
5
|
+
import { normalizeCollectionScope } from '../utils/datapath.js'
|
|
5
6
|
|
|
6
7
|
export interface FEAASCustomizations {
|
|
7
8
|
processTemplate?: (element: HTMLElement, template: string, data: any) => string
|
|
@@ -81,11 +82,13 @@ export function setDOMAttribute(element: HTMLElement, attribute: string, value:
|
|
|
81
82
|
}
|
|
82
83
|
var prop = attribute.replace('data-path-', '')
|
|
83
84
|
if (attribute == 'data-path-src') {
|
|
84
|
-
|
|
85
|
+
const picture = Array.from(element.children).find((n) => n.localName == 'picture') || element
|
|
86
|
+
target = element.localName == 'img' ? element : <HTMLElement>picture.querySelector('img, video')
|
|
85
87
|
} else if (attribute == 'data-path-href') {
|
|
86
88
|
target = element.querySelector('a') || element.closest('a')
|
|
87
89
|
} else if (attribute == 'data-path-alt') {
|
|
88
|
-
|
|
90
|
+
const picture = Array.from(element.children).find((n) => n.localName == 'picture') || element
|
|
91
|
+
target = picture.querySelector('img, picture')
|
|
89
92
|
} else if (attribute == 'data-path-hidden') {
|
|
90
93
|
value = !value ? true : null
|
|
91
94
|
} else if (attribute == 'data-path' || attribute == 'data-embed-html' || attribute == 'html') {
|
|
@@ -117,7 +120,9 @@ export function setDOMAttribute(element: HTMLElement, attribute: string, value:
|
|
|
117
120
|
}
|
|
118
121
|
if (!target) return
|
|
119
122
|
|
|
120
|
-
var placeholder = String(
|
|
123
|
+
var placeholder = String(
|
|
124
|
+
(element.getAttribute('data-path-placeholder') || target[prop as keyof HTMLElement]) ?? target.getAttribute(prop)
|
|
125
|
+
)
|
|
121
126
|
|
|
122
127
|
if (value != null) {
|
|
123
128
|
if (prop == 'innerHTML') {
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import type { SDK, Style, VersionModel, ComponentModel } from '@sitecore-feaas/sdk'
|
|
2
|
+
import { fetchAndRevalidate } from '../fetch.js'
|
|
3
|
+
import { renderComponent } from './component.js'
|
|
4
|
+
|
|
5
|
+
export type Callback = (image: HTMLImageElement, isCached: boolean) => void
|
|
6
|
+
|
|
7
|
+
export const convertBase64toBlob = (base64: string) => fetch(base64).then((res) => res.blob())
|
|
8
|
+
|
|
9
|
+
/** Parse fetch response as blob and turn it into HTMLImageElement off thread */
|
|
10
|
+
export const responseToImage = (response: Response): Promise<HTMLImageElement> =>
|
|
11
|
+
response.blob().then((blob) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const img = new Image()
|
|
14
|
+
img.onerror = reject
|
|
15
|
+
img.onload = () => {
|
|
16
|
+
resolve(img)
|
|
17
|
+
}
|
|
18
|
+
img.src = URL.createObjectURL(blob)
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the version of a given component that best fits a specific display width.
|
|
24
|
+
*
|
|
25
|
+
* The function ensures that the version data for the component is available and selects the one that is most suitable
|
|
26
|
+
* for a display width of 800 pixels.
|
|
27
|
+
*
|
|
28
|
+
* @param {ComponentModel} component - The component for which the version needs to be obtained.
|
|
29
|
+
* @returns {Promise<VersionModel>} - A promise that resolves to the most suitable version for a display width of 800
|
|
30
|
+
* pixels.
|
|
31
|
+
*/
|
|
32
|
+
export async function getVersionForThumbnail(component: ComponentModel): Promise<VersionModel> {
|
|
33
|
+
if (!component.versions.length) await component.versions.fetch()
|
|
34
|
+
return component.getVersionForWidth(800)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Ensures an up-to-date thumbnail for a specific component and version.
|
|
39
|
+
*
|
|
40
|
+
* This operation initiates thumbnail load and, if that fails or if file is out of date, a new thumbnail is generated.
|
|
41
|
+
* It can work with a callback, which is designed to potentially fire twice for enhancing user experience. This dual
|
|
42
|
+
* firing allows for an immediate display of a possibly stale cached image while downloading or generating new version
|
|
43
|
+
* in the background.
|
|
44
|
+
*
|
|
45
|
+
* 1. The first call can happen with the stale cached or outdated image.
|
|
46
|
+
* 2. The second call occurs with the up-to-date loaded or freshly generated generated image.
|
|
47
|
+
*
|
|
48
|
+
* @param {ComponentModel} component - Relevant component.
|
|
49
|
+
* @param {VersionModel} [version] - Relevant version (optional, falls back to responsive).
|
|
50
|
+
* @param {Callback} [callback] - Callback to handle the loaded or generated image (optional).
|
|
51
|
+
* @returns {Promise<HTMLImageElement | null>} - The final thumbnail image (either loaded or freshly generated).
|
|
52
|
+
*/
|
|
53
|
+
export async function get(component: ComponentModel, version?: VersionModel, callback?: Callback) {
|
|
54
|
+
try {
|
|
55
|
+
if (!component.isNew) {
|
|
56
|
+
return await load(component, version, callback)
|
|
57
|
+
}
|
|
58
|
+
} catch (e) {
|
|
59
|
+
component.sdk.log(
|
|
60
|
+
`FEAAS: Thumbnail ${component.id}, version: ${
|
|
61
|
+
version?.id || 'responsive'
|
|
62
|
+
}, status: generating ${status}, reason: ${e}`
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
// generate the image
|
|
66
|
+
const img = await generate(component, version)
|
|
67
|
+
callback?.(img, false)
|
|
68
|
+
|
|
69
|
+
// upload base64 image as blob off-thread
|
|
70
|
+
if (img && !component.isNew) {
|
|
71
|
+
convertBase64toBlob(img.src).then((blob) => upload(component, version, blob))
|
|
72
|
+
}
|
|
73
|
+
return img
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Retrieves the thumbnail image for a given component/version combination, with a responsive default if version is not
|
|
78
|
+
* provided.
|
|
79
|
+
*
|
|
80
|
+
* The resulting HTML <img> element will have `width`, `height`, and `src` properties ready for use. Although it can
|
|
81
|
+
* operate as a regular promise that resolves when the thumbnail is loaded, it also supports a callback for an enhanced
|
|
82
|
+
* user experience. This callback may be invoked twice, depending on the state of the cache:
|
|
83
|
+
*
|
|
84
|
+
* 1. The first call occurs if the image was previously cached by the browser, but the cache is stale.
|
|
85
|
+
* 2. The second call happens when the image is updated from a fresh download. By this design, the user gets to see a
|
|
86
|
+
* cached image immediately while the updated image is being downloaded in the background.
|
|
87
|
+
*
|
|
88
|
+
* If the version of a component is known to be newer than remote file, the function will throw `InvalidResponse` error.
|
|
89
|
+
* As a way to handle that error, the `generate` function may be used to produce new thumbnail (@see render).
|
|
90
|
+
*
|
|
91
|
+
* @param {ComponentModel} component - The component to load a thumbnail for.
|
|
92
|
+
* @param {VersionModel} [version] - The version of the component to load a thumbnail for. If not provided, a version
|
|
93
|
+
* suitable for a responsive component is selected.
|
|
94
|
+
* @param {Callback} [callback] - A callback to handle the loaded image. This callback may be invoked twice for the
|
|
95
|
+
* reasons described above.
|
|
96
|
+
* @returns {Promise<HTMLImageElement>} - The thumbnail image for the component/version combination.
|
|
97
|
+
*/
|
|
98
|
+
export async function load(
|
|
99
|
+
component: ComponentModel,
|
|
100
|
+
version?: VersionModel,
|
|
101
|
+
callback?: Callback
|
|
102
|
+
): Promise<HTMLImageElement> {
|
|
103
|
+
const url = version ? version.getThumbnailURL() : component.getThumbnailURL()
|
|
104
|
+
const stylesheet = component.library.stylesheet
|
|
105
|
+
const lastModifiedDate = new Date(Math.max(Number(component.modifiedAt), Number(stylesheet.modifiedAt)))
|
|
106
|
+
component.sdk.log(`FEAAS: Thumbnail ${component.id}/${version?.id || 'responsive'}, loading`)
|
|
107
|
+
// Otherwise use a logic that will invoke callback for cached request and then try to revalidate it
|
|
108
|
+
var img: HTMLImageElement
|
|
109
|
+
await fetchAndRevalidate(
|
|
110
|
+
url,
|
|
111
|
+
{},
|
|
112
|
+
(response, isCached) => {
|
|
113
|
+
// <2 second difference is not considered outdated
|
|
114
|
+
// If thumbnail is detected as outdated it will throw error and promise will be rejected
|
|
115
|
+
const isUpToDate =
|
|
116
|
+
Math.abs(Number(new Date(response.headers.get('x-ms-meta-componentRenderedAt'))) - Number(lastModifiedDate)) <
|
|
117
|
+
2000
|
|
118
|
+
|
|
119
|
+
if (isCached && !isUpToDate) {
|
|
120
|
+
responseToImage(response).then((image) => {
|
|
121
|
+
img = image
|
|
122
|
+
callback(image, isCached)
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
return isUpToDate
|
|
126
|
+
},
|
|
127
|
+
(response, state, isCached) => {
|
|
128
|
+
component.sdk.log(`FEAAS: Thumbnail ${component.id}/${version?.id || 'responsive'}, status: ${state}`)
|
|
129
|
+
responseToImage(response).then((image) => {
|
|
130
|
+
img = image
|
|
131
|
+
callback(image, isCached)
|
|
132
|
+
})
|
|
133
|
+
},
|
|
134
|
+
null
|
|
135
|
+
)
|
|
136
|
+
return img
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Creates a thumbnail image for a given component and version.
|
|
141
|
+
*
|
|
142
|
+
* First, it determines the appropriate version to use. If not provided, it attempts to fetch one suitable for
|
|
143
|
+
* thumbnail. It then leverages `render` to generate an image with provided parameters and finally, if the component
|
|
144
|
+
* isn't new, uploads the generated as a Blob onto cdn for future retrieval.
|
|
145
|
+
*
|
|
146
|
+
* @param {ComponentModel} component - Component instance.
|
|
147
|
+
* @param {VersionModel} [version] - Version instance.
|
|
148
|
+
* @returns {Promise<HTMLImageElement | null>} Thumbnail image or null if generation isn't possible.
|
|
149
|
+
*/
|
|
150
|
+
export async function generate(component: ComponentModel, version?: VersionModel) {
|
|
151
|
+
const sourceVersion = version || (component.isNew ? null : await getVersionForThumbnail(component))
|
|
152
|
+
if (!sourceVersion || sourceVersion.isViewEmpty()) {
|
|
153
|
+
return null
|
|
154
|
+
}
|
|
155
|
+
const { stylesheet, datasources } = component.library
|
|
156
|
+
const img = await render(
|
|
157
|
+
sourceVersion.classList,
|
|
158
|
+
stylesheet.getBreakpointForWidth(800, sourceVersion.getBreakpoints()),
|
|
159
|
+
sourceVersion.view,
|
|
160
|
+
datasources.reduce((object, datasource) => {
|
|
161
|
+
return Object.assign(object, { [datasource.id]: datasource.sample })
|
|
162
|
+
}, {}),
|
|
163
|
+
stylesheet.css,
|
|
164
|
+
component.sdk
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
return img
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Transforms a specific component version into an image.
|
|
172
|
+
*
|
|
173
|
+
* Generates an isolated environment (iframe) to render the component with supplied parameters. 'html2canvas.js' is then
|
|
174
|
+
* injected into the iframe to capture the rendered component as an image. The image is created respecting the provided
|
|
175
|
+
* design breakpoint, ensuring an accurate visual representation.
|
|
176
|
+
*
|
|
177
|
+
* @param {string[]} classList - Array of class names applied to the rendered component.
|
|
178
|
+
* @param {Style.Rule<'breakpoint'>} breakpoint - Breakpoint style rule that dictates the viewport dimensions for
|
|
179
|
+
* rendering.
|
|
180
|
+
* @param {string} template - String template of the component to render.
|
|
181
|
+
* @param {any} data - Data object to be used when rendering the component.
|
|
182
|
+
* @param {string} cssText - CSS rules to be applied during component rendering.
|
|
183
|
+
* @param {EnvironmentVariables} env - Environment variables used to set frontend and backend endpoint URLs.
|
|
184
|
+
* @returns {Promise<BasicImage>} - Promise that resolves to an object containing the src, width, and height of the
|
|
185
|
+
* created image.
|
|
186
|
+
*/
|
|
187
|
+
function render(
|
|
188
|
+
classList: string[],
|
|
189
|
+
breakpoint: Style.Rule<'breakpoint'>,
|
|
190
|
+
template: string,
|
|
191
|
+
data: any,
|
|
192
|
+
cssText: string,
|
|
193
|
+
sdk: SDK
|
|
194
|
+
) {
|
|
195
|
+
const iframe = document.createElement('iframe')
|
|
196
|
+
iframe.style.position = 'absolute'
|
|
197
|
+
iframe.style.top = '-2000px'
|
|
198
|
+
iframe.style.left = '-2000px'
|
|
199
|
+
iframe.style.width = breakpoint.props.minWidth + 'px'
|
|
200
|
+
iframe.style.height = '720px'
|
|
201
|
+
iframe.style.zIndex = '100011'
|
|
202
|
+
document.body.appendChild(iframe)
|
|
203
|
+
|
|
204
|
+
return new Promise<HTMLImageElement>((resolve, reject) => {
|
|
205
|
+
const script = document.createElement('script')
|
|
206
|
+
script.onerror = reject
|
|
207
|
+
script.src = sdk.frontend + '/assets/html2canvas.js'
|
|
208
|
+
script.onload = () => {
|
|
209
|
+
;(iframe.contentWindow.document.fonts?.ready || Promise.resolve()).then(function () {
|
|
210
|
+
return (
|
|
211
|
+
iframe.contentWindow
|
|
212
|
+
//@ts-ignore
|
|
213
|
+
.html2canvas(root, {
|
|
214
|
+
// 1.1 scale is to remove shadows that are drawn incorrectly
|
|
215
|
+
scale: Math.max(1.1, 1000 / Math.min(1401, Math.max(320, breakpoint.props.minWidth))),
|
|
216
|
+
width: Math.min(1401, Math.max(320, breakpoint.props.minWidth)),
|
|
217
|
+
height: Math.max(1, Math.min(2000, root.offsetHeight)),
|
|
218
|
+
windowWidth: breakpoint.props.minWidth + 'px',
|
|
219
|
+
windowHeight: 1000,
|
|
220
|
+
proxy: sdk.backend + '/proxy/media',
|
|
221
|
+
backgroundColor: '#ffffff'
|
|
222
|
+
})
|
|
223
|
+
.then((canvas: HTMLCanvasElement) => {
|
|
224
|
+
const src = canvas.toDataURL('image/jpeg', 0.6)
|
|
225
|
+
iframe.parentElement.removeChild(iframe)
|
|
226
|
+
const img = new Image()
|
|
227
|
+
img.onload = () => {
|
|
228
|
+
resolve(img)
|
|
229
|
+
}
|
|
230
|
+
img.onerror = reject
|
|
231
|
+
img.src = src
|
|
232
|
+
})
|
|
233
|
+
)
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
iframe.contentWindow.document.head.appendChild(script)
|
|
237
|
+
const stylesheet = iframe.contentWindow.document.createElement('style')
|
|
238
|
+
stylesheet.textContent = `
|
|
239
|
+
${cssText}
|
|
240
|
+
body, html { padding: 0; margin: 0; overflow: hidden }
|
|
241
|
+
`
|
|
242
|
+
|
|
243
|
+
iframe.contentWindow.document.head.appendChild(stylesheet)
|
|
244
|
+
const root = renderComponent({ template, data })
|
|
245
|
+
root.classList.add(...classList, '-feaas--preview', '-emulate--' + breakpoint.details.slug)
|
|
246
|
+
iframe.contentWindow.document.body.appendChild(root)
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Uploads a thumbnail Blob to a CDN, enabling it for future retrieval.
|
|
252
|
+
*
|
|
253
|
+
* It constructs a URL based on component and version information, if available. The thumbnail's blob is uploaded with
|
|
254
|
+
* several metadata points, including identifiers and timestamps. Upon successful upload, it sends a no-cache fetch
|
|
255
|
+
* request to the constructed URL to bust the cache.
|
|
256
|
+
*
|
|
257
|
+
* @param {ComponentModel} component - Component instance.
|
|
258
|
+
* @param {VersionModel} version - Version instance.
|
|
259
|
+
* @param {Blob} blob - Blob instance to upload.
|
|
260
|
+
* @returns {Promise<string>} URL of the uploaded Blob.
|
|
261
|
+
*/
|
|
262
|
+
export async function upload(component: ComponentModel, version: VersionModel, blob: Blob): Promise<string> {
|
|
263
|
+
const url = version ? version.getThumbnailURL() : component.getThumbnailURL()
|
|
264
|
+
const lastModifiedDate = new Date(
|
|
265
|
+
Math.max(Number(component.modifiedAt), Number(component.library.stylesheet.modifiedAt))
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
try {
|
|
269
|
+
await component.sdk.uploadBlob('thumbnails', url.split(/\/thumbnails\//)[1], blob, {
|
|
270
|
+
headers: {
|
|
271
|
+
'x-ms-blob-content-type': 'image/jpg',
|
|
272
|
+
'x-ms-blob-cache-control': 'public,max-age=31536000,immutable',
|
|
273
|
+
'x-ms-meta-versionId': version ? version.id : null,
|
|
274
|
+
'x-ms-meta-versionRevision': version ? version.revision.toString() : null,
|
|
275
|
+
'x-ms-meta-componentId': component.id,
|
|
276
|
+
'x-ms-meta-componentRenderedAt': lastModifiedDate.toUTCString(),
|
|
277
|
+
'x-ms-meta-componentModifiedAt': component.modifiedAt.toUTCString(),
|
|
278
|
+
'x-ms-meta-componentCreatedAt': component.createdAt.toUTCString(),
|
|
279
|
+
'x-ms-meta-modifiedAt': component.modifiedAt.toUTCString()
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
// bust thumbnail cache for the future, off-thread
|
|
283
|
+
fetch(url, {
|
|
284
|
+
cache: 'no-cache'
|
|
285
|
+
}).then(() => {
|
|
286
|
+
component.sdk.log('FEAAS: Thumbnail uploaded', url)
|
|
287
|
+
})
|
|
288
|
+
} catch (e) {
|
|
289
|
+
component.sdk.log('FEAAS: Thumbnail failed to upload', url, e)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return url
|
|
293
|
+
}
|
package/src/fetch.ts
CHANGED
|
@@ -13,12 +13,12 @@ import { ControlledPromise } from './utils/promise.js'
|
|
|
13
13
|
* errors will not be caught. Function throws when unable to fetch valid response.
|
|
14
14
|
*/
|
|
15
15
|
const phases = ['initial', 'check', 'updated'] as const
|
|
16
|
-
export type ValidatableRequestPhase = typeof phases[number]
|
|
16
|
+
export type ValidatableRequestPhase = (typeof phases)[number]
|
|
17
17
|
export type FetchCache = Record<string, FetchCacheRequest>
|
|
18
18
|
export interface ValidatableRequest {
|
|
19
19
|
url: Parameters<typeof fetch>[0]
|
|
20
20
|
options?: Parameters<typeof fetch>[1]
|
|
21
|
-
validator?: (resposne: Response) => boolean
|
|
21
|
+
validator?: (resposne: Response, isCached: boolean) => boolean
|
|
22
22
|
callback?: (response: Response, status: ValidatableRequestPhase, isCached: boolean) => any
|
|
23
23
|
isCached?: boolean
|
|
24
24
|
response?: Response
|
|
@@ -66,6 +66,8 @@ export function getFetchCache(url: Parameters<typeof fetch>[0], dontCreate = fal
|
|
|
66
66
|
return cache
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
export class InvalidResponse extends Error {}
|
|
70
|
+
|
|
69
71
|
export async function fetchWithCacheAwareness(
|
|
70
72
|
{ url, options, validator, callback, cache }: ValidatableRequest,
|
|
71
73
|
phase: ValidatableRequestPhase = 'initial'
|
|
@@ -88,11 +90,11 @@ export async function fetchWithCacheAwareness(
|
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
92
|
if (!response.ok) throw new Error('Fetch failed')
|
|
91
|
-
if (validator?.(response) === false) throw new Error('Response is not valid')
|
|
92
93
|
// see if response was pulled from cache
|
|
93
94
|
const isCached =
|
|
94
95
|
new Date(response.headers.get('date')) < date ||
|
|
95
96
|
(promise && phase == 'initial' && (cachedRequest.phase == 'updated' || cachedRequest.phase == 'check'))
|
|
97
|
+
if (validator?.(response, isCached) === false) throw new InvalidResponse('Response is not valid')
|
|
96
98
|
await callback?.(response, phase, isCached)
|
|
97
99
|
return {
|
|
98
100
|
url,
|
package/src/headless.ts
CHANGED
|
@@ -17,6 +17,8 @@ export * from './components/FEAASElement.js'
|
|
|
17
17
|
export * from './components/FEAASComponent.js'
|
|
18
18
|
export * from './components/FEAASLoader.js'
|
|
19
19
|
export * from './components/FEAASStylesheet.js'
|
|
20
|
+
export * from './utils/datapath.js'
|
|
21
|
+
export * as Thumbnail from './dom/thumbnail.js'
|
|
20
22
|
|
|
21
23
|
declare global {
|
|
22
24
|
interface Window {
|
package/src/ui/FEAASContext.tsx
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type * as FEAASContextImports from '@sitecore-feaas/frontend/context'
|
|
2
2
|
import type { AuthModel } from '@sitecore-feaas/sdk'
|
|
3
3
|
import { FEAASBase, OptionalExcept } from '../components/FEAASBase.js'
|
|
4
|
+
import { ControlledPromise } from '../utils/promise.js'
|
|
4
5
|
|
|
5
|
-
export function FEAASContextProps(element: HTMLElement
|
|
6
|
+
export function FEAASContextProps(element: HTMLElement) {
|
|
6
7
|
return {
|
|
7
8
|
library: element?.getAttribute('library') as string,
|
|
8
9
|
src: element?.getAttribute('src') ?? '/context.js',
|
|
@@ -66,11 +67,16 @@ export class FEAASContext extends FEAASBase<FEAASContextProps, typeof FEAASConte
|
|
|
66
67
|
super.connectedCallback()
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
sdk: FEAASContextImports.SDK
|
|
71
|
+
onStatusChange: FEAASContextImports.SDKStatusCallback = (status, sdk) => {
|
|
70
72
|
if (status == 'ready') {
|
|
71
|
-
this.
|
|
73
|
+
this.whenSDKReady.resolve(sdk)
|
|
74
|
+
this.dispatchEvent(new CustomEvent('feaasReady', { bubbles: true, composed: true, detail: sdk }))
|
|
72
75
|
}
|
|
73
76
|
}
|
|
77
|
+
whenSDKReady = ControlledPromise<FEAASContextImports.SDK>((sdk) => {
|
|
78
|
+
this.sdk = sdk
|
|
79
|
+
})
|
|
74
80
|
|
|
75
81
|
render(
|
|
76
82
|
{ children, library }: React.PropsWithChildren<FEAASContextProps>,
|
package/src/ui/FEAASEditor.tsx
CHANGED
|
@@ -38,7 +38,7 @@ export function getFramePosition(element: HTMLElement) {
|
|
|
38
38
|
{ left: 0, top: 0 }
|
|
39
39
|
)
|
|
40
40
|
}
|
|
41
|
-
export function FEAASEditorProps(element: FEAASBase
|
|
41
|
+
export function FEAASEditorProps(element: FEAASBase) {
|
|
42
42
|
return {
|
|
43
43
|
context: HTMLElement,
|
|
44
44
|
frontend: element?.getContextAttribute('frontend'),
|
|
@@ -447,6 +447,7 @@ export class FEAASEditor extends FEAASBase<FEAASEditorProps, typeof FEAASEditorI
|
|
|
447
447
|
Chrome,
|
|
448
448
|
ReactDOM,
|
|
449
449
|
useSDK,
|
|
450
|
+
useLibrary,
|
|
450
451
|
useSlots,
|
|
451
452
|
useModelObserver,
|
|
452
453
|
ConfirmationContext,
|
|
@@ -455,12 +456,11 @@ export class FEAASEditor extends FEAASBase<FEAASEditorProps, typeof FEAASEditorI
|
|
|
455
456
|
) {
|
|
456
457
|
if (!this.payload) return
|
|
457
458
|
const { React } = this.payload
|
|
458
|
-
const
|
|
459
|
+
const sdk = useSDK()
|
|
460
|
+
const library = useLibrary()
|
|
459
461
|
const [component, setComponent] = useState<FEAASEditorImports.SDK.ComponentModel>()
|
|
460
462
|
const { setConfirm } = useContext(ConfirmationContext)
|
|
461
|
-
|
|
462
463
|
const usedId = instanceId || versionId
|
|
463
|
-
const library = sdk.library
|
|
464
464
|
|
|
465
465
|
if (component) this.component = component
|
|
466
466
|
|
|
@@ -600,7 +600,7 @@ export class FEAASEditor extends FEAASBase<FEAASEditorProps, typeof FEAASEditorI
|
|
|
600
600
|
<Editor
|
|
601
601
|
datasources={sdk.datasources}
|
|
602
602
|
sdk={sdk}
|
|
603
|
-
library={
|
|
603
|
+
library={library}
|
|
604
604
|
component={component}
|
|
605
605
|
versions={versions}
|
|
606
606
|
context={{
|
package/src/ui/FEAASExternal.tsx
CHANGED
|
@@ -20,8 +20,6 @@ const convertSchemaPropertiesToKebabCase = (schema: any) => {
|
|
|
20
20
|
return { ...schema, properties: kebabSchemaProperties, required: kebabRequiredProperties }
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
// TODO: parse object, number, array, boolean etc props
|
|
24
|
-
|
|
25
23
|
export const parseProps = (propName: string, propValue: string, properties: any) => {
|
|
26
24
|
switch (properties?.[propName]?.type) {
|
|
27
25
|
case 'string':
|
|
@@ -41,14 +39,14 @@ export const parseProps = (propName: string, propValue: string, properties: any)
|
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
|
|
44
|
-
export function FEAASExternalProps(element: FEAASBase
|
|
42
|
+
export function FEAASExternalProps(element: FEAASBase) {
|
|
45
43
|
const attributes = element.getAttributeNames()
|
|
46
44
|
const id = element.getAttribute('data-external-id')
|
|
47
45
|
const schema: any = FEAASExternal.registered[id]?.schema
|
|
48
46
|
const props = attributes.reduce((prev, curr) => {
|
|
49
47
|
if (!curr.startsWith('data-attribute') && !['class', 'id', 'contenteditable'].includes(curr)) {
|
|
50
48
|
const value = element.getAttribute(curr)
|
|
51
|
-
return { ...prev, [reverseKebabCase(curr)]: parseProps(curr, value, schema?.properties) }
|
|
49
|
+
return { ...prev, [reverseKebabCase(curr)]: parseProps(reverseKebabCase(curr), value, schema?.properties) }
|
|
52
50
|
} else {
|
|
53
51
|
return prev
|
|
54
52
|
}
|
|
@@ -77,8 +75,10 @@ export default class FEAASExternal extends FEAASBase<FEAASExternalProps, ReactMo
|
|
|
77
75
|
return FEAASExternalProps(this)
|
|
78
76
|
}
|
|
79
77
|
|
|
80
|
-
static registerComponent(component: React.FC, schema: any = {}
|
|
81
|
-
|
|
78
|
+
static registerComponent(component: React.FC, schema: any = {}) {
|
|
79
|
+
const id = schema.name || component.name
|
|
80
|
+
const enrichedSchema = { ...schema, title: id, type: 'object' }
|
|
81
|
+
FEAASExternal.registered[id] = { component, schema: enrichedSchema }
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
static setRegistrationCallback() {
|
|
@@ -86,10 +86,10 @@ export default class FEAASExternal extends FEAASBase<FEAASExternalProps, ReactMo
|
|
|
86
86
|
if (!FEAASExternal.registrationCallback && typeof window !== 'undefined') {
|
|
87
87
|
FEAASExternal.registrationCallback = setTimeout(() => {
|
|
88
88
|
// create components array from object
|
|
89
|
-
const components = Object.keys(FEAASExternal.registered).map((
|
|
90
|
-
...FEAASExternal.registered[
|
|
91
|
-
name,
|
|
92
|
-
schema: convertSchemaPropertiesToKebabCase(FEAASExternal.registered[
|
|
89
|
+
const components = Object.keys(FEAASExternal.registered).map((id) => ({
|
|
90
|
+
...FEAASExternal.registered[id],
|
|
91
|
+
name: id,
|
|
92
|
+
schema: convertSchemaPropertiesToKebabCase(FEAASExternal.registered[id].schema)
|
|
93
93
|
//component: FEAASExternal.registered[name].component.toString()
|
|
94
94
|
}))
|
|
95
95
|
|
package/src/ui/FEAASPicker.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type * as FEAASPickerImports from '@sitecore-feaas/frontend/picker'
|
|
2
2
|
import { FEAASBase } from '../components/FEAASBase.js'
|
|
3
3
|
|
|
4
|
-
export function FEAASPickerProps(element: FEAASBase<any, any>
|
|
4
|
+
export function FEAASPickerProps(element: FEAASBase<any, any>) {
|
|
5
5
|
return {
|
|
6
6
|
part: element?.getAttribute('part') || 'picker',
|
|
7
7
|
slot: element?.getAttribute('part') || 'picker',
|
|
@@ -35,13 +35,20 @@ export class FEAASPicker extends FEAASBase<FEAASPickerProps, typeof FEAASPickerI
|
|
|
35
35
|
|
|
36
36
|
render(
|
|
37
37
|
{ placeholder, context, mode, children, variant }: React.PropsWithChildren<FEAASPickerProps>,
|
|
38
|
-
{ Picker, React,
|
|
38
|
+
{ Picker, React, useLibrary }: typeof FEAASPickerImports
|
|
39
39
|
) {
|
|
40
|
+
const collections = useLibrary('collections')
|
|
41
|
+
const rules = useLibrary('stylesheet.styles')
|
|
40
42
|
return (
|
|
41
43
|
<Picker
|
|
44
|
+
rules={rules}
|
|
45
|
+
collections={collections}
|
|
46
|
+
themeContext={context}
|
|
42
47
|
placeholder={placeholder}
|
|
43
48
|
mode={mode || undefined}
|
|
44
|
-
|
|
49
|
+
onModeChange={(mode) => {
|
|
50
|
+
if (!mode) this.props.onClose(mode)
|
|
51
|
+
}}
|
|
45
52
|
onContextAttributeSet={this.props.onContextAttributeSet}
|
|
46
53
|
context={context}
|
|
47
54
|
variant={variant}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const parseLimit = (qualifier: string) => {
|
|
2
|
+
const limit = parseInt(qualifier?.match(/\[0:(\d+)\]/)?.[1], 10)
|
|
3
|
+
if (!isNaN(limit) && limit > 0) return String(limit)
|
|
4
|
+
return null
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const parseRange = (qualifier: string) => {
|
|
8
|
+
const bits = qualifier
|
|
9
|
+
?.match(/\[([^\]]+)\]/)?.[1]
|
|
10
|
+
.split(/\s*,\s*/g)
|
|
11
|
+
.filter(Boolean)
|
|
12
|
+
.map((bit) => {
|
|
13
|
+
// individual indexes
|
|
14
|
+
if (bit.match(/^-?\d+$/)) return bit
|
|
15
|
+
// ranges
|
|
16
|
+
const range = bit.match(/^(\d+):(\d+)$/)
|
|
17
|
+
return range && parseInt(range[1], 10) < parseInt(range[2], 10) ? range[1] + '-' + range[2] : null
|
|
18
|
+
})
|
|
19
|
+
if (!bits?.every(Boolean)) return null
|
|
20
|
+
return bits.join(',')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const getQualifier = (jsonpath: string) => jsonpath?.match(/(\.\*|\[[^\]]+\])$/)?.[0]
|
|
24
|
+
|
|
25
|
+
export function cleanCollectionBit(path: string) {
|
|
26
|
+
return path.replace(/(\[[^\]\[]+\]|\.\*)$/g, '')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function normalizeCollectionScope(path: string) {
|
|
30
|
+
return cleanCollectionBit(path) + '.*'
|
|
31
|
+
}
|