@builder.io/sdk-svelte 0.4.5 → 0.5.0
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/package/--package.json +40 -29
- package/package/CHANGELOG.md +4 -0
- package/package/blocks/BaseText.svelte +1 -1
- package/package/blocks/button/button.svelte +18 -33
- package/package/blocks/button/button.svelte.d.ts +3 -1
- package/package/blocks/button/component-info.d.ts +1 -1
- package/package/blocks/button/component-info.js +9 -13
- package/package/blocks/columns/columns.svelte +12 -18
- package/package/blocks/columns/columns.svelte.d.ts +11 -7
- package/package/blocks/columns/component-info.d.ts +1 -1
- package/package/blocks/columns/component-info.js +62 -83
- package/package/blocks/custom-code/component-info.d.ts +1 -1
- package/package/blocks/custom-code/component-info.js +7 -11
- package/package/blocks/embed/component-info.d.ts +1 -1
- package/package/blocks/embed/component-info.js +8 -15
- package/package/blocks/embed/helpers.js +1 -5
- package/package/blocks/form/component-info.d.ts +1 -1
- package/package/blocks/form/component-info.js +94 -129
- package/package/blocks/form/form.svelte +2 -289
- package/package/blocks/form/form.svelte.d.ts +1 -57
- package/package/blocks/fragment/component-info.d.ts +1 -1
- package/package/blocks/fragment/component-info.js +1 -1
- package/package/blocks/helpers.d.ts +13 -0
- package/package/blocks/helpers.js +40 -0
- package/package/blocks/image/component-info.d.ts +1 -1
- package/package/blocks/image/component-info.js +33 -63
- package/package/blocks/image/image.helpers.js +2 -11
- package/package/blocks/image/image.svelte.d.ts +0 -1
- package/package/blocks/img/component-info.d.ts +1 -1
- package/package/blocks/img/component-info.js +4 -6
- package/package/blocks/img/img.svelte +4 -29
- package/package/blocks/img/img.svelte.d.ts +3 -1
- package/package/blocks/input/component-info.d.ts +1 -1
- package/package/blocks/input/component-info.js +15 -40
- package/package/blocks/input/input.svelte +4 -29
- package/package/blocks/input/input.svelte.d.ts +3 -1
- package/package/blocks/raw-text/component-info.d.ts +1 -1
- package/package/blocks/raw-text/component-info.js +3 -5
- package/package/blocks/raw-text/raw-text.svelte +3 -5
- package/package/blocks/raw-text/raw-text.svelte.d.ts +1 -1
- package/package/blocks/section/component-info.d.ts +1 -1
- package/package/blocks/section/component-info.js +13 -18
- package/package/blocks/section/section.svelte +4 -29
- package/package/blocks/section/section.svelte.d.ts +3 -1
- package/package/blocks/select/component-info.d.ts +1 -1
- package/package/blocks/select/component-info.js +22 -34
- package/package/blocks/select/select.svelte +4 -29
- package/package/blocks/select/select.svelte.d.ts +3 -1
- package/package/blocks/submit-button/component-info.d.ts +1 -1
- package/package/blocks/submit-button/component-info.js +5 -7
- package/package/blocks/submit-button/submit-button.svelte +4 -29
- package/package/blocks/submit-button/submit-button.svelte.d.ts +3 -1
- package/package/blocks/symbol/component-info.d.ts +1 -1
- package/package/blocks/symbol/component-info.js +12 -17
- package/package/blocks/symbol/symbol.helpers.d.ts +14 -0
- package/package/blocks/symbol/symbol.helpers.js +31 -0
- package/package/blocks/symbol/symbol.svelte +37 -87
- package/package/blocks/symbol/symbol.svelte.d.ts +13 -7
- package/package/blocks/text/component-info.d.ts +1 -1
- package/package/blocks/text/component-info.js +6 -7
- package/package/blocks/text/text.svelte +1 -1
- package/package/blocks/textarea/component-info.d.ts +1 -1
- package/package/blocks/textarea/component-info.js +13 -19
- package/package/blocks/textarea/textarea.svelte +4 -29
- package/package/blocks/textarea/textarea.svelte.d.ts +3 -1
- package/package/blocks/video/component-info.d.ts +1 -1
- package/package/blocks/video/component-info.js +35 -55
- package/package/blocks/video/video.svelte +4 -30
- package/package/blocks/video/video.svelte.d.ts +2 -0
- package/package/components/block/block.helpers.d.ts +13 -0
- package/package/components/{render-block/render-block.helpers.js → block/block.helpers.js} +12 -29
- package/package/components/block/block.svelte +135 -0
- package/package/components/block/block.svelte.d.ts +26 -0
- package/package/components/{render-block → block/components}/block-styles.svelte +20 -22
- package/package/components/{render-block → block/components}/block-styles.svelte.d.ts +2 -2
- package/package/components/block/components/block-wrapper.svelte +53 -0
- package/package/components/block/components/block-wrapper.svelte.d.ts +33 -0
- package/package/components/block/components/component-ref/component-ref.helpers.d.ts +36 -0
- package/package/components/block/components/component-ref/component-ref.helpers.js +22 -0
- package/package/components/block/components/component-ref/component-ref.svelte +37 -0
- package/package/components/block/components/component-ref/component-ref.svelte.d.ts +24 -0
- package/package/components/block/components/interactive-element.svelte +29 -0
- package/package/components/block/components/interactive-element.svelte.d.ts +31 -0
- package/package/components/{render-block/render-repeated-block.svelte → block/components/repeated-block.svelte} +4 -3
- package/package/components/block/components/repeated-block.svelte.d.ts +25 -0
- package/package/components/{render-blocks.svelte → blocks/blocks-wrapper.svelte} +3 -20
- package/package/components/blocks/blocks-wrapper.svelte.d.ts +29 -0
- package/package/components/blocks/blocks.svelte +26 -0
- package/package/components/blocks/blocks.svelte.d.ts +28 -0
- package/package/components/{render-content/render-content.svelte → content/components/enable-editor.svelte} +47 -166
- package/package/components/content/components/enable-editor.svelte.d.ts +36 -0
- package/package/components/{render-content/components/render-styles.helpers.d.ts → content/components/styles.helpers.d.ts} +1 -1
- package/package/components/{render-content/components/render-styles.helpers.js → content/components/styles.helpers.js} +3 -4
- package/package/components/{render-content/components/render-styles.svelte → content/components/styles.svelte} +2 -2
- package/package/components/{render-content/components/render-styles.svelte.d.ts → content/components/styles.svelte.d.ts} +5 -5
- package/package/components/content/content.helpers.d.ts +7 -0
- package/package/components/content/content.helpers.js +30 -0
- package/package/components/content/content.svelte +113 -0
- package/package/components/content/content.svelte.d.ts +30 -0
- package/package/components/{render-content/render-content.types.d.ts → content/content.types.d.ts} +4 -4
- package/package/components/content/index.d.ts +1 -0
- package/package/components/content/index.js +1 -0
- package/package/components/{render-content-variants/render-content-variants.svelte → content-variants/content-variants.svelte} +20 -19
- package/package/components/{render-content-variants/render-content-variants.svelte.d.ts → content-variants/content-variants.svelte.d.ts} +6 -6
- package/package/components/{render-content-variants/render-content-variants.types.d.ts → content-variants/content-variants.types.d.ts} +5 -5
- package/package/components/{render-content-variants → content-variants}/helpers.d.ts +7 -7
- package/package/components/{render-content-variants → content-variants}/helpers.js +13 -22
- package/package/constants/builder-registered-components.js +34 -13
- package/package/constants/device-sizes.js +9 -8
- package/package/constants/sdk-version.d.ts +1 -1
- package/package/constants/sdk-version.js +1 -1
- package/package/constants/target.d.ts +1 -1
- package/package/context/builder.context.d.ts +1 -1
- package/package/context/builder.context.js +1 -1
- package/package/context/types.d.ts +1 -1
- package/package/functions/apply-patch-with-mutation.d.ts +10 -0
- package/package/functions/apply-patch-with-mutation.js +54 -0
- package/package/functions/evaluate/acorn.d.ts +0 -0
- package/package/functions/evaluate/acorn.js +2433 -0
- package/package/functions/{evaluate.d.ts → evaluate/evaluate.d.ts} +5 -2
- package/package/functions/{evaluate.js → evaluate/evaluate.js} +34 -13
- package/package/functions/evaluate/index.d.ts +1 -0
- package/package/functions/evaluate/index.js +1 -0
- package/package/functions/evaluate/interpreter.d.ts +2 -0
- package/package/functions/evaluate/interpreter.js +3853 -0
- package/package/functions/evaluate/non-node-runtime.d.ts +2 -0
- package/package/functions/evaluate/non-node-runtime.js +84 -0
- package/package/functions/evaluate/types.d.ts +10 -0
- package/package/functions/extract-text-styles.js +2 -14
- package/package/functions/get-block-actions-handler.js +3 -3
- package/package/functions/get-block-actions.d.ts +1 -0
- package/package/functions/get-block-actions.js +14 -1
- package/package/functions/get-block-component-options.js +1 -1
- package/package/functions/get-block-properties.d.ts +5 -1
- package/package/functions/get-block-properties.js +15 -9
- package/package/functions/get-builder-search-params/index.js +3 -7
- package/package/functions/get-content/generate-content-url.js +7 -5
- package/package/functions/get-content/index.d.ts +2 -6
- package/package/functions/get-content/index.js +26 -6
- package/package/functions/get-processed-block.d.ts +1 -1
- package/package/functions/get-processed-block.js +11 -7
- package/package/functions/get-react-native-block-styles.d.ts +1 -1
- package/package/functions/get-react-native-block-styles.js +2 -2
- package/package/functions/if-target.d.ts +1 -1
- package/package/functions/if-target.js +1 -1
- package/package/functions/is-editing.js +1 -3
- package/package/functions/is-non-node-server.d.ts +4 -0
- package/package/functions/is-non-node-server.js +8 -0
- package/package/functions/on-change.js +1 -1
- package/package/functions/register-component.d.ts +2 -1
- package/package/functions/register-component.js +20 -8
- package/package/functions/register.js +2 -2
- package/package/functions/sanitize-react-native-block-styles.js +13 -5
- package/package/functions/set-editor-settings.js +1 -1
- package/package/functions/set.js +2 -9
- package/package/functions/track/helpers.js +6 -11
- package/package/functions/track/index.js +26 -14
- package/package/functions/track/interaction.js +15 -16
- package/package/helpers/ab-tests.d.ts +2 -2
- package/package/helpers/ab-tests.js +42 -30
- package/package/helpers/canTrack.js +1 -1
- package/package/helpers/cookie.d.ts +2 -2
- package/package/helpers/cookie.js +13 -26
- package/package/helpers/css.d.ts +1 -1
- package/package/helpers/css.js +1 -1
- package/package/helpers/flatten.js +8 -8
- package/package/helpers/localStorage.d.ts +2 -2
- package/package/helpers/localStorage.js +2 -2
- package/package/helpers/logger.js +1 -1
- package/package/helpers/preview-lru-cache/get.d.ts +1 -0
- package/package/helpers/preview-lru-cache/get.js +7 -0
- package/package/helpers/preview-lru-cache/helpers.d.ts +1 -0
- package/package/helpers/preview-lru-cache/helpers.js +9 -0
- package/package/helpers/preview-lru-cache/init.d.ts +6 -0
- package/package/helpers/preview-lru-cache/init.js +15 -0
- package/package/helpers/preview-lru-cache/set.d.ts +7 -0
- package/package/helpers/preview-lru-cache/set.js +9 -0
- package/package/helpers/preview-lru-cache/types.d.ts +12 -0
- package/package/helpers/preview-lru-cache/types.js +6 -0
- package/package/helpers/sessionId.d.ts +2 -2
- package/package/helpers/sessionId.js +11 -4
- package/package/helpers/uuid.js +1 -1
- package/package/helpers/visitorId.d.ts +1 -1
- package/package/helpers/visitorId.js +7 -4
- package/package/index-helpers/blocks-exports.d.ts +2 -2
- package/package/index-helpers/blocks-exports.js +2 -2
- package/package/index.d.ts +2 -2
- package/package/index.js +1 -1
- package/package/scripts/init-editing.js +65 -57
- package/package/types/builder-props.d.ts +10 -0
- package/package/types/components.d.ts +8 -2
- package/package/types/enforced-partials.d.ts +1 -1
- package/package/types/targets.d.ts +1 -3
- package/package/types/typescript.d.ts +3 -0
- package/package.json +2 -2
- package/src/blocks/BaseText.svelte +1 -1
- package/src/blocks/button/button.svelte +21 -34
- package/src/blocks/button/component-info.ts +20 -27
- package/src/blocks/columns/columns.svelte +26 -30
- package/src/blocks/columns/component-info.ts +205 -240
- package/src/blocks/custom-code/component-info.ts +20 -27
- package/src/blocks/embed/component-info.ts +34 -51
- package/src/blocks/embed/helpers.ts +2 -8
- package/src/blocks/form/component-info.ts +204 -253
- package/src/blocks/form/form.svelte +1 -368
- package/src/blocks/fragment/component-info.ts +3 -5
- package/src/blocks/helpers.ts +43 -0
- package/src/blocks/image/component-info.ts +113 -161
- package/src/blocks/image/image.helpers.ts +5 -33
- package/src/blocks/img/component-info.ts +11 -16
- package/src/blocks/img/img.svelte +7 -31
- package/src/blocks/input/component-info.ts +35 -66
- package/src/blocks/input/input.svelte +7 -31
- package/src/blocks/raw-text/component-info.ts +8 -12
- package/src/blocks/raw-text/raw-text.svelte +2 -5
- package/src/blocks/section/component-info.ts +28 -37
- package/src/blocks/section/section.svelte +7 -30
- package/src/blocks/select/component-info.ts +38 -55
- package/src/blocks/select/select.svelte +7 -31
- package/src/blocks/submit-button/component-info.ts +10 -15
- package/src/blocks/submit-button/submit-button.svelte +7 -30
- package/src/blocks/symbol/component-info.ts +31 -39
- package/src/blocks/symbol/symbol.helpers.ts +47 -0
- package/src/blocks/symbol/symbol.svelte +50 -97
- package/src/blocks/text/component-info.ts +14 -18
- package/src/blocks/text/text.svelte +1 -1
- package/src/blocks/textarea/component-info.ts +26 -36
- package/src/blocks/textarea/textarea.svelte +7 -30
- package/src/blocks/video/component-info.ts +77 -103
- package/src/blocks/video/video.svelte +4 -31
- package/src/components/{render-block/render-block.helpers.ts → block/block.helpers.ts} +22 -49
- package/src/components/block/block.svelte +158 -0
- package/src/components/{render-block → block/components}/block-styles.svelte +23 -24
- package/src/components/block/components/block-wrapper.svelte +70 -0
- package/src/components/block/components/component-ref/component-ref.helpers.ts +53 -0
- package/src/components/block/components/component-ref/component-ref.svelte +41 -0
- package/src/components/block/components/interactive-element.svelte +42 -0
- package/src/components/block/components/repeated-block.svelte +30 -0
- package/src/components/{render-block → block}/types.ts +1 -2
- package/src/components/{render-blocks.svelte → blocks/blocks-wrapper.svelte} +14 -32
- package/src/components/blocks/blocks.svelte +39 -0
- package/src/components/{render-content/render-content.svelte → content/components/enable-editor.svelte} +71 -199
- package/src/components/{render-content/components/render-styles.helpers.ts → content/components/styles.helpers.ts} +9 -11
- package/src/components/{render-content/components/render-styles.svelte → content/components/styles.svelte} +3 -3
- package/src/components/content/content.helpers.ts +42 -0
- package/src/components/content/content.svelte +138 -0
- package/src/components/{render-content/render-content.types.ts → content/content.types.ts} +5 -9
- package/src/components/content/index.ts +1 -0
- package/src/components/{render-content → content}/wrap-component-ref.ts +1 -1
- package/src/components/{render-content-variants/render-content-variants.svelte → content-variants/content-variants.svelte} +26 -24
- package/src/components/{render-content-variants/render-content-variants.types.ts → content-variants/content-variants.types.ts} +9 -11
- package/src/components/{render-content-variants → content-variants}/helpers.ts +27 -73
- package/src/constants/builder-registered-components.ts +34 -14
- package/src/constants/device-sizes.ts +16 -22
- package/src/constants/sdk-version.ts +1 -1
- package/src/constants/target.ts +3 -1
- package/src/context/builder.context.ts +1 -1
- package/src/context/types.ts +2 -7
- package/src/functions/apply-patch-with-mutation.ts +61 -0
- package/src/functions/camel-to-kebab-case.ts +1 -2
- package/src/functions/evaluate/acorn.ts +2501 -0
- package/src/functions/evaluate/evaluate.ts +90 -0
- package/src/functions/evaluate/index.ts +1 -0
- package/src/functions/evaluate/interpreter.ts +3887 -0
- package/src/functions/evaluate/non-node-runtime.ts +94 -0
- package/src/functions/evaluate/types.ts +10 -0
- package/src/functions/extract-text-styles.ts +3 -18
- package/src/functions/fast-clone.ts +1 -2
- package/src/functions/get-block-actions-handler.ts +11 -20
- package/src/functions/get-block-actions.ts +22 -14
- package/src/functions/get-block-component-options.ts +2 -3
- package/src/functions/get-block-properties.ts +24 -17
- package/src/functions/get-builder-search-params/index.ts +5 -21
- package/src/functions/get-content/generate-content-url.ts +10 -25
- package/src/functions/get-content/index.ts +34 -37
- package/src/functions/get-content/types.ts +4 -2
- package/src/functions/get-fetch.ts +3 -10
- package/src/functions/get-global-this.ts +1 -2
- package/src/functions/get-processed-block.ts +14 -21
- package/src/functions/get-react-native-block-styles.ts +6 -12
- package/src/functions/if-target.ts +2 -3
- package/src/functions/is-browser.ts +1 -1
- package/src/functions/is-editing.ts +2 -7
- package/src/functions/is-iframe.ts +1 -2
- package/src/functions/is-non-node-server.ts +9 -0
- package/src/functions/is-previewing.ts +1 -4
- package/src/functions/on-change.ts +2 -2
- package/src/functions/register-component.ts +24 -26
- package/src/functions/register.ts +6 -8
- package/src/functions/sanitize-react-native-block-styles.ts +17 -24
- package/src/functions/set-editor-settings.ts +5 -6
- package/src/functions/set.ts +3 -15
- package/src/functions/track/helpers.ts +8 -24
- package/src/functions/track/index.ts +37 -42
- package/src/functions/track/interaction.ts +21 -38
- package/src/functions/transform-block-properties.ts +1 -1
- package/src/functions/transform-block.ts +1 -1
- package/src/helpers/ab-tests.ts +64 -79
- package/src/helpers/canTrack.ts +2 -4
- package/src/helpers/cookie.ts +15 -46
- package/src/helpers/css.ts +4 -13
- package/src/helpers/flatten.ts +11 -16
- package/src/helpers/localStorage.ts +4 -8
- package/src/helpers/logger.ts +2 -2
- package/src/helpers/nullable.ts +1 -3
- package/src/helpers/preview-lru-cache/get.ts +8 -0
- package/src/helpers/preview-lru-cache/helpers.ts +9 -0
- package/src/helpers/preview-lru-cache/init.ts +17 -0
- package/src/helpers/preview-lru-cache/set.ts +18 -0
- package/src/helpers/preview-lru-cache/types.ts +12 -0
- package/src/helpers/sessionId.ts +12 -13
- package/src/helpers/time.ts +1 -3
- package/src/helpers/url.ts +1 -2
- package/src/helpers/uuid.ts +3 -3
- package/src/helpers/visitorId.ts +14 -17
- package/src/index-helpers/blocks-exports.ts +2 -2
- package/src/index-helpers/top-of-file.ts +1 -1
- package/src/index.ts +2 -13
- package/src/scripts/init-editing.ts +85 -98
- package/src/types/api-version.ts +1 -1
- package/src/types/builder-block.ts +1 -3
- package/src/types/builder-content.ts +7 -4
- package/src/types/builder-props.ts +10 -0
- package/src/types/can-track.ts +1 -1
- package/src/types/components.ts +17 -13
- package/src/types/deep-partial.ts +1 -7
- package/src/types/element.ts +1 -3
- package/src/types/enforced-partials.ts +6 -20
- package/src/types/input.ts +6 -9
- package/src/types/targets.ts +1 -6
- package/src/types/typescript.ts +9 -9
- package/package/blocks/util.d.ts +0 -4
- package/package/blocks/util.js +0 -13
- package/package/components/render-block/render-block.helpers.d.ts +0 -12
- package/package/components/render-block/render-block.svelte +0 -152
- package/package/components/render-block/render-block.svelte.d.ts +0 -24
- package/package/components/render-block/render-component.svelte +0 -21
- package/package/components/render-block/render-component.svelte.d.ts +0 -34
- package/package/components/render-block/render-repeated-block.svelte.d.ts +0 -23
- package/package/components/render-blocks.svelte.d.ts +0 -26
- package/package/components/render-content/builder-editing.svelte +0 -4
- package/package/components/render-content/builder-editing.svelte.d.ts +0 -14
- package/package/components/render-content/index.d.ts +0 -1
- package/package/components/render-content/index.js +0 -1
- package/package/components/render-content/render-content.helpers.d.ts +0 -7
- package/package/components/render-content/render-content.helpers.js +0 -30
- package/package/components/render-content/render-content.svelte.d.ts +0 -30
- package/package/functions/evaluate.test.js +0 -17
- package/package/functions/get-builder-search-params/fn.test.js +0 -13
- package/package/functions/get-content/generate-content-url.test.d.ts +0 -1
- package/package/functions/get-content/generate-content-url.test.js +0 -97
- package/package/functions/get-processed-block.test.d.ts +0 -1
- package/package/functions/get-processed-block.test.js +0 -33
- package/package/functions/on-change.test.d.ts +0 -1
- package/package/functions/on-change.test.js +0 -20
- package/package/functions/set.test.d.ts +0 -1
- package/package/functions/set.test.js +0 -16
- package/package/helpers/url.test.d.ts +0 -1
- package/package/helpers/url.test.js +0 -21
- package/src/blocks/util.ts +0 -18
- package/src/components/render-block/render-block.svelte +0 -173
- package/src/components/render-block/render-component.svelte +0 -40
- package/src/components/render-block/render-repeated-block.svelte +0 -25
- package/src/components/render-content/builder-editing.svelte +0 -4
- package/src/components/render-content/index.ts +0 -1
- package/src/components/render-content/render-content.helpers.ts +0 -48
- package/src/functions/evaluate.test.ts +0 -21
- package/src/functions/evaluate.ts +0 -99
- package/src/functions/get-builder-search-params/fn.test.ts +0 -19
- package/src/functions/get-content/generate-content-url.test.ts +0 -112
- package/src/functions/get-processed-block.test.ts +0 -35
- package/src/functions/on-change.test.ts +0 -23
- package/src/functions/set.test.ts +0 -19
- package/src/helpers/url.test.ts +0 -23
- /package/package/components/{render-block → block}/types.d.ts +0 -0
- /package/package/components/{render-block → block}/types.js +0 -0
- /package/package/components/{render-content/render-content.types.js → content/content.types.js} +0 -0
- /package/package/components/{render-content → content}/wrap-component-ref.d.ts +0 -0
- /package/package/components/{render-content → content}/wrap-component-ref.js +0 -0
- /package/package/components/{render-content-variants/render-content-variants.types.js → content-variants/content-variants.types.js} +0 -0
- /package/package/functions/{evaluate.test.d.ts → evaluate/types.js} +0 -0
- /package/package/{functions/get-builder-search-params/fn.test.d.ts → types/builder-props.js} +0 -0
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
function round(num: number) {
|
|
2
2
|
return Math.round(num * 1000) / 1000;
|
|
3
3
|
}
|
|
4
|
-
const findParentElement = (
|
|
5
|
-
target: HTMLElement,
|
|
6
|
-
callback: (element: HTMLElement) => boolean,
|
|
7
|
-
checkElement = true
|
|
8
|
-
): HTMLElement | null => {
|
|
4
|
+
const findParentElement = (target: HTMLElement, callback: (element: HTMLElement) => boolean, checkElement = true): HTMLElement | null => {
|
|
9
5
|
if (!(target instanceof HTMLElement)) {
|
|
10
6
|
return null;
|
|
11
7
|
}
|
|
@@ -14,30 +10,24 @@ const findParentElement = (
|
|
|
14
10
|
if (!parent) {
|
|
15
11
|
return null;
|
|
16
12
|
}
|
|
17
|
-
|
|
18
13
|
const matches = callback(parent);
|
|
19
14
|
if (matches) {
|
|
20
15
|
return parent;
|
|
21
16
|
}
|
|
22
|
-
} while (
|
|
23
|
-
|
|
17
|
+
} while (parent = parent.parentElement);
|
|
24
18
|
return null;
|
|
25
19
|
};
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return Boolean(id?.indexOf('builder-') === 0);
|
|
31
|
-
});
|
|
32
|
-
|
|
20
|
+
const findBuilderParent = (target: HTMLElement) => findParentElement(target, el => {
|
|
21
|
+
const id = el.getAttribute('builder-id') || el.id;
|
|
22
|
+
return Boolean(id?.indexOf('builder-') === 0);
|
|
23
|
+
});
|
|
33
24
|
type Offset = {
|
|
34
25
|
x: number;
|
|
35
26
|
y: number;
|
|
36
27
|
};
|
|
37
|
-
|
|
38
28
|
const computeOffset = ({
|
|
39
29
|
event,
|
|
40
|
-
target
|
|
30
|
+
target
|
|
41
31
|
}: {
|
|
42
32
|
event: MouseEvent;
|
|
43
33
|
target: HTMLElement;
|
|
@@ -45,36 +35,29 @@ const computeOffset = ({
|
|
|
45
35
|
const targetRect = target.getBoundingClientRect();
|
|
46
36
|
const xOffset = event.clientX - targetRect.left;
|
|
47
37
|
const yOffset = event.clientY - targetRect.top;
|
|
48
|
-
|
|
49
38
|
const xRatio = round(xOffset / targetRect.width);
|
|
50
39
|
const yRatio = round(yOffset / targetRect.height);
|
|
51
|
-
|
|
52
40
|
return {
|
|
53
41
|
x: xRatio,
|
|
54
|
-
y: yRatio
|
|
42
|
+
y: yRatio
|
|
55
43
|
};
|
|
56
44
|
};
|
|
57
|
-
|
|
58
45
|
export const getInteractionPropertiesForEvent = (event: MouseEvent) => {
|
|
59
|
-
const target = event.target as HTMLElement | null;
|
|
46
|
+
const target = (event.target as HTMLElement | null);
|
|
60
47
|
const targetBuilderElement = target && findBuilderParent(target);
|
|
61
|
-
const builderId =
|
|
62
|
-
targetBuilderElement?.getAttribute('builder-id') ||
|
|
63
|
-
targetBuilderElement?.id;
|
|
64
|
-
|
|
48
|
+
const builderId = targetBuilderElement?.getAttribute('builder-id') || targetBuilderElement?.id;
|
|
65
49
|
return {
|
|
66
50
|
targetBuilderElement: builderId || undefined,
|
|
67
51
|
metadata: {
|
|
68
|
-
targetOffset: target ? computeOffset({
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
},
|
|
52
|
+
targetOffset: target ? computeOffset({
|
|
53
|
+
event,
|
|
54
|
+
target
|
|
55
|
+
}) : undefined,
|
|
56
|
+
builderTargetOffset: targetBuilderElement ? computeOffset({
|
|
57
|
+
event,
|
|
58
|
+
target: targetBuilderElement
|
|
59
|
+
}) : undefined,
|
|
60
|
+
builderElementIndex: targetBuilderElement && builderId ? ([] as Element[]).slice.call(document.getElementsByClassName(builderId)).indexOf(targetBuilderElement) : undefined
|
|
61
|
+
}
|
|
79
62
|
};
|
|
80
|
-
}
|
|
63
|
+
}
|
package/src/helpers/ab-tests.ts
CHANGED
|
@@ -1,50 +1,48 @@
|
|
|
1
1
|
import type { CanTrack } from '../types/can-track.js';
|
|
2
2
|
import { getCookie, getCookieSync, setCookie } from './cookie.js';
|
|
3
|
-
import type {
|
|
4
|
-
BuilderContent,
|
|
5
|
-
BuilderContentVariation,
|
|
6
|
-
} from '../types/builder-content.js';
|
|
3
|
+
import type { BuilderContent, BuilderContentVariation } from '../types/builder-content.js';
|
|
7
4
|
import type { Nullable, Overwrite } from '../types/typescript.js';
|
|
8
5
|
import { checkIsDefined } from '../helpers/nullable.js';
|
|
9
6
|
import { logger } from './logger.js';
|
|
10
7
|
import { TARGET } from '../constants/target.js';
|
|
11
|
-
|
|
12
8
|
const BUILDER_STORE_PREFIX = 'builder.tests';
|
|
13
|
-
|
|
14
9
|
const getContentTestKey = (id: string) => `${BUILDER_STORE_PREFIX}.${id}`;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
10
|
+
const getContentVariationCookie = ({
|
|
11
|
+
contentId
|
|
12
|
+
}: {
|
|
13
|
+
contentId: string;
|
|
14
|
+
}) => getCookie({
|
|
15
|
+
name: getContentTestKey(contentId),
|
|
16
|
+
canTrack: true
|
|
17
|
+
});
|
|
18
|
+
const getContentVariationCookieSync = ({
|
|
19
|
+
contentId
|
|
20
|
+
}: {
|
|
21
|
+
contentId: string;
|
|
22
|
+
}) => getCookieSync({
|
|
23
|
+
name: getContentTestKey(contentId),
|
|
24
|
+
canTrack: true
|
|
25
|
+
});
|
|
22
26
|
const setContentVariationCookie = ({
|
|
23
27
|
contentId,
|
|
24
|
-
value
|
|
28
|
+
value
|
|
25
29
|
}: {
|
|
26
30
|
contentId: string;
|
|
27
31
|
value: string;
|
|
28
|
-
}) => setCookie({
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const checkIsBuilderContentWithVariations = (
|
|
36
|
-
item: BuilderContent
|
|
37
|
-
): item is BuilderContentWithVariations =>
|
|
38
|
-
checkIsDefined(item.id) &&
|
|
39
|
-
checkIsDefined(item.variations) &&
|
|
40
|
-
Object.keys(item.variations).length > 0;
|
|
32
|
+
}) => setCookie({
|
|
33
|
+
name: getContentTestKey(contentId),
|
|
34
|
+
value,
|
|
35
|
+
canTrack: true
|
|
36
|
+
});
|
|
37
|
+
type BuilderContentWithVariations = Overwrite<BuilderContent, Required<Pick<BuilderContent, 'variations' | 'id'>>>;
|
|
38
|
+
const checkIsBuilderContentWithVariations = (item: BuilderContent): item is BuilderContentWithVariations => checkIsDefined(item.id) && checkIsDefined(item.variations) && Object.keys(item.variations).length > 0;
|
|
41
39
|
|
|
42
40
|
/**
|
|
43
41
|
* Randomly assign a variation to a user
|
|
44
42
|
*/
|
|
45
43
|
const getRandomVariationId = ({
|
|
46
44
|
id,
|
|
47
|
-
variations
|
|
45
|
+
variations
|
|
48
46
|
}: BuilderContentWithVariations) => {
|
|
49
47
|
let n = 0;
|
|
50
48
|
const random = Math.random();
|
|
@@ -54,7 +52,6 @@ const getRandomVariationId = ({
|
|
|
54
52
|
for (const id in variations) {
|
|
55
53
|
const testRatio = variations[id]?.testRatio;
|
|
56
54
|
n += testRatio!;
|
|
57
|
-
|
|
58
55
|
if (random < n) {
|
|
59
56
|
return id;
|
|
60
57
|
}
|
|
@@ -64,7 +61,6 @@ const getRandomVariationId = ({
|
|
|
64
61
|
// if we arrive here, then it means that the random number fits in the default variation bucket.
|
|
65
62
|
return id;
|
|
66
63
|
};
|
|
67
|
-
|
|
68
64
|
const getAndSetVariantId = (args: BuilderContentWithVariations) => {
|
|
69
65
|
// if variation not found in storage, assign a random variation to this user
|
|
70
66
|
const randomVariationId = getRandomVariationId(args);
|
|
@@ -72,110 +68,99 @@ const getAndSetVariantId = (args: BuilderContentWithVariations) => {
|
|
|
72
68
|
// store variation in cookies/storage
|
|
73
69
|
setContentVariationCookie({
|
|
74
70
|
contentId: args.id,
|
|
75
|
-
value: randomVariationId
|
|
76
|
-
}).catch(
|
|
71
|
+
value: randomVariationId
|
|
72
|
+
}).catch(err => {
|
|
77
73
|
logger.error('could not store A/B test variation: ', err);
|
|
78
74
|
});
|
|
79
|
-
|
|
80
75
|
return randomVariationId;
|
|
81
76
|
};
|
|
82
|
-
|
|
83
77
|
type TestFields = {
|
|
84
78
|
data?: BuilderContentVariation['data'];
|
|
85
79
|
testVariationId?: string;
|
|
86
80
|
testVariationName: string;
|
|
87
81
|
};
|
|
88
|
-
|
|
89
82
|
const getTestFields = ({
|
|
90
83
|
item,
|
|
91
|
-
testGroupId
|
|
84
|
+
testGroupId
|
|
92
85
|
}: {
|
|
93
86
|
item: BuilderContentWithVariations;
|
|
94
87
|
testGroupId: string;
|
|
95
88
|
}): TestFields => {
|
|
96
89
|
const variationValue = item.variations[testGroupId];
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
!variationValue
|
|
101
|
-
) {
|
|
90
|
+
if (testGroupId === item.id ||
|
|
91
|
+
// handle edge-case where `testGroupId` points to non-existing variation
|
|
92
|
+
!variationValue) {
|
|
102
93
|
return {
|
|
103
94
|
testVariationId: item.id,
|
|
104
|
-
testVariationName: 'Default'
|
|
95
|
+
testVariationName: 'Default'
|
|
105
96
|
};
|
|
106
97
|
} else {
|
|
107
98
|
return {
|
|
108
99
|
data: variationValue.data,
|
|
109
100
|
testVariationId: variationValue.id,
|
|
110
|
-
testVariationName:
|
|
111
|
-
variationValue.name || (variationValue.id === item.id ? 'Default' : ''),
|
|
101
|
+
testVariationName: variationValue.name || (variationValue.id === item.id ? 'Default' : '')
|
|
112
102
|
};
|
|
113
103
|
}
|
|
114
104
|
};
|
|
115
|
-
|
|
116
105
|
export const handleABTestingSync = ({
|
|
117
106
|
item,
|
|
118
|
-
canTrack
|
|
119
|
-
}: {
|
|
107
|
+
canTrack
|
|
108
|
+
}: {
|
|
109
|
+
item: Nullable<BuilderContent>;
|
|
110
|
+
} & CanTrack): Nullable<BuilderContent> => {
|
|
120
111
|
/**
|
|
121
112
|
* We cannot SSR in React-Native.
|
|
122
113
|
*/
|
|
123
114
|
if (TARGET === 'reactNative') return item;
|
|
124
|
-
|
|
125
115
|
if (!canTrack) {
|
|
126
116
|
return item;
|
|
127
117
|
}
|
|
128
|
-
|
|
129
118
|
if (!item) {
|
|
130
119
|
return undefined;
|
|
131
120
|
}
|
|
132
|
-
|
|
133
121
|
if (!checkIsBuilderContentWithVariations(item)) {
|
|
134
122
|
return item;
|
|
135
123
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const variationValue = getTestFields({ item, testGroupId });
|
|
124
|
+
const testGroupId = getContentVariationCookieSync({
|
|
125
|
+
contentId: item.id
|
|
126
|
+
}) || getAndSetVariantId({
|
|
127
|
+
variations: item.variations,
|
|
128
|
+
id: item.id
|
|
129
|
+
});
|
|
130
|
+
const variationValue = getTestFields({
|
|
131
|
+
item,
|
|
132
|
+
testGroupId
|
|
133
|
+
});
|
|
147
134
|
return {
|
|
148
135
|
...item,
|
|
149
|
-
...variationValue
|
|
136
|
+
...variationValue
|
|
150
137
|
};
|
|
151
138
|
};
|
|
152
|
-
|
|
153
139
|
export const handleABTesting = async ({
|
|
154
140
|
item,
|
|
155
|
-
canTrack
|
|
156
|
-
}: {
|
|
141
|
+
canTrack
|
|
142
|
+
}: {
|
|
143
|
+
item: BuilderContent;
|
|
144
|
+
} & CanTrack): Promise<BuilderContent> => {
|
|
157
145
|
if (!canTrack) {
|
|
158
146
|
return item;
|
|
159
147
|
}
|
|
160
|
-
|
|
161
148
|
if (!checkIsBuilderContentWithVariations(item)) {
|
|
162
149
|
return item;
|
|
163
150
|
}
|
|
164
|
-
|
|
165
151
|
const cookieValue = await getContentVariationCookie({
|
|
166
|
-
contentId: item.id
|
|
152
|
+
contentId: item.id
|
|
153
|
+
});
|
|
154
|
+
const testGroupId = cookieValue || getAndSetVariantId({
|
|
155
|
+
variations: item.variations,
|
|
156
|
+
id: item.id
|
|
157
|
+
});
|
|
158
|
+
const variationValue = getTestFields({
|
|
159
|
+
item,
|
|
160
|
+
testGroupId
|
|
167
161
|
});
|
|
168
|
-
|
|
169
|
-
const testGroupId =
|
|
170
|
-
cookieValue ||
|
|
171
|
-
getAndSetVariantId({
|
|
172
|
-
variations: item.variations,
|
|
173
|
-
id: item.id,
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
const variationValue = getTestFields({ item, testGroupId });
|
|
177
162
|
return {
|
|
178
163
|
...item,
|
|
179
|
-
...variationValue
|
|
164
|
+
...variationValue
|
|
180
165
|
};
|
|
181
|
-
}
|
|
166
|
+
}
|
package/src/helpers/canTrack.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import { checkIsDefined } from './nullable';
|
|
2
|
-
|
|
3
|
-
export const getDefaultCanTrack = (canTrack?: boolean) =>
|
|
4
|
-
checkIsDefined(canTrack) ? canTrack : true;
|
|
1
|
+
import { checkIsDefined } from './nullable.js';
|
|
2
|
+
export const getDefaultCanTrack = (canTrack?: boolean) => checkIsDefined(canTrack) ? canTrack : true
|
package/src/helpers/cookie.ts
CHANGED
|
@@ -3,14 +3,12 @@ import type { CanTrack } from '../types/can-track.js';
|
|
|
3
3
|
import { logger } from './logger.js';
|
|
4
4
|
import { checkIsDefined } from './nullable.js';
|
|
5
5
|
import { getTopLevelDomain } from './url.js';
|
|
6
|
-
|
|
7
6
|
type GetCookieArgs = {
|
|
8
7
|
name: string;
|
|
9
8
|
} & CanTrack;
|
|
10
|
-
|
|
11
9
|
export const getCookieSync = ({
|
|
12
10
|
name,
|
|
13
|
-
canTrack
|
|
11
|
+
canTrack
|
|
14
12
|
}: GetCookieArgs): string | undefined => {
|
|
15
13
|
try {
|
|
16
14
|
if (!canTrack) {
|
|
@@ -21,10 +19,7 @@ export const getCookieSync = ({
|
|
|
21
19
|
* Extracted from MDN docs
|
|
22
20
|
* https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie#example_2_get_a_sample_cookie_named_test2
|
|
23
21
|
*/
|
|
24
|
-
return document.cookie
|
|
25
|
-
.split('; ')
|
|
26
|
-
.find((row) => row.startsWith(`${name}=`))
|
|
27
|
-
?.split('=')[1];
|
|
22
|
+
return document.cookie.split('; ').find(row => row.startsWith(`${name}=`))?.split('=')[1];
|
|
28
23
|
} catch (err: any) {
|
|
29
24
|
logger.warn('[COOKIE] GET error: ', err?.message || err);
|
|
30
25
|
return undefined;
|
|
@@ -35,55 +30,25 @@ export const getCookieSync = ({
|
|
|
35
30
|
* The sync version is only safe to use in code blocks that `react-native` is guaranteed not to not run.
|
|
36
31
|
*/
|
|
37
32
|
export const getCookie = async (args: GetCookieArgs) => getCookieSync(args);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
| ['path', '/']
|
|
42
|
-
| ['domain', string]
|
|
43
|
-
| ['secure', '']
|
|
44
|
-
| ['SameSite', 'None']
|
|
45
|
-
| [string, string]
|
|
46
|
-
| []
|
|
47
|
-
>;
|
|
48
|
-
|
|
49
|
-
const stringifyCookie = (cookie: CookieConfiguration): string =>
|
|
50
|
-
cookie
|
|
51
|
-
.map(([key, value]) => (value ? `${key}=${value}` : key))
|
|
52
|
-
.filter(checkIsDefined)
|
|
53
|
-
.join('; ');
|
|
54
|
-
|
|
55
|
-
const SECURE_CONFIG: CookieConfiguration = [
|
|
56
|
-
['secure', ''],
|
|
57
|
-
['SameSite', 'None'],
|
|
58
|
-
];
|
|
59
|
-
|
|
33
|
+
type CookieConfiguration = Array<['expires', string] | ['path', '/'] | ['domain', string] | ['secure', ''] | ['SameSite', 'None'] | [string, string] | []>;
|
|
34
|
+
const stringifyCookie = (cookie: CookieConfiguration): string => cookie.map(([key, value]) => value ? `${key}=${value}` : key).filter(checkIsDefined).join('; ');
|
|
35
|
+
const SECURE_CONFIG: CookieConfiguration = [['secure', ''], ['SameSite', 'None']];
|
|
60
36
|
const createCookieString = ({
|
|
61
37
|
name,
|
|
62
38
|
value,
|
|
63
|
-
expires
|
|
39
|
+
expires
|
|
64
40
|
}: {
|
|
65
41
|
name: string;
|
|
66
42
|
value: string;
|
|
67
43
|
expires?: Date;
|
|
68
44
|
}) => {
|
|
69
45
|
const secure = isBrowser() ? location.protocol === 'https:' : true;
|
|
70
|
-
|
|
71
46
|
const secureObj: CookieConfiguration = secure ? SECURE_CONFIG : [[]];
|
|
72
47
|
|
|
73
48
|
// TODO: need to know if secure server side
|
|
74
|
-
const expiresObj: CookieConfiguration = expires
|
|
75
|
-
|
|
76
|
-
: [[]];
|
|
77
|
-
|
|
78
|
-
const cookieValue: CookieConfiguration = [
|
|
79
|
-
[name, value],
|
|
80
|
-
...expiresObj,
|
|
81
|
-
['path', '/'],
|
|
82
|
-
['domain', getTopLevelDomain(window.location.hostname)],
|
|
83
|
-
...secureObj,
|
|
84
|
-
];
|
|
49
|
+
const expiresObj: CookieConfiguration = expires ? [['expires', expires.toUTCString()]] : [[]];
|
|
50
|
+
const cookieValue: CookieConfiguration = [[name, value], ...expiresObj, ['path', '/'], ['domain', getTopLevelDomain(window.location.hostname)], ...secureObj];
|
|
85
51
|
const cookie = stringifyCookie(cookieValue);
|
|
86
|
-
|
|
87
52
|
return cookie;
|
|
88
53
|
};
|
|
89
54
|
|
|
@@ -94,7 +59,7 @@ export const setCookie = async ({
|
|
|
94
59
|
name,
|
|
95
60
|
value,
|
|
96
61
|
expires,
|
|
97
|
-
canTrack
|
|
62
|
+
canTrack
|
|
98
63
|
}: {
|
|
99
64
|
name: string;
|
|
100
65
|
value: string;
|
|
@@ -104,9 +69,13 @@ export const setCookie = async ({
|
|
|
104
69
|
if (!canTrack) {
|
|
105
70
|
return;
|
|
106
71
|
}
|
|
107
|
-
const cookie = createCookieString({
|
|
72
|
+
const cookie = createCookieString({
|
|
73
|
+
name,
|
|
74
|
+
value,
|
|
75
|
+
expires
|
|
76
|
+
});
|
|
108
77
|
document.cookie = cookie;
|
|
109
78
|
} catch (err: any) {
|
|
110
79
|
logger.warn('[COOKIE] SET error: ', err?.message || err);
|
|
111
80
|
}
|
|
112
|
-
}
|
|
81
|
+
}
|
package/src/helpers/css.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { camelToKebabCase } from '../functions/camel-to-kebab-case.js';
|
|
2
2
|
import { checkIsDefined } from './nullable.js';
|
|
3
|
-
|
|
4
|
-
export const convertStyleMapToCSSArray = (
|
|
5
|
-
style: Partial<CSSStyleDeclaration>
|
|
6
|
-
): string[] => {
|
|
3
|
+
export const convertStyleMapToCSSArray = (style: Partial<CSSStyleDeclaration>): string[] => {
|
|
7
4
|
const cssProps = Object.entries(style).map(([key, value]) => {
|
|
8
5
|
if (typeof value === 'string') {
|
|
9
6
|
return `${camelToKebabCase(key)}: ${value};`;
|
|
@@ -11,18 +8,13 @@ export const convertStyleMapToCSSArray = (
|
|
|
11
8
|
return undefined;
|
|
12
9
|
}
|
|
13
10
|
});
|
|
14
|
-
|
|
15
11
|
return cssProps.filter(checkIsDefined);
|
|
16
12
|
};
|
|
17
|
-
|
|
18
|
-
export const convertStyleMapToCSS = (
|
|
19
|
-
style: Partial<CSSStyleDeclaration>
|
|
20
|
-
): string => convertStyleMapToCSSArray(style).join('\n');
|
|
21
|
-
|
|
13
|
+
export const convertStyleMapToCSS = (style: Partial<CSSStyleDeclaration>): string => convertStyleMapToCSSArray(style).join('\n');
|
|
22
14
|
export const createCssClass = ({
|
|
23
15
|
mediaQuery,
|
|
24
16
|
className,
|
|
25
|
-
styles
|
|
17
|
+
styles
|
|
26
18
|
}: {
|
|
27
19
|
mediaQuery?: string;
|
|
28
20
|
className: string;
|
|
@@ -31,7 +23,6 @@ export const createCssClass = ({
|
|
|
31
23
|
const cssClass = `.${className} {
|
|
32
24
|
${convertStyleMapToCSS(styles)}
|
|
33
25
|
}`;
|
|
34
|
-
|
|
35
26
|
if (mediaQuery) {
|
|
36
27
|
return `${mediaQuery} {
|
|
37
28
|
${cssClass}
|
|
@@ -39,4 +30,4 @@ export const createCssClass = ({
|
|
|
39
30
|
} else {
|
|
40
31
|
return cssClass;
|
|
41
32
|
}
|
|
42
|
-
}
|
|
33
|
+
}
|
package/src/helpers/flatten.ts
CHANGED
|
@@ -3,22 +3,17 @@
|
|
|
3
3
|
*
|
|
4
4
|
* { foo: { bar: 'baz' }} -> { 'foo.bar': 'baz' }
|
|
5
5
|
*/
|
|
6
|
-
export function flatten<T extends Record<string, any>>(
|
|
7
|
-
object: T,
|
|
8
|
-
path: string | null = null,
|
|
9
|
-
separator = '.'
|
|
10
|
-
): T {
|
|
6
|
+
export function flatten<T extends Record<string, any>>(object: T, path: string | null = null, separator = '.'): T {
|
|
11
7
|
return Object.keys(object).reduce((acc: T, key: string): T => {
|
|
12
8
|
const value = object[key];
|
|
13
9
|
const newPath = [path, key].filter(Boolean).join(separator);
|
|
14
|
-
const isObject = [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
10
|
+
const isObject = [typeof value === 'object', value !== null, !(Array.isArray(value) && value.length === 0)].every(Boolean);
|
|
11
|
+
return isObject ? {
|
|
12
|
+
...acc,
|
|
13
|
+
...flatten(value, newPath, separator)
|
|
14
|
+
} : {
|
|
15
|
+
...acc,
|
|
16
|
+
[newPath]: value
|
|
17
|
+
};
|
|
18
|
+
}, ({} as T));
|
|
19
|
+
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { isBrowser } from '../functions/is-browser.js';
|
|
2
2
|
import type { CanTrack } from '../types/can-track.js';
|
|
3
3
|
import type { Nullable } from './nullable.js';
|
|
4
|
-
|
|
5
|
-
const getLocalStorage = () =>
|
|
6
|
-
isBrowser() && typeof localStorage !== 'undefined' ? localStorage : undefined;
|
|
7
|
-
|
|
4
|
+
const getLocalStorage = () => isBrowser() && typeof localStorage !== 'undefined' ? localStorage : undefined;
|
|
8
5
|
export const getLocalStorageItem = ({
|
|
9
6
|
key,
|
|
10
|
-
canTrack
|
|
7
|
+
canTrack
|
|
11
8
|
}: {
|
|
12
9
|
key: string;
|
|
13
10
|
} & CanTrack): Nullable<string> => {
|
|
@@ -21,11 +18,10 @@ export const getLocalStorageItem = ({
|
|
|
21
18
|
return undefined;
|
|
22
19
|
}
|
|
23
20
|
};
|
|
24
|
-
|
|
25
21
|
export const setLocalStorageItem = ({
|
|
26
22
|
key,
|
|
27
23
|
canTrack,
|
|
28
|
-
value
|
|
24
|
+
value
|
|
29
25
|
}: {
|
|
30
26
|
key: string;
|
|
31
27
|
value: string;
|
|
@@ -37,4 +33,4 @@ export const setLocalStorageItem = ({
|
|
|
37
33
|
} catch (err) {
|
|
38
34
|
console.debug('[LocalStorage] SET error: ', err);
|
|
39
35
|
}
|
|
40
|
-
}
|
|
36
|
+
}
|
package/src/helpers/logger.ts
CHANGED
|
@@ -3,5 +3,5 @@ export const logger = {
|
|
|
3
3
|
log: (...message: any[]) => console.log(MSG_PREFIX, ...message),
|
|
4
4
|
error: (...message: any[]) => console.error(MSG_PREFIX, ...message),
|
|
5
5
|
warn: (...message: any[]) => console.warn(MSG_PREFIX, ...message),
|
|
6
|
-
debug: (...message: any[]) => console.debug(MSG_PREFIX, ...message)
|
|
7
|
-
}
|
|
6
|
+
debug: (...message: any[]) => console.debug(MSG_PREFIX, ...message)
|
|
7
|
+
}
|
package/src/helpers/nullable.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
1
|
export type Nullable<T> = T | null | undefined;
|
|
2
|
-
|
|
3
|
-
export const checkIsDefined = <T>(maybeT: Nullable<T>): maybeT is T =>
|
|
4
|
-
maybeT !== null && maybeT !== undefined;
|
|
2
|
+
export const checkIsDefined = <T,>(maybeT: Nullable<T>): maybeT is T => maybeT !== null && maybeT !== undefined
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { getIdFromSearchParams } from './helpers';
|
|
2
|
+
import { init } from './init';
|
|
3
|
+
import type { GlobalWCache } from './types';
|
|
4
|
+
export function getPreviewContent(searchParams: URLSearchParams) {
|
|
5
|
+
init();
|
|
6
|
+
const id = getIdFromSearchParams(searchParams);
|
|
7
|
+
return typeof id === 'string' ? (globalThis as GlobalWCache)._BUILDER_PREVIEW_LRU_CACHE.get(id) : undefined;
|
|
8
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { logger } from '../logger';
|
|
2
|
+
export const getIdFromSearchParams = (searchParams: URLSearchParams) => {
|
|
3
|
+
const previewedModel = searchParams.get('preview');
|
|
4
|
+
const previewedId = searchParams.get('overrides.' + previewedModel);
|
|
5
|
+
if (!previewedId) {
|
|
6
|
+
logger.warn('No previewed ID found in search params.');
|
|
7
|
+
}
|
|
8
|
+
return previewedId;
|
|
9
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is an LRU cache to hold preview content on the server-side.
|
|
3
|
+
*
|
|
4
|
+
* Note: This logic is only used by the NextJS SDK.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { LRUCache } from 'lru-cache';
|
|
8
|
+
import type { GlobalWCache } from './types';
|
|
9
|
+
export function init() {
|
|
10
|
+
if (!(globalThis as GlobalWCache)._BUILDER_PREVIEW_LRU_CACHE) {
|
|
11
|
+
(globalThis as GlobalWCache)._BUILDER_PREVIEW_LRU_CACHE = new LRUCache({
|
|
12
|
+
max: 500,
|
|
13
|
+
// how long to live in ms
|
|
14
|
+
ttl: 1000 * 60 * 5
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use server';
|
|
2
|
+
|
|
3
|
+
import type { BuilderContent } from '../../types/builder-content';
|
|
4
|
+
import { init } from './init';
|
|
5
|
+
import type { GlobalWCache } from './types';
|
|
6
|
+
export async function postPreviewContent({
|
|
7
|
+
key,
|
|
8
|
+
value
|
|
9
|
+
}: {
|
|
10
|
+
key: string;
|
|
11
|
+
value: BuilderContent;
|
|
12
|
+
}) {
|
|
13
|
+
init();
|
|
14
|
+
(globalThis as GlobalWCache)._BUILDER_PREVIEW_LRU_CACHE.set(key, value);
|
|
15
|
+
return {
|
|
16
|
+
[key]: value
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is an LRU cache to hold preview content on the server-side.
|
|
3
|
+
*
|
|
4
|
+
* Note: This logic is only used by the NextJS SDK.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { LRUCache } from 'lru-cache';
|
|
8
|
+
import type { BuilderContent } from '../../types/builder-content';
|
|
9
|
+
type BuilderLRUCache = LRUCache<string, BuilderContent>;
|
|
10
|
+
export type GlobalWCache = typeof globalThis & {
|
|
11
|
+
_BUILDER_PREVIEW_LRU_CACHE: BuilderLRUCache;
|
|
12
|
+
}
|