Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hardcoded Stitches dependency from Easyblocks. #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions packages/core/src/_internals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { CompilationCache } from "./compiler/CompilationCache";
export type { CompilationCacheItemValue } from "./compiler/CompilationCache";
export { compileBox, getBoxStyles } from "./compiler/box";
export { RichTextEditor } from "./compiler/builtins/$richText/$richText.editor";
export { TextEditor } from "./compiler/builtins/$text/$text.editor";
export { buildText } from "./compiler/builtins/$text/buildText";
Expand All @@ -16,7 +15,7 @@ export {
export { normalize } from "./compiler/normalize";
export * from "./compiler/parsePath";
export type { PathInfo } from "./compiler/parsePath";
export { scalarizeConfig } from "./compiler/resop";
export { scalarizeConfig } from "./scalarizeConfig";
export * from "./compiler/schema";
export * from "./compiler/schema/buttonSchemaProps";
export { traverseComponents } from "./compiler/traverseComponents";
Expand All @@ -28,6 +27,7 @@ export type {
InternalComponentDefinition,
InternalRenderableComponentDefinition,
} from "./compiler/types";

export {
EasyblocksMetadataProvider,
useEasyblocksMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import React, { ReactElement, ReactNode } from "react";
interface RichTextProps {
elements: Array<ReactElement>;
Root: ReactElement<{ children: ReactNode }>;
__textRootClasses: string;
}

function RichTextClient(props: RichTextProps) {
const { elements: Elements, Root } = props;

return (
<Root.type {...Root.props}>
<div className={props.__textRootClasses}>
{Elements.map((Element, index) => {
return <Element.type {...Element.props} key={index} />;
})}
</Root.type>
</div>
);
}

Expand Down
204 changes: 72 additions & 132 deletions packages/core/src/compiler/builtins/$richText/$richText.editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ import type {
RenderPlaceholderProps,
} from "slate-react";
import { Editable, ReactEditor, Slate, withReact } from "slate-react";
import { Box } from "../../../components/Box/Box";
import {
ComponentBuilder,
InternalNoCodeComponentProps,
} from "../../../components/ComponentBuilder/ComponentBuilder";
import { RichTextChangedEvent } from "../../../events";
import { getFallbackForLocale } from "../../../locales";
import { responsiveValueFill } from "../../../responsiveness";
import { Devices, ResponsiveValue } from "../../../types";
import { compileBox, getBoxStyles } from "../../box";
import { getDevicesWidths } from "../../devices";
import { ResponsiveValue } from "../../../types";
import { duplicateConfig } from "../../duplicateConfig";
import type { RichTextComponentConfig } from "./$richText";
import {
Expand All @@ -59,6 +55,8 @@ import { getFocusedFieldsFromSlateSelection } from "./utils/getFocusedFieldsFrom
import { getFocusedRichTextPartsConfigPaths } from "./utils/getFocusedRichTextPartsConfigPaths";
import { getRichTextComponentConfigFragment } from "./utils/getRichTextComponentConfigFragment";
import { NORMALIZED_IDS_TO_IDS, withEasyblocks } from "./withEasyblocks";
import { liStyles, olStyles, paragraphStyles, ulStyles } from "./styles";
import { useEasyblocksMetadata } from "../../../_internals";

interface RichTextProps extends InternalNoCodeComponentProps {
elements: Array<
Expand All @@ -81,11 +79,10 @@ function RichTextEditor(props: RichTextProps) {
setFocussedField,
} = editorContext;

const meta = useEasyblocksMetadata();

const {
__easyblocks: {
path,
runtime: { resop, stitches, devices },
},
__easyblocks: { path },
align,
} = props;

Expand Down Expand Up @@ -364,43 +361,51 @@ function RichTextEditor(props: RichTextProps) {
throw new Error("Missing element");
}

const compiledStyles = (() => {
if (Element._component === "@easyblocks/rich-text-block-element") {
if (Element.props.type === "bulleted-list") {
return Element.styled.BulletedList;
} else if (Element.props.type === "numbered-list") {
return Element.styled.NumberedList;
} else if (Element.props.type === "paragraph") {
return Element.styled.Paragraph;
}
} else if (Element._component === "@easyblocks/rich-text-line-element") {
if (element.type === "text-line") {
return Element.styled.TextLine;
} else if (element.type === "list-item") {
return Element.styled.ListItem;
}
}
})();
if (Element._component === "@easyblocks/rich-text-block-element") {
if (Element.props.type === "bulleted-list") {
return (
<ul style={ulStyles} {...attributes}>
{children}
</ul>
);

// return Element.props.__styled.BulletedList;
} else if (Element.props.type === "numbered-list") {
return (
<ol style={olStyles} {...attributes}>
{children}
</ol>
);

// return Element.props.__styled.NumberedList;
} else if (Element.props.type === "paragraph") {
return React.createElement(
// @ts-ignore
Element.props.accessibilityRole,
attributes,
children
);

if (compiledStyles === undefined) {
throw new Error("Unknown element type");
// return Element.props.__styled.Paragraph;
}
} else if (Element._component === "@easyblocks/rich-text-line-element") {
if (element.type === "text-line") {
return (
<div style={paragraphStyles} {...attributes}>
{children}
</div>
);
} else if (element.type === "list-item") {
return (
<li style={liStyles} {...attributes}>
<span>{children}</span>
</li>
);
// return Element.props.__styled.ListItem;
}
}

return (
<Box
__compiled={compiledStyles}
devices={devices}
stitches={stitches}
{...attributes}
// Element annotation for easier debugging
{...(process.env.NODE_ENV === "development" && {
"data-shopstory-element-type": element.type,
"data-shopstory-id": element.id,
})}
>
{element.type === "list-item" ? <div>{children}</div> : children}
</Box>
);
throw new Error("Unknown element type");
}

const TextParts = extractTextPartsFromCompiledComponents(props);
Expand All @@ -425,17 +430,16 @@ function RichTextEditor(props: RichTextProps) {
throw new Error("Missing part");
}

const __textPartClasses: string = meta.renderer.generateClassNames(
// @ts-ignore
TextPart.props.__textPart,
meta
);

const TextPartComponent = (
<RichTextPartClient
value={children}
Text={
<Box
__compiled={TextPart.styled.Text}
devices={devices}
stitches={stitches}
{...attributes}
/>
}
textAttributes={attributes}
TextWrapper={
TextPart.components.TextWrapper[0] ? (
<ComponentBuilder
Expand All @@ -449,6 +453,7 @@ function RichTextEditor(props: RichTextProps) {
/>
) : undefined
}
__textPartClasses={__textPartClasses}
/>
);

Expand Down Expand Up @@ -738,61 +743,30 @@ function RichTextEditor(props: RichTextProps) {
}
}

const contentEditableClassName = useMemo(() => {
const responsiveAlignmentStyles = mapResponsiveAlignmentToStyles(align, {
devices: editorContext.devices,
resop,
});
const isFallbackValueShown =
localizedRichTextElements === undefined &&
fallbackRichTextElements !== undefined;

const isFallbackValueShown =
localizedRichTextElements === undefined &&
fallbackRichTextElements !== undefined;

// When we make a selection of text within editable container and then blur
// sometimes the browser selection changes and shows incorrectly selected chunks.
const getStyles = stitches.css({
display: "flex",
...responsiveAlignmentStyles,
cursor: !isEnabled ? "inherit" : "text",
"& *": {
pointerEvents: isEnabled ? "auto" : "none",
userSelect: isEnabled ? "auto" : "none",
},
"& *::selection": {
backgroundColor: "#b4d5fe",
},
...(isDecorationActive && {
"& *::selection": {
backgroundColor: "transparent",
},
"& *[data-easyblocks-rich-text-selection]": {
backgroundColor: "#b4d5fe",
},
}),
...(isFallbackValueShown && {
opacity: 0.5,
}),
// Remove any text decoration from slate nodes that are elements. We only need text decoration on text elements.
"[data-slate-node]": {
textDecoration: "none",
},
});
const __textRootClasses: string = meta.renderer.generateClassNames(
// @ts-ignore
props.__textRoot,
meta
);

return getStyles().className;
}, [
align,
isDecorationActive,
localizedRichTextElements,
fallbackRichTextElements,
isEnabled,
]);
const classes = `EasyblocksRichTextEditor_Root ${
isEnabled ? "EasyblocksRichTextEditor_Root--enabled" : ""
} ${
isFallbackValueShown ? "EasyblocksRichTextEditor_Root--fallbackValue" : ""
} ${
isDecorationActive ? "EasyblocksRichTextEditor_Root--decorationActive" : ""
}`;

return (
<Slate editor={editor} value={editorValue} onChange={handleEditableChange}>
<div>
{/* this wrapper div prevents from Chrome bug where "pointer-events: none" on contenteditable is ignored*/}
<Editable
className={contentEditableClassName}
className={`${classes} ${__textRootClasses}`}
placeholder="Here goes text content"
renderElement={renderElement}
renderLeaf={renderLeaf}
Expand Down Expand Up @@ -866,40 +840,6 @@ function isConfigEqual(newConfig: any, oldConfig: any) {
return deepCompare(newConfig, oldConfig);
}

function mapResponsiveAlignmentToStyles(
align: ResponsiveValue<Alignment>,
{ devices, resop }: { devices: Devices; resop: any }
) {
function mapAlignmentToFlexAlignment(align: Alignment) {
if (align === "center") {
return "center";
}

if (align === "right") {
return "flex-end";
}

return "flex-start";
}

const responsiveStyles = resop(
{
align: responsiveValueFill(align, devices, getDevicesWidths(devices)),
},
(values: any) => {
return {
justifyContent: mapAlignmentToFlexAlignment(values.align),
textAlign: values.align,
};
},
devices
);

const compiledStyles = compileBox(responsiveStyles, devices);

return getBoxStyles(compiledStyles, devices);
}

function createTextSelectionDecorator(editor: Editor) {
return ([node, path]: NodeEntry) => {
const decorations: Array<Range> = [];
Expand Down
36 changes: 19 additions & 17 deletions packages/core/src/compiler/builtins/$richText/$richText.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
NoCodeComponentStylesFunctionInput,
NoCodeComponentStylesFunctionResult,
} from "../../../types";
import { Alignment } from "./$richText.types";
// import { Alignment } from "./$richText.types";

export function richTextStyles({
values,
Expand All @@ -11,13 +11,15 @@ export function richTextStyles({
const align = params.passedAlign ?? values.align;

return {
styled: {
Root: {
display: "flex",
justifyContent: mapAlignmentToFlexAlignment(align),
textAlign: align,
},
},
// styled: {
// Root: {
// // display: "flex",
// // justifyContent: mapAlignmentToFlexAlignment(align),
// // textAlign: align,
// // color: values.mainColor,
// // ...values.mainFont,
// },
// },
components: {
elements: {
// We store values within $richText to allow for changing them from sidebar, but we use them inside of $richTextBlockElement.
Expand All @@ -36,14 +38,14 @@ export function richTextStyles({
};
}

export function mapAlignmentToFlexAlignment(align: Alignment) {
if (align === "center") {
return "center";
}
// export function mapAlignmentToFlexAlignment(align: Alignment) {
// if (align === "center") {
// return "center";
// }

if (align === "right") {
return "flex-end";
}
// if (align === "right") {
// return "flex-end";
// }

return "flex-start";
}
// return "flex-start";
// }
Loading