UNPKG

ec-print-vue

Version:
105 lines (98 loc) 3.97 kB
/*! * ec-print-vue v1.0.0 * (c) 2023 Gavin Hao * @license Apache License 2.0 */ import EcPrinter, { Providers } from 'ec-print'; import { ref, inject, unref, reactive } from 'vue'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(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 printerKey = Symbol('ec-printer'); var printerStatusKey = Symbol('ec-printer-status'); var allProviders = Object.keys(Providers).map(function (p) { return { key: p.toLowerCase(), providerFactory: Providers[p], options: {}, }; }); var _defaultOptions = { providers: allProviders, socketOption: { autoReconnect: true, }, }; var printerStatusRef = ref({}); function createPrinter(options) { var printer = new EcPrinter(); var _opts = _defaultOptions; if (options) { _opts.providers = options.providers && options.providers.length ? options.providers : _defaultOptions.providers; _opts.socketOption = __assign(__assign({}, _opts.socketOption), (options.socketOption || {})); } _opts.providers.forEach(function (provider) { var _a, _b; var agent; // typeof class extends PrintProvider if ((_a = provider.providerFactory.prototype) === null || _a === void 0 ? void 0 : _a.print) { var construct = provider.providerFactory; agent = new construct({ options: __assign(__assign({}, provider.socketOption), _opts.socketOption), }); } else { agent = provider.providerFactory(); } var key = (_b = provider.key) !== null && _b !== void 0 ? _b : agent.providerKey; agent.addEventListener('open', function (e) { printerStatusRef.value[key] = true; }); agent.addEventListener('close', function (e) { printerStatusRef.value[key] = false; }); printer.registerOne(key, agent); }); function install(app) { app.config.globalProperties['$printer'] = printer; Object.defineProperty(app.config.globalProperties, '$printer_connect_status', { enumerable: true, get: function () { return unref(printerStatusRef); }, }); app.provide(printerKey, printer); app.provide(printerStatusKey, reactive(printerStatusRef)); var unmountApp = app.unmount; app.unmount = function () { printer === null || printer === void 0 ? void 0 : printer.disconnect(); unmountApp(); }; } return { instance: printer, install: install }; } function usePrinter() { return inject(printerKey); } function usePrinterConnectStatus() { return inject(printerStatusKey); } export { createPrinter, usePrinter, usePrinterConnectStatus }; //# sourceMappingURL=ec-print-vue.esm.js.map