mirror of
https://github.com/penpot/penpot.git
synced 2025-12-12 06:24:17 +01:00
🐛 Fix copy/paste issues
This commit is contained in:
@@ -75,6 +75,17 @@ function getInertElement() {
|
||||
return inertElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a default declaration.
|
||||
*
|
||||
* @returns {CSSStyleDeclaration}
|
||||
*/
|
||||
function getStyleDefaultsDeclaration() {
|
||||
const element = getInertElement();
|
||||
resetInertElement();
|
||||
return element.style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the styles of an element the same way `window.getComputedStyle` does.
|
||||
*
|
||||
@@ -115,22 +126,26 @@ export function getComputedStyle(element) {
|
||||
* CSS properties like `font-family` or some CSS variables.
|
||||
*
|
||||
* @param {Node} node
|
||||
* @param {CSSStyleDeclaration} styleDefaults
|
||||
* @param {CSSStyleDeclaration} [styleDefaults]
|
||||
* @returns {CSSStyleDeclaration}
|
||||
*/
|
||||
export function normalizeStyles(node, styleDefaults) {
|
||||
export function normalizeStyles(node, styleDefaults = getStyleDefaultsDeclaration()) {
|
||||
const styleDeclaration = mergeStyleDeclarations(
|
||||
styleDefaults,
|
||||
getComputedStyle(node.parentElement)
|
||||
);
|
||||
|
||||
// If there's a color property, we should convert it to
|
||||
// a --fills CSS variable property.
|
||||
const fills = styleDeclaration.getPropertyValue("--fills");
|
||||
const color = styleDeclaration.getPropertyValue("color");
|
||||
if (color && !fills) {
|
||||
if (color) {
|
||||
styleDeclaration.removeProperty("color");
|
||||
styleDeclaration.setProperty("--fills", getFills(color));
|
||||
} else {
|
||||
styleDeclaration.setProperty("--fills", fills);
|
||||
}
|
||||
|
||||
// If there's a font-family property and not a --font-id, then
|
||||
// we remove the font-family because it will not work.
|
||||
const fontFamily = styleDeclaration.getPropertyValue("font-family");
|
||||
@@ -145,8 +160,15 @@ export function normalizeStyles(node, styleDefaults) {
|
||||
}
|
||||
|
||||
const lineHeight = styleDeclaration.getPropertyValue("line-height");
|
||||
if (!lineHeight || lineHeight === "") {
|
||||
if (!lineHeight || lineHeight === "" || !lineHeight.endsWith("px")) {
|
||||
// TODO: Podríamos convertir unidades en decimales.
|
||||
styleDeclaration.setProperty("line-height", DEFAULT_LINE_HEIGHT);
|
||||
} else if (lineHeight.endsWith("px")) {
|
||||
const fontSize = styleDeclaration.getPropertyValue("font-size");
|
||||
styleDeclaration.setProperty(
|
||||
"line-height",
|
||||
parseFloat(lineHeight) / parseFloat(fontSize),
|
||||
);
|
||||
}
|
||||
return styleDeclaration
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user