mirror of
https://github.com/foomo/contentfultree.git
synced 2025-10-16 12:25:41 +00:00
Merge pull request #3 from foomo/feat/add-indentation-prop
feat: indentation prop, syntax changes
This commit is contained in:
commit
3905400ad7
@ -12,32 +12,42 @@ export interface ContentTreeProps {
|
|||||||
nodeContentTypes: string[];
|
nodeContentTypes: string[];
|
||||||
titleFields: string[];
|
titleFields: string[];
|
||||||
locales: string[]; // the first is the default locale
|
locales: string[]; // the first is the default locale
|
||||||
|
indentation?: number;
|
||||||
iconRegistry?: { [index: string]: string };
|
iconRegistry?: { [index: string]: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ContentTree = (props: ContentTreeProps): ReactElement => {
|
export const ContentTree = ({
|
||||||
const [stLocale] = useState(props.locales[0]);
|
sdkInstance,
|
||||||
|
cma,
|
||||||
|
rootType,
|
||||||
|
nodeContentTypes,
|
||||||
|
titleFields,
|
||||||
|
locales,
|
||||||
|
indentation = 1,
|
||||||
|
iconRegistry,
|
||||||
|
}: ContentTreeProps): ReactElement => {
|
||||||
|
const [stLocale] = useState(locales[0]);
|
||||||
const [rootNodes, setRootNodes] = useImmer([emptyNodeProps()]);
|
const [rootNodes, setRootNodes] = useImmer([emptyNodeProps()]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (props.sdkInstance) {
|
if (sdkInstance) {
|
||||||
loadData().catch((err) => {
|
loadData().catch((err) => {
|
||||||
throw new Error('loadRootData', err);
|
throw new Error('loadRootData', err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [props.sdkInstance]);
|
}, [sdkInstance]);
|
||||||
|
|
||||||
const loadData = async (): Promise<void> => {
|
const loadData = async (): Promise<void> => {
|
||||||
const CfRootData = await props.cma.entry.getMany({
|
const CfRootData = await cma.entry.getMany({
|
||||||
query: { content_type: props.rootType },
|
query: { content_type: rootType },
|
||||||
});
|
});
|
||||||
const nodes = cfEntriesToNodes(
|
const nodes = cfEntriesToNodes(
|
||||||
CfRootData.items,
|
CfRootData.items,
|
||||||
props.titleFields,
|
titleFields,
|
||||||
stLocale,
|
stLocale,
|
||||||
props.locales,
|
locales,
|
||||||
props.nodeContentTypes,
|
nodeContentTypes,
|
||||||
props.iconRegistry
|
iconRegistry
|
||||||
);
|
);
|
||||||
setRootNodes(nodes);
|
setRootNodes(nodes);
|
||||||
};
|
};
|
||||||
@ -48,12 +58,13 @@ export const ContentTree = (props: ContentTreeProps): ReactElement => {
|
|||||||
<ContentTreeRoot
|
<ContentTreeRoot
|
||||||
key={i.toString()}
|
key={i.toString()}
|
||||||
node={node}
|
node={node}
|
||||||
locales={props.locales}
|
locales={locales}
|
||||||
nodeContentTypes={props.nodeContentTypes}
|
nodeContentTypes={nodeContentTypes}
|
||||||
iconRegistry={props.iconRegistry}
|
iconRegistry={iconRegistry}
|
||||||
cma={props.cma}
|
cma={cma}
|
||||||
titleFields={props.titleFields}
|
titleFields={titleFields}
|
||||||
sdkInstance={props.sdkInstance}
|
sdkInstance={sdkInstance}
|
||||||
|
depth={indentation}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</>
|
</>
|
||||||
|
|||||||
@ -19,20 +19,30 @@ export interface ContentTreeRootProps {
|
|||||||
nodeContentTypes: string[];
|
nodeContentTypes: string[];
|
||||||
titleFields: string[];
|
titleFields: string[];
|
||||||
locales: string[]; // the first is the default locale
|
locales: string[]; // the first is the default locale
|
||||||
|
depth: number;
|
||||||
iconRegistry?: { [index: string]: string };
|
iconRegistry?: { [index: string]: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ContentTreeRoot = (props: ContentTreeRootProps): ReactElement => {
|
export const ContentTreeRoot = ({
|
||||||
const [stLocale] = useState(props.locales[0]);
|
node,
|
||||||
|
sdkInstance,
|
||||||
|
cma,
|
||||||
|
nodeContentTypes,
|
||||||
|
titleFields,
|
||||||
|
locales,
|
||||||
|
depth,
|
||||||
|
iconRegistry,
|
||||||
|
}: ContentTreeRootProps): ReactElement => {
|
||||||
|
const [stLocale] = useState(locales[0]);
|
||||||
const [stRoot, setStRoot] = useImmer(emptyNodeProps());
|
const [stRoot, setStRoot] = useImmer(emptyNodeProps());
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (props.node.id) {
|
if (node.id) {
|
||||||
loadRootData(props.node).catch((err) => {
|
loadRootData(node).catch((err) => {
|
||||||
throw new Error('loadRootData', err);
|
throw new Error('loadRootData', err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [props.node]);
|
}, [node]);
|
||||||
|
|
||||||
const loadRootData = async (
|
const loadRootData = async (
|
||||||
rootNode: ContentTreeNodeProps
|
rootNode: ContentTreeNodeProps
|
||||||
@ -40,11 +50,11 @@ export const ContentTreeRoot = (props: ContentTreeRootProps): ReactElement => {
|
|||||||
const childEntries = await getContentfulChildEntries(rootNode.id);
|
const childEntries = await getContentfulChildEntries(rootNode.id);
|
||||||
const childNodes = cfEntriesToNodes(
|
const childNodes = cfEntriesToNodes(
|
||||||
childEntries,
|
childEntries,
|
||||||
props.titleFields,
|
titleFields,
|
||||||
stLocale,
|
stLocale,
|
||||||
props.locales,
|
locales,
|
||||||
props.nodeContentTypes,
|
nodeContentTypes,
|
||||||
props.iconRegistry,
|
iconRegistry,
|
||||||
rootNode.id
|
rootNode.id
|
||||||
);
|
);
|
||||||
const nodes = [rootNode, ...childNodes];
|
const nodes = [rootNode, ...childNodes];
|
||||||
@ -76,11 +86,11 @@ export const ContentTreeRoot = (props: ContentTreeRootProps): ReactElement => {
|
|||||||
const cfChildren = await getContentfulChildEntries(node.id);
|
const cfChildren = await getContentfulChildEntries(node.id);
|
||||||
childNodes = cfEntriesToNodes(
|
childNodes = cfEntriesToNodes(
|
||||||
cfChildren,
|
cfChildren,
|
||||||
props.titleFields,
|
titleFields,
|
||||||
stLocale,
|
stLocale,
|
||||||
props.locales,
|
locales,
|
||||||
props.nodeContentTypes,
|
nodeContentTypes,
|
||||||
props.iconRegistry,
|
iconRegistry,
|
||||||
node.id
|
node.id
|
||||||
);
|
);
|
||||||
setStRoot((draft) => {
|
setStRoot((draft) => {
|
||||||
@ -111,16 +121,16 @@ export const ContentTreeRoot = (props: ContentTreeRootProps): ReactElement => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const editEntry = async (entryId: string): Promise<void> => {
|
const editEntry = async (entryId: string): Promise<void> => {
|
||||||
await props.sdkInstance.navigator.openEntry(entryId, { slideIn: true });
|
await sdkInstance.navigator.openEntry(entryId, { slideIn: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
const getContentfulChildEntries = async (
|
const getContentfulChildEntries = async (
|
||||||
parentId: string
|
parentId: string
|
||||||
): Promise<Array<EntryProps<KeyValueMap>>> => {
|
): Promise<Array<EntryProps<KeyValueMap>>> => {
|
||||||
const parentItem = await props.cma.entry.get({ entryId: parentId });
|
const parentItem = await cma.entry.get({ entryId: parentId });
|
||||||
const allChildIds: string[] = [];
|
const allChildIds: string[] = [];
|
||||||
for (const key of Object.keys(parentItem.fields)) {
|
for (const key of Object.keys(parentItem.fields)) {
|
||||||
if (props.nodeContentTypes.includes(key)) {
|
if (nodeContentTypes.includes(key)) {
|
||||||
const childNodeRefs = parentItem.fields[key][stLocale] as
|
const childNodeRefs = parentItem.fields[key][stLocale] as
|
||||||
| Link<string>
|
| Link<string>
|
||||||
| Array<Link<string>>;
|
| Array<Link<string>>;
|
||||||
@ -137,7 +147,7 @@ export const ContentTreeRoot = (props: ContentTreeRootProps): ReactElement => {
|
|||||||
let done = false;
|
let done = false;
|
||||||
let skip = 0;
|
let skip = 0;
|
||||||
while (!done) {
|
while (!done) {
|
||||||
const col = await props.cma.entry.getMany({
|
const col = await cma.entry.getMany({
|
||||||
query: {
|
query: {
|
||||||
'sys.id[in]': allChildIds.join(','),
|
'sys.id[in]': allChildIds.join(','),
|
||||||
skip,
|
skip,
|
||||||
@ -192,7 +202,7 @@ export const ContentTreeRoot = (props: ContentTreeRootProps): ReactElement => {
|
|||||||
</tr>
|
</tr>
|
||||||
<ContentTreeNode
|
<ContentTreeNode
|
||||||
node={stRoot}
|
node={stRoot}
|
||||||
depth={1}
|
depth={depth}
|
||||||
addChildNodes={addChildNodes}
|
addChildNodes={addChildNodes}
|
||||||
removeChildNodes={removeChildNodes}
|
removeChildNodes={removeChildNodes}
|
||||||
editEntry={editEntry}
|
editEntry={editEntry}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user