-
Notifications
You must be signed in to change notification settings - Fork 63
feat(utils): add utils package #268
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
Open
cryzzchen
wants to merge
7
commits into
master
Choose a base branch
from
feat/utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f94bce
fix: add return value in tools
44c6bbd
chore: delete utils in main package & add env dependencies
36af0e1
fix: remove uni dep in main
ad59c17
chore: up migrate
02d4489
chore: up migrate
b0a13dd
chore: remove useless
d09cb05
feat: utils unuse del
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
const glob = require('glob'); | ||
const fs = require('fs-extra'); | ||
const babelParser = require('@babel/parser'); | ||
const traverse = require('@babel/traverse').default; | ||
const types = require('@babel/types'); | ||
const generate = require('@babel/generator').default; | ||
|
||
|
||
function parse(file) { | ||
const source = fs.readFileSync(file, { encoding: 'utf8' }); | ||
let ast; | ||
try { | ||
ast = babelParser.parse(source, { | ||
sourceType: 'module', | ||
plugins: [ | ||
'typescript', | ||
'classProperties', | ||
'objectRestSpread', | ||
'optionalCatchBinding', | ||
'dynamicImport', | ||
'decorators-legacy', | ||
'asyncGenerators', | ||
'exportDefaultFrom', | ||
'exportNamespaceFrom', | ||
'optionalCatchBinding', | ||
'throwExpressions', | ||
'optionalChaining', | ||
'nullishCoalescingOperator', | ||
], | ||
}); | ||
} catch (e) { | ||
console.error('error', file); | ||
return; | ||
} | ||
|
||
if (!ast) { | ||
console.error('error', file); | ||
return; | ||
} | ||
let changed = false; | ||
traverse(ast, { | ||
Program(path) { | ||
const { body } = path.node; | ||
let specifiers = []; | ||
const indexs = []; | ||
body.forEach((n, index) => { | ||
if (types.isImportDeclaration(n) && n.source.value.startsWith('@utils')) { | ||
specifiers = specifiers.concat(n.specifiers); | ||
indexs.push(index); | ||
} | ||
}); | ||
|
||
if (indexs.length > 0) { | ||
indexs.reverse().forEach(i => body.splice(i, 1)); | ||
changed = true; | ||
body.unshift(types.importDeclaration(specifiers, types.stringLiteral('@uni/utils'))); | ||
} | ||
}, | ||
}); | ||
if (changed) { | ||
fs.writeFileSync(file, generate(ast, {}, source).code); | ||
} | ||
} | ||
|
||
glob('./src/packages/**/src/**/*.ts', function(err, files) { | ||
files.forEach(file => { | ||
parse(file); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 +1,96 @@ | ||
import { CONTAINER_NAME } from '@uni/utils'; | ||
import { CanvasContext, Options } from '../types'; | ||
import { normalize } from '../common'; | ||
import { CONTAINER_NAME } from '@utils/constant'; | ||
import { isLessThanVersion } from '@utils/tools'; | ||
|
||
const createContext = normalize((canvasOptions: Options): Promise<CanvasContext> => { | ||
const { canvasId, type = '2d' } = canvasOptions; | ||
const { | ||
canvasId, | ||
type = '2d', | ||
} = canvasOptions; | ||
|
||
if (isLessThanVersion(my.SDKVersion, '2.7.0')) { | ||
return new Promise((resolve) => { | ||
const canvasContext = my.createCanvasContext(canvasId); | ||
const _clearRect = canvasContext.clearRect; | ||
|
||
canvasContext.clearRect = (...args) => { | ||
_clearRect.apply(canvasContext, args); | ||
|
||
canvasContext.draw(true); | ||
}; | ||
|
||
const _fill = canvasContext.fill; | ||
|
||
canvasContext.fill = (...args) => { | ||
_fill.apply(canvasContext, args); | ||
|
||
canvasContext.draw(true); | ||
}; | ||
|
||
const _fillRect = canvasContext.fillRect; | ||
|
||
canvasContext.fillRect = (...args) => { | ||
_fillRect.apply(canvasContext, args); | ||
|
||
canvasContext.draw(true); | ||
}; | ||
|
||
const _fillText = canvasContext.fillText; | ||
|
||
canvasContext.fillText = (...args) => { | ||
_fillText.apply(canvasContext, args); | ||
|
||
canvasContext.draw(true); | ||
}; | ||
|
||
const _stroke = canvasContext.stroke; | ||
|
||
canvasContext.stroke = (...args) => { | ||
_stroke.apply(canvasContext, args); | ||
|
||
canvasContext.draw(true); | ||
}; | ||
|
||
const _strokeRect = canvasContext.strokeRect; | ||
|
||
canvasContext.strokeRect = (...args) => { | ||
_strokeRect.apply(canvasContext, args); | ||
|
||
canvasContext.draw(true); | ||
}; | ||
|
||
const _strokeText = canvasContext.strokeText; | ||
|
||
canvasContext.strokeText = (...args) => { | ||
_strokeText.apply(canvasContext, args); | ||
|
||
canvasContext.draw(true); | ||
}; | ||
|
||
Object.defineProperty(canvasContext, 'fillStyle', { | ||
get() { | ||
return canvasContext.setFillStyle; | ||
}, | ||
|
||
set(value) { | ||
canvasContext.setFillStyle(value); | ||
}, | ||
|
||
}); | ||
resolve(canvasContext); | ||
}); | ||
} else { | ||
// 仅 2.7.0+ 基础库版本支持 | ||
return new Promise((resolve, reject) => { | ||
const query = my.createSelectorQuery(); | ||
query | ||
.select(`#${canvasId}`) | ||
.node() | ||
.exec((res) => { | ||
if (!res[0] || !res[0].node) reject(new Error('The canvas node may not exist.')); | ||
const canvasNode: HTMLCanvasElement = res[0].node; | ||
const canvasContext: CanvasContext = canvasNode.getContext(type); | ||
|
||
resolve(canvasContext); | ||
}); | ||
query.select(`#${canvasId}`).node().exec((res) => { | ||
if (!res[0] || !res[0].node) reject(new Error('The canvas node may not exist.')); | ||
const canvasNode: HTMLCanvasElement = res[0].node; | ||
const canvasContext: CanvasContext = canvasNode.getContext(type); | ||
resolve(canvasContext); | ||
}); | ||
}); | ||
} | ||
}, CONTAINER_NAME.ALIPAY); | ||
|
||
export default createContext; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.