-
Notifications
You must be signed in to change notification settings - Fork 117
feat: support paragraph bar borders #2736
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||||||
| * | ||||||||||
| * @ooxml w:pPr/w:pBdr — paragraph border properties | ||||||||||
| * @ooxml w:pPr/w:pBdr/w:top, w:bottom, w:left, w:right — side borders | ||||||||||
| * @ooxml w:pPr/w:pBdr/w:bar — bar border (rendered as a separate left-side rule) | ||||||||||
| * @ooxml w:pPr/w:pBdr/w:between — between border (rendered as bottom within groups) | ||||||||||
| * @ooxml w:pPr/w:shd — paragraph shading (background fill) | ||||||||||
| * @spec ECMA-376 §17.3.1.24 (pBdr), §17.3.1.31 (shd) | ||||||||||
|
|
@@ -182,6 +183,93 @@ const computeRenderedBorderWidths = ( | |||||||||
|
|
||||||||||
| type CssBorderSide = 'top' | 'right' | 'bottom' | 'left'; | ||||||||||
| const BORDER_SIDES: CssBorderSide[] = ['top', 'right', 'bottom', 'left']; | ||||||||||
| const PARAGRAPH_BAR_CLASS = 'superdoc-paragraph-bar'; | ||||||||||
|
|
||||||||||
| type ResolvedParagraphBorder = { | ||||||||||
| style: 'none' | 'solid' | 'dashed' | 'dotted' | 'double'; | ||||||||||
| width: number; | ||||||||||
| color: string; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| const resolveParagraphBorder = (border: ParagraphBorder): ResolvedParagraphBorder => { | ||||||||||
| const style = border.style && border.style !== 'none' ? border.style : border.style === 'none' ? 'none' : 'solid'; | ||||||||||
|
|
||||||||||
| if (style === 'none') { | ||||||||||
| return { | ||||||||||
| style, | ||||||||||
| width: 0, | ||||||||||
| color: border.color ?? '#000', | ||||||||||
| }; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return { | ||||||||||
| style, | ||||||||||
| width: border.width != null ? Math.max(0, border.width) : 1, | ||||||||||
| color: border.color ?? '#000', | ||||||||||
| }; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| const getRenderedParagraphBorderWidth = (border?: ParagraphBorder): number => { | ||||||||||
| if (!border) return 0; | ||||||||||
| const resolved = resolveParagraphBorder(border); | ||||||||||
| if (resolved.style === 'none') return 0; | ||||||||||
| return resolved.width; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| const getParagraphBarElement = (element: HTMLElement): HTMLElement | undefined => { | ||||||||||
| return Array.from(element.children).find( | ||||||||||
| (child): child is HTMLElement => child instanceof HTMLElement && child.classList.contains(PARAGRAPH_BAR_CLASS), | ||||||||||
| ); | ||||||||||
|
Comment on lines
+219
to
+222
Contributor
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.
|
||||||||||
| }; | ||||||||||
|
|
||||||||||
| const syncParagraphBarElement = ( | ||||||||||
| element: HTMLElement, | ||||||||||
| barBorder?: ParagraphBorder, | ||||||||||
| leftBorder?: ParagraphBorder, | ||||||||||
| ): void => { | ||||||||||
| const existingBarElement = getParagraphBarElement(element); | ||||||||||
| if (!barBorder) { | ||||||||||
| existingBarElement?.remove(); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const resolvedBar = resolveParagraphBorder(barBorder); | ||||||||||
| if (resolvedBar.style === 'none' || resolvedBar.width <= 0) { | ||||||||||
| existingBarElement?.remove(); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const computedPosition = element.ownerDocument.defaultView?.getComputedStyle(element).position; | ||||||||||
| if (!element.style.position && (!computedPosition || computedPosition === 'static')) { | ||||||||||
| element.style.position = 'relative'; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const barElement = existingBarElement ?? element.ownerDocument.createElement('div'); | ||||||||||
| if (!existingBarElement) { | ||||||||||
| barElement.classList.add(PARAGRAPH_BAR_CLASS); | ||||||||||
| element.appendChild(barElement); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const barSpace = Math.max(0, barBorder.space ?? 0) * PX_PER_PT; | ||||||||||
| const renderedLeftBorderWidth = getRenderedParagraphBorderWidth(leftBorder); | ||||||||||
|
|
||||||||||
| barElement.style.position = 'absolute'; | ||||||||||
| barElement.style.pointerEvents = 'none'; | ||||||||||
| barElement.style.boxSizing = 'border-box'; | ||||||||||
| barElement.style.top = '0px'; | ||||||||||
| barElement.style.bottom = '0px'; | ||||||||||
| barElement.style.left = `-${renderedLeftBorderWidth + barSpace}px`; | ||||||||||
| barElement.style.width = '0px'; | ||||||||||
| barElement.style.borderLeftStyle = resolvedBar.style; | ||||||||||
| barElement.style.borderLeftWidth = `${resolvedBar.width}px`; | ||||||||||
| barElement.style.borderLeftColor = resolvedBar.color; | ||||||||||
|
Comment on lines
+263
to
+265
Contributor
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. these three lines do the same thing as
Suggested change
|
||||||||||
| }; | ||||||||||
|
|
||||||||||
| const clearBorderSideStyle = (element: HTMLElement, side: CssBorderSide): void => { | ||||||||||
| element.style.removeProperty(`border-${side}-style`); | ||||||||||
| element.style.removeProperty(`border-${side}-width`); | ||||||||||
| element.style.removeProperty(`border-${side}-color`); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Applies paragraph border styles to an HTML element. | ||||||||||
|
|
@@ -195,7 +283,14 @@ export const applyParagraphBorderStyles = ( | |||||||||
| borders?: ParagraphAttrs['borders'], | ||||||||||
| betweenInfo?: BetweenBorderInfo, | ||||||||||
| ): void => { | ||||||||||
| if (!borders) return; | ||||||||||
| BORDER_SIDES.forEach((side) => { | ||||||||||
| clearBorderSideStyle(element, side); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| if (!borders) { | ||||||||||
| syncParagraphBarElement(element); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| const showBetweenBorder = betweenInfo?.showBetweenBorder ?? false; | ||||||||||
| const suppressTopBorder = betweenInfo?.suppressTopBorder ?? false; | ||||||||||
| const suppressBottomBorder = betweenInfo?.suppressBottomBorder ?? false; | ||||||||||
|
|
@@ -214,12 +309,13 @@ export const applyParagraphBorderStyles = ( | |||||||||
| if (showBetweenBorder && borders.between) { | ||||||||||
| setBorderSideStyle(element, 'bottom', borders.between); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| syncParagraphBarElement(element, borders.bar, borders.left); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| const setBorderSideStyle = (element: HTMLElement, side: CssBorderSide, border: ParagraphBorder): void => { | ||||||||||
| const resolvedStyle = | ||||||||||
| border.style && border.style !== 'none' ? border.style : border.style === 'none' ? 'none' : 'solid'; | ||||||||||
| if (resolvedStyle === 'none') { | ||||||||||
| const resolved = resolveParagraphBorder(border); | ||||||||||
| if (resolved.style === 'none') { | ||||||||||
| element.style.setProperty(`border-${side}-style`, 'none'); | ||||||||||
| element.style.setProperty(`border-${side}-width`, '0px'); | ||||||||||
| if (border.color) { | ||||||||||
|
|
@@ -228,10 +324,9 @@ const setBorderSideStyle = (element: HTMLElement, side: CssBorderSide, border: P | |||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const width = border.width != null ? Math.max(0, border.width) : undefined; | ||||||||||
| element.style.setProperty(`border-${side}-style`, resolvedStyle); | ||||||||||
| element.style.setProperty(`border-${side}-width`, `${width ?? 1}px`); | ||||||||||
| element.style.setProperty(`border-${side}-color`, border.color ?? '#000'); | ||||||||||
| element.style.setProperty(`border-${side}-style`, resolved.style); | ||||||||||
| element.style.setProperty(`border-${side}-width`, `${resolved.width}px`); | ||||||||||
| element.style.setProperty(`border-${side}-color`, resolved.color); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| // ─── Dataset stamping ───────────────────────────────────────────── | ||||||||||
|
|
||||||||||
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.
heads up — this test expects bar-only differences to break grouping, but Word and Google Docs both keep them grouped. if the grouping change above goes in, this test would need to flip.