|
| 1 | +# Use third-party libraries |
| 2 | + |
| 3 | +Every developer learns one of the most important principles of software engineering early in their career: DRY (Don’t Repeat Yourself). Using third-party libraries can save your time as you do not need to develop the functionality that the library provides. Openblocks provides some built-in third-party libraries for common uses, and you can manually import other libraries on demand. |
| 4 | + |
| 5 | +### Built-in third-party libraries |
| 6 | + |
| 7 | +Openblocks provides some JavaScript built-in libraries for use: |
| 8 | + |
| 9 | +| Library | Docs | Version | |
| 10 | +| --------- | -------------------------------------------------------------------- | -------------------------- | |
| 11 | +| lodash | [https://lodash.com/docs/](https://lodash.com/docs/) | 4.17.21 | |
| 12 | +| moment | [https://momentjs.com/docs/](https://momentjs.com/docs/) | 2.29.3 | |
| 13 | +| uuid | [https://github.com/uuidjs/uuid](https://github.com/uuidjs/uuid) | 8.3.2(Support v1/v3/v4/v5) | |
| 14 | +| numbro | [https://numbrojs.com/format.html](https://numbrojs.com/format.html) | 2.3.6 | |
| 15 | +| papaparse | [https://www.papaparse.com/docs](https://www.papaparse.com/docs) | 5.3.2 | |
| 16 | + |
| 17 | +### Manually import third-party libraries |
| 18 | + |
| 19 | +Openblocks supports setting up preloaded JavaScript and libraries which can be **app-level** or **workspace-level**. |
| 20 | + |
| 21 | +* **App-level** JavaScript **** gets loaded only in the app where defined. Plus, app A cannot use libraries that are set up for app B. |
| 22 | +* **Workspace-level** JavaScript **** will be loaded when you open any application in your workspace. All the apps can access those libraries. There can be a certain impact on app performance, especially when you have complex JavaScript functions that aren't being used in every app. |
| 23 | + |
| 24 | +{% hint style="info" %} |
| 25 | +**Tips you should know before setting up libraries:** |
| 26 | + |
| 27 | +* External libraries are loaded and run in the browser. |
| 28 | +* NodeJS-only libraries are not supported now. |
| 29 | +* URLs of external libraries need to support cross-domain. |
| 30 | +* The export of the library must be set directly on the window object, global variables like (var xxx = xxx) do not take effect. |
| 31 | +* The external libraries run in a restricted sandbox environment and the following global variables are not available: |
| 32 | + |
| 33 | + <mark style="background-color:yellow;">`parent`</mark> |
| 34 | + |
| 35 | + <mark style="background-color:yellow;">`document`</mark> |
| 36 | + |
| 37 | + <mark style="background-color:yellow;">`location`</mark> |
| 38 | + |
| 39 | + <mark style="background-color:yellow;">`chrome`</mark> |
| 40 | + |
| 41 | + <mark style="background-color:yellow;">`setTimeout`</mark> |
| 42 | + |
| 43 | + <mark style="background-color:yellow;">`fetch`</mark> |
| 44 | + |
| 45 | + <mark style="background-color:yellow;">`setInterval`</mark> |
| 46 | + |
| 47 | + <mark style="background-color:yellow;">`clearInterval`</mark> |
| 48 | + |
| 49 | + <mark style="background-color:yellow;">`setImmediate`</mark> |
| 50 | + |
| 51 | + <mark style="background-color:yellow;">`XMLHttpRequest`</mark> |
| 52 | + |
| 53 | + <mark style="background-color:yellow;">`importScripts`</mark> |
| 54 | + |
| 55 | + <mark style="background-color:yellow;">`Navigator`</mark> |
| 56 | + |
| 57 | + <mark style="background-color:yellow;">`MutationObserver`</mark> |
| 58 | +{% endhint %} |
| 59 | + |
| 60 | +\ |
| 61 | +Now let's take **cowsay** as an example and import it on app-level and workspace-level. |
| 62 | + |
| 63 | +* GitHub page: [https://github.com/piuccio/cowsay](https://github.com/piuccio/cowsay) |
| 64 | +* Library link: [https://unpkg.com/cowsay@1.5.0/build/cowsay.umd.js](https://unpkg.com/cowsay@1.5.0/build/cowsay.umd.js) |
| 65 | + |
| 66 | +#### Import on App-level |
| 67 | + |
| 68 | +Navigate to the left sidebar, click <img src="../.gitbook/assets/image (1).png" alt="" data-size="line"> > **Other** > **Scripts and style** > **Add a library**. Then paste the **cowsay** link here. |
| 69 | + |
| 70 | +<figure><img src="../.gitbook/assets/Add a library (2).png" alt=""><figcaption></figcaption></figure> |
| 71 | + |
| 72 | +Now you can write code in **JS query** to test its usage with `cowsay.say`: |
| 73 | + |
| 74 | +<figure><img src="../.gitbook/assets/write code in JS query (2).png" alt=""><figcaption></figcaption></figure> |
| 75 | + |
| 76 | +or in **Properties** to use: |
| 77 | + |
| 78 | +<figure><img src="../.gitbook/assets/or in Properties (2).png" alt=""><figcaption></figcaption></figure> |
| 79 | + |
| 80 | +And since you have set up cowsay just for **Openblocks's new application 1,** you can not use cowsay in another app. |
| 81 | + |
| 82 | +<figure><img src="../.gitbook/assets/in another app (1).png" alt=""><figcaption></figcaption></figure> |
| 83 | + |
| 84 | +#### Import on Workspace-level libraries |
| 85 | + |
| 86 | +Navigate to **Workspace settings** > **Advanced** > **JavaScript library** > **Add a library**. Then paste the **cowsay** link here and click **Save**. Now you can use **cowsay** library **** in any app of your workspace.\ |
| 87 | +.png>) |
| 88 | + |
| 89 | +<figure><img src="../.gitbook/assets/Import on Workspace-level libraries-advanced.png" alt=""><figcaption></figcaption></figure> |
0 commit comments