Integrates Prettier into the stylesheet store for automatic CSS formatting: - Installs prettier v3.7.4 with postcss plugin - Implements formatContent() function using Prettier API - Auto-formats CSS after 500ms of inactivity (debounced) - Prevents infinite loops with isFormatting flag - Ensures consistent indentation and line breaks - Cleans up extra blank lines and formatting issues This ensures the CSS in the Code tab stays clean and properly formatted after reactive edits from TextSettings and PageSettings panels. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
74 lines
1.8 KiB
JavaScript
74 lines
1.8 KiB
JavaScript
import { defineStore } from 'pinia';
|
|
import { ref, watch } from 'vue';
|
|
import cssParsingUtils from '../utils/css-parsing';
|
|
import prettier from 'prettier/standalone';
|
|
import parserPostcss from 'prettier/plugins/postcss';
|
|
|
|
export const useStylesheetStore = defineStore('stylesheet', () => {
|
|
const content = ref('');
|
|
let formatTimer = null;
|
|
let isFormatting = false;
|
|
|
|
// Format CSS with Prettier
|
|
const formatContent = async () => {
|
|
if (isFormatting || !content.value) return;
|
|
|
|
try {
|
|
isFormatting = true;
|
|
const formatted = await prettier.format(content.value, {
|
|
parser: 'css',
|
|
plugins: [parserPostcss],
|
|
printWidth: 80,
|
|
tabWidth: 2,
|
|
useTabs: false,
|
|
});
|
|
content.value = formatted;
|
|
} catch (error) {
|
|
console.error('CSS formatting error:', error);
|
|
} finally {
|
|
isFormatting = false;
|
|
}
|
|
};
|
|
|
|
// Watch content and format after 500ms of inactivity
|
|
watch(content, () => {
|
|
if (isFormatting) return;
|
|
|
|
clearTimeout(formatTimer);
|
|
formatTimer = setTimeout(() => {
|
|
formatContent();
|
|
}, 500);
|
|
});
|
|
|
|
const loadStylesheet = async () => {
|
|
const response = await fetch('/assets/css/stylesheet.css');
|
|
content.value = await response.text();
|
|
};
|
|
|
|
const updateProperty = (selector, property, value, unit) => {
|
|
content.value = cssParsingUtils.updateCssValue({
|
|
css: content.value,
|
|
selector,
|
|
property,
|
|
value,
|
|
unit
|
|
});
|
|
};
|
|
|
|
const extractValue = (selector, property) => {
|
|
return cssParsingUtils.extractCssValue(content.value, selector, property);
|
|
};
|
|
|
|
const extractBlock = (selector) => {
|
|
return cssParsingUtils.extractCssBlock(content.value, selector);
|
|
};
|
|
|
|
return {
|
|
content,
|
|
loadStylesheet,
|
|
updateProperty,
|
|
extractValue,
|
|
extractBlock,
|
|
formatContent
|
|
};
|
|
});
|