Skip to content

Commit

Permalink
Update inserting tables in fields.markdoc/fields.mdx to include a…
Browse files Browse the repository at this point in the history
… table header row by default and remove button to toggle table header in `fields.mdx` (#1305)
  • Loading branch information
emmatown authored Sep 12, 2024
1 parent 234d025 commit 98bd2da
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-carrots-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Update inserting tables in `fields.markdoc`/`fields.mdx` to include a table header row by default and remove button to toggle table header in `fields.mdx` since header rows cannot be removed from GFM tables
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { setBlockType } from 'prosemirror-commands';
import { NodeType } from 'prosemirror-model';
import { Command, NodeSelection } from 'prosemirror-state';
import { getEditorSchema } from '../schema';

export function insertNode(nodeType: NodeType): Command {
return (state, dispatch) => {
Expand Down Expand Up @@ -47,12 +48,15 @@ export function toggleCodeBlock(
export function insertTable(tableType: NodeType): Command {
const rowType = tableType.contentMatch.defaultType!;
const cellType = rowType.contentMatch.defaultType!;
const headerType = getEditorSchema(tableType.schema).nodes.table_header!;
return (state, dispatch) => {
const header = headerType.createAndFill()!;
const cell = cellType.createAndFill()!;
const headerRow = rowType.create(undefined, [header, header, header]);
const row = rowType.create(undefined, [cell, cell, cell]);
dispatch?.(
state.tr.replaceSelectionWith(
tableType.create(undefined, [row, row, row])
tableType.create(undefined, [headerRow, row, row])
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,26 @@ const popoverComponents: Record<

return (
<Flex gap="regular" padding="regular">
<TooltipTrigger>
<ActionButton
prominence="low"
isSelected={
props.node.firstChild?.firstChild?.type ===
schema.nodes.table_header
}
onPress={() => {
dispatchCommand(toggleHeader('row'));
}}
>
<Icon src={sheetIcon} />
</ActionButton>
<Tooltip>Header row</Tooltip>
</TooltipTrigger>
<Divider orientation="vertical" />
{schema.format === 'markdoc' && (
<>
<TooltipTrigger>
<ActionButton
prominence="low"
isSelected={
props.node.firstChild?.firstChild?.type ===
schema.nodes.table_header
}
onPress={() => {
dispatchCommand(toggleHeader('row'));
}}
>
<Icon src={sheetIcon} />
</ActionButton>
<Tooltip>Header row</Tooltip>
</TooltipTrigger>
<Divider orientation="vertical" />
</>
)}
<TooltipTrigger>
<ActionButton
prominence="low"
Expand Down
2 changes: 2 additions & 0 deletions packages/keystatic/src/form/fields/markdoc/editor/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ export type EditorSchema = {
config: EditorConfig;
components: Record<string, ContentComponent>;
insertMenuItems: InsertMenuItem[];
format: 'mdx' | 'markdoc';
};

export function createEditorSchema(
Expand Down Expand Up @@ -657,6 +658,7 @@ export function createEditorSchema(
config,
components,
insertMenuItems: [],
format: isMDX ? 'mdx' : 'markdoc',
};
schemaToEditorSchema.set(schema, editorSchema);

Expand Down

0 comments on commit 98bd2da

Please sign in to comment.