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: Table row/column extension UI #1172

Merged
merged 19 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions packages/ariakit/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { SuggestionMenuItem } from "./suggestionMenu/SuggestionMenuItem.js";
import { SuggestionMenuLabel } from "./suggestionMenu/SuggestionMenuLabel.js";
import { SuggestionMenuLoader } from "./suggestionMenu/SuggestionMenuLoader.js";
import { TableHandle } from "./tableHandle/TableHandle.js";
import { ExtendButton } from "./tableHandle/ExtendButton.js";
import { Toolbar } from "./toolbar/Toolbar.js";
import { ToolbarButton } from "./toolbar/ToolbarButton.js";
import { ToolbarSelect } from "./toolbar/ToolbarSelect.js";
Expand Down Expand Up @@ -81,6 +82,7 @@ export const components: Components = {
},
TableHandle: {
Root: TableHandle,
ExtendButton: ExtendButton,
},
Generic: {
Form: {
Expand Down
25 changes: 24 additions & 1 deletion packages/ariakit/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,33 @@
}

.bn-ariakit .bn-side-menu,
.bn-ariakit .bn-table-handle {
.bn-ariakit .bn-table-handle,
.bn-ariakit .bn-extend-button {
color: gray;
}

.bn-ariakit .bn-extend-button-editing {
background-color: hsl(204 4% 0% / 0.05);
}

.bn-ariakit .bn-extend-button-editing:where(.dark, .dark *) {
background-color: hsl(204 20% 100% / 0.05);
}

.bn-ariakit .bn-extend-button-row {
height: 100%;
width: 18px;
padding: 0;
margin-left: 4px;
}

.bn-ariakit .bn-extend-button-column {
height: 18px;
width: 100%;
padding: 0;
margin-top: 4px;
}

.bn-ak-button:where(.dark, .dark *) {
color: hsl(204 20% 100%);
}
Expand Down
30 changes: 30 additions & 0 deletions packages/ariakit/src/tableHandle/ExtendButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Button as AriakitButton } from "@ariakit/react";

import { forwardRef } from "react";
import { ComponentProps } from "@blocknote/react";
import { assertEmpty, mergeCSSClasses } from "@blocknote/core";

export const ExtendButton = forwardRef<
HTMLButtonElement,
ComponentProps["TableHandle"]["ExtendButton"]
>((props, ref) => {
const { children, className, onDragStart, onMouseDown, ...rest } = props;

// false, because rest props can be added by mantine when button is used as a trigger
// assertEmpty in this case is only used at typescript level, not runtime level
assertEmpty(rest, false);

return (
<AriakitButton
className={mergeCSSClasses(
"bn-ak-button bn-ak-secondary",
className || ""
)}
ref={ref}
onDragStart={onDragStart}
onMouseDown={onMouseDown}
{...rest}>
{children}
</AriakitButton>
);
});
17 changes: 15 additions & 2 deletions packages/core/src/api/nodeConversions/blockToNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ export function tableContentToNodes<

for (const row of tableContent.rows) {
const columnNodes: Node[] = [];
for (const cell of row.cells) {
for (let i = 0; i < row.cells.length; i++) {
const cell = row.cells[i];
let pNode: Node;
if (!cell) {
pNode = schema.nodes["tableParagraph"].create({});
Expand All @@ -172,7 +173,19 @@ export function tableContentToNodes<
pNode = schema.nodes["tableParagraph"].create({}, textNodes);
}

const cellNode = schema.nodes["tableCell"].create({}, pNode);
const cellNode = schema.nodes["tableCell"].create(
{
// The colwidth array should have multiple values when the colspan of
// a cell is greater than 1. However, this is not yet implemented so
// we can always assume a length of 1.
colwidth: [
tableContent.columnWidths
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
? tableContent.columnWidths[i] || 100
: 100,
],
},
pNode
);
columnNodes.push(cellNode);
}
const rowNode = schema.nodes["tableRow"].create({}, columnNodes);
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/api/nodeConversions/nodeToBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@ export function contentNodeToTableContent<
>(contentNode: Node, inlineContentSchema: I, styleSchema: S) {
const ret: TableContent<I, S> = {
type: "tableContent",
columnWidths: [],
rows: [],
};

contentNode.content.forEach((rowNode) => {
contentNode.content.forEach((rowNode, _offset, index) => {
const row: TableContent<I, S>["rows"][0] = {
cells: [],
};

if (index === 0) {
rowNode.content.forEach((cellNode) => {
// The colwidth array should have multiple values when the colspan of a
// cell is greater than 1. However, this is not yet implemented so we
// can always assume a length of 1.
ret.columnWidths.push(cellNode.attrs.colwidth[0] || 100);
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
});
}

rowNode.content.forEach((cellNode) => {
row.cells.push(
contentNodeToInlineContent(
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/blocks/TableBlockContent/TableExtension.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
import { callOrReturn, Extension, getExtensionField } from "@tiptap/core";
import { columnResizing, tableEditing } from "prosemirror-tables";
import { TableView } from "prosemirror-tables";
import { Node as PMNode } from "prosemirror-model";
import { mergeCSSClasses } from "../../util/browser.js";

export const TableExtension = Extension.create({
name: "BlockNoteTableExtension",

addProseMirrorPlugins: () => {
class CustomTableView extends TableView {
YousefED marked this conversation as resolved.
Show resolved Hide resolved
constructor(public node: PMNode, public cellMinWidth: number) {
super(node, cellMinWidth);

const blockContent = document.createElement("div");
blockContent.className = mergeCSSClasses(
"bn-block-content"
// blockContentHTMLAttributes.class
);
blockContent.setAttribute("data-content-type", "table");
// for (const [attribute, value] of Object.entries(blockContentHTMLAttributes)) {
// if (attribute !== "class") {
// blockContent.setAttribute(attribute, value);
// }
// }

const tableWrapper = this.dom;
blockContent.appendChild(tableWrapper);

this.dom = blockContent;
}
}

return [
columnResizing({
cellMinWidth: 100,
View: CustomTableView,
}),
tableEditing(),
];
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/editor/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Tippy popups that are appended to document.body directly
/* table related: */
.bn-editor table {
width: auto !important;
margin-bottom: 2em;
}
.bn-editor th,
.bn-editor td {
Expand All @@ -115,10 +116,6 @@ Tippy popups that are appended to document.body directly
padding: 3px 5px;
}

.bn-editor .tableWrapper {
margin: 1em 0;
}

.bn-editor th {
font-weight: bold;
text-align: left;
Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/extensions/TableHandles/TableHandlesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class TableHandlesView<

public tableId: string | undefined;
public tablePos: number | undefined;
public tableElement: HTMLElement | undefined;

public menuFrozen = false;

Expand Down Expand Up @@ -195,6 +196,7 @@ export class TableHandlesView<
if (!blockEl) {
return;
}
this.tableElement = blockEl.node;

let tableBlock: Block<any, any, any> | undefined = undefined;

Expand Down Expand Up @@ -410,6 +412,41 @@ export class TableHandlesView<
}
};

// Updates drag handle positions on table content updates.
update() {
if (!this.state || !this.state.show) {
return;
}

// TODO: Can also just do down the DOM tree until we find it but this seems
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
// cleaner.
const tableBody = this.tableElement!.querySelector("tbody");
if (!tableBody) {
return;
}

if (this.state.rowIndex >= tableBody.children.length) {
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
this.state.rowIndex = tableBody.children.length - 1;
this.emitUpdate();

return;
}
const row = tableBody.children[this.state.rowIndex];

if (this.state.colIndex >= tableBody.children[0].children.length) {
this.state.colIndex = tableBody.children[0].children.length - 1;
this.emitUpdate();

return;
}
const cell = row.children[this.state.colIndex];

// TODO: Check if DOMRects changed first?
this.state.referencePosCell = cell.getBoundingClientRect();
this.state.referencePosTable = tableBody.getBoundingClientRect();
this.emitUpdate();
}

destroy() {
this.pmView.dom.removeEventListener("mousemove", this.mouseMoveHandler);
this.pmView.root.removeEventListener(
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/schema/blocks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export type TableContent<
S extends StyleSchema = StyleSchema
> = {
type: "tableContent";
columnWidths: number[];
matthewlipski marked this conversation as resolved.
Show resolved Hide resolved
rows: {
cells: InlineContent<I, S>[][];
}[];
Expand Down Expand Up @@ -224,6 +225,7 @@ export type PartialTableContent<
S extends StyleSchema = StyleSchema
> = {
type: "tableContent";
columnWidths?: (number | undefined)[];
rows: {
cells: PartialInlineContent<I, S>[];
}[];
Expand Down
2 changes: 2 additions & 0 deletions packages/mantine/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { SuggestionMenuItem } from "./suggestionMenu/SuggestionMenuItem.js";
import { SuggestionMenuLabel } from "./suggestionMenu/SuggestionMenuLabel.js";
import { SuggestionMenuLoader } from "./suggestionMenu/SuggestionMenuLoader.js";
import { TableHandle } from "./tableHandle/TableHandle.js";
import { ExtendButton } from "./tableHandle/ExtendButton.js";
import { Toolbar } from "./toolbar/Toolbar.js";
import { ToolbarButton } from "./toolbar/ToolbarButton.js";
import { ToolbarSelect } from "./toolbar/ToolbarSelect.js";
Expand Down Expand Up @@ -90,6 +91,7 @@ export const components: Components = {
},
TableHandle: {
Root: TableHandle,
ExtendButton: ExtendButton,
},
Generic: {
Form: {
Expand Down
19 changes: 17 additions & 2 deletions packages/mantine/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@
}

/* Table Handle styling */
.bn-mantine .bn-table-handle {
.bn-mantine .bn-table-handle,
.bn-mantine .bn-extend-button {
align-items: center;
background-color: var(--bn-colors-menu-background);
border: var(--bn-border);
Expand All @@ -508,10 +509,24 @@
}

.bn-mantine .bn-table-handle:hover,
.bn-mantine .bn-table-handle-dragging {
.bn-mantine .bn-table-handle-dragging,
.bn-mantine .bn-extend-button:hover,
.bn-mantine .bn-extend-button-editing {
background-color: var(--bn-colors-hovered-background);
}

.bn-mantine .bn-extend-button-row {
height: 100%;
width: 18px;
margin-left: 4px;
}

.bn-mantine .bn-extend-button-column {
height: 18px;
width: 100%;
margin-top: 4px;
}

/* Drag Handle & Table Handle Menu styling */
.bn-mantine .bn-drag-handle-menu {
overflow: visible;
Expand Down
27 changes: 27 additions & 0 deletions packages/mantine/src/tableHandle/ExtendButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Button as MantineButton } from "@mantine/core";

import { forwardRef } from "react";
import { ComponentProps } from "@blocknote/react";
import { assertEmpty } from "@blocknote/core";

export const ExtendButton = forwardRef<
HTMLButtonElement,
ComponentProps["TableHandle"]["ExtendButton"]
>((props, ref) => {
const { children, className, onDragStart, onMouseDown, ...rest } = props;

// false, because rest props can be added by mantine when button is used as a trigger
// assertEmpty in this case is only used at typescript level, not runtime level
assertEmpty(rest, false);

return (
<MantineButton
className={className}
ref={ref}
onDragStart={onDragStart}
onMouseDown={onMouseDown}
{...rest}>
{children}
</MantineButton>
);
});
Loading
Loading