-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added default simple layout algorithm for Masonry (createCellPositioner)
- Loading branch information
Brian Vaughn
committed
Mar 26, 2017
1 parent
6aff655
commit f3f6867
Showing
8 changed files
with
179 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
border-radius: .5rem; | ||
padding: 0.5rem; | ||
background-color: #f7f7f7; | ||
word-break: break-all; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** @flow */ | ||
import type { | ||
CellMeasurerCache, | ||
Positioner | ||
} from './Masonry' | ||
|
||
type createCellPositionerParams = { | ||
cellMeasurerCache: CellMeasurerCache, | ||
columnCount: number, | ||
columnWidth: number, | ||
spacer?: number | ||
}; | ||
|
||
type resetParams = { | ||
columnCount: number, | ||
columnWidth: number, | ||
spacer?: number | ||
}; | ||
|
||
export default function createCellPositioner ({ | ||
cellMeasurerCache, | ||
columnCount, | ||
columnWidth, | ||
spacer = 0 | ||
}: createCellPositionerParams): Positioner { | ||
let columnHeights | ||
|
||
initOrResetDerivedValues() | ||
|
||
function cellPositioner (index) { | ||
// Find the shortest column and use it. | ||
let columnIndex = 0 | ||
for (let i = 1; i < columnHeights.length; i++) { | ||
if (columnHeights[i] < columnHeights[columnIndex]) { | ||
columnIndex = i | ||
} | ||
} | ||
|
||
const left = columnIndex * (columnWidth + spacer) | ||
const top = columnHeights[columnIndex] || 0 | ||
|
||
columnHeights[columnIndex] = top + cellMeasurerCache.getHeight(index) | ||
|
||
return { | ||
left, | ||
top | ||
} | ||
} | ||
|
||
function initOrResetDerivedValues (): void { | ||
// Track the height of each column. | ||
// Layout algorithm below always inserts into the shortest column. | ||
columnHeights = [] | ||
for (let i = 0; i < columnCount; i++) { | ||
columnHeights[i] = 0 | ||
} | ||
} | ||
|
||
function reset (params: resetParams): void { | ||
columnCount = params.columnCount | ||
columnWidth = params.columnWidth | ||
spacer = params.spacer | ||
|
||
initOrResetDerivedValues() | ||
} | ||
|
||
cellPositioner.reset = reset | ||
|
||
return cellPositioner | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/** @flow */ | ||
export default from './Masonry' | ||
export Masonry from './Masonry' | ||
export createCellPositioner from './createCellPositioner' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters