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:
parent
0c682c78c0
commit
69d5ebe7ed
7 changed files with 816 additions and 1187 deletions
|
|
@ -1,277 +1,192 @@
|
|||
<template>
|
||||
<div
|
||||
v-if="visible"
|
||||
<BasePopup
|
||||
ref="basePopup"
|
||||
id="element-popup"
|
||||
class="settings-popup"
|
||||
:style="{ top: position.y + 'px', left: position.x + 'px' }"
|
||||
:display-css="displayedCss"
|
||||
:editable-css="elementCss"
|
||||
:popup-width="800"
|
||||
:popup-height="600"
|
||||
@close="close"
|
||||
@css-input="handleCssInput"
|
||||
@toggle-inheritance="toggleInheritance"
|
||||
>
|
||||
<div class="popup-header">
|
||||
<div class="header-left">
|
||||
<span class="element-label">{{ selectorTag }}</span>
|
||||
<span class="instance-count">{{ instanceCount }} instances</span>
|
||||
</div>
|
||||
<button class="close-btn" @click="close">×</button>
|
||||
</div>
|
||||
<template #header-left>
|
||||
<span class="element-label">{{ selectorTag }}</span>
|
||||
<span class="instance-count">{{ instanceCount }} instances</span>
|
||||
</template>
|
||||
|
||||
<div class="popup-body">
|
||||
<!-- Left: Controls -->
|
||||
<div class="popup-controls">
|
||||
<!-- Font Family -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field field-font" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="font-family">Police</label>
|
||||
<div class="field-with-option">
|
||||
<select v-model="fontFamily.value" :disabled="inheritanceLocked">
|
||||
<option v-for="f in fonts" :key="f" :value="f">{{ f }}</option>
|
||||
</select>
|
||||
<div class="field-checkbox">
|
||||
<input type="checkbox" v-model="fontStyle.italic" :disabled="inheritanceLocked" />
|
||||
<label class="label-with-tooltip" data-css="font-style">Italique</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Font Weight -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="font-weight">Graisse</label>
|
||||
<UnitToggle v-model="fontWeightString" :units="weights" :disabled="inheritanceLocked" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Font Size -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="font-size">Taille du texte</label>
|
||||
<div class="input-with-unit">
|
||||
<NumberInput
|
||||
v-model="fontSize.value"
|
||||
:min="0"
|
||||
:step="1"
|
||||
:disabled="inheritanceLocked"
|
||||
/>
|
||||
<div class="unit-toggle">
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: fontSize.unit === 'px' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updateFontSizeUnit('px')"
|
||||
>
|
||||
px
|
||||
</button>
|
||||
<!-- <button
|
||||
type="button"
|
||||
:class="{ active: fontSize.unit === 'em' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updateFontSizeUnit('em')"
|
||||
>
|
||||
em
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: fontSize.unit === 'rem' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updateFontSizeUnit('rem')"
|
||||
>
|
||||
rem
|
||||
</button> -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Text Alignment -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="text-align">Alignement</label>
|
||||
<select v-model="textAlign.value" :disabled="inheritanceLocked">
|
||||
<option value="left">Gauche</option>
|
||||
<option value="center">Centre</option>
|
||||
<option value="right">Droite</option>
|
||||
<option value="justify">Justifié</option>
|
||||
<template #controls>
|
||||
<!-- Font Family -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field field-font" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="font-family">Police</label>
|
||||
<div class="field-with-option">
|
||||
<select v-model="fontFamily.value" :disabled="inheritanceLocked">
|
||||
<option v-for="f in fonts" :key="f" :value="f">{{ f }}</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Color -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field field-simple" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="color">Couleur</label>
|
||||
<div class="input-with-color">
|
||||
<input
|
||||
ref="colorInput"
|
||||
type="text"
|
||||
v-model="color.value"
|
||||
:disabled="inheritanceLocked"
|
||||
data-coloris
|
||||
/>
|
||||
<div class="field-checkbox">
|
||||
<input type="checkbox" v-model="fontStyle.italic" :disabled="inheritanceLocked" />
|
||||
<label class="label-with-tooltip" data-css="font-style">Italique</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Background -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field field-simple" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="background">Arrière-plan</label>
|
||||
<div class="input-with-color">
|
||||
<input
|
||||
ref="backgroundInput"
|
||||
type="text"
|
||||
v-model="background.value"
|
||||
:disabled="inheritanceLocked"
|
||||
data-coloris
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Outer Margins -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="margin">Marges extérieures</label>
|
||||
<div class="input-with-unit">
|
||||
<NumberInput
|
||||
v-model="marginOuter.value"
|
||||
:min="0"
|
||||
:step="1"
|
||||
:disabled="inheritanceLocked"
|
||||
/>
|
||||
<div class="unit-toggle">
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: marginOuter.unit === 'mm' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updateMarginOuterUnit('mm')"
|
||||
>
|
||||
mm
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: marginOuter.unit === 'px' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updateMarginOuterUnit('px')"
|
||||
>
|
||||
px
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Inner Margins (Padding) -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="padding">Marges intérieures</label>
|
||||
<div class="input-with-unit">
|
||||
<NumberInput
|
||||
v-model="paddingInner.value"
|
||||
:min="0"
|
||||
:step="1"
|
||||
:disabled="inheritanceLocked"
|
||||
/>
|
||||
<div class="unit-toggle">
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: paddingInner.unit === 'mm' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updatePaddingInnerUnit('mm')"
|
||||
>
|
||||
mm
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: paddingInner.unit === 'px' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updatePaddingInnerUnit('px')"
|
||||
>
|
||||
px
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</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"
|
||||
:class="{ 'field--view-only': inheritanceLocked }"
|
||||
>
|
||||
<span class="toggle-label">Mode édition</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
v-model="isEditable"
|
||||
<!-- Font Weight -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="font-weight">Graisse</label>
|
||||
<UnitToggle v-model="fontWeightString" :units="weights" :disabled="inheritanceLocked" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Font Size -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="font-size">Taille du texte</label>
|
||||
<div class="input-with-unit">
|
||||
<NumberInput
|
||||
v-model="fontSize.value"
|
||||
:min="0"
|
||||
:step="1"
|
||||
:disabled="inheritanceLocked"
|
||||
/>
|
||||
<span class="toggle-switch"></span>
|
||||
</label>
|
||||
<div class="unit-toggle">
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: fontSize.unit === 'px' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updateFontSizeUnit('px')"
|
||||
>
|
||||
px
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<pre
|
||||
v-if="!isEditable"
|
||||
class="readonly"
|
||||
><code class="hljs language-css" v-html="highlightedCss"></code></pre>
|
||||
<textarea
|
||||
v-else
|
||||
:value="elementCss"
|
||||
@input="handleCssInput"
|
||||
:disabled="inheritanceLocked"
|
||||
spellcheck="false"
|
||||
></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Text Alignment -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="text-align">Alignement</label>
|
||||
<select v-model="textAlign.value" :disabled="inheritanceLocked">
|
||||
<option value="left">Gauche</option>
|
||||
<option value="center">Centre</option>
|
||||
<option value="right">Droite</option>
|
||||
<option value="justify">Justifié</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Color -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field field-simple" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="color">Couleur</label>
|
||||
<div class="input-with-color">
|
||||
<input
|
||||
ref="colorInput"
|
||||
type="text"
|
||||
v-model="color.value"
|
||||
:disabled="inheritanceLocked"
|
||||
data-coloris
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Background -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field field-simple" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="background">Arrière-plan</label>
|
||||
<div class="input-with-color">
|
||||
<input
|
||||
ref="backgroundInput"
|
||||
type="text"
|
||||
v-model="background.value"
|
||||
:disabled="inheritanceLocked"
|
||||
data-coloris
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Outer Margins -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="margin">Marges extérieures</label>
|
||||
<div class="input-with-unit">
|
||||
<NumberInput
|
||||
v-model="marginOuter.value"
|
||||
:min="0"
|
||||
:step="1"
|
||||
:disabled="inheritanceLocked"
|
||||
/>
|
||||
<div class="unit-toggle">
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: marginOuter.unit === 'mm' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updateMarginOuterUnit('mm')"
|
||||
>
|
||||
mm
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: marginOuter.unit === 'px' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updateMarginOuterUnit('px')"
|
||||
>
|
||||
px
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Inner Margins (Padding) -->
|
||||
<div class="settings-subsection">
|
||||
<div class="field" :class="{ 'field--view-only': inheritanceLocked }">
|
||||
<label class="label-with-tooltip" data-css="padding">Marges intérieures</label>
|
||||
<div class="input-with-unit">
|
||||
<NumberInput
|
||||
v-model="paddingInner.value"
|
||||
:min="0"
|
||||
:step="1"
|
||||
:disabled="inheritanceLocked"
|
||||
/>
|
||||
<div class="unit-toggle">
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: paddingInner.unit === 'mm' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updatePaddingInnerUnit('mm')"
|
||||
>
|
||||
mm
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: paddingInner.unit === 'px' }"
|
||||
:disabled="inheritanceLocked"
|
||||
@click="updatePaddingInnerUnit('px')"
|
||||
>
|
||||
px
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</BasePopup>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed, watch, nextTick } from 'vue';
|
||||
import { useStylesheetStore } from '../stores/stylesheet';
|
||||
import { usePopupPosition } from '../composables/usePopupPosition';
|
||||
import { useDebounce } from '../composables/useDebounce';
|
||||
import NumberInput from './ui/NumberInput.vue';
|
||||
import UnitToggle from './ui/UnitToggle.vue';
|
||||
import BasePopup from './ui/BasePopup.vue';
|
||||
import { convertUnit } from '../utils/unit-conversion';
|
||||
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);
|
||||
|
||||
const stylesheetStore = useStylesheetStore();
|
||||
|
||||
|
|
@ -281,18 +196,14 @@ const props = defineProps({
|
|||
|
||||
const emit = defineEmits(['close']);
|
||||
|
||||
const POPUP_WIDTH = 800;
|
||||
const POPUP_HEIGHT = 600;
|
||||
const basePopup = ref(null);
|
||||
|
||||
const { calculatePosition } = usePopupPosition(POPUP_WIDTH, POPUP_HEIGHT);
|
||||
const visible = computed(() => basePopup.value?.visible ?? false);
|
||||
const inheritanceLocked = computed(() => basePopup.value?.inheritanceLocked ?? true);
|
||||
|
||||
const visible = ref(false);
|
||||
const position = ref({ x: 0, y: 0 });
|
||||
const selector = ref('');
|
||||
const selectedElement = ref(null);
|
||||
const elementInstanceCount = ref(0); // Count of similar elements
|
||||
const isEditable = ref(false);
|
||||
const inheritanceLocked = ref(true);
|
||||
const elementInstanceCount = ref(0);
|
||||
const colorInput = ref(null);
|
||||
const backgroundInput = ref(null);
|
||||
|
||||
|
|
@ -342,15 +253,12 @@ const updatePaddingInnerUnit = (newUnit) => {
|
|||
};
|
||||
|
||||
const getSelectorFromElement = (element) => {
|
||||
// Try to build a meaningful selector
|
||||
if (element.id) {
|
||||
return `#${element.id}`;
|
||||
}
|
||||
|
||||
// Get tag name
|
||||
const tagName = element.tagName.toLowerCase();
|
||||
|
||||
// Get first class if available (filter out state classes)
|
||||
const classes = Array.from(element.classList).filter(
|
||||
(cls) => !['element-hovered', 'element-selected', 'page-hovered', 'page-selected'].includes(cls)
|
||||
);
|
||||
|
|
@ -391,7 +299,6 @@ const generatePreviewCss = () => {
|
|||
|
||||
const properties = [];
|
||||
|
||||
// Include all properties with their current values
|
||||
if (fontFamily.value.value) {
|
||||
properties.push(` font-family: ${fontFamily.value.value};`);
|
||||
}
|
||||
|
|
@ -428,12 +335,10 @@ const generatePreviewCss = () => {
|
|||
const displayedCss = computed(() => {
|
||||
if (!selector.value) return '';
|
||||
|
||||
// If unlocked, show the actual CSS block from stylesheet
|
||||
if (!inheritanceLocked.value) {
|
||||
return elementCss.value || generatePreviewCss();
|
||||
}
|
||||
|
||||
// If locked, show commented preview of what would be applied
|
||||
const preview = generatePreviewCss();
|
||||
if (!preview) return '';
|
||||
|
||||
|
|
@ -442,27 +347,6 @@ const displayedCss = computed(() => {
|
|||
' */';
|
||||
});
|
||||
|
||||
// Remove the element-specific CSS block to restore inheritance
|
||||
const removeElementBlock = () => {
|
||||
if (!selector.value) return;
|
||||
|
||||
const block = stylesheetStore.extractBlock(selector.value);
|
||||
if (block) {
|
||||
// Escape special regex characters in selector
|
||||
const escaped = selector.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
// Remove the block and any surrounding whitespace
|
||||
stylesheetStore.replaceInCustomCss(
|
||||
new RegExp(`\\n?${escaped}\\s*\\{[^}]*\\}\\n?`),
|
||||
'\n'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const highlightedCss = computed(() => {
|
||||
if (!displayedCss.value) return '';
|
||||
return hljs.highlight(displayedCss.value, { language: 'css' }).value;
|
||||
});
|
||||
|
||||
// Update functions for each property
|
||||
const updateFontFamily = () => {
|
||||
if (!selector.value || !fontFamily.value.value) return;
|
||||
|
|
@ -524,99 +408,39 @@ const applyAllStyles = () => {
|
|||
updatePaddingInner();
|
||||
};
|
||||
|
||||
// Watch for changes
|
||||
watch(() => fontFamily.value.value, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
immediateUpdate(updateFontFamily);
|
||||
});
|
||||
|
||||
watch(() => fontStyle.value.italic, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
immediateUpdate(updateFontStyle);
|
||||
});
|
||||
|
||||
watch(() => fontWeight.value.value, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
immediateUpdate(updateFontWeight);
|
||||
});
|
||||
|
||||
watch(() => fontSize.value.value, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
debouncedUpdate(updateFontSize);
|
||||
});
|
||||
|
||||
watch(() => fontSize.value.unit, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
immediateUpdate(updateFontSize);
|
||||
});
|
||||
|
||||
watch(() => textAlign.value.value, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
immediateUpdate(updateTextAlign);
|
||||
});
|
||||
|
||||
watch(() => color.value.value, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
debouncedUpdate(updateColor);
|
||||
});
|
||||
|
||||
watch(() => background.value.value, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
debouncedUpdate(updateBackground);
|
||||
});
|
||||
|
||||
watch(() => marginOuter.value.value, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
debouncedUpdate(updateMarginOuter);
|
||||
});
|
||||
|
||||
watch(() => marginOuter.value.unit, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
immediateUpdate(updateMarginOuter);
|
||||
});
|
||||
|
||||
watch(() => paddingInner.value.value, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
debouncedUpdate(updatePaddingInner);
|
||||
});
|
||||
|
||||
watch(() => paddingInner.value.unit, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
immediateUpdate(updatePaddingInner);
|
||||
});
|
||||
|
||||
let cssDebounceTimer = null;
|
||||
|
||||
const handleCssInput = (event) => {
|
||||
const newCss = event.target.value;
|
||||
|
||||
if (cssDebounceTimer) {
|
||||
clearTimeout(cssDebounceTimer);
|
||||
}
|
||||
|
||||
cssDebounceTimer = setTimeout(() => {
|
||||
const oldBlock = elementCss.value;
|
||||
if (oldBlock) {
|
||||
stylesheetStore.replaceInCustomCss(oldBlock, newCss);
|
||||
}
|
||||
}, 500);
|
||||
// Helper to reduce watcher boilerplate
|
||||
const watchProp = (getter, updateFn, debounce = false) => {
|
||||
watch(getter, () => {
|
||||
if (isUpdatingFromStore) return;
|
||||
debounce ? debouncedUpdate(updateFn) : immediateUpdate(updateFn);
|
||||
});
|
||||
};
|
||||
|
||||
// Watch isEditable to format when exiting edit mode
|
||||
watch(isEditable, async (newValue, oldValue) => {
|
||||
stylesheetStore.isEditing = newValue;
|
||||
watchProp(() => fontFamily.value.value, updateFontFamily);
|
||||
watchProp(() => fontStyle.value.italic, updateFontStyle);
|
||||
watchProp(() => fontWeight.value.value, updateFontWeight);
|
||||
watchProp(() => fontSize.value.value, updateFontSize, true);
|
||||
watchProp(() => fontSize.value.unit, updateFontSize);
|
||||
watchProp(() => textAlign.value.value, updateTextAlign);
|
||||
watchProp(() => color.value.value, updateColor, true);
|
||||
watchProp(() => background.value.value, updateBackground, true);
|
||||
watchProp(() => marginOuter.value.value, updateMarginOuter, true);
|
||||
watchProp(() => marginOuter.value.unit, updateMarginOuter);
|
||||
watchProp(() => paddingInner.value.value, updatePaddingInner, true);
|
||||
watchProp(() => paddingInner.value.unit, updatePaddingInner);
|
||||
|
||||
// Format when exiting editing mode
|
||||
if (oldValue && !newValue) {
|
||||
await stylesheetStore.formatCustomCss();
|
||||
const handleCssInput = (newCss) => {
|
||||
const oldBlock = elementCss.value;
|
||||
if (oldBlock) {
|
||||
stylesheetStore.replaceInCustomCss(oldBlock, newCss);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Watch stylesheet changes to sync values
|
||||
watch(
|
||||
() => stylesheetStore.customCss,
|
||||
() => {
|
||||
if (visible.value && !isUpdatingFromStore) {
|
||||
if (basePopup.value?.visible && !isUpdatingFromStore) {
|
||||
isUpdatingFromStore = true;
|
||||
loadValuesFromStylesheet();
|
||||
nextTick(() => {
|
||||
|
|
@ -630,8 +454,7 @@ watch(
|
|||
watch(
|
||||
() => stylesheetStore.isEditing,
|
||||
(isEditing, wasEditing) => {
|
||||
// When exiting edit mode, reload values
|
||||
if (visible.value && wasEditing && !isEditing && !isUpdatingFromStore) {
|
||||
if (basePopup.value?.visible && wasEditing && !isEditing && !isUpdatingFromStore) {
|
||||
isUpdatingFromStore = true;
|
||||
loadValuesFromStylesheet();
|
||||
nextTick(() => {
|
||||
|
|
@ -645,63 +468,54 @@ const loadValuesFromStylesheet = () => {
|
|||
if (!selector.value) return;
|
||||
|
||||
try {
|
||||
// Extract font-family
|
||||
const fontFamilyData = stylesheetStore.extractValue(selector.value, 'font-family');
|
||||
if (fontFamilyData) {
|
||||
const value = typeof fontFamilyData === 'string' ? fontFamilyData : fontFamilyData.value;
|
||||
fontFamily.value.value = value.replace(/['"]/g, '');
|
||||
}
|
||||
|
||||
// Extract font-style
|
||||
const fontStyleData = stylesheetStore.extractValue(selector.value, 'font-style');
|
||||
if (fontStyleData) {
|
||||
const value = typeof fontStyleData === 'string' ? fontStyleData : fontStyleData.value;
|
||||
fontStyle.value.italic = value === 'italic';
|
||||
}
|
||||
|
||||
// Extract font-weight
|
||||
const fontWeightData = stylesheetStore.extractValue(selector.value, 'font-weight');
|
||||
if (fontWeightData) {
|
||||
const value = typeof fontWeightData === 'string' ? fontWeightData : fontWeightData.value;
|
||||
fontWeight.value.value = parseInt(value);
|
||||
}
|
||||
|
||||
// Extract font-size
|
||||
const fontSizeData = stylesheetStore.extractValue(selector.value, 'font-size');
|
||||
if (fontSizeData && fontSizeData.value !== undefined) {
|
||||
fontSize.value.value = fontSizeData.value;
|
||||
fontSize.value.unit = fontSizeData.unit;
|
||||
}
|
||||
|
||||
// Extract text-align
|
||||
const textAlignData = stylesheetStore.extractValue(selector.value, 'text-align');
|
||||
if (textAlignData) {
|
||||
const value = typeof textAlignData === 'string' ? textAlignData : textAlignData.value;
|
||||
textAlign.value.value = value;
|
||||
}
|
||||
|
||||
// Extract color
|
||||
const colorData = stylesheetStore.extractValue(selector.value, 'color');
|
||||
if (colorData) {
|
||||
const value = typeof colorData === 'string' ? colorData : colorData.value;
|
||||
color.value.value = value;
|
||||
}
|
||||
|
||||
// Extract background
|
||||
const backgroundData = stylesheetStore.extractValue(selector.value, 'background');
|
||||
if (backgroundData) {
|
||||
const value = typeof backgroundData === 'string' ? backgroundData : backgroundData.value;
|
||||
background.value.value = value;
|
||||
}
|
||||
|
||||
// Extract margin
|
||||
const marginData = stylesheetStore.extractValue(selector.value, 'margin');
|
||||
if (marginData && marginData.value !== undefined) {
|
||||
marginOuter.value.value = marginData.value;
|
||||
marginOuter.value.unit = marginData.unit;
|
||||
}
|
||||
|
||||
// Extract padding
|
||||
const paddingData = stylesheetStore.extractValue(selector.value, 'padding');
|
||||
if (paddingData && paddingData.value !== undefined) {
|
||||
paddingInner.value.value = paddingData.value;
|
||||
|
|
@ -713,12 +527,10 @@ const loadValuesFromStylesheet = () => {
|
|||
};
|
||||
|
||||
const open = (element, event, count = null) => {
|
||||
// Block all watchers during initialization
|
||||
isUpdatingFromStore = true;
|
||||
|
||||
selectedElement.value = element;
|
||||
selector.value = getSelectorFromElement(element);
|
||||
position.value = calculatePosition(event);
|
||||
|
||||
// Store instance count if provided, otherwise calculate it
|
||||
elementInstanceCount.value = count !== null ? count : getInstanceCount(selector.value);
|
||||
|
|
@ -727,61 +539,24 @@ const open = (element, event, count = null) => {
|
|||
const blockState = stylesheetStore.getBlockState(selector.value);
|
||||
|
||||
if (blockState === 'active') {
|
||||
// Block exists and is active (not commented) → unlocked
|
||||
inheritanceLocked.value = false;
|
||||
} else if (blockState === 'commented') {
|
||||
// Block exists but is commented → locked with custom values
|
||||
inheritanceLocked.value = true;
|
||||
basePopup.value.inheritanceLocked = false;
|
||||
} else {
|
||||
// No block → locked with inherited values
|
||||
inheritanceLocked.value = true;
|
||||
basePopup.value.inheritanceLocked = true;
|
||||
}
|
||||
|
||||
// Load values from stylesheet (includes commented blocks)
|
||||
loadValuesFromStylesheet();
|
||||
|
||||
visible.value = true;
|
||||
// Open popup (sets visible, position, inits Coloris)
|
||||
basePopup.value.open(event);
|
||||
|
||||
// Re-enable watchers after initialization (use nextTick to ensure watchers see the flag)
|
||||
nextTick(() => {
|
||||
isUpdatingFromStore = false;
|
||||
});
|
||||
|
||||
// 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);
|
||||
};
|
||||
|
||||
const close = () => {
|
||||
visible.value = false;
|
||||
isEditable.value = false;
|
||||
basePopup.value?.close();
|
||||
selector.value = '';
|
||||
selectedElement.value = null;
|
||||
emit('close');
|
||||
|
|
@ -795,8 +570,7 @@ const handleIframeClick = (event, targetElement = null, elementCount = null) =>
|
|||
return;
|
||||
}
|
||||
|
||||
// If popup is already open, just close it instead of opening a new one
|
||||
if (visible.value) {
|
||||
if (basePopup.value?.visible) {
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
|
@ -807,51 +581,35 @@ const handleIframeClick = (event, targetElement = null, elementCount = null) =>
|
|||
const toggleInheritance = () => {
|
||||
const blockState = stylesheetStore.getBlockState(selector.value);
|
||||
|
||||
if (inheritanceLocked.value && blockState === 'commented') {
|
||||
// Case 1: Locked with commented block → Uncomment to unlock
|
||||
if (basePopup.value.inheritanceLocked && blockState === 'commented') {
|
||||
stylesheetStore.uncommentCssBlock(selector.value);
|
||||
inheritanceLocked.value = false;
|
||||
} else if (inheritanceLocked.value && blockState === 'none') {
|
||||
// Case 2: Locked with no custom CSS → Capture computed values and create block
|
||||
basePopup.value.inheritanceLocked = false;
|
||||
} else if (basePopup.value.inheritanceLocked && blockState === 'none') {
|
||||
if (selectedElement.value && props.iframeRef && props.iframeRef.contentWindow) {
|
||||
const computed = props.iframeRef.contentWindow.getComputedStyle(selectedElement.value);
|
||||
|
||||
// Update fields with computed values before creating the block
|
||||
isUpdatingFromStore = true;
|
||||
|
||||
// Font family
|
||||
fontFamily.value.value = computed.fontFamily.replace(/['"]/g, '').split(',')[0].trim();
|
||||
|
||||
// Font style
|
||||
fontStyle.value.italic = computed.fontStyle === 'italic';
|
||||
|
||||
// Font weight
|
||||
fontWeight.value.value = parseInt(computed.fontWeight);
|
||||
|
||||
// Font size
|
||||
const fontSizeMatch = computed.fontSize.match(/([\d.]+)(px|rem|em|pt)/);
|
||||
if (fontSizeMatch) {
|
||||
fontSize.value.value = parseFloat(fontSizeMatch[1]);
|
||||
fontSize.value.unit = fontSizeMatch[2];
|
||||
}
|
||||
|
||||
// Text align
|
||||
textAlign.value.value = computed.textAlign;
|
||||
|
||||
// Color
|
||||
color.value.value = computed.color;
|
||||
|
||||
// Background
|
||||
background.value.value = computed.backgroundColor;
|
||||
|
||||
// Margin (take the top margin)
|
||||
const marginMatch = computed.marginTop.match(/([\d.]+)(px|mm|pt)/);
|
||||
if (marginMatch) {
|
||||
marginOuter.value.value = parseFloat(marginMatch[1]);
|
||||
marginOuter.value.unit = marginMatch[2];
|
||||
}
|
||||
|
||||
// Padding (take the top padding)
|
||||
const paddingMatch = computed.paddingTop.match(/([\d.]+)(px|mm|pt)/);
|
||||
if (paddingMatch) {
|
||||
paddingInner.value.value = parseFloat(paddingMatch[1]);
|
||||
|
|
@ -861,13 +619,11 @@ const toggleInheritance = () => {
|
|||
isUpdatingFromStore = false;
|
||||
}
|
||||
|
||||
// Now create the block with captured values
|
||||
applyAllStyles();
|
||||
inheritanceLocked.value = false;
|
||||
} else if (!inheritanceLocked.value && blockState === 'active') {
|
||||
// Case 3: Unlocked with active block → Comment to lock
|
||||
basePopup.value.inheritanceLocked = false;
|
||||
} else if (!basePopup.value.inheritanceLocked && blockState === 'active') {
|
||||
stylesheetStore.commentCssBlock(selector.value);
|
||||
inheritanceLocked.value = true;
|
||||
basePopup.value.inheritanceLocked = true;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue