Skip to content

Commit

Permalink
Merge branch 'master' into u/jisong/resizetable
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong authored Feb 7, 2025
2 parents 2ebfdea + 9dbe329 commit fc752dd
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ export const defaultContentModelFormatMap: DefaultImplicitFormatMap = {
marginTop: '1em',
marginBottom: '1em',
},
th: {
fontWeight: 'bold',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export const defaultHTMLStyleMap: DefaultStyleMap = {
},
th: {
display: 'table-cell',
fontWeight: 'bold',
},
u: {
textDecoration: 'underline',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ export function setFirstColumnFormatBorders(
switch (rowIndex) {
case 0:
cell.isHeader = !!format.hasHeaderRow;

if (cell.isHeader) {
cell.format.fontWeight = 'bold';
}
break;
case rows.length - 1:
setBorderColor(cell.format, 'borderTop');
Expand Down Expand Up @@ -295,6 +299,7 @@ function setHeaderRowFormat(
const cell = mutateBlock(readonlyCell);

cell.isHeader = true;
cell.format.fontWeight = 'bold';

if (format.headerRowColor) {
if (!metaOverrides.bgColorOverrides[rowIndex][cellIndex]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { hasMetadata } from '../../modelApi/metadata/updateMetadata';
import { isBlockEmpty } from '../../modelApi/common/isEmpty';
import { moveChildNodes } from '../../domUtils/moveChildNodes';
import { reuseCachedElement } from '../../domUtils/reuseCachedElement';
import { stackFormat } from '../utils/stackFormat';
import type {
ContentModelBlockHandler,
ContentModelTable,
Expand Down Expand Up @@ -96,9 +97,9 @@ export const handleTable: ContentModelBlockHandler<ContentModelTable> = (
}

if (!cell.spanAbove && !cell.spanLeft) {
const tag = cell.isHeader ? 'th' : 'td';
const td =
(context.allowCacheElement && cell.cachedElement) ||
doc.createElement(cell.isHeader ? 'th' : 'td');
(context.allowCacheElement && cell.cachedElement) || doc.createElement(tag);

tr.appendChild(td);

Expand Down Expand Up @@ -132,18 +133,25 @@ export const handleTable: ContentModelBlockHandler<ContentModelTable> = (
}
}

if (!cell.cachedElement) {
if (context.allowCacheElement) {
cell.cachedElement = td;
stackFormat(context, tag, () => {
if (!cell.cachedElement) {
if (context.allowCacheElement) {
cell.cachedElement = td;
}

applyFormat(td, context.formatAppliers.block, cell.format, context);
applyFormat(td, context.formatAppliers.tableCell, cell.format, context);
applyFormat(
td,
context.formatAppliers.tableCellBorder,
cell.format,
context
);
applyFormat(td, context.formatAppliers.dataset, cell.dataset, context);
}

applyFormat(td, context.formatAppliers.block, cell.format, context);
applyFormat(td, context.formatAppliers.tableCell, cell.format, context);
applyFormat(td, context.formatAppliers.tableCellBorder, cell.format, context);
applyFormat(td, context.formatAppliers.dataset, cell.dataset, context);
}

context.modelHandlers.blockGroupChildren(doc, td, cell, context);
context.modelHandlers.blockGroupChildren(doc, td, cell, context);
});

context.onNodeCreated?.(cell, td);
}
Expand Down
153 changes: 153 additions & 0 deletions packages/roosterjs-content-model-dom/test/endToEndTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2226,4 +2226,157 @@ describe('End to end test for DOM => Model => DOM/TEXT', () => {
'<div style="text-align: center;">test</div>'
);
});

it('TH without font-weight', () => {
runTest(
'<table><tr><th>test</th></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
widths: [],
rows: [
{
height: 0,
cells: [
{
spanAbove: false,
spanLeft: false,
isHeader: true,
blockGroupType: 'TableCell',
blocks: [
{
isImplicit: true,
segments: [
{
text: 'test',
segmentType: 'Text',
format: {
fontWeight: 'bold',
},
},
],
blockType: 'Paragraph',
format: {},
},
],
format: {},
dataset: {},
},
],
format: {},
},
],
blockType: 'Table',
format: {},
dataset: {},
},
],
},
'test',
'<table><tbody><tr><th>test</th></tr></tbody></table>'
);
});

it('TH with font-weight: 400', () => {
runTest(
'<table><tr><th style="font-weight:400">test</th></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
widths: [],
rows: [
{
height: 0,
cells: [
{
spanAbove: false,
spanLeft: false,
isHeader: true,
blockGroupType: 'TableCell',
blocks: [
{
isImplicit: true,
segments: [
{
text: 'test',
segmentType: 'Text',
format: {
fontWeight: '400',
},
},
],
blockType: 'Paragraph',
format: {},
},
],
format: {},
dataset: {},
},
],
format: {},
},
],
blockType: 'Table',
format: {},
dataset: {},
},
],
},
'test',
'<table><tbody><tr><th><span style="font-weight: 400;">test</span></th></tr></tbody></table>'
);
});

it('TH with font-weight: bold', () => {
runTest(
'<table><tr><th style="font-weight: bold">test</th></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
widths: [],
rows: [
{
height: 0,
cells: [
{
spanAbove: false,
spanLeft: false,
isHeader: true,
blockGroupType: 'TableCell',
blocks: [
{
isImplicit: true,
segments: [
{
text: 'test',
segmentType: 'Text',
format: {
fontWeight: 'bold',
},
},
],
blockType: 'Paragraph',
format: {},
},
],
format: {},
dataset: {},
},
],
format: {},
},
],
blockType: 'Table',
format: {},
dataset: {},
},
],
},
'test',
'<table><tbody><tr><th>test</th></tr></tbody></table>'
);
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BoldFormat } from './formatParts/BoldFormat';
import type { BorderBoxFormat } from './formatParts/BorderBoxFormat';
import type { ContentModelBlockFormat } from './ContentModelBlockFormat';
import type { SizeFormat } from './formatParts/SizeFormat';
Expand All @@ -13,4 +14,5 @@ export type ContentModelTableCellFormat = ContentModelBlockFormat &
VerticalAlignFormat &
WordBreakFormat &
TextColorFormat &
SizeFormat;
SizeFormat &
BoldFormat;

0 comments on commit fc752dd

Please sign in to comment.