igniteui-astro-components 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +374 -0
- package/package.json +109 -0
- package/src/components/DocsSidebar/DocsSidebar.astro +99 -0
- package/src/components/DocsSidebar/README.md +166 -0
- package/src/components/DocsSidebar/SidebarFilterInput.astro +74 -0
- package/src/components/DocsSidebar/SidebarLabel.astro +40 -0
- package/src/components/DocsSidebar/sidebar-filter.ts +393 -0
- package/src/components/DocsSidebar/toTreeNodes.ts +66 -0
- package/src/components/DocsSubHeader/DocsSubHeader.astro +320 -0
- package/src/components/DocsSubHeader/README.md +74 -0
- package/src/components/DocsToc/DocsToc.astro +307 -0
- package/src/components/DocsToc/README.md +68 -0
- package/src/components/DocsToc/toTreeNodes.ts +57 -0
- package/src/components/DocsTree/DocsTree.astro +66 -0
- package/src/components/DocsTree/DocsTreeItem.astro +84 -0
- package/src/components/DocsTree/README.md +105 -0
- package/src/components/DocsTree/docs-tree.css +227 -0
- package/src/components/DocsTree/types.ts +47 -0
- package/src/components/GlobalFooter/GlobalFooter.astro +90 -0
- package/src/components/GlobalFooter/GlobalFooter.module.scss +23 -0
- package/src/components/GlobalFooter/README.md +35 -0
- package/src/components/GlobalNavBar/GlobalNavBar.astro +218 -0
- package/src/components/GlobalNavBar/GlobalNavBar.module.scss +215 -0
- package/src/components/GlobalNavBar/README.md +49 -0
- package/src/components/MobileSidebarToggle/MobileSidebarToggle.astro +112 -0
- package/src/components/MobileSidebarToggle/README.md +31 -0
- package/src/components/Search/README.md +43 -0
- package/src/components/Search/Search.astro +222 -0
- package/src/components/ThemingWidget/README.md +35 -0
- package/src/components/ThemingWidget/ThemingWidget.astro +152 -0
- package/src/components/mdx/ApiLink/ApiLink.astro +121 -0
- package/src/components/mdx/ApiLink/README.md +68 -0
- package/src/components/mdx/ApiRef/ApiRef.astro +92 -0
- package/src/components/mdx/ApiRef/README.md +50 -0
- package/src/components/mdx/DocsAside/DocsAside.astro +86 -0
- package/src/components/mdx/DocsAside/README.md +66 -0
- package/src/components/mdx/DocsAside/icons/caution.svg +3 -0
- package/src/components/mdx/DocsAside/icons/danger.svg +3 -0
- package/src/components/mdx/DocsAside/icons/note.svg +3 -0
- package/src/components/mdx/DocsAside/icons/tip.svg +4 -0
- package/src/components/mdx/PlatformBlock/PlatformBlock.astro +24 -0
- package/src/components/mdx/PlatformBlock/README.md +57 -0
- package/src/components/mdx/Sample/README.md +59 -0
- package/src/components/mdx/Sample/Sample.astro +142 -0
- package/src/content-helper.ts +159 -0
- package/src/content.config.ts +17 -0
- package/src/index.ts +42 -0
- package/src/integration.ts +841 -0
- package/src/layouts/DocsLayout/DocsLayout.astro +307 -0
- package/src/layouts/DocsLayout/README.md +124 -0
- package/src/lib/nav-helpers.ts +155 -0
- package/src/lib/platform-context.ts +305 -0
- package/src/lib/sidebar/helpers.ts +81 -0
- package/src/lib/sidebar/types.ts +36 -0
- package/src/llms.ts +315 -0
- package/src/platform.ts +281 -0
- package/src/plugins/remark-docfx.ts +368 -0
- package/src/scripts/sample-widget.ts +516 -0
- package/src/sidebar.ts +167 -0
- package/src/styles/custom.scss +254 -0
- package/src/styles/ig-theme.scss +43 -0
- package/src/virtual-modules.d.ts +35 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sample-widget.ts
|
|
3
|
+
*
|
|
4
|
+
* Client-side widget initialization for the <Sample> Astro component.
|
|
5
|
+
*
|
|
6
|
+
* The HTML shell is pre-rendered by Sample.astro (navbar, iframe, loading state).
|
|
7
|
+
* This module handles the dynamic parts:
|
|
8
|
+
* – Serial iframe loading (one at a time, IntersectionObserver-based)
|
|
9
|
+
* – Code-tab fetching and rendering (Angular JSON or xplat JSON)
|
|
10
|
+
* – Tab switching between EXAMPLE and code views
|
|
11
|
+
* – Live-editing footer (StackBlitz / CodeSandbox)
|
|
12
|
+
*
|
|
13
|
+
* Selector: `.code-view[data-platform]`
|
|
14
|
+
* This distinguishes pre-rendered Sample.astro widgets from `.ig-code-view`
|
|
15
|
+
* elements that come from the docfx pipeline (handled by code-view.js).
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// ─── Constants ────────────────────────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
const SAMPLES_ORDER = ['modules', 'ts', 'html', 'scss', 'css'];
|
|
21
|
+
|
|
22
|
+
const XPLAT_SAMPLES_ORDER: Record<string, string[]> = {
|
|
23
|
+
react: ['tsx', 'ts', 'html', 'css'],
|
|
24
|
+
wc: ['tsx', 'ts', 'html', 'css'],
|
|
25
|
+
blazor: ['razor', 'cs', 'js', 'css'],
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const XPLAT_CODE_BASE: Record<string, string> = {
|
|
29
|
+
wc: '/assets/code-viewer/',
|
|
30
|
+
react: '/code-viewer/',
|
|
31
|
+
blazor: '/code-viewer/',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const DV_PATHS = ['gauges/', 'maps/', 'excel/', 'charts/'];
|
|
35
|
+
const ASSETS_RE = /([.]{0,2}\/)*assets\//g;
|
|
36
|
+
|
|
37
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
interface SampleFile {
|
|
40
|
+
fileExtension: string;
|
|
41
|
+
fileHeader: string;
|
|
42
|
+
content: string;
|
|
43
|
+
isMain: boolean;
|
|
44
|
+
path?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface WidgetContext {
|
|
48
|
+
widget: HTMLElement;
|
|
49
|
+
iframeSrc: string;
|
|
50
|
+
demosBaseUrl: string;
|
|
51
|
+
widgetIndex: number;
|
|
52
|
+
navbar: HTMLElement;
|
|
53
|
+
container: HTMLElement;
|
|
54
|
+
activateTab: (tab: HTMLElement) => void;
|
|
55
|
+
githubSrc: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Normalizes the platform string so it matches the lookup table keys.
|
|
62
|
+
* platform-context.ts returns 'webcomponents' but XPLAT_* tables use 'wc'.
|
|
63
|
+
*/
|
|
64
|
+
function normalizePlatform(raw: string): string {
|
|
65
|
+
if (raw === 'webcomponents' || raw === 'web-components') return 'wc';
|
|
66
|
+
return raw;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isXplatPlatform(platform: string): boolean {
|
|
70
|
+
return platform === 'react' || platform === 'wc' || platform === 'blazor';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getSamplePath(iframeSrc: string, demosBaseUrl: string): string {
|
|
74
|
+
return iframeSrc
|
|
75
|
+
.replace(demosBaseUrl + '/', '')
|
|
76
|
+
.replace(/\?.*/, '')
|
|
77
|
+
.replace(/\/$/, '');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function isDvSample(samplePath: string): boolean {
|
|
81
|
+
return DV_PATHS.some(p => samplePath.startsWith(p));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function getRepoBranch(demosBaseUrl: string, isDv: boolean): string {
|
|
85
|
+
const host = new URL(demosBaseUrl).host;
|
|
86
|
+
const isNonProd = host.includes('staging')
|
|
87
|
+
|| window.location.hostname === 'localhost'
|
|
88
|
+
|| window.location.hostname === '127.0.0.1';
|
|
89
|
+
if (isDv) return 'vnext';
|
|
90
|
+
return isNonProd ? 'vNext' : 'master';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function getXplatBranch(demosBaseUrl: string): string {
|
|
94
|
+
const host = new URL(demosBaseUrl).host;
|
|
95
|
+
const isNonProd = host.includes('staging')
|
|
96
|
+
|| window.location.hostname === 'localhost'
|
|
97
|
+
|| window.location.hostname === '127.0.0.1';
|
|
98
|
+
return isNonProd ? 'vnext' : 'master';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function replaceRelativeAssetUrls(files: SampleFile[], demosBaseUrl: string): void {
|
|
102
|
+
const assetsBase = demosBaseUrl.replace(/\/$/, '') + '/assets/';
|
|
103
|
+
files.forEach(f => {
|
|
104
|
+
if (f.content && ASSETS_RE.test(f.content)) {
|
|
105
|
+
f.content = f.content.replace(ASSETS_RE, assetsBase);
|
|
106
|
+
}
|
|
107
|
+
ASSETS_RE.lastIndex = 0;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ─── Tab Switching ────────────────────────────────────────────────────────────
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Returns an `activateTab` callback that mirrors the original buildShell closure.
|
|
115
|
+
* Operates purely on the pre-rendered HTML — no DOM construction needed.
|
|
116
|
+
*/
|
|
117
|
+
function buildActivateTab(
|
|
118
|
+
navbar: HTMLElement,
|
|
119
|
+
container: HTMLElement,
|
|
120
|
+
fsBtn: HTMLElement | null,
|
|
121
|
+
exampleTabId: string,
|
|
122
|
+
): (tab: HTMLElement) => void {
|
|
123
|
+
return function activateTab(tab: HTMLElement) {
|
|
124
|
+
navbar.querySelectorAll<HTMLElement>('.code-view-tab, .code-view-tab--active').forEach(t => {
|
|
125
|
+
t.classList.remove('code-view-tab--active');
|
|
126
|
+
t.classList.add('code-view-tab');
|
|
127
|
+
});
|
|
128
|
+
container.querySelectorAll<HTMLElement>('.code-view-tab-content').forEach(c => {
|
|
129
|
+
c.style.display = 'none';
|
|
130
|
+
});
|
|
131
|
+
tab.classList.remove('code-view-tab');
|
|
132
|
+
tab.classList.add('code-view-tab--active');
|
|
133
|
+
const pane = container.querySelector<HTMLElement>('#' + tab.dataset.tabId);
|
|
134
|
+
if (pane) pane.style.display = '';
|
|
135
|
+
if (fsBtn) {
|
|
136
|
+
fsBtn.style.visibility = tab.dataset.tabId === exampleTabId ? 'visible' : 'hidden';
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ─── Code Tab Rendering ───────────────────────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
function addCodeTab(
|
|
144
|
+
navbar: HTMLElement,
|
|
145
|
+
container: HTMLElement,
|
|
146
|
+
activateTab: (tab: HTMLElement) => void,
|
|
147
|
+
widgetIndex: number,
|
|
148
|
+
file: SampleFile,
|
|
149
|
+
tabIndex: number,
|
|
150
|
+
): void {
|
|
151
|
+
const lang = file.fileExtension === 'js' ? 'javascript' : (file.fileExtension || 'text');
|
|
152
|
+
const tabId = `cv-${widgetIndex}-code-${tabIndex}`;
|
|
153
|
+
|
|
154
|
+
const tab = document.createElement('div');
|
|
155
|
+
tab.className = 'code-view-tab';
|
|
156
|
+
tab.textContent = file.fileHeader.toUpperCase();
|
|
157
|
+
tab.dataset.tabId = tabId;
|
|
158
|
+
tab.addEventListener('click', () => activateTab(tab));
|
|
159
|
+
// Insert before the last child (the fullscreen span stays last in the navbar).
|
|
160
|
+
navbar.insertBefore(tab, navbar.lastElementChild);
|
|
161
|
+
|
|
162
|
+
const pane = document.createElement('div');
|
|
163
|
+
pane.id = tabId;
|
|
164
|
+
pane.className = 'code-view-tab-content';
|
|
165
|
+
pane.style.display = 'none';
|
|
166
|
+
|
|
167
|
+
const pre = document.createElement('pre');
|
|
168
|
+
pre.className = 'code-wrapper';
|
|
169
|
+
|
|
170
|
+
const code = document.createElement('code');
|
|
171
|
+
code.className = `language-${lang}`;
|
|
172
|
+
code.textContent = file.content;
|
|
173
|
+
|
|
174
|
+
const copyBtn = document.createElement('button');
|
|
175
|
+
copyBtn.className = 'cv-hljs-code-copy hidden';
|
|
176
|
+
copyBtn.textContent = 'COPY CODE';
|
|
177
|
+
copyBtn.addEventListener('click', () => {
|
|
178
|
+
navigator.clipboard.writeText(file.content).then(() => {
|
|
179
|
+
const orig = copyBtn.textContent!;
|
|
180
|
+
copyBtn.textContent = 'COPIED!';
|
|
181
|
+
setTimeout(() => { copyBtn.textContent = orig; }, 1500);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
pre.addEventListener('mouseenter', () => copyBtn.classList.remove('hidden'));
|
|
186
|
+
pre.addEventListener('mouseleave', () => copyBtn.classList.add('hidden'));
|
|
187
|
+
|
|
188
|
+
pre.appendChild(code);
|
|
189
|
+
pre.appendChild(copyBtn);
|
|
190
|
+
pane.appendChild(pre);
|
|
191
|
+
container.appendChild(pane);
|
|
192
|
+
|
|
193
|
+
if (typeof (window as any).hljs !== 'undefined') {
|
|
194
|
+
(window as any).hljs.highlightElement(code);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ─── Footer / Live Edit Buttons ───────────────────────────────────────────────
|
|
199
|
+
|
|
200
|
+
function addFooter(
|
|
201
|
+
widget: HTMLElement,
|
|
202
|
+
explicitEditor: string | null,
|
|
203
|
+
onStackblitz: (() => void) | null,
|
|
204
|
+
onCodeSandbox: (() => void) | null,
|
|
205
|
+
): void {
|
|
206
|
+
const footer = document.createElement('div');
|
|
207
|
+
footer.className = 'editing-buttons-container';
|
|
208
|
+
|
|
209
|
+
const label = document.createElement('span');
|
|
210
|
+
label.className = 'editing-label';
|
|
211
|
+
label.textContent = 'Edit in: ';
|
|
212
|
+
footer.appendChild(label);
|
|
213
|
+
|
|
214
|
+
if ((!explicitEditor || explicitEditor === 'stackblitz') && onStackblitz) {
|
|
215
|
+
const btn = document.createElement('button');
|
|
216
|
+
btn.className = 'stackblitz-btn';
|
|
217
|
+
btn.textContent = 'StackBlitz';
|
|
218
|
+
btn.style.fontWeight = '500';
|
|
219
|
+
btn.addEventListener('click', onStackblitz);
|
|
220
|
+
footer.appendChild(btn);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if ((!explicitEditor || explicitEditor === 'csb') && onCodeSandbox) {
|
|
224
|
+
const btn = document.createElement('button');
|
|
225
|
+
btn.className = 'codesandbox-btn';
|
|
226
|
+
btn.textContent = 'CodeSandbox';
|
|
227
|
+
btn.style.fontWeight = '500';
|
|
228
|
+
btn.addEventListener('click', onCodeSandbox);
|
|
229
|
+
footer.appendChild(btn);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
widget.appendChild(footer);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ─── Angular Code Service ─────────────────────────────────────────────────────
|
|
236
|
+
|
|
237
|
+
class AngularCodeService {
|
|
238
|
+
async init(ctx: WidgetContext): Promise<void> {
|
|
239
|
+
const { widget, iframeSrc, demosBaseUrl, widgetIndex, navbar, container, activateTab } = ctx;
|
|
240
|
+
const samplePath = getSamplePath(iframeSrc, demosBaseUrl);
|
|
241
|
+
const isDv = isDvSample(samplePath);
|
|
242
|
+
const assetDir = isDv ? 'code-viewer' : 'samples';
|
|
243
|
+
const explicitEditor = isDv ? 'csb' : null;
|
|
244
|
+
|
|
245
|
+
const fallbackStackblitz = isDv ? null : () => this._openStackBlitzUrl(samplePath, demosBaseUrl);
|
|
246
|
+
const fallbackCodeSandbox = () => this._openCodeSandboxUrl(samplePath, demosBaseUrl);
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
const metaRes = await fetch(
|
|
250
|
+
`${demosBaseUrl}/assets/samples/meta.json?t=${Date.now()}`,
|
|
251
|
+
{ credentials: 'omit' },
|
|
252
|
+
);
|
|
253
|
+
if (!metaRes.ok) throw new Error(`meta.json ${metaRes.status}`);
|
|
254
|
+
const { generationTimeStamp } = await metaRes.json();
|
|
255
|
+
|
|
256
|
+
const sampleJsonPath = isDv
|
|
257
|
+
? `${demosBaseUrl}/assets/${assetDir}/${samplePath}.json`
|
|
258
|
+
: `${demosBaseUrl}/assets/${assetDir}/${samplePath.replace('/', '--')}.json`;
|
|
259
|
+
|
|
260
|
+
const [sampleRes, sharedRes] = await Promise.all([
|
|
261
|
+
fetch(`${sampleJsonPath}?t=${generationTimeStamp}`, { credentials: 'omit' }),
|
|
262
|
+
fetch(`${demosBaseUrl}/assets/samples/shared.json?t=${generationTimeStamp}`, { credentials: 'omit' }),
|
|
263
|
+
]);
|
|
264
|
+
if (!sampleRes.ok) throw new Error(`sample JSON ${sampleRes.status}`);
|
|
265
|
+
|
|
266
|
+
const [sampleData, sharedData] = await Promise.all([
|
|
267
|
+
sampleRes.json(),
|
|
268
|
+
sharedRes.ok ? sharedRes.json() : Promise.resolve(null),
|
|
269
|
+
]);
|
|
270
|
+
|
|
271
|
+
replaceRelativeAssetUrls(sampleData.sampleFiles || [], demosBaseUrl);
|
|
272
|
+
if (sharedData) replaceRelativeAssetUrls(sharedData.files || [], demosBaseUrl);
|
|
273
|
+
|
|
274
|
+
(sampleData.sampleFiles || [] as SampleFile[])
|
|
275
|
+
.filter((f: SampleFile) => f.isMain)
|
|
276
|
+
.sort((a: SampleFile, b: SampleFile) =>
|
|
277
|
+
SAMPLES_ORDER.indexOf(a.fileHeader) - SAMPLES_ORDER.indexOf(b.fileHeader))
|
|
278
|
+
.forEach((file: SampleFile, i: number) =>
|
|
279
|
+
addCodeTab(navbar, container, activateTab, widgetIndex, file, i));
|
|
280
|
+
|
|
281
|
+
const onStackblitz = isDv ? null : () => this._openInStackBlitz(sampleData, sharedData);
|
|
282
|
+
addFooter(widget, explicitEditor, onStackblitz, fallbackCodeSandbox);
|
|
283
|
+
|
|
284
|
+
} catch (err: any) {
|
|
285
|
+
console.warn('[sample-widget] Could not fetch Angular sample files:', err.message);
|
|
286
|
+
addFooter(widget, explicitEditor, fallbackStackblitz, fallbackCodeSandbox);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private async _openInStackBlitz(sampleData: any, sharedData: any): Promise<void> {
|
|
291
|
+
let sdk: any;
|
|
292
|
+
try {
|
|
293
|
+
sdk = await this._loadStackBlitzSdk();
|
|
294
|
+
} catch (e: any) {
|
|
295
|
+
console.warn('[sample-widget] StackBlitz SDK failed to load:', e.message);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const files: Record<string, string> = {};
|
|
299
|
+
(sharedData?.files || []).forEach((f: any) => {
|
|
300
|
+
if (f.path) files[f.path.replace(/^\.\//, '')] = f.content || '';
|
|
301
|
+
});
|
|
302
|
+
(sampleData.sampleFiles || []).forEach((f: any) => {
|
|
303
|
+
if (f.path) files[f.path.replace(/^\.\//, '')] = f.content || '';
|
|
304
|
+
});
|
|
305
|
+
sdk.openProject(
|
|
306
|
+
{
|
|
307
|
+
title: 'Infragistics Angular Components',
|
|
308
|
+
description: 'Auto-generated from Infragistics Angular Docs',
|
|
309
|
+
template: 'node',
|
|
310
|
+
files,
|
|
311
|
+
tags: ['angular', 'material', 'cdk', 'web', 'example'],
|
|
312
|
+
},
|
|
313
|
+
{ openFile: 'src/app/app.component.ts' },
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private _loadStackBlitzSdk(): Promise<any> {
|
|
318
|
+
if ((window as any).StackBlitzSDK) return Promise.resolve((window as any).StackBlitzSDK);
|
|
319
|
+
return new Promise((resolve, reject) => {
|
|
320
|
+
const s = document.createElement('script');
|
|
321
|
+
s.src = 'https://unpkg.com/@stackblitz/sdk/bundles/sdk.umd.js';
|
|
322
|
+
s.onload = () => resolve((window as any).StackBlitzSDK);
|
|
323
|
+
s.onerror = () => reject(new Error('Failed to load StackBlitz SDK'));
|
|
324
|
+
document.head.appendChild(s);
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private _openCodeSandboxUrl(samplePath: string, demosBaseUrl: string): void {
|
|
329
|
+
const isDv = isDvSample(samplePath);
|
|
330
|
+
const branch = getRepoBranch(demosBaseUrl, isDv);
|
|
331
|
+
const repo = isDv ? 'igniteui-angular-examples' : 'igniteui-live-editing-samples';
|
|
332
|
+
const segs = new URL(demosBaseUrl).pathname.replace(/\/$/, '').split('/').filter(Boolean);
|
|
333
|
+
const prefix = isDv ? 'samples/' : (segs.pop() || 'angular-demos') + '/';
|
|
334
|
+
window.open(
|
|
335
|
+
`https://codesandbox.io/p/sandbox/github/IgniteUI/${repo}/tree/${branch}/${prefix}${samplePath}`,
|
|
336
|
+
'_blank',
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private _openStackBlitzUrl(samplePath: string, demosBaseUrl: string): void {
|
|
341
|
+
const isDv = isDvSample(samplePath);
|
|
342
|
+
const branch = getRepoBranch(demosBaseUrl, isDv);
|
|
343
|
+
const repo = isDv ? 'igniteui-angular-examples' : 'igniteui-live-editing-samples';
|
|
344
|
+
const segs = new URL(demosBaseUrl).pathname.replace(/\/$/, '').split('/').filter(Boolean);
|
|
345
|
+
const prefix = isDv ? 'samples/' : (segs.pop() || 'angular-demos') + '/';
|
|
346
|
+
window.open(
|
|
347
|
+
`https://stackblitz.com/github/IgniteUI/${repo}/tree/${branch}/${prefix}${samplePath}`,
|
|
348
|
+
'_blank',
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// ─── Xplat Code Service ───────────────────────────────────────────────────────
|
|
354
|
+
|
|
355
|
+
class XplatCodeService {
|
|
356
|
+
private platform: string;
|
|
357
|
+
private samplesOrder: string[];
|
|
358
|
+
private codeBase: string;
|
|
359
|
+
private enableLiveEditing: boolean;
|
|
360
|
+
|
|
361
|
+
constructor(platform: string) {
|
|
362
|
+
this.platform = platform;
|
|
363
|
+
this.samplesOrder = XPLAT_SAMPLES_ORDER[platform] || ['ts', 'html', 'css'];
|
|
364
|
+
this.codeBase = XPLAT_CODE_BASE[platform] || '/code-viewer/';
|
|
365
|
+
this.enableLiveEditing = platform !== 'blazor';
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async init(ctx: WidgetContext): Promise<void> {
|
|
369
|
+
const { widget, iframeSrc, demosBaseUrl, widgetIndex, navbar, container, activateTab, githubSrc } = ctx;
|
|
370
|
+
const samplePath = getSamplePath(iframeSrc, demosBaseUrl);
|
|
371
|
+
const addXplatFooter = () => {
|
|
372
|
+
if (!this.enableLiveEditing || !githubSrc) return;
|
|
373
|
+
addFooter(widget, 'csb', null, () => this._openCodeSandbox(githubSrc, demosBaseUrl));
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
try {
|
|
377
|
+
// Blazor's local dev server uses a self-signed cert on a non-4200 port,
|
|
378
|
+
// causing CORS errors. Use a relative URL so Vite's proxy handles it.
|
|
379
|
+
const jsonUrl = this.platform === 'blazor'
|
|
380
|
+
? `${this.codeBase}${samplePath}.json`
|
|
381
|
+
: `${demosBaseUrl}${this.codeBase}${samplePath}.json`;
|
|
382
|
+
|
|
383
|
+
const res = await fetch(jsonUrl, { credentials: 'omit' });
|
|
384
|
+
if (!res.ok) throw new Error(`xplat sample JSON ${res.status}`);
|
|
385
|
+
const data = await res.json();
|
|
386
|
+
|
|
387
|
+
(data.sampleFiles || [] as SampleFile[])
|
|
388
|
+
.filter((f: SampleFile) => f.isMain)
|
|
389
|
+
.sort((a: SampleFile, b: SampleFile) =>
|
|
390
|
+
this.samplesOrder.indexOf(a.fileHeader) - this.samplesOrder.indexOf(b.fileHeader))
|
|
391
|
+
.forEach((file: SampleFile, i: number) =>
|
|
392
|
+
addCodeTab(navbar, container, activateTab, widgetIndex, file, i));
|
|
393
|
+
|
|
394
|
+
addXplatFooter();
|
|
395
|
+
} catch (err: any) {
|
|
396
|
+
console.warn('[sample-widget] Could not fetch xplat sample files:', err.message);
|
|
397
|
+
addXplatFooter();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
private _openCodeSandbox(githubSrc: string, demosBaseUrl: string): void {
|
|
402
|
+
const branch = getXplatBranch(demosBaseUrl);
|
|
403
|
+
window.open(
|
|
404
|
+
`https://codesandbox.io/p/sandbox/github/IgniteUI/igniteui-${this.platform}-examples/tree/${branch}/samples/${githubSrc}`,
|
|
405
|
+
'_blank',
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// ─── Entry Point ──────────────────────────────────────────────────────────────
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Initialize all Sample.astro widgets on the page.
|
|
414
|
+
*
|
|
415
|
+
* Called once per page by the deduplicated <script> in Sample.astro.
|
|
416
|
+
* Processes every `.code-view[data-platform]` element — the `data-platform`
|
|
417
|
+
* attribute is what distinguishes pre-rendered Sample.astro widgets from
|
|
418
|
+
* `.ig-code-view` elements managed by code-view.js.
|
|
419
|
+
*/
|
|
420
|
+
export function initSampleWidgets(): void {
|
|
421
|
+
// Serial iframe-load queue. Loading all iframes simultaneously causes the
|
|
422
|
+
// browser to parse and execute many JS bundles at once, freezing the main
|
|
423
|
+
// thread. Instead we load one iframe at a time: next starts only after the
|
|
424
|
+
// current fires its `load` event.
|
|
425
|
+
const iframeQueue: Array<{ iframe: HTMLIFrameElement; samplePane: HTMLElement }> = [];
|
|
426
|
+
let iframeLoading = false;
|
|
427
|
+
|
|
428
|
+
function processIframeQueue() {
|
|
429
|
+
if (iframeLoading || iframeQueue.length === 0) return;
|
|
430
|
+
iframeLoading = true;
|
|
431
|
+
const { iframe, samplePane } = iframeQueue.shift()!;
|
|
432
|
+
|
|
433
|
+
iframe.addEventListener('load', () => {
|
|
434
|
+
samplePane.classList.remove('loading');
|
|
435
|
+
iframeLoading = false;
|
|
436
|
+
processIframeQueue();
|
|
437
|
+
}, { once: true });
|
|
438
|
+
|
|
439
|
+
iframe.src = iframe.dataset.src!;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
document.querySelectorAll<HTMLElement>('.code-view[data-platform]').forEach((widget, index) => {
|
|
443
|
+
// Skip widgets already initialized (e.g. Astro client router restoring a cached page).
|
|
444
|
+
if (widget.dataset.widgetInitialized) return;
|
|
445
|
+
widget.dataset.widgetInitialized = 'true';
|
|
446
|
+
|
|
447
|
+
const iframeSrc = widget.dataset.iframeSrc || '';
|
|
448
|
+
const demosBaseUrl = widget.dataset.demosBaseUrl || '';
|
|
449
|
+
const githubSrc = widget.dataset.githubSrc || '';
|
|
450
|
+
const platform = normalizePlatform(widget.dataset.platform || 'angular');
|
|
451
|
+
|
|
452
|
+
if (!iframeSrc) return;
|
|
453
|
+
|
|
454
|
+
const navbar = widget.querySelector<HTMLElement>('.code-view-navbar');
|
|
455
|
+
const container = widget.querySelector<HTMLElement>('.code-views-container');
|
|
456
|
+
const samplePane = widget.querySelector<HTMLElement>('.sample-container');
|
|
457
|
+
const iframe = widget.querySelector<HTMLIFrameElement>('iframe');
|
|
458
|
+
const fsBtn = widget.querySelector<HTMLElement>('.fs-button-container');
|
|
459
|
+
const exampleTab = widget.querySelector<HTMLElement>('.code-view-tab--active');
|
|
460
|
+
|
|
461
|
+
if (!navbar || !container) return;
|
|
462
|
+
|
|
463
|
+
const exampleTabId = exampleTab?.dataset.tabId ?? `${widget.id}-example`;
|
|
464
|
+
const activateTab = buildActivateTab(navbar, container, fsBtn, exampleTabId);
|
|
465
|
+
|
|
466
|
+
// Wire click handler onto the pre-rendered EXAMPLE tab.
|
|
467
|
+
exampleTab?.addEventListener('click', () => activateTab(exampleTab));
|
|
468
|
+
|
|
469
|
+
// Fullscreen button: open the iframe URL in a new tab.
|
|
470
|
+
if (fsBtn && iframe) {
|
|
471
|
+
fsBtn.addEventListener('click', () => {
|
|
472
|
+
const src = iframe.src || iframe.dataset.src || '';
|
|
473
|
+
if (src) window.open(src + (src.includes('?') ? '&' : '?') + 'nav=0', '_blank');
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Enqueue iframe loading.
|
|
478
|
+
if (iframe && samplePane) {
|
|
479
|
+
const enqueue = () => {
|
|
480
|
+
iframeQueue.push({ iframe, samplePane });
|
|
481
|
+
processIframeQueue();
|
|
482
|
+
};
|
|
483
|
+
if (index === 0) {
|
|
484
|
+
requestAnimationFrame(enqueue);
|
|
485
|
+
} else {
|
|
486
|
+
const visObs = new IntersectionObserver(entries => {
|
|
487
|
+
if (entries[0].isIntersecting) { visObs.disconnect(); enqueue(); }
|
|
488
|
+
});
|
|
489
|
+
visObs.observe(samplePane);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (!demosBaseUrl) return;
|
|
494
|
+
|
|
495
|
+
const service = isXplatPlatform(platform)
|
|
496
|
+
? new XplatCodeService(platform)
|
|
497
|
+
: new AngularCodeService();
|
|
498
|
+
|
|
499
|
+
const ctx: WidgetContext = {
|
|
500
|
+
widget, iframeSrc, demosBaseUrl, widgetIndex: index, navbar, container, activateTab, githubSrc,
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
// Fetch code-tab data only when the widget becomes visible.
|
|
504
|
+
const startFetch = () => service.init(ctx);
|
|
505
|
+
if (index === 0) {
|
|
506
|
+
typeof requestIdleCallback !== 'undefined'
|
|
507
|
+
? requestIdleCallback(startFetch)
|
|
508
|
+
: requestAnimationFrame(startFetch);
|
|
509
|
+
} else {
|
|
510
|
+
const fetchObs = new IntersectionObserver(entries => {
|
|
511
|
+
if (entries[0].isIntersecting) { fetchObs.disconnect(); startFetch(); }
|
|
512
|
+
});
|
|
513
|
+
fetchObs.observe(samplePane ?? widget);
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
package/src/sidebar.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sidebar.ts
|
|
3
|
+
*
|
|
4
|
+
* Builds an Astro Starlight sidebar from a YAML or JSON TOC file.
|
|
5
|
+
* Source-agnostic: consuming repos pass their own paths.
|
|
6
|
+
*
|
|
7
|
+
* Usage in a consuming repo's astro.config.ts:
|
|
8
|
+
*
|
|
9
|
+
* import { buildSidebarFromToc } from 'docs-template/sidebar';
|
|
10
|
+
*
|
|
11
|
+
* const sidebar = buildSidebarFromToc({
|
|
12
|
+
* tocPath: './node_modules/my-docs-source/toc.yml', // YAML or JSON
|
|
13
|
+
* docsDir: './node_modules/my-docs-source/en/components',
|
|
14
|
+
* // exclude: [/^internal\//], // optional extra exclude patterns
|
|
15
|
+
* });
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import * as yaml from 'js-yaml';
|
|
21
|
+
import type { SidebarEntry, SidebarGroup, SidebarLink } from './lib/sidebar/types';
|
|
22
|
+
|
|
23
|
+
// Re-export so consumers (astro.config.ts in child sites) can import the
|
|
24
|
+
// canonical types from the same module they already use.
|
|
25
|
+
export type { SidebarEntry, SidebarGroup, SidebarLink } from './lib/sidebar/types';
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Internal types
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
interface TocItem {
|
|
32
|
+
name?: string;
|
|
33
|
+
href?: string;
|
|
34
|
+
header?: boolean;
|
|
35
|
+
items?: TocItem[];
|
|
36
|
+
new?: boolean;
|
|
37
|
+
preview?: boolean;
|
|
38
|
+
updated?: boolean;
|
|
39
|
+
premium?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// Internal helpers
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
function docExists(docsDir: string, href: string, exclude: RegExp[]): boolean {
|
|
47
|
+
if (!href) return false;
|
|
48
|
+
if (exclude?.some((p) => p.test(href))) return false;
|
|
49
|
+
// Check the href as-is, then also with .md ↔ .mdx swapped so that toc.json
|
|
50
|
+
// entries like "charts/chart-features.md" resolve even when the file on disk
|
|
51
|
+
// is "charts/chart-features.mdx" (and vice-versa).
|
|
52
|
+
if (fs.existsSync(path.join(docsDir, href))) return true;
|
|
53
|
+
const alt = href.endsWith('.mdx')
|
|
54
|
+
? href.slice(0, -4) + '.md'
|
|
55
|
+
: href.endsWith('.md') ? href.slice(0, -3) + '.mdx' : null;
|
|
56
|
+
return alt !== null && fs.existsSync(path.join(docsDir, alt));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function hrefToSlug(href: string): string {
|
|
60
|
+
if (!href) return '';
|
|
61
|
+
let slug = href
|
|
62
|
+
.replace(/\\/g, '/')
|
|
63
|
+
.replace(/\.(md|mdx)$/i, '')
|
|
64
|
+
.toLowerCase();
|
|
65
|
+
slug = slug.replace(/\/index$/, '');
|
|
66
|
+
return slug === 'index' ? '' : slug;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Initial collapsed state by depth:
|
|
71
|
+
* • depth 0 (root groups, incl. `header:true` sections) → `collapsed: false`
|
|
72
|
+
* • depth ≥ 1 (nested groups) → `collapsed: true`
|
|
73
|
+
*/
|
|
74
|
+
function collapsedForDepth(depth: number): boolean {
|
|
75
|
+
return depth > 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function convertTocItem(
|
|
79
|
+
docsDir: string,
|
|
80
|
+
item: TocItem,
|
|
81
|
+
exclude: RegExp[],
|
|
82
|
+
depth: number,
|
|
83
|
+
): SidebarEntry | null {
|
|
84
|
+
if (!item.name) return null;
|
|
85
|
+
|
|
86
|
+
if (item.items && item.items.length > 0) {
|
|
87
|
+
const group: SidebarGroup = {
|
|
88
|
+
label: item.name,
|
|
89
|
+
items: [],
|
|
90
|
+
collapsed: collapsedForDepth(depth),
|
|
91
|
+
};
|
|
92
|
+
if (item.href && docExists(docsDir, item.href, exclude)) {
|
|
93
|
+
group.items.push({ label: 'Overview', slug: hrefToSlug(item.href) });
|
|
94
|
+
}
|
|
95
|
+
for (const child of item.items) {
|
|
96
|
+
const entry = convertTocItem(docsDir, child, exclude, depth + 1);
|
|
97
|
+
if (entry) group.items.push(entry);
|
|
98
|
+
}
|
|
99
|
+
return group.items.length > 0 ? group : null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (item.href) {
|
|
103
|
+
if (!docExists(docsDir, item.href, exclude)) return null;
|
|
104
|
+
const entry: SidebarLink = { label: item.name, slug: hrefToSlug(item.href) };
|
|
105
|
+
// Status badge — only one slot available in Starlight, priority order:
|
|
106
|
+
if (item.new) entry.badge = { text: 'New', variant: 'success' };
|
|
107
|
+
else if (item.preview) entry.badge = { text: 'Preview', variant: 'caution' };
|
|
108
|
+
else if (item.updated) entry.badge = { text: 'Updated', variant: 'note' };
|
|
109
|
+
// Premium is rendered via data-premium attr so it can coexist with a
|
|
110
|
+
// status badge (CSS ::after pseudo-element adds the star icon).
|
|
111
|
+
if (item.premium) {
|
|
112
|
+
entry.attrs = { 'data-premium': 'true' };
|
|
113
|
+
// Only use the badge slot for premium when no status badge is shown.
|
|
114
|
+
if (!entry.badge) entry.badge = { text: 'Premium', variant: 'tip' };
|
|
115
|
+
}
|
|
116
|
+
return entry;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ---------------------------------------------------------------------------
|
|
123
|
+
// Public API
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
export interface BuildSidebarFromTocOptions {
|
|
127
|
+
/** Absolute path to the TOC file (.yml or .json). */
|
|
128
|
+
tocPath: string;
|
|
129
|
+
/** Absolute path to the Markdown docs directory. */
|
|
130
|
+
docsDir: string;
|
|
131
|
+
/** Extra patterns to exclude (matched against the `href`). */
|
|
132
|
+
exclude?: RegExp[];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Reads a YAML or JSON TOC file and converts it to a Starlight sidebar array.
|
|
137
|
+
*/
|
|
138
|
+
export function buildSidebarFromToc({ tocPath, docsDir, exclude = [] }: BuildSidebarFromTocOptions): SidebarEntry[] {
|
|
139
|
+
if (!tocPath || !fs.existsSync(tocPath)) return [];
|
|
140
|
+
const tocRaw = fs.readFileSync(tocPath, 'utf-8');
|
|
141
|
+
const tocItems = tocPath.endsWith('.json') ? JSON.parse(tocRaw) : yaml.load(tocRaw) as TocItem[];
|
|
142
|
+
|
|
143
|
+
const sidebar: SidebarEntry[] = [];
|
|
144
|
+
let currentGroup: SidebarGroup | null = null;
|
|
145
|
+
|
|
146
|
+
for (const item of tocItems) {
|
|
147
|
+
if (item.header) {
|
|
148
|
+
if (currentGroup) sidebar.push(currentGroup);
|
|
149
|
+
// Root-level header section — open by default.
|
|
150
|
+
currentGroup = { label: item.name!, items: [], collapsed: collapsedForDepth(0) };
|
|
151
|
+
if (item.href && docExists(docsDir, item.href, exclude)) {
|
|
152
|
+
currentGroup.items.push({ label: 'Overview', slug: hrefToSlug(item.href) });
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
// Items inside a header section are at depth 1 (nested);
|
|
157
|
+
// items outside any header section are at depth 0 (root).
|
|
158
|
+
const depth = currentGroup ? 1 : 0;
|
|
159
|
+
const entry = convertTocItem(docsDir, item, exclude, depth);
|
|
160
|
+
if (!entry) continue;
|
|
161
|
+
if (currentGroup) currentGroup.items.push(entry);
|
|
162
|
+
else sidebar.push(entry);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (currentGroup) sidebar.push(currentGroup);
|
|
166
|
+
return sidebar;
|
|
167
|
+
}
|