Skip to content

Commit

Permalink
Refactor and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock committed Dec 10, 2024
1 parent 834b841 commit 7803323
Show file tree
Hide file tree
Showing 45 changed files with 180 additions and 679 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Alert.tsx → src/Common/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {ReactNode} from 'react';
import { ErrorComponent } from './ErrorComponent';
import { Warning } from './Warning';

export const Alert = ({
export const Message = ({
children,
mode,
...extraProps
Expand Down
6 changes: 3 additions & 3 deletions src/TryCatch.tsx → src/Common/TryCatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
import type {ReactNode} from 'react';

import * as React from 'react';
import { Alert } from './Alert';
import { Message } from './Message';

export const TryCatch = ({
children,
Expand All @@ -19,15 +19,15 @@ export const TryCatch = ({
} catch (e) {
if (mode === 'edit' || mode === 'inline') {
return (
<Alert mode={mode}>
<Message mode={mode}>
<h2>Error rendering component</h2>
<p>{e.message}</p>
{
mode === 'edit' && (
<pre>{e.stack || ''}</pre>
)
}
</Alert>
</Message>
);
}
console.warn(`TryCatch component didn't get mode prop! children:${children}`);
Expand Down
6 changes: 3 additions & 3 deletions src/Warning.tsx → src/Common/Warning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ContentStudioEditModePlaceholderStyle } from './ErrorComponent';
const lightYellow = '#c9ba9b';
const brown = '#794b02';

const STYLE = {
export const WARNING_STYLE = {
...ContentStudioEditModePlaceholderStyle,
borderColor: lightYellow,
color: brown,
Expand All @@ -22,10 +22,10 @@ export function Warning({
if (html) {
return (
// Using dangerouslySetInnerHTML avoids encoding < to &lt;
<div {...extraProps} dangerouslySetInnerHTML={{ __html: html }} style={STYLE}/>
<div {...extraProps} dangerouslySetInnerHTML={{ __html: html }} style={WARNING_STYLE}/>
);
}
return (
<div {...extraProps} style={STYLE}>{children}</div>
<div {...extraProps} style={WARNING_STYLE}>{children}</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import type {Component} from '@enonic-types/core';
import type {ComponentRegistry} from '../ComponentRegistry';
import type {ComponentRegistry} from './ComponentRegistry';
import type {RenderableComponent} from '../types';

import { toStr } from '@enonic/js-utils/value/toStr';
import * as React from 'react';

import {RENDERABLE_COMPONENT_TYPE} from '../constants';
import {ErrorComponent} from '../ErrorComponent';
import {Warning} from '../Warning';
import {XpBaseLayout} from './XpBaseLayout';
import {XpBasePage} from './XpBasePage';
import {XpBasePart} from './XpBasePart';
import {XpBaseText} from './XpBaseText';
import {XpContentType} from './XpContentType';
import {ErrorComponent} from '../Common/ErrorComponent';
import {Warning} from '../Common/Warning';
import {BaseLayout} from './BaseLayout';
import {BasePage} from './BasePage';
import {BasePart} from './BasePart';
import {BaseText} from './BaseText';
import {BaseContentType} from './BaseContentType';
import {XpFallback} from './XpFallback';


export function XpComponent({
export function BaseComponent({
component,
componentRegistry
}: {
component: RenderableComponent
componentRegistry?: ComponentRegistry
}) {
// console.debug('XpComponent component:', toStr(component));
// console.debug('BaseComponent component:', toStr(component));

if (!componentRegistry) {
console.warn('XpComponent componentRegistry missing! with component:', toStr(component));
console.warn('BaseComponent componentRegistry missing! with component:', toStr(component));
return (
<XpFallback component={component as Component}/>
);
Expand All @@ -37,45 +37,45 @@ export function XpComponent({
type
} = component;
if (!type) {
console.error('XpComponent component missing type:', toStr(component));
console.error('BaseComponent component missing type:', toStr(component));
return (
<XpFallback component={component}/>
);
}
// console.info('XpComponent type:', type);
// console.info('BaseComponent type:', type);

switch (type) {
case RENDERABLE_COMPONENT_TYPE.PART:
return (
<XpBasePart
<BasePart
component={component}
componentRegistry={componentRegistry}
/>
);
case RENDERABLE_COMPONENT_TYPE.LAYOUT:
return (
<XpBaseLayout
<BaseLayout
component={component}
componentRegistry={componentRegistry}
/>
);
case RENDERABLE_COMPONENT_TYPE.PAGE:
return (
<XpBasePage
<BasePage
component={component}
componentRegistry={componentRegistry}
/>
);
case RENDERABLE_COMPONENT_TYPE.CONTENT_TYPE:
return (
<XpContentType
<BaseContentType
component={component}
componentRegistry={componentRegistry}
/>
);
case RENDERABLE_COMPONENT_TYPE.TEXT: {
return (
<XpBaseText
<BaseText
component={component}
componentRegistry={componentRegistry}
/>
Expand Down Expand Up @@ -112,4 +112,4 @@ export function XpComponent({
<XpFallback component={component as Component}/>
);

} // XpComponent
} // BaseComponent
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type {
} from '../types';

import * as React from 'react';
import { Alert } from '../Alert';
import { Message } from '../Common/Message';

export const XpContentType = ({
export const BaseContentType = ({
component,
componentRegistry
}: {
Expand All @@ -22,7 +22,7 @@ export const XpContentType = ({

// if (warning && (mode === 'edit' || mode === 'inline')) {
// return (
// <Alert mode={mode}>{warning}</Alert>
// <Message mode={mode}>{warning}</Message>
// );
// }

Expand All @@ -31,20 +31,20 @@ export const XpContentType = ({
}>(contentType);
if (!contentTypeDefinition) {
return (
<Alert mode={mode}>{`ContentType:${contentType} not registered in ComponentRegistry!`}</Alert>
<Message mode={mode}>{`ContentType:${contentType} not registered in ComponentRegistry!`}</Message>
);
}

const {View: ContentTypeView} = contentTypeDefinition;
if (!ContentTypeView) {
return (
<Alert mode={mode}>{`No View found for contentType:${contentType} in ComponentRegistry!`}</Alert>
<Message mode={mode}>{`No View found for contentType:${contentType} in ComponentRegistry!`}</Message>
);
}

if (!props) {
return (
<Alert mode={mode}>{`ContentType component missing props: ${contentType}!`}</Alert>
<Message mode={mode}>{`ContentType component missing props: ${contentType}!`}</Message>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {
} from '../types';

import * as React from 'react';
import { Alert } from '../Alert';
import { Message } from '../Common/Message';

export function XpBaseLayout({
export function BaseLayout({
component,
componentRegistry
}: {
Expand All @@ -23,27 +23,27 @@ export function XpBaseLayout({

if (warning && (mode === 'edit' || mode === 'inline')) {
return (
<Alert mode={mode}>{warning}</Alert>
<Message mode={mode}>{warning}</Message>
);
}

const layoutDefinition = componentRegistry.getLayout(descriptor);
if (!layoutDefinition) {
return (
<Alert mode={mode}>{`Layout descriptor:${descriptor} not registered in ComponentRegistry!`}</Alert>
<Message mode={mode}>{`Layout descriptor:${descriptor} not registered in ComponentRegistry!`}</Message>
);
}

const {View: LayoutView} = layoutDefinition;
if (!LayoutView) {
return (
<Alert mode={mode}>{`No View found for layout descriptor:${descriptor} in ComponentRegistry!`}</Alert>
<Message mode={mode}>{`No View found for layout descriptor:${descriptor} in ComponentRegistry!`}</Message>
);
}

if (!props) {
return (
<Alert mode={mode}>{`Layout component missing props: ${descriptor}!`}</Alert>
<Message mode={mode}>{`Layout component missing props: ${descriptor}!`}</Message>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type {
} from '../types';

import * as React from 'react';
import { Alert } from '../Alert';
import { ErrorComponent } from '../ErrorComponent';
import { Message } from '../Common/Message';
import { ErrorComponent } from '../Common/ErrorComponent';

export function XpBasePage({
export function BasePage({
component,
componentRegistry
}: {
Expand All @@ -31,27 +31,27 @@ export function XpBasePage({

if (warning && (mode === 'inline' || mode === 'edit')) {
return (
<Alert mode={mode} children={warning}/>
<Message mode={mode} children={warning}/>
);
}

const pageDefinition = componentRegistry.getPage(descriptor);
if (!pageDefinition) {
return (
<Alert mode={mode}>{`Page descriptor:${descriptor} not registered in ComponentRegistry!`}</Alert>
<Message mode={mode}>{`Page descriptor:${descriptor} not registered in ComponentRegistry!`}</Message>
);
}

const {View: PageView} = pageDefinition;
if (!PageView) {
return (
<Alert mode={mode}>{`No View found for page descriptor:${descriptor} in ComponentRegistry!`}</Alert>
<Message mode={mode}>{`No View found for page descriptor:${descriptor} in ComponentRegistry!`}</Message>
);
}

if (!props) {
return (
<Alert mode={mode}>{`Page component missing props: ${descriptor}!`}</Alert>
<Message mode={mode}>{`Page component missing props: ${descriptor}!`}</Message>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import type {

// import { toStr } from '@enonic/js-utils/value/toStr';
import * as React from 'react';
import { Alert } from '../Alert';
import { Message } from '../Common/Message';

export function XpBasePart({
export function BasePart({
component,
componentRegistry
}: {
Expand All @@ -25,7 +25,7 @@ export function XpBasePart({

if (warning && (mode === 'edit' || mode === 'inline')) {
return (
<Alert {...{
<Message {...{
children: warning,
'data-portal-component-type': mode === 'edit' ? 'part' : undefined,
mode,
Expand All @@ -39,7 +39,7 @@ export function XpBasePart({

if (!partDefinition) {
return (
<Alert {...{
<Message {...{
children: `Part descriptor:${descriptor} not registered in ComponentRegistry!`,
'data-portal-component-type': mode === 'edit' ? 'part' : undefined,
mode,
Expand All @@ -50,7 +50,7 @@ export function XpBasePart({
const {View: PartView} = partDefinition;
if (!PartView) {
return (
<Alert {...{
<Message {...{
children: `No View found for part descriptor:${descriptor} in ComponentRegistry!`,
'data-portal-component-type': mode === 'edit' ? 'part' : undefined,
mode,
Expand All @@ -60,7 +60,7 @@ export function XpBasePart({

if (!props) {
return (
<Alert {...{
<Message {...{
children: `Part component missing props: ${descriptor}!`,
'data-portal-component-type': mode === 'edit' ? 'part' : undefined,
mode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type {
ComponentRegistry,
RenderableTextComponent,
XpTextProps,
TextProps,
} from '../types';

import { toStr } from '@enonic/js-utils/value/toStr';
import * as React from 'react';
import { Alert } from '../Alert';
import { Message } from '../Common/Message';
import { XpFallback } from './XpFallback';
import { XpText } from './XpText';
import { Text } from './Text';

export const XpBaseText = ({
export const BaseText = ({
component,
componentRegistry
}: {
Expand All @@ -25,17 +25,17 @@ export const XpBaseText = ({
if (!props) {
if (mode === 'edit' || mode === 'inline') {
return (
<Alert mode={mode}>Text component missing props: {toStr(component)}</Alert>
<Message mode={mode}>Text component missing props: {toStr(component)}</Message>
);
}
console.warn('XpBaseText: Text component missing props:', toStr(component));
console.warn('BaseText: Text component missing props:', toStr(component));
return <XpFallback component={component}/>
}

const textProps = props as XpTextProps;
const textProps = props as TextProps;

return (
<XpText {...{
<Text {...{
...textProps,
componentRegistry,
'data-portal-component-type': mode === 'edit' ? 'text' : undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
ComponentDefinitionParams,
ComponentDictionary,
ComponentRegistry as ComponentRegistryInterface
} from './types';
} from '../types';

// import {XP_COMPONENT_TYPE} from './constants';

Expand Down
Loading

0 comments on commit 7803323

Please sign in to comment.