Skip to content

Commit

Permalink
fix(composable): change how composable is exported
Browse files Browse the repository at this point in the history
The composable now exports the value from the plugin
The option to enable/disable dthe plugin was removed
The documentation was updated
  • Loading branch information
BayBreezy committed Sep 26, 2023
1 parent 65c9564 commit 93bde56
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 35 deletions.
19 changes: 8 additions & 11 deletions docs/content/1.introduction/3.options.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ export default defineNuxtConfig({
* @default true
*/
enabled: true;
/**
* Enable the plugin injection
* @default true
*/
enablePlugin: true;
/**
* Enable the composable injection. This adds both the `usePDFMake` & `useFontPresets` composables
* @default true
Expand All @@ -47,7 +42,7 @@ const exportComposable = () => {
const pdfMake = usePDFMake();

pdfMake.createPdf(docDef, tableLayoutDef, useFontPresets()).open();
}
};
```

You can also use a specific font from the set like so:
Expand All @@ -56,27 +51,29 @@ You can also use a specific font from the set like so:
const exportData = () => {
const pdfMake = usePDFMake();

pdfMake.createPdf(docDef, tableLayoutDef, { Inter: useFontPresets().Inter }).open();
}
pdfMake
.createPdf(docDef, tableLayoutDef, { Inter: useFontPresets().Inter })
.open();
};
```


Currently, I only need these 4 fonts lol. Feel free to add your own fonts by following the guide [here](https://pdfmake.github.io/docs/0.1/fonts/custom-fonts-client-side/vfs/#2-assign-pdfmakefonts-in-your-javascript).

### Current Fonts

::list{type="success"}

- Inter
- Roboto
- Poppins
- Lora
::
::

This is what the full signature of createPdf looks like:

```ts
// The full signature of createPdf looks like this.
// tableLayouts, fonts and vfs are all optional - falsy values will cause
// pdfMake.tableLayouts, pdfMake.fonts or pdfMake.vfs to be used.
pdfMake.createPdf(docDefinition, tableLayouts, fonts, vfs)
pdfMake.createPdf(docDefinition, tableLayouts, fonts, vfs);
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"changelogen": "^0.5.5",
"eslint": "^8.50.0",
"nuxt": "^3.7.4",
"postcss-custom-properties": "^13.3.2",
"vitest": "^0.34.5"
}
}
}
20 changes: 1 addition & 19 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ export interface ModuleOptions {
* @default true
*/
enabled?: boolean;
/**
* Enable the plugin injection
* @default true
*/
enablePlugin?: boolean;
/**
* Enable the composable injection. This adds both the `usePDFMake` & `useFontPresets` composables
* @default true
Expand All @@ -42,7 +37,6 @@ export default defineNuxtModule<ModuleOptions>({
// Default configuration options of the Nuxt module
defaults: {
enabled: true,
enablePlugin: true,
enableComposable: true,
enableDevtools: true,
},
Expand All @@ -52,19 +46,7 @@ export default defineNuxtModule<ModuleOptions>({
if (!options.enabled) return;

// Add plugin
if (options.enablePlugin) {
nuxt.options.app.head.script ||= [];
nuxt.options.app.head.script.push(
{
src: "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js",
},
{
src: "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.min.js",
}
);
// Do not add the extension since the `.ts` will be transpiled to `.mjs` after `npm run prepack`
addPlugin(resolver.resolve("./runtime/pdfmake.client"));
}
addPlugin(resolver.resolve("./runtime/pdfmake.client"));

// Add composable
if (options.enableComposable) {
Expand Down
4 changes: 1 addition & 3 deletions src/runtime/composables/usePDFMake.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as pdfMake from "pdfmake/build/pdfmake";

/**
* A composable that returns the pdfMake instance.
* @returns The pdfMake instance.
Expand All @@ -14,5 +12,5 @@ import * as pdfMake from "pdfmake/build/pdfmake";
* @see https://pdfmake.github.io/docs/0.1/getting-started/client-side/methods/
*/
export const usePDFMake = () => {
return pdfMake;
return useNuxtApp().$pdfMake;
};
16 changes: 15 additions & 1 deletion src/runtime/pdfmake.client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { defineNuxtPlugin } from "#app";
import * as pdfMake from "pdfmake/build/pdfmake";

export default defineNuxtPlugin(() => {
const pdfMake = window?.pdfMake as typeof import("pdfmake/build/pdfmake");
if (process.client) {
// @ts-ignore
pdfMake.fonts = {
Roboto: {
normal:
"https://cdn.jsdelivr.net/npm/typeface-roboto@1.1.13/files/roboto-latin-400.woff",
bold: "https://cdn.jsdelivr.net/npm/typeface-roboto@1.1.13/files/roboto-latin-700.woff",
italics:
"https://cdn.jsdelivr.net/npm/typeface-roboto@1.1.13/files/roboto-latin-400italic.woff",
bolditalics:
"https://cdn.jsdelivr.net/npm/typeface-roboto@1.1.13/files/roboto-latin-700italic.woff",
},
};
}
return {
provide: {
pdfMake,
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,31 @@
resolved "https://registry.yarnpkg.com/@csstools/cascade-layer-name-parser/-/cascade-layer-name-parser-1.0.4.tgz#3ff490b84660dc0592b4315029f22908f3de0577"
integrity sha512-zXMGsJetbLoXe+gjEES07MEGjL0Uy3hMxmnGtVBrRpVKr5KV9OgCB09zr/vLrsEtoVQTgJFewxaU8IYSAE4tjg==

"@csstools/cascade-layer-name-parser@^1.0.5":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@csstools/cascade-layer-name-parser/-/cascade-layer-name-parser-1.0.5.tgz#c4d276e32787651df0007af22c9fa70d9c9ca3c2"
integrity sha512-v/5ODKNBMfBl0us/WQjlfsvSlYxfZLhNMVIsuCPib2ulTwGKYbKJbwqw671+qH9Y4wvWVnu7LBChvml/wBKjFg==

"@csstools/css-parser-algorithms@^2.3.1":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@csstools/css-parser-algorithms/-/css-parser-algorithms-2.3.1.tgz#ec4fc764ba45d2bb7ee2774667e056aa95003f3a"
integrity sha512-xrvsmVUtefWMWQsGgFffqWSK03pZ1vfDki4IVIIUxxDKnGBzqNgv0A7SB1oXtVNEkcVO8xi1ZrTL29HhSu5kGA==

"@csstools/css-parser-algorithms@^2.3.2":
version "2.3.2"
resolved "https://registry.yarnpkg.com/@csstools/css-parser-algorithms/-/css-parser-algorithms-2.3.2.tgz#1e0d581dbf4518cb3e939c3b863cb7180c8cedad"
integrity sha512-sLYGdAdEY2x7TSw9FtmdaTrh2wFtRJO5VMbBrA8tEqEod7GEggFmxTSK9XqExib3yMuYNcvcTdCZIP6ukdjAIA==

"@csstools/css-tokenizer@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@csstools/css-tokenizer/-/css-tokenizer-2.2.0.tgz#9d70e6dcbe94e44c7400a2929928db35c4de32b5"
integrity sha512-wErmsWCbsmig8sQKkM6pFhr/oPha1bHfvxsUY5CYSQxwyhA9Ulrs8EqCgClhg4Tgg2XapVstGqSVcz0xOYizZA==

"@csstools/css-tokenizer@^2.2.1":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@csstools/css-tokenizer/-/css-tokenizer-2.2.1.tgz#9dc431c9a5f61087af626e41ac2a79cce7bb253d"
integrity sha512-Zmsf2f/CaEPWEVgw29odOj+WEVoiJy9s9NOv5GgNY9mZ1CZ7394By6wONrONrTsnNDv6F9hR02nvFihrGVGHBg==

"@csstools/selector-specificity@^2.0.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@csstools/selector-specificity/-/selector-specificity-2.2.0.tgz#2cbcf822bf3764c9658c4d2e568bd0c0cb748016"
Expand Down Expand Up @@ -6369,6 +6384,16 @@ postcss-custom-properties@^13.2.0:
"@csstools/css-tokenizer" "^2.2.0"
postcss-value-parser "^4.2.0"

postcss-custom-properties@^13.3.2:
version "13.3.2"
resolved "https://registry.yarnpkg.com/postcss-custom-properties/-/postcss-custom-properties-13.3.2.tgz#88952f883003d897ade5c836e1e005b09a12f02b"
integrity sha512-2Coszybpo8lpLY24vy2CYv9AasiZ39/bs8Imv0pWMq55Gl8NWzfc24OAo3zIX7rc6uUJAqESnVOMZ6V6lpMjJA==
dependencies:
"@csstools/cascade-layer-name-parser" "^1.0.5"
"@csstools/css-parser-algorithms" "^2.3.2"
"@csstools/css-tokenizer" "^2.2.1"
postcss-value-parser "^4.2.0"

postcss-discard-comments@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/postcss-discard-comments/-/postcss-discard-comments-6.0.0.tgz#9ca335e8b68919f301b24ba47dde226a42e535fe"
Expand Down

0 comments on commit 93bde56

Please sign in to comment.