-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(module:image): image fit content when drag released (#6262)
- Loading branch information
1 parent
41696e3
commit 07ae66a
Showing
4 changed files
with
270 additions
and
3 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
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,126 @@ | ||
/** | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
/** | ||
* fit content details: https://github.com/NG-ZORRO/ng-zorro-antd/pull/6154#issuecomment-745025554 | ||
* | ||
* calc position x,y point | ||
* | ||
* CASE (width <= clientWidth && height <= clientHeight): | ||
* | ||
* ------------- clientWidth ------------- | ||
* | | | ||
* | ------ width ------ | | ||
* | | | | | ||
* | | | | | ||
* client height | | | ||
* Height | | | | ||
* | | | | | ||
* | ------------------- | | ||
* | | | ||
* | | | ||
* --------------------------------------- | ||
* fixedPosition = { x: 0, y: 0 } | ||
* | ||
* | ||
* | ||
* CASE (width > clientWidth || height > clientHeight): | ||
* | ||
* ------------- clientWidth ------------- | ||
* | | | | ||
* | top | | ||
* | | | | ||
* |--left--|--------------- width ----------------- | ||
* | | | | ||
* client | | | ||
* Height | | | ||
* | | | | ||
* | | | | ||
* | height | | ||
* | | | | ||
* ---------| | | ||
* | | | ||
* | | | ||
* | | | ||
* ---------------------------------------- | ||
* | ||
* | ||
* - left || top > 0 | ||
* left -> 0 || top -> 0 | ||
* | ||
* - (left + width) < clientWidth || (top + height) < clientHeight | ||
* - left | top + width | height < clientWidth | clientHeight -> Back left | top + width | height === clientWidth | clientHeight | ||
* | ||
* DEFAULT: | ||
* - hold position | ||
* | ||
*/ | ||
export function getFitContentPosition(params: { | ||
width: number; | ||
height: number; | ||
left: number; | ||
top: number; | ||
clientWidth: number; | ||
clientHeight: number; | ||
}): { x?: number; y?: number } { | ||
let fixPos = {}; | ||
|
||
if (params.width <= params.clientWidth && params.height <= params.clientHeight) { | ||
fixPos = { | ||
x: 0, | ||
y: 0 | ||
}; | ||
} | ||
|
||
if (params.width > params.clientWidth || params.height > params.clientHeight) { | ||
fixPos = { | ||
x: fitPoint(params.left, params.width, params.clientWidth), | ||
y: fitPoint(params.top, params.height, params.clientHeight) | ||
}; | ||
} | ||
|
||
return fixPos; | ||
} | ||
|
||
export function getOffset(node: HTMLElement): { left: number; top: number } { | ||
const box = node.getBoundingClientRect(); | ||
const docElem = document.documentElement; | ||
|
||
// use docElem.scrollLeft to support IE | ||
return { | ||
left: box.left + (window.pageXOffset || docElem.scrollLeft) - (docElem.clientLeft || document.body.clientLeft || 0), | ||
top: box.top + (window.pageYOffset || docElem.scrollTop) - (docElem.clientTop || document.body.clientTop || 0) | ||
}; | ||
} | ||
|
||
export function getClientSize(): { width: number; height: number } { | ||
const width = document.documentElement.clientWidth; | ||
const height = window.innerHeight || document.documentElement.clientHeight; | ||
return { | ||
width, | ||
height | ||
}; | ||
} | ||
|
||
function fitPoint(start: number, size: number, clientSize: number): number | null { | ||
const startAddSize = start + size; | ||
const offsetStart = (size - clientSize) / 2; | ||
let distance: number | null = null; | ||
|
||
if (size > clientSize) { | ||
if (start > 0) { | ||
distance = offsetStart; | ||
} | ||
if (start < 0 && startAddSize < clientSize) { | ||
distance = -offsetStart; | ||
} | ||
} else { | ||
if (start < 0 || startAddSize > clientSize) { | ||
distance = start < 0 ? offsetStart : -offsetStart; | ||
} | ||
} | ||
|
||
return distance; | ||
} |