|
| 1 | +import { RelativePattern, Uri } from 'vscode'; |
| 2 | +import { Indexer } from 'indexer/Indexer'; |
| 3 | +import { IndexerKey } from 'types/indexer'; |
| 4 | +import { Layout } from './types'; |
| 5 | +import { XMLParser } from 'fast-xml-parser'; |
| 6 | +import FileSystem from 'util/FileSystem'; |
| 7 | +import IndexManager from 'indexer/IndexManager'; |
| 8 | +import ThemeIndexer from 'indexer/theme/ThemeIndexer'; |
| 9 | +import { Theme } from 'indexer/theme/types'; |
| 10 | + |
| 11 | +export default class LayoutIndexer extends Indexer<Layout> { |
| 12 | + public static readonly KEY = 'layout'; |
| 13 | + |
| 14 | + private xmlParser: XMLParser; |
| 15 | + |
| 16 | + public constructor() { |
| 17 | + super(); |
| 18 | + |
| 19 | + this.xmlParser = new XMLParser({ |
| 20 | + ignoreAttributes: false, |
| 21 | + attributeNamePrefix: '@_', |
| 22 | + isArray: () => { |
| 23 | + return true; |
| 24 | + }, |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + public getVersion(): number { |
| 29 | + return 1; |
| 30 | + } |
| 31 | + |
| 32 | + public getId(): IndexerKey { |
| 33 | + return LayoutIndexer.KEY; |
| 34 | + } |
| 35 | + |
| 36 | + public getName(): string { |
| 37 | + return 'layout'; |
| 38 | + } |
| 39 | + |
| 40 | + public getPattern(uri: Uri): RelativePattern { |
| 41 | + return new RelativePattern(uri, '**/layout/*.xml'); |
| 42 | + } |
| 43 | + |
| 44 | + public async indexFile(uri: Uri): Promise<Layout | undefined> { |
| 45 | + const xml = await FileSystem.readFile(uri); |
| 46 | + const parsed = this.xmlParser.parse(xml); |
| 47 | + |
| 48 | + const pageNode = Array.isArray(parsed?.page) ? parsed.page[0] : undefined; |
| 49 | + |
| 50 | + if (!pageNode) { |
| 51 | + return undefined; |
| 52 | + } |
| 53 | + |
| 54 | + const getAttr = (node: any, name: string): string | undefined => { |
| 55 | + const value = node?.[`@_${name}`]; |
| 56 | + if (Array.isArray(value)) { |
| 57 | + return value[0]; |
| 58 | + } |
| 59 | + return value; |
| 60 | + }; |
| 61 | + |
| 62 | + const getBoolAttr = (node: any, name: string): boolean | undefined => { |
| 63 | + const raw = getAttr(node, name); |
| 64 | + if (raw === undefined) { |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + const lowered = String(raw).toLowerCase(); |
| 68 | + if (lowered === 'true' || lowered === '1') { |
| 69 | + return true; |
| 70 | + } |
| 71 | + if (lowered === 'false' || lowered === '0') { |
| 72 | + return false; |
| 73 | + } |
| 74 | + return undefined; |
| 75 | + }; |
| 76 | + |
| 77 | + const getNumAttr = (node: any, name: string): number | undefined => { |
| 78 | + const raw = getAttr(node, name); |
| 79 | + if (raw === undefined) { |
| 80 | + return undefined; |
| 81 | + } |
| 82 | + const n = Number(raw); |
| 83 | + return Number.isFinite(n) ? n : undefined; |
| 84 | + }; |
| 85 | + |
| 86 | + const mapUiComponents = (nodes: any[] | undefined) => { |
| 87 | + if (!Array.isArray(nodes)) { |
| 88 | + return []; |
| 89 | + } |
| 90 | + return nodes.map(node => ({ |
| 91 | + name: getAttr(node, 'name'), |
| 92 | + component: getAttr(node, 'component'), |
| 93 | + as: getAttr(node, 'as'), |
| 94 | + ttl: getNumAttr(node, 'ttl'), |
| 95 | + group: getAttr(node, 'group'), |
| 96 | + acl: getAttr(node, 'acl'), |
| 97 | + cacheable: getBoolAttr(node, 'cacheable'), |
| 98 | + })); |
| 99 | + }; |
| 100 | + |
| 101 | + const mapBlocks = (nodes: any[] | undefined): any[] => { |
| 102 | + if (!Array.isArray(nodes)) { |
| 103 | + return []; |
| 104 | + } |
| 105 | + return nodes.map(node => ({ |
| 106 | + name: getAttr(node, 'name'), |
| 107 | + class: getAttr(node, 'class'), |
| 108 | + cacheable: getBoolAttr(node, 'cacheable'), |
| 109 | + as: getAttr(node, 'as'), |
| 110 | + ttl: getNumAttr(node, 'ttl'), |
| 111 | + group: getAttr(node, 'group'), |
| 112 | + acl: getAttr(node, 'acl'), |
| 113 | + block: mapBlocks(node.block), |
| 114 | + container: mapContainers(node.container), |
| 115 | + referenceBlock: mapReferenceBlocks(node.referenceBlock), |
| 116 | + uiComponent: mapUiComponents(node.uiComponent), |
| 117 | + })); |
| 118 | + }; |
| 119 | + |
| 120 | + const mapReferenceBlocks = (nodes: any[] | undefined): any[] => { |
| 121 | + if (!Array.isArray(nodes)) { |
| 122 | + return []; |
| 123 | + } |
| 124 | + return nodes.map(node => ({ |
| 125 | + name: getAttr(node, 'name') as string, |
| 126 | + template: getAttr(node, 'template'), |
| 127 | + class: getAttr(node, 'class'), |
| 128 | + group: getAttr(node, 'group'), |
| 129 | + display: getBoolAttr(node, 'display'), |
| 130 | + remove: getBoolAttr(node, 'remove'), |
| 131 | + block: mapBlocks(node.block), |
| 132 | + referenceBlock: mapReferenceBlocks(node.referenceBlock), |
| 133 | + uiComponent: mapUiComponents(node.uiComponent), |
| 134 | + container: mapContainers(node.container), |
| 135 | + })); |
| 136 | + }; |
| 137 | + |
| 138 | + const mapContainers = (nodes: any[] | undefined): any[] => { |
| 139 | + if (!Array.isArray(nodes)) { |
| 140 | + return []; |
| 141 | + } |
| 142 | + return nodes.map(node => ({ |
| 143 | + name: getAttr(node, 'name') as string, |
| 144 | + after: getAttr(node, 'after'), |
| 145 | + before: getAttr(node, 'before'), |
| 146 | + block: mapBlocks(node.block), |
| 147 | + referenceBlock: mapReferenceBlocks(node.referenceBlock), |
| 148 | + uiComponent: mapUiComponents(node.uiComponent), |
| 149 | + container: mapContainers(node.container), |
| 150 | + })); |
| 151 | + }; |
| 152 | + |
| 153 | + const mapMoves = (nodes: any[] | undefined) => { |
| 154 | + if (!Array.isArray(nodes)) { |
| 155 | + return []; |
| 156 | + } |
| 157 | + return nodes.map(node => ({ |
| 158 | + element: getAttr(node, 'element') as string, |
| 159 | + destination: getAttr(node, 'destination') as string, |
| 160 | + as: getAttr(node, 'as'), |
| 161 | + after: getAttr(node, 'after'), |
| 162 | + before: getAttr(node, 'before'), |
| 163 | + })); |
| 164 | + }; |
| 165 | + |
| 166 | + const bodyNode = Array.isArray(pageNode.body) ? pageNode.body[0] : undefined; |
| 167 | + const body = bodyNode |
| 168 | + ? { |
| 169 | + block: mapBlocks(bodyNode.block), |
| 170 | + referenceBlock: mapReferenceBlocks(bodyNode.referenceBlock), |
| 171 | + uiComponent: mapUiComponents(bodyNode.uiComponent), |
| 172 | + container: mapContainers(bodyNode.container), |
| 173 | + move: mapMoves(bodyNode.move), |
| 174 | + } |
| 175 | + : { block: [], referenceBlock: [], uiComponent: [], container: [], move: [] }; |
| 176 | + |
| 177 | + const page = { |
| 178 | + update: Array.isArray(pageNode.update) |
| 179 | + ? pageNode.update.map((u: any) => ({ |
| 180 | + handle: getAttr(u, 'handle') as string, |
| 181 | + })) |
| 182 | + : [], |
| 183 | + body: [body], |
| 184 | + }; |
| 185 | + |
| 186 | + const path = uri.fsPath; |
| 187 | + const p = path.replace(/\\/g, '/'); |
| 188 | + |
| 189 | + let area = 'base'; |
| 190 | + if (p.includes('/view/frontend/layout/') || p.includes('/app/design/frontend/')) { |
| 191 | + area = 'frontend'; |
| 192 | + } else if (p.includes('/view/adminhtml/layout/') || p.includes('/app/design/adminhtml/')) { |
| 193 | + area = 'adminhtml'; |
| 194 | + } else if (p.includes('/view/base/layout/') || p.includes('/app/design/base/')) { |
| 195 | + area = 'base'; |
| 196 | + } |
| 197 | + |
| 198 | + const theme = this.getTheme(path); |
| 199 | + |
| 200 | + const layout: Layout = { |
| 201 | + area, |
| 202 | + theme: theme?.title ?? '-', |
| 203 | + path, |
| 204 | + page, |
| 205 | + }; |
| 206 | + |
| 207 | + return layout; |
| 208 | + } |
| 209 | + |
| 210 | + private getTheme(path: string): Theme | undefined { |
| 211 | + const themeIndexData = IndexManager.getIndexData(ThemeIndexer.KEY); |
| 212 | + |
| 213 | + return themeIndexData?.getThemeByFilePath(path); |
| 214 | + } |
| 215 | +} |
0 commit comments