|
| 1 | +# AAC Pageset Quickstart (Node + Browser) |
| 2 | + |
| 3 | +This guide shows two simple ways to generate or convert AAC pagesets using `aac-processors`: |
| 4 | + |
| 5 | +- Node.js: full conversion between formats (read + write) |
| 6 | +- Browser: generate or export to OBF/OBZ in-memory (downloadable) |
| 7 | + |
| 8 | +If you need lossless conversion in the browser, use a Node/worker service for the save step (file I/O is required for most formats). |
| 9 | + |
| 10 | +## Node.js: Convert and Generate Pagesets |
| 11 | + |
| 12 | +### Install |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install aac-processors |
| 16 | +``` |
| 17 | + |
| 18 | +### Convert a Gridset to OBF |
| 19 | + |
| 20 | +```ts |
| 21 | +import { getProcessor, ObfProcessor } from 'aac-processors'; |
| 22 | + |
| 23 | +async function convertGridsetToObf() { |
| 24 | + const sourcePath = './input/example.gridset'; |
| 25 | + const targetPath = './output/example.obf'; |
| 26 | + |
| 27 | + const sourceProcessor = getProcessor(sourcePath); // GridsetProcessor |
| 28 | + const tree = await sourceProcessor.loadIntoTree(sourcePath); |
| 29 | + |
| 30 | + const obf = new ObfProcessor(); |
| 31 | + await obf.saveFromTree(tree, targetPath); |
| 32 | + |
| 33 | + console.log('Saved:', targetPath); |
| 34 | +} |
| 35 | + |
| 36 | +convertGridsetToObf().catch(console.error); |
| 37 | +``` |
| 38 | + |
| 39 | +### Generate a Simple Pageset and Save as OBZ |
| 40 | + |
| 41 | +```ts |
| 42 | +import { AACTree, AACPage, AACButton, ObfProcessor } from 'aac-processors'; |
| 43 | + |
| 44 | +async function generateObz() { |
| 45 | + const tree = new AACTree(); |
| 46 | + tree.metadata = { name: 'Starter Demo', locale: 'en' }; |
| 47 | + |
| 48 | + const hello = new AACButton({ id: 'hello', label: 'Hello', message: 'Hello' }); |
| 49 | + const thanks = new AACButton({ id: 'thanks', label: 'Thanks', message: 'Thank you' }); |
| 50 | + |
| 51 | + const home = new AACPage({ |
| 52 | + id: 'home', |
| 53 | + name: 'Home', |
| 54 | + buttons: [hello, thanks], |
| 55 | + grid: [[hello, thanks]], |
| 56 | + }); |
| 57 | + |
| 58 | + tree.addPage(home); |
| 59 | + tree.rootId = 'home'; |
| 60 | + |
| 61 | + const obf = new ObfProcessor(); |
| 62 | + await obf.saveFromTree(tree, './output/starter.obz'); |
| 63 | + |
| 64 | + console.log('Saved: ./output/starter.obz'); |
| 65 | +} |
| 66 | + |
| 67 | +generateObz().catch(console.error); |
| 68 | +``` |
| 69 | + |
| 70 | +## Browser: Generate or Convert to OBF/OBZ |
| 71 | + |
| 72 | +In the browser you can still parse files and build an `AACTree`, but most processors cannot write to disk (no `fs`). |
| 73 | +The example below generates an `AACTree`, converts it to OBF JSON in-memory, and downloads it. |
| 74 | + |
| 75 | +If you need full conversions in a browser app, do the save step in Node (server or worker). |
| 76 | + |
| 77 | +### Generate a Pageset and Download as OBF |
| 78 | + |
| 79 | +```ts |
| 80 | +import { AACTree, AACPage, AACButton, ObfProcessor } from 'aac-processors'; |
| 81 | + |
| 82 | +function downloadBlob(data: BlobPart, filename: string, type: string) { |
| 83 | + const blob = new Blob([data], { type }); |
| 84 | + const url = URL.createObjectURL(blob); |
| 85 | + const a = document.createElement('a'); |
| 86 | + a.href = url; |
| 87 | + a.download = filename; |
| 88 | + a.click(); |
| 89 | + URL.revokeObjectURL(url); |
| 90 | +} |
| 91 | + |
| 92 | +function buildSampleTree() { |
| 93 | + const tree = new AACTree(); |
| 94 | + tree.metadata = { name: 'Browser Demo', locale: 'en' }; |
| 95 | + |
| 96 | + const hello = new AACButton({ id: 'hello', label: 'Hello', message: 'Hello' }); |
| 97 | + const yes = new AACButton({ id: 'yes', label: 'Yes', message: 'Yes' }); |
| 98 | + |
| 99 | + const home = new AACPage({ |
| 100 | + id: 'home', |
| 101 | + name: 'Home', |
| 102 | + buttons: [hello, yes], |
| 103 | + grid: [[hello, yes]], |
| 104 | + }); |
| 105 | + |
| 106 | + tree.addPage(home); |
| 107 | + tree.rootId = 'home'; |
| 108 | + return tree; |
| 109 | +} |
| 110 | + |
| 111 | +async function exportObf(tree: AACTree) { |
| 112 | + // This mirrors the browser demo approach: create OBF JSON and download. |
| 113 | + // ObfProcessor.saveFromTree writes to disk, so we build a board in-memory. |
| 114 | + const obf = new ObfProcessor() as ObfProcessor & { |
| 115 | + createObfBoardFromPage?: (page: AACPage, fallbackName: string, metadata?: AACTree['metadata']) => any; |
| 116 | + }; |
| 117 | + |
| 118 | + const rootPage = tree.rootId ? tree.getPage(tree.rootId) : Object.values(tree.pages)[0]; |
| 119 | + const board = obf.createObfBoardFromPage |
| 120 | + ? obf.createObfBoardFromPage(rootPage!, 'Board', tree.metadata) |
| 121 | + : { |
| 122 | + format: 'open-board-0.1', |
| 123 | + id: rootPage?.id ?? 'board', |
| 124 | + name: rootPage?.name ?? 'Board', |
| 125 | + locale: tree.metadata?.locale || 'en', |
| 126 | + grid: { rows: 1, columns: rootPage?.buttons.length ?? 0, order: [] }, |
| 127 | + buttons: (rootPage?.buttons || []).map((button) => ({ |
| 128 | + id: button.id, |
| 129 | + label: button.label, |
| 130 | + vocalization: button.message || button.label, |
| 131 | + })), |
| 132 | + }; |
| 133 | + |
| 134 | + const json = JSON.stringify(board, null, 2); |
| 135 | + downloadBlob(json, 'browser-demo.obf', 'application/json'); |
| 136 | +} |
| 137 | + |
| 138 | +const tree = buildSampleTree(); |
| 139 | +exportObf(tree); |
| 140 | +``` |
| 141 | + |
| 142 | +### Convert an Uploaded Pageset to OBZ (Browser) |
| 143 | + |
| 144 | +This uses the same idea as the Vite demo: parse any supported file into a tree, |
| 145 | +then export OBF/OBZ in-memory and download. |
| 146 | + |
| 147 | +```ts |
| 148 | +import { getProcessor, ObfProcessor, type AACTree, type AACPage } from 'aac-processors'; |
| 149 | +import JSZip from 'jszip'; |
| 150 | + |
| 151 | +async function convertToObz(file: File) { |
| 152 | + const extension = '.' + file.name.split('.').pop(); |
| 153 | + const processor = getProcessor(extension); |
| 154 | + const buffer = await file.arrayBuffer(); |
| 155 | + const tree = await processor.loadIntoTree(buffer); |
| 156 | + |
| 157 | + const obf = new ObfProcessor() as ObfProcessor & { |
| 158 | + createObfBoardFromPage?: (page: AACPage, fallbackName: string, metadata?: AACTree['metadata']) => any; |
| 159 | + }; |
| 160 | + |
| 161 | + const zip = new JSZip(); |
| 162 | + Object.values(tree.pages).forEach((page) => { |
| 163 | + const board = obf.createObfBoardFromPage |
| 164 | + ? obf.createObfBoardFromPage(page, 'Board', tree.metadata) |
| 165 | + : { format: 'open-board-0.1', id: page.id, name: page.name, grid: { rows: 0, columns: 0, order: [] }, buttons: [] }; |
| 166 | + zip.file(`${page.id}.obf`, JSON.stringify(board, null, 2)); |
| 167 | + }); |
| 168 | + |
| 169 | + const data = await zip.generateAsync({ type: 'uint8array' }); |
| 170 | + const blob = new Blob([data], { type: 'application/zip' }); |
| 171 | + const url = URL.createObjectURL(blob); |
| 172 | + const a = document.createElement('a'); |
| 173 | + a.href = url; |
| 174 | + a.download = 'converted.obz'; |
| 175 | + a.click(); |
| 176 | + URL.revokeObjectURL(url); |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +## Tips |
| 181 | + |
| 182 | +- Use `loadIntoTree()` to normalize different AAC formats into one structure. |
| 183 | +- In Node, `saveFromTree()` lets you write to OBF, OBZ, Gridset, etc. |
| 184 | +- In the browser, build the output in memory and offer it for download. |
| 185 | +- For full-fidelity conversions in the browser, use a server-side endpoint to save files. |
0 commit comments