@plone/aurora 1.0.0-alpha.5

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.
Files changed (41) hide show
  1. package/.release-it.json +29 -0
  2. package/.stylelintrc +8 -0
  3. package/CHANGELOG.md +211 -0
  4. package/LICENSE.md +21 -0
  5. package/Makefile +82 -0
  6. package/README.md +33 -0
  7. package/acceptance/tests/basic.test.ts +14 -0
  8. package/app/config/index.test.ts +19 -0
  9. package/app/config/index.ts +15 -0
  10. package/app/config/server/content-migrations.server.test.ts +184 -0
  11. package/app/config/server/content-migrations.server.ts +59 -0
  12. package/app/config/server/migrations.server.ts +137 -0
  13. package/app/config/server.server.test.ts +33 -0
  14. package/app/config/server.server.ts +52 -0
  15. package/app/config/types.ts +42 -0
  16. package/app/entry.client.tsx +51 -0
  17. package/app/entry.server.tsx +81 -0
  18. package/app/i18n.ts +11 -0
  19. package/app/i18next.server.ts +26 -0
  20. package/app/middleware.server.test.ts +1088 -0
  21. package/app/middleware.server.ts +241 -0
  22. package/app/okroute.test.tsx +9 -0
  23. package/app/okroute.tsx +5 -0
  24. package/app/reset-fetcher.test.tsx +13 -0
  25. package/app/reset-fetcher.tsx +7 -0
  26. package/app/root.test.tsx +300 -0
  27. package/app/root.tsx +178 -0
  28. package/app/routes.ts +20 -0
  29. package/news/.gitkeep +0 -0
  30. package/package.json +103 -0
  31. package/public/favicon.ico +0 -0
  32. package/public/icon.svg +13 -0
  33. package/react-router.config.ts +5 -0
  34. package/registry-publicui.config.ts +11 -0
  35. package/registry.config.ts +13 -0
  36. package/tests/setup.ts +6 -0
  37. package/tests/testHelpers.tsx +29 -0
  38. package/towncrier.toml +33 -0
  39. package/tsconfig.json +35 -0
  40. package/vite.config.ts +111 -0
  41. package/vitest.config.ts +32 -0
@@ -0,0 +1,59 @@
1
+ import config from '@plone/registry';
2
+ import type { Value } from '@plone/plate/components/editor';
3
+ import type { Content } from '@plone/types';
4
+ import '../types';
5
+ import { SOMERSAULT_KEY } from '@plone/plate/constants';
6
+
7
+ type SomersaultValue = Value;
8
+
9
+ const createSomersaultValue = (content: Content): SomersaultValue => {
10
+ const orderedBlockIds = Array.isArray(content.blocks_layout?.items)
11
+ ? content.blocks_layout.items
12
+ : [];
13
+ const somersaultBlockMigrations = config.getUtilities({
14
+ type: 'somersaultBlockMigration',
15
+ });
16
+
17
+ return orderedBlockIds.flatMap((blockId) => {
18
+ const block = content.blocks?.[blockId] as
19
+ Record<string, unknown> | undefined;
20
+ if (!block) return [];
21
+
22
+ return somersaultBlockMigrations.flatMap(
23
+ (utility) =>
24
+ utility.method({
25
+ block,
26
+ blockId,
27
+ content,
28
+ }) as SomersaultValue,
29
+ );
30
+ });
31
+ };
32
+
33
+ export const migrateContent = (content: Content) => {
34
+ if (content.blocks?.[SOMERSAULT_KEY]) return content;
35
+ if (!content.blocks || !Array.isArray(content.blocks_layout?.items)) {
36
+ return content;
37
+ }
38
+
39
+ const somersaultMigrations = config.getUtilities({
40
+ type: 'somersaultMigration',
41
+ });
42
+
43
+ const initialValue = createSomersaultValue(content);
44
+ const migratedValue = somersaultMigrations.reduce<SomersaultValue>(
45
+ (value, utility) =>
46
+ utility.method({
47
+ content,
48
+ value,
49
+ }) as SomersaultValue,
50
+ initialValue,
51
+ );
52
+
53
+ content.blocks[SOMERSAULT_KEY] = {
54
+ '@type': SOMERSAULT_KEY,
55
+ value: migratedValue,
56
+ } as (typeof content.blocks)[string];
57
+
58
+ return content;
59
+ };
@@ -0,0 +1,137 @@
1
+ import config from '@plone/registry';
2
+ import {
3
+ getStyleFieldsFromBlockSchema,
4
+ PLONE_BLOCK_TYPE,
5
+ } from '@plone/helpers';
6
+ import type { BlockConfigBase, BlocksFormData } from '@plone/types';
7
+ import {
8
+ migrateLegacyBoldInValue,
9
+ migrateLegacyBlockWidthsInValue,
10
+ migrateLegacyItalicInValue,
11
+ migrateLegacyLinksInValueStatic,
12
+ migrateLegacyListsInValue,
13
+ migrateLegacyStrikethroughInValue,
14
+ } from '@plone/plate/migrations';
15
+ import type {
16
+ SomersaultBlockMigrationArgs,
17
+ SomersaultMigrationArgs,
18
+ } from '../types';
19
+
20
+ const isRegisteredNativeBlock = (block: Record<string, unknown>) => {
21
+ const blockType = block['@type'];
22
+ if (typeof blockType !== 'string') return false;
23
+
24
+ const blocksConfig = config.blocks?.blocksConfig as
25
+ Record<string, unknown> | undefined;
26
+
27
+ return Boolean(blocksConfig?.[blockType]);
28
+ };
29
+
30
+ const DEFAULT_BLOCK_WIDTH = 'default';
31
+
32
+ const getMigratedPloneBlockWidth = (block: Record<string, unknown>) => {
33
+ const blockType = block['@type'];
34
+
35
+ if (typeof blockType !== 'string') {
36
+ return DEFAULT_BLOCK_WIDTH;
37
+ }
38
+
39
+ const blocksConfig = config.blocks?.blocksConfig as
40
+ Record<string, BlockConfigBase> | undefined;
41
+ const blockConfig = blocksConfig?.[blockType];
42
+ const styleFields = getStyleFieldsFromBlockSchema(
43
+ blockConfig,
44
+ block as BlocksFormData,
45
+ );
46
+
47
+ return (
48
+ styleFields.blockWidth?.defaultValue ??
49
+ blockConfig?.defaultBlockWidth ??
50
+ DEFAULT_BLOCK_WIDTH
51
+ );
52
+ };
53
+
54
+ export default function install() {
55
+ config.registerUtility({
56
+ name: 'somersaultBlockMigrationTitle',
57
+ type: 'somersaultBlockMigration',
58
+ method: ({ block, content }: SomersaultBlockMigrationArgs) =>
59
+ block['@type'] === 'title'
60
+ ? [
61
+ {
62
+ type: 'title',
63
+ children: [
64
+ {
65
+ text: typeof content.title === 'string' ? content.title : '',
66
+ },
67
+ ],
68
+ },
69
+ ]
70
+ : [],
71
+ });
72
+
73
+ config.registerUtility({
74
+ name: 'somersaultBlockMigrationLegacyValue',
75
+ type: 'somersaultBlockMigration',
76
+ method: ({ block }: SomersaultBlockMigrationArgs) =>
77
+ Array.isArray(block.value) ? block.value : [],
78
+ });
79
+
80
+ config.registerUtility({
81
+ name: 'somersaultBlockMigrationUnknown',
82
+ type: 'somersaultBlockMigration',
83
+ method: ({ block }: SomersaultBlockMigrationArgs) =>
84
+ isRegisteredNativeBlock(block) && !Array.isArray(block.value)
85
+ ? [
86
+ {
87
+ ...block,
88
+ blockWidth: getMigratedPloneBlockWidth(block),
89
+ type: PLONE_BLOCK_TYPE,
90
+ children: [{ text: '' }],
91
+ },
92
+ ]
93
+ : [],
94
+ });
95
+
96
+ config.registerUtility({
97
+ name: 'somersaultMigrationLegacyBold',
98
+ type: 'somersaultMigration',
99
+ method: ({ value }: SomersaultMigrationArgs) =>
100
+ migrateLegacyBoldInValue(value),
101
+ });
102
+
103
+ config.registerUtility({
104
+ name: 'somersaultMigrationLegacyItalic',
105
+ type: 'somersaultMigration',
106
+ method: ({ value }: SomersaultMigrationArgs) =>
107
+ migrateLegacyItalicInValue(value),
108
+ });
109
+
110
+ config.registerUtility({
111
+ name: 'somersaultMigrationLegacyStrikethrough',
112
+ type: 'somersaultMigration',
113
+ method: ({ value }: SomersaultMigrationArgs) =>
114
+ migrateLegacyStrikethroughInValue(value),
115
+ });
116
+
117
+ config.registerUtility({
118
+ name: 'somersaultMigrationLegacyLinks',
119
+ type: 'somersaultMigration',
120
+ method: ({ value }: SomersaultMigrationArgs) =>
121
+ migrateLegacyLinksInValueStatic(value),
122
+ });
123
+
124
+ config.registerUtility({
125
+ name: 'somersaultMigrationLegacyLists',
126
+ type: 'somersaultMigration',
127
+ method: ({ value }: SomersaultMigrationArgs) =>
128
+ migrateLegacyListsInValue(value),
129
+ });
130
+
131
+ config.registerUtility({
132
+ name: 'somersaultMigrationBlockWidths',
133
+ type: 'somersaultMigration',
134
+ method: ({ value }: SomersaultMigrationArgs) =>
135
+ migrateLegacyBlockWidthsInValue(value),
136
+ });
137
+ }
@@ -0,0 +1,33 @@
1
+ import { expect, describe, it, afterEach } from 'vitest';
2
+ import config from '@plone/registry';
3
+ import installServer from './server.server';
4
+
5
+ describe('config/server', () => {
6
+ afterEach(() => {
7
+ config.settings = {};
8
+ const utilities = config.utilities as Partial<Record<string, unknown>>;
9
+ delete utilities.client;
10
+ delete utilities.somersaultBlockMigration;
11
+ delete utilities.somersaultMigration;
12
+ delete utilities.rootContentSubRequest;
13
+ });
14
+
15
+ it('should set the default config', () => {
16
+ installServer();
17
+ expect(config.settings).toStrictEqual(
18
+ expect.objectContaining({
19
+ supportedLanguages: ['en'],
20
+ defaultLanguage: 'en',
21
+ apiPath: 'http://localhost:8080/Plone',
22
+ }),
23
+ );
24
+ expect(
25
+ config
26
+ .getUtility({
27
+ name: 'ploneClient',
28
+ type: 'client',
29
+ })
30
+ .method(),
31
+ ).toHaveProperty('initialize');
32
+ });
33
+ });
@@ -0,0 +1,52 @@
1
+ /**
2
+ * This is the server side config entry point
3
+ */
4
+ import config from '@plone/registry';
5
+ import PloneClient from '@plone/client';
6
+ // eslint-disable-next-line import/no-unresolved
7
+ import applyAddonConfiguration from '../../.plone/registry.loader';
8
+ // eslint-disable-next-line import/no-unresolved
9
+ import applyServerAddonConfiguration from '../../.plone/registry.loader.server';
10
+
11
+ import type { ListingBlockFormData } from '@plone/types';
12
+ import type { LoaderUtilityArgs } from './types';
13
+ import installMigrations from './server/migrations.server';
14
+
15
+ export default function install() {
16
+ config.settings.apiPath =
17
+ process.env.PLONE_API_PATH || 'http://localhost:8080/Plone';
18
+
19
+ config.registerUtility({
20
+ name: 'ploneClient',
21
+ type: 'client',
22
+ method: () => PloneClient,
23
+ });
24
+
25
+ config.registerUtility({
26
+ name: 'ListingBlocksContentLoading',
27
+ type: 'rootContentSubRequest',
28
+ method: async (args: LoaderUtilityArgs) => {
29
+ const listingBlocks: { id: string; block: ListingBlockFormData }[] =
30
+ Object.entries(args.content.blocks ?? {})
31
+ .filter(([, block]) => block['@type'] === 'listing')
32
+ .map(([id, block]) => ({ id, block: block as ListingBlockFormData }));
33
+ for (let i = 0; i < listingBlocks.length; i++) {
34
+ const { id, block } = listingBlocks[i];
35
+ if (block.querystring) {
36
+ const results = await args.cli.querystringSearch(block.querystring);
37
+ args.content.blocks[id].items = results.data.items;
38
+ }
39
+ }
40
+ },
41
+ });
42
+
43
+ installMigrations();
44
+
45
+ config.settings.defaultLanguage = 'en';
46
+ config.settings.supportedLanguages = ['en'];
47
+
48
+ applyAddonConfiguration(config);
49
+ applyServerAddonConfiguration(config);
50
+
51
+ return config;
52
+ }
@@ -0,0 +1,42 @@
1
+ import type { Content } from '@plone/types';
2
+ import type PloneClient from '@plone/client';
3
+ import type { Value } from '@plone/plate/components/editor';
4
+ import type { Params } from 'react-router';
5
+
6
+ export type PloneClientUtility = typeof PloneClient;
7
+
8
+ declare module '@plone/types' {
9
+ interface UtilityTypeMap {
10
+ client: () => PloneClientUtility;
11
+ rootContentSubRequest: (args: LoaderUtilityArgs) => Promise<unknown>;
12
+ rootLoaderData: (
13
+ args: LoaderUtilityArgs,
14
+ ) => Promise<{ status: number; data: unknown }>;
15
+ somersaultBlockMigration: (
16
+ args: SomersaultBlockMigrationArgs,
17
+ ) => SomersaultMigrationArgs['value'];
18
+ somersaultMigration: (
19
+ args: SomersaultMigrationArgs,
20
+ ) => SomersaultMigrationArgs['value'];
21
+ }
22
+ }
23
+
24
+ export interface LoaderUtilityArgs {
25
+ cli: PloneClient;
26
+ content: Content;
27
+ request: Request;
28
+ path: string;
29
+ params: Params;
30
+ locale: string;
31
+ }
32
+
33
+ export interface SomersaultMigrationArgs {
34
+ content: Content;
35
+ value: Value;
36
+ }
37
+
38
+ export interface SomersaultBlockMigrationArgs {
39
+ block: Record<string, unknown>;
40
+ blockId: string;
41
+ content: Content;
42
+ }
@@ -0,0 +1,51 @@
1
+ import { startTransition, StrictMode } from 'react';
2
+ import { hydrateRoot } from 'react-dom/client';
3
+ import { HydratedRouter } from 'react-router/dom';
4
+ import i18n from './i18n';
5
+ import i18next from 'i18next';
6
+ import { I18nextProvider, initReactI18next } from 'react-i18next';
7
+ import LanguageDetector from 'i18next-browser-languagedetector';
8
+ import Backend from 'i18next-http-backend';
9
+ import install from './config';
10
+
11
+ install();
12
+
13
+ async function hydrate() {
14
+ await i18next
15
+ .use(initReactI18next) // Tell i18next to use the react-i18next plugin
16
+ .use(LanguageDetector) // Setup a client-side language detector
17
+ .use(Backend) // Setup your backend
18
+ .init({
19
+ ...i18n, // spread the configuration
20
+ ns: i18n.defaultNS,
21
+ backend: { loadPath: '/locales/{{lng}}/{{ns}}.json' },
22
+ detection: {
23
+ // Here only enable htmlTag detection, we'll detect the language only
24
+ // server-side with remix-i18next, by using the `<html lang>` attribute
25
+ // we can communicate to the client the language detected server-side
26
+ order: ['htmlTag'],
27
+ // Because we only use htmlTag, there's no reason to cache the language
28
+ // on the browser, so we disable it
29
+ caches: [],
30
+ },
31
+ });
32
+
33
+ startTransition(() => {
34
+ hydrateRoot(
35
+ document,
36
+ <I18nextProvider i18n={i18next}>
37
+ <StrictMode>
38
+ <HydratedRouter />
39
+ </StrictMode>
40
+ </I18nextProvider>,
41
+ );
42
+ });
43
+ }
44
+
45
+ if (window.requestIdleCallback) {
46
+ window.requestIdleCallback(hydrate);
47
+ } else {
48
+ // Safari doesn't support requestIdleCallback
49
+ // https://caniuse.com/requestidlecallback
50
+ window.setTimeout(hydrate, 1);
51
+ }
@@ -0,0 +1,81 @@
1
+ import { PassThrough } from 'node:stream';
2
+
3
+ import type {
4
+ // AppLoadContext,
5
+ EntryContext,
6
+ RouterContextProvider,
7
+ } from 'react-router';
8
+ import { createReadableStreamFromReadable } from '@react-router/node';
9
+ import { ServerRouter } from 'react-router';
10
+ import { isbot } from 'isbot';
11
+ import type { RenderToPipeableStreamOptions } from 'react-dom/server';
12
+ import { renderToPipeableStream } from 'react-dom/server';
13
+ import { getInstance } from './i18next.server';
14
+ import { I18nextProvider } from 'react-i18next';
15
+
16
+ export const streamTimeout = 5_000;
17
+
18
+ export default async function handleRequest(
19
+ request: Request,
20
+ responseStatusCode: number,
21
+ responseHeaders: Headers,
22
+ routerContext: EntryContext,
23
+ // loadContext: AppLoadContext,
24
+ // If you have middleware enabled:
25
+ loadContext: RouterContextProvider,
26
+ ) {
27
+ // Ensure requests from bots and SPA Mode renders wait for all content to load before responding
28
+ // https://react.dev/reference/react-dom/server/renderToPipeableStream#waiting-for-all-content-to-load-for-crawlers-and-static-generation
29
+ const userAgent = request.headers.get('user-agent');
30
+ const readyOption: keyof RenderToPipeableStreamOptions =
31
+ (userAgent && isbot(userAgent)) || routerContext.isSpaMode
32
+ ? 'onAllReady'
33
+ : 'onShellReady';
34
+
35
+ const instance = getInstance(loadContext);
36
+
37
+ return new Promise((resolve, reject) => {
38
+ let shellRendered = false;
39
+
40
+ const { pipe, abort } = renderToPipeableStream(
41
+ <I18nextProvider i18n={instance}>
42
+ <ServerRouter context={routerContext} url={request.url} />
43
+ </I18nextProvider>,
44
+ {
45
+ [readyOption]() {
46
+ shellRendered = true;
47
+ const body = new PassThrough();
48
+ const stream = createReadableStreamFromReadable(body);
49
+
50
+ responseHeaders.set('Content-Type', 'text/html');
51
+
52
+ resolve(
53
+ new Response(stream, {
54
+ headers: responseHeaders,
55
+ status: responseStatusCode,
56
+ }),
57
+ );
58
+
59
+ pipe(body);
60
+ },
61
+ onShellError(error: unknown) {
62
+ reject(error);
63
+ },
64
+ onError(error: unknown) {
65
+ responseStatusCode = 500;
66
+ // Log streaming rendering errors from inside the shell. Don't log
67
+ // errors encountered during initial shell rendering since they'll
68
+ // reject and get logged in handleDocumentRequest.
69
+ if (shellRendered) {
70
+ // eslint-disable-next-line no-console
71
+ console.error(error);
72
+ }
73
+ },
74
+ },
75
+ );
76
+
77
+ // Abort the rendering stream after the `streamTimeout` so it has time to
78
+ // flush down the rejected boundaries
79
+ setTimeout(abort, streamTimeout + 1000);
80
+ });
81
+ }
package/app/i18n.ts ADDED
@@ -0,0 +1,11 @@
1
+ import config from '@plone/registry';
2
+
3
+ export default {
4
+ // This is the list of languages your application supports
5
+ supportedLngs: config.settings.supportedLanguages ?? ['en'],
6
+ // This is the language you want to use in case
7
+ // if the user language is not in the supportedLngs
8
+ fallbackLng: (config.settings.defaultLanguage as string | undefined) ?? 'en',
9
+ // The default namespace of i18next is "translation", but you can customize it here
10
+ defaultNS: 'common',
11
+ };
@@ -0,0 +1,26 @@
1
+ import Backend from 'i18next-fs-backend/cjs';
2
+ import { resolve } from 'node:path';
3
+ import { initReactI18next } from 'react-i18next';
4
+ import { createI18nextMiddleware } from 'remix-i18next';
5
+ import i18n from './i18n'; // your i18n configuration file
6
+
7
+ export const [i18nextMiddleware, getLocale, getInstance] =
8
+ createI18nextMiddleware({
9
+ detection: {
10
+ supportedLanguages: i18n.supportedLngs,
11
+ fallbackLanguage: i18n.fallbackLng as string,
12
+ },
13
+ // This is the configuration for i18next used
14
+ // when translating messages server-side only
15
+ i18next: {
16
+ ...i18n,
17
+ fallbackLng: i18n.fallbackLng as string,
18
+ ns: i18n.defaultNS,
19
+ backend: {
20
+ loadPath: resolve('../locales/{{lng}}/{{ns}}.json'),
21
+ },
22
+ },
23
+ // The i18next plugins the middleware's instance uses.
24
+ // Tip: You could pass `resources` to the `i18next` configuration and avoid a backend here
25
+ plugins: [Backend, initReactI18next],
26
+ });