import * as React from "react";
import { DndProvider } from "react-dnd";
import Backend from "react-dnd-html5-backend";
import { LoadComponent, ItemLegend } from "./components";
import {
EditorMode,
EditorDragMode,
EditorProps,
// EditorActions,
} from "./types";
// import { EditorObjectState } from "./components/EditorObject/types";
import { contentReducer } from "./reducers";
/**
* Recursively loop through content prop to display editor content
*
* Load the component & pass along props (along with the edit=true prop - that way component can show editor version)
*
* @param content The initial content array
* @param parent The initial parent to start the recurrsive loop with
*/
const simpleGeneratePage = (
content,
parent = false,
mode = EditorMode.EDITOR
) =>
content.map((c, k) => (
{c.children}
));
const generatePage = (content, parent = false, mode = EditorMode.EDITOR) => {
return content
.filter((c) => c.parent === parent)
.map((c, key) => (
{c.children || generatePage(content, c.id, mode)}
));
};
export const EditorContext = React.createContext({
dispatch: ({ type, payload }) => null,
content: [],
});
/**
* A flexible module for quickly building powerful, drag-and-drop editors like what you see in PowerPoint, Canva, or 10 Minute Pages
*/
export const Editor: React.FC = ({
name = "Editor",
mode = EditorMode.EDITOR,
dragMode = EditorDragMode.OBJECTS,
snapGuidesAreEnabled = false,
itemLegendIsVisible = false,
// undoStack = [],
// undoPosition = 0,
readOnly = false,
zoom = 1,
initialContent = [],
itemLegendSettings = {},
children,
}) => {
const [theContent, dispatch] = React.useReducer(contentReducer, {
past: [],
present: initialContent,
future: [],
});
let content = theContent.present;
const BuilderComponent = () => (
{generatePage(content)}
);
const ItemLegendComponent = () => {
return itemLegendIsVisible ? (
) : (
);
};
return (
{children ? (
children({
ItemLegend: ItemLegendComponent,
Builder: BuilderComponent,
dispatch,
})
) : (
<>
>
)}
{/* {children} */}
);
};
export default Editor;