Skip to content

Commit acb0969

Browse files
committed
https://github.com/haxtheweb/issues/issues/1146
1 parent 21570d7 commit acb0969

File tree

8 files changed

+133
-5
lines changed

8 files changed

+133
-5
lines changed

dist/wc-registry.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

elements/clean-two/clean-two.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,15 @@ class CleanTwo extends LTIResizingMixin(
702702
${MicroFrontendRegistry.has("@core/htmlToPdf")
703703
? this.PDFPageButton()
704704
: ``}
705+
${MicroFrontendRegistry.has("@haxcms/siteToHtml")
706+
? this.PrintSiteButton()
707+
: html`<replace-tag
708+
with="site-print-button"
709+
position="right"
710+
class="btn js-toolbar-action"
711+
import-method="view"
712+
part="print-btn"
713+
></replace-tag>`}
705714
${MicroFrontendRegistry.has("@haxcms/siteToHtml")
706715
? this.PrintBranchButton()
707716
: html`<replace-tag

elements/clean-two/src/clean-two.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,15 @@ class CleanTwo extends LTIResizingMixin(
702702
${MicroFrontendRegistry.has("@core/htmlToPdf")
703703
? this.PDFPageButton()
704704
: ``}
705+
${MicroFrontendRegistry.has("@haxcms/siteToHtml")
706+
? this.PrintSiteButton()
707+
: html`<replace-tag
708+
with="site-print-button"
709+
position="right"
710+
class="btn js-toolbar-action"
711+
import-method="view"
712+
part="print-btn"
713+
></replace-tag>`}
705714
${MicroFrontendRegistry.has("@haxcms/siteToHtml")
706715
? this.PrintBranchButton()
707716
: html`<replace-tag

elements/haxcms-elements/demo/wc-registry.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

elements/haxcms-elements/lib/core/utils/PrintBranchMixin.js

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const PrintBranchMixin = function (SuperClass) {
1515
super();
1616
enableServices(["haxcms"]);
1717
this.t.print = "Print";
18+
this.t.printSite = "Print site";
1819
this.t.printPage = "Print page";
1920
this.t.printingPleaseWait = "Printing, please wait..";
2021
this.__printBranchLoading = false;
@@ -63,14 +64,14 @@ export const PrintBranchMixin = function (SuperClass) {
6364
icon-position="top"
6465
label="${this.__printBranchLoading
6566
? this.t.printingPleaseWait
66-
: this.t.print}"
67+
: this.t.printPage}"
6768
id="print-branch-btn"
6869
>
6970
</simple-icon-button-lite>
7071
<simple-tooltip for="print-branch-btn" position="${position}">
7172
${this.__printBranchLoading
7273
? this.t.printingPleaseWait
73-
: this.t.print}
74+
: this.t.printPage}
7475
</simple-tooltip>
7576
</div>`
7677
: ``}
@@ -103,6 +104,107 @@ export const PrintBranchMixin = function (SuperClass) {
103104
link: base,
104105
magic: globalThis.__appCDN,
105106
base: base,
107+
format: 'json',
108+
};
109+
const response = await MicroFrontendRegistry.call(
110+
"@haxcms/siteToHtml",
111+
params,
112+
);
113+
if (response.status == 200 && response.data) {
114+
const link = globalThis.document.createElement("a");
115+
// click link to download file
116+
// @todo this downloads but claims to be corrupt.
117+
link.href = globalThis.URL.createObjectURL(
118+
b64toBlob(
119+
btoa(unescape(encodeURIComponent(response.data))),
120+
"text/html",
121+
),
122+
);
123+
/*link.download = `${toJS(store.activeTitle)}.html`;
124+
link.target = "_blank";
125+
this.appendChild(link);
126+
link.click();
127+
this.removeChild(link);*/
128+
// fallback in case the service fails
129+
globalThis.open(
130+
globalThis.URL.createObjectURL(
131+
b64toBlob(
132+
btoa(unescape(encodeURIComponent(response.data))),
133+
"text/html",
134+
),
135+
),
136+
"",
137+
"left=0,top=0,width=800,height=800,toolbar=0,scrollbars=0,status=0,noopener=1,noreferrer=1",
138+
);
139+
} else {
140+
// fallback in case the service fails
141+
globalThis.open(
142+
globalThis.location.href + "?format=print-page",
143+
"",
144+
"left=0,top=0,width=800,height=800,toolbar=0,scrollbars=0,status=0,noopener=1,noreferrer=1",
145+
);
146+
}
147+
this.__printBranchLoading = false;
148+
}
149+
// full site print button
150+
PrintSiteButton(position = "auto") {
151+
return html`
152+
${MicroFrontendRegistry.has("@haxcms/siteToHtml")
153+
? html` <div
154+
class="print-branch-btn"
155+
part="${this.editMode ? `edit-mode-active` : ``}"
156+
>
157+
<simple-icon-button-lite
158+
part="print-branch-btn"
159+
class="btn"
160+
icon="${this.__printBranchLoading
161+
? `hax:loading`
162+
: `icons:print`}"
163+
@click="${this.printWholeSite}"
164+
icon-position="top"
165+
label="${this.__printBranchLoading
166+
? this.t.printingPleaseWait
167+
: this.t.printSite}"
168+
id="print-branch-btn"
169+
>
170+
</simple-icon-button-lite>
171+
<simple-tooltip for="print-branch-btn" position="${position}">
172+
${this.__printBranchLoading
173+
? this.t.printingPleaseWait
174+
: this.t.printSite}
175+
</simple-tooltip>
176+
</div>`
177+
: ``}
178+
`;
179+
}
180+
/**
181+
* Download PDF, via microservice
182+
*/
183+
async printWholeSite(e) {
184+
this.__printBranchLoading = true;
185+
// base helps w/ calculating URLs in content
186+
var base = "";
187+
if (globalThis.document.querySelector("base")) {
188+
base = globalThis.document.querySelector("base").href;
189+
}
190+
const site = toJS(store.manifest);
191+
const params = {
192+
type: "site",
193+
site: {
194+
file: base + "site.json",
195+
id: site.id,
196+
title: site.title,
197+
author: site.author,
198+
description: site.description,
199+
license: site.license,
200+
metadata: site.metadata,
201+
items: site.items,
202+
},
203+
ancestor: null,
204+
link: base,
205+
magic: globalThis.__appCDN,
206+
base: base,
207+
format: 'json',
106208
};
107209
const response = await MicroFrontendRegistry.call(
108210
"@haxcms/siteToHtml",

elements/micro-frontend-registry/micro-frontend-registry.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export const MicroFrontendRegCapabilities = function (SuperClass) {
163163
callback = null,
164164
caller = null,
165165
urlStringAddon = "",
166+
rawResponse = false
166167
) {
167168
if (this.has(name)) {
168169
const item = this.get(name);
@@ -192,6 +193,9 @@ export const MicroFrontendRegCapabilities = function (SuperClass) {
192193
},
193194
)
194195
.then((d) => {
196+
if (rawResponse) {
197+
return d.text();
198+
}
195199
return d.ok ? d.json() : { status: d.status, data: null };
196200
})
197201
.catch((e, d) => {

elements/micro-frontend-registry/src/micro-frontend-registry.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export const MicroFrontendRegCapabilities = function (SuperClass) {
163163
callback = null,
164164
caller = null,
165165
urlStringAddon = "",
166+
rawResponse = false
166167
) {
167168
if (this.has(name)) {
168169
const item = this.get(name);
@@ -192,6 +193,9 @@ export const MicroFrontendRegCapabilities = function (SuperClass) {
192193
},
193194
)
194195
.then((d) => {
196+
if (rawResponse) {
197+
return d.text();
198+
}
195199
return d.ok ? d.json() : { status: d.status, data: null };
196200
})
197201
.catch((e, d) => {

wc-registry.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)