|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { registerTextEditorCommand } from '@zardoy/vscode-utils/build/commands' |
| 3 | +import { noCase } from 'change-case' |
| 4 | +import { getExtensionCommandId, registerExtensionCommand, showQuickPick } from 'vscode-framework' |
| 5 | +import postcssJs from 'postcss-js' |
| 6 | +import postcss from 'postcss' |
| 7 | + |
| 8 | +export default () => { |
| 9 | + registerTextEditorCommand('fixCssInJs', async (editor, _, mode) => { |
| 10 | + const commands = ['fixCssToJs', 'fixJsToCss'] |
| 11 | + let command: string | undefined |
| 12 | + if (mode === 'auto') |
| 13 | + command = |
| 14 | + editor.document.languageId === 'css' || editor.document.languageId === 'scss' || editor.document.languageId === 'less' |
| 15 | + ? 'fixCssToJs' |
| 16 | + : 'fixJsToCss' |
| 17 | + else |
| 18 | + command = await showQuickPick( |
| 19 | + commands.map(command => ({ |
| 20 | + label: noCase(command), |
| 21 | + value: command, |
| 22 | + })), |
| 23 | + ) |
| 24 | + |
| 25 | + if (!command) return |
| 26 | + await vscode.commands.executeCommand(getExtensionCommandId(command as any)) |
| 27 | + }) |
| 28 | + |
| 29 | + registerTextEditorCommand('fixJsToCss', async editor => { |
| 30 | + let text = editor.document.getText(editor.selection) |
| 31 | + if (!text.trim()) return |
| 32 | + const transformJsPropToCss = (prop: string) => { |
| 33 | + const converted = prop.replace(/([A-Z])/g, '-$1').toLowerCase() |
| 34 | + return converted |
| 35 | + } |
| 36 | + |
| 37 | + text = text.replaceAll(/(\w+)(: )/gi, (_, prop, after) => { |
| 38 | + const converted = transformJsPropToCss(prop) |
| 39 | + return `${converted}${after}` |
| 40 | + }) |
| 41 | + text = text.replaceAll(/: (.*?),?(\r?\n|$)/g, (_, value, ending) => { |
| 42 | + let converted = Number.isNaN(Number(value)) ? value : `${value}px` |
| 43 | + if (converted.startsWith("'") || converted.startsWith('"')) converted = converted.slice(1, -1) |
| 44 | + return `: ${converted};${ending ?? ''}` |
| 45 | + }) |
| 46 | + await editor.edit(edit => edit.replace(editor.selection, text)) |
| 47 | + }) |
| 48 | + registerTextEditorCommand('fixCssToJs', async editor => { |
| 49 | + const text = editor.document.getText(editor.selection) |
| 50 | + if (!text.trim()) return |
| 51 | + const root = postcss.parse(text) |
| 52 | + |
| 53 | + const patch = input => { |
| 54 | + if (typeof input === 'object') { |
| 55 | + const result = {} |
| 56 | + for (const key of Object.keys(input)) result[key] = patch(input[key]) |
| 57 | + |
| 58 | + return result |
| 59 | + } |
| 60 | + |
| 61 | + if (typeof input === 'string') { |
| 62 | + const pxMatch = /^(\d+)px$/.exec(input) |
| 63 | + return pxMatch ? +pxMatch[1]! : input |
| 64 | + } |
| 65 | + |
| 66 | + return input |
| 67 | + } |
| 68 | + |
| 69 | + const transformed = JSON.stringify(patch(postcssJs.objectify(root)), null, '\t') |
| 70 | + await editor.edit(edit => edit.replace(editor.selection, transformed.slice(1, -1))) |
| 71 | + }) |
| 72 | + // todo suggest file imports |
| 73 | +} |
0 commit comments