Skip to content

Commit

Permalink
[lexical-playground] Feat: columns resizing in layouts plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hmetkel-ontic authored and harshmetkel24 committed Nov 5, 2024
1 parent c5f4722 commit 1f29966
Show file tree
Hide file tree
Showing 6 changed files with 647 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/lexical-playground/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {RichTextPlugin} from '@lexical/react/LexicalRichTextPlugin';
import {TabIndentationPlugin} from '@lexical/react/LexicalTabIndentationPlugin';
import {TablePlugin} from '@lexical/react/LexicalTablePlugin';
import {useLexicalEditable} from '@lexical/react/useLexicalEditable';
import * as React from 'react';
import {useEffect, useState} from 'react';
import {CAN_USE_DOM} from 'shared/canUseDOM';

Expand Down Expand Up @@ -51,6 +50,8 @@ import FloatingTextFormatToolbarPlugin from './plugins/FloatingTextFormatToolbar
import ImagesPlugin from './plugins/ImagesPlugin';
import InlineImagePlugin from './plugins/InlineImagePlugin';
import KeywordsPlugin from './plugins/KeywordsPlugin';
import LayoutColumnResizerPlugin from './plugins/LayoutColumnResizer';
import LayoutColumnHoverActions from './plugins/LayoutHoverActionsPlugin';
import {LayoutPlugin} from './plugins/LayoutPlugin/LayoutPlugin';
import LinkPlugin from './plugins/LinkPlugin';
import ListMaxIndentLevelPlugin from './plugins/ListMaxIndentLevelPlugin';
Expand Down Expand Up @@ -200,6 +201,8 @@ export default function Editor(): JSX.Element {
<CollapsiblePlugin />
<PageBreakPlugin />
<LayoutPlugin />
<LayoutColumnResizerPlugin />
<LayoutColumnHoverActions />
{floatingAnchorElem && !isSmallWidthViewport && (
<>
<DraggableBlockPlugin anchorElem={floatingAnchorElem} />
Expand Down
41 changes: 40 additions & 1 deletion packages/lexical-playground/src/nodes/LayoutContainerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import type {
Spread,
} from 'lexical';

import {addClassNamesToElement} from '@lexical/utils';
import {$findMatchingParent, addClassNamesToElement} from '@lexical/utils';
import {ElementNode} from 'lexical';
import {times} from 'lodash-es';

import {LayoutItemNode} from './LayoutItemNode';

export type SerializedLayoutContainerNode = Spread<
{
Expand Down Expand Up @@ -122,6 +125,13 @@ export class LayoutContainerNode extends ElementNode {
setTemplateColumns(templateColumns: string) {
this.getWritable().__templateColumns = templateColumns;
}

updateTemplateColumnWithIndex(index: number, value: string) {
const currentGridTemplateColumns = this.getTemplateColumns();
const newGridTemplateColumns = currentGridTemplateColumns.split(' ');
newGridTemplateColumns[index] = value;
return this.setTemplateColumns(newGridTemplateColumns.join(' '));
}
}

export function $createLayoutContainerNode(
Expand All @@ -135,3 +145,32 @@ export function $isLayoutContainerNode(
): node is LayoutContainerNode {
return node instanceof LayoutContainerNode;
}

export function $getLayoutContainerNodeIfLayoutItemNodeOrThrow(
layoutItemNode: LayoutItemNode,
): LayoutContainerNode {
const node = $findMatchingParent(layoutItemNode, $isLayoutContainerNode);

if ($isLayoutContainerNode(node)) {
return node;
}

throw new Error(
'Expected LayoutItemNode to be inside of LayoutContainerNode.',
);
}

export function $findLayoutItemIndexGivenLayoutContainerNode(
layoutItemNode: LayoutItemNode,
): number {
const layoutContainerNode =
$getLayoutContainerNodeIfLayoutItemNodeOrThrow(layoutItemNode);

return layoutContainerNode
.getChildren()
.findIndex((node) => node.is(layoutItemNode));
}

export function $getGridTemplateColumnsWithEqualWidth(count: number) {
return times(count, () => '1fr').join(' ');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PlaygroundEditorTheme__resizer {
z-index: 1302;
position: absolute;
}
Loading

0 comments on commit 1f29966

Please sign in to comment.