-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44c2542
commit 8138a32
Showing
42 changed files
with
2,025 additions
and
1,220 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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# istanbul-widget | ||
|
||
## 1.3.0 | ||
|
||
### Minor Changes | ||
|
||
- feat: support plugin | ||
|
||
## 1.2.0 | ||
|
||
### Minor Changes | ||
|
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 @@ | ||
export * from './dist/es/components/ui/index' |
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
import { Button } from './src/components/ui' | ||
import { IstanbulWidget } from './src/istanbul-widget' | ||
|
||
function MyPlugin() { | ||
return <Button size={'sm'}>this is my Plugin</Button> | ||
} | ||
|
||
// 自定义插件 | ||
|
||
const myPlugin = new IstanbulWidget.IstanbulWidgetReactPlugin('my_plugin', 'My Plugin', MyPlugin) | ||
|
||
myPlugin.event.on('init', () => { | ||
console.log('my plugin inited') | ||
}) | ||
|
||
const istanbulWidget = new IstanbulWidget({ | ||
defaultPosition: { | ||
x: -100, | ||
y: 100, | ||
}, | ||
plugin: { | ||
report: { | ||
onReport(coverage) { | ||
console.log('上报', coverage) | ||
throw new Error('上报失败') | ||
}, | ||
}, | ||
setting: { | ||
requireReporter: true, | ||
}, | ||
buttonGroup: [ | ||
{ | ||
text: '额外按钮 - 1', | ||
onClick() { | ||
console.log('1') | ||
}, | ||
}, | ||
{ | ||
text: '额外按钮 - 2', | ||
onClick() { | ||
console.log('2') | ||
}, | ||
}, | ||
], | ||
}, | ||
}) | ||
|
||
istanbulWidget.addPlugin(myPlugin) |
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,3 @@ | ||
declare module 'strip-dirs' { | ||
export default (path: string, length: number) => string | ||
} |
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,10 @@ | ||
export * from './alert-dialog' | ||
export * from './button' | ||
export * from './dialog' | ||
export * from './input' | ||
export * from './label' | ||
export * from './popover' | ||
export * from './switch' | ||
export * from './toast' | ||
export * from './toaster' | ||
export * from './use-toast' |
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,88 @@ | ||
import { useDebounceFn, useLatest, useSetState } from '@minko-fe/react-hook' | ||
import { createContainer } from 'context-state' | ||
import { useToast } from '@/components/ui' | ||
import { type IstanbulWidgetOptions, type PluginName, type PluginType, type ReportParams } from './options.interface' | ||
|
||
export type InitialWidgetProps = IstanbulWidgetOptions & { | ||
/** | ||
* 插件 | ||
*/ | ||
pluginList: { [id: string]: PluginType } | ||
} | ||
|
||
function useContext(initialValues: InitialWidgetProps) { | ||
/* ------------------- 上报 ------------------- */ | ||
type Action = { | ||
id: string | ||
fn: (params: ReportParams, ...args: any[]) => Promise<void> | void | ||
} | ||
const [reportActions, setReportActions] = useSetState<{ | ||
before: Action[] | ||
on: Action[] | ||
after: Action[] | ||
}>({ | ||
before: [], | ||
on: [], | ||
after: [], | ||
}) | ||
|
||
const { toast } = useToast() | ||
|
||
const { beforeReport, onReport, afterReport } = initialValues.plugin?.report || {} | ||
|
||
/** | ||
* 额外参数,上报触发时代入 | ||
*/ | ||
const [_params, setReportFnParams] = useSetState<{ | ||
[key in PluginName]?: any | ||
}>({}) | ||
|
||
const params = useLatest(_params) | ||
|
||
const reportFn = useDebounceFn( | ||
async (showToast: boolean = true) => { | ||
const latestParams = params.current | ||
try { | ||
// before report | ||
await Promise.all(reportActions.before.map(async (action) => await action.fn(latestParams))) | ||
await beforeReport?.(window.__coverage__, latestParams) | ||
|
||
await Promise.all(reportActions.on.map(async (action) => await action.fn(latestParams))) | ||
await onReport?.(window.__coverage__, latestParams) | ||
showToast && | ||
toast({ | ||
description: '上报成功', | ||
}) | ||
} catch (e) { | ||
showToast && | ||
toast({ | ||
description: '上报失败,请打开控制台查看原因', | ||
variant: 'destructive', | ||
}) | ||
console.error('[istanbul-widget]:', e) | ||
} finally { | ||
// after report | ||
await Promise.all(reportActions.after.map(async (action) => await action.fn(latestParams))) | ||
await afterReport?.(window.__coverage__, latestParams) | ||
} | ||
}, | ||
{ | ||
wait: 300, | ||
leading: true, | ||
trailing: false, | ||
}, | ||
) | ||
|
||
return { | ||
...initialValues, | ||
reportActions, | ||
setReportActions, | ||
reportFn, | ||
setReportFnParams, | ||
reportFnparams: params.current, | ||
} | ||
} | ||
|
||
const Context = createContainer(useContext) | ||
|
||
export default Context |
Oops, something went wrong.