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

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 #1305

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
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
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
Loading