Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/oss-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"decorator-transforms": "^2.3.0",
"dompurify": "^3.3.0",
"ember-modifier": "^3.2.7",
"ember-truth-helpers": "^4.0.3"
"ember-truth-helpers": "^4.0.3",
"jspdf": "^4.2.1",
"modern-screenshot": "^4.7.0"
},
"devDependencies": {
"@babel/core": "^7.25.2",
Expand Down Expand Up @@ -283,6 +285,7 @@
"./components/o-s-s/panel.js": "./dist/_app_/components/o-s-s/panel.js",
"./components/o-s-s/panel/row.js": "./dist/_app_/components/o-s-s/panel/row.js",
"./components/o-s-s/password-input.js": "./dist/_app_/components/o-s-s/password-input.js",
"./components/o-s-s/pdf-export.js": "./dist/_app_/components/o-s-s/pdf-export.js",
"./components/o-s-s/phone-number-input.js": "./dist/_app_/components/o-s-s/phone-number-input.js",
"./components/o-s-s/pill.js": "./dist/_app_/components/o-s-s/pill.js",
"./components/o-s-s/popover.js": "./dist/_app_/components/o-s-s/popover.js",
Expand Down Expand Up @@ -358,6 +361,7 @@
"./services/asset-map.js": "./dist/_app_/services/asset-map.js",
"./services/base-uploader.js": "./dist/_app_/services/base-uploader.js",
"./services/form-manager.js": "./dist/_app_/services/form-manager.js",
"./services/pdf-export.js": "./dist/_app_/services/pdf-export.js",
"./services/toast.js": "./dist/_app_/services/toast.js",
"./services/wizard-manager.js": "./dist/_app_/services/wizard-manager.js"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/oss-components/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default {
exclude: [
'**/*.stories.*',
'components/o-s-s/private/base-modal.*',
'modifiers/install-scroll-reached-modifier.*'
'modifiers/install-scroll-reached-modifier.*',
'utils/pdf-export/render.*'
]
}),
addon.appReexports(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
fx-row fx-xalign-center"
role={{unless @mainAction "button"}}
tabindex={{unless @mainAction "0"}}
aria-busy={{if @loading "true"}}
{{on "click" this.onDropdownClick}}
...attributes
>
Expand All @@ -15,7 +16,9 @@
tabindex={{if @mainAction "0"}}
{{on "click" this.onMainAction}}
>
{{#if @icon}}
{{#if @loading}}
<OSS::Icon @style="solid" @icon="fa-circle-notch fa-spin" />
{{else if @icon}}
<OSS::Icon @style={{fa-icon-style @icon}} @icon={{fa-icon-value @icon}} />
{{/if}}
{{#if (and @label (not @square))}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import OSSButtonDropdown from './button-dropdown.ts';

const Template = (args) => ({
template: hbs`
<OSS::ButtonDropdown @icon={{this.icon}} @label={{this.label}} @mainAction={{this.mainAction}} @square={{this.square}} @hideArrow={{this.hideArrow}} @size={{this.size}}>
<OSS::ButtonDropdown @icon={{this.icon}} @label={{this.label}} @mainAction={{this.mainAction}} @square={{this.square}} @hideArrow={{this.hideArrow}} @size={{this.size}} @loading={{this.loading}}>
<:items>
<div class="oss-button-dropdown__item font-color-gray-900">
<OSS::Icon @style="solid" @icon="fa-share" /> Share
Expand All @@ -21,6 +21,7 @@ const defaultArgs = {
label: 'Actions',
square: false,
hideArrow: false,
loading: false,
mainAction: fn()
};

Expand Down
17 changes: 16 additions & 1 deletion packages/oss-components/src/components/o-s-s/button-dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface OSSButtonDropdownArgs {
* @default 'md'
*/
size?: 'sm' | 'md';
/**
* Replaces the icon with a spinner, and blocks opening the dropdown and the main action
* @default false
*/
loading?: boolean;
/** An action that is triggered on click in the left part of the button. */
mainAction?(): void;
}
Expand Down Expand Up @@ -61,14 +66,21 @@ export default class OSSButtonDropdown extends Component<OSSButtonDropdownSignat

computedClasses.push(`oss-button-dropdown--${this.args.size ?? 'md'}`);

if (this.args.loading) {
computedClasses.push('oss-button-dropdown--loading');
}

return computedClasses.join(' ');
}

@action
onMainAction(event: MouseEvent): void {
if (this.args.mainAction) {
event.stopPropagation();
this.args.mainAction();

if (!this.args.loading) {
this.args.mainAction();
}
}
}

Expand All @@ -82,6 +94,9 @@ export default class OSSButtonDropdown extends Component<OSSButtonDropdownSignat
@action
toggleDropdown(event: MouseEvent): void {
event.stopPropagation();

if (this.args.loading && !this.displayDropdown) return;

this.displayDropdown = !this.displayDropdown;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/oss-components/src/components/o-s-s/pdf-export.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{#in-element this.portalTarget insertBefore=null}}
<div class="oss-pdf-export" aria-hidden="true" inert ...attributes>
<div class="oss-pdf-export__page" style={{this.pageStyle}} {{did-insert this.registerPage}}>
{{yield (hash ready=this.ready) to="content"}}
</div>
{{#if (has-block "footer")}}
<div class="oss-pdf-export__footer" style={{this.pageStyle}} {{did-insert this.registerFooter}}>
{{yield to="footer"}}
</div>
{{/if}}
</div>
{{/in-element}}
84 changes: 84 additions & 0 deletions packages/oss-components/src/components/o-s-s/pdf-export.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { hbs } from 'ember-cli-htmlbars';
import { fn } from 'storybook/test';

const Template = (args) => ({
template: hbs`
<OSS::Button @label="Export as PDF" @loading={{this.exporting}} />
{{#if this.exporting}}
<OSS::PdfExport @fileName={{this.fileName}} @width={{this.width}} @timeout={{this.timeout}}
@onSuccess={{this.onSuccess}} @onError={{this.onError}}>
<:content as |pdf|>
<div class="fx-col fx-gap-px-24 padding-px-36" {{did-insert pdf.ready}}>
<span class="font-size-xl font-weight-semibold">Creator report</span>
<OSS::Alert @skin="info" @title="Exported off-screen" @subtitle="This content is rendered out of view, then saved as a PDF file." />
</div>
</:content>
<:footer>
<div class="fx-row fx-malign-space-between padding-px-12 background-color-primary-500 font-color-white">
<span>Powered by Upfluence</span>
<span>www.upfluence.com</span>
</div>
</:footer>
</OSS::PdfExport>
{{/if}}
`,
context: args
});

const defaultArgs = {
exporting: false,
fileName: 'creator-report.pdf',
width: 800,
timeout: 30000,
onSuccess: fn(),
onError: fn()
};

export default {
title: 'Components/OSS::PdfExport',
component: 'pdf-export',
render: Template,
args: defaultArgs,
argTypes: {
exporting: {
description: 'Story only - Renders the component, which starts the export.',
table: { type: { summary: 'boolean' }, defaultValue: { summary: 'false' } },
control: { type: 'boolean' }
},
fileName: {
description: 'The name of the downloaded file',
table: { type: { summary: 'string' }, defaultValue: { summary: 'undefined' } },
control: { type: 'text' }
},
width: {
description: 'The width, in pixels, at which the content is rendered. The page is scaled to the A4 width.',
table: { type: { summary: 'number' }, defaultValue: { summary: 'undefined' } },
control: { type: 'number' }
},
timeout: {
description:
'Optional - The delay, in milliseconds, allowed for the content to call ready, then again for the capture',
table: { type: { summary: 'number' }, defaultValue: { summary: '30000' } },
control: { type: 'number' }
},
onSuccess: {
description: 'Called once the PDF file is downloaded, or offered for download',
table: { category: 'Actions', type: { summary: 'onSuccess(): void' } }
},
onError: {
description: 'Called when the export fails or times out',
table: { category: 'Actions', type: { summary: 'onError(error: unknown): void' } }
}
},
parameters: {
docs: {
description: {
component:
'Renders the `content` block off-screen and downloads it as an A4 PDF file once the content calls the yielded `ready` action. The optional `footer` block is repeated at the bottom of every page. The component is meant to be rendered only while an export is running.'
},
iframeHeight: 120
}
}
};

export const Usage = {};
109 changes: 109 additions & 0 deletions packages/oss-components/src/components/o-s-s/pdf-export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { assert } from '@ember/debug';
import { action } from '@ember/object';
import type Owner from '@ember/owner';
import { next } from '@ember/runloop';
import { service } from '@ember/service';
import { htmlSafe } from '@ember/template';
import Component from '@glimmer/component';

import type PdfExportService from '../../services/pdf-export';

interface OSSPdfExportArgs {
fileName: string;
width: number;
timeout?: number;
onSuccess(): void;
onError(error: unknown): void;
}

const DEFAULT_TIMEOUT = 30_000;

export default class OSSPdfExport extends Component<OSSPdfExportArgs> {
@service declare pdfExport: PdfExportService;

portalTarget: HTMLElement = document.body;

private controller: AbortController = new AbortController();
private page?: HTMLElement;
private footer?: HTMLElement;
private started: boolean = false;
private timeoutId?: ReturnType<typeof setTimeout>;

constructor(owner: Owner, args: OSSPdfExportArgs) {
super(owner, args);

assert('[component][OSS::PdfExport] The @fileName argument is mandatory.', args.fileName);
assert('[component][OSS::PdfExport] The @width argument is mandatory.', args.width);

this.restartTimeout();
}

get pageStyle(): ReturnType<typeof htmlSafe> {
return htmlSafe(`width: ${this.args.width}px;`);
}

willDestroy(): void {
super.willDestroy();
clearTimeout(this.timeoutId);
this.controller.abort();
}

@action
registerPage(element: HTMLElement): void {
this.page = element;
}

@action
registerFooter(element: HTMLElement): void {
this.footer = element;
}

@action
ready(): void {
if (this.started) return;

this.started = true;
this.restartTimeout();
next(this, this.capture);
}

private restartTimeout(): void {
clearTimeout(this.timeoutId);
this.timeoutId = setTimeout(
() => this.fail(new Error('[component][OSS::PdfExport] The export timed out.')),
this.args.timeout ?? DEFAULT_TIMEOUT
);
}

private async capture(): Promise<void> {
try {
await this.pdfExport.export(this.page!, {
fileName: this.args.fileName,
footer: this.footer,
signal: this.controller.signal
});
this.succeed();
} catch (error) {
this.fail(error);
}
}

private get isFinished(): boolean {
return this.isDestroying || this.isDestroyed || this.controller.signal.aborted;
}

private succeed(): void {
if (this.isFinished) return;

clearTimeout(this.timeoutId);
this.args.onSuccess();
}

private fail(error: unknown): void {
if (this.isFinished) return;

clearTimeout(this.timeoutId);
this.controller.abort();
this.args.onError(error);
}
}
Loading
Loading