Skip to content

Commit e3c55ed

Browse files
authored
Merge pull request #6079 from influxdata/chore-js-refactor-footer-scripts-modules
Chore: JavaScript: refactor footer scripts modules
2 parents c137885 + 36056ce commit e3c55ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+19494
-458
lines changed

.circleci/config.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 2
1+
version: 2.1
22
jobs:
33
build:
44
docker:
@@ -41,7 +41,7 @@ jobs:
4141
- /home/circleci/bin
4242
- run:
4343
name: Hugo Build
44-
command: npx hugo --logLevel info --minify --destination workspace/public
44+
command: npx hugo --config config/production/config.yml --logLevel info --minify --gc --destination workspace/public
4545
- persist_to_workspace:
4646
root: workspace
4747
paths:
@@ -68,7 +68,6 @@ jobs:
6868
when: on_success
6969

7070
workflows:
71-
version: 2
7271
build:
7372
jobs:
7473
- build

.github/instructions/contributing.instructions.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ The shortcode takes a regular expression for matching placeholder names.
16791679
Use the `code-placeholder-key` shortcode to format the placeholder names in
16801680
text that describes the placeholder--for example:
16811681
1682-
```
1682+
```markdown
16831683
{{% code-placeholders "DATABASE_NAME|USERNAME|PASSWORD_OR_TOKEN|API_TOKEN|exampleuser@influxdata.com" %}}
16841684
```sh
16851685
curl --request POST http://localhost:8086/write?db=DATABASE_NAME \
@@ -1703,3 +1703,64 @@ InfluxDB API documentation when documentation is deployed.
17031703
Redoc generates HTML documentation using the InfluxDB `swagger.yml`.
17041704
For more information about generating InfluxDB API documentation, see the
17051705
[API Documentation README](https://github.com/influxdata/docs-v2/tree/master/api-docs#readme).
1706+
1707+
## JavaScript in the documentation UI
1708+
1709+
The InfluxData documentation UI uses JavaScript with ES6+ syntax and
1710+
`assets/js/main.js` as the entry point to import modules from
1711+
`assets/js`.
1712+
Only `assets/js/main.js` should be imported in HTML files.
1713+
1714+
`assets/js/main.js` registers components and initializes them on page load.
1715+
1716+
If you're adding UI functionality that requires JavaScript, follow these steps:
1717+
1718+
1. In your HTML file, add a `data-component` attribute to the element that
1719+
should be initialized by your JavaScript code. For example:
1720+
1721+
```html
1722+
<div data-component="my-component"></div>
1723+
```
1724+
1725+
2. Following the component pattern, create a single-purpose JavaScript module
1726+
(`assets/js/components/my-component.js`)
1727+
that exports a single function that receives the component element and initializes it.
1728+
3. In `assets/js/main.js`, import the module and register the component to ensure
1729+
the component is initialized on page load.
1730+
1731+
### Debugging JavaScript
1732+
1733+
To debug JavaScript code used in the InfluxData documentation UI:
1734+
1735+
1. In your JavaScript module, import debug helpers from `assets/js/utils/debug-helpers.js`.
1736+
These helpers provide breakpoints and console logging as a workaround for
1737+
Hugo's lack of source map support in the asset pipeline.
1738+
2. Insert debug statements by calling the helper functions in your code--for example:
1739+
1740+
```js
1741+
import { debugLog, debugBreak, debugInspect } from './utils/debug-helpers.js';
1742+
1743+
const data = debugInspect(someData, 'Data');
1744+
debugLog('Processing data', 'myFunction');
1745+
1746+
function processData() {
1747+
// Add a breakpoint that works with DevTools
1748+
debugBreak();
1749+
1750+
// Your existing code...
1751+
}
1752+
```
1753+
1754+
3. Start Hugo in development mode--for example:
1755+
1756+
```bash
1757+
yarn hugo server
1758+
```
1759+
1760+
4. In VS Code, go to Run > Start Debugging, and select the "Debug Docs (console-based)" configuration.
1761+
1762+
Your system uses the configuration in `launch.json` to launch the site in Chrome
1763+
and attach the debugger to the Developer Tools console.
1764+
1765+
Make sure to remove the debug statements before merging your changes.
1766+
The debug helpers are designed to be used in development and should not be used in production.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ node_modules
2121
test-results.xml
2222
/influxdb3cli-build-scripts/content
2323
.vscode/*
24+
!.vscode/launch.json
2425
.idea
2526
**/config.toml
2627
package-lock.json

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
**/.svn
44
**/.hg
55
**/node_modules
6+
assets/jsconfig.json

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug Docs (console-based)",
6+
"type": "chrome",
7+
"request": "launch",
8+
"url": "http://localhost:1313",
9+
"webRoot": "${workspaceFolder}",
10+
"skipFiles": [
11+
"<node_internals>/**"
12+
],
13+
"sourceMaps": false,
14+
"trace": true,
15+
"smartStep": false
16+
}
17+
]
18+
}

CONTRIBUTING.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ The shortcode takes a regular expression for matching placeholder names.
16671667
Use the `code-placeholder-key` shortcode to format the placeholder names in
16681668
text that describes the placeholder--for example:
16691669
1670-
```
1670+
```markdown
16711671
{{% code-placeholders "DATABASE_NAME|USERNAME|PASSWORD_OR_TOKEN|API_TOKEN|exampleuser@influxdata.com" %}}
16721672
```sh
16731673
curl --request POST http://localhost:8086/write?db=DATABASE_NAME \
@@ -1691,3 +1691,64 @@ InfluxDB API documentation when documentation is deployed.
16911691
Redoc generates HTML documentation using the InfluxDB `swagger.yml`.
16921692
For more information about generating InfluxDB API documentation, see the
16931693
[API Documentation README](https://github.com/influxdata/docs-v2/tree/master/api-docs#readme).
1694+
1695+
## JavaScript in the documentation UI
1696+
1697+
The InfluxData documentation UI uses JavaScript with ES6+ syntax and
1698+
`assets/js/main.js` as the entry point to import modules from
1699+
`assets/js`.
1700+
Only `assets/js/main.js` should be imported in HTML files.
1701+
1702+
`assets/js/main.js` registers components and initializes them on page load.
1703+
1704+
If you're adding UI functionality that requires JavaScript, follow these steps:
1705+
1706+
1. In your HTML file, add a `data-component` attribute to the element that
1707+
should be initialized by your JavaScript code. For example:
1708+
1709+
```html
1710+
<div data-component="my-component"></div>
1711+
```
1712+
1713+
2. Following the component pattern, create a single-purpose JavaScript module
1714+
(`assets/js/components/my-component.js`)
1715+
that exports a single function that receives the component element and initializes it.
1716+
3. In `assets/js/main.js`, import the module and register the component to ensure
1717+
the component is initialized on page load.
1718+
1719+
### Debugging JavaScript
1720+
1721+
To debug JavaScript code used in the InfluxData documentation UI:
1722+
1723+
1. In your JavaScript module, import debug helpers from `assets/js/utils/debug-helpers.js`.
1724+
These helpers provide breakpoints and console logging as a workaround for
1725+
Hugo's lack of source map support in the asset pipeline.
1726+
2. Insert debug statements by calling the helper functions in your code--for example:
1727+
1728+
```js
1729+
import { debugLog, debugBreak, debugInspect } from './utils/debug-helpers.js';
1730+
1731+
const data = debugInspect(someData, 'Data');
1732+
debugLog('Processing data', 'myFunction');
1733+
1734+
function processData() {
1735+
// Add a breakpoint that works with DevTools
1736+
debugBreak();
1737+
1738+
// Your existing code...
1739+
}
1740+
```
1741+
1742+
3. Start Hugo in development mode--for example:
1743+
1744+
```bash
1745+
yarn hugo server
1746+
```
1747+
1748+
4. In VS Code, go to Run > Start Debugging, and select the "Debug Docs (console-based)" configuration.
1749+
1750+
Your system uses the configuration in `launch.json` to launch the site in Chrome
1751+
and attach the debugger to the Developer Tools console.
1752+
1753+
Make sure to remove the debug statements before merging your changes.
1754+
The debug helpers are designed to be used in development and should not be used in production.

assets/js/components/diagram.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Memoize the mermaid module import
2+
let mermaidPromise = null;
3+
4+
export default function Diagram({ component }) {
5+
// Import mermaid.js module (memoized)
6+
if (!mermaidPromise) {
7+
mermaidPromise = import('mermaid');
8+
}
9+
mermaidPromise.then(({ default: mermaid }) => {
10+
// Configure mermaid with InfluxData theming
11+
mermaid.initialize({
12+
startOnLoad: false, // We'll manually call run()
13+
theme: document.body.classList.contains('dark-theme') ? 'dark' : 'default',
14+
themeVariables: {
15+
fontFamily: 'Proxima Nova',
16+
fontSize: '16px',
17+
lineColor: '#22ADF6',
18+
primaryColor: '#22ADF6',
19+
primaryTextColor: '#545454',
20+
secondaryColor: '#05CE78',
21+
tertiaryColor: '#f4f5f5',
22+
},
23+
securityLevel: 'loose', // Required for interactive diagrams
24+
logLevel: 'error'
25+
});
26+
27+
// Process the specific diagram component
28+
try {
29+
mermaid.run({ nodes: [component] });
30+
} catch (error) {
31+
console.error('Mermaid diagram rendering error:', error);
32+
}
33+
34+
// Store reference to mermaid for theme switching
35+
if (!window.mermaidInstances) {
36+
window.mermaidInstances = new Map();
37+
}
38+
window.mermaidInstances.set(component, mermaid);
39+
}).catch(error => {
40+
console.error('Failed to load Mermaid library:', error);
41+
});
42+
43+
// Listen for theme changes to refresh diagrams
44+
const observer = new MutationObserver((mutations) => {
45+
mutations.forEach((mutation) => {
46+
if (mutation.attributeName === 'class' &&
47+
document.body.classList.contains('dark-theme') !== window.isDarkTheme) {
48+
window.isDarkTheme = document.body.classList.contains('dark-theme');
49+
50+
// Reload this specific diagram with new theme
51+
if (window.mermaidInstances?.has(component)) {
52+
const mermaid = window.mermaidInstances.get(component);
53+
mermaid.initialize({
54+
theme: window.isDarkTheme ? 'dark' : 'default'
55+
});
56+
mermaid.run({ nodes: [component] });
57+
}
58+
}
59+
});
60+
});
61+
62+
// Watch for theme changes on body element
63+
observer.observe(document.body, { attributes: true });
64+
65+
// Return cleanup function to be called when component is destroyed
66+
return () => {
67+
observer.disconnect();
68+
if (window.mermaidInstances?.has(component)) {
69+
window.mermaidInstances.delete(component);
70+
}
71+
};
72+
}

0 commit comments

Comments
 (0)