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

feat: enabling lists in table cells [] #1696

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion packages/rich-text/src/Toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const Toolbar = ({ isDisabled }: ToolbarProps) => {
const sdk = useSdkContext();
const editor = useContentfulEditor();
const canInsertBlocks = !isNodeTypeSelected(editor, BLOCKS.TABLE);
const canInsertListBlocks = !isNodeTypeSelected(editor, BLOCKS.TABLE_HEADER_CELL);
const validationInfo = React.useMemo(() => getValidationInfo(sdk.field), [sdk.field]);
const isListSelected =
isNodeTypeSelected(editor, BLOCKS.UL_LIST) || isNodeTypeSelected(editor, BLOCKS.OL_LIST);
Expand Down Expand Up @@ -183,7 +184,7 @@ const Toolbar = ({ isDisabled }: ToolbarProps) => {

{validationInfo.isAnyBlockFormattingEnabled && <span className={styles.divider} />}

<ToolbarListButton isDisabled={isDisabled || !canInsertBlocks} />
<ToolbarListButton isDisabled={isDisabled || !canInsertListBlocks} />

{isNodeTypeEnabled(sdk.field, BLOCKS.QUOTE) && (
<ToolbarQuoteButton isDisabled={isDisabled || !canInsertBlocks} />
Expand Down
24 changes: 12 additions & 12 deletions packages/rich-text/src/internal/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export const isNode = (value: unknown): value is Node => {

export const isSelectionAtBlockEnd = (
editor: PlateEditor,
options?: p.GetAboveNodeOptions<Value>,
options?: p.GetAboveNodeOptions<Value>
) => {
return p.isSelectionAtBlockEnd(editor, options);
};

export const isSelectionAtBlockStart = (
editor: PlateEditor,
options?: p.GetAboveNodeOptions<Value>,
options?: p.GetAboveNodeOptions<Value>
) => {
return p.isSelectionAtBlockStart(editor, options);
};
Expand All @@ -72,15 +72,15 @@ export const getNodeEntries = (editor: PlateEditor, options?: p.GetNodeEntriesOp
export const getNodeChildren = (
root: Ancestor,
path: Path,
options?: s.NodeChildrenOptions | undefined,
options?: s.NodeChildrenOptions | undefined
) => {
return p.getNodeChildren(root, path, options);
};

export const getParentNode = (
editor: PlateEditor,
at: Location,
options?: s.EditorParentOptions,
options?: s.EditorParentOptions
) => {
return p.getParentNode(editor, at, options) as NodeEntry | undefined;
};
Expand Down Expand Up @@ -109,7 +109,7 @@ export const getDescendantNodeByPath = (root: Node, path: s.Path): Node => {

export const getNodeDescendants = (
root: PlateEditor | Node,
options?: s.NodeDescendantsOptions,
options?: s.NodeDescendantsOptions
) => {
return p.getNodeDescendants(root, { ...options, pass: undefined });
};
Expand All @@ -123,7 +123,7 @@ export const isRangeAcrossBlocks = (
editor: p.TEditor<p.Value>,
options?:
| (Omit<p.GetAboveNodeOptions<p.Value>, 'at'> & { at?: s.BaseRange | null | undefined })
| undefined,
| undefined
) => {
return p.isRangeAcrossBlocks(editor, options);
};
Expand Down Expand Up @@ -159,7 +159,7 @@ export const getNextNode = (editor: PlateEditor, opts?: p.GetNextNodeOptions<Val
export const getCommonNode = (
root: PlateEditor | Node,
path: s.Path,
another: s.Path,
another: s.Path
): NodeEntry => {
return p.getCommonNode(root, path, another);
};
Expand All @@ -171,7 +171,7 @@ export const getNodeTexts = (
to?: s.Path;
pass?: (ne: NodeEntry) => boolean;
reverse?: boolean;
},
}
) => {
return p.getNodeTexts(root, opts);
};
Expand Down Expand Up @@ -258,31 +258,31 @@ export const matchNode = (node: Node, path: s.Path, fn: p.Predicate<PlateEditor

export const someHtmlElement = (
rootNode: globalThis.Node,
predicate: (node: HTMLElement) => boolean,
predicate: (node: HTMLElement) => boolean
) => {
return p.someHtmlElement(rootNode, predicate);
};

export const getPointBefore = (
editor: PlateEditor,
at: Location,
options?: s.EditorBeforeOptions,
options?: s.EditorBeforeOptions
) => {
return p.getPointBefore(editor, at, options);
};

export const getPointAfter = (
editor: PlateEditor,
at: Location,
options?: s.EditorAfterOptions,
options?: s.EditorAfterOptions
) => {
return p.getPointAfter(editor, at, options);
};

export const isEndPoint = (
editor: PlateEditor,
point: BasePoint | null | undefined,
at: Location,
at: Location
) => {
return p.isEndPoint(editor, point, at);
};
Expand Down
24 changes: 16 additions & 8 deletions packages/rich-text/src/internal/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
*/
export const normalize = (
editor: PlateEditor,
options: s.EditorNormalizeOptions = { force: true },
options: s.EditorNormalizeOptions = { force: true }
) => {
return p.normalizeEditor(editor, options);
};
Expand Down Expand Up @@ -60,23 +60,23 @@ export const collapseSelection = (editor: PlateEditor, options?: SelectionCollap
export const setNodes = (
editor: PlateEditor,
attrs: Partial<Except<Node, 'children' | 'text'>>,
opts?: p.SetNodesOptions<Value>,
opts?: p.SetNodesOptions<Value>
) => {
p.setNodes(editor, attrs, opts);
};

export const unsetNodes = (
editor: PlateEditor,
props: string | number | (string | number)[],
options?: p.UnsetNodesOptions<Value> | undefined,
options?: p.UnsetNodesOptions<Value> | undefined
) => {
p.unsetNodes(editor, props, options);
};

export const insertNodes = (
editor: PlateEditor,
nodes: Node | Node[],
opts?: p.InsertNodesOptions,
opts?: p.InsertNodesOptions
) => {
return p.insertNodes(editor, nodes, opts);
};
Expand All @@ -96,15 +96,15 @@ export const unwrapNodes = (editor: PlateEditor, options?: p.UnwrapNodesOptions<
export const wrapNodes = (
editor: PlateEditor,
element: Element,
options?: p.WrapNodesOptions<Value>,
options?: p.WrapNodesOptions<Value>
) => {
return p.wrapNodes(editor, element, options);
};

export const toggleNodeType = (
editor: PlateEditor,
options: ToggleNodeTypeOptions,
editorOptions?: Omit<EditorNodesOptions, 'match'>,
editorOptions?: Omit<EditorNodesOptions, 'match'>
) => {
p.toggleNodeType(editor, options, editorOptions);
};
Expand All @@ -116,7 +116,7 @@ export const removeMark = (editor: PlateEditor, type: string, at: BaseRange) =>
export const unhangRange = (
editor: PlateEditor,
range?: Path | BasePoint | BaseRange | Span | null | undefined,
options?: p.UnhangRangeOptions | undefined,
options?: p.UnhangRangeOptions | undefined
) => {
return p.unhangRange(editor, range, options);
};
Expand Down Expand Up @@ -147,7 +147,7 @@ export const moveNodes = (editor: PlateEditor, opts?: p.MoveNodesOptions<Value>)

export const deleteFragment = (
editor: PlateEditor,
options?: s.EditorFragmentDeletionOptions | undefined,
options?: s.EditorFragmentDeletionOptions | undefined
) => {
return p.deleteFragment(editor, options);
};
Expand Down Expand Up @@ -178,3 +178,11 @@ export const setEditorValue = (editor: PlateEditor, nodes?: Node[]): void => {
}
});
};

export const setElements = (
editor: PlateEditor,
props: Partial<p.TNodeProps<p.TElement>>,
options?: p.SetNodesOptions<Value>
) => {
p.setElements(editor, props, options);
};
103 changes: 82 additions & 21 deletions packages/rich-text/src/plugins/List/transforms/unwrapList.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,97 @@
/**
* Credit: Modified version of Plate's list plugin
* See: https://github.com/udecode/plate/blob/main/packages/nodes/list
* See: https://github.com/udecode/plate/blob/main/packages/list/src/transforms/unwrapList.ts
*/

import { BLOCKS } from '@contentful/rich-text-types';
import { getListTypes } from '@udecode/plate-list';

import { withoutNormalizing } from '../../../internal';
import { getNodeEntries, isElement } from '../../../internal/queries';
import { unwrapNodes, liftNodes } from '../../../internal/transforms';
import { PlateEditor, Path } from '../../../internal/types';
import {
getAboveNode,
getBlockAbove,
getCommonNode,
getPluginType,
isElement,
} from '../../../internal/queries';
import { withoutNormalizing, unwrapNodes, setElements } from '../../../internal/transforms';
import { PlateEditor, Path, Descendant } from '../../../internal/types';

function hasUnliftedListItems(editor: PlateEditor, at?: Path) {
return getNodeEntries(editor, {
at,
match: (node, path) => isElement(node) && node.type === BLOCKS.LIST_ITEM && path.length >= 2,
}).next().done;
function handleEmbeddedType(
editor: PlateEditor,
blockType: BLOCKS.EMBEDDED_ASSET | BLOCKS.EMBEDDED_ENTRY,
at?: Path,
firstChild?: Descendant
) {
if (firstChild) {
setElements(editor, {
at,
type: getPluginType(editor, blockType),
value: { ...firstChild },
});
}
}

export const unwrapList = (editor: PlateEditor, { at }: { at?: Path } = {}) => {
const ancestorListTypeCheck = () => {
if (getAboveNode(editor, { match: { at, type: getListTypes(editor) } })) {
return true;
}
// The selection's common node might be a list type
if (!at && editor.selection) {
const commonNode = getCommonNode(
editor,
editor.selection.anchor.path,
editor.selection.focus.path
);

if (isElement(commonNode[0]) && getListTypes(editor).includes(commonNode[0].type)) {
return true;
}
}

return false;
};

withoutNormalizing(editor, () => {
do {
// lift list items to the root level
liftNodes(editor, {
const licEntry = getBlockAbove(editor, {
at,
match: (node) => isElement(node) && node.type === BLOCKS.LIST_ITEM,
mode: 'lowest',
match: { type: getPluginType(editor, BLOCKS.LIST_ITEM) },
});
} while (!hasUnliftedListItems(editor, at));

// finally unwrap all lifted items
unwrapNodes(editor, {
at,
match: { type: BLOCKS.LIST_ITEM },
split: false,
});
if (licEntry) {
// Special case for embedded entry and asset
Copy link
Contributor Author

@aodhagan-cf aodhagan-cf Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Special handling for embedded assets and entries, aside from this everything else is pretty much the same as the original

// if we don't do these they will get replaced by a paragraph and lose
// their value.
const firstChild = licEntry[0]?.children[0];
if (
firstChild.type === BLOCKS.EMBEDDED_ENTRY ||
firstChild.type === BLOCKS.EMBEDDED_ASSET
) {
handleEmbeddedType(editor, firstChild.type, at, firstChild);
} else {
setElements(editor, {
at,
type: getPluginType(editor, BLOCKS.PARAGRAPH),
});
}
}

unwrapNodes(editor, {
at,
match: { type: getPluginType(editor, BLOCKS.LIST_ITEM) },
// in original code split is set to true
// ommited here as we get an extra list item when switching off list blocks
});

unwrapNodes(editor, {
at,
match: {
type: [getPluginType(editor, BLOCKS.UL_LIST), getPluginType(editor, BLOCKS.OL_LIST)],
},
// in original code split is set to true
// ommited here as we get an extra list item when switching off list blocks
});
} while (ancestorListTypeCheck());
});
};
7 changes: 6 additions & 1 deletion packages/rich-text/src/plugins/Table/createTablePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ export const createTablePlugin = (): PlatePlugin =>
component: Cell,
normalizer: [
{
validChildren: CONTAINERS[BLOCKS.TABLE_CELL],
validChildren: [
...CONTAINERS[BLOCKS.TABLE_CELL],
...CONTAINERS[BLOCKS.UL_LIST],
...CONTAINERS[BLOCKS.OL_LIST],
...CONTAINERS[BLOCKS.LIST_ITEM],
],
transform: withInvalidCellChildrenTracking(transformParagraphs),
},
],
Expand Down
Loading