printButton.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { AbstractViewerNavbarButton } from "../viewerTemplatePlugin";
  2. import { DefaultViewer } from "../../viewer/defaultViewer";
  3. import { EventCallback } from "../templateManager";
  4. import { Tools } from "babylonjs";
  5. export class PrintButtonPlugin extends AbstractViewerNavbarButton {
  6. private _currentModelUrl: string;
  7. constructor(private _viewer: DefaultViewer) {
  8. super("print", "print-button", PrintButtonPlugin.HtmlTemplate);
  9. this._viewer.onModelLoadedObservable.add((model) => {
  10. this._currentModelUrl = "";
  11. if (model.configuration.url) {
  12. let filename = Tools.GetFilename(model.configuration.url) || model.configuration.url;
  13. let baseUrl = model.configuration.root || Tools.GetFolderPath(model.configuration.url);
  14. //gltf, obj, stl
  15. let extension = model.configuration.loader || filename.split(".").pop() || "";
  16. let printable = false;
  17. // not using .some sue to IE11
  18. ["gltf", "glb", "obj", "stl"].forEach(ext => {
  19. if (extension.indexOf(ext) !== -1) {
  20. printable = true;
  21. }
  22. })
  23. if (printable) {
  24. this._currentModelUrl = baseUrl + filename;
  25. }
  26. }
  27. })
  28. }
  29. onEvent(event: EventCallback): void {
  30. if (this._currentModelUrl) {
  31. let printUrl = this._currentModelUrl.replace(/https?:\/\//, "com.microsoft.builder3d://");
  32. window.open(printUrl, "_self");
  33. }
  34. }
  35. protected static HtmlTemplate: string = `
  36. {{#unless hidePrint}}
  37. <style>
  38. /* Show only if it's a windows 10 printer */
  39. .print-icon.not-win-10 {
  40. display: none;
  41. }
  42. .print-icon:after {
  43. font-size: 16px;
  44. content: "\\E914";
  45. }
  46. </style>
  47. <button class="print-button ${window.navigator.userAgent.indexOf("Windows NT 10.0") === -1 ? "no-win-10" : ""}" title="{{text.printButton}}">
  48. <span class="icon print-icon"></span>
  49. </button>
  50. {{/unless}}
  51. `;
  52. }