Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
feat: support simple table (#6911)
Kilu.Hef9a5458b945f
diff --git a/frontend/appflowy_web_app/src/application/types.ts b/frontend/appflowy_web_app/src/application/types.tsindex e54483894e..9ccd1da92b 100644--- a/frontend/appflowy_web_app/src/application/types.ts+++ b/frontend/appflowy_web_app/src/application/types.ts@@ -36,6 +36,9 @@ export enum BlockType {FileBlock = 'file',GalleryBlock = 'multi_image',SubpageBlock = 'sub_page',+ SimpleTableBlock = 'simple_table',+ SimpleTableRowBlock = 'simple_table_row',+ SimpleTableCellBlock = 'simple_table_cell',}export enum InlineBlockType {@@ -140,6 +143,22 @@ export interface TableBlockData extends BlockData {rowsLen: number;}+export enum TableAlignType {+ Left = 'Left',+ Center = 'Center',+ Right = 'Right',+}++export interface SimpleTableData extends BlockData {+ column_widths: Record<string, number>;+ enable_header_row: boolean;+ row_colors: Record<string, string>;+ enable_header_column: boolean;+ column_aligns: Record<string, TableAlignType>;+ column_colors: Record<string, string>;+ row_aligns: Record<string, TableAlignType>;+}+export interface TableCellBlockData extends BlockData {colPosition: number;height: number;diff --git a/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTable.tsx b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTable.tsxnew file mode 100644index 0000000000..da62087f27--- /dev/null+++ b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTable.tsx@@ -0,0 +1,91 @@+import { EditorElementProps, SimpleTableNode, SimpleTableRowNode } from '@/components/editor/editor.type';+import { useEditorContext } from '@/components/editor/EditorContext';+import TableContainer from '@/components/editor/components/table-container/TableContainer';+import isEqual from 'lodash-es/isEqual';+import React, { forwardRef, memo, useMemo } from 'react';+import './simple-table.scss';++const MIN_WIDTH = 120;++const SimpleTable = memo(+ forwardRef<HTMLDivElement, EditorElementProps<SimpleTableNode>>(({+ node,+ children,+ className: classNameProp,+ ...attributes+ }, ref) => {+ const context = useEditorContext();+ const readSummary = context.readSummary;+ const { data, blockId, children: rows } = node;+ const { column_widths, column_colors, enable_header_column, enable_header_row } = data;++ const columnCount = useMemo(() => {+ const firstRow = rows[0] as SimpleTableRowNode;++ if (!firstRow) return 0;++ return firstRow.children.length;+ }, [rows]);++ const columns = useMemo(() => {+ return Array.from({ length: columnCount }, (_, index) => {+ const width = column_widths[index] || MIN_WIDTH;+ const bgColor = column_colors[index] || 'transparent';++ return { width, bgColor };+ });+ }, [columnCount, column_colors, column_widths]);+ const colGroup = useMemo(() => {+ if (!columns) return null;+ return <colgroup>+ {columns.map((column, index) => (+ <col+ key={index}+ style={{ width: `${column.width}px` }}+ />+ ))}+ </colgroup>;+ }, [columns]);++ const className = useMemo(() => {+ const classList = ['simple-table', 'appflowy-scroller'];++ if (classNameProp) {+ classList.push(classNameProp);+ }++ if (enable_header_column) {+ classList.push('enable-header-column');+ }++ if (enable_header_row) {+ classList.push('enable-header-row');+ }++ return classList.join(' ');+ }, [classNameProp, enable_header_column, enable_header_row]);++ return (+ <div+ ref={ref}+ {...attributes}+ className={className}+ >+ <TableContainer+ blockId={blockId}+ readSummary={readSummary}+ >+ <table>+ {colGroup}+ <tbody>+ {children}+ </tbody>+ </table>+ </TableContainer>+ </div>+ );+ }),+ (prevProps, nextProps) => isEqual(prevProps.node, nextProps.node),+);++export default SimpleTable;\ No newline at end of filediff --git a/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTableCell.tsx b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTableCell.tsxnew file mode 100644index 0000000000..baa0c15f82--- /dev/null+++ b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTableCell.tsx@@ -0,0 +1,92 @@+import { BlockType, TableAlignType } from '@/application/types';+import { EditorElementProps, SimpleTableCellBlockNode, SimpleTableNode } from '@/components/editor/editor.type';+import { renderColor } from '@/utils/color';+import React, { forwardRef, useMemo } from 'react';+import { Editor, Element, NodeEntry } from 'slate';+import { ReactEditor, useSlate } from 'slate-react';++const SimpleTableCell =+ forwardRef<HTMLTableCellElement, EditorElementProps<SimpleTableCellBlockNode>>(({+ node,+ children,+ ...attributes+ }, ref) => {+ const { blockId } = node;+ const editor = useSlate();+ const path = ReactEditor.findPath(editor, node);++ const table = useMemo(() => {+ const match = Editor.above(editor, {+ match: (n) => {+ return !Editor.isEditor(n) && Element.isElement(n) && n.type === BlockType.SimpleTableBlock;+ },+ at: path,+ });++ if (!match) return null;++ return match as NodeEntry<Element>;+ }, [editor, path]);++ const row = useMemo(() => {+ const match = Editor.above(editor, {+ match: (n) => {+ return !Editor.isEditor(n) && Element.isElement(n) && n.type === BlockType.SimpleTableRowBlock;+ },+ at: path,+ });++ if (!match) return null;++ return match as NodeEntry<Element>;+ }, [editor, path]);++ const colIndex = useMemo(() => {+ if (!row) return 0;++ const [parentElement] = row;++ return (parentElement.children as Element[]).findIndex((n) => n.blockId === node.blockId);+ }, [node, row]);++ const { horizontalAlign, bgColor } = useMemo(() => {+ if (!table || !row) return {+ bgColor: '',+ horizontalAlign: TableAlignType.Left,+ };++ const [parentElement] = table;++ const horizontalAlign = (parentElement as SimpleTableNode).data.column_aligns[colIndex];+ const bgColor = (parentElement as SimpleTableNode).data.column_colors[colIndex];++ return {+ horizontalAlign,+ bgColor,+ };+ }, [colIndex, row, table]);++ return (+ <td+ data-block-type={node.type}+ data-block-cell={blockId}+ data-cell-index={colIndex}+ ref={ref}+ {...attributes}+ rowSpan={1}+ colSpan={1}+ data-table-cell-horizontal-align={horizontalAlign?.toLowerCase()}+ style={{+ ...attributes.style,+ backgroundColor: bgColor ? renderColor(bgColor) : undefined,+ }}+ >+ <div className={'cell-children'}>+ {children}+ </div>+ </td>+ );+ },+ );++export default SimpleTableCell;\ No newline at end of filediff --git a/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTableRow.tsx b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTableRow.tsxnew file mode 100644index 0000000000..b60c578cdc--- /dev/null+++ b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/SimpleTableRow.tsx@@ -0,0 +1,72 @@+import { BlockType, TableAlignType } from '@/application/types';+import { EditorElementProps, SimpleTableNode, SimpleTableRowNode } from '@/components/editor/editor.type';+import { renderColor } from '@/utils/color';+import React, { forwardRef, useMemo } from 'react';+import { Editor, Element, NodeEntry } from 'slate';+import { ReactEditor, useSlate } from 'slate-react';++const SimpleTableRow =+ forwardRef<HTMLTableRowElement, EditorElementProps<SimpleTableRowNode>>(({+ node,+ children,+ ...attributes+ }, ref) => {+ const { blockId } = node;+ const editor = useSlate();+ const path = ReactEditor.findPath(editor, node);++ const parent = useMemo(() => {+ const match = Editor.above(editor, {+ match: (n) => {+ return !Editor.isEditor(n) && Element.isElement(n) && n.type === BlockType.SimpleTableBlock;+ },+ at: path,+ });++ if (!match) return null;++ return match as NodeEntry<Element>;+ }, [editor, path]);++ const index = useMemo(() => {+ if (!parent) return 0;++ const [parentElement] = parent;++ return (parentElement.children as Element[]).findIndex((n) => n.blockId === node.blockId);+ }, [node, parent]);++ const { align, bgColor } = useMemo(() => {+ if (!parent) return {+ align: TableAlignType.Left,+ bgColor: '',+ };++ const [parentElement] = parent;++ return {+ align: (parentElement as SimpleTableNode).data.row_aligns[index],+ bgColor: (parentElement as SimpleTableNode).data.row_colors[index],+ };+ }, [index, parent]);++ return (+ <tr+ data-row-index={index}+ data-block-type={node.type}+ ref={ref}+ {...attributes}+ data-table-row={blockId}+ data-table-row-horizontal-align={align?.toLowerCase()}+ style={{+ ...attributes.style,+ backgroundColor: bgColor ? renderColor(bgColor) : undefined,+ }}+ >+ {children}+ </tr>+ );+ },+ );++export default SimpleTableRow;\ No newline at end of filediff --git a/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/index.ts b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/index.tsnew file mode 100644index 0000000000..05a4c8a75c--- /dev/null+++ b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/index.ts@@ -0,0 +1 @@+export * from './SimpleTable';\ No newline at end of filediff --git a/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/simple-table.scss b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/simple-table.scssnew file mode 100644index 0000000000..6457c94842--- /dev/null+++ b/frontend/appflowy_web_app/src/components/editor/components/blocks/simple-table/simple-table.scss@@ -0,0 +1,79 @@+.simple-table {+ &.enable-header-column {+ tr [data-block-type="simple_table_cell"]:first-child {+ @apply font-semibold;+ }+ }++ &.enable-header-row {+ tr:first-child > [data-block-type="simple_table_cell"] {+ @apply font-semibold;+ }+ }++ tr {+ vertical-align: top;+ }++ tr [data-block-type="simple_table_cell"]:first-child {+ @apply border-l border-t border-line-divider;+ }++ tr:first-child > [data-block-type="simple_table_cell"] {+ @apply border-t border-line-divider;+ }++ td[data-block-type="simple_table_cell"] {+ @apply border-r border-b border-line-divider;+ .cell-children {+ .block-element {+ @apply m-0;+ }+ }+ }++ tr[data-table-row-horizontal-align="center"] {+ .block-element {+ > div > .text-element {+ text-align: center;+ justify-content: center;+ }+ }+ }++ tr[data-table-row-horizontal-align="right"] {+ .block-element {+ > div > .text-element {+ text-align: right;+ justify-content: flex-end;+ }+ }+ }+++ td[data-table-cell-horizontal-align="center"] {+ .block-element {+ > div > .text-element {+ text-align: center;+ justify-content: center;+ }+ }+ }++ td[data-table-cell-horizontal-align="right"] {+ .block-element {+ > div > .text-element {+ text-align: right;+ justify-content: flex-end;+ }+ }+ }++ td .cell-children {+ @apply min-w-[120px] leading-[1.8em];+ }++ td {+ @apply p-2;+ }+}\ No newline at end of filediff --git a/frontend/appflowy_web_app/src/components/editor/components/element/Element.tsx b/frontend/appflowy_web_app/src/components/editor/components/element/Element.tsxindex 32c772b8e2..f79e58fb53 100644--- a/frontend/appflowy_web_app/src/components/editor/components/element/Element.tsx+++ b/frontend/appflowy_web_app/src/components/editor/components/element/Element.tsx@@ -14,6 +14,9 @@ import { Outline } from '@/components/editor/components/blocks/outline';import { Page } from '@/components/editor/components/blocks/page';import { Paragraph } from '@/components/editor/components/blocks/paragraph';import { Quote } from '@/components/editor/components/blocks/quote';+import SimpleTable from '@/components/editor/components/blocks/simple-table/SimpleTable';+import SimpleTableCell from '@/components/editor/components/blocks/simple-table/SimpleTableCell';+import SimpleTableRow from '@/components/editor/components/blocks/simple-table/SimpleTableRow';import { TableBlock, TableCellBlock } from '@/components/editor/components/blocks/table';import { Text } from '@/components/editor/components/blocks/text';import { useEditorContext } from '@/components/editor/EditorContext';@@ -122,6 +125,12 @@ export const Element = ({return GalleryBlock;case BlockType.SubpageBlock:return SubPage;+ case BlockType.SimpleTableBlock:+ return SimpleTable;+ case BlockType.SimpleTableRowBlock:+ return SimpleTableRow;+ case BlockType.SimpleTableCellBlock:+ return SimpleTableCell;default:return UnSupportedBlock;}@@ -151,6 +160,17 @@ export const Element = ({);}+ if ([BlockType.SimpleTableRowBlock, BlockType.SimpleTableCellBlock].includes(node.type as BlockType)) {+ return (+ <Component+ node={node}+ {...attributes}+ >+ {children}+ </Component>+ );+ }+return (<ErrorBoundary fallbackRender={ElementFallbackRender}><div {...attributes} data-block-type={node.type}diff --git a/frontend/appflowy_web_app/src/components/editor/components/table-container/TableContainer.tsx b/frontend/appflowy_web_app/src/components/editor/components/table-container/TableContainer.tsxnew file mode 100644index 0000000000..d3333f83ac--- /dev/null+++ b/frontend/appflowy_web_app/src/components/editor/components/table-container/TableContainer.tsx@@ -0,0 +1,96 @@+import { getScrollParent } from '@/components/global-comment/utils';+import React, { useCallback, useEffect, useRef } from 'react';+import { ReactEditor, useReadOnly, useSlateStatic } from 'slate-react';++function TableContainer ({ readSummary, children, paddingLeft = 0 }: {+ blockId: string;+ readSummary?: boolean;+ children?: React.ReactNode;+ paddingLeft?: number;+}) {+ const readOnly = useReadOnly();+ const editor = useSlateStatic();+ const [showControl, setShowControl] = React.useState(false);+ const controlRef = useRef<HTMLDivElement>(null);+ const [width, setWidth] = React.useState<number | undefined>(undefined);+ const offsetLeftRef = useRef(paddingLeft);+ const calcTableWidth = useCallback((editorDom: HTMLElement, scrollContainer: HTMLElement) => {+ const scrollRect = scrollContainer.getBoundingClientRect();++ setWidth(scrollRect.width - 196 + paddingLeft);+ const offsetLeft = editorDom.getBoundingClientRect().left - scrollRect.left;++ offsetLeftRef.current = offsetLeft + paddingLeft;+ }, [paddingLeft]);++ useEffect(() => {+ if (readSummary) return;+ const editorDom = ReactEditor.toDOMNode(editor, editor);+ const scrollContainer = getScrollParent(editorDom) as HTMLElement;++ if (!scrollContainer) return;+ calcTableWidth(editorDom, scrollContainer);+ const onResize = () => {+ calcTableWidth(editorDom, scrollContainer);+ };++ const resizeObserver = new ResizeObserver(onResize);++ resizeObserver.observe(scrollContainer);+ return () => {+ resizeObserver.disconnect();+ };+ }, [calcTableWidth, editor, readSummary]);+ return (+ <div+ draggable={false}+ onMouseEnter={() => {+ if (readOnly) return;+ setShowControl(true);+ }}+ onMouseLeave={() => {+ setShowControl(false);+ }}+ className={`relative w-full `}+ style={{+ width,+ maxWidth: width,+ flex: 'none',+ left: -offsetLeftRef.current,+ }}+ >+ <div+ ref={controlRef}+ style={{+ left: offsetLeftRef.current - 64,+ display: showControl ? 'block' : 'none',+ }}+ contentEditable={false}+ className={'absolute z-[10] w-[64px] top-2'}+ >+ {/*<ControlActions blockId={blockId} />*/}+ </div>+ <div+ draggable={false}+ contentEditable={false}+ onScroll={e => {+ const isHorizontal = e.currentTarget.scrollLeft > 0;+ const controlEl = controlRef.current;++ if (isHorizontal && controlEl) {+ controlEl.style.left = Math.max(-e.currentTarget.scrollLeft + offsetLeftRef.current - 64, -64) + 'px';+ }+ }}+ className={'h-full w-full overflow-x-auto overflow-y-hidden'}+ style={{+ paddingLeft: Math.max(offsetLeftRef.current - paddingLeft, 0) + 'px',+ }}+ >+ {children}+ </div>++ </div>+ );+}++export default TableContainer;\ No newline at end of filediff --git a/frontend/appflowy_web_app/src/components/editor/editor.scss b/frontend/appflowy_web_app/src/components/editor/editor.scssindex 5b90c8cf89..e265eb723d 100644--- a/frontend/appflowy_web_app/src/components/editor/editor.scss+++ b/frontend/appflowy_web_app/src/components/editor/editor.scss@@ -2,7 +2,6 @@.block-element:not([data-block-type="table/cell"]) {@apply my-[4px];-}.block-element .block-element:not([data-block-type="table/cell"]) {diff --git a/frontend/appflowy_web_app/src/components/editor/editor.type.ts b/frontend/appflowy_web_app/src/components/editor/editor.type.tsindex 2d7af49765..278e310d12 100644--- a/frontend/appflowy_web_app/src/components/editor/editor.type.ts+++ b/frontend/appflowy_web_app/src/components/editor/editor.type.ts@@ -15,7 +15,7 @@ import {BlockId,BlockData,DatabaseNodeData,- LinkPreviewBlockData, FileBlockData, GalleryBlockData, SubpageNodeData,+ LinkPreviewBlockData, FileBlockData, GalleryBlockData, SubpageNodeData, SimpleTableData,} from '@/application/types';import { HTMLAttributes } from 'react';import { Element } from 'slate';@@ -126,6 +126,22 @@ export interface OutlineNode extends BlockNode {data: OutlineBlockData;}+export interface SimpleTableNode extends BlockNode {+ type: BlockType.SimpleTableBlock;+ blockId: string;+ data: SimpleTableData;+}++export interface SimpleTableRowNode extends BlockNode {+ type: BlockType.SimpleTableRowBlock;+ blockId: string;+}++export interface SimpleTableCellBlockNode extends BlockNode {+ type: BlockType.SimpleTableCellBlock;+ blockId: string;+}+export interface TableNode extends BlockNode {type: BlockType.TableBlock;blockId: string;