0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-06 14:50:20 -05:00

Merge pull request #5377 from penpot/azazeln28-fix-text-editor-issues

🐛 Fix text editor issues
This commit is contained in:
Andrey Antukh 2024-11-27 09:08:39 +01:00 committed by GitHub
commit 30a06249ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 6 deletions

View file

@ -223,7 +223,9 @@ export class TextEditor extends EventTarget {
* @param {FocusEvent} e
*/
#onFocus = (e) => {
this.#selectionController.restoreSelection();
if (!this.#selectionController.restoreSelection()) {
this.selectAll();
}
if (this.#selectionImposterElement) {
this.#selectionImposterElement.replaceChildren();
}

View file

@ -49,9 +49,10 @@ export function mapContentFragmentFromDocument(document, root, styleDefaults) {
}
}
currentParagraph.appendChild(
createInline(new Text(currentNode.nodeValue), currentStyle)
);
const inline = createInline(new Text(currentNode.nodeValue), currentStyle);
const fontSize = inline.style.getPropertyValue("font-size");
if (!fontSize) console.warn("font-size", fontSize);
currentParagraph.appendChild(inline);
currentNode = nodeIterator.nextNode();
}

View file

@ -173,13 +173,31 @@ export function setStyle(element, styleName, styleValue, styleUnit) {
return element;
}
/**
* Returns the value of the font size
*
* @param {number} styleValueAsNumber
* @param {string} styleValue
* @returns {string}
*/
function getStyleFontSize(styleValueAsNumber, styleValue) {
if (styleValue.endsWith("pt")) {
return (styleValueAsNumber * 1.3333).toFixed();
} else if (styleValue.endsWith("em")) {
return (styleValueAsNumber * baseSize).toFixed();
} else if (styleValue.endsWith("%")) {
return ((styleValueAsNumber / 100) * baseSize).toFixed();
}
return styleValueAsNumber.toFixed();
}
/**
* Returns the value of a style from a declaration.
*
* @param {CSSStyleDeclaration} style
* @param {string} styleName
* @param {string|undefined} [styleUnit]
* @returns {*}
* @returns {string}
*/
export function getStyleFromDeclaration(style, styleName, styleUnit) {
if (styleName.startsWith("--")) {
@ -189,7 +207,14 @@ export function getStyleFromDeclaration(style, styleName, styleUnit) {
if (styleValue.endsWith(styleUnit)) {
return styleValue.slice(0, -styleUnit.length);
}
return styleValue;
const styleValueAsNumber = parseFloat(styleValue);
if (styleName === "font-size") {
return getStyleFontSize(styleValueAsNumber, styleValue);
}
if (Number.isNaN(styleValueAsNumber)) {
return styleValue;
}
return styleValueAsNumber.toFixed();
}
/**