2025-11-24 17:55:42 +01:00
|
|
|
import { defineStore } from 'pinia';
|
2025-12-05 16:23:42 +01:00
|
|
|
import { ref, watch } from 'vue';
|
2025-11-24 17:55:42 +01:00
|
|
|
import cssParsingUtils from '../utils/css-parsing';
|
2025-12-05 16:23:42 +01:00
|
|
|
import prettier from 'prettier/standalone';
|
|
|
|
|
import parserPostcss from 'prettier/plugins/postcss';
|
2025-11-24 17:55:42 +01:00
|
|
|
|
|
|
|
|
export const useStylesheetStore = defineStore('stylesheet', () => {
|
|
|
|
|
const content = ref('');
|
2025-12-05 16:52:00 +01:00
|
|
|
const isEditing = ref(false);
|
2025-12-05 16:23:42 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-05 16:52:00 +01:00
|
|
|
// Watch content and format after 500ms of inactivity (only when not editing)
|
2025-12-05 16:23:42 +01:00
|
|
|
watch(content, () => {
|
2025-12-05 16:52:00 +01:00
|
|
|
if (isFormatting || isEditing.value) return;
|
2025-12-05 16:23:42 +01:00
|
|
|
|
|
|
|
|
clearTimeout(formatTimer);
|
|
|
|
|
formatTimer = setTimeout(() => {
|
|
|
|
|
formatContent();
|
|
|
|
|
}, 500);
|
|
|
|
|
});
|
2025-11-24 17:55:42 +01:00
|
|
|
|
|
|
|
|
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,
|
2025-12-05 16:52:00 +01:00
|
|
|
isEditing,
|
2025-11-24 17:55:42 +01:00
|
|
|
loadStylesheet,
|
|
|
|
|
updateProperty,
|
|
|
|
|
extractValue,
|
2025-12-05 16:23:42 +01:00
|
|
|
extractBlock,
|
|
|
|
|
formatContent
|
2025-11-24 17:55:42 +01:00
|
|
|
};
|
|
|
|
|
});
|