refactor: extract shared patterns from popup/settings components

- Create useColoris composable (shared Coloris init across 4 files)
- Create useLinkedSpacing composable (linked margin/padding logic from TextSettings)
- Create BasePopup component (shared popup shell, CSS editor, inheritance button)
- Add watchProp helper in ElementPopup (12 watchers → 12 compact lines)
- Use extractSpacing for @page margin parsing in PagePopup and PageSettings

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
isUnknown 2026-02-26 15:35:45 +01:00
parent 0c682c78c0
commit 69d5ebe7ed
7 changed files with 816 additions and 1187 deletions

View file

@ -1,301 +1,202 @@
<template>
<div
v-if="visible"
<BasePopup
ref="basePopup"
id="page-popup"
class="settings-popup"
:style="{ top: position.y + 'px', left: position.x + 'px' }"
:display-css="displayedCss"
:editable-css="pageCss"
:popup-width="550"
:popup-height="600"
@close="close"
@css-input="handleCssInput"
@toggle-inheritance="toggleInheritance"
>
<div class="popup-header">
<div class="header-left">
<span class="page-label">@page</span>
<span class="page-name">{{ templateName || 'default' }}</span>
<span class="page-count">{{ pageCount }} page{{ pageCount > 1 ? 's' : '' }}</span>
</div>
<button class="close-btn" @click="close">×</button>
</div>
<template #header-left>
<span class="page-label">@page</span>
<span class="page-name">{{ templateName || 'default' }}</span>
<span class="page-count">{{ pageCount }} page{{ pageCount > 1 ? 's' : '' }}</span>
</template>
<div class="popup-body">
<!-- Left: Controls -->
<div class="popup-controls">
<!-- Margins -->
<div class="settings-subsection">
<h4>Marges</h4>
<div class="margin-grid">
<div
class="field"
:class="{ 'field--view-only': inheritanceLocked }"
>
<label class="label-with-tooltip" data-css="margin-top">Haut</label>
<div class="input-with-unit">
<NumberInput
v-model="margins.top.value"
:min="0"
:step="1"
:disabled="inheritanceLocked"
/>
<div class="unit-toggle">
<button
type="button"
:class="{ active: margins.top.unit === 'mm' }"
:disabled="inheritanceLocked"
@click="margins.top.unit = 'mm'"
>
mm
</button>
<button
type="button"
:class="{ active: margins.top.unit === 'px' }"
:disabled="inheritanceLocked"
@click="margins.top.unit = 'px'"
>
px
</button>
<!-- <button
type="button"
:class="{ active: margins.top.unit === 'rem' }"
:disabled="inheritanceLocked"
@click="margins.top.unit = 'rem'"
>
rem
</button> -->
</div>
</div>
</div>
<div
class="field"
:class="{ 'field--view-only': inheritanceLocked }"
>
<label class="label-with-tooltip" data-css="margin-bottom">Bas</label>
<div class="input-with-unit">
<NumberInput
v-model="margins.bottom.value"
:min="0"
:step="1"
:disabled="inheritanceLocked"
/>
<div class="unit-toggle">
<button
type="button"
:class="{ active: margins.bottom.unit === 'mm' }"
:disabled="inheritanceLocked"
@click="margins.bottom.unit = 'mm'"
>
mm
</button>
<button
type="button"
:class="{ active: margins.bottom.unit === 'px' }"
:disabled="inheritanceLocked"
@click="margins.bottom.unit = 'px'"
>
px
</button>
<!-- <button
type="button"
:class="{ active: margins.bottom.unit === 'rem' }"
:disabled="inheritanceLocked"
@click="margins.bottom.unit = 'rem'"
>
rem
</button> -->
</div>
</div>
</div>
<div
class="field"
:class="{ 'field--view-only': inheritanceLocked }"
>
<label class="label-with-tooltip" data-css="margin-left">Gauche</label>
<div class="input-with-unit">
<NumberInput
v-model="margins.left.value"
:min="0"
:step="1"
:disabled="inheritanceLocked"
/>
<div class="unit-toggle">
<button
type="button"
:class="{ active: margins.left.unit === 'mm' }"
:disabled="inheritanceLocked"
@click="margins.left.unit = 'mm'"
>
mm
</button>
<button
type="button"
:class="{ active: margins.left.unit === 'px' }"
:disabled="inheritanceLocked"
@click="margins.left.unit = 'px'"
>
px
</button>
<!-- <button
type="button"
:class="{ active: margins.left.unit === 'rem' }"
:disabled="inheritanceLocked"
@click="margins.left.unit = 'rem'"
>
rem
</button> -->
</div>
</div>
</div>
<div
class="field"
:class="{ 'field--view-only': inheritanceLocked }"
>
<label class="label-with-tooltip" data-css="margin-right">Droite</label>
<div class="input-with-unit">
<NumberInput
v-model="margins.right.value"
:min="0"
:step="1"
:disabled="inheritanceLocked"
/>
<div class="unit-toggle">
<button
type="button"
:class="{ active: margins.right.unit === 'mm' }"
:disabled="inheritanceLocked"
@click="margins.right.unit = 'mm'"
>
mm
</button>
<button
type="button"
:class="{ active: margins.right.unit === 'px' }"
:disabled="inheritanceLocked"
@click="margins.right.unit = 'px'"
>
px
</button>
<!-- <button
type="button"
:class="{ active: margins.right.unit === 'rem' }"
:disabled="inheritanceLocked"
@click="margins.right.unit = 'rem'"
>
rem
</button> -->
</div>
</div>
</div>
</div>
</div>
<!-- Background -->
<div class="settings-subsection">
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
<label class="label-with-tooltip" data-css="background">Arrière-plan</label>
<div class="input-with-color">
<input
ref="backgroundColorInput"
type="text"
v-model="background.value"
:disabled="inheritanceLocked"
data-coloris
/>
</div>
</div>
</div>
<!-- Patterns -->
<div class="settings-subsection">
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
<label class="label-with-tooltip" data-css="background-image">Motifs</label>
<select v-model="pattern" :disabled="inheritanceLocked">
<option value="">Choisissez</option>
<option value="dots">Points</option>
<option value="lines">Lignes</option>
<option value="grid">Grille</option>
</select>
</div>
</div>
<!-- Lock/Unlock Inheritance Button -->
<div class="settings-subsection">
<button class="inheritance-btn" @click="toggleInheritance">
<svg
v-if="inheritanceLocked"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
>
<path
d="M19 10H20C20.5523 10 21 10.4477 21 11V21C21 21.5523 20.5523 22 20 22H4C3.44772 22 3 21.5523 3 21V11C3 10.4477 3.44772 10 4 10H5V9C5 5.13401 8.13401 2 12 2C15.866 2 19 5.13401 19 9V10ZM5 12V20H19V12H5ZM11 14H13V18H11V14ZM17 10V9C17 6.23858 14.7614 4 12 4C9.23858 4 7 6.23858 7 9V10H17Z"
></path>
</svg>
<svg
v-else
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
>
<path
d="M7 10H20C20.5523 10 21 10.4477 21 11V21C21 21.5523 20.5523 22 20 22H4C3.44772 22 3 21.5523 3 21V11C3 10.4477 3.44772 10 4 10H5V9C5 5.13401 8.13401 2 12 2C14.7405 2 17.1131 3.5748 18.2624 5.86882L16.4731 6.76344C15.6522 5.12486 13.9575 4 12 4C9.23858 4 7 6.23858 7 9V10ZM5 12V20H19V12H5ZM10 15H14V17H10V15Z"
></path>
</svg>
<span>{{
inheritanceLocked
? "Déverrouiller l'héritage"
: "Verrouiller l'héritage"
}}</span>
</button>
</div>
</div>
<!-- Right: CSS Editor -->
<div class="popup-css">
<div class="css-header">
<span>CSS</span>
<label
class="toggle"
<template #controls>
<!-- Margins -->
<div class="settings-subsection">
<h4>Marges</h4>
<div class="margin-grid">
<div
class="field"
:class="{ 'field--view-only': inheritanceLocked }"
>
<span class="toggle-label">Mode édition</span>
<input
type="checkbox"
v-model="isEditable"
:disabled="inheritanceLocked"
/>
<span class="toggle-switch"></span>
</label>
<label class="label-with-tooltip" data-css="margin-top">Haut</label>
<div class="input-with-unit">
<NumberInput
v-model="margins.top.value"
:min="0"
:step="1"
:disabled="inheritanceLocked"
/>
<div class="unit-toggle">
<button
type="button"
:class="{ active: margins.top.unit === 'mm' }"
:disabled="inheritanceLocked"
@click="margins.top.unit = 'mm'"
>
mm
</button>
<button
type="button"
:class="{ active: margins.top.unit === 'px' }"
:disabled="inheritanceLocked"
@click="margins.top.unit = 'px'"
>
px
</button>
</div>
</div>
</div>
<div
class="field"
:class="{ 'field--view-only': inheritanceLocked }"
>
<label class="label-with-tooltip" data-css="margin-bottom">Bas</label>
<div class="input-with-unit">
<NumberInput
v-model="margins.bottom.value"
:min="0"
:step="1"
:disabled="inheritanceLocked"
/>
<div class="unit-toggle">
<button
type="button"
:class="{ active: margins.bottom.unit === 'mm' }"
:disabled="inheritanceLocked"
@click="margins.bottom.unit = 'mm'"
>
mm
</button>
<button
type="button"
:class="{ active: margins.bottom.unit === 'px' }"
:disabled="inheritanceLocked"
@click="margins.bottom.unit = 'px'"
>
px
</button>
</div>
</div>
</div>
<div
class="field"
:class="{ 'field--view-only': inheritanceLocked }"
>
<label class="label-with-tooltip" data-css="margin-left">Gauche</label>
<div class="input-with-unit">
<NumberInput
v-model="margins.left.value"
:min="0"
:step="1"
:disabled="inheritanceLocked"
/>
<div class="unit-toggle">
<button
type="button"
:class="{ active: margins.left.unit === 'mm' }"
:disabled="inheritanceLocked"
@click="margins.left.unit = 'mm'"
>
mm
</button>
<button
type="button"
:class="{ active: margins.left.unit === 'px' }"
:disabled="inheritanceLocked"
@click="margins.left.unit = 'px'"
>
px
</button>
</div>
</div>
</div>
<div
class="field"
:class="{ 'field--view-only': inheritanceLocked }"
>
<label class="label-with-tooltip" data-css="margin-right">Droite</label>
<div class="input-with-unit">
<NumberInput
v-model="margins.right.value"
:min="0"
:step="1"
:disabled="inheritanceLocked"
/>
<div class="unit-toggle">
<button
type="button"
:class="{ active: margins.right.unit === 'mm' }"
:disabled="inheritanceLocked"
@click="margins.right.unit = 'mm'"
>
mm
</button>
<button
type="button"
:class="{ active: margins.right.unit === 'px' }"
:disabled="inheritanceLocked"
@click="margins.right.unit = 'px'"
>
px
</button>
</div>
</div>
</div>
</div>
<pre
v-if="!isEditable"
class="readonly"
><code class="hljs language-css" v-html="highlightedCss"></code></pre>
<textarea
v-else
:value="pageCss"
@input="handleCssInput"
:disabled="inheritanceLocked"
spellcheck="false"
></textarea>
</div>
</div>
</div>
<!-- Background -->
<div class="settings-subsection">
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
<label class="label-with-tooltip" data-css="background">Arrière-plan</label>
<div class="input-with-color">
<input
ref="backgroundColorInput"
type="text"
v-model="background.value"
:disabled="inheritanceLocked"
data-coloris
/>
</div>
</div>
</div>
<!-- Patterns -->
<div class="settings-subsection">
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
<label class="label-with-tooltip" data-css="background-image">Motifs</label>
<select v-model="pattern" :disabled="inheritanceLocked">
<option value="">Choisissez</option>
<option value="dots">Points</option>
<option value="lines">Lignes</option>
<option value="grid">Grille</option>
</select>
</div>
</div>
</template>
</BasePopup>
</template>
<script setup>
import { ref, computed, watch, onMounted } from 'vue';
import { ref, computed, watch } from 'vue';
import { useStylesheetStore } from '../stores/stylesheet';
import { usePopupPosition } from '../composables/usePopupPosition';
import { useDebounce } from '../composables/useDebounce';
import { useCssSync } from '../composables/useCssSync';
import NumberInput from './ui/NumberInput.vue';
import Coloris from '@melloware/coloris';
import '@melloware/coloris/dist/coloris.css';
import hljs from 'highlight.js/lib/core';
import css from 'highlight.js/lib/languages/css';
import 'highlight.js/styles/atom-one-dark.css';
hljs.registerLanguage('css', css);
import BasePopup from './ui/BasePopup.vue';
const stylesheetStore = useStylesheetStore();
const { extractSpacing } = useCssSync();
const props = defineProps({
iframeRef: Object,
@ -303,13 +204,14 @@ const props = defineProps({
const emit = defineEmits(['close']);
const visible = ref(false);
const position = ref({ x: 0, y: 0 });
const basePopup = ref(null);
const visible = computed(() => basePopup.value?.visible ?? false);
const inheritanceLocked = computed(() => basePopup.value?.inheritanceLocked ?? true);
const selectedPageElement = ref(null);
const pageCount = ref(0);
const templateName = ref('');
const isEditable = ref(false);
const inheritanceLocked = ref(true);
const backgroundColorInput = ref(null);
let isUpdatingFromStore = false;
@ -333,11 +235,6 @@ const immediateUpdate = (callback) => {
callback();
};
const POPUP_WIDTH = 550;
const POPUP_HEIGHT = 600;
const { calculatePosition } = usePopupPosition(POPUP_WIDTH, POPUP_HEIGHT);
// Get the selector for the current template's @page rule
const getTemplateSelector = () => {
return templateName.value ? `@page ${templateName.value}` : '@page';
@ -349,10 +246,8 @@ const getOrCreateTemplateBlock = () => {
let block = stylesheetStore.extractBlock(selector);
if (!block && templateName.value) {
// Create new block with current values from @page
const baseBlock = stylesheetStore.extractBlock('@page');
if (baseBlock) {
// Insert the new template block after @page
const marginValue = `${margins.value.top.value}${margins.value.top.unit} ${margins.value.right.value}${margins.value.right.unit} ${margins.value.bottom.value}${margins.value.bottom.unit} ${margins.value.left.value}${margins.value.left.unit}`;
const newBlock = `\n@page ${templateName.value} {\n margin: ${marginValue};${background.value.value ? `\n background: ${background.value.value};` : ''}\n}\n`;
@ -375,7 +270,6 @@ const removeTemplateBlock = () => {
const block = stylesheetStore.extractBlock(selector);
if (block) {
// Remove the block and any surrounding whitespace
stylesheetStore.replaceInCustomCss(
new RegExp(`\\n?${selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*\\{[^}]*\\}\\n?`),
'\n'
@ -384,7 +278,6 @@ const removeTemplateBlock = () => {
};
const updateMargins = (force = false) => {
// Only update if inheritance is unlocked (unless forced)
if (!force && inheritanceLocked.value) return;
const marginValue = `${margins.value.top.value}${margins.value.top.unit} ${margins.value.right.value}${margins.value.right.unit} ${margins.value.bottom.value}${margins.value.bottom.unit} ${margins.value.left.value}${margins.value.left.unit}`;
@ -392,8 +285,6 @@ const updateMargins = (force = false) => {
const currentBlock = getOrCreateTemplateBlock();
if (!currentBlock) return;
const selector = getTemplateSelector();
if (currentBlock.includes('margin:')) {
const updatedBlock = currentBlock.replace(
/(margin:\s*)[^;]+/,
@ -416,7 +307,6 @@ const updateMargins = (force = false) => {
};
const updateBackground = (force = false) => {
// Only update if inheritance is unlocked (unless forced)
if (!force && inheritanceLocked.value) return;
if (!background.value.value) return;
@ -491,37 +381,27 @@ const loadValuesFromStylesheet = () => {
try {
isUpdatingFromStore = true;
// Extract values from @page block (same logic as PageSettings)
const pageBlock = stylesheetStore.extractBlock('@page');
if (!pageBlock) return;
// Parse margins with regex (top right bottom left)
const marginMatch = pageBlock.match(
/margin:\s*([0-9.]+)([a-z]+)\s+([0-9.]+)([a-z]+)\s+([0-9.]+)([a-z]+)\s+([0-9.]+)([a-z]+)/i
);
if (marginMatch) {
margins.value.top = {
value: parseFloat(marginMatch[1]),
unit: marginMatch[2],
};
margins.value.right = {
value: parseFloat(marginMatch[3]),
unit: marginMatch[4],
};
margins.value.bottom = {
value: parseFloat(marginMatch[5]),
unit: marginMatch[6],
};
margins.value.left = {
value: parseFloat(marginMatch[7]),
unit: marginMatch[8],
};
// Use extractSpacing from useCssSync
const spacing = extractSpacing('@page', 'margin');
if (spacing?.detailed) {
margins.value.top = spacing.detailed.top;
margins.value.right = spacing.detailed.right;
margins.value.bottom = spacing.detailed.bottom;
margins.value.left = spacing.detailed.left;
} else if (spacing?.simple) {
margins.value.top = { ...spacing.simple };
margins.value.right = { ...spacing.simple };
margins.value.bottom = { ...spacing.simple };
margins.value.left = { ...spacing.simple };
}
// Extract background
const bgMatch = pageBlock.match(/background:\s*([^;]+)/);
if (bgMatch) {
background.value.value = bgMatch[1].trim();
const pageBlock = stylesheetStore.extractBlock('@page');
if (pageBlock) {
const bgMatch = pageBlock.match(/background:\s*([^;]+)/);
if (bgMatch) {
background.value.value = bgMatch[1].trim();
}
}
} catch (error) {
console.error('Error loading values from stylesheet:', error);
@ -533,100 +413,61 @@ const loadValuesFromStylesheet = () => {
const open = (pageElement, event, count = 1) => {
selectedPageElement.value = pageElement;
pageCount.value = count;
position.value = calculatePosition(event);
// Extract template name from data-page-type attribute
templateName.value = pageElement.getAttribute('data-page-type') || '';
// Read inheritance state from page element's data attribute
inheritanceLocked.value = pageElement.dataset.inheritanceUnlocked !== 'true';
basePopup.value.inheritanceLocked = pageElement.dataset.inheritanceUnlocked !== 'true';
// Load values from stylesheet (@page block)
loadValuesFromStylesheet();
visible.value = true;
// Initialize Coloris after opening
setTimeout(() => {
Coloris.init();
Coloris({
el: '[data-coloris]',
theme: 'pill',
themeMode: 'dark',
formatToggle: true,
alpha: true,
closeButton: true,
closeLabel: 'Fermer',
clearButton: true,
clearLabel: 'Effacer',
swatchesOnly: false,
inline: false,
wrap: true,
swatches: [
'#264653',
'#2a9d8f',
'#e9c46a',
'#f4a261',
'#e76f51',
'#d62828',
'#023e8a',
'#0077b6',
'#ffffff',
'#000000',
],
});
}, 0);
// Open popup (sets visible, position, inits Coloris)
basePopup.value.open(event);
};
const close = () => {
selectedPageElement.value = null;
visible.value = false;
isEditable.value = false;
basePopup.value?.close();
emit('close');
};
const toggleInheritance = () => {
const wasLocked = inheritanceLocked.value;
inheritanceLocked.value = !inheritanceLocked.value;
const wasLocked = basePopup.value.inheritanceLocked;
basePopup.value.inheritanceLocked = !wasLocked;
// Store the inheritance state in the page element's data attribute
if (selectedPageElement.value) {
if (inheritanceLocked.value) {
if (basePopup.value.inheritanceLocked) {
delete selectedPageElement.value.dataset.inheritanceUnlocked;
} else {
selectedPageElement.value.dataset.inheritanceUnlocked = 'true';
}
}
if (inheritanceLocked.value && !wasLocked) {
// Re-locking: remove the template-specific block
if (basePopup.value.inheritanceLocked && !wasLocked) {
removeTemplateBlock();
} else if (!inheritanceLocked.value && wasLocked) {
// Unlocking: apply all current field values to create the CSS block
} else if (!basePopup.value.inheritanceLocked && wasLocked) {
applyAllStyles();
}
};
const pageCss = computed(() => {
// Show template-specific block if unlocked and exists, otherwise show @page
if (!inheritanceLocked.value && templateName.value) {
if (!basePopup.value?.inheritanceLocked && templateName.value) {
const templateBlock = stylesheetStore.extractBlock(`@page ${templateName.value}`);
if (templateBlock) return templateBlock;
}
return stylesheetStore.extractBlock('@page') || '';
});
// Generate a preview CSS block from current field values
const generatePreviewCss = () => {
if (!templateName.value) return '';
const properties = [];
// Always include margin with current values
const marginValue = `${margins.value.top.value}${margins.value.top.unit} ${margins.value.right.value}${margins.value.right.unit} ${margins.value.bottom.value}${margins.value.bottom.unit} ${margins.value.left.value}${margins.value.left.unit}`;
properties.push(` margin: ${marginValue};`);
// Include background if it has a value
if (background.value.value) {
properties.push(` background: ${background.value.value};`);
}
@ -637,14 +478,11 @@ const generatePreviewCss = () => {
};
const displayedCss = computed(() => {
// If unlocked, show the actual CSS block from stylesheet
if (!inheritanceLocked.value) {
if (!basePopup.value?.inheritanceLocked) {
return pageCss.value;
}
// If locked, show commented preview of what would be applied
if (!templateName.value) {
// For base @page, just show it normally
return pageCss.value;
}
@ -656,47 +494,18 @@ const displayedCss = computed(() => {
' */';
});
const highlightedCss = computed(() => {
if (!displayedCss.value) return '';
return hljs.highlight(displayedCss.value, { language: 'css' }).value;
});
let cssDebounceTimer = null;
const handleCssInput = (event) => {
const newCss = event.target.value;
if (cssDebounceTimer) {
clearTimeout(cssDebounceTimer);
const handleCssInput = (newCss) => {
const oldBlock = pageCss.value;
if (oldBlock) {
stylesheetStore.replaceInCustomCss(oldBlock, newCss);
}
cssDebounceTimer = setTimeout(() => {
// Get the actual CSS block (not the commented preview)
const oldBlock = pageCss.value;
if (oldBlock) {
stylesheetStore.replaceInCustomCss(
oldBlock,
newCss
);
}
}, 500);
};
// Watch isEditable to format when exiting edit mode
watch(isEditable, async (newValue, oldValue) => {
stylesheetStore.isEditing = newValue;
// Format when exiting editing mode
if (oldValue && !newValue) {
await stylesheetStore.formatCustomCss();
}
});
// Watch stylesheet changes to sync values
watch(
() => stylesheetStore.content,
() => {
if (visible.value && !stylesheetStore.isEditing) {
if (basePopup.value?.visible && !stylesheetStore.isEditing) {
loadValuesFromStylesheet();
}
}