UNPKG

@shopify/react-native-skia

Version:

High-performance React Native Graphics using Skia

128 lines (124 loc) 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SkiaJSDomView = void 0; var _react = _interopRequireDefault(require("react")); var _Platform = require("../Platform"); var _SkiaPictureViewNativeComponent = _interopRequireDefault(require("../specs/SkiaPictureViewNativeComponent")); var _types = require("../dom/types"); var _api = require("./api"); var _SkiaViewNativeId = require("./SkiaViewNativeId"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } const NativeSkiaPictureView = _Platform.Platform.OS !== "web" ? _SkiaPictureViewNativeComponent.default : // eslint-disable-next-line @typescript-eslint/no-explicit-any null; class SkiaJSDomView extends _react.default.Component { constructor(props) { super(props); _defineProperty(this, "_nativeId", void 0); this._nativeId = _SkiaViewNativeId.SkiaViewNativeId.current++; const { root, onTouch, onSize } = props; if (root) { assertSkiaViewApi(); _api.SkiaViewApi.setJsiProperty(this._nativeId, "root", root); } if (onTouch) { assertSkiaViewApi(); _api.SkiaViewApi.setJsiProperty(this._nativeId, "onTouch", onTouch); } if (onSize) { assertSkiaViewApi(); _api.SkiaViewApi.setJsiProperty(this._nativeId, "onSize", onSize); } } get nativeId() { return this._nativeId; } componentDidUpdate(prevProps) { const { root, onTouch, onSize } = this.props; if (root !== prevProps.root && root !== undefined) { assertSkiaViewApi(); this.draw(); } if (onTouch !== prevProps.onTouch) { assertSkiaViewApi(); _api.SkiaViewApi.setJsiProperty(this._nativeId, "onTouch", onTouch); } if (onSize !== prevProps.onSize) { assertSkiaViewApi(); _api.SkiaViewApi.setJsiProperty(this._nativeId, "onSize", onSize); } } /** * Creates a snapshot from the canvas in the surface * @param rect Rect to use as bounds. Optional. * @returns An Image object. */ makeImageSnapshot(rect) { assertSkiaViewApi(); return _api.SkiaViewApi.makeImageSnapshot(this._nativeId, rect); } /** * Sends a redraw request to the native SkiaView. */ redraw() { assertSkiaViewApi(); this.draw(); //SkiaViewApi.requestRedraw(this._nativeId); } draw() { const { root, Skia } = this.props; if (root !== undefined) { assertSkiaViewApi(); const rec = Skia.PictureRecorder(); const canvas = rec.beginRecording(); const ctx = new _types.JsiDrawingContext(Skia, canvas); root.render(ctx); const picture = rec.finishRecordingAsPicture(); _api.SkiaViewApi.setJsiProperty(this._nativeId, "picture", picture); } } /** * Clear up the dom node when unmounting to release resources. */ componentWillUnmount() { assertSkiaViewApi(); _api.SkiaViewApi.setJsiProperty(this._nativeId, "picture", null); } render() { const { mode, debug = false, ...viewProps } = this.props; return /*#__PURE__*/_react.default.createElement(NativeSkiaPictureView, _extends({ collapsable: false, nativeID: `${this._nativeId}`, mode: mode, debug: debug }, viewProps)); } } exports.SkiaJSDomView = SkiaJSDomView; const assertSkiaViewApi = () => { if (_api.SkiaViewApi === null || _api.SkiaViewApi.setJsiProperty === null || _api.SkiaViewApi.callJsiMethod === null || _api.SkiaViewApi.requestRedraw === null || _api.SkiaViewApi.makeImageSnapshot === null) { throw Error("Skia View Api was not found."); } }; //# sourceMappingURL=SkiaJSDomView.js.map