🐛 Fix copy/paste not working on follow up pastes

This commit is contained in:
Belén Albeza
2025-07-23 13:15:15 +02:00
parent 098fd9fb0f
commit af5b942e05
7 changed files with 121 additions and 88 deletions

View File

@@ -10,7 +10,7 @@ import { getFills } from "./Color.js";
const DEFAULT_FONT_SIZE = "16px";
const DEFAULT_LINE_HEIGHT = "1.2";
const DEFAULT_FONT_WEIGHT = "400";
/**
* Merges two style declarations. `source` -> `target`.
*
@@ -55,7 +55,7 @@ let inertElement = null;
* Resets the style declaration of the inert
* element.
*/
function resetInertElement() {
export function resetInertElement() {
if (!inertElement) throw new Error("Invalid inert element");
resetStyleDeclaration(inertElement.style);
return inertElement;
@@ -94,11 +94,21 @@ function getStyleDefaultsDeclaration() {
* @returns {CSSStyleDeclaration}
*/
export function getComputedStyle(element) {
if (typeof window !== "undefined" && window.getComputedStyle) {
const inertElement = getInertElement();
const computedStyle = window.getComputedStyle(element);
inertElement.style = computedStyle;
return inertElement.style;
}
return getComputedStylePolyfill(element);
}
export function getComputedStylePolyfill(element) {
const inertElement = getInertElement();
let currentElement = element;
while (currentElement) {
// This is better but it doesn't work in JSDOM.
// for (const styleName of currentElement.style) {
for (let index = 0; index < currentElement.style.length; index++) {
const styleName = currentElement.style.item(index);
const currentValue = inertElement.style.getPropertyValue(styleName);
@@ -159,6 +169,11 @@ export function normalizeStyles(
styleDeclaration.setProperty("font-size", DEFAULT_FONT_SIZE);
}
const fontWeight = styleDeclaration.getPropertyValue("font-weight");
if (!fontWeight || fontWeight === "0") {
styleDeclaration.setProperty("font-weight", DEFAULT_FONT_WEIGHT);
}
const lineHeight = styleDeclaration.getPropertyValue("line-height");
if (!lineHeight || lineHeight === "" || !lineHeight.endsWith("px")) {
// TODO: Podríamos convertir unidades en decimales.