Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 1, 2019 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/7972e4f0329870c0aee6f807b488f408 to your computer and use it in GitHub Desktop.
Save parzibyte/7972e4f0329870c0aee6f807b488f408 to your computer and use it in GitHub Desktop.
/**
* Una clase para interactuar con el plugin
*
* @author parzibyte
* @see https://parzibyte.me/blog
*/
const C = {
AccionWrite: "write",
AccionAlign: "align",
AccionFontSize: "fontsize",
AccionFont: "font",
AccionEmphasize: "emphasize",
AccionFeed: "feed",
AlineacionCentro: "center",
AlineacionDerecha: "right",
AlineacionIzquierda: "left",
FuenteA: "A",
FuenteB: "B"
};
class OperacionTicket {
constructor(accion, datos) {
this.accion = accion + "";
this.datos = datos + "";
}
}
class Impresora {
constructor(ruta) {
if (!ruta) ruta = "http://localhost:8000";
this.ruta = ruta;
this.operaciones = [];
}
setFontSize(a, b) {
this.operaciones.push(new OperacionTicket(C.AccionFontSize, `${a},${b}`));
}
setFont(font) {
if (font !== C.FuenteA && font !== C.FuenteB) throw Error("Fuente inválida");
this.operaciones.push(new OperacionTicket(C.AccionFont, font));
}
setEmphasize(val) {
if (isNaN(parseInt(val)) || parseInt(val) < 0) throw Error("Valor inválido");
this.operaciones.push(new OperacionTicket(C.AccionEmphasize, val));
}
setAlign(align) {
if (align !== C.AlineacionCentro && align !== C.AlineacionDerecha && align !== C.AlineacionIzquierda) {
throw Error(`Alineación ${align} inválida`);
}
this.operaciones.push(new OperacionTicket(C.AccionAlign, align));
}
write(text) {
this.operaciones.push(new OperacionTicket(C.AccionWrite, text));
}
feed(n) {
if (!parseInt(n) || parseInt(n) < 0) {
throw Error("Valor para feed inválido");
}
this.operaciones.push(new OperacionTicket(C.AccionFeed, n));
}
end() {
return fetch(this.ruta + "/imprimir", {
method: "POST",
body: JSON.stringify(this.operaciones),
})
.then(r => r.json());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment