Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions apps/aurora/app/config/server/content-migrations.server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { afterEach, describe, expect, it } from 'vitest';
import config from '@plone/registry';
import { SOMERSAULT_KEY } from '@plone/plate/constants';
import type { Content, ContentBase } from '@plone/types';
import installMigrations from './migrations.server';
import { migrateContent } from './content-migrations.server';

type TestContent = Partial<ContentBase> & {
'@id': string;
title: string;
blocks: Record<string, unknown>;
blocks_layout: {
items: string[];
};
};

describe('content migrations', () => {
afterEach(() => {
config.settings = {};
config.blocks = {} as typeof config.blocks;
const utilities = config.utilities as Partial<Record<string, unknown>>;
delete utilities.somersaultBlockMigration;
delete utilities.somersaultMigration;
});

it('migrates title and legacy value blocks into the somersault block', () => {
config.blocks = {
blocksConfig: {},
} as typeof config.blocks;
installMigrations();

const content: TestContent = {
'@id': 'http://example.com/',
title: 'Page title',
blocks: {
text: {
'@type': 'slate',
value: [{ type: 'p', children: [{ text: 'First block' }] }],
},
titleBlock: {
'@type': 'title',
},
},
blocks_layout: {
items: ['text', 'titleBlock'],
},
};

const migrated = migrateContent(content as unknown as Content);

expect(migrated.blocks?.[SOMERSAULT_KEY]).toEqual({
'@type': SOMERSAULT_KEY,
value: [
{
blockWidth: 'default',
type: 'p',
children: [{ text: 'First block' }],
},
{
blockWidth: 'default',
type: 'title',
children: [{ text: 'Page title' }],
},
],
});
});

it('moves native blocks into the somersault field as unknown nodes', () => {
config.blocks = {
blocksConfig: {
listing: {},
image: {},
},
} as typeof config.blocks;
installMigrations();

const content: TestContent = {
'@id': 'http://example.com/',
title: 'Page title',
blocks: {
titleBlock: {
'@type': 'title',
},
listing: {
'@type': 'listing',
querystring: {
criteria: [],
},
},
image: {
'@type': 'image',
url: '/image',
alt: 'Example image',
},
custom: {
'@type': 'custom-unregistered',
foo: 'bar',
},
},
blocks_layout: {
items: ['titleBlock', 'listing', 'image', 'custom'],
},
};

const migrated = migrateContent(content as unknown as Content);

expect(migrated.blocks?.[SOMERSAULT_KEY]).toEqual({
'@type': SOMERSAULT_KEY,
value: [
{
blockWidth: 'default',
type: 'title',
children: [{ text: 'Page title' }],
},
{
'@type': 'listing',
children: [{ text: '' }],
querystring: {
criteria: [],
},
type: 'unknown',
},
{
'@type': 'image',
alt: 'Example image',
children: [{ text: '' }],
type: 'unknown',
url: '/image',
},
],
});
});
});
26 changes: 26 additions & 0 deletions apps/aurora/app/config/server/migrations.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import type {
SomersaultMigrationArgs,
} from '../types';

const isRegisteredNativeBlock = (block: Record<string, unknown>) => {
const blockType = block['@type'];
if (typeof blockType !== 'string') return false;

const blocksConfig = config.blocks?.blocksConfig as
| Record<string, unknown>
| undefined;

return Boolean(blocksConfig?.[blockType]);
};

export default function install() {
config.registerUtility({
name: 'somersaultBlockMigrationTitle',
Expand All @@ -38,6 +49,21 @@ export default function install() {
Array.isArray(block.value) ? block.value : [],
});

config.registerUtility({
name: 'somersaultBlockMigrationUnknown',
type: 'somersaultBlockMigration',
method: ({ block }: SomersaultBlockMigrationArgs) =>
isRegisteredNativeBlock(block) && !Array.isArray(block.value)
? [
{
...block,
type: 'unknown',
children: [{ text: '' }],
},
]
: [],
});

config.registerUtility({
name: 'somersaultMigrationLegacyBold',
type: 'somersaultMigration',
Expand Down
1 change: 1 addition & 0 deletions apps/aurora/news/+native-blocks-to-somersault.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Registered Aurora native blocks are now migrated into the Somersault field as `type: 'unknown'` nodes so they can be rendered by the new editor pipeline later. @sneridagh
Loading