Skip to content

Commit af4f384

Browse files
committed
refactor: add attribute to ignore background image
1 parent f860916 commit af4f384

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

paginator.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ class View {
336336
// in scrolled mode the view expands to fit the image.
337337
const bgUrl = this.docBackground
338338
?.match(/url\(["']?([^"')]+)["']?\)/)?.[1]
339-
if (bgUrl) {
339+
if (bgUrl && !this.container.noBackground) {
340340
const img = new Image()
341341
img.onload = () => {
342342
this.#bgImageSize = {
@@ -390,14 +390,16 @@ class View {
390390
})
391391
const availableWidth = Math.trunc(width - marginLeft - marginRight)
392392
const availableHeight = Math.trunc(height - marginTop - marginBottom)
393+
const sidePaddingLeft = marginLeft / 2 + gap / 2
394+
const sidePaddingRight = marginRight / 2 + gap / 2
393395
setStyles(doc.documentElement, {
394396
'padding': vertical
395397
? `${marginTop * 1.5}px 0px ${marginBottom * 1.5}px 0px`
396-
: `0px ${gap / 2 + marginRight / 2}px 0px ${gap / 2 + marginLeft / 2}px`,
398+
: `0px ${sidePaddingRight}px 0px ${sidePaddingLeft}px`,
397399
'--page-margin-top': `${vertical ? marginTop * 1.5 : marginTop}px`,
398-
'--page-margin-right': `${vertical ? marginRight : marginRight + gap /2}px`,
400+
'--page-margin-right': `${vertical ? marginRight : sidePaddingRight}px`,
399401
'--page-margin-bottom': `${vertical ? marginBottom * 1.5 : marginBottom}px`,
400-
'--page-margin-left': `${vertical ? marginLeft : marginLeft + gap / 2}px`,
402+
'--page-margin-left': `${vertical ? marginLeft : sidePaddingLeft}px`,
401403
'--full-width': `${Math.trunc(window.innerWidth)}`,
402404
'--full-height': `${Math.trunc(window.innerHeight)}`,
403405
'--available-width': `${availableWidth}`,
@@ -419,10 +421,13 @@ class View {
419421
this.#columnCount = columnCount || 1
420422

421423
const doc = this.document
424+
const horizontalColumnGap = (marginLeft + marginRight) / 2 + gap
425+
const sidePaddingLeft = marginLeft / 2 + gap / 2
426+
const sidePaddingRight = marginRight / 2 + gap / 2
422427
setStylesImportant(doc.documentElement, {
423428
'box-sizing': 'border-box',
424429
'column-width': `${Math.trunc(columnWidth)}px`,
425-
'column-gap': vertical ? `${(marginTop + marginBottom) * 1.5}px` : `${gap + marginRight / 2 + marginLeft / 2}px`,
430+
'column-gap': vertical ? `${(marginTop + marginBottom) * 1.5}px` : `${horizontalColumnGap}px`,
426431
'column-fill': 'auto',
427432
...(vertical
428433
? { 'width': `${width}px` }
@@ -446,11 +451,11 @@ class View {
446451
setStyles(doc.documentElement, {
447452
'padding': vertical
448453
? `${marginTop * 1.5}px ${marginRight}px ${marginBottom * 1.5}px ${marginLeft}px`
449-
: `${marginTop}px ${gap / 2 + marginRight / 2}px ${marginBottom}px ${gap / 2 + marginLeft / 2}px`,
454+
: `${marginTop}px ${sidePaddingRight}px ${marginBottom}px ${sidePaddingLeft}px`,
450455
'--page-margin-top': `${vertical ? marginTop * 1.5 : marginTop}px`,
451-
'--page-margin-right': `${vertical ? marginRight : marginRight / 2 + gap /2}px`,
456+
'--page-margin-right': `${vertical ? marginRight : sidePaddingRight}px`,
452457
'--page-margin-bottom': `${vertical ? marginBottom * 1.5 : marginBottom}px`,
453-
'--page-margin-left': `${vertical ? marginLeft : marginLeft / 2 + gap / 2}px`,
458+
'--page-margin-left': `${vertical ? marginLeft : sidePaddingLeft}px`,
454459
'--full-width': `${Math.trunc(window.innerWidth)}`,
455460
'--full-height': `${Math.trunc(window.innerHeight)}`,
456461
'--available-width': `${availableWidth}`,
@@ -769,7 +774,7 @@ export class Paginator extends HTMLElement {
769774
static observedAttributes = [
770775
'flow', 'gap', 'margin-top', 'margin-bottom', 'margin-left', 'margin-right',
771776
'max-inline-size', 'max-block-size', 'max-column-count',
772-
'no-preload', 'no-continuous-scroll',
777+
'no-preload', 'no-background', 'no-continuous-scroll',
773778
]
774779
#root = this.attachShadow({ mode: 'open' })
775780
#observer = new ResizeObserver(() => this.render())
@@ -1196,6 +1201,7 @@ export class Paginator extends HTMLElement {
11961201
#replaceBackground(atPosition) {
11971202
const doc = this.#primaryView?.document
11981203
if (!doc?.documentElement) return
1204+
if (this.noBackground) return
11991205
const htmlStyle = doc.defaultView.getComputedStyle(doc.documentElement)
12001206
const themeBgColor = htmlStyle.getPropertyValue('--theme-bg-color')
12011207
const overrideColor = htmlStyle.getPropertyValue('--override-color') === 'true'
@@ -1387,6 +1393,9 @@ export class Paginator extends HTMLElement {
13871393
get noPreload() {
13881394
return this.hasAttribute('no-preload')
13891395
}
1396+
get noBackground() {
1397+
return this.hasAttribute('no-background')
1398+
}
13901399
get noContinuousScroll() {
13911400
return this.scrolled && this.hasAttribute('no-continuous-scroll')
13921401
}

0 commit comments

Comments
 (0)