-
Notifications
You must be signed in to change notification settings - Fork 167
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
Remove table format #2225
Remove table format #2225
Changes from 4 commits
f74149b
7dce8ed
34519c3
453c6e1
6c07965
263e6eb
82b1b5d
2900d67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,9 @@ const Black = '#000000'; | |
* @param color The color to set | ||
* @param isColorOverride @optional When pass true, it means this shade color is not part of table format, so it can be preserved when apply table format | ||
* @param applyToSegments @optional When pass true, we will also apply text color from table cell to its child blocks and segments | ||
* | ||
*/ | ||
|
||
export function setTableCellBackgroundColor( | ||
cell: ContentModelTableCell, | ||
color: string | null | undefined, | ||
|
@@ -43,28 +45,80 @@ export function setTableCellBackgroundColor( | |
delete cell.format.textColor; | ||
} | ||
|
||
if (applyToSegments && cell.format.textColor) { | ||
cell.blocks.forEach(block => { | ||
if (block.blockType == 'Paragraph') { | ||
if (applyToSegments) { | ||
setAdaptiveCellColor(cell); | ||
} | ||
} else { | ||
delete cell.format.backgroundColor; | ||
delete cell.format.textColor; | ||
if (applyToSegments) { | ||
removeAdaptiveCellColor(cell); | ||
} | ||
} | ||
|
||
delete cell.cachedElement; | ||
} | ||
|
||
function removeAdaptiveCellColor(cell: ContentModelTableCell) { | ||
cell.blocks.forEach(block => { | ||
if (block.blockType == 'Paragraph') { | ||
if ( | ||
block.segmentFormat?.textColor && | ||
shouldRemoveColor(block.segmentFormat?.textColor, cell.format.backgroundColor || '') | ||
) { | ||
delete block.segmentFormat.textColor; | ||
} | ||
block.segments.forEach(segment => { | ||
if ( | ||
segment.format.textColor && | ||
shouldRemoveColor(segment.format.textColor, cell.format.backgroundColor || '') | ||
) { | ||
delete segment.format.textColor; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
function setAdaptiveCellColor(cell: ContentModelTableCell) { | ||
if (cell.format.textColor) { | ||
cell.blocks.forEach(block => { | ||
if (block.blockType == 'Paragraph') { | ||
if (!block.segmentFormat?.textColor) { | ||
block.segmentFormat = { | ||
...block.segmentFormat, | ||
textColor: cell.format.textColor, | ||
}; | ||
block.segments.forEach(segment => { | ||
} | ||
block.segments.forEach(segment => { | ||
if (!segment.format?.textColor) { | ||
segment.format = { | ||
...segment.format, | ||
textColor: cell.format.textColor, | ||
}; | ||
}); | ||
} | ||
}); | ||
} | ||
} else { | ||
delete cell.format.backgroundColor; | ||
delete cell.format.textColor; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
delete cell.cachedElement; | ||
/** | ||
* If the cell background color is white or black, and the text color is white or black, we should remove the text color | ||
* @param textColor the segment or block text color | ||
* @param cellBackgroundColor the cell background color | ||
* @returns | ||
*/ | ||
function shouldRemoveColor(textColor: string, cellBackgroundColor: string) { | ||
if ( | ||
([White, 'rgb(255,255,255)'].indexOf(textColor) > -1 && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems you are checking both text color and background color to be white (or black). Are you sure about that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed to consider other bright or dark colors |
||
[White, 'rgb(255,255,255)', ''].indexOf(cellBackgroundColor) > -1) || | ||
([Black, 'rgb(0,0,0)'].indexOf(textColor) > -1 && | ||
[Black, 'rgb(0,0,0)', ''].indexOf(cellBackgroundColor) > -1) | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function calculateLightness(color: string) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this empty line