|
| 1 | +import { Command } from 'command/Command'; |
| 2 | +import IndexManager from 'indexer/IndexManager'; |
| 3 | +import ModuleIndexer from 'indexer/module/ModuleIndexer'; |
| 4 | +import { Uri, window, env } from 'vscode'; |
| 5 | + |
| 6 | +export default class CopyMagentoPathCommand extends Command { |
| 7 | + public static readonly TEMPLATE_EXTENSIONS = ['.phtml']; |
| 8 | + public static readonly WEB_EXTENSIONS = ['.css', '.js']; |
| 9 | + public static readonly IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.svg']; |
| 10 | + |
| 11 | + private static readonly TEMPLATE_PATHS = [ |
| 12 | + 'view/frontend/templates/', |
| 13 | + 'view/adminhtml/templates/', |
| 14 | + 'view/base/templates/', |
| 15 | + 'templates/', |
| 16 | + ]; |
| 17 | + |
| 18 | + private static readonly WEB_PATHS = [ |
| 19 | + 'view/frontend/web/', |
| 20 | + 'view/adminhtml/web/', |
| 21 | + 'view/base/web/', |
| 22 | + 'web/', |
| 23 | + ]; |
| 24 | + |
| 25 | + constructor() { |
| 26 | + super('magento-toolbox.copyMagentoPath'); |
| 27 | + } |
| 28 | + |
| 29 | + public async execute(file: Uri): Promise<void> { |
| 30 | + if (!file) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const moduleIndexData = IndexManager.getIndexData(ModuleIndexer.KEY); |
| 35 | + |
| 36 | + if (!moduleIndexData) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const module = moduleIndexData.getModuleByUri(file); |
| 41 | + |
| 42 | + if (!module) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const paths = this.getPaths(file); |
| 47 | + |
| 48 | + if (!paths) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const pathIndex = paths.findIndex(p => file.fsPath.lastIndexOf(p) !== -1); |
| 53 | + |
| 54 | + if (pathIndex === -1) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const endIndex = file.fsPath.lastIndexOf(paths[pathIndex]); |
| 59 | + const offset = paths[pathIndex].length; |
| 60 | + const relativePath = file.fsPath.slice(offset + endIndex); |
| 61 | + |
| 62 | + const magentoPath = `${module.name}::${relativePath}`; |
| 63 | + |
| 64 | + await env.clipboard.writeText(magentoPath); |
| 65 | + window.showInformationMessage(`Copied: ${magentoPath}`); |
| 66 | + } |
| 67 | + |
| 68 | + private getPaths(file: Uri): string[] | undefined { |
| 69 | + if (CopyMagentoPathCommand.TEMPLATE_EXTENSIONS.some(ext => file.fsPath.endsWith(ext))) { |
| 70 | + return CopyMagentoPathCommand.TEMPLATE_PATHS; |
| 71 | + } |
| 72 | + |
| 73 | + if ( |
| 74 | + CopyMagentoPathCommand.WEB_EXTENSIONS.some(ext => file.fsPath.endsWith(ext)) || |
| 75 | + CopyMagentoPathCommand.IMAGE_EXTENSIONS.some(ext => file.fsPath.endsWith(ext)) |
| 76 | + ) { |
| 77 | + return CopyMagentoPathCommand.WEB_PATHS; |
| 78 | + } |
| 79 | + |
| 80 | + return undefined; |
| 81 | + } |
| 82 | +} |
0 commit comments