Skip to content
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

feat: use jpg as default file type #178

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions src/methods.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
isFunc,
isPlainObject
isFunc
} from './utils/tools'
import {
draw,
Expand Down Expand Up @@ -104,7 +103,8 @@ export default function methods () {
}

self.getCropperImage = (opt, fn) => {
const customOptions = opt
const customOptions = Object.assign({fileType: 'jpg'}, opt)
const callback = isFunc(opt) ? opt : isFunc(fn) ? fn : null

let canvasOptions = {
canvasId: id,
Expand All @@ -116,10 +116,7 @@ export default function methods () {

let task = () => Promise.resolve()

if (
isPlainObject(customOptions) &&
customOptions.original
) {
if (customOptions.original) {
// original mode
task = () => {
self.targetCtx.drawImage(
Expand All @@ -144,14 +141,7 @@ export default function methods () {

return task()
.then(() => {
if (isPlainObject(customOptions)) {
canvasOptions = Object.assign({}, canvasOptions, customOptions)
}

if (isFunc(customOptions)) {
fn = customOptions
}

Object.assign(canvasOptions, customOptions)
const arg = canvasOptions.componentContext
? [canvasOptions, canvasOptions.componentContext]
: [canvasOptions]
Expand All @@ -160,14 +150,13 @@ export default function methods () {
})
.then(res => {
const tempFilePath = res.tempFilePath

return isFunc(fn)
? fn.call(self, tempFilePath, null)
return callback
? callback.call(self, tempFilePath, null)
: tempFilePath
})
.catch((err) => {
if (isFunc(fn)) {
fn.call(self, null, err)
if (callback) {
callback.call(self, null, err)
} else {
throw err
}
Expand Down
24 changes: 12 additions & 12 deletions src/utils/tools.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/**
* String type check
*/
exports.isStr = (v) => typeof v === 'string'
export const isStr = (v) => typeof v === 'string'
/**
* Number type check
*/
exports.isNum = (v) => typeof v === 'number'
export const isNum = (v) => typeof v === 'number'
/**
* Array type check
*/
exports.isArr = Array.isArray
export const isArr = Array.isArray
/**
* undefined type check
*/
exports.isUndef = (v) => v === undefined
export const isUndef = (v) => v === undefined

exports.isTrue = (v) => v === true
export const isTrue = (v) => v === true

exports.isFalse = (v) => v === false
export const isFalse = (v) => v === false
/**
* Function type check
*/
exports.isFunc = (v) => typeof v === 'function'
export const isFunc = (v) => typeof v === 'function'
/**
* Quick object check - this is primarily used to tell
* Objects from primitive values when we know the value
* is a JSON-compliant type.
*/
exports.isObj = exports.isObject = (obj) => {
export const isObject = (obj) => {
return obj !== null && typeof obj === 'object'
}

Expand All @@ -36,15 +36,15 @@ exports.isObj = exports.isObject = (obj) => {
* for plain JavaScript objects.
*/
const _toString = Object.prototype.toString
exports.isPlainObject = (obj) => {
export const isPlainObject = (obj) => {
return _toString.call(obj) === '[object Object]'
}

/**
* Check whether the object has the property.
*/
const hasOwnProperty = Object.prototype.hasOwnProperty
exports.hasOwn = (obj, key) => {
export const hasOwn = (obj, key) => {
return hasOwnProperty.call(obj, key)
}

Expand All @@ -53,12 +53,12 @@ exports.hasOwn = (obj, key) => {
* Stubbing args to make Flow happy without leaving useless transpiled code
* with ...rest (https://flow.org/blog/2017/05/07/Strict-Function-Call-Arity/)
*/
exports.noop = (a, b, c) => {}
export const noop = (a, b, c) => {}

/**
* Check if val is a valid array index.
*/
exports.isValidArrayIndex = (val) => {
export const isValidArrayIndex = (val) => {
const n = parseFloat(String(val))
return n >= 0 && Math.floor(n) === n && isFinite(val)
}