-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1323 from didi/fix_web_compile
feat: add preview image api
- Loading branch information
Showing
6 changed files
with
141 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
.__mpx_preview__ | ||
display none | ||
width 100% | ||
height 100% | ||
background black | ||
position fixed | ||
top 0 | ||
left 0 | ||
z-index 1000 | ||
.__mpx_preview_tip__ | ||
position absolute | ||
top 0 | ||
left 50% | ||
transform translateX(-50%) | ||
color white | ||
font-size 48px | ||
letter-spacing 0.3em | ||
.__mpx_preview_images__ | ||
display flex | ||
width 100% | ||
height 100% | ||
transition transform 0.3s ease-in-out | ||
&>div | ||
flex-shrink 0 | ||
width 100% | ||
height 100% | ||
background-position center | ||
background-size contain | ||
background-repeat no-repeat |
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,80 @@ | ||
import { webHandleSuccess, webHandleFail, createDom, warn } from '../../../common/js' | ||
import '../../../common/stylus/Preview.styl' | ||
/** | ||
* Preview class for displaying images in a slideshow format. | ||
* todo: unit test | ||
*/ | ||
export default class Preview { | ||
constructor () { | ||
this.currentIndex = 0 | ||
this.maxIndex = 0 | ||
this.minDistance = 30 | ||
this.preview = createDom('div', { class: '__mpx_preview__' }, [ | ||
this.textTip = createDom('div', { class: '__mpx_preview_tip__' }) | ||
]) | ||
document.body.appendChild(this.preview) | ||
this.initEvent() | ||
} | ||
|
||
/** | ||
* Initializes the event listeners for the preview image feature. | ||
*/ | ||
initEvent () { | ||
// swipe to change image | ||
let startX = 0 | ||
this.preview.addEventListener('touchstart', (e) => { | ||
startX = e.touches[0].pageX | ||
}) | ||
this.preview.addEventListener('touchend', (e) => { | ||
const endX = e.changedTouches[0].pageX | ||
const distance = endX - startX | ||
if (Math.abs(distance) < this.minDistance) { | ||
return | ||
} | ||
if (distance > 0) { | ||
this.currentIndex = Math.max(0, this.currentIndex - 1) | ||
} | ||
if (distance < 0) { | ||
this.currentIndex = Math.min(this.maxIndex - 1, this.currentIndex + 1) | ||
} | ||
this.preview.querySelector('.__mpx_preview_images__').style.transform = `translateX(-${this.currentIndex * 100}%)` | ||
this.updateTextTip() | ||
}) | ||
// click to close | ||
this.preview.addEventListener('click', () => { | ||
this.preview.style.display = 'none' | ||
this.preview.querySelector('.__mpx_preview_images__').remove() | ||
}) | ||
} | ||
|
||
/** | ||
* 显示预览图片 | ||
* @param {Object} options - 选项对象 | ||
* @param {string[]} options.urls - 图片地址数组 | ||
*/ | ||
show (options) { | ||
const supported = ['urls', 'success', 'fail', 'complete'] | ||
Object.keys(options).forEach(key => !supported.includes(key) && warn(`previewImage: 暂不支持选项 ${key} !`)) | ||
const { urls, success, fail, complete } = options | ||
try { | ||
this.preview.style.display = 'block' | ||
// create images with urls | ||
// append to preview | ||
this.preview.appendChild(createDom('div', { class: '__mpx_preview_images__' }, urls.map(url => createDom('div', { | ||
style: `background-image: url(${url})` | ||
})))) | ||
this.maxIndex = urls.length | ||
this.updateTextTip() | ||
webHandleSuccess({ errMsg: 'previewImage:ok' }, success, complete) | ||
} catch (e) { | ||
webHandleFail({ errMsg: 'previewImage:fail', err: e }, fail, complete) | ||
} | ||
} | ||
|
||
/** | ||
* 更新文本提示 | ||
*/ | ||
updateTextTip () { | ||
this.textTip.textContent = `${this.currentIndex + 1}/${this.maxIndex}` | ||
} | ||
} |
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,12 @@ | ||
import Preview from './Preview' | ||
|
||
let preview = null | ||
|
||
/** | ||
* 预览图片 | ||
* @param {Object} options - 预览图片的配置项 | ||
*/ | ||
export const previewImage = (options) => { | ||
if (!preview) preview = new Preview() | ||
preview.show(options) | ||
} |
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