react-native-canvas
Version:
A Canvas component for React Native
172 lines (171 loc) • 6.3 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.webviewEvents = exports.webviewProperties = exports.webviewMethods = exports.webviewConstructor = exports.webviewTarget = exports.constructors = exports.WEBVIEW_TARGET = void 0;
exports.WEBVIEW_TARGET = "@@WEBVIEW_TARGET";
/**
* @mutable
*/
exports.constructors = {};
var webviewTarget = function (targetName) { return function (target) {
target.prototype[exports.WEBVIEW_TARGET] = targetName;
}; };
exports.webviewTarget = webviewTarget;
var ID = function () { return Math.random().toString(32).slice(2); };
/**
* These are where objects need other objects as an argument.
* Because when the data is sent as JSON it removes the class.
*
* One example being ImageData which requires the Uint8ClampedArray
* object as the first parameter.
*/
var SPECIAL_CONSTRUCTOR = {
ImageData: {
className: "Uint8ClampedArray",
paramNum: 0,
},
};
var webviewConstructor = function (constructorName) { return function (target) {
exports.constructors[constructorName] = target;
target.constructLocally = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
// Pass noOnConstruction
return new (target.bind.apply(target, __spreadArray(__spreadArray([void 0], args, false), [true], false)))();
};
/**
* Arguments should be identical to the arguments passed to the constructor
* just without the canvas instance
*/
target.prototype.onConstruction = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (SPECIAL_CONSTRUCTOR[constructorName] !== undefined) {
var _a = SPECIAL_CONSTRUCTOR[constructorName], className = _a.className, paramNum = _a.paramNum;
args[paramNum] = { className: className, classArgs: [args[paramNum]] };
}
this[exports.WEBVIEW_TARGET] = ID();
this.postMessage({
type: "construct",
payload: {
constructor: constructorName,
id: this[exports.WEBVIEW_TARGET],
args: args,
},
});
};
target.prototype.toJSON = function () {
return { __ref__: this[exports.WEBVIEW_TARGET] };
};
}; };
exports.webviewConstructor = webviewConstructor;
var webviewMethods = function (methods) { return function (target) {
var _loop_1 = function (method) {
target.prototype[method] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return this.postMessage({
type: "exec",
payload: {
target: this[exports.WEBVIEW_TARGET],
method: method,
args: args,
},
});
};
};
for (var _i = 0, methods_1 = methods; _i < methods_1.length; _i++) {
var method = methods_1[_i];
_loop_1(method);
}
}; };
exports.webviewMethods = webviewMethods;
var webviewProperties = function (properties) { return function (target) {
var _loop_2 = function (key) {
var initialValue = properties[key];
var privateKey = "__".concat(key, "__");
target.prototype[privateKey] = initialValue;
Object.defineProperty(target.prototype, key, {
get: function () {
return this[privateKey];
},
set: function (value) {
this.postMessage({
type: "set",
payload: {
target: this[exports.WEBVIEW_TARGET],
key: key,
value: value,
},
});
if (this.forceUpdate) {
this.forceUpdate();
}
return (this[privateKey] = value);
},
});
};
for (var _i = 0, _a = Object.keys(properties); _i < _a.length; _i++) {
var key = _a[_i];
_loop_2(key);
}
}; };
exports.webviewProperties = webviewProperties;
var webviewEvents = function (types) { return function (target) {
var onConstruction = target.prototype.onConstruction;
target.prototype.onConstruction = function () {
if (onConstruction) {
onConstruction.call(this);
}
this.postMessage({
type: "listen",
payload: {
types: types,
target: this[exports.WEBVIEW_TARGET],
},
});
};
target.prototype.addEventListener = function (type, callback) {
var _this = this;
this.addMessageListener(function (message) {
if (message &&
message.type === "event" &&
message.payload.target[exports.WEBVIEW_TARGET] === _this[exports.WEBVIEW_TARGET] &&
message.payload.type === type) {
for (var key in message.payload.target) {
var value = message.payload.target[key];
if (key in _this && _this[key] !== value) {
_this[key] = value;
}
}
callback(__assign(__assign({}, message.payload), { target: _this }));
}
});
};
}; };
exports.webviewEvents = webviewEvents;