提交 caa87deb 编写于 作者: D DCloud_LXH

chore: eventBus、canvas、showActionSheet

上级 38e534db
......@@ -7,7 +7,7 @@ export const ShowActionSheetProtocol: ApiProtocol<API_TYPE_SHOW_ACTION_SHEET> =
required: true,
},
itemColor: String,
// popover: Object,
popover: Object,
}
export const ShowActionSheetOptions: ApiOptions<API_TYPE_SHOW_ACTION_SHEET> = {
formatArgs: {
......
......@@ -13,94 +13,46 @@ import {
EmitProtocol,
OnceProtocol,
} from '../../protocols/base/eventBus'
import { Emitter } from '@dcloudio/uni-core'
type EventName = string | number | symbol
type EventCallback = (...args: any) => any
type EventStopHandler = () => void
class Emitter {
private eventMap: Map<EventName, Array<EventCallback>>
constructor() {
this.eventMap = new Map()
}
on = (name: EventName, callback: EventCallback): EventStopHandler => {
if (!this.eventMap.has(name)) {
this.eventMap.set(name, [])
}
this.eventMap.get(name)!.push(callback)
return () => this.off(name, callback)
}
once = (name: EventName, callback: EventCallback): EventStopHandler => {
const listener = (...args: any[]) => {
this.off(name, listener)
callback(...args)
}
this.on(name, listener)
return () => this.off(name, listener)
}
emit = (name: EventName, ...args: any[]): void => {
const cbs = this.eventMap.get(name)
if (cbs instanceof Array) {
cbs.forEach((cb) => {
typeof cb === 'function' && cb(...args)
})
}
}
off = (names?: EventName | EventName[], callback?: EventCallback): void => {
if (!names) {
this.eventMap.clear()
return
}
if (!(names instanceof Array)) {
names = [names]
}
if (typeof callback === 'function') {
names.forEach((name) => {
if (this.eventMap.has(name)) {
this.eventMap.set(
name,
this.eventMap.get(name)!.filter((cb) => cb !== callback)
)
}
})
} else {
names.forEach((name) => {
this.eventMap.delete(name)
})
}
}
}
const emitter = new Emitter()
export const $on = defineSyncApi<API_TYPE_ON>(
API_ON,
(type, callback): EventStopHandler => emitter.on(type, callback),
(name, callback): EventStopHandler => {
emitter.on(name, callback)
return () => emitter.off(name, callback)
},
OnProtocol
)
export const $once = defineSyncApi<API_TYPE_ONCE>(
API_ONCE,
(type, callback): EventStopHandler => emitter.once(type, callback),
(name, callback): EventStopHandler => {
emitter.once(name, callback)
return () => emitter.off(name, callback)
},
OnceProtocol
)
export const $off = defineSyncApi<API_TYPE_OFF>(
API_OFF,
(type, callback) => {
emitter.off(type, callback)
(name, callback) => {
if (!name) {
emitter.e = {}
return
}
if (!Array.isArray(name)) name = [name]
name.forEach((n) => emitter.off(n, callback))
},
OffProtocol
)
export const $emit = defineSyncApi<API_TYPE_EMIT>(
API_EMIT,
emitter.emit,
(name, ...args: any[]) => {
emitter.emit(name, ...args)
},
EmitProtocol
)
......@@ -795,20 +795,21 @@ export class CanvasContext implements UniApp.CanvasContext {
;[...methods1, ...methods2].forEach(function (method) {
function get(method: string) {
// @ts-ignore
let _this = this as CanvasContext
switch (method) {
case 'fill':
case 'stroke':
return function () {
_this.actions.push({
// @ts-ignore
this.actions.push({
method: method + 'Path',
data: [..._this.path],
// @ts-ignore
data: [...this.path],
})
}
case 'fillRect':
return function (x: number, y: number, width: number, height: number) {
_this.actions.push({
// @ts-ignore
this.actions.push({
method: 'fillPath',
data: [
{
......@@ -820,7 +821,8 @@ export class CanvasContext implements UniApp.CanvasContext {
}
case 'strokeRect':
return function (x: number, y: number, width: number, height: number) {
_this.actions.push({
// @ts-ignore
this.actions.push({
method: 'strokePath',
data: [
{
......@@ -837,7 +839,8 @@ export class CanvasContext implements UniApp.CanvasContext {
if (typeof maxWidth === 'number') {
data.push(maxWidth)
}
_this.actions.push({
// @ts-ignore
this.actions.push({
method,
data,
})
......@@ -864,7 +867,7 @@ export class CanvasContext implements UniApp.CanvasContext {
dWidth = undefined
dHeight = undefined
}
let data: ActionsItemData
var data
function isNumber(e: any) {
return typeof e === 'number'
......@@ -888,14 +891,16 @@ export class CanvasContext implements UniApp.CanvasContext {
: isNumber(sWidth) && isNumber(sHeight)
? [imageResource, sx, sy, sWidth, sHeight]
: [imageResource, sx, sy]
_this.actions.push({
// @ts-ignore
this.actions.push({
method,
data,
})
}
default:
return function (...data: ActionsItemData) {
_this.actions.push({
return function (...data: any) {
// @ts-ignore
this.actions.push({
method,
data,
})
......@@ -906,32 +911,29 @@ export class CanvasContext implements UniApp.CanvasContext {
})
methods3.forEach(function (method) {
function get(method: string) {
// @ts-ignore
let _this = this as CanvasContext
switch (method) {
case 'setFillStyle':
case 'setStrokeStyle':
return function (color: string | Data) {
if (typeof color !== 'object') {
_this.actions.push({
// @ts-ignore
this.actions.push({
method,
data: ['normal', checkColor(color)],
})
} else {
_this.actions.push({
// @ts-ignore
this.actions.push({
method,
data: [
color.type as ActionsItemType,
color.data as ActionsItemType,
color.colorStop as ActionsItemType,
],
data: [color.type, color.data, color.colorStop],
})
}
}
case 'setGlobalAlpha':
return function (alpha: number) {
alpha = Math.floor(255 * parseFloat(alpha as unknown as string))
_this.actions.push({
// @ts-ignore
this.actions.push({
method,
data: [alpha],
})
......@@ -943,41 +945,52 @@ methods3.forEach(function (method) {
blur: number,
color: string
) {
let _color = checkColor(color)
_this.actions.push({
color = checkColor(color) as any
// @ts-ignore
this.actions.push({
method,
data: [offsetX, offsetY, blur, color],
})
_this.state.shadowBlur = blur
_this.state.shadowColor = _color
_this.state.shadowOffsetX = offsetX
_this.state.shadowOffsetY = offsetY
// @ts-ignore
this.state.shadowBlur = blur
// @ts-ignore
this.state.shadowColor = color
// @ts-ignore
this.state.shadowOffsetX = offsetX
// @ts-ignore
this.state.shadowOffsetY = offsetY
}
case 'setLineDash':
return function (pattern: Array<number> | undefined, offset: number) {
pattern = pattern || [0, 0]
offset = offset || 0
_this.actions.push({
// @ts-ignore
this.actions.push({
method,
data: [pattern, offset],
})
_this.state.lineDash = pattern
// @ts-ignore
this.state.lineDash = pattern
}
case 'setFontSize':
return function (fontSize: number) {
_this.state.font = _this.state.font.replace(
// @ts-ignore
this.state.font = this.state.font.replace(
/\d+\.?\d*px/,
fontSize + 'px'
)
_this.state.fontSize = fontSize
_this.actions.push({
// @ts-ignore
this.state.fontSize = fontSize
// @ts-ignore
this.actions.push({
method,
data: [fontSize],
})
}
default:
return function (...data: ActionsItemData) {
_this.actions.push({
return function (...data: any) {
// @ts-ignore
this.actions.push({
method,
data,
})
......
function E() {
interface E {
e: Data
on: (name: EventName, callback: EventCallback, ctx?: any) => this
once: (name: EventName, callback: EventCallback, ctx?: any) => this
emit: (name: EventName, ...args: any[]) => this
off: (name: EventName, callback?: EventCallback) => this
}
const E = function () {
// Keep this empty so it's easier to inherit from
// (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
}
} as unknown as { new (): E }
// export type EventName = string | number | symbol
export type EventName = string
export type EventCallback = Function
E.prototype = {
on: function (name, callback, ctx) {
on: function (name: EventName, callback: EventCallback, ctx?: any) {
var e = this.e || (this.e = {})
;(e[name] || (e[name] = [])).push({
......@@ -15,7 +27,7 @@ E.prototype = {
return this
},
once: function (name, callback, ctx) {
once: function (name: EventName, callback: EventCallback, ctx?: any) {
var self = this
function listener() {
self.off(name, listener)
......@@ -26,7 +38,7 @@ E.prototype = {
return this.on(name, listener, ctx)
},
emit: function (name) {
emit: function (name: EventName) {
var data = [].slice.call(arguments, 1)
var evtArr = ((this.e || (this.e = {}))[name] || []).slice()
var i = 0
......@@ -39,7 +51,7 @@ E.prototype = {
return this
},
off: function (name, callback) {
off: function (name: EventName, callback?: EventCallback) {
var e = this.e || (this.e = {})
var evts = e[name]
var liveEvents = []
......
......@@ -5,3 +5,4 @@ export * from './page'
export * from './scroll'
export * from './getRealRoute'
export * from './callbacks'
export { default as Emitter } from './TinyEmitter'
......@@ -115,8 +115,8 @@ const initI18nVideoMsgsOnce = /* @__PURE__ */ uniShared.once(() => {
i18n.add(uniI18n.LOCALE_ZH_HANT, normalizeMessages(name, {danmu: "\u5F48\u5E55", volume: "\u97F3\u91CF"}));
}
});
function E() {
}
const E = function() {
};
E.prototype = {
on: function(name, callback2, ctx) {
var e2 = this.e || (this.e = {});
......
......@@ -188,8 +188,8 @@ const initI18nVideoMsgsOnce = /* @__PURE__ */ once(() => {
i18n.add(LOCALE_ZH_HANT, normalizeMessages(name, {danmu: "\u5F48\u5E55", volume: "\u97F3\u91CF"}));
}
});
function E() {
}
const E = function() {
};
E.prototype = {
on: function(name, callback2, ctx) {
var e2 = this.e || (this.e = {});
......@@ -2010,61 +2010,27 @@ const EmitProtocol = [
required: true
}
];
class Emitter {
constructor() {
this.on = (name, callback2) => {
if (!this.eventMap.has(name)) {
this.eventMap.set(name, []);
}
this.eventMap.get(name).push(callback2);
return () => this.off(name, callback2);
};
this.once = (name, callback2) => {
const listener2 = (...args) => {
this.off(name, listener2);
callback2(...args);
};
this.on(name, listener2);
return () => this.off(name, listener2);
};
this.emit = (name, ...args) => {
const cbs = this.eventMap.get(name);
if (cbs instanceof Array) {
cbs.forEach((cb) => {
typeof cb === "function" && cb(...args);
});
}
};
this.off = (names, callback2) => {
if (!names) {
this.eventMap.clear();
return;
}
if (!(names instanceof Array)) {
names = [names];
}
if (typeof callback2 === "function") {
names.forEach((name) => {
if (this.eventMap.has(name)) {
this.eventMap.set(name, this.eventMap.get(name).filter((cb) => cb !== callback2));
}
});
} else {
names.forEach((name) => {
this.eventMap.delete(name);
});
}
};
this.eventMap = new Map();
const emitter = new E();
const $on = /* @__PURE__ */ defineSyncApi(API_ON, (name, callback2) => {
emitter.on(name, callback2);
return () => emitter.off(name, callback2);
}, OnProtocol);
const $once = /* @__PURE__ */ defineSyncApi(API_ONCE, (name, callback2) => {
emitter.once(name, callback2);
return () => emitter.off(name, callback2);
}, OnceProtocol);
const $off = /* @__PURE__ */ defineSyncApi(API_OFF, (name, callback2) => {
if (!name) {
emitter.e = {};
return;
}
}
const emitter = new Emitter();
const $on = /* @__PURE__ */ defineSyncApi(API_ON, (type, callback2) => emitter.on(type, callback2), OnProtocol);
const $once = /* @__PURE__ */ defineSyncApi(API_ONCE, (type, callback2) => emitter.once(type, callback2), OnceProtocol);
const $off = /* @__PURE__ */ defineSyncApi(API_OFF, (type, callback2) => {
emitter.off(type, callback2);
if (!Array.isArray(name))
name = [name];
name.forEach((n) => emitter.off(n, callback2));
}, OffProtocol);
const $emit = /* @__PURE__ */ defineSyncApi(API_EMIT, emitter.emit, EmitProtocol);
const $emit = /* @__PURE__ */ defineSyncApi(API_EMIT, (name, ...args) => {
emitter.emit(name, ...args);
}, EmitProtocol);
const validator = [
{
name: "id",
......@@ -2853,19 +2819,18 @@ class CanvasContext {
}
[...methods1, ...methods2].forEach(function(method) {
function get(method2) {
let _this = this;
switch (method2) {
case "fill":
case "stroke":
return function() {
_this.actions.push({
this.actions.push({
method: method2 + "Path",
data: [..._this.path]
data: [...this.path]
});
};
case "fillRect":
return function(x, y, width, height) {
_this.actions.push({
this.actions.push({
method: "fillPath",
data: [
{
......@@ -2877,7 +2842,7 @@ class CanvasContext {
};
case "strokeRect":
return function(x, y, width, height) {
_this.actions.push({
this.actions.push({
method: "strokePath",
data: [
{
......@@ -2894,7 +2859,7 @@ class CanvasContext {
if (typeof maxWidth === "number") {
data.push(maxWidth);
}
_this.actions.push({
this.actions.push({
method: method2,
data
});
......@@ -2911,7 +2876,7 @@ class CanvasContext {
dWidth = void 0;
dHeight = void 0;
}
let data;
var data;
function isNumber(e2) {
return typeof e2 === "number";
}
......@@ -2926,14 +2891,14 @@ class CanvasContext {
dWidth,
dHeight
] : isNumber(sWidth) && isNumber(sHeight) ? [imageResource, sx, sy, sWidth, sHeight] : [imageResource, sx, sy];
_this.actions.push({
this.actions.push({
method: method2,
data
});
};
default:
return function(...data) {
_this.actions.push({
this.actions.push({
method: method2,
data
});
......@@ -2944,69 +2909,64 @@ class CanvasContext {
});
methods3.forEach(function(method) {
function get(method2) {
let _this = this;
switch (method2) {
case "setFillStyle":
case "setStrokeStyle":
return function(color) {
if (typeof color !== "object") {
_this.actions.push({
this.actions.push({
method: method2,
data: ["normal", checkColor(color)]
});
} else {
_this.actions.push({
this.actions.push({
method: method2,
data: [
color.type,
color.data,
color.colorStop
]
data: [color.type, color.data, color.colorStop]
});
}
};
case "setGlobalAlpha":
return function(alpha) {
alpha = Math.floor(255 * parseFloat(alpha));
_this.actions.push({
this.actions.push({
method: method2,
data: [alpha]
});
};
case "setShadow":
return function(offsetX, offsetY, blur, color) {
let _color = checkColor(color);
_this.actions.push({
color = checkColor(color);
this.actions.push({
method: method2,
data: [offsetX, offsetY, blur, color]
});
_this.state.shadowBlur = blur;
_this.state.shadowColor = _color;
_this.state.shadowOffsetX = offsetX;
_this.state.shadowOffsetY = offsetY;
this.state.shadowBlur = blur;
this.state.shadowColor = color;
this.state.shadowOffsetX = offsetX;
this.state.shadowOffsetY = offsetY;
};
case "setLineDash":
return function(pattern, offset) {
pattern = pattern || [0, 0];
offset = offset || 0;
_this.actions.push({
this.actions.push({
method: method2,
data: [pattern, offset]
});
_this.state.lineDash = pattern;
this.state.lineDash = pattern;
};
case "setFontSize":
return function(fontSize) {
_this.state.font = _this.state.font.replace(/\d+\.?\d*px/, fontSize + "px");
_this.state.fontSize = fontSize;
_this.actions.push({
this.state.font = this.state.font.replace(/\d+\.?\d*px/, fontSize + "px");
this.state.fontSize = fontSize;
this.actions.push({
method: method2,
data: [fontSize]
});
};
default:
return function(...data) {
_this.actions.push({
this.actions.push({
method: method2,
data
});
......@@ -3846,7 +3806,8 @@ const ShowActionSheetProtocol = {
type: Array,
required: true
},
itemColor: String
itemColor: String,
popover: Object
};
const ShowActionSheetOptions = {
formatArgs: {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册